blob: 9a8201eb5b829911b9c10721ef28d9dea714349e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
454static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
455static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
456static char_u *string_quote(char_u *str, int function);
457static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
458static int find_internal_func(char_u *name);
459static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
460static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void emsg_funcname(char *ermsg, char_u *name);
462static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_abs(typval_T *argvars, typval_T *rettv);
466static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_add(typval_T *argvars, typval_T *rettv);
469static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
470static void f_and(typval_T *argvars, typval_T *rettv);
471static void f_append(typval_T *argvars, typval_T *rettv);
472static void f_argc(typval_T *argvars, typval_T *rettv);
473static void f_argidx(typval_T *argvars, typval_T *rettv);
474static void f_arglistid(typval_T *argvars, typval_T *rettv);
475static void f_argv(typval_T *argvars, typval_T *rettv);
476static void f_assert_equal(typval_T *argvars, typval_T *rettv);
477static void f_assert_exception(typval_T *argvars, typval_T *rettv);
478static void f_assert_fails(typval_T *argvars, typval_T *rettv);
479static void f_assert_false(typval_T *argvars, typval_T *rettv);
480static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_asin(typval_T *argvars, typval_T *rettv);
483static void f_atan(typval_T *argvars, typval_T *rettv);
484static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000485#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100486static void f_browse(typval_T *argvars, typval_T *rettv);
487static void f_browsedir(typval_T *argvars, typval_T *rettv);
488static void f_bufexists(typval_T *argvars, typval_T *rettv);
489static void f_buflisted(typval_T *argvars, typval_T *rettv);
490static void f_bufloaded(typval_T *argvars, typval_T *rettv);
491static void f_bufname(typval_T *argvars, typval_T *rettv);
492static void f_bufnr(typval_T *argvars, typval_T *rettv);
493static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
494static void f_byte2line(typval_T *argvars, typval_T *rettv);
495static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
496static void f_byteidx(typval_T *argvars, typval_T *rettv);
497static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
498static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100500static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000501#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100502static void f_changenr(typval_T *argvars, typval_T *rettv);
503static void f_char2nr(typval_T *argvars, typval_T *rettv);
504static void f_cindent(typval_T *argvars, typval_T *rettv);
505static void f_clearmatches(typval_T *argvars, typval_T *rettv);
506static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000507#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100508static void f_complete(typval_T *argvars, typval_T *rettv);
509static void f_complete_add(typval_T *argvars, typval_T *rettv);
510static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_confirm(typval_T *argvars, typval_T *rettv);
513static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000514#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_cos(typval_T *argvars, typval_T *rettv);
516static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#endif
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100518#ifdef FEAT_CHANNEL
519static void f_connect(typval_T *argvars, typval_T *rettv);
520#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_count(typval_T *argvars, typval_T *rettv);
522static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
523static void f_cursor(typval_T *argsvars, typval_T *rettv);
524static void f_deepcopy(typval_T *argvars, typval_T *rettv);
525static void f_delete(typval_T *argvars, typval_T *rettv);
526static void f_did_filetype(typval_T *argvars, typval_T *rettv);
527static void f_diff_filler(typval_T *argvars, typval_T *rettv);
528static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100529#ifdef FEAT_CHANNEL
530static void f_disconnect(typval_T *argvars, typval_T *rettv);
531#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_empty(typval_T *argvars, typval_T *rettv);
533static void f_escape(typval_T *argvars, typval_T *rettv);
534static void f_eval(typval_T *argvars, typval_T *rettv);
535static void f_eventhandler(typval_T *argvars, typval_T *rettv);
536static void f_executable(typval_T *argvars, typval_T *rettv);
537static void f_exepath(typval_T *argvars, typval_T *rettv);
538static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200539#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200541#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_expand(typval_T *argvars, typval_T *rettv);
543static void f_extend(typval_T *argvars, typval_T *rettv);
544static void f_feedkeys(typval_T *argvars, typval_T *rettv);
545static void f_filereadable(typval_T *argvars, typval_T *rettv);
546static void f_filewritable(typval_T *argvars, typval_T *rettv);
547static void f_filter(typval_T *argvars, typval_T *rettv);
548static void f_finddir(typval_T *argvars, typval_T *rettv);
549static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000550#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_float2nr(typval_T *argvars, typval_T *rettv);
552static void f_floor(typval_T *argvars, typval_T *rettv);
553static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000554#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100555static void f_fnameescape(typval_T *argvars, typval_T *rettv);
556static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
557static void f_foldclosed(typval_T *argvars, typval_T *rettv);
558static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
559static void f_foldlevel(typval_T *argvars, typval_T *rettv);
560static void f_foldtext(typval_T *argvars, typval_T *rettv);
561static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
562static void f_foreground(typval_T *argvars, typval_T *rettv);
563static void f_function(typval_T *argvars, typval_T *rettv);
564static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
565static void f_get(typval_T *argvars, typval_T *rettv);
566static void f_getbufline(typval_T *argvars, typval_T *rettv);
567static void f_getbufvar(typval_T *argvars, typval_T *rettv);
568static void f_getchar(typval_T *argvars, typval_T *rettv);
569static void f_getcharmod(typval_T *argvars, typval_T *rettv);
570static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
571static void f_getcmdline(typval_T *argvars, typval_T *rettv);
572static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
573static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
574static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
575static void f_getcwd(typval_T *argvars, typval_T *rettv);
576static void f_getfontname(typval_T *argvars, typval_T *rettv);
577static void f_getfperm(typval_T *argvars, typval_T *rettv);
578static void f_getfsize(typval_T *argvars, typval_T *rettv);
579static void f_getftime(typval_T *argvars, typval_T *rettv);
580static void f_getftype(typval_T *argvars, typval_T *rettv);
581static void f_getline(typval_T *argvars, typval_T *rettv);
582static void f_getmatches(typval_T *argvars, typval_T *rettv);
583static void f_getpid(typval_T *argvars, typval_T *rettv);
584static void f_getcurpos(typval_T *argvars, typval_T *rettv);
585static void f_getpos(typval_T *argvars, typval_T *rettv);
586static void f_getqflist(typval_T *argvars, typval_T *rettv);
587static void f_getreg(typval_T *argvars, typval_T *rettv);
588static void f_getregtype(typval_T *argvars, typval_T *rettv);
589static void f_gettabvar(typval_T *argvars, typval_T *rettv);
590static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
591static void f_getwinposx(typval_T *argvars, typval_T *rettv);
592static void f_getwinposy(typval_T *argvars, typval_T *rettv);
593static void f_getwinvar(typval_T *argvars, typval_T *rettv);
594static void f_glob(typval_T *argvars, typval_T *rettv);
595static void f_globpath(typval_T *argvars, typval_T *rettv);
596static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
597static void f_has(typval_T *argvars, typval_T *rettv);
598static void f_has_key(typval_T *argvars, typval_T *rettv);
599static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
600static void f_hasmapto(typval_T *argvars, typval_T *rettv);
601static void f_histadd(typval_T *argvars, typval_T *rettv);
602static void f_histdel(typval_T *argvars, typval_T *rettv);
603static void f_histget(typval_T *argvars, typval_T *rettv);
604static void f_histnr(typval_T *argvars, typval_T *rettv);
605static void f_hlID(typval_T *argvars, typval_T *rettv);
606static void f_hlexists(typval_T *argvars, typval_T *rettv);
607static void f_hostname(typval_T *argvars, typval_T *rettv);
608static void f_iconv(typval_T *argvars, typval_T *rettv);
609static void f_indent(typval_T *argvars, typval_T *rettv);
610static void f_index(typval_T *argvars, typval_T *rettv);
611static void f_input(typval_T *argvars, typval_T *rettv);
612static void f_inputdialog(typval_T *argvars, typval_T *rettv);
613static void f_inputlist(typval_T *argvars, typval_T *rettv);
614static void f_inputrestore(typval_T *argvars, typval_T *rettv);
615static void f_inputsave(typval_T *argvars, typval_T *rettv);
616static void f_inputsecret(typval_T *argvars, typval_T *rettv);
617static void f_insert(typval_T *argvars, typval_T *rettv);
618static void f_invert(typval_T *argvars, typval_T *rettv);
619static void f_isdirectory(typval_T *argvars, typval_T *rettv);
620static void f_islocked(typval_T *argvars, typval_T *rettv);
621static void f_items(typval_T *argvars, typval_T *rettv);
622static void f_join(typval_T *argvars, typval_T *rettv);
623static void f_jsondecode(typval_T *argvars, typval_T *rettv);
624static void f_jsonencode(typval_T *argvars, typval_T *rettv);
625static void f_keys(typval_T *argvars, typval_T *rettv);
626static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
627static void f_len(typval_T *argvars, typval_T *rettv);
628static void f_libcall(typval_T *argvars, typval_T *rettv);
629static void f_libcallnr(typval_T *argvars, typval_T *rettv);
630static void f_line(typval_T *argvars, typval_T *rettv);
631static void f_line2byte(typval_T *argvars, typval_T *rettv);
632static void f_lispindent(typval_T *argvars, typval_T *rettv);
633static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000634#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100635static void f_log(typval_T *argvars, typval_T *rettv);
636static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000637#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200638#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_map(typval_T *argvars, typval_T *rettv);
642static void f_maparg(typval_T *argvars, typval_T *rettv);
643static void f_mapcheck(typval_T *argvars, typval_T *rettv);
644static void f_match(typval_T *argvars, typval_T *rettv);
645static void f_matchadd(typval_T *argvars, typval_T *rettv);
646static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
647static void f_matcharg(typval_T *argvars, typval_T *rettv);
648static void f_matchdelete(typval_T *argvars, typval_T *rettv);
649static void f_matchend(typval_T *argvars, typval_T *rettv);
650static void f_matchlist(typval_T *argvars, typval_T *rettv);
651static void f_matchstr(typval_T *argvars, typval_T *rettv);
652static void f_max(typval_T *argvars, typval_T *rettv);
653static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000654#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000656#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100658#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
662static void f_nr2char(typval_T *argvars, typval_T *rettv);
663static void f_or(typval_T *argvars, typval_T *rettv);
664static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100665#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100666static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100667#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
672static void f_printf(typval_T *argvars, typval_T *rettv);
673static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200674#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200676#endif
677#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_range(typval_T *argvars, typval_T *rettv);
681static void f_readfile(typval_T *argvars, typval_T *rettv);
682static void f_reltime(typval_T *argvars, typval_T *rettv);
683static void f_reltimestr(typval_T *argvars, typval_T *rettv);
684static void f_remote_expr(typval_T *argvars, typval_T *rettv);
685static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
686static void f_remote_peek(typval_T *argvars, typval_T *rettv);
687static void f_remote_read(typval_T *argvars, typval_T *rettv);
688static void f_remote_send(typval_T *argvars, typval_T *rettv);
689static void f_remove(typval_T *argvars, typval_T *rettv);
690static void f_rename(typval_T *argvars, typval_T *rettv);
691static void f_repeat(typval_T *argvars, typval_T *rettv);
692static void f_resolve(typval_T *argvars, typval_T *rettv);
693static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_screenattr(typval_T *argvars, typval_T *rettv);
698static void f_screenchar(typval_T *argvars, typval_T *rettv);
699static void f_screencol(typval_T *argvars, typval_T *rettv);
700static void f_screenrow(typval_T *argvars, typval_T *rettv);
701static void f_search(typval_T *argvars, typval_T *rettv);
702static void f_searchdecl(typval_T *argvars, typval_T *rettv);
703static void f_searchpair(typval_T *argvars, typval_T *rettv);
704static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
705static void f_searchpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3b5f9292016-01-28 22:37:01 +0100706#ifdef FEAT_CHANNEL
707static void f_sendexpr(typval_T *argvars, typval_T *rettv);
708static void f_sendraw(typval_T *argvars, typval_T *rettv);
709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_server2client(typval_T *argvars, typval_T *rettv);
711static void f_serverlist(typval_T *argvars, typval_T *rettv);
712static void f_setbufvar(typval_T *argvars, typval_T *rettv);
713static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
714static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
715static void f_setline(typval_T *argvars, typval_T *rettv);
716static void f_setloclist(typval_T *argvars, typval_T *rettv);
717static void f_setmatches(typval_T *argvars, typval_T *rettv);
718static void f_setpos(typval_T *argvars, typval_T *rettv);
719static void f_setqflist(typval_T *argvars, typval_T *rettv);
720static void f_setreg(typval_T *argvars, typval_T *rettv);
721static void f_settabvar(typval_T *argvars, typval_T *rettv);
722static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
723static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100724#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100725static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100726#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_shellescape(typval_T *argvars, typval_T *rettv);
728static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
729static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000730#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100731static void f_sin(typval_T *argvars, typval_T *rettv);
732static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_sort(typval_T *argvars, typval_T *rettv);
735static void f_soundfold(typval_T *argvars, typval_T *rettv);
736static void f_spellbadword(typval_T *argvars, typval_T *rettv);
737static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
738static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000739#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100740static void f_sqrt(typval_T *argvars, typval_T *rettv);
741static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_str2nr(typval_T *argvars, typval_T *rettv);
744static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000745#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100746static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000747#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_stridx(typval_T *argvars, typval_T *rettv);
749static void f_string(typval_T *argvars, typval_T *rettv);
750static void f_strlen(typval_T *argvars, typval_T *rettv);
751static void f_strpart(typval_T *argvars, typval_T *rettv);
752static void f_strridx(typval_T *argvars, typval_T *rettv);
753static void f_strtrans(typval_T *argvars, typval_T *rettv);
754static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
755static void f_strwidth(typval_T *argvars, typval_T *rettv);
756static void f_submatch(typval_T *argvars, typval_T *rettv);
757static void f_substitute(typval_T *argvars, typval_T *rettv);
758static void f_synID(typval_T *argvars, typval_T *rettv);
759static void f_synIDattr(typval_T *argvars, typval_T *rettv);
760static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
761static void f_synstack(typval_T *argvars, typval_T *rettv);
762static void f_synconcealed(typval_T *argvars, typval_T *rettv);
763static void f_system(typval_T *argvars, typval_T *rettv);
764static void f_systemlist(typval_T *argvars, typval_T *rettv);
765static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
766static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
767static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
768static void f_taglist(typval_T *argvars, typval_T *rettv);
769static void f_tagfiles(typval_T *argvars, typval_T *rettv);
770static void f_tempname(typval_T *argvars, typval_T *rettv);
771static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200772#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_tan(typval_T *argvars, typval_T *rettv);
774static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200775#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_tolower(typval_T *argvars, typval_T *rettv);
777static void f_toupper(typval_T *argvars, typval_T *rettv);
778static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000779#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000781#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_type(typval_T *argvars, typval_T *rettv);
783static void f_undofile(typval_T *argvars, typval_T *rettv);
784static void f_undotree(typval_T *argvars, typval_T *rettv);
785static void f_uniq(typval_T *argvars, typval_T *rettv);
786static void f_values(typval_T *argvars, typval_T *rettv);
787static void f_virtcol(typval_T *argvars, typval_T *rettv);
788static void f_visualmode(typval_T *argvars, typval_T *rettv);
789static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
790static void f_winbufnr(typval_T *argvars, typval_T *rettv);
791static void f_wincol(typval_T *argvars, typval_T *rettv);
792static void f_winheight(typval_T *argvars, typval_T *rettv);
793static void f_winline(typval_T *argvars, typval_T *rettv);
794static void f_winnr(typval_T *argvars, typval_T *rettv);
795static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
796static void f_winrestview(typval_T *argvars, typval_T *rettv);
797static void f_winsaveview(typval_T *argvars, typval_T *rettv);
798static void f_winwidth(typval_T *argvars, typval_T *rettv);
799static void f_writefile(typval_T *argvars, typval_T *rettv);
800static void f_wordcount(typval_T *argvars, typval_T *rettv);
801static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000802
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
804static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
805static int get_env_len(char_u **arg);
806static int get_id_len(char_u **arg);
807static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
808static 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 +0000809#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
810#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
811 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100812static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
813static int eval_isnamec(int c);
814static int eval_isnamec1(int c);
815static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
816static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
817static typval_T *alloc_tv(void);
818static typval_T *alloc_string_tv(char_u *string);
819static void init_tv(typval_T *varp);
820static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100821#ifdef FEAT_FLOAT
822static float_T get_tv_float(typval_T *varp);
823#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100824static linenr_T get_tv_lnum(typval_T *argvars);
825static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
826static char_u *get_tv_string(typval_T *varp);
827static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
828static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
829static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
830static hashtab_T *find_var_ht(char_u *name, char_u **varname);
831static funccall_T *get_funccal(void);
832static void vars_clear_ext(hashtab_T *ht, int free_val);
833static void delete_var(hashtab_T *ht, hashitem_T *hi);
834static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
835static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
836static void set_var(char_u *name, typval_T *varp, int copy);
837static int var_check_ro(int flags, char_u *name, int use_gettext);
838static int var_check_fixed(int flags, char_u *name, int use_gettext);
839static int var_check_func_name(char_u *name, int new_var);
840static int valid_varname(char_u *varname);
841static int tv_check_lock(int lock, char_u *name, int use_gettext);
842static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
843static char_u *find_option_end(char_u **arg, int *opt_flags);
844static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
845static int eval_fname_script(char_u *p);
846static int eval_fname_sid(char_u *p);
847static void list_func_head(ufunc_T *fp, int indent);
848static ufunc_T *find_func(char_u *name);
849static int function_exists(char_u *name);
850static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static void func_do_profile(ufunc_T *fp);
853static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
854static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000855static int
856# ifdef __BORLANDC__
857 _RTLENTRYF
858# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000860static int
861# ifdef __BORLANDC__
862 _RTLENTRYF
863# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100866static int script_autoload(char_u *name, int reload);
867static char_u *autoload_name(char_u *name);
868static void cat_func_name(char_u *buf, ufunc_T *fp);
869static void func_free(ufunc_T *fp);
870static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
871static int can_free_funccal(funccall_T *fc, int copyID) ;
872static void free_funccal(funccall_T *fc, int free_val);
873static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
874static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
875static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
876static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
877static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
878static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
879static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
880static int write_list(FILE *fd, list_T *list, int binary);
881static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000882
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883
884#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885static int compare_func_name(const void *s1, const void *s2);
886static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200887#endif
888
Bram Moolenaar33570922005-01-25 22:26:29 +0000889/*
890 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000891 */
892 void
893eval_init()
894{
Bram Moolenaar33570922005-01-25 22:26:29 +0000895 int i;
896 struct vimvar *p;
897
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200898 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
899 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200900 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000901 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000902 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000903
904 for (i = 0; i < VV_LEN; ++i)
905 {
906 p = &vimvars[i];
907 STRCPY(p->vv_di.di_key, p->vv_name);
908 if (p->vv_flags & VV_RO)
909 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
910 else if (p->vv_flags & VV_RO_SBX)
911 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
912 else
913 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000914
915 /* add to v: scope dict, unless the value is not always available */
916 if (p->vv_type != VAR_UNKNOWN)
917 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000918 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000919 /* add to compat scope dict */
920 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000921 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000922 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100923 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200924 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100925 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100926
927 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
928 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
929 set_vim_var_nr(VV_NONE, VVAL_NONE);
930 set_vim_var_nr(VV_NULL, VVAL_NULL);
931
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200932 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200933
934#ifdef EBCDIC
935 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100936 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200937 */
938 sortFunctions();
939#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000940}
941
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000942#if defined(EXITFREE) || defined(PROTO)
943 void
944eval_clear()
945{
946 int i;
947 struct vimvar *p;
948
949 for (i = 0; i < VV_LEN; ++i)
950 {
951 p = &vimvars[i];
952 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000953 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000954 vim_free(p->vv_str);
955 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000956 }
957 else if (p->vv_di.di_tv.v_type == VAR_LIST)
958 {
959 list_unref(p->vv_list);
960 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000961 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000962 }
963 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000964 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000965 hash_clear(&compat_hashtab);
966
Bram Moolenaard9fba312005-06-26 22:34:35 +0000967 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100968# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200969 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100970# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000971
972 /* global variables */
973 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000974
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000975 /* autoloaded script names */
976 ga_clear_strings(&ga_loaded);
977
Bram Moolenaarcca74132013-09-25 21:00:28 +0200978 /* Script-local variables. First clear all the variables and in a second
979 * loop free the scriptvar_T, because a variable in one script might hold
980 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200981 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200982 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200983 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200984 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200985 ga_clear(&ga_scripts);
986
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000987 /* unreferenced lists and dicts */
988 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000989
990 /* functions */
991 free_all_functions();
992 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000993}
994#endif
995
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * Return the name of the executed function.
998 */
999 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001000func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001002 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003}
1004
1005/*
1006 * Return the address holding the next breakpoint line for a funccall cookie.
1007 */
1008 linenr_T *
1009func_breakpoint(cookie)
1010 void *cookie;
1011{
Bram Moolenaar33570922005-01-25 22:26:29 +00001012 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
1014
1015/*
1016 * Return the address holding the debug tick for a funccall cookie.
1017 */
1018 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001019func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar33570922005-01-25 22:26:29 +00001021 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/*
1025 * Return the nesting level for a funccall cookie.
1026 */
1027 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001028func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001034funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001036/* pointer to list of previously used funccal, still around because some
1037 * item in it is still being used. */
1038funccall_T *previous_funccal = NULL;
1039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040/*
1041 * Return TRUE when a function was ended by a ":return" command.
1042 */
1043 int
1044current_func_returned()
1045{
1046 return current_funccal->returned;
1047}
1048
1049
1050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 * Set an internal variable to a string value. Creates the variable if it does
1052 * not already exist.
1053 */
1054 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001055set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001057 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001058 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
1060 val = vim_strsave(value);
1061 if (val != NULL)
1062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001063 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001064 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001066 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001067 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 }
1070}
1071
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001072static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001074static char_u *redir_endp = NULL;
1075static char_u *redir_varname = NULL;
1076
1077/*
1078 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001079 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001080 * Returns OK if successfully completed the setup. FAIL otherwise.
1081 */
1082 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001083var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084{
1085 int save_emsg;
1086 int err;
1087 typval_T tv;
1088
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001089 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001090 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 {
1092 EMSG(_(e_invarg));
1093 return FAIL;
1094 }
1095
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001096 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097 redir_varname = vim_strsave(name);
1098 if (redir_varname == NULL)
1099 return FAIL;
1100
1101 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1102 if (redir_lval == NULL)
1103 {
1104 var_redir_stop();
1105 return FAIL;
1106 }
1107
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108 /* The output is stored in growarray "redir_ga" until redirection ends. */
1109 ga_init2(&redir_ga, (int)sizeof(char), 500);
1110
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001112 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001113 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1115 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001116 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 if (redir_endp != NULL && *redir_endp != NUL)
1118 /* Trailing characters are present after the variable name */
1119 EMSG(_(e_trailing));
1120 else
1121 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001122 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 var_redir_stop();
1124 return FAIL;
1125 }
1126
1127 /* check if we can write to the variable: set it to or append an empty
1128 * string */
1129 save_emsg = did_emsg;
1130 did_emsg = FALSE;
1131 tv.v_type = VAR_STRING;
1132 tv.vval.v_string = (char_u *)"";
1133 if (append)
1134 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1135 else
1136 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001137 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001139 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (err)
1141 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 var_redir_stop();
1144 return FAIL;
1145 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146
1147 return OK;
1148}
1149
1150/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151 * Append "value[value_len]" to the variable set by var_redir_start().
1152 * The actual appending is postponed until redirection ends, because the value
1153 * appended may in fact be the string we write to, changing it may cause freed
1154 * memory to be used:
1155 * :redir => foo
1156 * :let foo
1157 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 */
1159 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001160var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001162 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163
1164 if (redir_lval == NULL)
1165 return;
1166
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001168 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001170 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001172 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173 {
1174 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001175 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001176 }
1177 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179}
1180
1181/*
1182 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001183 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184 */
1185 void
1186var_redir_stop()
1187{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001188 typval_T tv;
1189
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190 if (redir_lval != NULL)
1191 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001192 /* If there was no error: assign the text to the variable. */
1193 if (redir_endp != NULL)
1194 {
1195 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1196 tv.v_type = VAR_STRING;
1197 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001198 /* Call get_lval() again, if it's inside a Dict or List it may
1199 * have changed. */
1200 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001201 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001202 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1203 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1204 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001205 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001207 /* free the collected output */
1208 vim_free(redir_ga.ga_data);
1209 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211 vim_free(redir_lval);
1212 redir_lval = NULL;
1213 }
1214 vim_free(redir_varname);
1215 redir_varname = NULL;
1216}
1217
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218# if defined(FEAT_MBYTE) || defined(PROTO)
1219 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001220eval_charconvert(
1221 char_u *enc_from,
1222 char_u *enc_to,
1223 char_u *fname_from,
1224 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1229 set_vim_var_string(VV_CC_TO, enc_to, -1);
1230 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1231 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1232 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1233 err = TRUE;
1234 set_vim_var_string(VV_CC_FROM, NULL, -1);
1235 set_vim_var_string(VV_CC_TO, NULL, -1);
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1238
1239 if (err)
1240 return FAIL;
1241 return OK;
1242}
1243# endif
1244
1245# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1246 int
1247eval_printexpr(fname, args)
1248 char_u *fname;
1249 char_u *args;
1250{
1251 int err = FALSE;
1252
1253 set_vim_var_string(VV_FNAME_IN, fname, -1);
1254 set_vim_var_string(VV_CMDARG, args, -1);
1255 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1256 err = TRUE;
1257 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1258 set_vim_var_string(VV_CMDARG, NULL, -1);
1259
1260 if (err)
1261 {
1262 mch_remove(fname);
1263 return FAIL;
1264 }
1265 return OK;
1266}
1267# endif
1268
1269# if defined(FEAT_DIFF) || defined(PROTO)
1270 void
1271eval_diff(origfile, newfile, outfile)
1272 char_u *origfile;
1273 char_u *newfile;
1274 char_u *outfile;
1275{
1276 int err = FALSE;
1277
1278 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1279 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1280 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1281 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1282 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1283 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1284 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1285}
1286
1287 void
1288eval_patch(origfile, difffile, outfile)
1289 char_u *origfile;
1290 char_u *difffile;
1291 char_u *outfile;
1292{
1293 int err;
1294
1295 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1296 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1297 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1298 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1299 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1300 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1301 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1302}
1303# endif
1304
1305/*
1306 * Top level evaluation function, returning a boolean.
1307 * Sets "error" to TRUE if there was an error.
1308 * Return TRUE or FALSE.
1309 */
1310 int
1311eval_to_bool(arg, error, nextcmd, skip)
1312 char_u *arg;
1313 int *error;
1314 char_u **nextcmd;
1315 int skip; /* only parse, don't execute */
1316{
Bram Moolenaar33570922005-01-25 22:26:29 +00001317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 int retval = FALSE;
1319
1320 if (skip)
1321 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 else
1325 {
1326 *error = FALSE;
1327 if (!skip)
1328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001329 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001330 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 }
1333 if (skip)
1334 --emsg_skip;
1335
1336 return retval;
1337}
1338
1339/*
1340 * Top level evaluation function, returning a string. If "skip" is TRUE,
1341 * only parsing to "nextcmd" is done, without reporting errors. Return
1342 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1343 */
1344 char_u *
1345eval_to_string_skip(arg, nextcmd, skip)
1346 char_u *arg;
1347 char_u **nextcmd;
1348 int skip; /* only parse, don't execute */
1349{
Bram Moolenaar33570922005-01-25 22:26:29 +00001350 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 char_u *retval;
1352
1353 if (skip)
1354 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 retval = NULL;
1357 else
1358 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 retval = vim_strsave(get_tv_string(&tv));
1360 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 }
1362 if (skip)
1363 --emsg_skip;
1364
1365 return retval;
1366}
1367
1368/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001369 * Skip over an expression at "*pp".
1370 * Return FAIL for an error, OK otherwise.
1371 */
1372 int
1373skip_expr(pp)
1374 char_u **pp;
1375{
Bram Moolenaar33570922005-01-25 22:26:29 +00001376 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001377
1378 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001380}
1381
1382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384 * When "convert" is TRUE convert a List into a sequence of lines and convert
1385 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 * Return pointer to allocated memory, or NULL for failure.
1387 */
1388 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001389eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 char_u *arg;
1391 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001392 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393{
Bram Moolenaar33570922005-01-25 22:26:29 +00001394 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001397#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001398 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001401 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 retval = NULL;
1403 else
1404 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001405 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001406 {
1407 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001408 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001409 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001410 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001411 if (tv.vval.v_list->lv_len > 0)
1412 ga_append(&ga, NL);
1413 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001414 ga_append(&ga, NUL);
1415 retval = (char_u *)ga.ga_data;
1416 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001417#ifdef FEAT_FLOAT
1418 else if (convert && tv.v_type == VAR_FLOAT)
1419 {
1420 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1421 retval = vim_strsave(numbuf);
1422 }
1423#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001424 else
1425 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 }
1428
1429 return retval;
1430}
1431
1432/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001433 * Call eval_to_string() without using current local variables and using
1434 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 */
1436 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001437eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 char_u *arg;
1439 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001440 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 char_u *retval;
1443 void *save_funccalp;
1444
1445 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001446 if (use_sandbox)
1447 ++sandbox;
1448 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001449 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001450 if (use_sandbox)
1451 --sandbox;
1452 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 restore_funccal(save_funccalp);
1454 return retval;
1455}
1456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457/*
1458 * Top level evaluation function, returning a number.
1459 * Evaluates "expr" silently.
1460 * Returns -1 for an error.
1461 */
1462 int
1463eval_to_number(expr)
1464 char_u *expr;
1465{
Bram Moolenaar33570922005-01-25 22:26:29 +00001466 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001468 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
1470 ++emsg_off;
1471
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001472 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 retval = -1;
1474 else
1475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001476 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001477 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 }
1479 --emsg_off;
1480
1481 return retval;
1482}
1483
Bram Moolenaara40058a2005-07-11 22:42:07 +00001484/*
1485 * Prepare v: variable "idx" to be used.
1486 * Save the current typeval in "save_tv".
1487 * When not used yet add the variable to the v: hashtable.
1488 */
1489 static void
1490prepare_vimvar(idx, save_tv)
1491 int idx;
1492 typval_T *save_tv;
1493{
1494 *save_tv = vimvars[idx].vv_tv;
1495 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1496 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1497}
1498
1499/*
1500 * Restore v: variable "idx" to typeval "save_tv".
1501 * When no longer defined, remove the variable from the v: hashtable.
1502 */
1503 static void
1504restore_vimvar(idx, save_tv)
1505 int idx;
1506 typval_T *save_tv;
1507{
1508 hashitem_T *hi;
1509
Bram Moolenaara40058a2005-07-11 22:42:07 +00001510 vimvars[idx].vv_tv = *save_tv;
1511 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1512 {
1513 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1514 if (HASHITEM_EMPTY(hi))
1515 EMSG2(_(e_intern2), "restore_vimvar()");
1516 else
1517 hash_remove(&vimvarht, hi);
1518 }
1519}
1520
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001521#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001522/*
1523 * Evaluate an expression to a list with suggestions.
1524 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001525 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001526 */
1527 list_T *
1528eval_spell_expr(badword, expr)
1529 char_u *badword;
1530 char_u *expr;
1531{
1532 typval_T save_val;
1533 typval_T rettv;
1534 list_T *list = NULL;
1535 char_u *p = skipwhite(expr);
1536
1537 /* Set "v:val" to the bad word. */
1538 prepare_vimvar(VV_VAL, &save_val);
1539 vimvars[VV_VAL].vv_type = VAR_STRING;
1540 vimvars[VV_VAL].vv_str = badword;
1541 if (p_verbose == 0)
1542 ++emsg_off;
1543
1544 if (eval1(&p, &rettv, TRUE) == OK)
1545 {
1546 if (rettv.v_type != VAR_LIST)
1547 clear_tv(&rettv);
1548 else
1549 list = rettv.vval.v_list;
1550 }
1551
1552 if (p_verbose == 0)
1553 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001554 restore_vimvar(VV_VAL, &save_val);
1555
1556 return list;
1557}
1558
1559/*
1560 * "list" is supposed to contain two items: a word and a number. Return the
1561 * word in "pp" and the number as the return value.
1562 * Return -1 if anything isn't right.
1563 * Used to get the good word and score from the eval_spell_expr() result.
1564 */
1565 int
1566get_spellword(list, pp)
1567 list_T *list;
1568 char_u **pp;
1569{
1570 listitem_T *li;
1571
1572 li = list->lv_first;
1573 if (li == NULL)
1574 return -1;
1575 *pp = get_tv_string(&li->li_tv);
1576
1577 li = li->li_next;
1578 if (li == NULL)
1579 return -1;
1580 return get_tv_number(&li->li_tv);
1581}
1582#endif
1583
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001584/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001585 * Top level evaluation function.
1586 * Returns an allocated typval_T with the result.
1587 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001588 */
1589 typval_T *
1590eval_expr(arg, nextcmd)
1591 char_u *arg;
1592 char_u **nextcmd;
1593{
1594 typval_T *tv;
1595
1596 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001597 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001598 {
1599 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001600 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001601 }
1602
1603 return tv;
1604}
1605
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001608 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609 * Uses argv[argc] for the function arguments. Only Number and String
1610 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001613 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001614call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 char_u *func;
1616 int argc;
1617 char_u **argv;
1618 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001619 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
Bram Moolenaar33570922005-01-25 22:26:29 +00001622 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 long n;
1624 int len;
1625 int i;
1626 int doesrange;
1627 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001630 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
1634 for (i = 0; i < argc; i++)
1635 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001636 /* Pass a NULL or empty argument as an empty string */
1637 if (argv[i] == NULL || *argv[i] == NUL)
1638 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001639 argvars[i].v_type = VAR_STRING;
1640 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001641 continue;
1642 }
1643
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001644 if (str_arg_only)
1645 len = 0;
1646 else
1647 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001648 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 if (len != 0 && len == (int)STRLEN(argv[i]))
1650 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001651 argvars[i].v_type = VAR_NUMBER;
1652 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 }
1654 else
1655 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001656 argvars[i].v_type = VAR_STRING;
1657 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
1659 }
1660
1661 if (safe)
1662 {
1663 save_funccalp = save_funccal();
1664 ++sandbox;
1665 }
1666
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1668 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001670 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (safe)
1672 {
1673 --sandbox;
1674 restore_funccal(save_funccalp);
1675 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 vim_free(argvars);
1677
1678 if (ret == FAIL)
1679 clear_tv(rettv);
1680
1681 return ret;
1682}
1683
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001684/*
1685 * Call vimL function "func" and return the result as a number.
1686 * Returns -1 when calling the function fails.
1687 * Uses argv[argc] for the function arguments.
1688 */
1689 long
1690call_func_retnr(func, argc, argv, safe)
1691 char_u *func;
1692 int argc;
1693 char_u **argv;
1694 int safe; /* use the sandbox */
1695{
1696 typval_T rettv;
1697 long retval;
1698
1699 /* All arguments are passed as strings, no conversion to number. */
1700 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1701 return -1;
1702
1703 retval = get_tv_number_chk(&rettv, NULL);
1704 clear_tv(&rettv);
1705 return retval;
1706}
1707
1708#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1709 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1710
Bram Moolenaar4f688582007-07-24 12:34:30 +00001711# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001713 * Call vimL function "func" and return the result as a string.
1714 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715 * Uses argv[argc] for the function arguments.
1716 */
1717 void *
1718call_func_retstr(func, argc, argv, safe)
1719 char_u *func;
1720 int argc;
1721 char_u **argv;
1722 int safe; /* use the sandbox */
1723{
1724 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001725 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001726
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001727 /* All arguments are passed as strings, no conversion to number. */
1728 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001729 return NULL;
1730
1731 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 return retval;
1734}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001735# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001737/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001738 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001740 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 */
1742 void *
1743call_func_retlist(func, argc, argv, safe)
1744 char_u *func;
1745 int argc;
1746 char_u **argv;
1747 int safe; /* use the sandbox */
1748{
1749 typval_T rettv;
1750
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001751 /* All arguments are passed as strings, no conversion to number. */
1752 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001753 return NULL;
1754
1755 if (rettv.v_type != VAR_LIST)
1756 {
1757 clear_tv(&rettv);
1758 return NULL;
1759 }
1760
1761 return rettv.vval.v_list;
1762}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#endif
1764
1765/*
1766 * Save the current function call pointer, and set it to NULL.
1767 * Used when executing autocommands and for ":source".
1768 */
1769 void *
1770save_funccal()
1771{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001772 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 current_funccal = NULL;
1775 return (void *)fc;
1776}
1777
1778 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001779restore_funccal(vfc)
1780 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001782 funccall_T *fc = (funccall_T *)vfc;
1783
1784 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785}
1786
Bram Moolenaar05159a02005-02-26 23:04:13 +00001787#if defined(FEAT_PROFILE) || defined(PROTO)
1788/*
1789 * Prepare profiling for entering a child or something else that is not
1790 * counted for the script/function itself.
1791 * Should always be called in pair with prof_child_exit().
1792 */
1793 void
1794prof_child_enter(tm)
1795 proftime_T *tm; /* place to store waittime */
1796{
1797 funccall_T *fc = current_funccal;
1798
1799 if (fc != NULL && fc->func->uf_profiling)
1800 profile_start(&fc->prof_child);
1801 script_prof_save(tm);
1802}
1803
1804/*
1805 * Take care of time spent in a child.
1806 * Should always be called after prof_child_enter().
1807 */
1808 void
1809prof_child_exit(tm)
1810 proftime_T *tm; /* where waittime was stored */
1811{
1812 funccall_T *fc = current_funccal;
1813
1814 if (fc != NULL && fc->func->uf_profiling)
1815 {
1816 profile_end(&fc->prof_child);
1817 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1818 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1819 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1820 }
1821 script_prof_restore(tm);
1822}
1823#endif
1824
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826#ifdef FEAT_FOLDING
1827/*
1828 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1829 * it in "*cp". Doesn't give error messages.
1830 */
1831 int
1832eval_foldexpr(arg, cp)
1833 char_u *arg;
1834 int *cp;
1835{
Bram Moolenaar33570922005-01-25 22:26:29 +00001836 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 int retval;
1838 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001839 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1840 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
1842 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001843 if (use_sandbox)
1844 ++sandbox;
1845 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 retval = 0;
1849 else
1850 {
1851 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 if (tv.v_type == VAR_NUMBER)
1853 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001854 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 retval = 0;
1856 else
1857 {
1858 /* If the result is a string, check if there is a non-digit before
1859 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (!VIM_ISDIGIT(*s) && *s != '-')
1862 *cp = *s++;
1863 retval = atol((char *)s);
1864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
1867 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001868 if (use_sandbox)
1869 --sandbox;
1870 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
1872 return retval;
1873}
1874#endif
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 * ":let" list all variable values
1878 * ":let var1 var2" list variable values
1879 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001880 * ":let var += expr" assignment command.
1881 * ":let var -= expr" assignment command.
1882 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 */
1885 void
1886ex_let(eap)
1887 exarg_T *eap;
1888{
1889 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 int var_count = 0;
1894 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001896 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001897 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Bram Moolenaardb552d602006-03-23 22:59:57 +00001899 argend = skip_var_list(arg, &var_count, &semicolon);
1900 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001902 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1903 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 expr = skipwhite(argend);
1905 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1906 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001908 /*
1909 * ":let" without "=": list variables
1910 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 if (*arg == '[')
1912 EMSG(_(e_invarg));
1913 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001915 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001919 list_glob_vars(&first);
1920 list_buf_vars(&first);
1921 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001922#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001924#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 list_script_vars(&first);
1926 list_func_vars(&first);
1927 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 eap->nextcmd = check_nextcmd(arg);
1930 }
1931 else
1932 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 op[0] = '=';
1934 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001935 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001937 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1938 op[0] = *expr; /* +=, -= or .= */
1939 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001941 else
1942 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (eap->skip)
1945 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (eap->skip)
1948 {
1949 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 --emsg_skip;
1952 }
1953 else if (i != FAIL)
1954 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959 }
1960}
1961
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962/*
1963 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1964 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 * When "nextchars" is not NULL it points to a string with characters that
1966 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1967 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 * Returns OK or FAIL;
1969 */
1970 static int
1971ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1972 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001973 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 int copy; /* copy values from "tv", don't move */
1975 int semicolon; /* from skip_var_list() */
1976 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978{
1979 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001980 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 listitem_T *item;
1983 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984
1985 if (*arg != '[')
1986 {
1987 /*
1988 * ":let var = expr" or ":for var in list"
1989 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 return OK;
1993 }
1994
1995 /*
1996 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1997 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001998 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 {
2000 EMSG(_(e_listreq));
2001 return FAIL;
2002 }
2003
2004 i = list_len(l);
2005 if (semicolon == 0 && var_count < i)
2006 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002007 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 return FAIL;
2009 }
2010 if (var_count - semicolon > i)
2011 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002012 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 return FAIL;
2014 }
2015
2016 item = l->lv_first;
2017 while (*arg != ']')
2018 {
2019 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 item = item->li_next;
2022 if (arg == NULL)
2023 return FAIL;
2024
2025 arg = skipwhite(arg);
2026 if (*arg == ';')
2027 {
2028 /* Put the rest of the list (may be empty) in the var after ';'.
2029 * Create a new list for this. */
2030 l = list_alloc();
2031 if (l == NULL)
2032 return FAIL;
2033 while (item != NULL)
2034 {
2035 list_append_tv(l, &item->li_tv);
2036 item = item->li_next;
2037 }
2038
2039 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002040 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 ltv.vval.v_list = l;
2042 l->lv_refcount = 1;
2043
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2045 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 clear_tv(&ltv);
2047 if (arg == NULL)
2048 return FAIL;
2049 break;
2050 }
2051 else if (*arg != ',' && *arg != ']')
2052 {
2053 EMSG2(_(e_intern2), "ex_let_vars()");
2054 return FAIL;
2055 }
2056 }
2057
2058 return OK;
2059}
2060
2061/*
2062 * Skip over assignable variable "var" or list of variables "[var, var]".
2063 * Used for ":let varvar = expr" and ":for varvar in expr".
2064 * For "[var, var]" increment "*var_count" for each variable.
2065 * for "[var, var; var]" set "semicolon".
2066 * Return NULL for an error.
2067 */
2068 static char_u *
2069skip_var_list(arg, var_count, semicolon)
2070 char_u *arg;
2071 int *var_count;
2072 int *semicolon;
2073{
2074 char_u *p, *s;
2075
2076 if (*arg == '[')
2077 {
2078 /* "[var, var]": find the matching ']'. */
2079 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002080 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 {
2082 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2083 s = skip_var_one(p);
2084 if (s == p)
2085 {
2086 EMSG2(_(e_invarg2), p);
2087 return NULL;
2088 }
2089 ++*var_count;
2090
2091 p = skipwhite(s);
2092 if (*p == ']')
2093 break;
2094 else if (*p == ';')
2095 {
2096 if (*semicolon == 1)
2097 {
2098 EMSG(_("Double ; in list of variables"));
2099 return NULL;
2100 }
2101 *semicolon = 1;
2102 }
2103 else if (*p != ',')
2104 {
2105 EMSG2(_(e_invarg2), p);
2106 return NULL;
2107 }
2108 }
2109 return p + 1;
2110 }
2111 else
2112 return skip_var_one(arg);
2113}
2114
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002115/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002116 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002117 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002119 static char_u *
2120skip_var_one(arg)
2121 char_u *arg;
2122{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002123 if (*arg == '@' && arg[1] != NUL)
2124 return arg + 2;
2125 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2126 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127}
2128
Bram Moolenaara7043832005-01-21 11:56:39 +00002129/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002130 * List variables for hashtab "ht" with prefix "prefix".
2131 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002132 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002133 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002135 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002137 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002139{
Bram Moolenaar33570922005-01-25 22:26:29 +00002140 hashitem_T *hi;
2141 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142 int todo;
2143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002144 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002145 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2146 {
2147 if (!HASHITEM_EMPTY(hi))
2148 {
2149 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002150 di = HI2DI(hi);
2151 if (empty || di->di_tv.v_type != VAR_STRING
2152 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002154 }
2155 }
2156}
2157
2158/*
2159 * List global variables.
2160 */
2161 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002162list_glob_vars(first)
2163 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002164{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002166}
2167
2168/*
2169 * List buffer variables.
2170 */
2171 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172list_buf_vars(first)
2173 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002174{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175 char_u numbuf[NUMBUFLEN];
2176
Bram Moolenaar429fa852013-04-15 12:27:36 +02002177 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002179
2180 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2182 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002183}
2184
2185/*
2186 * List window variables.
2187 */
2188 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189list_win_vars(first)
2190 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002191{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002192 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002194}
2195
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196#ifdef FEAT_WINDOWS
2197/*
2198 * List tab page variables.
2199 */
2200 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201list_tab_vars(first)
2202 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002204 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002206}
2207#endif
2208
Bram Moolenaara7043832005-01-21 11:56:39 +00002209/*
2210 * List Vim variables.
2211 */
2212 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213list_vim_vars(first)
2214 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217}
2218
2219/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220 * List script-local variables, if there is a script.
2221 */
2222 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223list_script_vars(first)
2224 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002225{
2226 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002227 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2228 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002229}
2230
2231/*
2232 * List function variables, if there is a function.
2233 */
2234 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235list_func_vars(first)
2236 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002237{
2238 if (current_funccal != NULL)
2239 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002241}
2242
2243/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 * List variables in "arg".
2245 */
2246 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002247list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 exarg_T *eap;
2249 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251{
2252 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 char_u *name_start;
2256 char_u *arg_subsc;
2257 char_u *tofree;
2258 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259
2260 while (!ends_excmd(*arg) && !got_int)
2261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002264 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2266 {
2267 emsg_severe = TRUE;
2268 EMSG(_(e_trailing));
2269 break;
2270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 /* get_name_len() takes care of expanding curly braces */
2275 name_start = name = arg;
2276 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2277 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 /* This is mainly to keep test 49 working: when expanding
2280 * curly braces fails overrule the exception error message. */
2281 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 emsg_severe = TRUE;
2284 EMSG2(_(e_invarg2), arg);
2285 break;
2286 }
2287 error = TRUE;
2288 }
2289 else
2290 {
2291 if (tofree != NULL)
2292 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002293 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 else
2296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 /* handle d.key, l[idx], f(expr) */
2298 arg_subsc = arg;
2299 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 case 'g': list_glob_vars(first); break;
2308 case 'b': list_buf_vars(first); break;
2309 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002310#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002311 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002312#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002313 case 'v': list_vim_vars(first); break;
2314 case 's': list_script_vars(first); break;
2315 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 default:
2317 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002319 }
2320 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 {
2322 char_u numbuf[NUMBUFLEN];
2323 char_u *tf;
2324 int c;
2325 char_u *s;
2326
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002327 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328 c = *arg;
2329 *arg = NUL;
2330 list_one_var_a((char_u *)"",
2331 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002332 tv.v_type,
2333 s == NULL ? (char_u *)"" : s,
2334 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335 *arg = c;
2336 vim_free(tf);
2337 }
2338 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
2341 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342
2343 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345
2346 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 }
2348
2349 return arg;
2350}
2351
2352/*
2353 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2354 * Returns a pointer to the char just after the var name.
2355 * Returns NULL if there is an error.
2356 */
2357 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002360 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002362 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364{
2365 int c1;
2366 char_u *name;
2367 char_u *p;
2368 char_u *arg_end = NULL;
2369 int len;
2370 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372
2373 /*
2374 * ":let $VAR = expr": Set environment variable.
2375 */
2376 if (*arg == '$')
2377 {
2378 /* Find the end of the name. */
2379 ++arg;
2380 name = arg;
2381 len = get_env_len(&arg);
2382 if (len == 0)
2383 EMSG2(_(e_invarg2), name - 1);
2384 else
2385 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 if (op != NULL && (*op == '+' || *op == '-'))
2387 EMSG2(_(e_letwrong), op);
2388 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002389 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002391 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 {
2393 c1 = name[len];
2394 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 p = get_tv_string_chk(tv);
2396 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 {
2398 int mustfree = FALSE;
2399 char_u *s = vim_getenv(name, &mustfree);
2400
2401 if (s != NULL)
2402 {
2403 p = tofree = concat_str(s, p);
2404 if (mustfree)
2405 vim_free(s);
2406 }
2407 }
2408 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002411 if (STRICMP(name, "HOME") == 0)
2412 init_homedir();
2413 else if (didset_vim && STRICMP(name, "VIM") == 0)
2414 didset_vim = FALSE;
2415 else if (didset_vimruntime
2416 && STRICMP(name, "VIMRUNTIME") == 0)
2417 didset_vimruntime = FALSE;
2418 arg_end = arg;
2419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002421 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 }
2423 }
2424 }
2425
2426 /*
2427 * ":let &option = expr": Set option value.
2428 * ":let &l:option = expr": Set local option value.
2429 * ":let &g:option = expr": Set global option value.
2430 */
2431 else if (*arg == '&')
2432 {
2433 /* Find the end of the name. */
2434 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435 if (p == NULL || (endchars != NULL
2436 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 EMSG(_(e_letunexp));
2438 else
2439 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 long n;
2441 int opt_type;
2442 long numval;
2443 char_u *stringval = NULL;
2444 char_u *s;
2445
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 c1 = *p;
2447 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448
2449 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 s = get_tv_string_chk(tv); /* != NULL if number or string */
2451 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 {
2453 opt_type = get_option_value(arg, &numval,
2454 &stringval, opt_flags);
2455 if ((opt_type == 1 && *op == '.')
2456 || (opt_type == 0 && *op != '.'))
2457 EMSG2(_(e_letwrong), op);
2458 else
2459 {
2460 if (opt_type == 1) /* number */
2461 {
2462 if (*op == '+')
2463 n = numval + n;
2464 else
2465 n = numval - n;
2466 }
2467 else if (opt_type == 0 && stringval != NULL) /* string */
2468 {
2469 s = concat_str(stringval, s);
2470 vim_free(stringval);
2471 stringval = s;
2472 }
2473 }
2474 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 if (s != NULL)
2476 {
2477 set_option_value(arg, n, s, opt_flags);
2478 arg_end = p;
2479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
2483 }
2484
2485 /*
2486 * ":let @r = expr": Set register contents.
2487 */
2488 else if (*arg == '@')
2489 {
2490 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 if (op != NULL && (*op == '+' || *op == '-'))
2492 EMSG2(_(e_letwrong), op);
2493 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002494 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 EMSG(_(e_letunexp));
2496 else
2497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002498 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 char_u *s;
2500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 p = get_tv_string_chk(tv);
2502 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002504 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 if (s != NULL)
2506 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 vim_free(s);
2509 }
2510 }
2511 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002512 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 arg_end = arg + 1;
2515 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002516 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 }
2518 }
2519
2520 /*
2521 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002524 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002528 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2532 EMSG(_(e_letunexp));
2533 else
2534 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 arg_end = p;
2537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 }
2541
2542 else
2543 EMSG2(_(e_invarg2), arg);
2544
2545 return arg_end;
2546}
2547
2548/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2550 */
2551 static int
2552check_changedtick(arg)
2553 char_u *arg;
2554{
2555 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2556 {
2557 EMSG2(_(e_readonlyvar), arg);
2558 return TRUE;
2559 }
2560 return FALSE;
2561}
2562
2563/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 * Get an lval: variable, Dict item or List item that can be assigned a value
2565 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2566 * "name.key", "name.key[expr]" etc.
2567 * Indexing only works if "name" is an existing List or Dictionary.
2568 * "name" points to the start of the name.
2569 * If "rettv" is not NULL it points to the value to be assigned.
2570 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2571 * wrong; must end in space or cmd separator.
2572 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002573 * flags:
2574 * GLV_QUIET: do not give error messages
2575 * GLV_NO_AUTOLOAD: do not use script autoloading
2576 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002578 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 */
2581 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002582get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 typval_T *rettv;
2585 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 int unlet;
2587 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002588 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002589 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 char_u *expr_start, *expr_end;
2593 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 dictitem_T *v;
2595 typval_T var1;
2596 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002601 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002602 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002605 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606
2607 if (skip)
2608 {
2609 /* When skipping just find the end of the name. */
2610 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002611 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 }
2613
2614 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002615 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (expr_start != NULL)
2617 {
2618 /* Don't expand the name when we already know there is an error. */
2619 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2620 && *p != '[' && *p != '.')
2621 {
2622 EMSG(_(e_trailing));
2623 return NULL;
2624 }
2625
2626 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2627 if (lp->ll_exp_name == NULL)
2628 {
2629 /* Report an invalid expression in braces, unless the
2630 * expression evaluation has been cancelled due to an
2631 * aborting error, an interrupt, or an exception. */
2632 if (!aborting() && !quiet)
2633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002634 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 EMSG2(_(e_invarg2), name);
2636 return NULL;
2637 }
2638 }
2639 lp->ll_name = lp->ll_exp_name;
2640 }
2641 else
2642 lp->ll_name = name;
2643
2644 /* Without [idx] or .key we are done. */
2645 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2646 return p;
2647
2648 cc = *p;
2649 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002650 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (v == NULL && !quiet)
2652 EMSG2(_(e_undefvar), lp->ll_name);
2653 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 if (v == NULL)
2655 return NULL;
2656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 /*
2658 * Loop until no more [idx] or .key is following.
2659 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002660 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2664 && !(lp->ll_tv->v_type == VAR_DICT
2665 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
2668 EMSG(_("E689: Can only index a List or Dictionary"));
2669 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (!quiet)
2674 EMSG(_("E708: [:] must come last"));
2675 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002676 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002677
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 len = -1;
2679 if (*p == '.')
2680 {
2681 key = p + 1;
2682 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2683 ;
2684 if (len == 0)
2685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
2687 EMSG(_(e_emptykey));
2688 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
2690 p = key + len;
2691 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 else
2693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (*p == ':')
2697 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 else
2699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 empty1 = FALSE;
2701 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002703 if (get_tv_string_chk(&var1) == NULL)
2704 {
2705 /* not a number or string */
2706 clear_tv(&var1);
2707 return NULL;
2708 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
2710
2711 /* Optionally get the second index [ :expr]. */
2712 if (*p == ':')
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002717 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002721 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (rettv != NULL && (rettv->v_type != VAR_LIST
2723 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!quiet)
2726 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
2731 p = skipwhite(p + 1);
2732 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 else
2735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 if (!empty1)
2740 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 if (get_tv_string_chk(&var2) == NULL)
2744 {
2745 /* not a number or string */
2746 if (!empty1)
2747 clear_tv(&var1);
2748 clear_tv(&var2);
2749 return NULL;
2750 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002756
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (!quiet)
2760 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (!empty1)
2762 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 }
2767
2768 /* Skip to past ']'. */
2769 ++p;
2770 }
2771
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 {
2774 if (len == -1)
2775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002777 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 if (*key == NUL)
2779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (!quiet)
2781 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 }
2785 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002786 lp->ll_list = NULL;
2787 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002788 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002789
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002790 /* When assigning to a scope dictionary check that a function and
2791 * variable name is valid (only variable name unless it is l: or
2792 * g: dictionary). Disallow overwriting a builtin function. */
2793 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002794 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002795 int prevval;
2796 int wrong;
2797
2798 if (len != -1)
2799 {
2800 prevval = key[len];
2801 key[len] = NUL;
2802 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002803 else
2804 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002805 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2806 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002807 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002808 || !valid_varname(key);
2809 if (len != -1)
2810 key[len] = prevval;
2811 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002812 return NULL;
2813 }
2814
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002817 /* Can't add "v:" variable. */
2818 if (lp->ll_dict == &vimvardict)
2819 {
2820 EMSG2(_(e_illvar), name);
2821 return NULL;
2822 }
2823
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002824 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002828 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 if (len == -1)
2830 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 }
2833 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 if (len == -1)
2838 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 p = NULL;
2841 break;
2842 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002843 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002844 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002845 return NULL;
2846
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 if (len == -1)
2848 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 }
2851 else
2852 {
2853 /*
2854 * Get the number and item for the only or first index of the List.
2855 */
2856 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 else
2859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002860 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 clear_tv(&var1);
2862 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002863 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 lp->ll_list = lp->ll_tv->vval.v_list;
2865 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2866 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002868 if (lp->ll_n1 < 0)
2869 {
2870 lp->ll_n1 = 0;
2871 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2872 }
2873 }
2874 if (lp->ll_li == NULL)
2875 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002878 if (!quiet)
2879 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 }
2882
2883 /*
2884 * May need to find the item or absolute index for the second
2885 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 * When no index given: "lp->ll_empty2" is TRUE.
2887 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002891 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002897 {
2898 if (!quiet)
2899 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002901 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 }
2904
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2906 if (lp->ll_n1 < 0)
2907 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2908 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002909 {
2910 if (!quiet)
2911 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002913 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002914 }
2915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002918 }
2919
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 return p;
2921}
2922
2923/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 */
2926 static void
2927clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002928 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929{
2930 vim_free(lp->ll_exp_name);
2931 vim_free(lp->ll_newkey);
2932}
2933
2934/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002935 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002937 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 */
2939 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002940set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002945 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946{
2947 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002948 listitem_T *ri;
2949 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950
2951 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 cc = *endp;
2956 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 if (op != NULL && *op != '=')
2958 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002959 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002962 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002963 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002964 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002965 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002966 if ((di == NULL
2967 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2968 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2969 FALSE)))
2970 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002971 set_var(lp->ll_name, &tv, FALSE);
2972 clear_tv(&tv);
2973 }
2974 }
2975 else
2976 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002980 else if (tv_check_lock(lp->ll_newkey == NULL
2981 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002982 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002983 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 else if (lp->ll_range)
2985 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002986 listitem_T *ll_li = lp->ll_li;
2987 int ll_n1 = lp->ll_n1;
2988
2989 /*
2990 * Check whether any of the list items is locked
2991 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002992 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002993 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002994 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002995 return;
2996 ri = ri->li_next;
2997 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2998 break;
2999 ll_li = ll_li->li_next;
3000 ++ll_n1;
3001 }
3002
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 /*
3004 * Assign the List values to the list items.
3005 */
3006 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003007 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003008 if (op != NULL && *op != '=')
3009 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3010 else
3011 {
3012 clear_tv(&lp->ll_li->li_tv);
3013 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3014 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 ri = ri->li_next;
3016 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3017 break;
3018 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003021 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003022 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 ri = NULL;
3024 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003025 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003026 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 lp->ll_li = lp->ll_li->li_next;
3028 ++lp->ll_n1;
3029 }
3030 if (ri != NULL)
3031 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 else if (lp->ll_empty2
3033 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 : lp->ll_n1 != lp->ll_n2)
3035 EMSG(_("E711: List value has not enough items"));
3036 }
3037 else
3038 {
3039 /*
3040 * Assign to a List or Dictionary item.
3041 */
3042 if (lp->ll_newkey != NULL)
3043 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 if (op != NULL && *op != '=')
3045 {
3046 EMSG2(_(e_letwrong), op);
3047 return;
3048 }
3049
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003051 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 if (di == NULL)
3053 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003054 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3055 {
3056 vim_free(di);
3057 return;
3058 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003060 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003061 else if (op != NULL && *op != '=')
3062 {
3063 tv_op(lp->ll_tv, rettv, op);
3064 return;
3065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003068
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003069 /*
3070 * Assign the value to the variable or list item.
3071 */
3072 if (copy)
3073 copy_tv(rettv, lp->ll_tv);
3074 else
3075 {
3076 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003077 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003078 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079 }
3080 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003081}
3082
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003083/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3085 * Returns OK or FAIL.
3086 */
3087 static int
3088tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003089 typval_T *tv1;
3090 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 char_u *op;
3092{
3093 long n;
3094 char_u numbuf[NUMBUFLEN];
3095 char_u *s;
3096
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003097 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3098 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3099 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003100 {
3101 switch (tv1->v_type)
3102 {
3103 case VAR_DICT:
3104 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003105 case VAR_SPECIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003106 break;
3107
3108 case VAR_LIST:
3109 if (*op != '+' || tv2->v_type != VAR_LIST)
3110 break;
3111 /* List += List */
3112 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3113 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3114 return OK;
3115
3116 case VAR_NUMBER:
3117 case VAR_STRING:
3118 if (tv2->v_type == VAR_LIST)
3119 break;
3120 if (*op == '+' || *op == '-')
3121 {
3122 /* nr += nr or nr -= nr*/
3123 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124#ifdef FEAT_FLOAT
3125 if (tv2->v_type == VAR_FLOAT)
3126 {
3127 float_T f = n;
3128
3129 if (*op == '+')
3130 f += tv2->vval.v_float;
3131 else
3132 f -= tv2->vval.v_float;
3133 clear_tv(tv1);
3134 tv1->v_type = VAR_FLOAT;
3135 tv1->vval.v_float = f;
3136 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138#endif
3139 {
3140 if (*op == '+')
3141 n += get_tv_number(tv2);
3142 else
3143 n -= get_tv_number(tv2);
3144 clear_tv(tv1);
3145 tv1->v_type = VAR_NUMBER;
3146 tv1->vval.v_number = n;
3147 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003148 }
3149 else
3150 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151 if (tv2->v_type == VAR_FLOAT)
3152 break;
3153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 /* str .= str */
3155 s = get_tv_string(tv1);
3156 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3157 clear_tv(tv1);
3158 tv1->v_type = VAR_STRING;
3159 tv1->vval.v_string = s;
3160 }
3161 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162
3163#ifdef FEAT_FLOAT
3164 case VAR_FLOAT:
3165 {
3166 float_T f;
3167
3168 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3169 && tv2->v_type != VAR_NUMBER
3170 && tv2->v_type != VAR_STRING))
3171 break;
3172 if (tv2->v_type == VAR_FLOAT)
3173 f = tv2->vval.v_float;
3174 else
3175 f = get_tv_number(tv2);
3176 if (*op == '+')
3177 tv1->vval.v_float += f;
3178 else
3179 tv1->vval.v_float -= f;
3180 }
3181 return OK;
3182#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003183 }
3184 }
3185
3186 EMSG2(_(e_letwrong), op);
3187 return FAIL;
3188}
3189
3190/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 * Add a watcher to a list.
3192 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003193 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003195 list_T *l;
3196 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197{
3198 lw->lw_next = l->lv_watch;
3199 l->lv_watch = lw;
3200}
3201
3202/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003203 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 * No warning when it isn't found...
3205 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003206 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003208 list_T *l;
3209 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003210{
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212
3213 lwp = &l->lv_watch;
3214 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3215 {
3216 if (lw == lwrem)
3217 {
3218 *lwp = lw->lw_next;
3219 break;
3220 }
3221 lwp = &lw->lw_next;
3222 }
3223}
3224
3225/*
3226 * Just before removing an item from a list: advance watchers to the next
3227 * item.
3228 */
3229 static void
3230list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 list_T *l;
3232 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3237 if (lw->lw_item == item)
3238 lw->lw_item = item->li_next;
3239}
3240
3241/*
3242 * Evaluate the expression used in a ":for var in expr" command.
3243 * "arg" points to "var".
3244 * Set "*errp" to TRUE for an error, FALSE otherwise;
3245 * Return a pointer that holds the info. Null when there is an error.
3246 */
3247 void *
3248eval_for_line(arg, errp, nextcmdp, skip)
3249 char_u *arg;
3250 int *errp;
3251 char_u **nextcmdp;
3252 int skip;
3253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 typval_T tv;
3257 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258
3259 *errp = TRUE; /* default: there is an error */
3260
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 if (fi == NULL)
3263 return NULL;
3264
3265 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3266 if (expr == NULL)
3267 return fi;
3268
3269 expr = skipwhite(expr);
3270 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3271 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003272 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 return fi;
3274 }
3275
3276 if (skip)
3277 ++emsg_skip;
3278 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3279 {
3280 *errp = FALSE;
3281 if (!skip)
3282 {
3283 l = tv.vval.v_list;
3284 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287 clear_tv(&tv);
3288 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003289 else
3290 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003291 /* No need to increment the refcount, it's already set for the
3292 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 fi->fi_list = l;
3294 list_add_watch(l, &fi->fi_lw);
3295 fi->fi_lw.lw_item = l->lv_first;
3296 }
3297 }
3298 }
3299 if (skip)
3300 --emsg_skip;
3301
3302 return fi;
3303}
3304
3305/*
3306 * Use the first item in a ":for" list. Advance to the next.
3307 * Assign the values to the variable (list). "arg" points to the first one.
3308 * Return TRUE when a valid item was found, FALSE when at end of list or
3309 * something wrong.
3310 */
3311 int
3312next_for_item(fi_void, arg)
3313 void *fi_void;
3314 char_u *arg;
3315{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003316 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003318 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319
3320 item = fi->fi_lw.lw_item;
3321 if (item == NULL)
3322 result = FALSE;
3323 else
3324 {
3325 fi->fi_lw.lw_item = item->li_next;
3326 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3327 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3328 }
3329 return result;
3330}
3331
3332/*
3333 * Free the structure used to store info used by ":for".
3334 */
3335 void
3336free_for_info(fi_void)
3337 void *fi_void;
3338{
Bram Moolenaar33570922005-01-25 22:26:29 +00003339 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003341 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003342 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003344 list_unref(fi->fi_list);
3345 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 vim_free(fi);
3347}
3348
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3350
3351 void
3352set_context_for_expression(xp, arg, cmdidx)
3353 expand_T *xp;
3354 char_u *arg;
3355 cmdidx_T cmdidx;
3356{
3357 int got_eq = FALSE;
3358 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361 if (cmdidx == CMD_let)
3362 {
3363 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003364 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 {
3366 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003367 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 {
3369 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003370 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003371 if (vim_iswhite(*p))
3372 break;
3373 }
3374 return;
3375 }
3376 }
3377 else
3378 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3379 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 while ((xp->xp_pattern = vim_strpbrk(arg,
3381 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3382 {
3383 c = *xp->xp_pattern;
3384 if (c == '&')
3385 {
3386 c = xp->xp_pattern[1];
3387 if (c == '&')
3388 {
3389 ++xp->xp_pattern;
3390 xp->xp_context = cmdidx != CMD_let || got_eq
3391 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3392 }
3393 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003394 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003396 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3397 xp->xp_pattern += 2;
3398
3399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 }
3401 else if (c == '$')
3402 {
3403 /* environment variable */
3404 xp->xp_context = EXPAND_ENV_VARS;
3405 }
3406 else if (c == '=')
3407 {
3408 got_eq = TRUE;
3409 xp->xp_context = EXPAND_EXPRESSION;
3410 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003411 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 && xp->xp_context == EXPAND_FUNCTIONS
3413 && vim_strchr(xp->xp_pattern, '(') == NULL)
3414 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003415 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 break;
3417 }
3418 else if (cmdidx != CMD_let || got_eq)
3419 {
3420 if (c == '"') /* string */
3421 {
3422 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3423 if (c == '\\' && xp->xp_pattern[1] != NUL)
3424 ++xp->xp_pattern;
3425 xp->xp_context = EXPAND_NOTHING;
3426 }
3427 else if (c == '\'') /* literal string */
3428 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003429 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3431 /* skip */ ;
3432 xp->xp_context = EXPAND_NOTHING;
3433 }
3434 else if (c == '|')
3435 {
3436 if (xp->xp_pattern[1] == '|')
3437 {
3438 ++xp->xp_pattern;
3439 xp->xp_context = EXPAND_EXPRESSION;
3440 }
3441 else
3442 xp->xp_context = EXPAND_COMMANDS;
3443 }
3444 else
3445 xp->xp_context = EXPAND_EXPRESSION;
3446 }
3447 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003448 /* Doesn't look like something valid, expand as an expression
3449 * anyway. */
3450 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 arg = xp->xp_pattern;
3452 if (*arg != NUL)
3453 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3454 /* skip */ ;
3455 }
3456 xp->xp_pattern = arg;
3457}
3458
3459#endif /* FEAT_CMDL_COMPL */
3460
3461/*
3462 * ":1,25call func(arg1, arg2)" function call.
3463 */
3464 void
3465ex_call(eap)
3466 exarg_T *eap;
3467{
3468 char_u *arg = eap->arg;
3469 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003471 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003473 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 linenr_T lnum;
3475 int doesrange;
3476 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003477 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003479 if (eap->skip)
3480 {
3481 /* trans_function_name() doesn't work well when skipping, use eval0()
3482 * instead to skip to any following command, e.g. for:
3483 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003484 ++emsg_skip;
3485 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3486 clear_tv(&rettv);
3487 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003488 return;
3489 }
3490
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003491 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003492 if (fudi.fd_newkey != NULL)
3493 {
3494 /* Still need to give an error message for missing key. */
3495 EMSG2(_(e_dictkey), fudi.fd_newkey);
3496 vim_free(fudi.fd_newkey);
3497 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003498 if (tofree == NULL)
3499 return;
3500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003501 /* Increase refcount on dictionary, it could get deleted when evaluating
3502 * the arguments. */
3503 if (fudi.fd_dict != NULL)
3504 ++fudi.fd_dict->dv_refcount;
3505
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003506 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003507 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003508 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar532c7802005-01-27 14:44:31 +00003510 /* Skip white space to allow ":call func ()". Not good, but required for
3511 * backward compatibility. */
3512 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003513 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 if (*startarg != '(')
3516 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003517 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 goto end;
3519 }
3520
3521 /*
3522 * When skipping, evaluate the function once, to find the end of the
3523 * arguments.
3524 * When the function takes a range, this is discovered after the first
3525 * call, and the loop is broken.
3526 */
3527 if (eap->skip)
3528 {
3529 ++emsg_skip;
3530 lnum = eap->line2; /* do it once, also with an invalid range */
3531 }
3532 else
3533 lnum = eap->line1;
3534 for ( ; lnum <= eap->line2; ++lnum)
3535 {
3536 if (!eap->skip && eap->addr_count > 0)
3537 {
3538 curwin->w_cursor.lnum = lnum;
3539 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003540#ifdef FEAT_VIRTUALEDIT
3541 curwin->w_cursor.coladd = 0;
3542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
3544 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003545 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003546 eap->line1, eap->line2, &doesrange,
3547 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
3549 failed = TRUE;
3550 break;
3551 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003552
3553 /* Handle a function returning a Funcref, Dictionary or List. */
3554 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3555 {
3556 failed = TRUE;
3557 break;
3558 }
3559
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003560 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (doesrange || eap->skip)
3562 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003565 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003566 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003567 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 if (aborting())
3569 break;
3570 }
3571 if (eap->skip)
3572 --emsg_skip;
3573
3574 if (!failed)
3575 {
3576 /* Check for trailing illegal characters and a following command. */
3577 if (!ends_excmd(*arg))
3578 {
3579 emsg_severe = TRUE;
3580 EMSG(_(e_trailing));
3581 }
3582 else
3583 eap->nextcmd = check_nextcmd(arg);
3584 }
3585
3586end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003587 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003588 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589}
3590
3591/*
3592 * ":unlet[!] var1 ... " command.
3593 */
3594 void
3595ex_unlet(eap)
3596 exarg_T *eap;
3597{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 ex_unletlock(eap, eap->arg, 0);
3599}
3600
3601/*
3602 * ":lockvar" and ":unlockvar" commands
3603 */
3604 void
3605ex_lockvar(eap)
3606 exarg_T *eap;
3607{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609 int deep = 2;
3610
3611 if (eap->forceit)
3612 deep = -1;
3613 else if (vim_isdigit(*arg))
3614 {
3615 deep = getdigits(&arg);
3616 arg = skipwhite(arg);
3617 }
3618
3619 ex_unletlock(eap, arg, deep);
3620}
3621
3622/*
3623 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3624 */
3625 static void
3626ex_unletlock(eap, argstart, deep)
3627 exarg_T *eap;
3628 char_u *argstart;
3629 int deep;
3630{
3631 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
3636 do
3637 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003638 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003639 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003640 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641 if (lv.ll_name == NULL)
3642 error = TRUE; /* error but continue parsing */
3643 if (name_end == NULL || (!vim_iswhite(*name_end)
3644 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 if (name_end != NULL)
3647 {
3648 emsg_severe = TRUE;
3649 EMSG(_(e_trailing));
3650 }
3651 if (!(eap->skip || error))
3652 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 break;
3654 }
3655
3656 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 {
3658 if (eap->cmdidx == CMD_unlet)
3659 {
3660 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3661 error = TRUE;
3662 }
3663 else
3664 {
3665 if (do_lock_var(&lv, name_end, deep,
3666 eap->cmdidx == CMD_lockvar) == FAIL)
3667 error = TRUE;
3668 }
3669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 if (!eap->skip)
3672 clear_lval(&lv);
3673
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 arg = skipwhite(name_end);
3675 } while (!ends_excmd(*arg));
3676
3677 eap->nextcmd = check_nextcmd(arg);
3678}
3679
Bram Moolenaar8c711452005-01-14 21:53:12 +00003680 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003681do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003682 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003684 int forceit;
3685{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 int ret = OK;
3687 int cc;
3688
3689 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003691 cc = *name_end;
3692 *name_end = NUL;
3693
3694 /* Normal name or expanded name. */
3695 if (check_changedtick(lp->ll_name))
3696 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003697 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003699 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003700 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003701 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003702 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003703 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003704 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003705 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 else if (lp->ll_range)
3707 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003708 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003709 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003710 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003711
3712 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3713 {
3714 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003715 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003716 return FAIL;
3717 ll_li = li;
3718 ++ll_n1;
3719 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720
3721 /* Delete a range of List items. */
3722 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3723 {
3724 li = lp->ll_li->li_next;
3725 listitem_remove(lp->ll_list, lp->ll_li);
3726 lp->ll_li = li;
3727 ++lp->ll_n1;
3728 }
3729 }
3730 else
3731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003732 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003733 /* unlet a List item. */
3734 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003736 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003737 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003738 }
3739
3740 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003741}
3742
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743/*
3744 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003745 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 */
3747 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003748do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003750 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751{
Bram Moolenaar33570922005-01-25 22:26:29 +00003752 hashtab_T *ht;
3753 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003754 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003755 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003756 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
Bram Moolenaar33570922005-01-25 22:26:29 +00003758 ht = find_var_ht(name, &varname);
3759 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003761 if (ht == &globvarht)
3762 d = &globvardict;
3763 else if (current_funccal != NULL
3764 && ht == &current_funccal->l_vars.dv_hashtab)
3765 d = &current_funccal->l_vars;
3766 else if (ht == &compat_hashtab)
3767 d = &vimvardict;
3768 else
3769 {
3770 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3771 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3772 }
3773 if (d == NULL)
3774 {
3775 EMSG2(_(e_intern2), "do_unlet()");
3776 return FAIL;
3777 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003778 hi = hash_find(ht, varname);
3779 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003780 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003781 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003782 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003783 || var_check_ro(di->di_flags, name, FALSE)
3784 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003785 return FAIL;
3786
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003787 delete_var(ht, hi);
3788 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003791 if (forceit)
3792 return OK;
3793 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 return FAIL;
3795}
3796
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003797/*
3798 * Lock or unlock variable indicated by "lp".
3799 * "deep" is the levels to go (-1 for unlimited);
3800 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3801 */
3802 static int
3803do_lock_var(lp, name_end, deep, lock)
3804 lval_T *lp;
3805 char_u *name_end;
3806 int deep;
3807 int lock;
3808{
3809 int ret = OK;
3810 int cc;
3811 dictitem_T *di;
3812
3813 if (deep == 0) /* nothing to do */
3814 return OK;
3815
3816 if (lp->ll_tv == NULL)
3817 {
3818 cc = *name_end;
3819 *name_end = NUL;
3820
3821 /* Normal name or expanded name. */
3822 if (check_changedtick(lp->ll_name))
3823 ret = FAIL;
3824 else
3825 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003826 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003827 if (di == NULL)
3828 ret = FAIL;
3829 else
3830 {
3831 if (lock)
3832 di->di_flags |= DI_FLAGS_LOCK;
3833 else
3834 di->di_flags &= ~DI_FLAGS_LOCK;
3835 item_lock(&di->di_tv, deep, lock);
3836 }
3837 }
3838 *name_end = cc;
3839 }
3840 else if (lp->ll_range)
3841 {
3842 listitem_T *li = lp->ll_li;
3843
3844 /* (un)lock a range of List items. */
3845 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3846 {
3847 item_lock(&li->li_tv, deep, lock);
3848 li = li->li_next;
3849 ++lp->ll_n1;
3850 }
3851 }
3852 else if (lp->ll_list != NULL)
3853 /* (un)lock a List item. */
3854 item_lock(&lp->ll_li->li_tv, deep, lock);
3855 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003856 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003857 item_lock(&lp->ll_di->di_tv, deep, lock);
3858
3859 return ret;
3860}
3861
3862/*
3863 * Lock or unlock an item. "deep" is nr of levels to go.
3864 */
3865 static void
3866item_lock(tv, deep, lock)
3867 typval_T *tv;
3868 int deep;
3869 int lock;
3870{
3871 static int recurse = 0;
3872 list_T *l;
3873 listitem_T *li;
3874 dict_T *d;
3875 hashitem_T *hi;
3876 int todo;
3877
3878 if (recurse >= DICT_MAXNEST)
3879 {
3880 EMSG(_("E743: variable nested too deep for (un)lock"));
3881 return;
3882 }
3883 if (deep == 0)
3884 return;
3885 ++recurse;
3886
3887 /* lock/unlock the item itself */
3888 if (lock)
3889 tv->v_lock |= VAR_LOCKED;
3890 else
3891 tv->v_lock &= ~VAR_LOCKED;
3892
3893 switch (tv->v_type)
3894 {
3895 case VAR_LIST:
3896 if ((l = tv->vval.v_list) != NULL)
3897 {
3898 if (lock)
3899 l->lv_lock |= VAR_LOCKED;
3900 else
3901 l->lv_lock &= ~VAR_LOCKED;
3902 if (deep < 0 || deep > 1)
3903 /* recursive: lock/unlock the items the List contains */
3904 for (li = l->lv_first; li != NULL; li = li->li_next)
3905 item_lock(&li->li_tv, deep - 1, lock);
3906 }
3907 break;
3908 case VAR_DICT:
3909 if ((d = tv->vval.v_dict) != NULL)
3910 {
3911 if (lock)
3912 d->dv_lock |= VAR_LOCKED;
3913 else
3914 d->dv_lock &= ~VAR_LOCKED;
3915 if (deep < 0 || deep > 1)
3916 {
3917 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003918 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003919 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3920 {
3921 if (!HASHITEM_EMPTY(hi))
3922 {
3923 --todo;
3924 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3925 }
3926 }
3927 }
3928 }
3929 }
3930 --recurse;
3931}
3932
Bram Moolenaara40058a2005-07-11 22:42:07 +00003933/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003934 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3935 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003936 */
3937 static int
3938tv_islocked(tv)
3939 typval_T *tv;
3940{
3941 return (tv->v_lock & VAR_LOCKED)
3942 || (tv->v_type == VAR_LIST
3943 && tv->vval.v_list != NULL
3944 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3945 || (tv->v_type == VAR_DICT
3946 && tv->vval.v_dict != NULL
3947 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3948}
3949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3951/*
3952 * Delete all "menutrans_" variables.
3953 */
3954 void
3955del_menutrans_vars()
3956{
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003958 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003961 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003963 {
3964 if (!HASHITEM_EMPTY(hi))
3965 {
3966 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3968 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003969 }
3970 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972}
3973#endif
3974
3975#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3976
3977/*
3978 * Local string buffer for the next two functions to store a variable name
3979 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3980 * get_user_var_name().
3981 */
3982
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003983static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
3985static char_u *varnamebuf = NULL;
3986static int varnamebuflen = 0;
3987
3988/*
3989 * Function to concatenate a prefix and a variable name.
3990 */
3991 static char_u *
3992cat_prefix_varname(prefix, name)
3993 int prefix;
3994 char_u *name;
3995{
3996 int len;
3997
3998 len = (int)STRLEN(name) + 3;
3999 if (len > varnamebuflen)
4000 {
4001 vim_free(varnamebuf);
4002 len += 10; /* some additional space */
4003 varnamebuf = alloc(len);
4004 if (varnamebuf == NULL)
4005 {
4006 varnamebuflen = 0;
4007 return NULL;
4008 }
4009 varnamebuflen = len;
4010 }
4011 *varnamebuf = prefix;
4012 varnamebuf[1] = ':';
4013 STRCPY(varnamebuf + 2, name);
4014 return varnamebuf;
4015}
4016
4017/*
4018 * Function given to ExpandGeneric() to obtain the list of user defined
4019 * (global/buffer/window/built-in) variable names.
4020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 char_u *
4022get_user_var_name(xp, idx)
4023 expand_T *xp;
4024 int idx;
4025{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004026 static long_u gdone;
4027 static long_u bdone;
4028 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029#ifdef FEAT_WINDOWS
4030 static long_u tdone;
4031#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004032 static int vidx;
4033 static hashitem_T *hi;
4034 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
4036 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004037 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004038 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004039#ifdef FEAT_WINDOWS
4040 tdone = 0;
4041#endif
4042 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004043
4044 /* Global variables */
4045 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004049 else
4050 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004051 while (HASHITEM_EMPTY(hi))
4052 ++hi;
4053 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4054 return cat_prefix_varname('g', hi->hi_key);
4055 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004057
4058 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004059 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004062 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004063 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004064 else
4065 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004066 while (HASHITEM_EMPTY(hi))
4067 ++hi;
4068 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004072 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 return (char_u *)"b:changedtick";
4074 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004075
4076 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004077 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004080 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004081 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004082 else
4083 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004084 while (HASHITEM_EMPTY(hi))
4085 ++hi;
4086 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004088
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004089#ifdef FEAT_WINDOWS
4090 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004091 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004092 if (tdone < ht->ht_used)
4093 {
4094 if (tdone++ == 0)
4095 hi = ht->ht_array;
4096 else
4097 ++hi;
4098 while (HASHITEM_EMPTY(hi))
4099 ++hi;
4100 return cat_prefix_varname('t', hi->hi_key);
4101 }
4102#endif
4103
Bram Moolenaar33570922005-01-25 22:26:29 +00004104 /* v: variables */
4105 if (vidx < VV_LEN)
4106 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
4108 vim_free(varnamebuf);
4109 varnamebuf = NULL;
4110 varnamebuflen = 0;
4111 return NULL;
4112}
4113
4114#endif /* FEAT_CMDL_COMPL */
4115
4116/*
4117 * types for expressions.
4118 */
4119typedef enum
4120{
4121 TYPE_UNKNOWN = 0
4122 , TYPE_EQUAL /* == */
4123 , TYPE_NEQUAL /* != */
4124 , TYPE_GREATER /* > */
4125 , TYPE_GEQUAL /* >= */
4126 , TYPE_SMALLER /* < */
4127 , TYPE_SEQUAL /* <= */
4128 , TYPE_MATCH /* =~ */
4129 , TYPE_NOMATCH /* !~ */
4130} exptype_T;
4131
4132/*
4133 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4136 */
4137
4138/*
4139 * Handle zero level expression.
4140 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004142 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 * Return OK or FAIL.
4144 */
4145 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004146eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 char_u **nextcmd;
4150 int evaluate;
4151{
4152 int ret;
4153 char_u *p;
4154
4155 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 if (ret == FAIL || !ends_excmd(*p))
4158 {
4159 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004160 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 /*
4162 * Report the invalid expression unless the expression evaluation has
4163 * been cancelled due to an aborting error, an interrupt, or an
4164 * exception.
4165 */
4166 if (!aborting())
4167 EMSG2(_(e_invexpr2), arg);
4168 ret = FAIL;
4169 }
4170 if (nextcmd != NULL)
4171 *nextcmd = check_nextcmd(p);
4172
4173 return ret;
4174}
4175
4176/*
4177 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004178 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 *
4180 * "arg" must point to the first non-white of the expression.
4181 * "arg" is advanced to the next non-white after the recognized expression.
4182 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004183 * Note: "rettv.v_lock" is not set.
4184 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 * Return OK or FAIL.
4186 */
4187 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 int evaluate;
4192{
4193 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004194 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
4196 /*
4197 * Get the first variable.
4198 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004199 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 return FAIL;
4201
4202 if ((*arg)[0] == '?')
4203 {
4204 result = FALSE;
4205 if (evaluate)
4206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 int error = FALSE;
4208
4209 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004212 if (error)
4213 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 }
4215
4216 /*
4217 * Get the second variable.
4218 */
4219 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 return FAIL;
4222
4223 /*
4224 * Check for the ":".
4225 */
4226 if ((*arg)[0] != ':')
4227 {
4228 EMSG(_("E109: Missing ':' after '?'"));
4229 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 return FAIL;
4232 }
4233
4234 /*
4235 * Get the third variable.
4236 */
4237 *arg = skipwhite(*arg + 1);
4238 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4239 {
4240 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return FAIL;
4243 }
4244 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247
4248 return OK;
4249}
4250
4251/*
4252 * Handle first level expression:
4253 * expr2 || expr2 || expr2 logical OR
4254 *
4255 * "arg" must point to the first non-white of the expression.
4256 * "arg" is advanced to the next non-white after the recognized expression.
4257 *
4258 * Return OK or FAIL.
4259 */
4260 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 int evaluate;
4265{
Bram Moolenaar33570922005-01-25 22:26:29 +00004266 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 long result;
4268 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270
4271 /*
4272 * Get the first variable.
4273 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 return FAIL;
4276
4277 /*
4278 * Repeat until there is no following "||".
4279 */
4280 first = TRUE;
4281 result = FALSE;
4282 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4283 {
4284 if (evaluate && first)
4285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004289 if (error)
4290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 first = FALSE;
4292 }
4293
4294 /*
4295 * Get the second variable.
4296 */
4297 *arg = skipwhite(*arg + 2);
4298 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4299 return FAIL;
4300
4301 /*
4302 * Compute the result.
4303 */
4304 if (evaluate && !result)
4305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 if (error)
4310 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312 if (evaluate)
4313 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 rettv->v_type = VAR_NUMBER;
4315 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 }
4318
4319 return OK;
4320}
4321
4322/*
4323 * Handle second level expression:
4324 * expr3 && expr3 && expr3 logical AND
4325 *
4326 * "arg" must point to the first non-white of the expression.
4327 * "arg" is advanced to the next non-white after the recognized expression.
4328 *
4329 * Return OK or FAIL.
4330 */
4331 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 int evaluate;
4336{
Bram Moolenaar33570922005-01-25 22:26:29 +00004337 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 long result;
4339 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
4342 /*
4343 * Get the first variable.
4344 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 return FAIL;
4347
4348 /*
4349 * Repeat until there is no following "&&".
4350 */
4351 first = TRUE;
4352 result = TRUE;
4353 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4354 {
4355 if (evaluate && first)
4356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004359 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 if (error)
4361 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 first = FALSE;
4363 }
4364
4365 /*
4366 * Get the second variable.
4367 */
4368 *arg = skipwhite(*arg + 2);
4369 if (eval4(arg, &var2, evaluate && result) == FAIL)
4370 return FAIL;
4371
4372 /*
4373 * Compute the result.
4374 */
4375 if (evaluate && result)
4376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004377 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004379 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004380 if (error)
4381 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383 if (evaluate)
4384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 rettv->v_type = VAR_NUMBER;
4386 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
4388 }
4389
4390 return OK;
4391}
4392
4393/*
4394 * Handle third level expression:
4395 * var1 == var2
4396 * var1 =~ var2
4397 * var1 != var2
4398 * var1 !~ var2
4399 * var1 > var2
4400 * var1 >= var2
4401 * var1 < var2
4402 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004403 * var1 is var2
4404 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 *
4406 * "arg" must point to the first non-white of the expression.
4407 * "arg" is advanced to the next non-white after the recognized expression.
4408 *
4409 * Return OK or FAIL.
4410 */
4411 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004412eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004414 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 int evaluate;
4416{
Bram Moolenaar33570922005-01-25 22:26:29 +00004417 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 char_u *p;
4419 int i;
4420 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004421 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 int len = 2;
4423 long n1, n2;
4424 char_u *s1, *s2;
4425 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4426 regmatch_T regmatch;
4427 int ic;
4428 char_u *save_cpo;
4429
4430 /*
4431 * Get the first variable.
4432 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004433 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 return FAIL;
4435
4436 p = *arg;
4437 switch (p[0])
4438 {
4439 case '=': if (p[1] == '=')
4440 type = TYPE_EQUAL;
4441 else if (p[1] == '~')
4442 type = TYPE_MATCH;
4443 break;
4444 case '!': if (p[1] == '=')
4445 type = TYPE_NEQUAL;
4446 else if (p[1] == '~')
4447 type = TYPE_NOMATCH;
4448 break;
4449 case '>': if (p[1] != '=')
4450 {
4451 type = TYPE_GREATER;
4452 len = 1;
4453 }
4454 else
4455 type = TYPE_GEQUAL;
4456 break;
4457 case '<': if (p[1] != '=')
4458 {
4459 type = TYPE_SMALLER;
4460 len = 1;
4461 }
4462 else
4463 type = TYPE_SEQUAL;
4464 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 case 'i': if (p[1] == 's')
4466 {
4467 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4468 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004469 i = p[len];
4470 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004471 {
4472 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4473 type_is = TRUE;
4474 }
4475 }
4476 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 }
4478
4479 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004480 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 */
4482 if (type != TYPE_UNKNOWN)
4483 {
4484 /* extra question mark appended: ignore case */
4485 if (p[len] == '?')
4486 {
4487 ic = TRUE;
4488 ++len;
4489 }
4490 /* extra '#' appended: match case */
4491 else if (p[len] == '#')
4492 {
4493 ic = FALSE;
4494 ++len;
4495 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004496 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 else
4498 ic = p_ic;
4499
4500 /*
4501 * Get the second variable.
4502 */
4503 *arg = skipwhite(p + len);
4504 if (eval5(arg, &var2, evaluate) == FAIL)
4505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004506 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 return FAIL;
4508 }
4509
4510 if (evaluate)
4511 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 if (type_is && rettv->v_type != var2.v_type)
4513 {
4514 /* For "is" a different type always means FALSE, for "notis"
4515 * it means TRUE. */
4516 n1 = (type == TYPE_NEQUAL);
4517 }
4518 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4519 {
4520 if (type_is)
4521 {
4522 n1 = (rettv->v_type == var2.v_type
4523 && rettv->vval.v_list == var2.vval.v_list);
4524 if (type == TYPE_NEQUAL)
4525 n1 = !n1;
4526 }
4527 else if (rettv->v_type != var2.v_type
4528 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4529 {
4530 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004531 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004532 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004533 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004534 clear_tv(rettv);
4535 clear_tv(&var2);
4536 return FAIL;
4537 }
4538 else
4539 {
4540 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004541 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4542 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 if (type == TYPE_NEQUAL)
4544 n1 = !n1;
4545 }
4546 }
4547
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004548 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4549 {
4550 if (type_is)
4551 {
4552 n1 = (rettv->v_type == var2.v_type
4553 && rettv->vval.v_dict == var2.vval.v_dict);
4554 if (type == TYPE_NEQUAL)
4555 n1 = !n1;
4556 }
4557 else if (rettv->v_type != var2.v_type
4558 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4559 {
4560 if (rettv->v_type != var2.v_type)
4561 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4562 else
4563 EMSG(_("E736: Invalid operation for Dictionary"));
4564 clear_tv(rettv);
4565 clear_tv(&var2);
4566 return FAIL;
4567 }
4568 else
4569 {
4570 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004571 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4572 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004573 if (type == TYPE_NEQUAL)
4574 n1 = !n1;
4575 }
4576 }
4577
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004578 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4579 {
4580 if (rettv->v_type != var2.v_type
4581 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4582 {
4583 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004584 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004585 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004586 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004587 clear_tv(rettv);
4588 clear_tv(&var2);
4589 return FAIL;
4590 }
4591 else
4592 {
4593 /* Compare two Funcrefs for being equal or unequal. */
4594 if (rettv->vval.v_string == NULL
4595 || var2.vval.v_string == NULL)
4596 n1 = FALSE;
4597 else
4598 n1 = STRCMP(rettv->vval.v_string,
4599 var2.vval.v_string) == 0;
4600 if (type == TYPE_NEQUAL)
4601 n1 = !n1;
4602 }
4603 }
4604
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004605#ifdef FEAT_FLOAT
4606 /*
4607 * If one of the two variables is a float, compare as a float.
4608 * When using "=~" or "!~", always compare as string.
4609 */
4610 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4611 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4612 {
4613 float_T f1, f2;
4614
4615 if (rettv->v_type == VAR_FLOAT)
4616 f1 = rettv->vval.v_float;
4617 else
4618 f1 = get_tv_number(rettv);
4619 if (var2.v_type == VAR_FLOAT)
4620 f2 = var2.vval.v_float;
4621 else
4622 f2 = get_tv_number(&var2);
4623 n1 = FALSE;
4624 switch (type)
4625 {
4626 case TYPE_EQUAL: n1 = (f1 == f2); break;
4627 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4628 case TYPE_GREATER: n1 = (f1 > f2); break;
4629 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4630 case TYPE_SMALLER: n1 = (f1 < f2); break;
4631 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4632 case TYPE_UNKNOWN:
4633 case TYPE_MATCH:
4634 case TYPE_NOMATCH: break; /* avoid gcc warning */
4635 }
4636 }
4637#endif
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 /*
4640 * If one of the two variables is a number, compare as a number.
4641 * When using "=~" or "!~", always compare as string.
4642 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004643 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4645 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004646 n1 = get_tv_number(rettv);
4647 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 switch (type)
4649 {
4650 case TYPE_EQUAL: n1 = (n1 == n2); break;
4651 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4652 case TYPE_GREATER: n1 = (n1 > n2); break;
4653 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4654 case TYPE_SMALLER: n1 = (n1 < n2); break;
4655 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4656 case TYPE_UNKNOWN:
4657 case TYPE_MATCH:
4658 case TYPE_NOMATCH: break; /* avoid gcc warning */
4659 }
4660 }
4661 else
4662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004663 s1 = get_tv_string_buf(rettv, buf1);
4664 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4666 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4667 else
4668 i = 0;
4669 n1 = FALSE;
4670 switch (type)
4671 {
4672 case TYPE_EQUAL: n1 = (i == 0); break;
4673 case TYPE_NEQUAL: n1 = (i != 0); break;
4674 case TYPE_GREATER: n1 = (i > 0); break;
4675 case TYPE_GEQUAL: n1 = (i >= 0); break;
4676 case TYPE_SMALLER: n1 = (i < 0); break;
4677 case TYPE_SEQUAL: n1 = (i <= 0); break;
4678
4679 case TYPE_MATCH:
4680 case TYPE_NOMATCH:
4681 /* avoid 'l' flag in 'cpoptions' */
4682 save_cpo = p_cpo;
4683 p_cpo = (char_u *)"";
4684 regmatch.regprog = vim_regcomp(s2,
4685 RE_MAGIC + RE_STRING);
4686 regmatch.rm_ic = ic;
4687 if (regmatch.regprog != NULL)
4688 {
4689 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004690 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (type == TYPE_NOMATCH)
4692 n1 = !n1;
4693 }
4694 p_cpo = save_cpo;
4695 break;
4696
4697 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4698 }
4699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004700 clear_tv(rettv);
4701 clear_tv(&var2);
4702 rettv->v_type = VAR_NUMBER;
4703 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 }
4706
4707 return OK;
4708}
4709
4710/*
4711 * Handle fourth level expression:
4712 * + number addition
4713 * - number subtraction
4714 * . string concatenation
4715 *
4716 * "arg" must point to the first non-white of the expression.
4717 * "arg" is advanced to the next non-white after the recognized expression.
4718 *
4719 * Return OK or FAIL.
4720 */
4721 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004722eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 int evaluate;
4726{
Bram Moolenaar33570922005-01-25 22:26:29 +00004727 typval_T var2;
4728 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 int op;
4730 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731#ifdef FEAT_FLOAT
4732 float_T f1 = 0, f2 = 0;
4733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 char_u *s1, *s2;
4735 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4736 char_u *p;
4737
4738 /*
4739 * Get the first variable.
4740 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004741 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return FAIL;
4743
4744 /*
4745 * Repeat computing, until no '+', '-' or '.' is following.
4746 */
4747 for (;;)
4748 {
4749 op = **arg;
4750 if (op != '+' && op != '-' && op != '.')
4751 break;
4752
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004753 if ((op != '+' || rettv->v_type != VAR_LIST)
4754#ifdef FEAT_FLOAT
4755 && (op == '.' || rettv->v_type != VAR_FLOAT)
4756#endif
4757 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004758 {
4759 /* For "list + ...", an illegal use of the first operand as
4760 * a number cannot be determined before evaluating the 2nd
4761 * operand: if this is also a list, all is ok.
4762 * For "something . ...", "something - ..." or "non-list + ...",
4763 * we know that the first operand needs to be a string or number
4764 * without evaluating the 2nd operand. So check before to avoid
4765 * side effects after an error. */
4766 if (evaluate && get_tv_string_chk(rettv) == NULL)
4767 {
4768 clear_tv(rettv);
4769 return FAIL;
4770 }
4771 }
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 /*
4774 * Get the second variable.
4775 */
4776 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004777 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 return FAIL;
4781 }
4782
4783 if (evaluate)
4784 {
4785 /*
4786 * Compute the result.
4787 */
4788 if (op == '.')
4789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004790 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4791 s2 = get_tv_string_buf_chk(&var2, buf2);
4792 if (s2 == NULL) /* type error ? */
4793 {
4794 clear_tv(rettv);
4795 clear_tv(&var2);
4796 return FAIL;
4797 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004798 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004799 clear_tv(rettv);
4800 rettv->v_type = VAR_STRING;
4801 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004803 else if (op == '+' && rettv->v_type == VAR_LIST
4804 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004805 {
4806 /* concatenate Lists */
4807 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4808 &var3) == FAIL)
4809 {
4810 clear_tv(rettv);
4811 clear_tv(&var2);
4812 return FAIL;
4813 }
4814 clear_tv(rettv);
4815 *rettv = var3;
4816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 else
4818 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004819 int error = FALSE;
4820
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004821#ifdef FEAT_FLOAT
4822 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004823 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824 f1 = rettv->vval.v_float;
4825 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004827 else
4828#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830 n1 = get_tv_number_chk(rettv, &error);
4831 if (error)
4832 {
4833 /* This can only happen for "list + non-list". For
4834 * "non-list + ..." or "something - ...", we returned
4835 * before evaluating the 2nd operand. */
4836 clear_tv(rettv);
4837 return FAIL;
4838 }
4839#ifdef FEAT_FLOAT
4840 if (var2.v_type == VAR_FLOAT)
4841 f1 = n1;
4842#endif
4843 }
4844#ifdef FEAT_FLOAT
4845 if (var2.v_type == VAR_FLOAT)
4846 {
4847 f2 = var2.vval.v_float;
4848 n2 = 0;
4849 }
4850 else
4851#endif
4852 {
4853 n2 = get_tv_number_chk(&var2, &error);
4854 if (error)
4855 {
4856 clear_tv(rettv);
4857 clear_tv(&var2);
4858 return FAIL;
4859 }
4860#ifdef FEAT_FLOAT
4861 if (rettv->v_type == VAR_FLOAT)
4862 f2 = n2;
4863#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004865 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866
4867#ifdef FEAT_FLOAT
4868 /* If there is a float on either side the result is a float. */
4869 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4870 {
4871 if (op == '+')
4872 f1 = f1 + f2;
4873 else
4874 f1 = f1 - f2;
4875 rettv->v_type = VAR_FLOAT;
4876 rettv->vval.v_float = f1;
4877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879#endif
4880 {
4881 if (op == '+')
4882 n1 = n1 + n2;
4883 else
4884 n1 = n1 - n2;
4885 rettv->v_type = VAR_NUMBER;
4886 rettv->vval.v_number = n1;
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004889 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 }
4891 }
4892 return OK;
4893}
4894
4895/*
4896 * Handle fifth level expression:
4897 * * number multiplication
4898 * / number division
4899 * % number modulo
4900 *
4901 * "arg" must point to the first non-white of the expression.
4902 * "arg" is advanced to the next non-white after the recognized expression.
4903 *
4904 * Return OK or FAIL.
4905 */
4906 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004907eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004909 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004911 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912{
Bram Moolenaar33570922005-01-25 22:26:29 +00004913 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 int op;
4915 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004916#ifdef FEAT_FLOAT
4917 int use_float = FALSE;
4918 float_T f1 = 0, f2;
4919#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004920 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
4922 /*
4923 * Get the first variable.
4924 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004925 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return FAIL;
4927
4928 /*
4929 * Repeat computing, until no '*', '/' or '%' is following.
4930 */
4931 for (;;)
4932 {
4933 op = **arg;
4934 if (op != '*' && op != '/' && op != '%')
4935 break;
4936
4937 if (evaluate)
4938 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939#ifdef FEAT_FLOAT
4940 if (rettv->v_type == VAR_FLOAT)
4941 {
4942 f1 = rettv->vval.v_float;
4943 use_float = TRUE;
4944 n1 = 0;
4945 }
4946 else
4947#endif
4948 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004949 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004950 if (error)
4951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953 else
4954 n1 = 0;
4955
4956 /*
4957 * Get the second variable.
4958 */
4959 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004960 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 return FAIL;
4962
4963 if (evaluate)
4964 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965#ifdef FEAT_FLOAT
4966 if (var2.v_type == VAR_FLOAT)
4967 {
4968 if (!use_float)
4969 {
4970 f1 = n1;
4971 use_float = TRUE;
4972 }
4973 f2 = var2.vval.v_float;
4974 n2 = 0;
4975 }
4976 else
4977#endif
4978 {
4979 n2 = get_tv_number_chk(&var2, &error);
4980 clear_tv(&var2);
4981 if (error)
4982 return FAIL;
4983#ifdef FEAT_FLOAT
4984 if (use_float)
4985 f2 = n2;
4986#endif
4987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989 /*
4990 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993#ifdef FEAT_FLOAT
4994 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 if (op == '*')
4997 f1 = f1 * f2;
4998 else if (op == '/')
4999 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005000# ifdef VMS
5001 /* VMS crashes on divide by zero, work around it */
5002 if (f2 == 0.0)
5003 {
5004 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005005 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005006 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005007 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005008 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005009 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005010 }
5011 else
5012 f1 = f1 / f2;
5013# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014 /* We rely on the floating point library to handle divide
5015 * by zero to result in "inf" and not a crash. */
5016 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005017# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005021 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 return FAIL;
5023 }
5024 rettv->v_type = VAR_FLOAT;
5025 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030 if (op == '*')
5031 n1 = n1 * n2;
5032 else if (op == '/')
5033 {
5034 if (n2 == 0) /* give an error message? */
5035 {
5036 if (n1 == 0)
5037 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5038 else if (n1 < 0)
5039 n1 = -0x7fffffffL;
5040 else
5041 n1 = 0x7fffffffL;
5042 }
5043 else
5044 n1 = n1 / n2;
5045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005047 {
5048 if (n2 == 0) /* give an error message? */
5049 n1 = 0;
5050 else
5051 n1 = n1 % n2;
5052 }
5053 rettv->v_type = VAR_NUMBER;
5054 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 }
5058
5059 return OK;
5060}
5061
5062/*
5063 * Handle sixth level expression:
5064 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005065 * "string" string constant
5066 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 * &option-name option value
5068 * @r register contents
5069 * identifier variable value
5070 * function() function call
5071 * $VAR environment variable
5072 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005073 * [expr, expr] List
5074 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 *
5076 * Also handle:
5077 * ! in front logical NOT
5078 * - in front unary minus
5079 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 * trailing [] subscript in String or List
5081 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 *
5083 * "arg" must point to the first non-white of the expression.
5084 * "arg" is advanced to the next non-white after the recognized expression.
5085 *
5086 * Return OK or FAIL.
5087 */
5088 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005089eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005093 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 long n;
5096 int len;
5097 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 char_u *start_leader, *end_leader;
5099 int ret = OK;
5100 char_u *alias;
5101
5102 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005103 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005104 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005106 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 /*
5109 * Skip '!' and '-' characters. They are handled later.
5110 */
5111 start_leader = *arg;
5112 while (**arg == '!' || **arg == '-' || **arg == '+')
5113 *arg = skipwhite(*arg + 1);
5114 end_leader = *arg;
5115
5116 switch (**arg)
5117 {
5118 /*
5119 * Number constant.
5120 */
5121 case '0':
5122 case '1':
5123 case '2':
5124 case '3':
5125 case '4':
5126 case '5':
5127 case '6':
5128 case '7':
5129 case '8':
5130 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005131 {
5132#ifdef FEAT_FLOAT
5133 char_u *p = skipdigits(*arg + 1);
5134 int get_float = FALSE;
5135
5136 /* We accept a float when the format matches
5137 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005138 * strict to avoid backwards compatibility problems.
5139 * Don't look for a float after the "." operator, so that
5140 * ":let vers = 1.2.3" doesn't fail. */
5141 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005143 get_float = TRUE;
5144 p = skipdigits(p + 2);
5145 if (*p == 'e' || *p == 'E')
5146 {
5147 ++p;
5148 if (*p == '-' || *p == '+')
5149 ++p;
5150 if (!vim_isdigit(*p))
5151 get_float = FALSE;
5152 else
5153 p = skipdigits(p + 1);
5154 }
5155 if (ASCII_ISALPHA(*p) || *p == '.')
5156 get_float = FALSE;
5157 }
5158 if (get_float)
5159 {
5160 float_T f;
5161
5162 *arg += string2float(*arg, &f);
5163 if (evaluate)
5164 {
5165 rettv->v_type = VAR_FLOAT;
5166 rettv->vval.v_float = f;
5167 }
5168 }
5169 else
5170#endif
5171 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005172 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005173 *arg += len;
5174 if (evaluate)
5175 {
5176 rettv->v_type = VAR_NUMBER;
5177 rettv->vval.v_number = n;
5178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182
5183 /*
5184 * String constant: "string".
5185 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 break;
5188
5189 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005190 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005192 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193 break;
5194
5195 /*
5196 * List: [expr, expr]
5197 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005198 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 break;
5200
5201 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005202 * Dictionary: {key: val, key: val}
5203 */
5204 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5205 break;
5206
5207 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005208 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005210 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
5212
5213 /*
5214 * Environment variable: $VAR.
5215 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005216 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 break;
5218
5219 /*
5220 * Register contents: @r.
5221 */
5222 case '@': ++*arg;
5223 if (evaluate)
5224 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005225 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005226 rettv->vval.v_string = get_reg_contents(**arg,
5227 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229 if (**arg != NUL)
5230 ++*arg;
5231 break;
5232
5233 /*
5234 * nested expression: (expression).
5235 */
5236 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005237 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 if (**arg == ')')
5239 ++*arg;
5240 else if (ret == OK)
5241 {
5242 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005243 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 ret = FAIL;
5245 }
5246 break;
5247
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 break;
5250 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251
5252 if (ret == NOTDONE)
5253 {
5254 /*
5255 * Must be a variable or function name.
5256 * Can also be a curly-braces kind of name: {expr}.
5257 */
5258 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005259 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 if (alias != NULL)
5261 s = alias;
5262
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005263 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 ret = FAIL;
5265 else
5266 {
5267 if (**arg == '(') /* recursive! */
5268 {
5269 /* If "s" is the name of a variable of type VAR_FUNC
5270 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005271 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272
5273 /* Invoke the function. */
5274 ret = get_func_tv(s, len, rettv, arg,
5275 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005276 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005277
5278 /* If evaluate is FALSE rettv->v_type was not set in
5279 * get_func_tv, but it's needed in handle_subscript() to parse
5280 * what follows. So set it here. */
5281 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5282 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005283 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005284 rettv->v_type = VAR_FUNC;
5285 }
5286
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 /* Stop the expression evaluation when immediately
5288 * aborting on error, or when an interrupt occurred or
5289 * an exception was thrown but not caught. */
5290 if (aborting())
5291 {
5292 if (ret == OK)
5293 clear_tv(rettv);
5294 ret = FAIL;
5295 }
5296 }
5297 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005298 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005299 else
5300 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005302 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 }
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 *arg = skipwhite(*arg);
5306
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005307 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5308 * expr(expr). */
5309 if (ret == OK)
5310 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
5312 /*
5313 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5314 */
5315 if (ret == OK && evaluate && end_leader > start_leader)
5316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318 int val = 0;
5319#ifdef FEAT_FLOAT
5320 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005322 if (rettv->v_type == VAR_FLOAT)
5323 f = rettv->vval.v_float;
5324 else
5325#endif
5326 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005327 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 clear_tv(rettv);
5330 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 else
5333 {
5334 while (end_leader > start_leader)
5335 {
5336 --end_leader;
5337 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005338 {
5339#ifdef FEAT_FLOAT
5340 if (rettv->v_type == VAR_FLOAT)
5341 f = !f;
5342 else
5343#endif
5344 val = !val;
5345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005347 {
5348#ifdef FEAT_FLOAT
5349 if (rettv->v_type == VAR_FLOAT)
5350 f = -f;
5351 else
5352#endif
5353 val = -val;
5354 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005355 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005356#ifdef FEAT_FLOAT
5357 if (rettv->v_type == VAR_FLOAT)
5358 {
5359 clear_tv(rettv);
5360 rettv->vval.v_float = f;
5361 }
5362 else
5363#endif
5364 {
5365 clear_tv(rettv);
5366 rettv->v_type = VAR_NUMBER;
5367 rettv->vval.v_number = val;
5368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371
5372 return ret;
5373}
5374
5375/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005376 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5377 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5379 */
5380 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005381eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005383 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005385 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386{
5387 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005388 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005390 long len = -1;
5391 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005395 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005397 if (verbose)
5398 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 return FAIL;
5400 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005401#ifdef FEAT_FLOAT
5402 else if (rettv->v_type == VAR_FLOAT)
5403 {
5404 if (verbose)
5405 EMSG(_(e_float_as_string));
5406 return FAIL;
5407 }
5408#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005409 else if (rettv->v_type == VAR_SPECIAL)
5410 {
5411 return FAIL;
5412 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005414 init_tv(&var1);
5415 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005418 /*
5419 * dict.name
5420 */
5421 key = *arg + 1;
5422 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5423 ;
5424 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005426 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 }
5428 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 /*
5431 * something[idx]
5432 *
5433 * Get the (first) variable from inside the [].
5434 */
5435 *arg = skipwhite(*arg + 1);
5436 if (**arg == ':')
5437 empty1 = TRUE;
5438 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5439 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005440 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5441 {
5442 /* not a number or string */
5443 clear_tv(&var1);
5444 return FAIL;
5445 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446
5447 /*
5448 * Get the second variable from inside the [:].
5449 */
5450 if (**arg == ':')
5451 {
5452 range = TRUE;
5453 *arg = skipwhite(*arg + 1);
5454 if (**arg == ']')
5455 empty2 = TRUE;
5456 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005458 if (!empty1)
5459 clear_tv(&var1);
5460 return FAIL;
5461 }
5462 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5463 {
5464 /* not a number or string */
5465 if (!empty1)
5466 clear_tv(&var1);
5467 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468 return FAIL;
5469 }
5470 }
5471
5472 /* Check for the ']'. */
5473 if (**arg != ']')
5474 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005475 if (verbose)
5476 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005477 clear_tv(&var1);
5478 if (range)
5479 clear_tv(&var2);
5480 return FAIL;
5481 }
5482 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484
5485 if (evaluate)
5486 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 n1 = 0;
5488 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 n1 = get_tv_number(&var1);
5491 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 }
5493 if (range)
5494 {
5495 if (empty2)
5496 n2 = -1;
5497 else
5498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 n2 = get_tv_number(&var2);
5500 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 }
5502 }
5503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 {
5506 case VAR_NUMBER:
5507 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 len = (long)STRLEN(s);
5510 if (range)
5511 {
5512 /* The resulting variable is a substring. If the indexes
5513 * are out of range the result is empty. */
5514 if (n1 < 0)
5515 {
5516 n1 = len + n1;
5517 if (n1 < 0)
5518 n1 = 0;
5519 }
5520 if (n2 < 0)
5521 n2 = len + n2;
5522 else if (n2 >= len)
5523 n2 = len;
5524 if (n1 >= len || n2 < 0 || n1 > n2)
5525 s = NULL;
5526 else
5527 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5528 }
5529 else
5530 {
5531 /* The resulting variable is a string of a single
5532 * character. If the index is too big or negative the
5533 * result is empty. */
5534 if (n1 >= len || n1 < 0)
5535 s = NULL;
5536 else
5537 s = vim_strnsave(s + n1, 1);
5538 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 clear_tv(rettv);
5540 rettv->v_type = VAR_STRING;
5541 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 break;
5543
5544 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005545 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546 if (n1 < 0)
5547 n1 = len + n1;
5548 if (!empty1 && (n1 < 0 || n1 >= len))
5549 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005550 /* For a range we allow invalid values and return an empty
5551 * list. A list index out of range is an error. */
5552 if (!range)
5553 {
5554 if (verbose)
5555 EMSGN(_(e_listidx), n1);
5556 return FAIL;
5557 }
5558 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 }
5560 if (range)
5561 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005562 list_T *l;
5563 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564
5565 if (n2 < 0)
5566 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005567 else if (n2 >= len)
5568 n2 = len - 1;
5569 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005570 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 l = list_alloc();
5572 if (l == NULL)
5573 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575 n1 <= n2; ++n1)
5576 {
5577 if (list_append_tv(l, &item->li_tv) == FAIL)
5578 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005579 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 return FAIL;
5581 }
5582 item = item->li_next;
5583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 clear_tv(rettv);
5585 rettv->v_type = VAR_LIST;
5586 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005587 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 }
5589 else
5590 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005591 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005592 clear_tv(rettv);
5593 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594 }
5595 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596
5597 case VAR_DICT:
5598 if (range)
5599 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 if (verbose)
5601 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 if (len == -1)
5603 clear_tv(&var1);
5604 return FAIL;
5605 }
5606 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005607 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608
5609 if (len == -1)
5610 {
5611 key = get_tv_string(&var1);
5612 if (*key == NUL)
5613 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005614 if (verbose)
5615 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 clear_tv(&var1);
5617 return FAIL;
5618 }
5619 }
5620
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005621 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005623 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005624 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625 if (len == -1)
5626 clear_tv(&var1);
5627 if (item == NULL)
5628 return FAIL;
5629
5630 copy_tv(&item->di_tv, &var1);
5631 clear_tv(rettv);
5632 *rettv = var1;
5633 }
5634 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 }
5636 }
5637
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005638 return OK;
5639}
5640
5641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 * Get an option value.
5643 * "arg" points to the '&' or '+' before the option name.
5644 * "arg" is advanced to character after the option name.
5645 * Return OK or FAIL.
5646 */
5647 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005648get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005650 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 int evaluate;
5652{
5653 char_u *option_end;
5654 long numval;
5655 char_u *stringval;
5656 int opt_type;
5657 int c;
5658 int working = (**arg == '+'); /* has("+option") */
5659 int ret = OK;
5660 int opt_flags;
5661
5662 /*
5663 * Isolate the option name and find its value.
5664 */
5665 option_end = find_option_end(arg, &opt_flags);
5666 if (option_end == NULL)
5667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 EMSG2(_("E112: Option name missing: %s"), *arg);
5670 return FAIL;
5671 }
5672
5673 if (!evaluate)
5674 {
5675 *arg = option_end;
5676 return OK;
5677 }
5678
5679 c = *option_end;
5680 *option_end = NUL;
5681 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005682 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
5684 if (opt_type == -3) /* invalid name */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 EMSG2(_("E113: Unknown option: %s"), *arg);
5688 ret = FAIL;
5689 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {
5692 if (opt_type == -2) /* hidden string option */
5693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 rettv->v_type = VAR_STRING;
5695 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 else if (opt_type == -1) /* hidden number option */
5698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005699 rettv->v_type = VAR_NUMBER;
5700 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
5702 else if (opt_type == 1) /* number option */
5703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 rettv->v_type = VAR_NUMBER;
5705 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707 else /* string option */
5708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005709 rettv->v_type = VAR_STRING;
5710 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
5712 }
5713 else if (working && (opt_type == -2 || opt_type == -1))
5714 ret = FAIL;
5715
5716 *option_end = c; /* put back for error messages */
5717 *arg = option_end;
5718
5719 return ret;
5720}
5721
5722/*
5723 * Allocate a variable for a string constant.
5724 * Return OK or FAIL.
5725 */
5726 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005727get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 int evaluate;
5731{
5732 char_u *p;
5733 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 int extra = 0;
5735
5736 /*
5737 * Find the end of the string, skipping backslashed characters.
5738 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005739 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
5741 if (*p == '\\' && p[1] != NUL)
5742 {
5743 ++p;
5744 /* A "\<x>" form occupies at least 4 characters, and produces up
5745 * to 6 characters: reserve space for 2 extra */
5746 if (*p == '<')
5747 extra += 2;
5748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 }
5750
5751 if (*p != '"')
5752 {
5753 EMSG2(_("E114: Missing quote: %s"), *arg);
5754 return FAIL;
5755 }
5756
5757 /* If only parsing, set *arg and return here */
5758 if (!evaluate)
5759 {
5760 *arg = p + 1;
5761 return OK;
5762 }
5763
5764 /*
5765 * Copy the string into allocated memory, handling backslashed
5766 * characters.
5767 */
5768 name = alloc((unsigned)(p - *arg + extra));
5769 if (name == NULL)
5770 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005771 rettv->v_type = VAR_STRING;
5772 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 {
5776 if (*p == '\\')
5777 {
5778 switch (*++p)
5779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 case 'b': *name++ = BS; ++p; break;
5781 case 'e': *name++ = ESC; ++p; break;
5782 case 'f': *name++ = FF; ++p; break;
5783 case 'n': *name++ = NL; ++p; break;
5784 case 'r': *name++ = CAR; ++p; break;
5785 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787 case 'X': /* hex: "\x1", "\x12" */
5788 case 'x':
5789 case 'u': /* Unicode: "\u0023" */
5790 case 'U':
5791 if (vim_isxdigit(p[1]))
5792 {
5793 int n, nr;
5794 int c = toupper(*p);
5795
5796 if (c == 'X')
5797 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005798 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005800 else
5801 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 nr = 0;
5803 while (--n >= 0 && vim_isxdigit(p[1]))
5804 {
5805 ++p;
5806 nr = (nr << 4) + hex2nr(*p);
5807 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809#ifdef FEAT_MBYTE
5810 /* For "\u" store the number according to
5811 * 'encoding'. */
5812 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 else
5815#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819
5820 /* octal: "\1", "\12", "\123" */
5821 case '0':
5822 case '1':
5823 case '2':
5824 case '3':
5825 case '4':
5826 case '5':
5827 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 case '7': *name = *p++ - '0';
5829 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 *name = (*name << 3) + *p++ - '0';
5832 if (*p >= '0' && *p <= '7')
5833 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 break;
5837
5838 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 if (extra != 0)
5841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 break;
5844 }
5845 /* FALLTHROUGH */
5846
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 break;
5849 }
5850 }
5851 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 *arg = p + 1;
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 return OK;
5859}
5860
5861/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 * Return OK or FAIL.
5864 */
5865 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005866get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 int evaluate;
5870{
5871 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 int reduce = 0;
5874
5875 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 */
5878 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5879 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 break;
5884 ++reduce;
5885 ++p;
5886 }
5887 }
5888
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 return FAIL;
5893 }
5894
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 if (!evaluate)
5897 {
5898 *arg = p + 1;
5899 return OK;
5900 }
5901
5902 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 */
5905 str = alloc((unsigned)((p - *arg) - reduce));
5906 if (str == NULL)
5907 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 rettv->v_type = VAR_STRING;
5909 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005910
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005916 break;
5917 ++p;
5918 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 *arg = p + 1;
5923
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 return OK;
5925}
5926
5927/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 * Allocate a variable for a List and fill it from "*arg".
5929 * Return OK or FAIL.
5930 */
5931 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005932get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005934 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 int evaluate;
5936{
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 list_T *l = NULL;
5938 typval_T tv;
5939 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940
5941 if (evaluate)
5942 {
5943 l = list_alloc();
5944 if (l == NULL)
5945 return FAIL;
5946 }
5947
5948 *arg = skipwhite(*arg + 1);
5949 while (**arg != ']' && **arg != NUL)
5950 {
5951 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5952 goto failret;
5953 if (evaluate)
5954 {
5955 item = listitem_alloc();
5956 if (item != NULL)
5957 {
5958 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005959 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 list_append(l, item);
5961 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005962 else
5963 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 }
5965
5966 if (**arg == ']')
5967 break;
5968 if (**arg != ',')
5969 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005970 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 goto failret;
5972 }
5973 *arg = skipwhite(*arg + 1);
5974 }
5975
5976 if (**arg != ']')
5977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005978 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979failret:
5980 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005981 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 return FAIL;
5983 }
5984
5985 *arg = skipwhite(*arg + 1);
5986 if (evaluate)
5987 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005988 rettv->v_type = VAR_LIST;
5989 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 ++l->lv_refcount;
5991 }
5992
5993 return OK;
5994}
5995
5996/*
5997 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005998 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006000 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001list_alloc()
6002{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006003 list_T *l;
6004
6005 l = (list_T *)alloc_clear(sizeof(list_T));
6006 if (l != NULL)
6007 {
6008 /* Prepend the list to the list of lists for garbage collection. */
6009 if (first_list != NULL)
6010 first_list->lv_used_prev = l;
6011 l->lv_used_prev = NULL;
6012 l->lv_used_next = first_list;
6013 first_list = l;
6014 }
6015 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016}
6017
6018/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006019 * Allocate an empty list for a return value.
6020 * Returns OK or FAIL.
6021 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006022 int
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006023rettv_list_alloc(rettv)
6024 typval_T *rettv;
6025{
6026 list_T *l = list_alloc();
6027
6028 if (l == NULL)
6029 return FAIL;
6030
6031 rettv->vval.v_list = l;
6032 rettv->v_type = VAR_LIST;
6033 ++l->lv_refcount;
6034 return OK;
6035}
6036
6037/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038 * Unreference a list: decrement the reference count and free it when it
6039 * becomes zero.
6040 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006041 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006045 if (l != NULL && --l->lv_refcount <= 0)
6046 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047}
6048
6049/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006050 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 * Ignores the reference count.
6052 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006053 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006054list_free(l, recurse)
6055 list_T *l;
6056 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057{
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006060 /* Remove the list from the list of lists for garbage collection. */
6061 if (l->lv_used_prev == NULL)
6062 first_list = l->lv_used_next;
6063 else
6064 l->lv_used_prev->lv_used_next = l->lv_used_next;
6065 if (l->lv_used_next != NULL)
6066 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6067
Bram Moolenaard9fba312005-06-26 22:34:35 +00006068 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006070 /* Remove the item before deleting it. */
6071 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006072 if (recurse || (item->li_tv.v_type != VAR_LIST
6073 && item->li_tv.v_type != VAR_DICT))
6074 clear_tv(&item->li_tv);
6075 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 }
6077 vim_free(l);
6078}
6079
6080/*
6081 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006082 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006084 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085listitem_alloc()
6086{
Bram Moolenaar33570922005-01-25 22:26:29 +00006087 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088}
6089
6090/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006091 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006093 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006095 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006097 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 vim_free(item);
6099}
6100
6101/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006102 * Remove a list item from a List and free it. Also clears the value.
6103 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006104 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006105listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 list_T *l;
6107 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006108{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006109 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006110 listitem_free(item);
6111}
6112
6113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114 * Get the number of items in a list.
6115 */
6116 static long
6117list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006118 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006119{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 if (l == NULL)
6121 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006122 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006123}
6124
6125/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126 * Return TRUE when two lists have exactly the same values.
6127 */
6128 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 list_T *l1;
6131 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006133 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006134{
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006136
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006137 if (l1 == NULL || l2 == NULL)
6138 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006139 if (l1 == l2)
6140 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141 if (list_len(l1) != list_len(l2))
6142 return FALSE;
6143
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006144 for (item1 = l1->lv_first, item2 = l2->lv_first;
6145 item1 != NULL && item2 != NULL;
6146 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006147 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006148 return FALSE;
6149 return item1 == NULL && item2 == NULL;
6150}
6151
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006152/*
6153 * Return the dictitem that an entry in a hashtable points to.
6154 */
6155 dictitem_T *
6156dict_lookup(hi)
6157 hashitem_T *hi;
6158{
6159 return HI2DI(hi);
6160}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006161
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006162/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163 * Return TRUE when two dictionaries have exactly the same key/values.
6164 */
6165 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006166dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 dict_T *d1;
6168 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006170 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006171{
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 hashitem_T *hi;
6173 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006174 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006175
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006176 if (d1 == NULL || d2 == NULL)
6177 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006178 if (d1 == d2)
6179 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006180 if (dict_len(d1) != dict_len(d2))
6181 return FALSE;
6182
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006183 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006185 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006186 if (!HASHITEM_EMPTY(hi))
6187 {
6188 item2 = dict_find(d2, hi->hi_key, -1);
6189 if (item2 == NULL)
6190 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006192 return FALSE;
6193 --todo;
6194 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006195 }
6196 return TRUE;
6197}
6198
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006199static int tv_equal_recurse_limit;
6200
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006201/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006202 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006204 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205 */
6206 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006208 typval_T *tv1;
6209 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006210 int ic; /* ignore case */
6211 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006212{
6213 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006214 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006215 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006216 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006217
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006218 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006219 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006220
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006221 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006222 * recursiveness to a limit. We guess they are equal then.
6223 * A fixed limit has the problem of still taking an awful long time.
6224 * Reduce the limit every time running into it. That should work fine for
6225 * deeply linked structures that are not recursively linked and catch
6226 * recursiveness quickly. */
6227 if (!recursive)
6228 tv_equal_recurse_limit = 1000;
6229 if (recursive_cnt >= tv_equal_recurse_limit)
6230 {
6231 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006232 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006233 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006234
6235 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006236 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006237 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006238 ++recursive_cnt;
6239 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6240 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006241 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242
6243 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006244 ++recursive_cnt;
6245 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6246 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006247 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006248
6249 case VAR_FUNC:
6250 return (tv1->vval.v_string != NULL
6251 && tv2->vval.v_string != NULL
6252 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6253
6254 case VAR_NUMBER:
6255 return tv1->vval.v_number == tv2->vval.v_number;
6256
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006257#ifdef FEAT_FLOAT
6258 case VAR_FLOAT:
6259 return tv1->vval.v_float == tv2->vval.v_float;
6260#endif
6261
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006262 case VAR_STRING:
6263 s1 = get_tv_string_buf(tv1, buf1);
6264 s2 = get_tv_string_buf(tv2, buf2);
6265 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006266
6267 case VAR_SPECIAL:
6268 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006269 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006270
6271 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006272 return TRUE;
6273}
6274
6275/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276 * Locate item with index "n" in list "l" and return it.
6277 * A negative index is counted from the end; -1 is the last item.
6278 * Returns NULL when "n" is out of range.
6279 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006280 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006282 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 long n;
6284{
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 long idx;
6287
6288 if (l == NULL)
6289 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006290
6291 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006293 n = l->lv_len + n;
6294
6295 /* Check for index out of range. */
6296 if (n < 0 || n >= l->lv_len)
6297 return NULL;
6298
6299 /* When there is a cached index may start search from there. */
6300 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006302 if (n < l->lv_idx / 2)
6303 {
6304 /* closest to the start of the list */
6305 item = l->lv_first;
6306 idx = 0;
6307 }
6308 else if (n > (l->lv_idx + l->lv_len) / 2)
6309 {
6310 /* closest to the end of the list */
6311 item = l->lv_last;
6312 idx = l->lv_len - 1;
6313 }
6314 else
6315 {
6316 /* closest to the cached index */
6317 item = l->lv_idx_item;
6318 idx = l->lv_idx;
6319 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320 }
6321 else
6322 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006323 if (n < l->lv_len / 2)
6324 {
6325 /* closest to the start of the list */
6326 item = l->lv_first;
6327 idx = 0;
6328 }
6329 else
6330 {
6331 /* closest to the end of the list */
6332 item = l->lv_last;
6333 idx = l->lv_len - 1;
6334 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006335 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006336
6337 while (n > idx)
6338 {
6339 /* search forward */
6340 item = item->li_next;
6341 ++idx;
6342 }
6343 while (n < idx)
6344 {
6345 /* search backward */
6346 item = item->li_prev;
6347 --idx;
6348 }
6349
6350 /* cache the used index */
6351 l->lv_idx = idx;
6352 l->lv_idx_item = item;
6353
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354 return item;
6355}
6356
6357/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006358 * Get list item "l[idx]" as a number.
6359 */
6360 static long
6361list_find_nr(l, idx, errorp)
6362 list_T *l;
6363 long idx;
6364 int *errorp; /* set to TRUE when something wrong */
6365{
6366 listitem_T *li;
6367
6368 li = list_find(l, idx);
6369 if (li == NULL)
6370 {
6371 if (errorp != NULL)
6372 *errorp = TRUE;
6373 return -1L;
6374 }
6375 return get_tv_number_chk(&li->li_tv, errorp);
6376}
6377
6378/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006379 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6380 */
6381 char_u *
6382list_find_str(l, idx)
6383 list_T *l;
6384 long idx;
6385{
6386 listitem_T *li;
6387
6388 li = list_find(l, idx - 1);
6389 if (li == NULL)
6390 {
6391 EMSGN(_(e_listidx), idx);
6392 return NULL;
6393 }
6394 return get_tv_string(&li->li_tv);
6395}
6396
6397/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006398 * Locate "item" list "l" and return its index.
6399 * Returns -1 when "item" is not in the list.
6400 */
6401 static long
6402list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 list_T *l;
6404 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006405{
6406 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006408
6409 if (l == NULL)
6410 return -1;
6411 idx = 0;
6412 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6413 ++idx;
6414 if (li == NULL)
6415 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006416 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006417}
6418
6419/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420 * Append item "item" to the end of list "l".
6421 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006422 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006424 list_T *l;
6425 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426{
6427 if (l->lv_last == NULL)
6428 {
6429 /* empty list */
6430 l->lv_first = item;
6431 l->lv_last = item;
6432 item->li_prev = NULL;
6433 }
6434 else
6435 {
6436 l->lv_last->li_next = item;
6437 item->li_prev = l->lv_last;
6438 l->lv_last = item;
6439 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006440 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441 item->li_next = NULL;
6442}
6443
6444/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006445 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006446 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006448 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006449list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006450 list_T *l;
6451 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006452{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006453 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454
Bram Moolenaar05159a02005-02-26 23:04:13 +00006455 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006456 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006457 copy_tv(tv, &li->li_tv);
6458 list_append(l, li);
6459 return OK;
6460}
6461
6462/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006463 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006464 * Return FAIL when out of memory.
6465 */
6466 int
6467list_append_dict(list, dict)
6468 list_T *list;
6469 dict_T *dict;
6470{
6471 listitem_T *li = listitem_alloc();
6472
6473 if (li == NULL)
6474 return FAIL;
6475 li->li_tv.v_type = VAR_DICT;
6476 li->li_tv.v_lock = 0;
6477 li->li_tv.vval.v_dict = dict;
6478 list_append(list, li);
6479 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480 return OK;
6481}
6482
6483/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006484 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006485 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006486 * Returns FAIL when out of memory.
6487 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006488 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006489list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006490 list_T *l;
6491 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006492 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006493{
6494 listitem_T *li = listitem_alloc();
6495
6496 if (li == NULL)
6497 return FAIL;
6498 list_append(l, li);
6499 li->li_tv.v_type = VAR_STRING;
6500 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006501 if (str == NULL)
6502 li->li_tv.vval.v_string = NULL;
6503 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006504 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006505 return FAIL;
6506 return OK;
6507}
6508
6509/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006510 * Append "n" to list "l".
6511 * Returns FAIL when out of memory.
6512 */
6513 static int
6514list_append_number(l, n)
6515 list_T *l;
6516 varnumber_T n;
6517{
6518 listitem_T *li;
6519
6520 li = listitem_alloc();
6521 if (li == NULL)
6522 return FAIL;
6523 li->li_tv.v_type = VAR_NUMBER;
6524 li->li_tv.v_lock = 0;
6525 li->li_tv.vval.v_number = n;
6526 list_append(l, li);
6527 return OK;
6528}
6529
6530/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 * If "item" is NULL append at the end.
6533 * Return FAIL when out of memory.
6534 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006535 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006537 list_T *l;
6538 typval_T *tv;
6539 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540{
Bram Moolenaar33570922005-01-25 22:26:29 +00006541 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542
6543 if (ni == NULL)
6544 return FAIL;
6545 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006546 list_insert(l, ni, item);
6547 return OK;
6548}
6549
6550 void
6551list_insert(l, ni, item)
6552 list_T *l;
6553 listitem_T *ni;
6554 listitem_T *item;
6555{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556 if (item == NULL)
6557 /* Append new item at end of list. */
6558 list_append(l, ni);
6559 else
6560 {
6561 /* Insert new item before existing item. */
6562 ni->li_prev = item->li_prev;
6563 ni->li_next = item;
6564 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006565 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006567 ++l->lv_idx;
6568 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006570 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006572 l->lv_idx_item = NULL;
6573 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006575 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006577}
6578
6579/*
6580 * Extend "l1" with "l2".
6581 * If "bef" is NULL append at the end, otherwise insert before this item.
6582 * Returns FAIL when out of memory.
6583 */
6584 static int
6585list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 list_T *l1;
6587 list_T *l2;
6588 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589{
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006591 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006593 /* We also quit the loop when we have inserted the original item count of
6594 * the list, avoid a hang when we extend a list with itself. */
6595 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006596 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6597 return FAIL;
6598 return OK;
6599}
6600
6601/*
6602 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6603 * Return FAIL when out of memory.
6604 */
6605 static int
6606list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006607 list_T *l1;
6608 list_T *l2;
6609 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006610{
Bram Moolenaar33570922005-01-25 22:26:29 +00006611 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006612
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006613 if (l1 == NULL || l2 == NULL)
6614 return FAIL;
6615
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006616 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006617 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006618 if (l == NULL)
6619 return FAIL;
6620 tv->v_type = VAR_LIST;
6621 tv->vval.v_list = l;
6622
6623 /* append all items from the second list */
6624 return list_extend(l, l2, NULL);
6625}
6626
6627/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006628 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006630 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 * Returns NULL when out of memory.
6632 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006633 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006634list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006635 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006636 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006637 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638{
Bram Moolenaar33570922005-01-25 22:26:29 +00006639 list_T *copy;
6640 listitem_T *item;
6641 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642
6643 if (orig == NULL)
6644 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645
6646 copy = list_alloc();
6647 if (copy != NULL)
6648 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006649 if (copyID != 0)
6650 {
6651 /* Do this before adding the items, because one of the items may
6652 * refer back to this list. */
6653 orig->lv_copyID = copyID;
6654 orig->lv_copylist = copy;
6655 }
6656 for (item = orig->lv_first; item != NULL && !got_int;
6657 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658 {
6659 ni = listitem_alloc();
6660 if (ni == NULL)
6661 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006662 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006663 {
6664 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6665 {
6666 vim_free(ni);
6667 break;
6668 }
6669 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006671 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672 list_append(copy, ni);
6673 }
6674 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 if (item != NULL)
6676 {
6677 list_unref(copy);
6678 copy = NULL;
6679 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680 }
6681
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682 return copy;
6683}
6684
6685/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006686 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006687 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006688 * This used to be called list_remove, but that conflicts with a Sun header
6689 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006691 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006692vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006693 list_T *l;
6694 listitem_T *item;
6695 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006696{
Bram Moolenaar33570922005-01-25 22:26:29 +00006697 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006698
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006699 /* notify watchers */
6700 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006701 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006702 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006703 list_fix_watch(l, ip);
6704 if (ip == item2)
6705 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006707
6708 if (item2->li_next == NULL)
6709 l->lv_last = item->li_prev;
6710 else
6711 item2->li_next->li_prev = item->li_prev;
6712 if (item->li_prev == NULL)
6713 l->lv_first = item2->li_next;
6714 else
6715 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006716 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006717}
6718
6719/*
6720 * Return an allocated string with the string representation of a list.
6721 * May return NULL.
6722 */
6723 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006724list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006725 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006726 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727{
6728 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006729
6730 if (tv->vval.v_list == NULL)
6731 return NULL;
6732 ga_init2(&ga, (int)sizeof(char), 80);
6733 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006734 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006735 {
6736 vim_free(ga.ga_data);
6737 return NULL;
6738 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006739 ga_append(&ga, ']');
6740 ga_append(&ga, NUL);
6741 return (char_u *)ga.ga_data;
6742}
6743
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006744typedef struct join_S {
6745 char_u *s;
6746 char_u *tofree;
6747} join_T;
6748
6749 static int
6750list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6751 garray_T *gap; /* to store the result in */
6752 list_T *l;
6753 char_u *sep;
6754 int echo_style;
6755 int copyID;
6756 garray_T *join_gap; /* to keep each list item string */
6757{
6758 int i;
6759 join_T *p;
6760 int len;
6761 int sumlen = 0;
6762 int first = TRUE;
6763 char_u *tofree;
6764 char_u numbuf[NUMBUFLEN];
6765 listitem_T *item;
6766 char_u *s;
6767
6768 /* Stringify each item in the list. */
6769 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6770 {
6771 if (echo_style)
6772 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6773 else
6774 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6775 if (s == NULL)
6776 return FAIL;
6777
6778 len = (int)STRLEN(s);
6779 sumlen += len;
6780
Bram Moolenaarcde88542015-08-11 19:14:00 +02006781 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006782 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6783 if (tofree != NULL || s != numbuf)
6784 {
6785 p->s = s;
6786 p->tofree = tofree;
6787 }
6788 else
6789 {
6790 p->s = vim_strnsave(s, len);
6791 p->tofree = p->s;
6792 }
6793
6794 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006795 if (did_echo_string_emsg) /* recursion error, bail out */
6796 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006797 }
6798
6799 /* Allocate result buffer with its total size, avoid re-allocation and
6800 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6801 if (join_gap->ga_len >= 2)
6802 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6803 if (ga_grow(gap, sumlen + 2) == FAIL)
6804 return FAIL;
6805
6806 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6807 {
6808 if (first)
6809 first = FALSE;
6810 else
6811 ga_concat(gap, sep);
6812 p = ((join_T *)join_gap->ga_data) + i;
6813
6814 if (p->s != NULL)
6815 ga_concat(gap, p->s);
6816 line_breakcheck();
6817 }
6818
6819 return OK;
6820}
6821
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006822/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006824 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006825 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006826 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006827 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006828list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006829 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006830 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006831 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006832 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006833 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006834{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006835 garray_T join_ga;
6836 int retval;
6837 join_T *p;
6838 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006839
Bram Moolenaard39a7512015-04-16 22:51:22 +02006840 if (l->lv_len < 1)
6841 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006842 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6843 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6844
6845 /* Dispose each item in join_ga. */
6846 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006847 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006848 p = (join_T *)join_ga.ga_data;
6849 for (i = 0; i < join_ga.ga_len; ++i)
6850 {
6851 vim_free(p->tofree);
6852 ++p;
6853 }
6854 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006855 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006856
6857 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006858}
6859
6860/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006861 * Return the next (unique) copy ID.
6862 * Used for serializing nested structures.
6863 */
6864 int
6865get_copyID()
6866{
6867 current_copyID += COPYID_INC;
6868 return current_copyID;
6869}
6870
6871/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 * Garbage collection for lists and dictionaries.
6873 *
6874 * We use reference counts to be able to free most items right away when they
6875 * are no longer used. But for composite items it's possible that it becomes
6876 * unused while the reference count is > 0: When there is a recursive
6877 * reference. Example:
6878 * :let l = [1, 2, 3]
6879 * :let d = {9: l}
6880 * :let l[1] = d
6881 *
6882 * Since this is quite unusual we handle this with garbage collection: every
6883 * once in a while find out which lists and dicts are not referenced from any
6884 * variable.
6885 *
6886 * Here is a good reference text about garbage collection (refers to Python
6887 * but it applies to all reference-counting mechanisms):
6888 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006889 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006890
6891/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892 * Do garbage collection for lists and dicts.
6893 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006894 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 int
6896garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006897{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006898 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 buf_T *buf;
6901 win_T *wp;
6902 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006903 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006904 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006905 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006906#ifdef FEAT_WINDOWS
6907 tabpage_T *tp;
6908#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006910 /* Only do this once. */
6911 want_garbage_collect = FALSE;
6912 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006913 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006914
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006915 /* We advance by two because we add one for items referenced through
6916 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006917 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006918
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 /*
6920 * 1. Go through all accessible variables and mark all lists and dicts
6921 * with copyID.
6922 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006923
6924 /* Don't free variables in the previous_funccal list unless they are only
6925 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006926 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006927 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6928 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6930 NULL);
6931 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6932 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 }
6934
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935 /* script-local variables */
6936 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938
6939 /* buffer-local variables */
6940 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006941 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6942 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943
6944 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006945 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6947 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006948#ifdef FEAT_AUTOCMD
6949 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6951 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006952#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006954#ifdef FEAT_WINDOWS
6955 /* tabpage-local variables */
6956 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6958 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006959#endif
6960
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006962 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006963
6964 /* function-local variables */
6965 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6966 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006967 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6968 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 }
6970
Bram Moolenaard812df62008-11-09 12:46:09 +00006971 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006972 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006973
Bram Moolenaar1dced572012-04-05 16:54:08 +02006974#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006975 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006976#endif
6977
Bram Moolenaardb913952012-06-29 12:54:53 +02006978#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006979 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006980#endif
6981
6982#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006983 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006984#endif
6985
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006986 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006988 /*
6989 * 2. Free lists and dictionaries that are not referenced.
6990 */
6991 did_free = free_unref_items(copyID);
6992
6993 /*
6994 * 3. Check if any funccal can be freed now.
6995 */
6996 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006998 if (can_free_funccal(*pfc, copyID))
6999 {
7000 fc = *pfc;
7001 *pfc = fc->caller;
7002 free_funccal(fc, TRUE);
7003 did_free = TRUE;
7004 did_free_funccal = TRUE;
7005 }
7006 else
7007 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007008 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007009 if (did_free_funccal)
7010 /* When a funccal was freed some more items might be garbage
7011 * collected, so run again. */
7012 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007013 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007014 else if (p_verbose > 0)
7015 {
7016 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7017 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018
7019 return did_free;
7020}
7021
7022/*
7023 * Free lists and dictionaries that are no longer referenced.
7024 */
7025 static int
7026free_unref_items(copyID)
7027 int copyID;
7028{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007029 dict_T *dd, *dd_next;
7030 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007031 int did_free = FALSE;
7032
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007034 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 */
7036 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007037 {
7038 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007039 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007041 /* Free the Dictionary and ordinary items it contains, but don't
7042 * recurse into Lists and Dictionaries, they will be in the list
7043 * of dicts or list of lists. */
7044 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007047 dd = dd_next;
7048 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049
7050 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007051 * Go through the list of lists and free items without the copyID.
7052 * But don't free a list that has a watcher (used in a for loop), these
7053 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007054 */
7055 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007056 {
7057 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007058 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7059 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007061 /* Free the List and ordinary items it contains, but don't recurse
7062 * into Lists and Dictionaries, they will be in the list of dicts
7063 * or list of lists. */
7064 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007065 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007067 ll = ll_next;
7068 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069 return did_free;
7070}
7071
7072/*
7073 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 * "list_stack" is used to add lists to be marked. Can be NULL.
7075 *
7076 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007078 int
7079set_ref_in_ht(ht, copyID, list_stack)
7080 hashtab_T *ht;
7081 int copyID;
7082 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007083{
7084 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007087 hashtab_T *cur_ht;
7088 ht_stack_T *ht_stack = NULL;
7089 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091 cur_ht = ht;
7092 for (;;)
7093 {
7094 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007096 /* Mark each item in the hashtab. If the item contains a hashtab
7097 * it is added to ht_stack, if it contains a list it is added to
7098 * list_stack. */
7099 todo = (int)cur_ht->ht_used;
7100 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7101 if (!HASHITEM_EMPTY(hi))
7102 {
7103 --todo;
7104 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7105 &ht_stack, list_stack);
7106 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108
7109 if (ht_stack == NULL)
7110 break;
7111
7112 /* take an item from the stack */
7113 cur_ht = ht_stack->ht;
7114 tempitem = ht_stack;
7115 ht_stack = ht_stack->prev;
7116 free(tempitem);
7117 }
7118
7119 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120}
7121
7122/*
7123 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007124 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7125 *
7126 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007127 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007128 int
7129set_ref_in_list(l, copyID, ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007130 list_T *l;
7131 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 ht_stack_T **ht_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007133{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007134 listitem_T *li;
7135 int abort = FALSE;
7136 list_T *cur_l;
7137 list_stack_T *list_stack = NULL;
7138 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007139
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007140 cur_l = l;
7141 for (;;)
7142 {
7143 if (!abort)
7144 /* Mark each item in the list. If the item contains a hashtab
7145 * it is added to ht_stack, if it contains a list it is added to
7146 * list_stack. */
7147 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7148 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7149 ht_stack, &list_stack);
7150 if (list_stack == NULL)
7151 break;
7152
7153 /* take an item from the stack */
7154 cur_l = list_stack->list;
7155 tempitem = list_stack;
7156 list_stack = list_stack->prev;
7157 free(tempitem);
7158 }
7159
7160 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007161}
7162
7163/*
7164 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 * "list_stack" is used to add lists to be marked. Can be NULL.
7166 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7167 *
7168 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007169 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007170 int
7171set_ref_in_item(tv, copyID, ht_stack, list_stack)
7172 typval_T *tv;
7173 int copyID;
7174 ht_stack_T **ht_stack;
7175 list_stack_T **list_stack;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007176{
7177 dict_T *dd;
7178 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007179 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007180
7181 switch (tv->v_type)
7182 {
7183 case VAR_DICT:
7184 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007185 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007186 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187 /* Didn't see this dict yet. */
7188 dd->dv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007189 if (ht_stack == NULL)
7190 {
7191 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7192 }
7193 else
7194 {
7195 ht_stack_T *newitem = (ht_stack_T*)malloc(
7196 sizeof(ht_stack_T));
7197 if (newitem == NULL)
7198 abort = TRUE;
7199 else
7200 {
7201 newitem->ht = &dd->dv_hashtab;
7202 newitem->prev = *ht_stack;
7203 *ht_stack = newitem;
7204 }
7205 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007206 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007207 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007208
7209 case VAR_LIST:
7210 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007211 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007212 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007213 /* Didn't see this list yet. */
7214 ll->lv_copyID = copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007215 if (list_stack == NULL)
7216 {
7217 abort = set_ref_in_list(ll, copyID, ht_stack);
7218 }
7219 else
7220 {
7221 list_stack_T *newitem = (list_stack_T*)malloc(
7222 sizeof(list_stack_T));
7223 if (newitem == NULL)
7224 abort = TRUE;
7225 else
7226 {
7227 newitem->list = ll;
7228 newitem->prev = *list_stack;
7229 *list_stack = newitem;
7230 }
7231 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007232 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007233 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007234 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007235 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007236}
7237
7238/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239 * Allocate an empty header for a dictionary.
7240 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007241 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242dict_alloc()
7243{
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007245
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007248 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007249 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007250 if (first_dict != NULL)
7251 first_dict->dv_used_prev = d;
7252 d->dv_used_next = first_dict;
7253 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007254 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007255
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007257 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007258 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007259 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007260 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007261 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007262 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263}
7264
7265/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007266 * Allocate an empty dict for a return value.
7267 * Returns OK or FAIL.
7268 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007269 int
Bram Moolenaara800b422010-06-27 01:15:55 +02007270rettv_dict_alloc(rettv)
7271 typval_T *rettv;
7272{
7273 dict_T *d = dict_alloc();
7274
7275 if (d == NULL)
7276 return FAIL;
7277
7278 rettv->vval.v_dict = d;
7279 rettv->v_type = VAR_DICT;
7280 ++d->dv_refcount;
7281 return OK;
7282}
7283
7284
7285/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 * Unreference a Dictionary: decrement the reference count and free it when it
7287 * becomes zero.
7288 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007289 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007291 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007293 if (d != NULL && --d->dv_refcount <= 0)
7294 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295}
7296
7297/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007298 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299 * Ignores the reference count.
7300 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007301 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007302dict_free(d, recurse)
7303 dict_T *d;
7304 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007308 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007310 /* Remove the dict from the list of dicts for garbage collection. */
7311 if (d->dv_used_prev == NULL)
7312 first_dict = d->dv_used_next;
7313 else
7314 d->dv_used_prev->dv_used_next = d->dv_used_next;
7315 if (d->dv_used_next != NULL)
7316 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7317
7318 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007319 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007320 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (!HASHITEM_EMPTY(hi))
7324 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007325 /* Remove the item before deleting it, just in case there is
7326 * something recursive causing trouble. */
7327 di = HI2DI(hi);
7328 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007329 if (recurse || (di->di_tv.v_type != VAR_LIST
7330 && di->di_tv.v_type != VAR_DICT))
7331 clear_tv(&di->di_tv);
7332 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 --todo;
7334 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 vim_free(d);
7338}
7339
7340/*
7341 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007342 * The "key" is copied to the new item.
7343 * Note that the value of the item "di_tv" still needs to be initialized!
7344 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007346 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347dictitem_alloc(key)
7348 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349{
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007352 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007355 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007356 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007357 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007359}
7360
7361/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007362 * Make a copy of a Dictionary item.
7363 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007364 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367{
Bram Moolenaar33570922005-01-25 22:26:29 +00007368 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007369
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007370 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7371 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 if (di != NULL)
7373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007374 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007375 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 copy_tv(&org->di_tv, &di->di_tv);
7377 }
7378 return di;
7379}
7380
7381/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 * Remove item "item" from Dictionary "dict" and free it.
7383 */
7384 static void
7385dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dict_T *dict;
7387 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007388{
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 if (HASHITEM_EMPTY(hi))
7393 EMSG2(_(e_intern2), "dictitem_remove()");
7394 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007396 dictitem_free(item);
7397}
7398
7399/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400 * Free a dict item. Also clears the value.
7401 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007402 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007403dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007404 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007407 if (item->di_flags & DI_FLAGS_ALLOC)
7408 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409}
7410
7411/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007412 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7413 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007414 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007415 * Returns NULL when out of memory.
7416 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007418dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007420 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007421 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422{
Bram Moolenaar33570922005-01-25 22:26:29 +00007423 dict_T *copy;
7424 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007427
7428 if (orig == NULL)
7429 return NULL;
7430
7431 copy = dict_alloc();
7432 if (copy != NULL)
7433 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007434 if (copyID != 0)
7435 {
7436 orig->dv_copyID = copyID;
7437 orig->dv_copydict = copy;
7438 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007439 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007440 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007441 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007442 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007443 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007444 --todo;
7445
7446 di = dictitem_alloc(hi->hi_key);
7447 if (di == NULL)
7448 break;
7449 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007450 {
7451 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7452 copyID) == FAIL)
7453 {
7454 vim_free(di);
7455 break;
7456 }
7457 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007458 else
7459 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7460 if (dict_add(copy, di) == FAIL)
7461 {
7462 dictitem_free(di);
7463 break;
7464 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007465 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007466 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007467
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007469 if (todo > 0)
7470 {
7471 dict_unref(copy);
7472 copy = NULL;
7473 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474 }
7475
7476 return copy;
7477}
7478
7479/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007481 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007483 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007485 dict_T *d;
7486 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487{
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489}
7490
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007492 * Add a number or string entry to dictionary "d".
7493 * When "str" is NULL use number "nr", otherwise use "str".
7494 * Returns FAIL when out of memory and when key already exists.
7495 */
7496 int
7497dict_add_nr_str(d, key, nr, str)
7498 dict_T *d;
7499 char *key;
7500 long nr;
7501 char_u *str;
7502{
7503 dictitem_T *item;
7504
7505 item = dictitem_alloc((char_u *)key);
7506 if (item == NULL)
7507 return FAIL;
7508 item->di_tv.v_lock = 0;
7509 if (str == NULL)
7510 {
7511 item->di_tv.v_type = VAR_NUMBER;
7512 item->di_tv.vval.v_number = nr;
7513 }
7514 else
7515 {
7516 item->di_tv.v_type = VAR_STRING;
7517 item->di_tv.vval.v_string = vim_strsave(str);
7518 }
7519 if (dict_add(d, item) == FAIL)
7520 {
7521 dictitem_free(item);
7522 return FAIL;
7523 }
7524 return OK;
7525}
7526
7527/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007528 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007529 * Returns FAIL when out of memory and when key already exists.
7530 */
7531 int
7532dict_add_list(d, key, list)
7533 dict_T *d;
7534 char *key;
7535 list_T *list;
7536{
7537 dictitem_T *item;
7538
7539 item = dictitem_alloc((char_u *)key);
7540 if (item == NULL)
7541 return FAIL;
7542 item->di_tv.v_lock = 0;
7543 item->di_tv.v_type = VAR_LIST;
7544 item->di_tv.vval.v_list = list;
7545 if (dict_add(d, item) == FAIL)
7546 {
7547 dictitem_free(item);
7548 return FAIL;
7549 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007550 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007551 return OK;
7552}
7553
7554/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007555 * Get the number of items in a Dictionary.
7556 */
7557 static long
7558dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007559 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007560{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561 if (d == NULL)
7562 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007563 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007564}
7565
7566/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 * Find item "key[len]" in Dictionary "d".
7568 * If "len" is negative use strlen(key).
7569 * Returns NULL when not found.
7570 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007571 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 char_u *key;
7575 int len;
7576{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577#define AKEYLEN 200
7578 char_u buf[AKEYLEN];
7579 char_u *akey;
7580 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007581 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007583 if (len < 0)
7584 akey = key;
7585 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007586 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007587 tofree = akey = vim_strnsave(key, len);
7588 if (akey == NULL)
7589 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007590 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007591 else
7592 {
7593 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007594 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007595 akey = buf;
7596 }
7597
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 vim_free(tofree);
7600 if (HASHITEM_EMPTY(hi))
7601 return NULL;
7602 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603}
7604
7605/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007606 * Get a string item from a dictionary.
7607 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007608 * Returns NULL if the entry doesn't exist or out of memory.
7609 */
7610 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007611get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007612 dict_T *d;
7613 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007614 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007615{
7616 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007617 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007618
7619 di = dict_find(d, key, -1);
7620 if (di == NULL)
7621 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007622 s = get_tv_string(&di->di_tv);
7623 if (save && s != NULL)
7624 s = vim_strsave(s);
7625 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007626}
7627
7628/*
7629 * Get a number item from a dictionary.
7630 * Returns 0 if the entry doesn't exist or out of memory.
7631 */
7632 long
7633get_dict_number(d, key)
7634 dict_T *d;
7635 char_u *key;
7636{
7637 dictitem_T *di;
7638
7639 di = dict_find(d, key, -1);
7640 if (di == NULL)
7641 return 0;
7642 return get_tv_number(&di->di_tv);
7643}
7644
7645/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 * Return an allocated string with the string representation of a Dictionary.
7647 * May return NULL.
7648 */
7649 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007650dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007651 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007652 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653{
7654 garray_T ga;
7655 int first = TRUE;
7656 char_u *tofree;
7657 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007663 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 return NULL;
7665 ga_init2(&ga, (int)sizeof(char), 80);
7666 ga_append(&ga, '{');
7667
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007668 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007669 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007671 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007673 --todo;
7674
7675 if (first)
7676 first = FALSE;
7677 else
7678 ga_concat(&ga, (char_u *)", ");
7679
7680 tofree = string_quote(hi->hi_key, FALSE);
7681 if (tofree != NULL)
7682 {
7683 ga_concat(&ga, tofree);
7684 vim_free(tofree);
7685 }
7686 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007687 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007688 if (s != NULL)
7689 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007691 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007692 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007693 line_breakcheck();
7694
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007697 if (todo > 0)
7698 {
7699 vim_free(ga.ga_data);
7700 return NULL;
7701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702
7703 ga_append(&ga, '}');
7704 ga_append(&ga, NUL);
7705 return (char_u *)ga.ga_data;
7706}
7707
7708/*
7709 * Allocate a variable for a Dictionary and fill it from "*arg".
7710 * Return OK or FAIL. Returns NOTDONE for {expr}.
7711 */
7712 static int
7713get_dict_tv(arg, rettv, evaluate)
7714 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 int evaluate;
7717{
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 dict_T *d = NULL;
7719 typval_T tvkey;
7720 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007721 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007722 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007724 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725
7726 /*
7727 * First check if it's not a curly-braces thing: {expr}.
7728 * Must do this without evaluating, otherwise a function may be called
7729 * twice. Unfortunately this means we need to call eval1() twice for the
7730 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007731 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007733 if (*start != '}')
7734 {
7735 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7736 return FAIL;
7737 if (*start == '}')
7738 return NOTDONE;
7739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740
7741 if (evaluate)
7742 {
7743 d = dict_alloc();
7744 if (d == NULL)
7745 return FAIL;
7746 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007747 tvkey.v_type = VAR_UNKNOWN;
7748 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007749
7750 *arg = skipwhite(*arg + 1);
7751 while (**arg != '}' && **arg != NUL)
7752 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007753 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 goto failret;
7755 if (**arg != ':')
7756 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007757 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007758 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759 goto failret;
7760 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007761 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007763 key = get_tv_string_buf_chk(&tvkey, buf);
7764 if (key == NULL || *key == NUL)
7765 {
7766 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7767 if (key != NULL)
7768 EMSG(_(e_emptykey));
7769 clear_tv(&tvkey);
7770 goto failret;
7771 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007772 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007773
7774 *arg = skipwhite(*arg + 1);
7775 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7776 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007777 if (evaluate)
7778 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779 goto failret;
7780 }
7781 if (evaluate)
7782 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007783 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007784 if (item != NULL)
7785 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007786 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007787 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788 clear_tv(&tv);
7789 goto failret;
7790 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007791 item = dictitem_alloc(key);
7792 clear_tv(&tvkey);
7793 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007794 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007795 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007796 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007797 if (dict_add(d, item) == FAIL)
7798 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007799 }
7800 }
7801
7802 if (**arg == '}')
7803 break;
7804 if (**arg != ',')
7805 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007806 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007807 goto failret;
7808 }
7809 *arg = skipwhite(*arg + 1);
7810 }
7811
7812 if (**arg != '}')
7813 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007814 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007815failret:
7816 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007817 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007818 return FAIL;
7819 }
7820
7821 *arg = skipwhite(*arg + 1);
7822 if (evaluate)
7823 {
7824 rettv->v_type = VAR_DICT;
7825 rettv->vval.v_dict = d;
7826 ++d->dv_refcount;
7827 }
7828
7829 return OK;
7830}
7831
Bram Moolenaar17a13432016-01-24 14:22:10 +01007832 static char *
7833get_var_special_name(int nr)
7834{
7835 switch (nr)
7836 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007837 case VVAL_FALSE: return "v:false";
7838 case VVAL_TRUE: return "v:true";
7839 case VVAL_NONE: return "v:none";
7840 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007841 }
7842 EMSG2(_(e_intern2), "get_var_special_name()");
7843 return "42";
7844}
7845
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847 * Return a string with the string representation of a variable.
7848 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007849 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007850 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007851 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007852 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007853 */
7854 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007855echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007857 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007858 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007859 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007860{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007861 static int recurse = 0;
7862 char_u *r = NULL;
7863
Bram Moolenaar33570922005-01-25 22:26:29 +00007864 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007865 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007866 if (!did_echo_string_emsg)
7867 {
7868 /* Only give this message once for a recursive call to avoid
7869 * flooding the user with errors. And stop iterating over lists
7870 * and dicts. */
7871 did_echo_string_emsg = TRUE;
7872 EMSG(_("E724: variable nested too deep for displaying"));
7873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007875 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876 }
7877 ++recurse;
7878
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007879 switch (tv->v_type)
7880 {
7881 case VAR_FUNC:
7882 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883 r = tv->vval.v_string;
7884 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007885
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007886 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007887 if (tv->vval.v_list == NULL)
7888 {
7889 *tofree = NULL;
7890 r = NULL;
7891 }
7892 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7893 {
7894 *tofree = NULL;
7895 r = (char_u *)"[...]";
7896 }
7897 else
7898 {
7899 tv->vval.v_list->lv_copyID = copyID;
7900 *tofree = list2string(tv, copyID);
7901 r = *tofree;
7902 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007903 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007904
Bram Moolenaar8c711452005-01-14 21:53:12 +00007905 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007906 if (tv->vval.v_dict == NULL)
7907 {
7908 *tofree = NULL;
7909 r = NULL;
7910 }
7911 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7912 {
7913 *tofree = NULL;
7914 r = (char_u *)"{...}";
7915 }
7916 else
7917 {
7918 tv->vval.v_dict->dv_copyID = copyID;
7919 *tofree = dict2string(tv, copyID);
7920 r = *tofree;
7921 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007922 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007923
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924 case VAR_STRING:
7925 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007926 *tofree = NULL;
7927 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007929
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007930#ifdef FEAT_FLOAT
7931 case VAR_FLOAT:
7932 *tofree = NULL;
7933 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7934 r = numbuf;
7935 break;
7936#endif
7937
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007938 case VAR_SPECIAL:
7939 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007940 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007941 break;
7942
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007945 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007946 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007947
Bram Moolenaar8502c702014-06-17 12:51:16 +02007948 if (--recurse == 0)
7949 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007950 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007951}
7952
7953/*
7954 * Return a string with the string representation of a variable.
7955 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7956 * "numbuf" is used for a number.
7957 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007958 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007959 */
7960 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007961tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007962 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007963 char_u **tofree;
7964 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007965 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007966{
7967 switch (tv->v_type)
7968 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 case VAR_FUNC:
7970 *tofree = string_quote(tv->vval.v_string, TRUE);
7971 return *tofree;
7972 case VAR_STRING:
7973 *tofree = string_quote(tv->vval.v_string, FALSE);
7974 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007975#ifdef FEAT_FLOAT
7976 case VAR_FLOAT:
7977 *tofree = NULL;
7978 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7979 return numbuf;
7980#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007981 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007982 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007983 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007984 case VAR_SPECIAL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007985 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007987 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007988 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007989 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007990}
7991
7992/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007993 * Return string "str" in ' quotes, doubling ' characters.
7994 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007995 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007996 */
7997 static char_u *
7998string_quote(str, function)
7999 char_u *str;
8000 int function;
8001{
Bram Moolenaar33570922005-01-25 22:26:29 +00008002 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008003 char_u *p, *r, *s;
8004
Bram Moolenaar33570922005-01-25 22:26:29 +00008005 len = (function ? 13 : 3);
8006 if (str != NULL)
8007 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008008 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008009 for (p = str; *p != NUL; mb_ptr_adv(p))
8010 if (*p == '\'')
8011 ++len;
8012 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008013 s = r = alloc(len);
8014 if (r != NULL)
8015 {
8016 if (function)
8017 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008018 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008019 r += 10;
8020 }
8021 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008022 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008023 if (str != NULL)
8024 for (p = str; *p != NUL; )
8025 {
8026 if (*p == '\'')
8027 *r++ = '\'';
8028 MB_COPY_CHAR(p, r);
8029 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008030 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008031 if (function)
8032 *r++ = ')';
8033 *r++ = NUL;
8034 }
8035 return s;
8036}
8037
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008038#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008039/*
8040 * Convert the string "text" to a floating point number.
8041 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8042 * this always uses a decimal point.
8043 * Returns the length of the text that was consumed.
8044 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008045 int
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008046string2float(text, value)
8047 char_u *text;
8048 float_T *value; /* result stored here */
8049{
8050 char *s = (char *)text;
8051 float_T f;
8052
8053 f = strtod(s, &s);
8054 *value = f;
8055 return (int)((char_u *)s - text);
8056}
8057#endif
8058
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 * Get the value of an environment variable.
8061 * "arg" is pointing to the '$'. It is advanced to after the name.
8062 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008063 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 */
8065 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008066get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 int evaluate;
8070{
8071 char_u *string = NULL;
8072 int len;
8073 int cc;
8074 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008075 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076
8077 ++*arg;
8078 name = *arg;
8079 len = get_env_len(arg);
8080 if (evaluate)
8081 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008082 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008083 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008084
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008085 cc = name[len];
8086 name[len] = NUL;
8087 /* first try vim_getenv(), fast for normal environment vars */
8088 string = vim_getenv(name, &mustfree);
8089 if (string != NULL && *string != NUL)
8090 {
8091 if (!mustfree)
8092 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008094 else
8095 {
8096 if (mustfree)
8097 vim_free(string);
8098
8099 /* next try expanding things like $VIM and ${HOME} */
8100 string = expand_env_save(name - 1);
8101 if (string != NULL && *string == '$')
8102 {
8103 vim_free(string);
8104 string = NULL;
8105 }
8106 }
8107 name[len] = cc;
8108
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008109 rettv->v_type = VAR_STRING;
8110 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 }
8112
8113 return OK;
8114}
8115
8116/*
8117 * Array with names and number of arguments of all internal functions
8118 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8119 */
8120static struct fst
8121{
8122 char *f_name; /* function name */
8123 char f_min_argc; /* minimal number of arguments */
8124 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008125 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008126 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127} functions[] =
8128{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008129#ifdef FEAT_FLOAT
8130 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008131 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008133 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008134 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008135 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"append", 2, 2, f_append},
8137 {"argc", 0, 0, f_argc},
8138 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008139 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008140 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008141#ifdef FEAT_FLOAT
8142 {"asin", 1, 1, f_asin}, /* WJMc */
8143#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008144 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008145 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008146 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008147 {"assert_false", 1, 2, f_assert_false},
8148 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008149#ifdef FEAT_FLOAT
8150 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008151 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008154 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"bufexists", 1, 1, f_bufexists},
8156 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8157 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8158 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8159 {"buflisted", 1, 1, f_buflisted},
8160 {"bufloaded", 1, 1, f_bufloaded},
8161 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008162 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"bufwinnr", 1, 1, f_bufwinnr},
8164 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008165 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008166 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008167 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#ifdef FEAT_FLOAT
8169 {"ceil", 1, 1, f_ceil},
8170#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008171 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008172 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008174 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008176#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008177 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008178 {"complete_add", 1, 1, f_complete_add},
8179 {"complete_check", 0, 0, f_complete_check},
8180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 {"confirm", 1, 4, f_confirm},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008182#ifdef FEAT_CHANNEL
8183 {"connect", 2, 3, f_connect},
8184#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008185 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008186#ifdef FEAT_FLOAT
8187 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008188 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008189#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008190 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008192 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008193 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008194 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008196 {"diff_filler", 1, 1, f_diff_filler},
8197 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008198#ifdef FEAT_CHANNEL
8199 {"disconnect", 1, 1, f_disconnect},
8200#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008201 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008203 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"eventhandler", 0, 0, f_eventhandler},
8205 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008206 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008208#ifdef FEAT_FLOAT
8209 {"exp", 1, 1, f_exp},
8210#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008211 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008212 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008213 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8215 {"filereadable", 1, 1, f_filereadable},
8216 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008217 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008218 {"finddir", 1, 3, f_finddir},
8219 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008220#ifdef FEAT_FLOAT
8221 {"float2nr", 1, 1, f_float2nr},
8222 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008223 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008224#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008225 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"fnamemodify", 2, 2, f_fnamemodify},
8227 {"foldclosed", 1, 1, f_foldclosed},
8228 {"foldclosedend", 1, 1, f_foldclosedend},
8229 {"foldlevel", 1, 1, f_foldlevel},
8230 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008231 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008233 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008234 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008235 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008236 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008237 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"getchar", 0, 1, f_getchar},
8239 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008240 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"getcmdline", 0, 0, f_getcmdline},
8242 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008243 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008244 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008245 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008246 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008247 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008248 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"getfsize", 1, 1, f_getfsize},
8250 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008251 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008252 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008253 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008254 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008255 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008256 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008257 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008258 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008260 {"gettabvar", 2, 3, f_gettabvar},
8261 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {"getwinposx", 0, 0, f_getwinposx},
8263 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008264 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008265 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008266 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008267 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008269 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008270 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008271 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8273 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8274 {"histadd", 2, 2, f_histadd},
8275 {"histdel", 1, 2, f_histdel},
8276 {"histget", 1, 2, f_histget},
8277 {"histnr", 1, 1, f_histnr},
8278 {"hlID", 1, 1, f_hlID},
8279 {"hlexists", 1, 1, f_hlexists},
8280 {"hostname", 0, 0, f_hostname},
8281 {"iconv", 3, 3, f_iconv},
8282 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008283 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008284 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008286 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"inputrestore", 0, 0, f_inputrestore},
8288 {"inputsave", 0, 0, f_inputsave},
8289 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008291 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008293 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008294 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008295 {"join", 1, 2, f_join},
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008296 {"jsondecode", 1, 1, f_jsondecode},
8297 {"jsonencode", 1, 1, f_jsonencode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008298 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008300 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"libcall", 3, 3, f_libcall},
8302 {"libcallnr", 3, 3, f_libcallnr},
8303 {"line", 1, 1, f_line},
8304 {"line2byte", 1, 1, f_line2byte},
8305 {"lispindent", 1, 1, f_lispindent},
8306 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008307#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008308 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008309 {"log10", 1, 1, f_log10},
8310#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008311#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008312 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008313#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008314 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008315 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008316 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008317 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008318 {"matchadd", 2, 5, f_matchadd},
8319 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008320 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008321 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008322 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008323 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008324 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008325 {"max", 1, 1, f_max},
8326 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008327#ifdef vim_mkdir
8328 {"mkdir", 1, 3, f_mkdir},
8329#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008330 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008331#ifdef FEAT_MZSCHEME
8332 {"mzeval", 1, 1, f_mzeval},
8333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008335 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008336 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008337 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008338#ifdef FEAT_PERL
8339 {"perleval", 1, 1, f_perleval},
8340#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008341#ifdef FEAT_FLOAT
8342 {"pow", 2, 2, f_pow},
8343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008345 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008346 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008347#ifdef FEAT_PYTHON3
8348 {"py3eval", 1, 1, f_py3eval},
8349#endif
8350#ifdef FEAT_PYTHON
8351 {"pyeval", 1, 1, f_pyeval},
8352#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008353 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008354 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008355 {"reltime", 0, 2, f_reltime},
8356 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {"remote_expr", 2, 3, f_remote_expr},
8358 {"remote_foreground", 1, 1, f_remote_foreground},
8359 {"remote_peek", 1, 2, f_remote_peek},
8360 {"remote_read", 1, 1, f_remote_read},
8361 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008362 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008364 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008366 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008367#ifdef FEAT_FLOAT
8368 {"round", 1, 1, f_round},
8369#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008370 {"screenattr", 2, 2, f_screenattr},
8371 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008372 {"screencol", 0, 0, f_screencol},
8373 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008374 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008375 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008376 {"searchpair", 3, 7, f_searchpair},
8377 {"searchpairpos", 3, 7, f_searchpairpos},
8378 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008379#ifdef FEAT_CHANNEL
8380 {"sendexpr", 2, 3, f_sendexpr},
8381 {"sendraw", 2, 3, f_sendraw},
8382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {"server2client", 2, 2, f_server2client},
8384 {"serverlist", 0, 0, f_serverlist},
8385 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008386 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 {"setcmdpos", 1, 1, f_setcmdpos},
8388 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008389 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008390 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008391 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008392 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008394 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008395 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008397#ifdef FEAT_CRYPT
8398 {"sha256", 1, 1, f_sha256},
8399#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008400 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008401 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008403#ifdef FEAT_FLOAT
8404 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008405 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008406#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008407 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008408 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008409 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008410 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008411 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008412#ifdef FEAT_FLOAT
8413 {"sqrt", 1, 1, f_sqrt},
8414 {"str2float", 1, 1, f_str2float},
8415#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008416 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008417 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008418 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419#ifdef HAVE_STRFTIME
8420 {"strftime", 1, 2, f_strftime},
8421#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008422 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008423 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"strlen", 1, 1, f_strlen},
8425 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008426 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008428 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008429 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"substitute", 4, 4, f_substitute},
8431 {"synID", 3, 3, f_synID},
8432 {"synIDattr", 2, 3, f_synIDattr},
8433 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008434 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008435 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008436 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008437 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008438 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008439 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008440 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008441 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008442 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008443#ifdef FEAT_FLOAT
8444 {"tan", 1, 1, f_tan},
8445 {"tanh", 1, 1, f_tanh},
8446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008448 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"tolower", 1, 1, f_tolower},
8450 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008451 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008452#ifdef FEAT_FLOAT
8453 {"trunc", 1, 1, f_trunc},
8454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008456 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008457 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008458 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008459 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {"virtcol", 1, 1, f_virtcol},
8461 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008462 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {"winbufnr", 1, 1, f_winbufnr},
8464 {"wincol", 0, 0, f_wincol},
8465 {"winheight", 1, 1, f_winheight},
8466 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008467 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008469 {"winrestview", 1, 1, f_winrestview},
8470 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008472 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008473 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008474 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475};
8476
8477#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8478
8479/*
8480 * Function given to ExpandGeneric() to obtain the list of internal
8481 * or user defined function names.
8482 */
8483 char_u *
8484get_function_name(xp, idx)
8485 expand_T *xp;
8486 int idx;
8487{
8488 static int intidx = -1;
8489 char_u *name;
8490
8491 if (idx == 0)
8492 intidx = -1;
8493 if (intidx < 0)
8494 {
8495 name = get_user_func_name(xp, idx);
8496 if (name != NULL)
8497 return name;
8498 }
8499 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8500 {
8501 STRCPY(IObuff, functions[intidx].f_name);
8502 STRCAT(IObuff, "(");
8503 if (functions[intidx].f_max_argc == 0)
8504 STRCAT(IObuff, ")");
8505 return IObuff;
8506 }
8507
8508 return NULL;
8509}
8510
8511/*
8512 * Function given to ExpandGeneric() to obtain the list of internal or
8513 * user defined variable or function names.
8514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 char_u *
8516get_expr_name(xp, idx)
8517 expand_T *xp;
8518 int idx;
8519{
8520 static int intidx = -1;
8521 char_u *name;
8522
8523 if (idx == 0)
8524 intidx = -1;
8525 if (intidx < 0)
8526 {
8527 name = get_function_name(xp, idx);
8528 if (name != NULL)
8529 return name;
8530 }
8531 return get_user_var_name(xp, ++intidx);
8532}
8533
8534#endif /* FEAT_CMDL_COMPL */
8535
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008536#if defined(EBCDIC) || defined(PROTO)
8537/*
8538 * Compare struct fst by function name.
8539 */
8540 static int
8541compare_func_name(s1, s2)
8542 const void *s1;
8543 const void *s2;
8544{
8545 struct fst *p1 = (struct fst *)s1;
8546 struct fst *p2 = (struct fst *)s2;
8547
8548 return STRCMP(p1->f_name, p2->f_name);
8549}
8550
8551/*
8552 * Sort the function table by function name.
8553 * The sorting of the table above is ASCII dependant.
8554 * On machines using EBCDIC we have to sort it.
8555 */
8556 static void
8557sortFunctions()
8558{
8559 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8560
8561 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8562}
8563#endif
8564
8565
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566/*
8567 * Find internal function in table above.
8568 * Return index, or -1 if not found
8569 */
8570 static int
8571find_internal_func(name)
8572 char_u *name; /* name of the function */
8573{
8574 int first = 0;
8575 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8576 int cmp;
8577 int x;
8578
8579 /*
8580 * Find the function name in the table. Binary search.
8581 */
8582 while (first <= last)
8583 {
8584 x = first + ((unsigned)(last - first) >> 1);
8585 cmp = STRCMP(name, functions[x].f_name);
8586 if (cmp < 0)
8587 last = x - 1;
8588 else if (cmp > 0)
8589 first = x + 1;
8590 else
8591 return x;
8592 }
8593 return -1;
8594}
8595
8596/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008597 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8598 * name it contains, otherwise return "name".
8599 */
8600 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008601deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008602 char_u *name;
8603 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008604 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008605{
Bram Moolenaar33570922005-01-25 22:26:29 +00008606 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008607 int cc;
8608
8609 cc = name[*lenp];
8610 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008611 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008612 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008613 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008614 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008615 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008616 {
8617 *lenp = 0;
8618 return (char_u *)""; /* just in case */
8619 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008620 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008621 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008622 }
8623
8624 return name;
8625}
8626
8627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 * Allocate a variable for the result of a function.
8629 * Return OK or FAIL.
8630 */
8631 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008632get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8633 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 char_u *name; /* name of the function */
8635 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 char_u **arg; /* argument, pointing to the '(' */
8638 linenr_T firstline; /* first line of range */
8639 linenr_T lastline; /* last line of range */
8640 int *doesrange; /* return: function handled range */
8641 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008642 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
8644 char_u *argp;
8645 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008646 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 int argcount = 0; /* number of arguments found */
8648
8649 /*
8650 * Get the arguments.
8651 */
8652 argp = *arg;
8653 while (argcount < MAX_FUNC_ARGS)
8654 {
8655 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8656 if (*argp == ')' || *argp == ',' || *argp == NUL)
8657 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8659 {
8660 ret = FAIL;
8661 break;
8662 }
8663 ++argcount;
8664 if (*argp != ',')
8665 break;
8666 }
8667 if (*argp == ')')
8668 ++argp;
8669 else
8670 ret = FAIL;
8671
8672 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008673 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008674 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008676 {
8677 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008678 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008679 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008680 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682
8683 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008684 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685
8686 *arg = skipwhite(argp);
8687 return ret;
8688}
8689
8690
8691/*
8692 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008693 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008694 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008696 int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008697call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008698 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008699 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008701 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008703 typval_T *argvars; /* vars for arguments, must have "argcount"
8704 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 linenr_T firstline; /* first line of range */
8706 linenr_T lastline; /* last line of range */
8707 int *doesrange; /* return: function handled range */
8708 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008709 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
8711 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712#define ERROR_UNKNOWN 0
8713#define ERROR_TOOMANY 1
8714#define ERROR_TOOFEW 2
8715#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008716#define ERROR_DICT 4
8717#define ERROR_NONE 5
8718#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 int error = ERROR_NONE;
8720 int i;
8721 int llen;
8722 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723#define FLEN_FIXED 40
8724 char_u fname_buf[FLEN_FIXED + 1];
8725 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008726 char_u *name;
8727
8728 /* Make a copy of the name, if it comes from a funcref variable it could
8729 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008730 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008731 if (name == NULL)
8732 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
8734 /*
8735 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8736 * Change <SNR>123_name() to K_SNR 123_name().
8737 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 llen = eval_fname_script(name);
8740 if (llen > 0)
8741 {
8742 fname_buf[0] = K_SPECIAL;
8743 fname_buf[1] = KS_EXTRA;
8744 fname_buf[2] = (int)KE_SNR;
8745 i = 3;
8746 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8747 {
8748 if (current_SID <= 0)
8749 error = ERROR_SCRIPT;
8750 else
8751 {
8752 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8753 i = (int)STRLEN(fname_buf);
8754 }
8755 }
8756 if (i + STRLEN(name + llen) < FLEN_FIXED)
8757 {
8758 STRCPY(fname_buf + i, name + llen);
8759 fname = fname_buf;
8760 }
8761 else
8762 {
8763 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8764 if (fname == NULL)
8765 error = ERROR_OTHER;
8766 else
8767 {
8768 mch_memmove(fname, fname_buf, (size_t)i);
8769 STRCPY(fname + i, name + llen);
8770 }
8771 }
8772 }
8773 else
8774 fname = name;
8775
8776 *doesrange = FALSE;
8777
8778
8779 /* execute the function if no errors detected and executing */
8780 if (evaluate && error == ERROR_NONE)
8781 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008782 char_u *rfname = fname;
8783
8784 /* Ignore "g:" before a function name. */
8785 if (fname[0] == 'g' && fname[1] == ':')
8786 rfname = fname + 2;
8787
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008788 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8789 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 error = ERROR_UNKNOWN;
8791
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008792 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 {
8794 /*
8795 * User defined function.
8796 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008797 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008798
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008800 /* Trigger FuncUndefined event, may load the function. */
8801 if (fp == NULL
8802 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008803 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008804 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008806 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008807 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 }
8809#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008810 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008811 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008812 {
8813 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008814 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008815 }
8816
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 if (fp != NULL)
8818 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008819 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008821 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008823 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008825 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008826 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 else
8828 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008829 int did_save_redo = FALSE;
8830
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 /*
8832 * Call the user function.
8833 * Save and restore search patterns, script variables and
8834 * redo buffer.
8835 */
8836 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008837#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008838 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008839#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008840 {
8841 saveRedobuff();
8842 did_save_redo = TRUE;
8843 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008844 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008846 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008847 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8848 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8849 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008850 /* Function was unreferenced while being used, free it
8851 * now. */
8852 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008853 if (did_save_redo)
8854 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 restore_search_patterns();
8856 error = ERROR_NONE;
8857 }
8858 }
8859 }
8860 else
8861 {
8862 /*
8863 * Find the function name in the table, call its implementation.
8864 */
8865 i = find_internal_func(fname);
8866 if (i >= 0)
8867 {
8868 if (argcount < functions[i].f_min_argc)
8869 error = ERROR_TOOFEW;
8870 else if (argcount > functions[i].f_max_argc)
8871 error = ERROR_TOOMANY;
8872 else
8873 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008874 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008875 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 error = ERROR_NONE;
8877 }
8878 }
8879 }
8880 /*
8881 * The function call (or "FuncUndefined" autocommand sequence) might
8882 * have been aborted by an error, an interrupt, or an explicitly thrown
8883 * exception that has not been caught so far. This situation can be
8884 * tested for by calling aborting(). For an error in an internal
8885 * function or for the "E132" error in call_user_func(), however, the
8886 * throw point at which the "force_abort" flag (temporarily reset by
8887 * emsg()) is normally updated has not been reached yet. We need to
8888 * update that flag first to make aborting() reliable.
8889 */
8890 update_force_abort();
8891 }
8892 if (error == ERROR_NONE)
8893 ret = OK;
8894
8895 /*
8896 * Report an error unless the argument evaluation or function call has been
8897 * cancelled due to an aborting error, an interrupt, or an exception.
8898 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008899 if (!aborting())
8900 {
8901 switch (error)
8902 {
8903 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008904 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008905 break;
8906 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008907 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008908 break;
8909 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008910 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008911 name);
8912 break;
8913 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008914 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008915 name);
8916 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008917 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008918 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008919 name);
8920 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008921 }
8922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 if (fname != name && fname != fname_buf)
8925 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008926 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927
8928 return ret;
8929}
8930
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008931/*
8932 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008933 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008934 */
8935 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008936emsg_funcname(ermsg, name)
8937 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008938 char_u *name;
8939{
8940 char_u *p;
8941
8942 if (*name == K_SPECIAL)
8943 p = concat_str((char_u *)"<SNR>", name + 3);
8944 else
8945 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008946 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008947 if (p != name)
8948 vim_free(p);
8949}
8950
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008951/*
8952 * Return TRUE for a non-zero Number and a non-empty String.
8953 */
8954 static int
8955non_zero_arg(argvars)
8956 typval_T *argvars;
8957{
8958 return ((argvars[0].v_type == VAR_NUMBER
8959 && argvars[0].vval.v_number != 0)
8960 || (argvars[0].v_type == VAR_STRING
8961 && argvars[0].vval.v_string != NULL
8962 && *argvars[0].vval.v_string != NUL));
8963}
8964
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965/*********************************************
8966 * Implementation of the built-in functions
8967 */
8968
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008969#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008970static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008971
8972/*
8973 * Get the float value of "argvars[0]" into "f".
8974 * Returns FAIL when the argument is not a Number or Float.
8975 */
8976 static int
8977get_float_arg(argvars, f)
8978 typval_T *argvars;
8979 float_T *f;
8980{
8981 if (argvars[0].v_type == VAR_FLOAT)
8982 {
8983 *f = argvars[0].vval.v_float;
8984 return OK;
8985 }
8986 if (argvars[0].v_type == VAR_NUMBER)
8987 {
8988 *f = (float_T)argvars[0].vval.v_number;
8989 return OK;
8990 }
8991 EMSG(_("E808: Number or Float required"));
8992 return FAIL;
8993}
8994
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008995/*
8996 * "abs(expr)" function
8997 */
8998 static void
8999f_abs(argvars, rettv)
9000 typval_T *argvars;
9001 typval_T *rettv;
9002{
9003 if (argvars[0].v_type == VAR_FLOAT)
9004 {
9005 rettv->v_type = VAR_FLOAT;
9006 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9007 }
9008 else
9009 {
9010 varnumber_T n;
9011 int error = FALSE;
9012
9013 n = get_tv_number_chk(&argvars[0], &error);
9014 if (error)
9015 rettv->vval.v_number = -1;
9016 else if (n > 0)
9017 rettv->vval.v_number = n;
9018 else
9019 rettv->vval.v_number = -n;
9020 }
9021}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009022
9023/*
9024 * "acos()" function
9025 */
9026 static void
9027f_acos(argvars, rettv)
9028 typval_T *argvars;
9029 typval_T *rettv;
9030{
9031 float_T f;
9032
9033 rettv->v_type = VAR_FLOAT;
9034 if (get_float_arg(argvars, &f) == OK)
9035 rettv->vval.v_float = acos(f);
9036 else
9037 rettv->vval.v_float = 0.0;
9038}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009039#endif
9040
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009042 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 */
9044 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009045f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009046 typval_T *argvars;
9047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048{
Bram Moolenaar33570922005-01-25 22:26:29 +00009049 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009051 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009052 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009054 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009055 && !tv_check_lock(l->lv_lock,
9056 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009057 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009059 }
9060 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009061 EMSG(_(e_listreq));
9062}
9063
9064/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009065 * "alloc_fail(id, countdown, repeat)" function
9066 */
9067 static void
9068f_alloc_fail(argvars, rettv)
9069 typval_T *argvars;
9070 typval_T *rettv UNUSED;
9071{
9072 if (argvars[0].v_type != VAR_NUMBER
9073 || argvars[0].vval.v_number <= 0
9074 || argvars[1].v_type != VAR_NUMBER
9075 || argvars[1].vval.v_number < 0
9076 || argvars[2].v_type != VAR_NUMBER)
9077 EMSG(_(e_invarg));
9078 else
9079 {
9080 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009081 if (alloc_fail_id >= aid_last)
9082 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009083 alloc_fail_countdown = argvars[1].vval.v_number;
9084 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009085 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009086 }
9087}
9088
9089/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009090 * "and(expr, expr)" function
9091 */
9092 static void
9093f_and(argvars, rettv)
9094 typval_T *argvars;
9095 typval_T *rettv;
9096{
9097 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9098 & get_tv_number_chk(&argvars[1], NULL);
9099}
9100
9101/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009102 * "append(lnum, string/list)" function
9103 */
9104 static void
9105f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009106 typval_T *argvars;
9107 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009108{
9109 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009111 list_T *l = NULL;
9112 listitem_T *li = NULL;
9113 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009114 long added = 0;
9115
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009116 /* When coming here from Insert mode, sync undo, so that this can be
9117 * undone separately from what was previously inserted. */
9118 if (u_sync_once == 2)
9119 {
9120 u_sync_once = 1; /* notify that u_sync() was called */
9121 u_sync(TRUE);
9122 }
9123
Bram Moolenaar0d660222005-01-07 21:51:51 +00009124 lnum = get_tv_lnum(argvars);
9125 if (lnum >= 0
9126 && lnum <= curbuf->b_ml.ml_line_count
9127 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009128 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009129 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009130 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009131 l = argvars[1].vval.v_list;
9132 if (l == NULL)
9133 return;
9134 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009135 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009136 for (;;)
9137 {
9138 if (l == NULL)
9139 tv = &argvars[1]; /* append a string */
9140 else if (li == NULL)
9141 break; /* end of list */
9142 else
9143 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 line = get_tv_string_chk(tv);
9145 if (line == NULL) /* type error */
9146 {
9147 rettv->vval.v_number = 1; /* Failed */
9148 break;
9149 }
9150 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009151 ++added;
9152 if (l == NULL)
9153 break;
9154 li = li->li_next;
9155 }
9156
9157 appended_lines_mark(lnum, added);
9158 if (curwin->w_cursor.lnum > lnum)
9159 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009161 else
9162 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163}
9164
9165/*
9166 * "argc()" function
9167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009169f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009170 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174}
9175
9176/*
9177 * "argidx()" function
9178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009181 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185}
9186
9187/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009188 * "arglistid()" function
9189 */
9190 static void
9191f_arglistid(argvars, rettv)
9192 typval_T *argvars UNUSED;
9193 typval_T *rettv;
9194{
9195 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009196
9197 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009198 wp = find_tabwin(&argvars[0], &argvars[1]);
9199 if (wp != NULL)
9200 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009201}
9202
9203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 * "argv(nr)" function
9205 */
9206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 typval_T *argvars;
9209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
9211 int idx;
9212
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009213 if (argvars[0].v_type != VAR_UNKNOWN)
9214 {
9215 idx = get_tv_number_chk(&argvars[0], NULL);
9216 if (idx >= 0 && idx < ARGCOUNT)
9217 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9218 else
9219 rettv->vval.v_string = NULL;
9220 rettv->v_type = VAR_STRING;
9221 }
9222 else if (rettv_list_alloc(rettv) == OK)
9223 for (idx = 0; idx < ARGCOUNT; ++idx)
9224 list_append_string(rettv->vval.v_list,
9225 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226}
9227
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009228static void prepare_assert_error(garray_T*gap);
9229static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9230static void assert_error(garray_T *gap);
9231static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009232
9233/*
9234 * Prepare "gap" for an assert error and add the sourcing position.
9235 */
9236 static void
9237prepare_assert_error(gap)
9238 garray_T *gap;
9239{
9240 char buf[NUMBUFLEN];
9241
9242 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009243 if (sourcing_name != NULL)
9244 {
9245 ga_concat(gap, sourcing_name);
9246 if (sourcing_lnum > 0)
9247 ga_concat(gap, (char_u *)" ");
9248 }
9249 if (sourcing_lnum > 0)
9250 {
9251 sprintf(buf, "line %ld", (long)sourcing_lnum);
9252 ga_concat(gap, (char_u *)buf);
9253 }
9254 if (sourcing_name != NULL || sourcing_lnum > 0)
9255 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009256}
9257
9258/*
9259 * Fill "gap" with information about an assert error.
9260 */
9261 static void
9262fill_assert_error(gap, opt_msg_tv, exp_str, exp_tv, got_tv)
9263 garray_T *gap;
9264 typval_T *opt_msg_tv;
9265 char_u *exp_str;
9266 typval_T *exp_tv;
9267 typval_T *got_tv;
9268{
9269 char_u numbuf[NUMBUFLEN];
9270 char_u *tofree;
9271
9272 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9273 {
9274 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9275 vim_free(tofree);
9276 }
9277 else
9278 {
9279 ga_concat(gap, (char_u *)"Expected ");
9280 if (exp_str == NULL)
9281 {
9282 ga_concat(gap, tv2string(exp_tv, &tofree, numbuf, 0));
9283 vim_free(tofree);
9284 }
9285 else
9286 ga_concat(gap, exp_str);
9287 ga_concat(gap, (char_u *)" but got ");
9288 ga_concat(gap, tv2string(got_tv, &tofree, numbuf, 0));
9289 vim_free(tofree);
9290 }
9291}
Bram Moolenaar43345542015-11-29 17:35:35 +01009292
9293/*
9294 * Add an assert error to v:errors.
9295 */
9296 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009297assert_error(gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009298 garray_T *gap;
9299{
9300 struct vimvar *vp = &vimvars[VV_ERRORS];
9301
9302 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9303 /* Make sure v:errors is a list. */
9304 set_vim_var_list(VV_ERRORS, list_alloc());
9305 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9306}
9307
Bram Moolenaar43345542015-11-29 17:35:35 +01009308/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009309 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009310 */
9311 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009312f_assert_equal(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009313 typval_T *argvars;
9314 typval_T *rettv UNUSED;
9315{
9316 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009317
9318 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9319 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009320 prepare_assert_error(&ga);
9321 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9322 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009323 ga_clear(&ga);
9324 }
9325}
9326
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009327/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009328 * "assert_exception(string[, msg])" function
9329 */
9330 static void
9331f_assert_exception(argvars, rettv)
9332 typval_T *argvars;
9333 typval_T *rettv UNUSED;
9334{
9335 garray_T ga;
9336 char *error;
9337
9338 error = (char *)get_tv_string_chk(&argvars[0]);
9339 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9340 {
9341 prepare_assert_error(&ga);
9342 ga_concat(&ga, (char_u *)"v:exception is not set");
9343 assert_error(&ga);
9344 ga_clear(&ga);
9345 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009346 else if (error != NULL
9347 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009348 {
9349 prepare_assert_error(&ga);
9350 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9351 &vimvars[VV_EXCEPTION].vv_tv);
9352 assert_error(&ga);
9353 ga_clear(&ga);
9354 }
9355}
9356
9357/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009358 * "assert_fails(cmd [, error])" function
9359 */
9360 static void
9361f_assert_fails(argvars, rettv)
9362 typval_T *argvars;
9363 typval_T *rettv UNUSED;
9364{
9365 char_u *cmd = get_tv_string_chk(&argvars[0]);
9366 garray_T ga;
9367
9368 called_emsg = FALSE;
9369 suppress_errthrow = TRUE;
9370 emsg_silent = TRUE;
9371 do_cmdline_cmd(cmd);
9372 if (!called_emsg)
9373 {
9374 prepare_assert_error(&ga);
9375 ga_concat(&ga, (char_u *)"command did not fail: ");
9376 ga_concat(&ga, cmd);
9377 assert_error(&ga);
9378 ga_clear(&ga);
9379 }
9380 else if (argvars[1].v_type != VAR_UNKNOWN)
9381 {
9382 char_u buf[NUMBUFLEN];
9383 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9384
9385 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9386 {
9387 prepare_assert_error(&ga);
9388 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9389 &vimvars[VV_ERRMSG].vv_tv);
9390 assert_error(&ga);
9391 ga_clear(&ga);
9392 }
9393 }
9394
9395 called_emsg = FALSE;
9396 suppress_errthrow = FALSE;
9397 emsg_silent = FALSE;
9398 emsg_on_display = FALSE;
9399 set_vim_var_string(VV_ERRMSG, NULL, 0);
9400}
9401
9402/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009403 * Common for assert_true() and assert_false().
9404 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009405 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009406assert_bool(argvars, isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009407 typval_T *argvars;
9408 int isTrue;
9409{
9410 int error = FALSE;
9411 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009412
9413 if (argvars[0].v_type != VAR_NUMBER
9414 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9415 || error)
9416 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009417 prepare_assert_error(&ga);
9418 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009419 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009420 NULL, &argvars[0]);
9421 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009422 ga_clear(&ga);
9423 }
9424}
9425
9426/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009427 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009428 */
9429 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009430f_assert_false(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009431 typval_T *argvars;
9432 typval_T *rettv UNUSED;
9433{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009434 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009435}
9436
9437/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009438 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009439 */
9440 static void
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009441f_assert_true(argvars, rettv)
Bram Moolenaar43345542015-11-29 17:35:35 +01009442 typval_T *argvars;
9443 typval_T *rettv UNUSED;
9444{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009445 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009446}
9447
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009448#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009449/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009450 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009451 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009452 static void
9453f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009454 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009455 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009456{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009457 float_T f;
9458
9459 rettv->v_type = VAR_FLOAT;
9460 if (get_float_arg(argvars, &f) == OK)
9461 rettv->vval.v_float = asin(f);
9462 else
9463 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009464}
9465
9466/*
9467 * "atan()" function
9468 */
9469 static void
9470f_atan(argvars, rettv)
9471 typval_T *argvars;
9472 typval_T *rettv;
9473{
9474 float_T f;
9475
9476 rettv->v_type = VAR_FLOAT;
9477 if (get_float_arg(argvars, &f) == OK)
9478 rettv->vval.v_float = atan(f);
9479 else
9480 rettv->vval.v_float = 0.0;
9481}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009482
9483/*
9484 * "atan2()" function
9485 */
9486 static void
9487f_atan2(argvars, rettv)
9488 typval_T *argvars;
9489 typval_T *rettv;
9490{
9491 float_T fx, fy;
9492
9493 rettv->v_type = VAR_FLOAT;
9494 if (get_float_arg(argvars, &fx) == OK
9495 && get_float_arg(&argvars[1], &fy) == OK)
9496 rettv->vval.v_float = atan2(fx, fy);
9497 else
9498 rettv->vval.v_float = 0.0;
9499}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009500#endif
9501
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502/*
9503 * "browse(save, title, initdir, default)" function
9504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009507 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
9510#ifdef FEAT_BROWSE
9511 int save;
9512 char_u *title;
9513 char_u *initdir;
9514 char_u *defname;
9515 char_u buf[NUMBUFLEN];
9516 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009519 save = get_tv_number_chk(&argvars[0], &error);
9520 title = get_tv_string_chk(&argvars[1]);
9521 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9522 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009524 if (error || title == NULL || initdir == NULL || defname == NULL)
9525 rettv->vval.v_string = NULL;
9526 else
9527 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009528 do_browse(save ? BROWSE_SAVE : 0,
9529 title, defname, NULL, initdir, NULL, curbuf);
9530#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009532#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009534}
9535
9536/*
9537 * "browsedir(title, initdir)" function
9538 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009541 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009542 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009543{
9544#ifdef FEAT_BROWSE
9545 char_u *title;
9546 char_u *initdir;
9547 char_u buf[NUMBUFLEN];
9548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 title = get_tv_string_chk(&argvars[0]);
9550 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 if (title == NULL || initdir == NULL)
9553 rettv->vval.v_string = NULL;
9554 else
9555 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009556 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561}
9562
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009563static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009564
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565/*
9566 * Find a buffer by number or exact name.
9567 */
9568 static buf_T *
9569find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009570 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571{
9572 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009574 if (avar->v_type == VAR_NUMBER)
9575 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009576 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009578 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009579 if (buf == NULL)
9580 {
9581 /* No full path name match, try a match with a URL or a "nofile"
9582 * buffer, these don't use the full path. */
9583 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9584 if (buf->b_fname != NULL
9585 && (path_with_url(buf->b_fname)
9586#ifdef FEAT_QUICKFIX
9587 || bt_nofile(buf)
9588#endif
9589 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009590 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009591 break;
9592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 }
9594 return buf;
9595}
9596
9597/*
9598 * "bufexists(expr)" function
9599 */
9600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009601f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009602 typval_T *argvars;
9603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606}
9607
9608/*
9609 * "buflisted(expr)" function
9610 */
9611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009613 typval_T *argvars;
9614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615{
9616 buf_T *buf;
9617
9618 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620}
9621
9622/*
9623 * "bufloaded(expr)" function
9624 */
9625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009627 typval_T *argvars;
9628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629{
9630 buf_T *buf;
9631
9632 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634}
9635
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009636static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009637
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638/*
9639 * Get buffer by number or pattern.
9640 */
9641 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009642get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009643 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009644 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009646 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 int save_magic;
9648 char_u *save_cpo;
9649 buf_T *buf;
9650
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009651 if (tv->v_type == VAR_NUMBER)
9652 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009653 if (tv->v_type != VAR_STRING)
9654 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 if (name == NULL || *name == NUL)
9656 return curbuf;
9657 if (name[0] == '$' && name[1] == NUL)
9658 return lastbuf;
9659
9660 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9661 save_magic = p_magic;
9662 p_magic = TRUE;
9663 save_cpo = p_cpo;
9664 p_cpo = (char_u *)"";
9665
9666 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009667 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668
9669 p_magic = save_magic;
9670 p_cpo = save_cpo;
9671
9672 /* If not found, try expanding the name, like done for bufexists(). */
9673 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675
9676 return buf;
9677}
9678
9679/*
9680 * "bufname(expr)" function
9681 */
9682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009684 typval_T *argvars;
9685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687 buf_T *buf;
9688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009689 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009691 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 --emsg_off;
9698}
9699
9700/*
9701 * "bufnr(expr)" function
9702 */
9703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009705 typval_T *argvars;
9706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
9708 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009709 int error = FALSE;
9710 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009712 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009714 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009715 --emsg_off;
9716
9717 /* If the buffer isn't found and the second argument is not zero create a
9718 * new buffer. */
9719 if (buf == NULL
9720 && argvars[1].v_type != VAR_UNKNOWN
9721 && get_tv_number_chk(&argvars[1], &error) != 0
9722 && !error
9723 && (name = get_tv_string_chk(&argvars[0])) != NULL
9724 && !error)
9725 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9726
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731}
9732
9733/*
9734 * "bufwinnr(nr)" function
9735 */
9736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009738 typval_T *argvars;
9739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741#ifdef FEAT_WINDOWS
9742 win_T *wp;
9743 int winnr = 0;
9744#endif
9745 buf_T *buf;
9746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009747 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009749 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750#ifdef FEAT_WINDOWS
9751 for (wp = firstwin; wp; wp = wp->w_next)
9752 {
9753 ++winnr;
9754 if (wp->w_buffer == buf)
9755 break;
9756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009759 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760#endif
9761 --emsg_off;
9762}
9763
9764/*
9765 * "byte2line(byte)" function
9766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009769 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771{
9772#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774#else
9775 long boff = 0;
9776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009781 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 (linenr_T)0, &boff);
9783#endif
9784}
9785
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009786 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009787byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009788 typval_T *argvars;
9789 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009790 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009791{
9792#ifdef FEAT_MBYTE
9793 char_u *t;
9794#endif
9795 char_u *str;
9796 long idx;
9797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009798 str = get_tv_string_chk(&argvars[0]);
9799 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009801 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009802 return;
9803
9804#ifdef FEAT_MBYTE
9805 t = str;
9806 for ( ; idx > 0; idx--)
9807 {
9808 if (*t == NUL) /* EOL reached */
9809 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009810 if (enc_utf8 && comp)
9811 t += utf_ptr2len(t);
9812 else
9813 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009814 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009815 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009816#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009817 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009819#endif
9820}
9821
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009822/*
9823 * "byteidx()" function
9824 */
9825 static void
9826f_byteidx(argvars, rettv)
9827 typval_T *argvars;
9828 typval_T *rettv;
9829{
9830 byteidx(argvars, rettv, FALSE);
9831}
9832
9833/*
9834 * "byteidxcomp()" function
9835 */
9836 static void
9837f_byteidxcomp(argvars, rettv)
9838 typval_T *argvars;
9839 typval_T *rettv;
9840{
9841 byteidx(argvars, rettv, TRUE);
9842}
9843
Bram Moolenaardb913952012-06-29 12:54:53 +02009844 int
9845func_call(name, args, selfdict, rettv)
9846 char_u *name;
9847 typval_T *args;
9848 dict_T *selfdict;
9849 typval_T *rettv;
9850{
9851 listitem_T *item;
9852 typval_T argv[MAX_FUNC_ARGS + 1];
9853 int argc = 0;
9854 int dummy;
9855 int r = 0;
9856
9857 for (item = args->vval.v_list->lv_first; item != NULL;
9858 item = item->li_next)
9859 {
9860 if (argc == MAX_FUNC_ARGS)
9861 {
9862 EMSG(_("E699: Too many arguments"));
9863 break;
9864 }
9865 /* Make a copy of each argument. This is needed to be able to set
9866 * v_lock to VAR_FIXED in the copy without changing the original list.
9867 */
9868 copy_tv(&item->li_tv, &argv[argc++]);
9869 }
9870
9871 if (item == NULL)
9872 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9873 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9874 &dummy, TRUE, selfdict);
9875
9876 /* Free the arguments. */
9877 while (argc > 0)
9878 clear_tv(&argv[--argc]);
9879
9880 return r;
9881}
9882
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009883/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009884 * "call(func, arglist)" function
9885 */
9886 static void
9887f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009888 typval_T *argvars;
9889 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009890{
9891 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009892 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009893
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009894 if (argvars[1].v_type != VAR_LIST)
9895 {
9896 EMSG(_(e_listreq));
9897 return;
9898 }
9899 if (argvars[1].vval.v_list == NULL)
9900 return;
9901
9902 if (argvars[0].v_type == VAR_FUNC)
9903 func = argvars[0].vval.v_string;
9904 else
9905 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009906 if (*func == NUL)
9907 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009908
Bram Moolenaare9a41262005-01-15 22:18:47 +00009909 if (argvars[2].v_type != VAR_UNKNOWN)
9910 {
9911 if (argvars[2].v_type != VAR_DICT)
9912 {
9913 EMSG(_(e_dictreq));
9914 return;
9915 }
9916 selfdict = argvars[2].vval.v_dict;
9917 }
9918
Bram Moolenaardb913952012-06-29 12:54:53 +02009919 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009920}
9921
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009922#ifdef FEAT_FLOAT
9923/*
9924 * "ceil({float})" function
9925 */
9926 static void
9927f_ceil(argvars, rettv)
9928 typval_T *argvars;
9929 typval_T *rettv;
9930{
9931 float_T f;
9932
9933 rettv->v_type = VAR_FLOAT;
9934 if (get_float_arg(argvars, &f) == OK)
9935 rettv->vval.v_float = ceil(f);
9936 else
9937 rettv->vval.v_float = 0.0;
9938}
9939#endif
9940
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009941/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009942 * "changenr()" function
9943 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009944 static void
9945f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009946 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009947 typval_T *rettv;
9948{
9949 rettv->vval.v_number = curbuf->b_u_seq_cur;
9950}
9951
9952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 * "char2nr(string)" function
9954 */
9955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009956f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 typval_T *argvars;
9958 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959{
9960#ifdef FEAT_MBYTE
9961 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009962 {
9963 int utf8 = 0;
9964
9965 if (argvars[1].v_type != VAR_UNKNOWN)
9966 utf8 = get_tv_number_chk(&argvars[1], NULL);
9967
9968 if (utf8)
9969 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9970 else
9971 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 else
9974#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009976}
9977
9978/*
9979 * "cindent(lnum)" function
9980 */
9981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009983 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985{
9986#ifdef FEAT_CINDENT
9987 pos_T pos;
9988 linenr_T lnum;
9989
9990 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009991 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9993 {
9994 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009995 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 curwin->w_cursor = pos;
9997 }
9998 else
9999#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001}
10002
10003/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010004 * "clearmatches()" function
10005 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010006 static void
10007f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +000010008 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010009 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010010{
10011#ifdef FEAT_SEARCH_EXTRA
10012 clear_matches(curwin);
10013#endif
10014}
10015
10016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 * "col(string)" function
10018 */
10019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010021 typval_T *argvars;
10022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023{
10024 colnr_T col = 0;
10025 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010026 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010028 fp = var2fpos(&argvars[0], FALSE, &fnum);
10029 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030 {
10031 if (fp->col == MAXCOL)
10032 {
10033 /* '> can be MAXCOL, get the length of the line then */
10034 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010035 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 else
10037 col = MAXCOL;
10038 }
10039 else
10040 {
10041 col = fp->col + 1;
10042#ifdef FEAT_VIRTUALEDIT
10043 /* col(".") when the cursor is on the NUL at the end of the line
10044 * because of "coladd" can be seen as an extra column. */
10045 if (virtual_active() && fp == &curwin->w_cursor)
10046 {
10047 char_u *p = ml_get_cursor();
10048
10049 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10050 curwin->w_virtcol - curwin->w_cursor.coladd))
10051 {
10052# ifdef FEAT_MBYTE
10053 int l;
10054
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010055 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 col += l;
10057# else
10058 if (*p != NUL && p[1] == NUL)
10059 ++col;
10060# endif
10061 }
10062 }
10063#endif
10064 }
10065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067}
10068
Bram Moolenaar572cb562005-08-05 21:35:02 +000010069#if defined(FEAT_INS_EXPAND)
10070/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010071 * "complete()" function
10072 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010073 static void
10074f_complete(argvars, rettv)
10075 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010076 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +000010077{
10078 int startcol;
10079
10080 if ((State & INSERT) == 0)
10081 {
10082 EMSG(_("E785: complete() can only be used in Insert mode"));
10083 return;
10084 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010085
10086 /* Check for undo allowed here, because if something was already inserted
10087 * the line was already saved for undo and this check isn't done. */
10088 if (!undo_allowed())
10089 return;
10090
Bram Moolenaarade00832006-03-10 21:46:58 +000010091 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10092 {
10093 EMSG(_(e_invarg));
10094 return;
10095 }
10096
10097 startcol = get_tv_number_chk(&argvars[0], NULL);
10098 if (startcol <= 0)
10099 return;
10100
10101 set_completion(startcol - 1, argvars[1].vval.v_list);
10102}
10103
10104/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010105 * "complete_add()" function
10106 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010107 static void
10108f_complete_add(argvars, rettv)
10109 typval_T *argvars;
10110 typval_T *rettv;
10111{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010112 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010113}
10114
10115/*
10116 * "complete_check()" function
10117 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010118 static void
10119f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010120 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +000010121 typval_T *rettv;
10122{
10123 int saved = RedrawingDisabled;
10124
10125 RedrawingDisabled = 0;
10126 ins_compl_check_keys(0);
10127 rettv->vval.v_number = compl_interrupted;
10128 RedrawingDisabled = saved;
10129}
10130#endif
10131
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132/*
10133 * "confirm(message, buttons[, default [, type]])" function
10134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010136f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010137 typval_T *argvars UNUSED;
10138 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139{
10140#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10141 char_u *message;
10142 char_u *buttons = NULL;
10143 char_u buf[NUMBUFLEN];
10144 char_u buf2[NUMBUFLEN];
10145 int def = 1;
10146 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010147 char_u *typestr;
10148 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 message = get_tv_string_chk(&argvars[0]);
10151 if (message == NULL)
10152 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010153 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010155 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10156 if (buttons == NULL)
10157 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010158 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010161 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010163 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10164 if (typestr == NULL)
10165 error = TRUE;
10166 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010168 switch (TOUPPER_ASC(*typestr))
10169 {
10170 case 'E': type = VIM_ERROR; break;
10171 case 'Q': type = VIM_QUESTION; break;
10172 case 'I': type = VIM_INFO; break;
10173 case 'W': type = VIM_WARNING; break;
10174 case 'G': type = VIM_GENERIC; break;
10175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 }
10177 }
10178 }
10179 }
10180
10181 if (buttons == NULL || *buttons == NUL)
10182 buttons = (char_u *)_("&Ok");
10183
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010184 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010186 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187#endif
10188}
10189
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010190/*
10191 * "copy()" function
10192 */
10193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010194f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010195 typval_T *argvars;
10196 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010197{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010198 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010199}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010201#ifdef FEAT_FLOAT
10202/*
10203 * "cos()" function
10204 */
10205 static void
10206f_cos(argvars, rettv)
10207 typval_T *argvars;
10208 typval_T *rettv;
10209{
10210 float_T f;
10211
10212 rettv->v_type = VAR_FLOAT;
10213 if (get_float_arg(argvars, &f) == OK)
10214 rettv->vval.v_float = cos(f);
10215 else
10216 rettv->vval.v_float = 0.0;
10217}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010218
10219/*
10220 * "cosh()" function
10221 */
10222 static void
10223f_cosh(argvars, rettv)
10224 typval_T *argvars;
10225 typval_T *rettv;
10226{
10227 float_T f;
10228
10229 rettv->v_type = VAR_FLOAT;
10230 if (get_float_arg(argvars, &f) == OK)
10231 rettv->vval.v_float = cosh(f);
10232 else
10233 rettv->vval.v_float = 0.0;
10234}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010235#endif
10236
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010238 * "count()" function
10239 */
10240 static void
10241f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010242 typval_T *argvars;
10243 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010244{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010245 long n = 0;
10246 int ic = FALSE;
10247
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010249 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010250 listitem_T *li;
10251 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010252 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010253
Bram Moolenaare9a41262005-01-15 22:18:47 +000010254 if ((l = argvars[0].vval.v_list) != NULL)
10255 {
10256 li = l->lv_first;
10257 if (argvars[2].v_type != VAR_UNKNOWN)
10258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259 int error = FALSE;
10260
10261 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262 if (argvars[3].v_type != VAR_UNKNOWN)
10263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010264 idx = get_tv_number_chk(&argvars[3], &error);
10265 if (!error)
10266 {
10267 li = list_find(l, idx);
10268 if (li == NULL)
10269 EMSGN(_(e_listidx), idx);
10270 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010272 if (error)
10273 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010274 }
10275
10276 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010277 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278 ++n;
10279 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 else if (argvars[0].v_type == VAR_DICT)
10282 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010283 int todo;
10284 dict_T *d;
10285 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010287 if ((d = argvars[0].vval.v_dict) != NULL)
10288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 int error = FALSE;
10290
Bram Moolenaare9a41262005-01-15 22:18:47 +000010291 if (argvars[2].v_type != VAR_UNKNOWN)
10292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010294 if (argvars[3].v_type != VAR_UNKNOWN)
10295 EMSG(_(e_invarg));
10296 }
10297
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010298 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010299 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010300 {
10301 if (!HASHITEM_EMPTY(hi))
10302 {
10303 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010304 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010305 ++n;
10306 }
10307 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010308 }
10309 }
10310 else
10311 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010312 rettv->vval.v_number = n;
10313}
10314
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010315#ifdef FEAT_CHANNEL
10316/*
10317 * Get a callback from "arg". It can be a Funcref or a function name.
10318 * When "arg" is zero return an empty string.
10319 * Return NULL for an invalid argument.
10320 */
10321 static char_u *
10322get_callback(typval_T *arg)
10323{
10324 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
10325 return arg->vval.v_string;
10326 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
10327 return (char_u *)"";
10328 EMSG(_("E999: Invalid callback argument"));
10329 return NULL;
10330}
10331
10332/*
10333 * "connect()" function
10334 */
10335 static void
10336f_connect(argvars, rettv)
10337 typval_T *argvars;
10338 typval_T *rettv;
10339{
10340 char_u *address;
10341 char_u *mode;
10342 char_u *callback = NULL;
10343 char_u buf1[NUMBUFLEN];
10344 char_u *p;
10345 int port;
10346 int json_mode = FALSE;
10347
10348 address = get_tv_string(&argvars[0]);
10349 mode = get_tv_string_buf(&argvars[1], buf1);
10350 if (argvars[2].v_type != VAR_UNKNOWN)
10351 {
10352 callback = get_callback(&argvars[2]);
10353 if (callback == NULL)
10354 return;
10355 }
10356
10357 /* parse address */
10358 p = vim_strchr(address, ':');
10359 if (p == NULL)
10360 {
10361 EMSG2(_(e_invarg2), address);
10362 return;
10363 }
10364 *p++ = NUL;
10365 port = atoi((char *)p);
10366 if (*address == NUL || port <= 0)
10367 {
10368 p[-1] = ':';
10369 EMSG2(_(e_invarg2), address);
10370 return;
10371 }
10372
10373 /* parse mode */
10374 if (STRCMP(mode, "json") == 0)
10375 json_mode = TRUE;
10376 else if (STRCMP(mode, "raw") != 0)
10377 {
10378 EMSG2(_(e_invarg2), mode);
10379 return;
10380 }
10381
10382 rettv->vval.v_number = channel_open((char *)address, port, NULL);
10383 if (rettv->vval.v_number >= 0)
10384 {
10385 channel_set_json_mode(rettv->vval.v_number, json_mode);
10386 if (callback != NULL && *callback != NUL)
10387 channel_set_callback(rettv->vval.v_number, callback);
10388 }
10389}
10390#endif
10391
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10394 *
10395 * Checks the existence of a cscope connection.
10396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010398f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010399 typval_T *argvars UNUSED;
10400 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010401{
10402#ifdef FEAT_CSCOPE
10403 int num = 0;
10404 char_u *dbpath = NULL;
10405 char_u *prepend = NULL;
10406 char_u buf[NUMBUFLEN];
10407
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010408 if (argvars[0].v_type != VAR_UNKNOWN
10409 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010411 num = (int)get_tv_number(&argvars[0]);
10412 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010413 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010414 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 }
10416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418#endif
10419}
10420
10421/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010422 * "cursor(lnum, col)" function, or
10423 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010425 * Moves the cursor to the specified line and column.
10426 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010430 typval_T *argvars;
10431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010434#ifdef FEAT_VIRTUALEDIT
10435 long coladd = 0;
10436#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010437 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010439 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010440 if (argvars[1].v_type == VAR_UNKNOWN)
10441 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010442 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010443 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010444
Bram Moolenaar493c1782014-05-28 14:34:46 +020010445 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010446 {
10447 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010448 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010449 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010450 line = pos.lnum;
10451 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010452#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010453 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010454#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010455 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010456 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010457 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010458 set_curswant = FALSE;
10459 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010460 }
10461 else
10462 {
10463 line = get_tv_lnum(argvars);
10464 col = get_tv_number_chk(&argvars[1], NULL);
10465#ifdef FEAT_VIRTUALEDIT
10466 if (argvars[2].v_type != VAR_UNKNOWN)
10467 coladd = get_tv_number_chk(&argvars[2], NULL);
10468#endif
10469 }
10470 if (line < 0 || col < 0
10471#ifdef FEAT_VIRTUALEDIT
10472 || coladd < 0
10473#endif
10474 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 if (line > 0)
10477 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 if (col > 0)
10479 curwin->w_cursor.col = col - 1;
10480#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010481 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482#endif
10483
10484 /* Make sure the cursor is in a valid position. */
10485 check_cursor();
10486#ifdef FEAT_MBYTE
10487 /* Correct cursor for multi-byte character. */
10488 if (has_mbyte)
10489 mb_adjust_cursor();
10490#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010491
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010492 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010493 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494}
10495
10496/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010497 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 */
10499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010500f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010501 typval_T *argvars;
10502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010504 int noref = 0;
10505
10506 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010508 if (noref < 0 || noref > 1)
10509 EMSG(_(e_invarg));
10510 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010511 {
10512 current_copyID += COPYID_INC;
10513 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515}
10516
10517/*
10518 * "delete()" function
10519 */
10520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010521f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010522 typval_T *argvars;
10523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010525 char_u nbuf[NUMBUFLEN];
10526 char_u *name;
10527 char_u *flags;
10528
10529 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010531 return;
10532
10533 name = get_tv_string(&argvars[0]);
10534 if (name == NULL || *name == NUL)
10535 {
10536 EMSG(_(e_invarg));
10537 return;
10538 }
10539
10540 if (argvars[1].v_type != VAR_UNKNOWN)
10541 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010543 flags = (char_u *)"";
10544
10545 if (*flags == NUL)
10546 /* delete a file */
10547 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10548 else if (STRCMP(flags, "d") == 0)
10549 /* delete an empty directory */
10550 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10551 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010552 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010553 rettv->vval.v_number = delete_recursive(name);
10554 else
10555 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556}
10557
10558/*
10559 * "did_filetype()" function
10560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010562f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010563 typval_T *argvars UNUSED;
10564 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565{
10566#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010567 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568#endif
10569}
10570
10571/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010572 * "diff_filler()" function
10573 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010576 typval_T *argvars UNUSED;
10577 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010578{
10579#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010581#endif
10582}
10583
10584/*
10585 * "diff_hlID()" function
10586 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010589 typval_T *argvars UNUSED;
10590 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010591{
10592#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010594 static linenr_T prev_lnum = 0;
10595 static int changedtick = 0;
10596 static int fnum = 0;
10597 static int change_start = 0;
10598 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010599 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010600 int filler_lines;
10601 int col;
10602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010603 if (lnum < 0) /* ignore type error in {lnum} arg */
10604 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010605 if (lnum != prev_lnum
10606 || changedtick != curbuf->b_changedtick
10607 || fnum != curbuf->b_fnum)
10608 {
10609 /* New line, buffer, change: need to get the values. */
10610 filler_lines = diff_check(curwin, lnum);
10611 if (filler_lines < 0)
10612 {
10613 if (filler_lines == -1)
10614 {
10615 change_start = MAXCOL;
10616 change_end = -1;
10617 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10618 hlID = HLF_ADD; /* added line */
10619 else
10620 hlID = HLF_CHD; /* changed line */
10621 }
10622 else
10623 hlID = HLF_ADD; /* added line */
10624 }
10625 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010626 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010627 prev_lnum = lnum;
10628 changedtick = curbuf->b_changedtick;
10629 fnum = curbuf->b_fnum;
10630 }
10631
10632 if (hlID == HLF_CHD || hlID == HLF_TXD)
10633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010634 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010635 if (col >= change_start && col <= change_end)
10636 hlID = HLF_TXD; /* changed text */
10637 else
10638 hlID = HLF_CHD; /* changed line */
10639 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010640 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010641#endif
10642}
10643
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010010644#ifdef FEAT_CHANNEL
10645/*
10646 * Get the channel index from the handle argument.
10647 * Returns -1 if the handle is invalid or the channel is closed.
10648 */
10649 static int
10650get_channel_arg(typval_T *tv)
10651{
10652 int ch_idx;
10653
10654 if (tv->v_type != VAR_NUMBER)
10655 {
10656 EMSG2(_(e_invarg2), get_tv_string(tv));
10657 return -1;
10658 }
10659 ch_idx = tv->vval.v_number;
10660
10661 if (!channel_is_open(ch_idx))
10662 {
10663 EMSGN(_("E999: not an open channel"), ch_idx);
10664 return -1;
10665 }
10666 return ch_idx;
10667}
10668
10669/*
10670 * "disconnect()" function
10671 */
10672 static void
10673f_disconnect(argvars, rettv)
10674 typval_T *argvars;
10675 typval_T *rettv UNUSED;
10676{
10677 int ch_idx = get_channel_arg(&argvars[0]);
10678
10679 if (ch_idx >= 0)
10680 channel_close(ch_idx);
10681}
10682#endif
10683
Bram Moolenaar47136d72004-10-12 20:02:24 +000010684/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010685 * "empty({expr})" function
10686 */
10687 static void
10688f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010689 typval_T *argvars;
10690 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010691{
10692 int n;
10693
10694 switch (argvars[0].v_type)
10695 {
10696 case VAR_STRING:
10697 case VAR_FUNC:
10698 n = argvars[0].vval.v_string == NULL
10699 || *argvars[0].vval.v_string == NUL;
10700 break;
10701 case VAR_NUMBER:
10702 n = argvars[0].vval.v_number == 0;
10703 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010704#ifdef FEAT_FLOAT
10705 case VAR_FLOAT:
10706 n = argvars[0].vval.v_float == 0.0;
10707 break;
10708#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010709 case VAR_LIST:
10710 n = argvars[0].vval.v_list == NULL
10711 || argvars[0].vval.v_list->lv_first == NULL;
10712 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010713 case VAR_DICT:
10714 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010715 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010716 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010717 case VAR_SPECIAL:
10718 n = argvars[0].vval.v_number != VVAL_TRUE;
10719 break;
10720
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010721 default:
10722 EMSG2(_(e_intern2), "f_empty()");
10723 n = 0;
10724 }
10725
10726 rettv->vval.v_number = n;
10727}
10728
10729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 * "escape({string}, {chars})" function
10731 */
10732 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010734 typval_T *argvars;
10735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736{
10737 char_u buf[NUMBUFLEN];
10738
Bram Moolenaar758711c2005-02-02 23:11:38 +000010739 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10740 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742}
10743
10744/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010745 * "eval()" function
10746 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010747 static void
10748f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010749 typval_T *argvars;
10750 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010751{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010752 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 s = get_tv_string_chk(&argvars[0]);
10755 if (s != NULL)
10756 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010757
Bram Moolenaar615b9972015-01-14 17:15:05 +010010758 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10760 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010761 if (p != NULL && !aborting())
10762 EMSG2(_(e_invexpr2), p);
10763 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010764 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010765 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010766 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010767 else if (*s != NUL)
10768 EMSG(_(e_trailing));
10769}
10770
10771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 * "eventhandler()" function
10773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010776 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780}
10781
10782/*
10783 * "executable()" function
10784 */
10785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010786f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010787 typval_T *argvars;
10788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010790 char_u *name = get_tv_string(&argvars[0]);
10791
10792 /* Check in $PATH and also check directly if there is a directory name. */
10793 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10794 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010795}
10796
10797/*
10798 * "exepath()" function
10799 */
10800 static void
10801f_exepath(argvars, rettv)
10802 typval_T *argvars;
10803 typval_T *rettv;
10804{
10805 char_u *p = NULL;
10806
Bram Moolenaarb5971142015-03-21 17:32:19 +010010807 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010808 rettv->v_type = VAR_STRING;
10809 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810}
10811
10812/*
10813 * "exists()" function
10814 */
10815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010817 typval_T *argvars;
10818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819{
10820 char_u *p;
10821 char_u *name;
10822 int n = FALSE;
10823 int len = 0;
10824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010825 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 if (*p == '$') /* environment variable */
10827 {
10828 /* first try "normal" environment variables (fast) */
10829 if (mch_getenv(p + 1) != NULL)
10830 n = TRUE;
10831 else
10832 {
10833 /* try expanding things like $VIM and ${HOME} */
10834 p = expand_env_save(p);
10835 if (p != NULL && *p != '$')
10836 n = TRUE;
10837 vim_free(p);
10838 }
10839 }
10840 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010841 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010842 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010843 if (*skipwhite(p) != NUL)
10844 n = FALSE; /* trailing garbage */
10845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 else if (*p == '*') /* internal or user defined function */
10847 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010848 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 }
10850 else if (*p == ':')
10851 {
10852 n = cmd_exists(p + 1);
10853 }
10854 else if (*p == '#')
10855 {
10856#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010857 if (p[1] == '#')
10858 n = autocmd_supported(p + 2);
10859 else
10860 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861#endif
10862 }
10863 else /* internal variable */
10864 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010865 char_u *tofree;
10866 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010868 /* get_name_len() takes care of expanding curly braces */
10869 name = p;
10870 len = get_name_len(&p, &tofree, TRUE, FALSE);
10871 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010873 if (tofree != NULL)
10874 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010875 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010876 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010878 /* handle d.key, l[idx], f(expr) */
10879 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10880 if (n)
10881 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 }
10883 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010884 if (*p != NUL)
10885 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010887 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 }
10889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010890 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891}
10892
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010893#ifdef FEAT_FLOAT
10894/*
10895 * "exp()" function
10896 */
10897 static void
10898f_exp(argvars, rettv)
10899 typval_T *argvars;
10900 typval_T *rettv;
10901{
10902 float_T f;
10903
10904 rettv->v_type = VAR_FLOAT;
10905 if (get_float_arg(argvars, &f) == OK)
10906 rettv->vval.v_float = exp(f);
10907 else
10908 rettv->vval.v_float = 0.0;
10909}
10910#endif
10911
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912/*
10913 * "expand()" function
10914 */
10915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010917 typval_T *argvars;
10918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919{
10920 char_u *s;
10921 int len;
10922 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010923 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010925 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010926 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010929 if (argvars[1].v_type != VAR_UNKNOWN
10930 && argvars[2].v_type != VAR_UNKNOWN
10931 && get_tv_number_chk(&argvars[2], &error)
10932 && !error)
10933 {
10934 rettv->v_type = VAR_LIST;
10935 rettv->vval.v_list = NULL;
10936 }
10937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 if (*s == '%' || *s == '#' || *s == '<')
10940 {
10941 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010942 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010944 if (rettv->v_type == VAR_LIST)
10945 {
10946 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10947 list_append_string(rettv->vval.v_list, result, -1);
10948 else
10949 vim_free(result);
10950 }
10951 else
10952 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 }
10954 else
10955 {
10956 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010957 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 if (argvars[1].v_type != VAR_UNKNOWN
10959 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010960 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 if (!error)
10962 {
10963 ExpandInit(&xpc);
10964 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010965 if (p_wic)
10966 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010967 if (rettv->v_type == VAR_STRING)
10968 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10969 options, WILD_ALL);
10970 else if (rettv_list_alloc(rettv) != FAIL)
10971 {
10972 int i;
10973
10974 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10975 for (i = 0; i < xpc.xp_numfiles; i++)
10976 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10977 ExpandCleanup(&xpc);
10978 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 }
10980 else
10981 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 }
10983}
10984
10985/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010986 * Go over all entries in "d2" and add them to "d1".
10987 * When "action" is "error" then a duplicate key is an error.
10988 * When "action" is "force" then a duplicate key is overwritten.
10989 * Otherwise duplicate keys are ignored ("action" is "keep").
10990 */
10991 void
10992dict_extend(d1, d2, action)
10993 dict_T *d1;
10994 dict_T *d2;
10995 char_u *action;
10996{
10997 dictitem_T *di1;
10998 hashitem_T *hi2;
10999 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011000 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011001
11002 todo = (int)d2->dv_hashtab.ht_used;
11003 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11004 {
11005 if (!HASHITEM_EMPTY(hi2))
11006 {
11007 --todo;
11008 di1 = dict_find(d1, hi2->hi_key, -1);
11009 if (d1->dv_scope != 0)
11010 {
11011 /* Disallow replacing a builtin function in l: and g:.
11012 * Check the key to be valid when adding to any
11013 * scope. */
11014 if (d1->dv_scope == VAR_DEF_SCOPE
11015 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11016 && var_check_func_name(hi2->hi_key,
11017 di1 == NULL))
11018 break;
11019 if (!valid_varname(hi2->hi_key))
11020 break;
11021 }
11022 if (di1 == NULL)
11023 {
11024 di1 = dictitem_copy(HI2DI(hi2));
11025 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11026 dictitem_free(di1);
11027 }
11028 else if (*action == 'e')
11029 {
11030 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11031 break;
11032 }
11033 else if (*action == 'f' && HI2DI(hi2) != di1)
11034 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011035 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11036 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011037 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011038 clear_tv(&di1->di_tv);
11039 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11040 }
11041 }
11042 }
11043}
11044
11045/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011046 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011047 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011048 */
11049 static void
11050f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011051 typval_T *argvars;
11052 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011053{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011054 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011055
Bram Moolenaare9a41262005-01-15 22:18:47 +000011056 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011057 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011058 list_T *l1, *l2;
11059 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011060 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011061 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011062
Bram Moolenaare9a41262005-01-15 22:18:47 +000011063 l1 = argvars[0].vval.v_list;
11064 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011065 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011066 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011067 {
11068 if (argvars[2].v_type != VAR_UNKNOWN)
11069 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070 before = get_tv_number_chk(&argvars[2], &error);
11071 if (error)
11072 return; /* type error; errmsg already given */
11073
Bram Moolenaar758711c2005-02-02 23:11:38 +000011074 if (before == l1->lv_len)
11075 item = NULL;
11076 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011077 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011078 item = list_find(l1, before);
11079 if (item == NULL)
11080 {
11081 EMSGN(_(e_listidx), before);
11082 return;
11083 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011084 }
11085 }
11086 else
11087 item = NULL;
11088 list_extend(l1, l2, item);
11089
Bram Moolenaare9a41262005-01-15 22:18:47 +000011090 copy_tv(&argvars[0], rettv);
11091 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011092 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11094 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011095 dict_T *d1, *d2;
11096 char_u *action;
11097 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011098
11099 d1 = argvars[0].vval.v_dict;
11100 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011101 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011102 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011103 {
11104 /* Check the third argument. */
11105 if (argvars[2].v_type != VAR_UNKNOWN)
11106 {
11107 static char *(av[]) = {"keep", "force", "error"};
11108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 action = get_tv_string_chk(&argvars[2]);
11110 if (action == NULL)
11111 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 for (i = 0; i < 3; ++i)
11113 if (STRCMP(action, av[i]) == 0)
11114 break;
11115 if (i == 3)
11116 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011117 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011118 return;
11119 }
11120 }
11121 else
11122 action = (char_u *)"force";
11123
Bram Moolenaara9922d62013-05-30 13:01:18 +020011124 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011125
Bram Moolenaare9a41262005-01-15 22:18:47 +000011126 copy_tv(&argvars[0], rettv);
11127 }
11128 }
11129 else
11130 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011131}
11132
11133/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011134 * "feedkeys()" function
11135 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011136 static void
11137f_feedkeys(argvars, rettv)
11138 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011139 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011140{
11141 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011142 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011143 char_u *keys, *flags;
11144 char_u nbuf[NUMBUFLEN];
11145 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011146 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011147 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011148
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011149 /* This is not allowed in the sandbox. If the commands would still be
11150 * executed in the sandbox it would be OK, but it probably happens later,
11151 * when "sandbox" is no longer set. */
11152 if (check_secure())
11153 return;
11154
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011155 keys = get_tv_string(&argvars[0]);
11156 if (*keys != NUL)
11157 {
11158 if (argvars[1].v_type != VAR_UNKNOWN)
11159 {
11160 flags = get_tv_string_buf(&argvars[1], nbuf);
11161 for ( ; *flags != NUL; ++flags)
11162 {
11163 switch (*flags)
11164 {
11165 case 'n': remap = FALSE; break;
11166 case 'm': remap = TRUE; break;
11167 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011168 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011169 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011170 }
11171 }
11172 }
11173
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011174 /* Need to escape K_SPECIAL and CSI before putting the string in the
11175 * typeahead buffer. */
11176 keys_esc = vim_strsave_escape_csi(keys);
11177 if (keys_esc != NULL)
11178 {
11179 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011180 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011181 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011182 if (vgetc_busy)
11183 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011184 if (execute)
11185 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011186 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011187 }
11188}
11189
11190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191 * "filereadable()" function
11192 */
11193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011195 typval_T *argvars;
11196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011198 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 char_u *p;
11200 int n;
11201
Bram Moolenaarc236c162008-07-13 17:41:49 +000011202#ifndef O_NONBLOCK
11203# define O_NONBLOCK 0
11204#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011206 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11207 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 {
11209 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011210 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 }
11212 else
11213 n = FALSE;
11214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216}
11217
11218/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011219 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 * rights to write into.
11221 */
11222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011223f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011224 typval_T *argvars;
11225 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011227 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228}
11229
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011230static void findfilendir(typval_T *argvars, typval_T *rettv, int find_what);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011231
11232 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011233findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011234 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011235 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011236 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011237{
11238#ifdef FEAT_SEARCHPATH
11239 char_u *fname;
11240 char_u *fresult = NULL;
11241 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11242 char_u *p;
11243 char_u pathbuf[NUMBUFLEN];
11244 int count = 1;
11245 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011246 int error = FALSE;
11247#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011248
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011249 rettv->vval.v_string = NULL;
11250 rettv->v_type = VAR_STRING;
11251
11252#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011253 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011254
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011255 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011257 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11258 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011259 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011260 else
11261 {
11262 if (*p != NUL)
11263 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011265 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011266 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011267 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011268 }
11269
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011270 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11271 error = TRUE;
11272
11273 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011275 do
11276 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011277 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011278 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279 fresult = find_file_in_path_option(first ? fname : NULL,
11280 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011281 0, first, path,
11282 find_what,
11283 curbuf->b_ffname,
11284 find_what == FINDFILE_DIR
11285 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011286 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011287
11288 if (fresult != NULL && rettv->v_type == VAR_LIST)
11289 list_append_string(rettv->vval.v_list, fresult, -1);
11290
11291 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011293
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011294 if (rettv->v_type == VAR_STRING)
11295 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011296#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011297}
11298
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011299static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11300static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011301
11302/*
11303 * Implementation of map() and filter().
11304 */
11305 static void
11306filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000011307 typval_T *argvars;
11308 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011309 int map;
11310{
11311 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011312 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011313 listitem_T *li, *nli;
11314 list_T *l = NULL;
11315 dictitem_T *di;
11316 hashtab_T *ht;
11317 hashitem_T *hi;
11318 dict_T *d = NULL;
11319 typval_T save_val;
11320 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011321 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011322 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011323 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011324 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011325 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011326 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011327 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011328
Bram Moolenaare9a41262005-01-15 22:18:47 +000011329 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011330 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011331 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011332 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011333 return;
11334 }
11335 else if (argvars[0].v_type == VAR_DICT)
11336 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011337 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011338 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011339 return;
11340 }
11341 else
11342 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011343 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011344 return;
11345 }
11346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347 expr = get_tv_string_buf_chk(&argvars[1], buf);
11348 /* On type errors, the preceding call has already displayed an error
11349 * message. Avoid a misleading error message for an empty string that
11350 * was not passed as argument. */
11351 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011353 prepare_vimvar(VV_VAL, &save_val);
11354 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011355
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011356 /* We reset "did_emsg" to be able to detect whether an error
11357 * occurred during evaluation of the expression. */
11358 save_did_emsg = did_emsg;
11359 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011360
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011361 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 vimvars[VV_KEY].vv_type = VAR_STRING;
11365
11366 ht = &d->dv_hashtab;
11367 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011368 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011369 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 if (!HASHITEM_EMPTY(hi))
11372 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011373 int r;
11374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 --todo;
11376 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011377 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011378 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11379 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011380 break;
11381 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011382 r = filter_map_one(&di->di_tv, expr, map, &rem);
11383 clear_tv(&vimvars[VV_KEY].vv_tv);
11384 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011385 break;
11386 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011387 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011388 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11389 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011390 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011391 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 }
11394 }
11395 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011396 }
11397 else
11398 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011399 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011401 for (li = l->lv_first; li != NULL; li = nli)
11402 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011403 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011404 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011406 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011407 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011408 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011409 break;
11410 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011412 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011413 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011414 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011415
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011416 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011418
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011419 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011420 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011421
11422 copy_tv(&argvars[0], rettv);
11423}
11424
11425 static int
11426filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011427 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011428 char_u *expr;
11429 int map;
11430 int *remp;
11431{
Bram Moolenaar33570922005-01-25 22:26:29 +000011432 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011433 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011434 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011435
Bram Moolenaar33570922005-01-25 22:26:29 +000011436 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011437 s = expr;
11438 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011439 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011440 if (*s != NUL) /* check for trailing chars after expr */
11441 {
11442 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011443 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011444 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011445 }
11446 if (map)
11447 {
11448 /* map(): replace the list item value */
11449 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011450 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011451 *tv = rettv;
11452 }
11453 else
11454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011455 int error = FALSE;
11456
Bram Moolenaare9a41262005-01-15 22:18:47 +000011457 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011458 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011459 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460 /* On type error, nothing has been removed; return FAIL to stop the
11461 * loop. The error message was given by get_tv_number_chk(). */
11462 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011463 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011464 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011465 retval = OK;
11466theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011467 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011468 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011469}
11470
11471/*
11472 * "filter()" function
11473 */
11474 static void
11475f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011476 typval_T *argvars;
11477 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011478{
11479 filter_map(argvars, rettv, FALSE);
11480}
11481
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011482/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011483 * "finddir({fname}[, {path}[, {count}]])" function
11484 */
11485 static void
11486f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011487 typval_T *argvars;
11488 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011489{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011490 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011491}
11492
11493/*
11494 * "findfile({fname}[, {path}[, {count}]])" function
11495 */
11496 static void
11497f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011498 typval_T *argvars;
11499 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011501 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011502}
11503
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011504#ifdef FEAT_FLOAT
11505/*
11506 * "float2nr({float})" function
11507 */
11508 static void
11509f_float2nr(argvars, rettv)
11510 typval_T *argvars;
11511 typval_T *rettv;
11512{
11513 float_T f;
11514
11515 if (get_float_arg(argvars, &f) == OK)
11516 {
11517 if (f < -0x7fffffff)
11518 rettv->vval.v_number = -0x7fffffff;
11519 else if (f > 0x7fffffff)
11520 rettv->vval.v_number = 0x7fffffff;
11521 else
11522 rettv->vval.v_number = (varnumber_T)f;
11523 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011524}
11525
11526/*
11527 * "floor({float})" function
11528 */
11529 static void
11530f_floor(argvars, rettv)
11531 typval_T *argvars;
11532 typval_T *rettv;
11533{
11534 float_T f;
11535
11536 rettv->v_type = VAR_FLOAT;
11537 if (get_float_arg(argvars, &f) == OK)
11538 rettv->vval.v_float = floor(f);
11539 else
11540 rettv->vval.v_float = 0.0;
11541}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011542
11543/*
11544 * "fmod()" function
11545 */
11546 static void
11547f_fmod(argvars, rettv)
11548 typval_T *argvars;
11549 typval_T *rettv;
11550{
11551 float_T fx, fy;
11552
11553 rettv->v_type = VAR_FLOAT;
11554 if (get_float_arg(argvars, &fx) == OK
11555 && get_float_arg(&argvars[1], &fy) == OK)
11556 rettv->vval.v_float = fmod(fx, fy);
11557 else
11558 rettv->vval.v_float = 0.0;
11559}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011560#endif
11561
Bram Moolenaar0d660222005-01-07 21:51:51 +000011562/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011563 * "fnameescape({string})" function
11564 */
11565 static void
11566f_fnameescape(argvars, rettv)
11567 typval_T *argvars;
11568 typval_T *rettv;
11569{
11570 rettv->vval.v_string = vim_strsave_fnameescape(
11571 get_tv_string(&argvars[0]), FALSE);
11572 rettv->v_type = VAR_STRING;
11573}
11574
11575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 * "fnamemodify({fname}, {mods})" function
11577 */
11578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011579f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011580 typval_T *argvars;
11581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582{
11583 char_u *fname;
11584 char_u *mods;
11585 int usedlen = 0;
11586 int len;
11587 char_u *fbuf = NULL;
11588 char_u buf[NUMBUFLEN];
11589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011590 fname = get_tv_string_chk(&argvars[0]);
11591 mods = get_tv_string_buf_chk(&argvars[1], buf);
11592 if (fname == NULL || mods == NULL)
11593 fname = NULL;
11594 else
11595 {
11596 len = (int)STRLEN(fname);
11597 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011604 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605 vim_free(fbuf);
11606}
11607
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011608static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609
11610/*
11611 * "foldclosed()" function
11612 */
11613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011615 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011617 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618{
11619#ifdef FEAT_FOLDING
11620 linenr_T lnum;
11621 linenr_T first, last;
11622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011623 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011624 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11625 {
11626 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11627 {
11628 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011631 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632 return;
11633 }
11634 }
11635#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011636 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637}
11638
11639/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011640 * "foldclosed()" function
11641 */
11642 static void
11643f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011644 typval_T *argvars;
11645 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011646{
11647 foldclosed_both(argvars, rettv, FALSE);
11648}
11649
11650/*
11651 * "foldclosedend()" function
11652 */
11653 static void
11654f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 typval_T *argvars;
11656 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011657{
11658 foldclosed_both(argvars, rettv, TRUE);
11659}
11660
11661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 * "foldlevel()" function
11663 */
11664 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011666 typval_T *argvars UNUSED;
11667 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668{
11669#ifdef FEAT_FOLDING
11670 linenr_T lnum;
11671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011672 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011674 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676}
11677
11678/*
11679 * "foldtext()" function
11680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011683 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
11686#ifdef FEAT_FOLDING
11687 linenr_T lnum;
11688 char_u *s;
11689 char_u *r;
11690 int len;
11691 char *txt;
11692#endif
11693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011694 rettv->v_type = VAR_STRING;
11695 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011696#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011697 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11698 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11699 <= curbuf->b_ml.ml_line_count
11700 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701 {
11702 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011703 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11704 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 {
11706 if (!linewhite(lnum))
11707 break;
11708 ++lnum;
11709 }
11710
11711 /* Find interesting text in this line. */
11712 s = skipwhite(ml_get(lnum));
11713 /* skip C comment-start */
11714 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011716 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011717 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011718 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011719 {
11720 s = skipwhite(ml_get(lnum + 1));
11721 if (*s == '*')
11722 s = skipwhite(s + 1);
11723 }
11724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725 txt = _("+-%s%3ld lines: ");
11726 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011727 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 + 20 /* for %3ld */
11729 + STRLEN(s))); /* concatenated */
11730 if (r != NULL)
11731 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11733 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11734 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 len = (int)STRLEN(r);
11736 STRCAT(r, s);
11737 /* remove 'foldmarker' and 'commentstring' */
11738 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011739 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 }
11741 }
11742#endif
11743}
11744
11745/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011746 * "foldtextresult(lnum)" function
11747 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011749f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011750 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011751 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011752{
11753#ifdef FEAT_FOLDING
11754 linenr_T lnum;
11755 char_u *text;
11756 char_u buf[51];
11757 foldinfo_T foldinfo;
11758 int fold_count;
11759#endif
11760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->v_type = VAR_STRING;
11762 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011763#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 /* treat illegal types and illegal string values for {lnum} the same */
11766 if (lnum < 0)
11767 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011768 fold_count = foldedCount(curwin, lnum, &foldinfo);
11769 if (fold_count > 0)
11770 {
11771 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11772 &foldinfo, buf);
11773 if (text == buf)
11774 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011775 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011776 }
11777#endif
11778}
11779
11780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 * "foreground()" function
11782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011785 typval_T *argvars UNUSED;
11786 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788#ifdef FEAT_GUI
11789 if (gui.in_use)
11790 gui_mch_set_foreground();
11791#else
11792# ifdef WIN32
11793 win32_set_foreground();
11794# endif
11795#endif
11796}
11797
11798/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011799 * "function()" function
11800 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011803 typval_T *argvars;
11804 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011805{
11806 char_u *s;
11807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011809 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011810 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011811 /* Don't check an autoload name for existence here. */
11812 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011813 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011814 else
11815 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011816 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011817 {
11818 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011819 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011820
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011821 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11822 * also be called from another script. Using trans_function_name()
11823 * would also work, but some plugins depend on the name being
11824 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011825 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011826 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011827 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011828 if (rettv->vval.v_string != NULL)
11829 {
11830 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011831 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011832 }
11833 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011834 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011835 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011837 }
11838}
11839
11840/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011841 * "garbagecollect()" function
11842 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011843 static void
11844f_garbagecollect(argvars, rettv)
11845 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011846 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011847{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011848 /* This is postponed until we are back at the toplevel, because we may be
11849 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11850 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011851
11852 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11853 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011854}
11855
11856/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011857 * "get()" function
11858 */
11859 static void
11860f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011861 typval_T *argvars;
11862 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011863{
Bram Moolenaar33570922005-01-25 22:26:29 +000011864 listitem_T *li;
11865 list_T *l;
11866 dictitem_T *di;
11867 dict_T *d;
11868 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011869
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011871 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011872 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011874 int error = FALSE;
11875
11876 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11877 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011878 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011879 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011880 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011881 else if (argvars[0].v_type == VAR_DICT)
11882 {
11883 if ((d = argvars[0].vval.v_dict) != NULL)
11884 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011885 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011886 if (di != NULL)
11887 tv = &di->di_tv;
11888 }
11889 }
11890 else
11891 EMSG2(_(e_listdictarg), "get()");
11892
11893 if (tv == NULL)
11894 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011895 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011896 copy_tv(&argvars[2], rettv);
11897 }
11898 else
11899 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011900}
11901
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011902static 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 +000011903
11904/*
11905 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011906 * Return a range (from start to end) of lines in rettv from the specified
11907 * buffer.
11908 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011909 */
11910 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011911get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011912 buf_T *buf;
11913 linenr_T start;
11914 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011915 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011916 typval_T *rettv;
11917{
11918 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011919
Bram Moolenaar959a1432013-12-14 12:17:38 +010011920 rettv->v_type = VAR_STRING;
11921 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011922 if (retlist && rettv_list_alloc(rettv) == FAIL)
11923 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011924
11925 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11926 return;
11927
11928 if (!retlist)
11929 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011930 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11931 p = ml_get_buf(buf, start, FALSE);
11932 else
11933 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011934 rettv->vval.v_string = vim_strsave(p);
11935 }
11936 else
11937 {
11938 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011939 return;
11940
11941 if (start < 1)
11942 start = 1;
11943 if (end > buf->b_ml.ml_line_count)
11944 end = buf->b_ml.ml_line_count;
11945 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011946 if (list_append_string(rettv->vval.v_list,
11947 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011948 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011949 }
11950}
11951
11952/*
11953 * "getbufline()" function
11954 */
11955 static void
11956f_getbufline(argvars, rettv)
11957 typval_T *argvars;
11958 typval_T *rettv;
11959{
11960 linenr_T lnum;
11961 linenr_T end;
11962 buf_T *buf;
11963
11964 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11965 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011966 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011967 --emsg_off;
11968
Bram Moolenaar661b1822005-07-28 22:36:45 +000011969 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011970 if (argvars[2].v_type == VAR_UNKNOWN)
11971 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011972 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011973 end = get_tv_lnum_buf(&argvars[2], buf);
11974
Bram Moolenaar342337a2005-07-21 21:11:17 +000011975 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011976}
11977
Bram Moolenaar0d660222005-01-07 21:51:51 +000011978/*
11979 * "getbufvar()" function
11980 */
11981 static void
11982f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011983 typval_T *argvars;
11984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011985{
11986 buf_T *buf;
11987 buf_T *save_curbuf;
11988 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011989 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011990 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011992 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11993 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011994 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011995 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011996
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011997 rettv->v_type = VAR_STRING;
11998 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011999
12000 if (buf != NULL && varname != NULL)
12001 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012002 /* set curbuf to be our buf, temporarily */
12003 save_curbuf = curbuf;
12004 curbuf = buf;
12005
Bram Moolenaar0d660222005-01-07 21:51:51 +000012006 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012007 {
12008 if (get_option_tv(&varname, rettv, TRUE) == OK)
12009 done = TRUE;
12010 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012011 else if (STRCMP(varname, "changedtick") == 0)
12012 {
12013 rettv->v_type = VAR_NUMBER;
12014 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012015 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012016 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012017 else
12018 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012019 /* Look up the variable. */
12020 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12021 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12022 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012023 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012024 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012025 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012026 done = TRUE;
12027 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012028 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012029
12030 /* restore previous notion of curbuf */
12031 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012032 }
12033
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012034 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12035 /* use the default value */
12036 copy_tv(&argvars[2], rettv);
12037
Bram Moolenaar0d660222005-01-07 21:51:51 +000012038 --emsg_off;
12039}
12040
12041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 * "getchar()" function
12043 */
12044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012045f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012046 typval_T *argvars;
12047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048{
12049 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012050 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012052 /* Position the cursor. Needed after a message that ends in a space. */
12053 windgoto(msg_row, msg_col);
12054
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 ++no_mapping;
12056 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012057 for (;;)
12058 {
12059 if (argvars[0].v_type == VAR_UNKNOWN)
12060 /* getchar(): blocking wait. */
12061 n = safe_vgetc();
12062 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12063 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012064 n = vpeekc_any();
12065 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012066 /* illegal argument or getchar(0) and no char avail: return zero */
12067 n = 0;
12068 else
12069 /* getchar(0) and char avail: return char */
12070 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012071
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012072 if (n == K_IGNORE)
12073 continue;
12074 break;
12075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 --no_mapping;
12077 --allow_keys;
12078
Bram Moolenaar219b8702006-11-01 14:32:36 +000012079 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12080 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12081 vimvars[VV_MOUSE_COL].vv_nr = 0;
12082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 if (IS_SPECIAL(n) || mod_mask != 0)
12085 {
12086 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12087 int i = 0;
12088
12089 /* Turn a special key into three bytes, plus modifier. */
12090 if (mod_mask != 0)
12091 {
12092 temp[i++] = K_SPECIAL;
12093 temp[i++] = KS_MODIFIER;
12094 temp[i++] = mod_mask;
12095 }
12096 if (IS_SPECIAL(n))
12097 {
12098 temp[i++] = K_SPECIAL;
12099 temp[i++] = K_SECOND(n);
12100 temp[i++] = K_THIRD(n);
12101 }
12102#ifdef FEAT_MBYTE
12103 else if (has_mbyte)
12104 i += (*mb_char2bytes)(n, temp + i);
12105#endif
12106 else
12107 temp[i++] = n;
12108 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012109 rettv->v_type = VAR_STRING;
12110 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012111
12112#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012113 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012114 {
12115 int row = mouse_row;
12116 int col = mouse_col;
12117 win_T *win;
12118 linenr_T lnum;
12119# ifdef FEAT_WINDOWS
12120 win_T *wp;
12121# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012122 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012123
12124 if (row >= 0 && col >= 0)
12125 {
12126 /* Find the window at the mouse coordinates and compute the
12127 * text position. */
12128 win = mouse_find_win(&row, &col);
12129 (void)mouse_comp_pos(win, &row, &col, &lnum);
12130# ifdef FEAT_WINDOWS
12131 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012132 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012133# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012134 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012135 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12136 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12137 }
12138 }
12139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140 }
12141}
12142
12143/*
12144 * "getcharmod()" function
12145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012147f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012148 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012149 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012151 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152}
12153
12154/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012155 * "getcharsearch()" function
12156 */
12157 static void
12158f_getcharsearch(argvars, rettv)
12159 typval_T *argvars UNUSED;
12160 typval_T *rettv;
12161{
12162 if (rettv_dict_alloc(rettv) != FAIL)
12163 {
12164 dict_T *dict = rettv->vval.v_dict;
12165
12166 dict_add_nr_str(dict, "char", 0L, last_csearch());
12167 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12168 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12169 }
12170}
12171
12172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012173 * "getcmdline()" function
12174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012176f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012177 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->v_type = VAR_STRING;
12181 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182}
12183
12184/*
12185 * "getcmdpos()" function
12186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012188f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012189 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193}
12194
12195/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012196 * "getcmdtype()" function
12197 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012198 static void
12199f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012200 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012201 typval_T *rettv;
12202{
12203 rettv->v_type = VAR_STRING;
12204 rettv->vval.v_string = alloc(2);
12205 if (rettv->vval.v_string != NULL)
12206 {
12207 rettv->vval.v_string[0] = get_cmdline_type();
12208 rettv->vval.v_string[1] = NUL;
12209 }
12210}
12211
12212/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012213 * "getcmdwintype()" function
12214 */
12215 static void
12216f_getcmdwintype(argvars, rettv)
12217 typval_T *argvars UNUSED;
12218 typval_T *rettv;
12219{
12220 rettv->v_type = VAR_STRING;
12221 rettv->vval.v_string = NULL;
12222#ifdef FEAT_CMDWIN
12223 rettv->vval.v_string = alloc(2);
12224 if (rettv->vval.v_string != NULL)
12225 {
12226 rettv->vval.v_string[0] = cmdwin_type;
12227 rettv->vval.v_string[1] = NUL;
12228 }
12229#endif
12230}
12231
12232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 * "getcwd()" function
12234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012236f_getcwd(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010012237 typval_T *argvars;
Bram Moolenaar33570922005-01-25 22:26:29 +000012238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012240 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012241 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012244 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012245
12246 wp = find_tabwin(&argvars[0], &argvars[1]);
12247 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012249 if (wp->w_localdir != NULL)
12250 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12251 else if(globaldir != NULL)
12252 rettv->vval.v_string = vim_strsave(globaldir);
12253 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012254 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012255 cwd = alloc(MAXPATHL);
12256 if (cwd != NULL)
12257 {
12258 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12259 rettv->vval.v_string = vim_strsave(cwd);
12260 vim_free(cwd);
12261 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012262 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012263#ifdef BACKSLASH_IN_FILENAME
12264 if (rettv->vval.v_string != NULL)
12265 slash_adjust(rettv->vval.v_string);
12266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 }
12268}
12269
12270/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012271 * "getfontname()" function
12272 */
12273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012274f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012275 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012276 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012277{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 rettv->v_type = VAR_STRING;
12279 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012280#ifdef FEAT_GUI
12281 if (gui.in_use)
12282 {
12283 GuiFont font;
12284 char_u *name = NULL;
12285
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012286 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012287 {
12288 /* Get the "Normal" font. Either the name saved by
12289 * hl_set_font_name() or from the font ID. */
12290 font = gui.norm_font;
12291 name = hl_get_font_name();
12292 }
12293 else
12294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012295 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012296 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12297 return;
12298 font = gui_mch_get_font(name, FALSE);
12299 if (font == NOFONT)
12300 return; /* Invalid font name, return empty string. */
12301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012302 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012303 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012304 gui_mch_free_font(font);
12305 }
12306#endif
12307}
12308
12309/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012310 * "getfperm({fname})" function
12311 */
12312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012313f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012314 typval_T *argvars;
12315 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012316{
12317 char_u *fname;
12318 struct stat st;
12319 char_u *perm = NULL;
12320 char_u flags[] = "rwx";
12321 int i;
12322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012325 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012326 if (mch_stat((char *)fname, &st) >= 0)
12327 {
12328 perm = vim_strsave((char_u *)"---------");
12329 if (perm != NULL)
12330 {
12331 for (i = 0; i < 9; i++)
12332 {
12333 if (st.st_mode & (1 << (8 - i)))
12334 perm[i] = flags[i % 3];
12335 }
12336 }
12337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012339}
12340
12341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 * "getfsize({fname})" function
12343 */
12344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012345f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012346 typval_T *argvars;
12347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348{
12349 char_u *fname;
12350 struct stat st;
12351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012352 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355
12356 if (mch_stat((char *)fname, &st) >= 0)
12357 {
12358 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012359 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012362 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012363
12364 /* non-perfect check for overflow */
12365 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12366 rettv->vval.v_number = -2;
12367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 }
12369 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012370 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012371}
12372
12373/*
12374 * "getftime({fname})" function
12375 */
12376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012377f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012378 typval_T *argvars;
12379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380{
12381 char_u *fname;
12382 struct stat st;
12383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385
12386 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012387 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012388 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390}
12391
12392/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012393 * "getftype({fname})" function
12394 */
12395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012397 typval_T *argvars;
12398 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012399{
12400 char_u *fname;
12401 struct stat st;
12402 char_u *type = NULL;
12403 char *t;
12404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012405 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012407 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012408 if (mch_lstat((char *)fname, &st) >= 0)
12409 {
12410#ifdef S_ISREG
12411 if (S_ISREG(st.st_mode))
12412 t = "file";
12413 else if (S_ISDIR(st.st_mode))
12414 t = "dir";
12415# ifdef S_ISLNK
12416 else if (S_ISLNK(st.st_mode))
12417 t = "link";
12418# endif
12419# ifdef S_ISBLK
12420 else if (S_ISBLK(st.st_mode))
12421 t = "bdev";
12422# endif
12423# ifdef S_ISCHR
12424 else if (S_ISCHR(st.st_mode))
12425 t = "cdev";
12426# endif
12427# ifdef S_ISFIFO
12428 else if (S_ISFIFO(st.st_mode))
12429 t = "fifo";
12430# endif
12431# ifdef S_ISSOCK
12432 else if (S_ISSOCK(st.st_mode))
12433 t = "fifo";
12434# endif
12435 else
12436 t = "other";
12437#else
12438# ifdef S_IFMT
12439 switch (st.st_mode & S_IFMT)
12440 {
12441 case S_IFREG: t = "file"; break;
12442 case S_IFDIR: t = "dir"; break;
12443# ifdef S_IFLNK
12444 case S_IFLNK: t = "link"; break;
12445# endif
12446# ifdef S_IFBLK
12447 case S_IFBLK: t = "bdev"; break;
12448# endif
12449# ifdef S_IFCHR
12450 case S_IFCHR: t = "cdev"; break;
12451# endif
12452# ifdef S_IFIFO
12453 case S_IFIFO: t = "fifo"; break;
12454# endif
12455# ifdef S_IFSOCK
12456 case S_IFSOCK: t = "socket"; break;
12457# endif
12458 default: t = "other";
12459 }
12460# else
12461 if (mch_isdir(fname))
12462 t = "dir";
12463 else
12464 t = "file";
12465# endif
12466#endif
12467 type = vim_strsave((char_u *)t);
12468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012469 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012470}
12471
12472/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012473 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012474 */
12475 static void
12476f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012477 typval_T *argvars;
12478 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012479{
12480 linenr_T lnum;
12481 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012482 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012483
12484 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012485 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012486 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012487 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012488 retlist = FALSE;
12489 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012490 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012491 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012492 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012493 retlist = TRUE;
12494 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012495
Bram Moolenaar342337a2005-07-21 21:11:17 +000012496 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012497}
12498
12499/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012500 * "getmatches()" function
12501 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012502 static void
12503f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012504 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010012505 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012506{
12507#ifdef FEAT_SEARCH_EXTRA
12508 dict_T *dict;
12509 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012510 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012511
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012512 if (rettv_list_alloc(rettv) == OK)
12513 {
12514 while (cur != NULL)
12515 {
12516 dict = dict_alloc();
12517 if (dict == NULL)
12518 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012519 if (cur->match.regprog == NULL)
12520 {
12521 /* match added with matchaddpos() */
12522 for (i = 0; i < MAXPOSMATCH; ++i)
12523 {
12524 llpos_T *llpos;
12525 char buf[6];
12526 list_T *l;
12527
12528 llpos = &cur->pos.pos[i];
12529 if (llpos->lnum == 0)
12530 break;
12531 l = list_alloc();
12532 if (l == NULL)
12533 break;
12534 list_append_number(l, (varnumber_T)llpos->lnum);
12535 if (llpos->col > 0)
12536 {
12537 list_append_number(l, (varnumber_T)llpos->col);
12538 list_append_number(l, (varnumber_T)llpos->len);
12539 }
12540 sprintf(buf, "pos%d", i + 1);
12541 dict_add_list(dict, buf, l);
12542 }
12543 }
12544 else
12545 {
12546 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12547 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012548 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012549 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12550 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012551# ifdef FEAT_CONCEAL
12552 if (cur->conceal_char)
12553 {
12554 char_u buf[MB_MAXBYTES + 1];
12555
12556 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12557 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12558 }
12559# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012560 list_append_dict(rettv->vval.v_list, dict);
12561 cur = cur->next;
12562 }
12563 }
12564#endif
12565}
12566
12567/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012568 * "getpid()" function
12569 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012570 static void
12571f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012572 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000012573 typval_T *rettv;
12574{
12575 rettv->vval.v_number = mch_get_pid();
12576}
12577
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012578static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012579
12580/*
12581 * "getcurpos()" function
12582 */
12583 static void
12584f_getcurpos(argvars, rettv)
12585 typval_T *argvars;
12586 typval_T *rettv;
12587{
12588 getpos_both(argvars, rettv, TRUE);
12589}
12590
Bram Moolenaar18081e32008-02-20 19:11:07 +000012591/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012592 * "getpos(string)" function
12593 */
12594 static void
12595f_getpos(argvars, rettv)
12596 typval_T *argvars;
12597 typval_T *rettv;
12598{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012599 getpos_both(argvars, rettv, FALSE);
12600}
12601
12602 static void
12603getpos_both(argvars, rettv, getcurpos)
12604 typval_T *argvars;
12605 typval_T *rettv;
12606 int getcurpos;
12607{
Bram Moolenaara5525202006-03-02 22:52:09 +000012608 pos_T *fp;
12609 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012610 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012611
12612 if (rettv_list_alloc(rettv) == OK)
12613 {
12614 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012615 if (getcurpos)
12616 fp = &curwin->w_cursor;
12617 else
12618 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012619 if (fnum != -1)
12620 list_append_number(l, (varnumber_T)fnum);
12621 else
12622 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012623 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12624 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012625 list_append_number(l, (fp != NULL)
12626 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012627 : (varnumber_T)0);
12628 list_append_number(l,
12629#ifdef FEAT_VIRTUALEDIT
12630 (fp != NULL) ? (varnumber_T)fp->coladd :
12631#endif
12632 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012633 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010012634 list_append_number(l, curwin->w_curswant == MAXCOL ?
12635 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000012636 }
12637 else
12638 rettv->vval.v_number = FALSE;
12639}
12640
12641/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012642 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012643 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012644 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000012645f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012646 typval_T *argvars UNUSED;
12647 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012648{
12649#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012650 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012651#endif
12652
Bram Moolenaar2641f772005-03-25 21:58:17 +000012653#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012654 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012655 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012656 wp = NULL;
12657 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12658 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012659 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012660 if (wp == NULL)
12661 return;
12662 }
12663
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012664 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012665 }
12666#endif
12667}
12668
12669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670 * "getreg()" function
12671 */
12672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012674 typval_T *argvars;
12675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676{
12677 char_u *strregname;
12678 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012679 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012680 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012681 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012682
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012683 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012685 strregname = get_tv_string_chk(&argvars[0]);
12686 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012687 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012688 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012690 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12691 return_list = get_tv_number_chk(&argvars[2], &error);
12692 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012695 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012696
12697 if (error)
12698 return;
12699
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700 regname = (strregname == NULL ? '"' : *strregname);
12701 if (regname == 0)
12702 regname = '"';
12703
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012704 if (return_list)
12705 {
12706 rettv->v_type = VAR_LIST;
12707 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12708 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012709 if (rettv->vval.v_list != NULL)
12710 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012711 }
12712 else
12713 {
12714 rettv->v_type = VAR_STRING;
12715 rettv->vval.v_string = get_reg_contents(regname,
12716 arg2 ? GREG_EXPR_SRC : 0);
12717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718}
12719
12720/*
12721 * "getregtype()" function
12722 */
12723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012725 typval_T *argvars;
12726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727{
12728 char_u *strregname;
12729 int regname;
12730 char_u buf[NUMBUFLEN + 2];
12731 long reglen = 0;
12732
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012733 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012734 {
12735 strregname = get_tv_string_chk(&argvars[0]);
12736 if (strregname == NULL) /* type error; errmsg already given */
12737 {
12738 rettv->v_type = VAR_STRING;
12739 rettv->vval.v_string = NULL;
12740 return;
12741 }
12742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 else
12744 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012745 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746
12747 regname = (strregname == NULL ? '"' : *strregname);
12748 if (regname == 0)
12749 regname = '"';
12750
12751 buf[0] = NUL;
12752 buf[1] = NUL;
12753 switch (get_reg_type(regname, &reglen))
12754 {
12755 case MLINE: buf[0] = 'V'; break;
12756 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 case MBLOCK:
12758 buf[0] = Ctrl_V;
12759 sprintf((char *)buf + 1, "%ld", reglen + 1);
12760 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 rettv->v_type = VAR_STRING;
12763 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764}
12765
12766/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012767 * "gettabvar()" function
12768 */
12769 static void
12770f_gettabvar(argvars, rettv)
12771 typval_T *argvars;
12772 typval_T *rettv;
12773{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012774 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012775 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012776 dictitem_T *v;
12777 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012778 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012779
12780 rettv->v_type = VAR_STRING;
12781 rettv->vval.v_string = NULL;
12782
12783 varname = get_tv_string_chk(&argvars[1]);
12784 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12785 if (tp != NULL && varname != NULL)
12786 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012787 /* Set tp to be our tabpage, temporarily. Also set the window to the
12788 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012789 if (switch_win(&oldcurwin, &oldtabpage,
12790 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012791 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012792 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012793 /* look up the variable */
12794 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12795 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12796 if (v != NULL)
12797 {
12798 copy_tv(&v->di_tv, rettv);
12799 done = TRUE;
12800 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012801 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012802
12803 /* restore previous notion of curwin */
12804 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012805 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012806
12807 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012808 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012809}
12810
12811/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012812 * "gettabwinvar()" function
12813 */
12814 static void
12815f_gettabwinvar(argvars, rettv)
12816 typval_T *argvars;
12817 typval_T *rettv;
12818{
12819 getwinvar(argvars, rettv, 1);
12820}
12821
12822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 * "getwinposx()" function
12824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012826f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012827 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831#ifdef FEAT_GUI
12832 if (gui.in_use)
12833 {
12834 int x, y;
12835
12836 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 }
12839#endif
12840}
12841
12842/*
12843 * "getwinposy()" function
12844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012847 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851#ifdef FEAT_GUI
12852 if (gui.in_use)
12853 {
12854 int x, y;
12855
12856 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012857 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 }
12859#endif
12860}
12861
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012862/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012863 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012864 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012865 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012866find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012867 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012868 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012869{
12870#ifdef FEAT_WINDOWS
12871 win_T *wp;
12872#endif
12873 int nr;
12874
12875 nr = get_tv_number_chk(vp, NULL);
12876
12877#ifdef FEAT_WINDOWS
12878 if (nr < 0)
12879 return NULL;
12880 if (nr == 0)
12881 return curwin;
12882
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012883 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12884 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012885 if (--nr <= 0)
12886 break;
12887 return wp;
12888#else
12889 if (nr == 0 || nr == 1)
12890 return curwin;
12891 return NULL;
12892#endif
12893}
12894
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012896 * Find window specified by "wvp" in tabpage "tvp".
12897 */
12898 static win_T *
12899find_tabwin(wvp, tvp)
12900 typval_T *wvp; /* VAR_UNKNOWN for current window */
12901 typval_T *tvp; /* VAR_UNKNOWN for current tab page */
12902{
12903 win_T *wp = NULL;
12904 tabpage_T *tp = NULL;
12905 long n;
12906
12907 if (wvp->v_type != VAR_UNKNOWN)
12908 {
12909 if (tvp->v_type != VAR_UNKNOWN)
12910 {
12911 n = get_tv_number(tvp);
12912 if (n >= 0)
12913 tp = find_tabpage(n);
12914 }
12915 else
12916 tp = curtab;
12917
12918 if (tp != NULL)
12919 wp = find_win_by_nr(wvp, tp);
12920 }
12921 else
12922 wp = curwin;
12923
12924 return wp;
12925}
12926
12927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928 * "getwinvar()" function
12929 */
12930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012932 typval_T *argvars;
12933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012935 getwinvar(argvars, rettv, 0);
12936}
12937
12938/*
12939 * getwinvar() and gettabwinvar()
12940 */
12941 static void
12942getwinvar(argvars, rettv, off)
12943 typval_T *argvars;
12944 typval_T *rettv;
12945 int off; /* 1 for gettabwinvar() */
12946{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012947 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012949 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012950 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012951 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012952#ifdef FEAT_WINDOWS
12953 win_T *oldcurwin;
12954 tabpage_T *oldtabpage;
12955 int need_switch_win;
12956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012958#ifdef FEAT_WINDOWS
12959 if (off == 1)
12960 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12961 else
12962 tp = curtab;
12963#endif
12964 win = find_win_by_nr(&argvars[off], tp);
12965 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012966 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012967
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012968 rettv->v_type = VAR_STRING;
12969 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970
12971 if (win != NULL && varname != NULL)
12972 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012973#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012974 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012975 * otherwise the window is not valid. Only do this when needed,
12976 * autocommands get blocked. */
12977 need_switch_win = !(tp == curtab && win == curwin);
12978 if (!need_switch_win
12979 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12980#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012981 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012982 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012983 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012984 if (get_option_tv(&varname, rettv, 1) == OK)
12985 done = TRUE;
12986 }
12987 else
12988 {
12989 /* Look up the variable. */
12990 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12991 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12992 varname, FALSE);
12993 if (v != NULL)
12994 {
12995 copy_tv(&v->di_tv, rettv);
12996 done = TRUE;
12997 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013000
Bram Moolenaarba117c22015-09-29 16:53:22 +020013001#ifdef FEAT_WINDOWS
13002 if (need_switch_win)
13003 /* restore previous notion of curwin */
13004 restore_win(oldcurwin, oldtabpage, TRUE);
13005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006 }
13007
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013008 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13009 /* use the default return value */
13010 copy_tv(&argvars[off + 2], rettv);
13011
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012 --emsg_off;
13013}
13014
13015/*
13016 * "glob()" function
13017 */
13018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013019f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013020 typval_T *argvars;
13021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013023 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013025 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013027 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013028 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013029 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013030 if (argvars[1].v_type != VAR_UNKNOWN)
13031 {
13032 if (get_tv_number_chk(&argvars[1], &error))
13033 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013034 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013035 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013036 if (get_tv_number_chk(&argvars[2], &error))
13037 {
13038 rettv->v_type = VAR_LIST;
13039 rettv->vval.v_list = NULL;
13040 }
13041 if (argvars[3].v_type != VAR_UNKNOWN
13042 && get_tv_number_chk(&argvars[3], &error))
13043 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013044 }
13045 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013046 if (!error)
13047 {
13048 ExpandInit(&xpc);
13049 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013050 if (p_wic)
13051 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013052 if (rettv->v_type == VAR_STRING)
13053 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013054 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013055 else if (rettv_list_alloc(rettv) != FAIL)
13056 {
13057 int i;
13058
13059 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13060 NULL, options, WILD_ALL_KEEP);
13061 for (i = 0; i < xpc.xp_numfiles; i++)
13062 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13063
13064 ExpandCleanup(&xpc);
13065 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013066 }
13067 else
13068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013069}
13070
13071/*
13072 * "globpath()" function
13073 */
13074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013075f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013076 typval_T *argvars;
13077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013079 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013081 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013082 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013083 garray_T ga;
13084 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013086 /* When the optional second argument is non-zero, don't remove matches
13087 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013088 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013089 if (argvars[2].v_type != VAR_UNKNOWN)
13090 {
13091 if (get_tv_number_chk(&argvars[2], &error))
13092 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013093 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013094 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013095 if (get_tv_number_chk(&argvars[3], &error))
13096 {
13097 rettv->v_type = VAR_LIST;
13098 rettv->vval.v_list = NULL;
13099 }
13100 if (argvars[4].v_type != VAR_UNKNOWN
13101 && get_tv_number_chk(&argvars[4], &error))
13102 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013103 }
13104 }
13105 if (file != NULL && !error)
13106 {
13107 ga_init2(&ga, (int)sizeof(char_u *), 10);
13108 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13109 if (rettv->v_type == VAR_STRING)
13110 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13111 else if (rettv_list_alloc(rettv) != FAIL)
13112 for (i = 0; i < ga.ga_len; ++i)
13113 list_append_string(rettv->vval.v_list,
13114 ((char_u **)(ga.ga_data))[i], -1);
13115 ga_clear_strings(&ga);
13116 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013117 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013118 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119}
13120
13121/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013122 * "glob2regpat()" function
13123 */
13124 static void
13125f_glob2regpat(argvars, rettv)
13126 typval_T *argvars;
13127 typval_T *rettv;
13128{
13129 char_u *pat = get_tv_string_chk(&argvars[0]);
13130
13131 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013132 rettv->vval.v_string = (pat == NULL)
13133 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013134}
13135
13136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 * "has()" function
13138 */
13139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013141 typval_T *argvars;
13142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143{
13144 int i;
13145 char_u *name;
13146 int n = FALSE;
13147 static char *(has_list[]) =
13148 {
13149#ifdef AMIGA
13150 "amiga",
13151# ifdef FEAT_ARP
13152 "arp",
13153# endif
13154#endif
13155#ifdef __BEOS__
13156 "beos",
13157#endif
13158#ifdef MSDOS
13159# ifdef DJGPP
13160 "dos32",
13161# else
13162 "dos16",
13163# endif
13164#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013165#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 "mac",
13167#endif
13168#if defined(MACOS_X_UNIX)
13169 "macunix",
13170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171#ifdef __QNX__
13172 "qnx",
13173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174#ifdef UNIX
13175 "unix",
13176#endif
13177#ifdef VMS
13178 "vms",
13179#endif
13180#ifdef WIN16
13181 "win16",
13182#endif
13183#ifdef WIN32
13184 "win32",
13185#endif
13186#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13187 "win32unix",
13188#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013189#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 "win64",
13191#endif
13192#ifdef EBCDIC
13193 "ebcdic",
13194#endif
13195#ifndef CASE_INSENSITIVE_FILENAME
13196 "fname_case",
13197#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013198#ifdef HAVE_ACL
13199 "acl",
13200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201#ifdef FEAT_ARABIC
13202 "arabic",
13203#endif
13204#ifdef FEAT_AUTOCMD
13205 "autocmd",
13206#endif
13207#ifdef FEAT_BEVAL
13208 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013209# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13210 "balloon_multiline",
13211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212#endif
13213#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13214 "builtin_terms",
13215# ifdef ALL_BUILTIN_TCAPS
13216 "all_builtin_terms",
13217# endif
13218#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013219#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13220 || defined(FEAT_GUI_W32) \
13221 || defined(FEAT_GUI_MOTIF))
13222 "browsefilter",
13223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224#ifdef FEAT_BYTEOFF
13225 "byte_offset",
13226#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013227#ifdef FEAT_CHANNEL
13228 "channel",
13229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230#ifdef FEAT_CINDENT
13231 "cindent",
13232#endif
13233#ifdef FEAT_CLIENTSERVER
13234 "clientserver",
13235#endif
13236#ifdef FEAT_CLIPBOARD
13237 "clipboard",
13238#endif
13239#ifdef FEAT_CMDL_COMPL
13240 "cmdline_compl",
13241#endif
13242#ifdef FEAT_CMDHIST
13243 "cmdline_hist",
13244#endif
13245#ifdef FEAT_COMMENTS
13246 "comments",
13247#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013248#ifdef FEAT_CONCEAL
13249 "conceal",
13250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251#ifdef FEAT_CRYPT
13252 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013253 "crypt-blowfish",
13254 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255#endif
13256#ifdef FEAT_CSCOPE
13257 "cscope",
13258#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013259#ifdef FEAT_CURSORBIND
13260 "cursorbind",
13261#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013262#ifdef CURSOR_SHAPE
13263 "cursorshape",
13264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265#ifdef DEBUG
13266 "debug",
13267#endif
13268#ifdef FEAT_CON_DIALOG
13269 "dialog_con",
13270#endif
13271#ifdef FEAT_GUI_DIALOG
13272 "dialog_gui",
13273#endif
13274#ifdef FEAT_DIFF
13275 "diff",
13276#endif
13277#ifdef FEAT_DIGRAPHS
13278 "digraphs",
13279#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013280#ifdef FEAT_DIRECTX
13281 "directx",
13282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283#ifdef FEAT_DND
13284 "dnd",
13285#endif
13286#ifdef FEAT_EMACS_TAGS
13287 "emacs_tags",
13288#endif
13289 "eval", /* always present, of course! */
13290#ifdef FEAT_EX_EXTRA
13291 "ex_extra",
13292#endif
13293#ifdef FEAT_SEARCH_EXTRA
13294 "extra_search",
13295#endif
13296#ifdef FEAT_FKMAP
13297 "farsi",
13298#endif
13299#ifdef FEAT_SEARCHPATH
13300 "file_in_path",
13301#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013302#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013303 "filterpipe",
13304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305#ifdef FEAT_FIND_ID
13306 "find_in_path",
13307#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013308#ifdef FEAT_FLOAT
13309 "float",
13310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311#ifdef FEAT_FOLDING
13312 "folding",
13313#endif
13314#ifdef FEAT_FOOTER
13315 "footer",
13316#endif
13317#if !defined(USE_SYSTEM) && defined(UNIX)
13318 "fork",
13319#endif
13320#ifdef FEAT_GETTEXT
13321 "gettext",
13322#endif
13323#ifdef FEAT_GUI
13324 "gui",
13325#endif
13326#ifdef FEAT_GUI_ATHENA
13327# ifdef FEAT_GUI_NEXTAW
13328 "gui_neXtaw",
13329# else
13330 "gui_athena",
13331# endif
13332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333#ifdef FEAT_GUI_GTK
13334 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013337#ifdef FEAT_GUI_GNOME
13338 "gui_gnome",
13339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340#ifdef FEAT_GUI_MAC
13341 "gui_mac",
13342#endif
13343#ifdef FEAT_GUI_MOTIF
13344 "gui_motif",
13345#endif
13346#ifdef FEAT_GUI_PHOTON
13347 "gui_photon",
13348#endif
13349#ifdef FEAT_GUI_W16
13350 "gui_win16",
13351#endif
13352#ifdef FEAT_GUI_W32
13353 "gui_win32",
13354#endif
13355#ifdef FEAT_HANGULIN
13356 "hangul_input",
13357#endif
13358#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13359 "iconv",
13360#endif
13361#ifdef FEAT_INS_EXPAND
13362 "insert_expand",
13363#endif
13364#ifdef FEAT_JUMPLIST
13365 "jumplist",
13366#endif
13367#ifdef FEAT_KEYMAP
13368 "keymap",
13369#endif
13370#ifdef FEAT_LANGMAP
13371 "langmap",
13372#endif
13373#ifdef FEAT_LIBCALL
13374 "libcall",
13375#endif
13376#ifdef FEAT_LINEBREAK
13377 "linebreak",
13378#endif
13379#ifdef FEAT_LISP
13380 "lispindent",
13381#endif
13382#ifdef FEAT_LISTCMDS
13383 "listcmds",
13384#endif
13385#ifdef FEAT_LOCALMAP
13386 "localmap",
13387#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013388#ifdef FEAT_LUA
13389# ifndef DYNAMIC_LUA
13390 "lua",
13391# endif
13392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393#ifdef FEAT_MENU
13394 "menu",
13395#endif
13396#ifdef FEAT_SESSION
13397 "mksession",
13398#endif
13399#ifdef FEAT_MODIFY_FNAME
13400 "modify_fname",
13401#endif
13402#ifdef FEAT_MOUSE
13403 "mouse",
13404#endif
13405#ifdef FEAT_MOUSESHAPE
13406 "mouseshape",
13407#endif
13408#if defined(UNIX) || defined(VMS)
13409# ifdef FEAT_MOUSE_DEC
13410 "mouse_dec",
13411# endif
13412# ifdef FEAT_MOUSE_GPM
13413 "mouse_gpm",
13414# endif
13415# ifdef FEAT_MOUSE_JSB
13416 "mouse_jsbterm",
13417# endif
13418# ifdef FEAT_MOUSE_NET
13419 "mouse_netterm",
13420# endif
13421# ifdef FEAT_MOUSE_PTERM
13422 "mouse_pterm",
13423# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013424# ifdef FEAT_MOUSE_SGR
13425 "mouse_sgr",
13426# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013427# ifdef FEAT_SYSMOUSE
13428 "mouse_sysmouse",
13429# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013430# ifdef FEAT_MOUSE_URXVT
13431 "mouse_urxvt",
13432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433# ifdef FEAT_MOUSE_XTERM
13434 "mouse_xterm",
13435# endif
13436#endif
13437#ifdef FEAT_MBYTE
13438 "multi_byte",
13439#endif
13440#ifdef FEAT_MBYTE_IME
13441 "multi_byte_ime",
13442#endif
13443#ifdef FEAT_MULTI_LANG
13444 "multi_lang",
13445#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013446#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013447#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013448 "mzscheme",
13449#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451#ifdef FEAT_OLE
13452 "ole",
13453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454#ifdef FEAT_PATH_EXTRA
13455 "path_extra",
13456#endif
13457#ifdef FEAT_PERL
13458#ifndef DYNAMIC_PERL
13459 "perl",
13460#endif
13461#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013462#ifdef FEAT_PERSISTENT_UNDO
13463 "persistent_undo",
13464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465#ifdef FEAT_PYTHON
13466#ifndef DYNAMIC_PYTHON
13467 "python",
13468#endif
13469#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013470#ifdef FEAT_PYTHON3
13471#ifndef DYNAMIC_PYTHON3
13472 "python3",
13473#endif
13474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475#ifdef FEAT_POSTSCRIPT
13476 "postscript",
13477#endif
13478#ifdef FEAT_PRINTER
13479 "printer",
13480#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013481#ifdef FEAT_PROFILE
13482 "profile",
13483#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013484#ifdef FEAT_RELTIME
13485 "reltime",
13486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487#ifdef FEAT_QUICKFIX
13488 "quickfix",
13489#endif
13490#ifdef FEAT_RIGHTLEFT
13491 "rightleft",
13492#endif
13493#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13494 "ruby",
13495#endif
13496#ifdef FEAT_SCROLLBIND
13497 "scrollbind",
13498#endif
13499#ifdef FEAT_CMDL_INFO
13500 "showcmd",
13501 "cmdline_info",
13502#endif
13503#ifdef FEAT_SIGNS
13504 "signs",
13505#endif
13506#ifdef FEAT_SMARTINDENT
13507 "smartindent",
13508#endif
13509#ifdef FEAT_SNIFF
13510 "sniff",
13511#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013512#ifdef STARTUPTIME
13513 "startuptime",
13514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515#ifdef FEAT_STL_OPT
13516 "statusline",
13517#endif
13518#ifdef FEAT_SUN_WORKSHOP
13519 "sun_workshop",
13520#endif
13521#ifdef FEAT_NETBEANS_INTG
13522 "netbeans_intg",
13523#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013524#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013525 "spell",
13526#endif
13527#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 "syntax",
13529#endif
13530#if defined(USE_SYSTEM) || !defined(UNIX)
13531 "system",
13532#endif
13533#ifdef FEAT_TAG_BINS
13534 "tag_binary",
13535#endif
13536#ifdef FEAT_TAG_OLDSTATIC
13537 "tag_old_static",
13538#endif
13539#ifdef FEAT_TAG_ANYWHITE
13540 "tag_any_white",
13541#endif
13542#ifdef FEAT_TCL
13543# ifndef DYNAMIC_TCL
13544 "tcl",
13545# endif
13546#endif
13547#ifdef TERMINFO
13548 "terminfo",
13549#endif
13550#ifdef FEAT_TERMRESPONSE
13551 "termresponse",
13552#endif
13553#ifdef FEAT_TEXTOBJ
13554 "textobjects",
13555#endif
13556#ifdef HAVE_TGETENT
13557 "tgetent",
13558#endif
13559#ifdef FEAT_TITLE
13560 "title",
13561#endif
13562#ifdef FEAT_TOOLBAR
13563 "toolbar",
13564#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013565#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13566 "unnamedplus",
13567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568#ifdef FEAT_USR_CMDS
13569 "user-commands", /* was accidentally included in 5.4 */
13570 "user_commands",
13571#endif
13572#ifdef FEAT_VIMINFO
13573 "viminfo",
13574#endif
13575#ifdef FEAT_VERTSPLIT
13576 "vertsplit",
13577#endif
13578#ifdef FEAT_VIRTUALEDIT
13579 "virtualedit",
13580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582#ifdef FEAT_VISUALEXTRA
13583 "visualextra",
13584#endif
13585#ifdef FEAT_VREPLACE
13586 "vreplace",
13587#endif
13588#ifdef FEAT_WILDIGN
13589 "wildignore",
13590#endif
13591#ifdef FEAT_WILDMENU
13592 "wildmenu",
13593#endif
13594#ifdef FEAT_WINDOWS
13595 "windows",
13596#endif
13597#ifdef FEAT_WAK
13598 "winaltkeys",
13599#endif
13600#ifdef FEAT_WRITEBACKUP
13601 "writebackup",
13602#endif
13603#ifdef FEAT_XIM
13604 "xim",
13605#endif
13606#ifdef FEAT_XFONTSET
13607 "xfontset",
13608#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013609#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013610 "xpm",
13611 "xpm_w32", /* for backward compatibility */
13612#else
13613# if defined(HAVE_XPM)
13614 "xpm",
13615# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617#ifdef USE_XSMP
13618 "xsmp",
13619#endif
13620#ifdef USE_XSMP_INTERACT
13621 "xsmp_interact",
13622#endif
13623#ifdef FEAT_XCLIPBOARD
13624 "xterm_clipboard",
13625#endif
13626#ifdef FEAT_XTERM_SAVE
13627 "xterm_save",
13628#endif
13629#if defined(UNIX) && defined(FEAT_X11)
13630 "X11",
13631#endif
13632 NULL
13633 };
13634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636 for (i = 0; has_list[i] != NULL; ++i)
13637 if (STRICMP(name, has_list[i]) == 0)
13638 {
13639 n = TRUE;
13640 break;
13641 }
13642
13643 if (n == FALSE)
13644 {
13645 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013646 {
13647 if (name[5] == '-'
13648 && STRLEN(name) > 11
13649 && vim_isdigit(name[6])
13650 && vim_isdigit(name[8])
13651 && vim_isdigit(name[10]))
13652 {
13653 int major = atoi((char *)name + 6);
13654 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013655
13656 /* Expect "patch-9.9.01234". */
13657 n = (major < VIM_VERSION_MAJOR
13658 || (major == VIM_VERSION_MAJOR
13659 && (minor < VIM_VERSION_MINOR
13660 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013661 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013662 }
13663 else
13664 n = has_patch(atoi((char *)name + 5));
13665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 else if (STRICMP(name, "vim_starting") == 0)
13667 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013668#ifdef FEAT_MBYTE
13669 else if (STRICMP(name, "multi_byte_encoding") == 0)
13670 n = has_mbyte;
13671#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013672#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13673 else if (STRICMP(name, "balloon_multiline") == 0)
13674 n = multiline_balloon_available();
13675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676#ifdef DYNAMIC_TCL
13677 else if (STRICMP(name, "tcl") == 0)
13678 n = tcl_enabled(FALSE);
13679#endif
13680#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13681 else if (STRICMP(name, "iconv") == 0)
13682 n = iconv_enabled(FALSE);
13683#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013684#ifdef DYNAMIC_LUA
13685 else if (STRICMP(name, "lua") == 0)
13686 n = lua_enabled(FALSE);
13687#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013688#ifdef DYNAMIC_MZSCHEME
13689 else if (STRICMP(name, "mzscheme") == 0)
13690 n = mzscheme_enabled(FALSE);
13691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692#ifdef DYNAMIC_RUBY
13693 else if (STRICMP(name, "ruby") == 0)
13694 n = ruby_enabled(FALSE);
13695#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013696#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697#ifdef DYNAMIC_PYTHON
13698 else if (STRICMP(name, "python") == 0)
13699 n = python_enabled(FALSE);
13700#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013701#endif
13702#ifdef FEAT_PYTHON3
13703#ifdef DYNAMIC_PYTHON3
13704 else if (STRICMP(name, "python3") == 0)
13705 n = python3_enabled(FALSE);
13706#endif
13707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708#ifdef DYNAMIC_PERL
13709 else if (STRICMP(name, "perl") == 0)
13710 n = perl_enabled(FALSE);
13711#endif
13712#ifdef FEAT_GUI
13713 else if (STRICMP(name, "gui_running") == 0)
13714 n = (gui.in_use || gui.starting);
13715# ifdef FEAT_GUI_W32
13716 else if (STRICMP(name, "gui_win32s") == 0)
13717 n = gui_is_win32s();
13718# endif
13719# ifdef FEAT_BROWSE
13720 else if (STRICMP(name, "browse") == 0)
13721 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13722# endif
13723#endif
13724#ifdef FEAT_SYN_HL
13725 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013726 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727#endif
13728#if defined(WIN3264)
13729 else if (STRICMP(name, "win95") == 0)
13730 n = mch_windows95();
13731#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013732#ifdef FEAT_NETBEANS_INTG
13733 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013734 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736 }
13737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739}
13740
13741/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013742 * "has_key()" function
13743 */
13744 static void
13745f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013746 typval_T *argvars;
13747 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013748{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013749 if (argvars[0].v_type != VAR_DICT)
13750 {
13751 EMSG(_(e_dictreq));
13752 return;
13753 }
13754 if (argvars[0].vval.v_dict == NULL)
13755 return;
13756
13757 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013758 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013759}
13760
13761/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013762 * "haslocaldir()" function
13763 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013764 static void
13765f_haslocaldir(argvars, rettv)
Bram Moolenaarc9703302016-01-17 21:49:33 +010013766 typval_T *argvars;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013767 typval_T *rettv;
13768{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013769 win_T *wp = NULL;
13770
13771 wp = find_tabwin(&argvars[0], &argvars[1]);
13772 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013773}
13774
13775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776 * "hasmapto()" function
13777 */
13778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013779f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013780 typval_T *argvars;
13781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782{
13783 char_u *name;
13784 char_u *mode;
13785 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013786 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013788 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013789 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790 mode = (char_u *)"nvo";
13791 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013792 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013793 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013794 if (argvars[2].v_type != VAR_UNKNOWN)
13795 abbr = get_tv_number(&argvars[2]);
13796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797
Bram Moolenaar2c932302006-03-18 21:42:09 +000013798 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013799 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013801 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802}
13803
13804/*
13805 * "histadd()" function
13806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013808f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013809 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811{
13812#ifdef FEAT_CMDHIST
13813 int histype;
13814 char_u *str;
13815 char_u buf[NUMBUFLEN];
13816#endif
13817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013818 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 if (check_restricted() || check_secure())
13820 return;
13821#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13823 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 if (histype >= 0)
13825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013826 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827 if (*str != NUL)
13828 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013829 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013831 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 return;
13833 }
13834 }
13835#endif
13836}
13837
13838/*
13839 * "histdel()" function
13840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013842f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013843 typval_T *argvars UNUSED;
13844 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845{
13846#ifdef FEAT_CMDHIST
13847 int n;
13848 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013849 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013851 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13852 if (str == NULL)
13853 n = 0;
13854 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013856 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013857 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013859 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013860 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861 else
13862 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013864 get_tv_string_buf(&argvars[1], buf));
13865 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866#endif
13867}
13868
13869/*
13870 * "histget()" function
13871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013873f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013874 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876{
13877#ifdef FEAT_CMDHIST
13878 int type;
13879 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013880 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013882 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13883 if (str == NULL)
13884 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013886 {
13887 type = get_histtype(str);
13888 if (argvars[1].v_type == VAR_UNKNOWN)
13889 idx = get_history_idx(type);
13890 else
13891 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13892 /* -1 on type error */
13893 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013896 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013898 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899}
13900
13901/*
13902 * "histnr()" function
13903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013905f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013906 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908{
13909 int i;
13910
13911#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013912 char_u *history = get_tv_string_chk(&argvars[0]);
13913
13914 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 if (i >= HIST_CMD && i < HIST_COUNT)
13916 i = get_history_idx(i);
13917 else
13918#endif
13919 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921}
13922
13923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924 * "highlightID(name)" function
13925 */
13926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013927f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013928 typval_T *argvars;
13929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013931 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932}
13933
13934/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013935 * "highlight_exists()" function
13936 */
13937 static void
13938f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013939 typval_T *argvars;
13940 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013941{
13942 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13943}
13944
13945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 * "hostname()" function
13947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013949f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013950 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952{
13953 char_u hostname[256];
13954
13955 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013956 rettv->v_type = VAR_STRING;
13957 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958}
13959
13960/*
13961 * iconv() function
13962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013965 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013966 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967{
13968#ifdef FEAT_MBYTE
13969 char_u buf1[NUMBUFLEN];
13970 char_u buf2[NUMBUFLEN];
13971 char_u *from, *to, *str;
13972 vimconv_T vimconv;
13973#endif
13974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 rettv->v_type = VAR_STRING;
13976 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977
13978#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013979 str = get_tv_string(&argvars[0]);
13980 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13981 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 vimconv.vc_type = CONV_NONE;
13983 convert_setup(&vimconv, from, to);
13984
13985 /* If the encodings are equal, no conversion needed. */
13986 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013987 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990
13991 convert_setup(&vimconv, NULL, NULL);
13992 vim_free(from);
13993 vim_free(to);
13994#endif
13995}
13996
13997/*
13998 * "indent()" function
13999 */
14000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014001f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014002 typval_T *argvars;
14003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004{
14005 linenr_T lnum;
14006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014007 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014009 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014011 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012}
14013
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014014/*
14015 * "index()" function
14016 */
14017 static void
14018f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014019 typval_T *argvars;
14020 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014021{
Bram Moolenaar33570922005-01-25 22:26:29 +000014022 list_T *l;
14023 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014024 long idx = 0;
14025 int ic = FALSE;
14026
14027 rettv->vval.v_number = -1;
14028 if (argvars[0].v_type != VAR_LIST)
14029 {
14030 EMSG(_(e_listreq));
14031 return;
14032 }
14033 l = argvars[0].vval.v_list;
14034 if (l != NULL)
14035 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014036 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014037 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014038 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014039 int error = FALSE;
14040
Bram Moolenaar758711c2005-02-02 23:11:38 +000014041 /* Start at specified item. Use the cached index that list_find()
14042 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014043 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014044 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014045 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014046 ic = get_tv_number_chk(&argvars[3], &error);
14047 if (error)
14048 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014049 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014050
Bram Moolenaar758711c2005-02-02 23:11:38 +000014051 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014052 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014053 {
14054 rettv->vval.v_number = idx;
14055 break;
14056 }
14057 }
14058}
14059
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060static int inputsecret_flag = 0;
14061
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014062static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014063
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014065 * This function is used by f_input() and f_inputdialog() functions. The third
14066 * argument to f_input() specifies the type of completion to use at the
14067 * prompt. The third argument to f_inputdialog() specifies the value to return
14068 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 */
14070 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014071get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000014072 typval_T *argvars;
14073 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014074 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077 char_u *p = NULL;
14078 int c;
14079 char_u buf[NUMBUFLEN];
14080 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014081 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014082 int xp_type = EXPAND_NOTHING;
14083 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087
14088#ifdef NO_CONSOLE_INPUT
14089 /* While starting up, there is no place to enter text. */
14090 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092#endif
14093
14094 cmd_silent = FALSE; /* Want to see the prompt. */
14095 if (prompt != NULL)
14096 {
14097 /* Only the part of the message after the last NL is considered as
14098 * prompt for the command line */
14099 p = vim_strrchr(prompt, '\n');
14100 if (p == NULL)
14101 p = prompt;
14102 else
14103 {
14104 ++p;
14105 c = *p;
14106 *p = NUL;
14107 msg_start();
14108 msg_clr_eos();
14109 msg_puts_attr(prompt, echo_attr);
14110 msg_didout = FALSE;
14111 msg_starthere();
14112 *p = c;
14113 }
14114 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014116 if (argvars[1].v_type != VAR_UNKNOWN)
14117 {
14118 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14119 if (defstr != NULL)
14120 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014122 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014123 {
14124 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014125 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014126 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014127
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014128 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014129 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014130
Bram Moolenaar4463f292005-09-25 22:20:24 +000014131 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14132 if (xp_name == NULL)
14133 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014134
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014135 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014136
Bram Moolenaar4463f292005-09-25 22:20:24 +000014137 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14138 &xp_arg) == FAIL)
14139 return;
14140 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014141 }
14142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014144 {
14145# ifdef FEAT_EX_EXTRA
14146 int save_ex_normal_busy = ex_normal_busy;
14147 ex_normal_busy = 0;
14148# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014150 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14151 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014152# ifdef FEAT_EX_EXTRA
14153 ex_normal_busy = save_ex_normal_busy;
14154# endif
14155 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014156 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014157 && argvars[1].v_type != VAR_UNKNOWN
14158 && argvars[2].v_type != VAR_UNKNOWN)
14159 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14160 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014161
14162 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 /* since the user typed this, no need to wait for return */
14165 need_wait_return = FALSE;
14166 msg_didout = FALSE;
14167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 cmd_silent = cmd_silent_save;
14169}
14170
14171/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014172 * "input()" function
14173 * Also handles inputsecret() when inputsecret is set.
14174 */
14175 static void
14176f_input(argvars, rettv)
14177 typval_T *argvars;
14178 typval_T *rettv;
14179{
14180 get_user_input(argvars, rettv, FALSE);
14181}
14182
14183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 * "inputdialog()" function
14185 */
14186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014187f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014188 typval_T *argvars;
14189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190{
14191#if defined(FEAT_GUI_TEXTDIALOG)
14192 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14193 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14194 {
14195 char_u *message;
14196 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 message = get_tv_string_chk(&argvars[0]);
14200 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014201 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014202 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203 else
14204 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 if (message != NULL && defstr != NULL
14206 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014207 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014208 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209 else
14210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 if (message != NULL && defstr != NULL
14212 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014213 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014214 rettv->vval.v_string = vim_strsave(
14215 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 }
14221 else
14222#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014223 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224}
14225
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014226/*
14227 * "inputlist()" function
14228 */
14229 static void
14230f_inputlist(argvars, rettv)
14231 typval_T *argvars;
14232 typval_T *rettv;
14233{
14234 listitem_T *li;
14235 int selected;
14236 int mouse_used;
14237
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014238#ifdef NO_CONSOLE_INPUT
14239 /* While starting up, there is no place to enter text. */
14240 if (no_console_input())
14241 return;
14242#endif
14243 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14244 {
14245 EMSG2(_(e_listarg), "inputlist()");
14246 return;
14247 }
14248
14249 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014250 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014251 lines_left = Rows; /* avoid more prompt */
14252 msg_scroll = TRUE;
14253 msg_clr_eos();
14254
14255 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14256 {
14257 msg_puts(get_tv_string(&li->li_tv));
14258 msg_putchar('\n');
14259 }
14260
14261 /* Ask for choice. */
14262 selected = prompt_for_number(&mouse_used);
14263 if (mouse_used)
14264 selected -= lines_left;
14265
14266 rettv->vval.v_number = selected;
14267}
14268
14269
Bram Moolenaar071d4272004-06-13 20:20:40 +000014270static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14271
14272/*
14273 * "inputrestore()" function
14274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014276f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014277 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279{
14280 if (ga_userinput.ga_len > 0)
14281 {
14282 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14284 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014285 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 }
14287 else if (p_verbose > 1)
14288 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014289 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014290 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 }
14292}
14293
14294/*
14295 * "inputsave()" function
14296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014298f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014299 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014302 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 if (ga_grow(&ga_userinput, 1) == OK)
14304 {
14305 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14306 + ga_userinput.ga_len);
14307 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014308 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 }
14310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014311 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312}
14313
14314/*
14315 * "inputsecret()" function
14316 */
14317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014318f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014319 typval_T *argvars;
14320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321{
14322 ++cmdline_star;
14323 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014324 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325 --cmdline_star;
14326 --inputsecret_flag;
14327}
14328
14329/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014330 * "insert()" function
14331 */
14332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014334 typval_T *argvars;
14335 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014336{
14337 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014338 listitem_T *item;
14339 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014340 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014341
14342 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014343 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014344 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014345 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014346 {
14347 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 before = get_tv_number_chk(&argvars[2], &error);
14349 if (error)
14350 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014351
Bram Moolenaar758711c2005-02-02 23:11:38 +000014352 if (before == l->lv_len)
14353 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014354 else
14355 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014356 item = list_find(l, before);
14357 if (item == NULL)
14358 {
14359 EMSGN(_(e_listidx), before);
14360 l = NULL;
14361 }
14362 }
14363 if (l != NULL)
14364 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014365 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014366 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014367 }
14368 }
14369}
14370
14371/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014372 * "invert(expr)" function
14373 */
14374 static void
14375f_invert(argvars, rettv)
14376 typval_T *argvars;
14377 typval_T *rettv;
14378{
14379 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14380}
14381
14382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 * "isdirectory()" function
14384 */
14385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014386f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014387 typval_T *argvars;
14388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014390 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391}
14392
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014393/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014394 * "islocked()" function
14395 */
14396 static void
14397f_islocked(argvars, rettv)
14398 typval_T *argvars;
14399 typval_T *rettv;
14400{
14401 lval_T lv;
14402 char_u *end;
14403 dictitem_T *di;
14404
14405 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014406 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14407 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014408 if (end != NULL && lv.ll_name != NULL)
14409 {
14410 if (*end != NUL)
14411 EMSG(_(e_trailing));
14412 else
14413 {
14414 if (lv.ll_tv == NULL)
14415 {
14416 if (check_changedtick(lv.ll_name))
14417 rettv->vval.v_number = 1; /* always locked */
14418 else
14419 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014420 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014421 if (di != NULL)
14422 {
14423 /* Consider a variable locked when:
14424 * 1. the variable itself is locked
14425 * 2. the value of the variable is locked.
14426 * 3. the List or Dict value is locked.
14427 */
14428 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14429 || tv_islocked(&di->di_tv));
14430 }
14431 }
14432 }
14433 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014434 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014435 else if (lv.ll_newkey != NULL)
14436 EMSG2(_(e_dictkey), lv.ll_newkey);
14437 else if (lv.ll_list != NULL)
14438 /* List item. */
14439 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14440 else
14441 /* Dictionary item. */
14442 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14443 }
14444 }
14445
14446 clear_lval(&lv);
14447}
14448
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014449static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014450
14451/*
14452 * Turn a dict into a list:
14453 * "what" == 0: list of keys
14454 * "what" == 1: list of values
14455 * "what" == 2: list of items
14456 */
14457 static void
14458dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000014459 typval_T *argvars;
14460 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014461 int what;
14462{
Bram Moolenaar33570922005-01-25 22:26:29 +000014463 list_T *l2;
14464 dictitem_T *di;
14465 hashitem_T *hi;
14466 listitem_T *li;
14467 listitem_T *li2;
14468 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014469 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014470
Bram Moolenaar8c711452005-01-14 21:53:12 +000014471 if (argvars[0].v_type != VAR_DICT)
14472 {
14473 EMSG(_(e_dictreq));
14474 return;
14475 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014476 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014477 return;
14478
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014479 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014480 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014481
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014482 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014483 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014484 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014485 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014486 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014487 --todo;
14488 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014489
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014490 li = listitem_alloc();
14491 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014492 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014493 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014494
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014495 if (what == 0)
14496 {
14497 /* keys() */
14498 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014499 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014500 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14501 }
14502 else if (what == 1)
14503 {
14504 /* values() */
14505 copy_tv(&di->di_tv, &li->li_tv);
14506 }
14507 else
14508 {
14509 /* items() */
14510 l2 = list_alloc();
14511 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014512 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014513 li->li_tv.vval.v_list = l2;
14514 if (l2 == NULL)
14515 break;
14516 ++l2->lv_refcount;
14517
14518 li2 = listitem_alloc();
14519 if (li2 == NULL)
14520 break;
14521 list_append(l2, li2);
14522 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014523 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014524 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14525
14526 li2 = listitem_alloc();
14527 if (li2 == NULL)
14528 break;
14529 list_append(l2, li2);
14530 copy_tv(&di->di_tv, &li2->li_tv);
14531 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014532 }
14533 }
14534}
14535
14536/*
14537 * "items(dict)" function
14538 */
14539 static void
14540f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014541 typval_T *argvars;
14542 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014543{
14544 dict_list(argvars, rettv, 2);
14545}
14546
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014548 * "join()" function
14549 */
14550 static void
14551f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014552 typval_T *argvars;
14553 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014554{
14555 garray_T ga;
14556 char_u *sep;
14557
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014558 if (argvars[0].v_type != VAR_LIST)
14559 {
14560 EMSG(_(e_listreq));
14561 return;
14562 }
14563 if (argvars[0].vval.v_list == NULL)
14564 return;
14565 if (argvars[1].v_type == VAR_UNKNOWN)
14566 sep = (char_u *)" ";
14567 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014569
14570 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014571
14572 if (sep != NULL)
14573 {
14574 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014575 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014576 ga_append(&ga, NUL);
14577 rettv->vval.v_string = (char_u *)ga.ga_data;
14578 }
14579 else
14580 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014581}
14582
14583/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014584 * "jsondecode()" function
14585 */
14586 static void
14587f_jsondecode(argvars, rettv)
14588 typval_T *argvars;
14589 typval_T *rettv;
14590{
14591 js_read_T reader;
14592
14593 reader.js_buf = get_tv_string(&argvars[0]);
14594 reader.js_eof = TRUE;
14595 reader.js_used = 0;
14596 json_decode(&reader, rettv);
14597}
14598
14599/*
14600 * "jsonencode()" function
14601 */
14602 static void
14603f_jsonencode(argvars, rettv)
14604 typval_T *argvars;
14605 typval_T *rettv;
14606{
14607 rettv->v_type = VAR_STRING;
14608 rettv->vval.v_string = json_encode(&argvars[0]);
14609}
14610
14611/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014612 * "keys()" function
14613 */
14614 static void
14615f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014616 typval_T *argvars;
14617 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014618{
14619 dict_list(argvars, rettv, 0);
14620}
14621
14622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623 * "last_buffer_nr()" function.
14624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014627 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014628 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629{
14630 int n = 0;
14631 buf_T *buf;
14632
14633 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14634 if (n < buf->b_fnum)
14635 n = buf->b_fnum;
14636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014637 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014638}
14639
14640/*
14641 * "len()" function
14642 */
14643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014644f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014645 typval_T *argvars;
14646 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014647{
14648 switch (argvars[0].v_type)
14649 {
14650 case VAR_STRING:
14651 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014652 rettv->vval.v_number = (varnumber_T)STRLEN(
14653 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014654 break;
14655 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014656 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014657 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014658 case VAR_DICT:
14659 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14660 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014661 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014662 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014663 break;
14664 }
14665}
14666
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014667static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014668
14669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014670libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014671 typval_T *argvars;
14672 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014673 int type;
14674{
14675#ifdef FEAT_LIBCALL
14676 char_u *string_in;
14677 char_u **string_result;
14678 int nr_result;
14679#endif
14680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014682 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014684
14685 if (check_restricted() || check_secure())
14686 return;
14687
14688#ifdef FEAT_LIBCALL
14689 /* The first two args must be strings, otherwise its meaningless */
14690 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14691 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014692 string_in = NULL;
14693 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014694 string_in = argvars[2].vval.v_string;
14695 if (type == VAR_NUMBER)
14696 string_result = NULL;
14697 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014698 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014699 if (mch_libcall(argvars[0].vval.v_string,
14700 argvars[1].vval.v_string,
14701 string_in,
14702 argvars[2].vval.v_number,
14703 string_result,
14704 &nr_result) == OK
14705 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014706 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014707 }
14708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709}
14710
14711/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014712 * "libcall()" function
14713 */
14714 static void
14715f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014716 typval_T *argvars;
14717 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014718{
14719 libcall_common(argvars, rettv, VAR_STRING);
14720}
14721
14722/*
14723 * "libcallnr()" function
14724 */
14725 static void
14726f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014727 typval_T *argvars;
14728 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014729{
14730 libcall_common(argvars, rettv, VAR_NUMBER);
14731}
14732
14733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 * "line(string)" function
14735 */
14736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014737f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014738 typval_T *argvars;
14739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740{
14741 linenr_T lnum = 0;
14742 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014743 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014745 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 if (fp != NULL)
14747 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749}
14750
14751/*
14752 * "line2byte(lnum)" function
14753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014756 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758{
14759#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014760 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761#else
14762 linenr_T lnum;
14763
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014766 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014768 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14769 if (rettv->vval.v_number >= 0)
14770 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771#endif
14772}
14773
14774/*
14775 * "lispindent(lnum)" function
14776 */
14777 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014778f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014779 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014780 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781{
14782#ifdef FEAT_LISP
14783 pos_T pos;
14784 linenr_T lnum;
14785
14786 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014787 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14789 {
14790 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014791 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792 curwin->w_cursor = pos;
14793 }
14794 else
14795#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014796 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014797}
14798
14799/*
14800 * "localtime()" function
14801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014803f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014804 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014807 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808}
14809
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014810static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811
14812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014813get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014814 typval_T *argvars;
14815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 int exact;
14817{
14818 char_u *keys;
14819 char_u *which;
14820 char_u buf[NUMBUFLEN];
14821 char_u *keys_buf = NULL;
14822 char_u *rhs;
14823 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014824 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014825 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014826 mapblock_T *mp;
14827 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828
14829 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014830 rettv->v_type = VAR_STRING;
14831 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 if (*keys == NUL)
14835 return;
14836
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014837 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014839 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014840 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014841 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014842 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014843 if (argvars[3].v_type != VAR_UNKNOWN)
14844 get_dict = get_tv_number(&argvars[3]);
14845 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 else
14848 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014849 if (which == NULL)
14850 return;
14851
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 mode = get_map_mode(&which, 0);
14853
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014854 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014855 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014857
14858 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014860 /* Return a string. */
14861 if (rhs != NULL)
14862 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863
Bram Moolenaarbd743252010-10-20 21:23:33 +020014864 }
14865 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14866 {
14867 /* Return a dictionary. */
14868 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14869 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14870 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871
Bram Moolenaarbd743252010-10-20 21:23:33 +020014872 dict_add_nr_str(dict, "lhs", 0L, lhs);
14873 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14874 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14875 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14876 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14877 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14878 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014879 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014880 dict_add_nr_str(dict, "mode", 0L, mapmode);
14881
14882 vim_free(lhs);
14883 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884 }
14885}
14886
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014887#ifdef FEAT_FLOAT
14888/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014889 * "log()" function
14890 */
14891 static void
14892f_log(argvars, rettv)
14893 typval_T *argvars;
14894 typval_T *rettv;
14895{
14896 float_T f;
14897
14898 rettv->v_type = VAR_FLOAT;
14899 if (get_float_arg(argvars, &f) == OK)
14900 rettv->vval.v_float = log(f);
14901 else
14902 rettv->vval.v_float = 0.0;
14903}
14904
14905/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014906 * "log10()" function
14907 */
14908 static void
14909f_log10(argvars, rettv)
14910 typval_T *argvars;
14911 typval_T *rettv;
14912{
14913 float_T f;
14914
14915 rettv->v_type = VAR_FLOAT;
14916 if (get_float_arg(argvars, &f) == OK)
14917 rettv->vval.v_float = log10(f);
14918 else
14919 rettv->vval.v_float = 0.0;
14920}
14921#endif
14922
Bram Moolenaar1dced572012-04-05 16:54:08 +020014923#ifdef FEAT_LUA
14924/*
14925 * "luaeval()" function
14926 */
14927 static void
14928f_luaeval(argvars, rettv)
14929 typval_T *argvars;
14930 typval_T *rettv;
14931{
14932 char_u *str;
14933 char_u buf[NUMBUFLEN];
14934
14935 str = get_tv_string_buf(&argvars[0], buf);
14936 do_luaeval(str, argvars + 1, rettv);
14937}
14938#endif
14939
Bram Moolenaar071d4272004-06-13 20:20:40 +000014940/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014941 * "map()" function
14942 */
14943 static void
14944f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014945 typval_T *argvars;
14946 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014947{
14948 filter_map(argvars, rettv, TRUE);
14949}
14950
14951/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014952 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953 */
14954 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014955f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014956 typval_T *argvars;
14957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014959 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960}
14961
14962/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014963 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964 */
14965 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014966f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014967 typval_T *argvars;
14968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014970 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971}
14972
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014973static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974
14975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014976find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014977 typval_T *argvars;
14978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 int type;
14980{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014981 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014982 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014983 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014984 char_u *pat;
14985 regmatch_T regmatch;
14986 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014987 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 char_u *save_cpo;
14989 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014990 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014991 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014992 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014993 list_T *l = NULL;
14994 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014995 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014996 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997
14998 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14999 save_cpo = p_cpo;
15000 p_cpo = (char_u *)"";
15001
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015002 rettv->vval.v_number = -1;
15003 if (type == 3)
15004 {
15005 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015006 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015007 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015008 }
15009 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015011 rettv->v_type = VAR_STRING;
15012 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015015 if (argvars[0].v_type == VAR_LIST)
15016 {
15017 if ((l = argvars[0].vval.v_list) == NULL)
15018 goto theend;
15019 li = l->lv_first;
15020 }
15021 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015022 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015023 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015024 len = (long)STRLEN(str);
15025 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015027 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15028 if (pat == NULL)
15029 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015030
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015031 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015033 int error = FALSE;
15034
15035 start = get_tv_number_chk(&argvars[2], &error);
15036 if (error)
15037 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015038 if (l != NULL)
15039 {
15040 li = list_find(l, start);
15041 if (li == NULL)
15042 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015043 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015044 }
15045 else
15046 {
15047 if (start < 0)
15048 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015049 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015050 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015051 /* When "count" argument is there ignore matches before "start",
15052 * otherwise skip part of the string. Differs when pattern is "^"
15053 * or "\<". */
15054 if (argvars[3].v_type != VAR_UNKNOWN)
15055 startcol = start;
15056 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015057 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015058 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015059 len -= start;
15060 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015061 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015062
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015063 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015064 nth = get_tv_number_chk(&argvars[3], &error);
15065 if (error)
15066 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067 }
15068
15069 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15070 if (regmatch.regprog != NULL)
15071 {
15072 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015073
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015074 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015075 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015076 if (l != NULL)
15077 {
15078 if (li == NULL)
15079 {
15080 match = FALSE;
15081 break;
15082 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015083 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015084 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015085 if (str == NULL)
15086 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015087 }
15088
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015089 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015090
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015091 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015092 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015093 if (l == NULL && !match)
15094 break;
15095
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015096 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015097 if (l != NULL)
15098 {
15099 li = li->li_next;
15100 ++idx;
15101 }
15102 else
15103 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015104#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015105 startcol = (colnr_T)(regmatch.startp[0]
15106 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015107#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015108 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015109#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015110 if (startcol > (colnr_T)len
15111 || str + startcol <= regmatch.startp[0])
15112 {
15113 match = FALSE;
15114 break;
15115 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015116 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015117 }
15118
15119 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015121 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015122 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015123 int i;
15124
15125 /* return list with matched string and submatches */
15126 for (i = 0; i < NSUBEXP; ++i)
15127 {
15128 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015129 {
15130 if (list_append_string(rettv->vval.v_list,
15131 (char_u *)"", 0) == FAIL)
15132 break;
15133 }
15134 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015135 regmatch.startp[i],
15136 (int)(regmatch.endp[i] - regmatch.startp[i]))
15137 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015138 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015139 }
15140 }
15141 else if (type == 2)
15142 {
15143 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015144 if (l != NULL)
15145 copy_tv(&li->li_tv, rettv);
15146 else
15147 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015149 }
15150 else if (l != NULL)
15151 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152 else
15153 {
15154 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015155 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156 (varnumber_T)(regmatch.startp[0] - str);
15157 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015158 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015160 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161 }
15162 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015163 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 }
15165
15166theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015167 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168 p_cpo = save_cpo;
15169}
15170
15171/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172 * "match()" function
15173 */
15174 static void
15175f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015176 typval_T *argvars;
15177 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015178{
15179 find_some_match(argvars, rettv, 1);
15180}
15181
15182/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015183 * "matchadd()" function
15184 */
15185 static void
15186f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015187 typval_T *argvars UNUSED;
15188 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015189{
15190#ifdef FEAT_SEARCH_EXTRA
15191 char_u buf[NUMBUFLEN];
15192 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15193 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15194 int prio = 10; /* default priority */
15195 int id = -1;
15196 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015197 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015198
15199 rettv->vval.v_number = -1;
15200
15201 if (grp == NULL || pat == NULL)
15202 return;
15203 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015204 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015205 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015206 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015207 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015208 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015209 if (argvars[4].v_type != VAR_UNKNOWN)
15210 {
15211 if (argvars[4].v_type != VAR_DICT)
15212 {
15213 EMSG(_(e_dictreq));
15214 return;
15215 }
15216 if (dict_find(argvars[4].vval.v_dict,
15217 (char_u *)"conceal", -1) != NULL)
15218 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15219 (char_u *)"conceal", FALSE);
15220 }
15221 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015222 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015223 if (error == TRUE)
15224 return;
15225 if (id >= 1 && id <= 3)
15226 {
15227 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15228 return;
15229 }
15230
Bram Moolenaar6561d522015-07-21 15:48:27 +020015231 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15232 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015233#endif
15234}
15235
15236/*
15237 * "matchaddpos()" function
15238 */
15239 static void
15240f_matchaddpos(argvars, rettv)
15241 typval_T *argvars UNUSED;
15242 typval_T *rettv UNUSED;
15243{
15244#ifdef FEAT_SEARCH_EXTRA
15245 char_u buf[NUMBUFLEN];
15246 char_u *group;
15247 int prio = 10;
15248 int id = -1;
15249 int error = FALSE;
15250 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015251 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015252
15253 rettv->vval.v_number = -1;
15254
15255 group = get_tv_string_buf_chk(&argvars[0], buf);
15256 if (group == NULL)
15257 return;
15258
15259 if (argvars[1].v_type != VAR_LIST)
15260 {
15261 EMSG2(_(e_listarg), "matchaddpos()");
15262 return;
15263 }
15264 l = argvars[1].vval.v_list;
15265 if (l == NULL)
15266 return;
15267
15268 if (argvars[2].v_type != VAR_UNKNOWN)
15269 {
15270 prio = get_tv_number_chk(&argvars[2], &error);
15271 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015272 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015273 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015274 if (argvars[4].v_type != VAR_UNKNOWN)
15275 {
15276 if (argvars[4].v_type != VAR_DICT)
15277 {
15278 EMSG(_(e_dictreq));
15279 return;
15280 }
15281 if (dict_find(argvars[4].vval.v_dict,
15282 (char_u *)"conceal", -1) != NULL)
15283 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15284 (char_u *)"conceal", FALSE);
15285 }
15286 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015287 }
15288 if (error == TRUE)
15289 return;
15290
15291 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15292 if (id == 1 || id == 2)
15293 {
15294 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15295 return;
15296 }
15297
Bram Moolenaar6561d522015-07-21 15:48:27 +020015298 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15299 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015300#endif
15301}
15302
15303/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015304 * "matcharg()" function
15305 */
15306 static void
15307f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015308 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015309 typval_T *rettv;
15310{
15311 if (rettv_list_alloc(rettv) == OK)
15312 {
15313#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015314 int id = get_tv_number(&argvars[0]);
15315 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015316
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015317 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015318 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015319 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15320 {
15321 list_append_string(rettv->vval.v_list,
15322 syn_id2name(m->hlg_id), -1);
15323 list_append_string(rettv->vval.v_list, m->pattern, -1);
15324 }
15325 else
15326 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015327 list_append_string(rettv->vval.v_list, NULL, -1);
15328 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015329 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015330 }
15331#endif
15332 }
15333}
15334
15335/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015336 * "matchdelete()" function
15337 */
15338 static void
15339f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015340 typval_T *argvars UNUSED;
15341 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015342{
15343#ifdef FEAT_SEARCH_EXTRA
15344 rettv->vval.v_number = match_delete(curwin,
15345 (int)get_tv_number(&argvars[0]), TRUE);
15346#endif
15347}
15348
15349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015350 * "matchend()" function
15351 */
15352 static void
15353f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015354 typval_T *argvars;
15355 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015356{
15357 find_some_match(argvars, rettv, 0);
15358}
15359
15360/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015361 * "matchlist()" function
15362 */
15363 static void
15364f_matchlist(argvars, rettv)
15365 typval_T *argvars;
15366 typval_T *rettv;
15367{
15368 find_some_match(argvars, rettv, 3);
15369}
15370
15371/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015372 * "matchstr()" function
15373 */
15374 static void
15375f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015376 typval_T *argvars;
15377 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015378{
15379 find_some_match(argvars, rettv, 2);
15380}
15381
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015382static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015383
15384 static void
15385max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000015386 typval_T *argvars;
15387 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015388 int domax;
15389{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015390 long n = 0;
15391 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015392 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015393
15394 if (argvars[0].v_type == VAR_LIST)
15395 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015396 list_T *l;
15397 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015398
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015399 l = argvars[0].vval.v_list;
15400 if (l != NULL)
15401 {
15402 li = l->lv_first;
15403 if (li != NULL)
15404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015405 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015406 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015407 {
15408 li = li->li_next;
15409 if (li == NULL)
15410 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015411 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015412 if (domax ? i > n : i < n)
15413 n = i;
15414 }
15415 }
15416 }
15417 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015418 else if (argvars[0].v_type == VAR_DICT)
15419 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015420 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015421 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015422 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015423 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015424
15425 d = argvars[0].vval.v_dict;
15426 if (d != NULL)
15427 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015428 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015429 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015431 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015433 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015434 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015435 if (first)
15436 {
15437 n = i;
15438 first = FALSE;
15439 }
15440 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015441 n = i;
15442 }
15443 }
15444 }
15445 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015446 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015447 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015448 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015449}
15450
15451/*
15452 * "max()" function
15453 */
15454 static void
15455f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015456 typval_T *argvars;
15457 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015458{
15459 max_min(argvars, rettv, TRUE);
15460}
15461
15462/*
15463 * "min()" function
15464 */
15465 static void
15466f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015467 typval_T *argvars;
15468 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015469{
15470 max_min(argvars, rettv, FALSE);
15471}
15472
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015473static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015474
15475/*
15476 * Create the directory in which "dir" is located, and higher levels when
15477 * needed.
15478 */
15479 static int
15480mkdir_recurse(dir, prot)
15481 char_u *dir;
15482 int prot;
15483{
15484 char_u *p;
15485 char_u *updir;
15486 int r = FAIL;
15487
15488 /* Get end of directory name in "dir".
15489 * We're done when it's "/" or "c:/". */
15490 p = gettail_sep(dir);
15491 if (p <= get_past_head(dir))
15492 return OK;
15493
15494 /* If the directory exists we're done. Otherwise: create it.*/
15495 updir = vim_strnsave(dir, (int)(p - dir));
15496 if (updir == NULL)
15497 return FAIL;
15498 if (mch_isdir(updir))
15499 r = OK;
15500 else if (mkdir_recurse(updir, prot) == OK)
15501 r = vim_mkdir_emsg(updir, prot);
15502 vim_free(updir);
15503 return r;
15504}
15505
15506#ifdef vim_mkdir
15507/*
15508 * "mkdir()" function
15509 */
15510 static void
15511f_mkdir(argvars, rettv)
15512 typval_T *argvars;
15513 typval_T *rettv;
15514{
15515 char_u *dir;
15516 char_u buf[NUMBUFLEN];
15517 int prot = 0755;
15518
15519 rettv->vval.v_number = FAIL;
15520 if (check_restricted() || check_secure())
15521 return;
15522
15523 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015524 if (*dir == NUL)
15525 rettv->vval.v_number = FAIL;
15526 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015527 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015528 if (*gettail(dir) == NUL)
15529 /* remove trailing slashes */
15530 *gettail_sep(dir) = NUL;
15531
15532 if (argvars[1].v_type != VAR_UNKNOWN)
15533 {
15534 if (argvars[2].v_type != VAR_UNKNOWN)
15535 prot = get_tv_number_chk(&argvars[2], NULL);
15536 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15537 mkdir_recurse(dir, prot);
15538 }
15539 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015540 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015541}
15542#endif
15543
Bram Moolenaar0d660222005-01-07 21:51:51 +000015544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 * "mode()" function
15546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015549 typval_T *argvars;
15550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015552 char_u buf[3];
15553
15554 buf[1] = NUL;
15555 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 if (VIsual_active)
15558 {
15559 if (VIsual_select)
15560 buf[0] = VIsual_mode + 's' - 'v';
15561 else
15562 buf[0] = VIsual_mode;
15563 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015564 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015565 || State == CONFIRM)
15566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015568 if (State == ASKMORE)
15569 buf[1] = 'm';
15570 else if (State == CONFIRM)
15571 buf[1] = '?';
15572 }
15573 else if (State == EXTERNCMD)
15574 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575 else if (State & INSERT)
15576 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015577#ifdef FEAT_VREPLACE
15578 if (State & VREPLACE_FLAG)
15579 {
15580 buf[0] = 'R';
15581 buf[1] = 'v';
15582 }
15583 else
15584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 if (State & REPLACE_FLAG)
15586 buf[0] = 'R';
15587 else
15588 buf[0] = 'i';
15589 }
15590 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015593 if (exmode_active)
15594 buf[1] = 'v';
15595 }
15596 else if (exmode_active)
15597 {
15598 buf[0] = 'c';
15599 buf[1] = 'e';
15600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015604 if (finish_op)
15605 buf[1] = 'o';
15606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015608 /* Clear out the minor mode when the argument is not a non-zero number or
15609 * non-empty string. */
15610 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015611 buf[1] = NUL;
15612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015613 rettv->vval.v_string = vim_strsave(buf);
15614 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615}
15616
Bram Moolenaar429fa852013-04-15 12:27:36 +020015617#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015618/*
15619 * "mzeval()" function
15620 */
15621 static void
15622f_mzeval(argvars, rettv)
15623 typval_T *argvars;
15624 typval_T *rettv;
15625{
15626 char_u *str;
15627 char_u buf[NUMBUFLEN];
15628
15629 str = get_tv_string_buf(&argvars[0], buf);
15630 do_mzeval(str, rettv);
15631}
Bram Moolenaar75676462013-01-30 14:55:42 +010015632
15633 void
15634mzscheme_call_vim(name, args, rettv)
15635 char_u *name;
15636 typval_T *args;
15637 typval_T *rettv;
15638{
15639 typval_T argvars[3];
15640
15641 argvars[0].v_type = VAR_STRING;
15642 argvars[0].vval.v_string = name;
15643 copy_tv(args, &argvars[1]);
15644 argvars[2].v_type = VAR_UNKNOWN;
15645 f_call(argvars, rettv);
15646 clear_tv(&argvars[1]);
15647}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015648#endif
15649
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015651 * "nextnonblank()" function
15652 */
15653 static void
15654f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015655 typval_T *argvars;
15656 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015657{
15658 linenr_T lnum;
15659
15660 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015662 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015663 {
15664 lnum = 0;
15665 break;
15666 }
15667 if (*skipwhite(ml_get(lnum)) != NUL)
15668 break;
15669 }
15670 rettv->vval.v_number = lnum;
15671}
15672
15673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674 * "nr2char()" function
15675 */
15676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015677f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015678 typval_T *argvars;
15679 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680{
15681 char_u buf[NUMBUFLEN];
15682
15683#ifdef FEAT_MBYTE
15684 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015685 {
15686 int utf8 = 0;
15687
15688 if (argvars[1].v_type != VAR_UNKNOWN)
15689 utf8 = get_tv_number_chk(&argvars[1], NULL);
15690 if (utf8)
15691 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15692 else
15693 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695 else
15696#endif
15697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015698 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699 buf[1] = NUL;
15700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015701 rettv->v_type = VAR_STRING;
15702 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015703}
15704
15705/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015706 * "or(expr, expr)" function
15707 */
15708 static void
15709f_or(argvars, rettv)
15710 typval_T *argvars;
15711 typval_T *rettv;
15712{
15713 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15714 | get_tv_number_chk(&argvars[1], NULL);
15715}
15716
15717/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015718 * "pathshorten()" function
15719 */
15720 static void
15721f_pathshorten(argvars, rettv)
15722 typval_T *argvars;
15723 typval_T *rettv;
15724{
15725 char_u *p;
15726
15727 rettv->v_type = VAR_STRING;
15728 p = get_tv_string_chk(&argvars[0]);
15729 if (p == NULL)
15730 rettv->vval.v_string = NULL;
15731 else
15732 {
15733 p = vim_strsave(p);
15734 rettv->vval.v_string = p;
15735 if (p != NULL)
15736 shorten_dir(p);
15737 }
15738}
15739
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015740#ifdef FEAT_PERL
15741/*
15742 * "perleval()" function
15743 */
15744 static void
15745f_perleval(argvars, rettv)
15746 typval_T *argvars;
15747 typval_T *rettv;
15748{
15749 char_u *str;
15750 char_u buf[NUMBUFLEN];
15751
15752 str = get_tv_string_buf(&argvars[0], buf);
15753 do_perleval(str, rettv);
15754}
15755#endif
15756
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015757#ifdef FEAT_FLOAT
15758/*
15759 * "pow()" function
15760 */
15761 static void
15762f_pow(argvars, rettv)
15763 typval_T *argvars;
15764 typval_T *rettv;
15765{
15766 float_T fx, fy;
15767
15768 rettv->v_type = VAR_FLOAT;
15769 if (get_float_arg(argvars, &fx) == OK
15770 && get_float_arg(&argvars[1], &fy) == OK)
15771 rettv->vval.v_float = pow(fx, fy);
15772 else
15773 rettv->vval.v_float = 0.0;
15774}
15775#endif
15776
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015777/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778 * "prevnonblank()" function
15779 */
15780 static void
15781f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015782 typval_T *argvars;
15783 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015784{
15785 linenr_T lnum;
15786
15787 lnum = get_tv_lnum(argvars);
15788 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15789 lnum = 0;
15790 else
15791 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15792 --lnum;
15793 rettv->vval.v_number = lnum;
15794}
15795
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015796#ifdef HAVE_STDARG_H
15797/* This dummy va_list is here because:
15798 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15799 * - locally in the function results in a "used before set" warning
15800 * - using va_start() to initialize it gives "function with fixed args" error */
15801static va_list ap;
15802#endif
15803
Bram Moolenaar8c711452005-01-14 21:53:12 +000015804/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015805 * "printf()" function
15806 */
15807 static void
15808f_printf(argvars, rettv)
15809 typval_T *argvars;
15810 typval_T *rettv;
15811{
15812 rettv->v_type = VAR_STRING;
15813 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000015814#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015815 {
15816 char_u buf[NUMBUFLEN];
15817 int len;
15818 char_u *s;
15819 int saved_did_emsg = did_emsg;
15820 char *fmt;
15821
15822 /* Get the required length, allocate the buffer and do it for real. */
15823 did_emsg = FALSE;
15824 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015825 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015826 if (!did_emsg)
15827 {
15828 s = alloc(len + 1);
15829 if (s != NULL)
15830 {
15831 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000015832 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015833 }
15834 }
15835 did_emsg |= saved_did_emsg;
15836 }
15837#endif
15838}
15839
15840/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015841 * "pumvisible()" function
15842 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015843 static void
15844f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015845 typval_T *argvars UNUSED;
15846 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015847{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015848#ifdef FEAT_INS_EXPAND
15849 if (pum_visible())
15850 rettv->vval.v_number = 1;
15851#endif
15852}
15853
Bram Moolenaardb913952012-06-29 12:54:53 +020015854#ifdef FEAT_PYTHON3
15855/*
15856 * "py3eval()" function
15857 */
15858 static void
15859f_py3eval(argvars, rettv)
15860 typval_T *argvars;
15861 typval_T *rettv;
15862{
15863 char_u *str;
15864 char_u buf[NUMBUFLEN];
15865
15866 str = get_tv_string_buf(&argvars[0], buf);
15867 do_py3eval(str, rettv);
15868}
15869#endif
15870
15871#ifdef FEAT_PYTHON
15872/*
15873 * "pyeval()" function
15874 */
15875 static void
15876f_pyeval(argvars, rettv)
15877 typval_T *argvars;
15878 typval_T *rettv;
15879{
15880 char_u *str;
15881 char_u buf[NUMBUFLEN];
15882
15883 str = get_tv_string_buf(&argvars[0], buf);
15884 do_pyeval(str, rettv);
15885}
15886#endif
15887
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015888/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015889 * "range()" function
15890 */
15891 static void
15892f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015893 typval_T *argvars;
15894 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015895{
15896 long start;
15897 long end;
15898 long stride = 1;
15899 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015900 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015902 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015903 if (argvars[1].v_type == VAR_UNKNOWN)
15904 {
15905 end = start - 1;
15906 start = 0;
15907 }
15908 else
15909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015910 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015911 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015912 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015913 }
15914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015915 if (error)
15916 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015917 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015918 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015919 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015920 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015921 else
15922 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015923 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015924 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015925 if (list_append_number(rettv->vval.v_list,
15926 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015927 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015928 }
15929}
15930
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015931/*
15932 * "readfile()" function
15933 */
15934 static void
15935f_readfile(argvars, rettv)
15936 typval_T *argvars;
15937 typval_T *rettv;
15938{
15939 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015940 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015941 char_u *fname;
15942 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015943 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15944 int io_size = sizeof(buf);
15945 int readlen; /* size of last fread() */
15946 char_u *prev = NULL; /* previously read bytes, if any */
15947 long prevlen = 0; /* length of data in prev */
15948 long prevsize = 0; /* size of prev buffer */
15949 long maxline = MAXLNUM;
15950 long cnt = 0;
15951 char_u *p; /* position in buf */
15952 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015953
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015954 if (argvars[1].v_type != VAR_UNKNOWN)
15955 {
15956 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15957 binary = TRUE;
15958 if (argvars[2].v_type != VAR_UNKNOWN)
15959 maxline = get_tv_number(&argvars[2]);
15960 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015961
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015962 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015963 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015964
15965 /* Always open the file in binary mode, library functions have a mind of
15966 * their own about CR-LF conversion. */
15967 fname = get_tv_string(&argvars[0]);
15968 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15969 {
15970 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15971 return;
15972 }
15973
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015974 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015975 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015976 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015977
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015978 /* This for loop processes what was read, but is also entered at end
15979 * of file so that either:
15980 * - an incomplete line gets written
15981 * - a "binary" file gets an empty line at the end if it ends in a
15982 * newline. */
15983 for (p = buf, start = buf;
15984 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15985 ++p)
15986 {
15987 if (*p == '\n' || readlen <= 0)
15988 {
15989 listitem_T *li;
15990 char_u *s = NULL;
15991 long_u len = p - start;
15992
15993 /* Finished a line. Remove CRs before NL. */
15994 if (readlen > 0 && !binary)
15995 {
15996 while (len > 0 && start[len - 1] == '\r')
15997 --len;
15998 /* removal may cross back to the "prev" string */
15999 if (len == 0)
16000 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16001 --prevlen;
16002 }
16003 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016004 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016005 else
16006 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016007 /* Change "prev" buffer to be the right size. This way
16008 * the bytes are only copied once, and very long lines are
16009 * allocated only once. */
16010 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016011 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016012 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016013 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016014 prev = NULL; /* the list will own the string */
16015 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016016 }
16017 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016018 if (s == NULL)
16019 {
16020 do_outofmem_msg((long_u) prevlen + len + 1);
16021 failed = TRUE;
16022 break;
16023 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016024
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016025 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016026 {
16027 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016028 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016029 break;
16030 }
16031 li->li_tv.v_type = VAR_STRING;
16032 li->li_tv.v_lock = 0;
16033 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016034 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016035
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016036 start = p + 1; /* step over newline */
16037 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016038 break;
16039 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016040 else if (*p == NUL)
16041 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016042#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016043 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16044 * when finding the BF and check the previous two bytes. */
16045 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016046 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016047 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16048 * + 1, these may be in the "prev" string. */
16049 char_u back1 = p >= buf + 1 ? p[-1]
16050 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16051 char_u back2 = p >= buf + 2 ? p[-2]
16052 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16053 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016054
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016055 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016056 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016057 char_u *dest = p - 2;
16058
16059 /* Usually a BOM is at the beginning of a file, and so at
16060 * the beginning of a line; then we can just step over it.
16061 */
16062 if (start == dest)
16063 start = p + 1;
16064 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016065 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016066 /* have to shuffle buf to close gap */
16067 int adjust_prevlen = 0;
16068
16069 if (dest < buf)
16070 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016071 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016072 dest = buf;
16073 }
16074 if (readlen > p - buf + 1)
16075 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16076 readlen -= 3 - adjust_prevlen;
16077 prevlen -= adjust_prevlen;
16078 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016079 }
16080 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016081 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016082#endif
16083 } /* for */
16084
16085 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16086 break;
16087 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016088 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016089 /* There's part of a line in buf, store it in "prev". */
16090 if (p - start + prevlen >= prevsize)
16091 {
16092 /* need bigger "prev" buffer */
16093 char_u *newprev;
16094
16095 /* A common use case is ordinary text files and "prev" gets a
16096 * fragment of a line, so the first allocation is made
16097 * small, to avoid repeatedly 'allocing' large and
16098 * 'reallocing' small. */
16099 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016100 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016101 else
16102 {
16103 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016104 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016105 prevsize = grow50pc > growmin ? grow50pc : growmin;
16106 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016107 newprev = prev == NULL ? alloc(prevsize)
16108 : vim_realloc(prev, prevsize);
16109 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016110 {
16111 do_outofmem_msg((long_u)prevsize);
16112 failed = TRUE;
16113 break;
16114 }
16115 prev = newprev;
16116 }
16117 /* Add the line part to end of "prev". */
16118 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016119 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016120 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016121 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016122
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016123 /*
16124 * For a negative line count use only the lines at the end of the file,
16125 * free the rest.
16126 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016127 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016128 while (cnt > -maxline)
16129 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016130 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016131 --cnt;
16132 }
16133
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016134 if (failed)
16135 {
16136 list_free(rettv->vval.v_list, TRUE);
16137 /* readfile doc says an empty list is returned on error */
16138 rettv->vval.v_list = list_alloc();
16139 }
16140
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016141 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016142 fclose(fd);
16143}
16144
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016145#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016146static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016147
16148/*
16149 * Convert a List to proftime_T.
16150 * Return FAIL when there is something wrong.
16151 */
16152 static int
16153list2proftime(arg, tm)
16154 typval_T *arg;
16155 proftime_T *tm;
16156{
16157 long n1, n2;
16158 int error = FALSE;
16159
16160 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16161 || arg->vval.v_list->lv_len != 2)
16162 return FAIL;
16163 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16164 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16165# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016166 tm->HighPart = n1;
16167 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016168# else
16169 tm->tv_sec = n1;
16170 tm->tv_usec = n2;
16171# endif
16172 return error ? FAIL : OK;
16173}
16174#endif /* FEAT_RELTIME */
16175
16176/*
16177 * "reltime()" function
16178 */
16179 static void
16180f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016181 typval_T *argvars UNUSED;
16182 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016183{
16184#ifdef FEAT_RELTIME
16185 proftime_T res;
16186 proftime_T start;
16187
16188 if (argvars[0].v_type == VAR_UNKNOWN)
16189 {
16190 /* No arguments: get current time. */
16191 profile_start(&res);
16192 }
16193 else if (argvars[1].v_type == VAR_UNKNOWN)
16194 {
16195 if (list2proftime(&argvars[0], &res) == FAIL)
16196 return;
16197 profile_end(&res);
16198 }
16199 else
16200 {
16201 /* Two arguments: compute the difference. */
16202 if (list2proftime(&argvars[0], &start) == FAIL
16203 || list2proftime(&argvars[1], &res) == FAIL)
16204 return;
16205 profile_sub(&res, &start);
16206 }
16207
16208 if (rettv_list_alloc(rettv) == OK)
16209 {
16210 long n1, n2;
16211
16212# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016213 n1 = res.HighPart;
16214 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016215# else
16216 n1 = res.tv_sec;
16217 n2 = res.tv_usec;
16218# endif
16219 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16220 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16221 }
16222#endif
16223}
16224
16225/*
16226 * "reltimestr()" function
16227 */
16228 static void
16229f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016230 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016231 typval_T *rettv;
16232{
16233#ifdef FEAT_RELTIME
16234 proftime_T tm;
16235#endif
16236
16237 rettv->v_type = VAR_STRING;
16238 rettv->vval.v_string = NULL;
16239#ifdef FEAT_RELTIME
16240 if (list2proftime(&argvars[0], &tm) == OK)
16241 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16242#endif
16243}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016244
Bram Moolenaar0d660222005-01-07 21:51:51 +000016245#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016246static void make_connection(void);
16247static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016248
16249 static void
16250make_connection()
16251{
16252 if (X_DISPLAY == NULL
16253# ifdef FEAT_GUI
16254 && !gui.in_use
16255# endif
16256 )
16257 {
16258 x_force_connect = TRUE;
16259 setup_term_clip();
16260 x_force_connect = FALSE;
16261 }
16262}
16263
16264 static int
16265check_connection()
16266{
16267 make_connection();
16268 if (X_DISPLAY == NULL)
16269 {
16270 EMSG(_("E240: No connection to Vim server"));
16271 return FAIL;
16272 }
16273 return OK;
16274}
16275#endif
16276
16277#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016278static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016279
16280 static void
16281remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000016282 typval_T *argvars;
16283 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016284 int expr;
16285{
16286 char_u *server_name;
16287 char_u *keys;
16288 char_u *r = NULL;
16289 char_u buf[NUMBUFLEN];
16290# ifdef WIN32
16291 HWND w;
16292# else
16293 Window w;
16294# endif
16295
16296 if (check_restricted() || check_secure())
16297 return;
16298
16299# ifdef FEAT_X11
16300 if (check_connection() == FAIL)
16301 return;
16302# endif
16303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016304 server_name = get_tv_string_chk(&argvars[0]);
16305 if (server_name == NULL)
16306 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016307 keys = get_tv_string_buf(&argvars[1], buf);
16308# ifdef WIN32
16309 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16310# else
16311 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16312 < 0)
16313# endif
16314 {
16315 if (r != NULL)
16316 EMSG(r); /* sending worked but evaluation failed */
16317 else
16318 EMSG2(_("E241: Unable to send to %s"), server_name);
16319 return;
16320 }
16321
16322 rettv->vval.v_string = r;
16323
16324 if (argvars[2].v_type != VAR_UNKNOWN)
16325 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016326 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016327 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016328 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016329
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016330 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016331 v.di_tv.v_type = VAR_STRING;
16332 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016333 idvar = get_tv_string_chk(&argvars[2]);
16334 if (idvar != NULL)
16335 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016336 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337 }
16338}
16339#endif
16340
16341/*
16342 * "remote_expr()" function
16343 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344 static void
16345f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016346 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016347 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016348{
16349 rettv->v_type = VAR_STRING;
16350 rettv->vval.v_string = NULL;
16351#ifdef FEAT_CLIENTSERVER
16352 remote_common(argvars, rettv, TRUE);
16353#endif
16354}
16355
16356/*
16357 * "remote_foreground()" function
16358 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016359 static void
16360f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016361 typval_T *argvars UNUSED;
16362 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016364#ifdef FEAT_CLIENTSERVER
16365# ifdef WIN32
16366 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016367 {
16368 char_u *server_name = get_tv_string_chk(&argvars[0]);
16369
16370 if (server_name != NULL)
16371 serverForeground(server_name);
16372 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016373# else
16374 /* Send a foreground() expression to the server. */
16375 argvars[1].v_type = VAR_STRING;
16376 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16377 argvars[2].v_type = VAR_UNKNOWN;
16378 remote_common(argvars, rettv, TRUE);
16379 vim_free(argvars[1].vval.v_string);
16380# endif
16381#endif
16382}
16383
Bram Moolenaar0d660222005-01-07 21:51:51 +000016384 static void
16385f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016386 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016387 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016388{
16389#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016390 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391 char_u *s = NULL;
16392# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016393 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016394# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016395 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016396
16397 if (check_restricted() || check_secure())
16398 {
16399 rettv->vval.v_number = -1;
16400 return;
16401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016402 serverid = get_tv_string_chk(&argvars[0]);
16403 if (serverid == NULL)
16404 {
16405 rettv->vval.v_number = -1;
16406 return; /* type error; errmsg already given */
16407 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016408# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016409 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410 if (n == 0)
16411 rettv->vval.v_number = -1;
16412 else
16413 {
16414 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16415 rettv->vval.v_number = (s != NULL);
16416 }
16417# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016418 if (check_connection() == FAIL)
16419 return;
16420
16421 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016422 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016423# endif
16424
16425 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016427 char_u *retvar;
16428
Bram Moolenaar33570922005-01-25 22:26:29 +000016429 v.di_tv.v_type = VAR_STRING;
16430 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016431 retvar = get_tv_string_chk(&argvars[1]);
16432 if (retvar != NULL)
16433 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016434 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016435 }
16436#else
16437 rettv->vval.v_number = -1;
16438#endif
16439}
16440
Bram Moolenaar0d660222005-01-07 21:51:51 +000016441 static void
16442f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016443 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016444 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016445{
16446 char_u *r = NULL;
16447
16448#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016449 char_u *serverid = get_tv_string_chk(&argvars[0]);
16450
16451 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016452 {
16453# ifdef WIN32
16454 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016455 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016456
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016457 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458 if (n != 0)
16459 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16460 if (r == NULL)
16461# else
16462 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016463 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464# endif
16465 EMSG(_("E277: Unable to read a server reply"));
16466 }
16467#endif
16468 rettv->v_type = VAR_STRING;
16469 rettv->vval.v_string = r;
16470}
16471
16472/*
16473 * "remote_send()" function
16474 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016475 static void
16476f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016477 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016478 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016479{
16480 rettv->v_type = VAR_STRING;
16481 rettv->vval.v_string = NULL;
16482#ifdef FEAT_CLIENTSERVER
16483 remote_common(argvars, rettv, FALSE);
16484#endif
16485}
16486
16487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016488 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016489 */
16490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016491f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016492 typval_T *argvars;
16493 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016494{
Bram Moolenaar33570922005-01-25 22:26:29 +000016495 list_T *l;
16496 listitem_T *item, *item2;
16497 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016498 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016499 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016500 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016501 dict_T *d;
16502 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016503 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016504
Bram Moolenaar8c711452005-01-14 21:53:12 +000016505 if (argvars[0].v_type == VAR_DICT)
16506 {
16507 if (argvars[2].v_type != VAR_UNKNOWN)
16508 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016509 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016510 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016512 key = get_tv_string_chk(&argvars[1]);
16513 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016515 di = dict_find(d, key, -1);
16516 if (di == NULL)
16517 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016518 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16519 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 {
16521 *rettv = di->di_tv;
16522 init_tv(&di->di_tv);
16523 dictitem_remove(d, di);
16524 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016525 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016526 }
16527 }
16528 else if (argvars[0].v_type != VAR_LIST)
16529 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016530 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016531 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016533 int error = FALSE;
16534
16535 idx = get_tv_number_chk(&argvars[1], &error);
16536 if (error)
16537 ; /* type error: do nothing, errmsg already given */
16538 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016539 EMSGN(_(e_listidx), idx);
16540 else
16541 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016542 if (argvars[2].v_type == VAR_UNKNOWN)
16543 {
16544 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016545 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016546 *rettv = item->li_tv;
16547 vim_free(item);
16548 }
16549 else
16550 {
16551 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016552 end = get_tv_number_chk(&argvars[2], &error);
16553 if (error)
16554 ; /* type error: do nothing */
16555 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016556 EMSGN(_(e_listidx), end);
16557 else
16558 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016559 int cnt = 0;
16560
16561 for (li = item; li != NULL; li = li->li_next)
16562 {
16563 ++cnt;
16564 if (li == item2)
16565 break;
16566 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016567 if (li == NULL) /* didn't find "item2" after "item" */
16568 EMSG(_(e_invrange));
16569 else
16570 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016571 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016572 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016573 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016574 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016575 l->lv_first = item;
16576 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016577 item->li_prev = NULL;
16578 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016579 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016580 }
16581 }
16582 }
16583 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016584 }
16585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586}
16587
16588/*
16589 * "rename({from}, {to})" function
16590 */
16591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016592f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016593 typval_T *argvars;
16594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016595{
16596 char_u buf[NUMBUFLEN];
16597
16598 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016599 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016600 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016601 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16602 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016603}
16604
16605/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016606 * "repeat()" function
16607 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016608 static void
16609f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016610 typval_T *argvars;
16611 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016612{
16613 char_u *p;
16614 int n;
16615 int slen;
16616 int len;
16617 char_u *r;
16618 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016619
16620 n = get_tv_number(&argvars[1]);
16621 if (argvars[0].v_type == VAR_LIST)
16622 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016623 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016624 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016625 if (list_extend(rettv->vval.v_list,
16626 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016627 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016628 }
16629 else
16630 {
16631 p = get_tv_string(&argvars[0]);
16632 rettv->v_type = VAR_STRING;
16633 rettv->vval.v_string = NULL;
16634
16635 slen = (int)STRLEN(p);
16636 len = slen * n;
16637 if (len <= 0)
16638 return;
16639
16640 r = alloc(len + 1);
16641 if (r != NULL)
16642 {
16643 for (i = 0; i < n; i++)
16644 mch_memmove(r + i * slen, p, (size_t)slen);
16645 r[len] = NUL;
16646 }
16647
16648 rettv->vval.v_string = r;
16649 }
16650}
16651
16652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653 * "resolve()" function
16654 */
16655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016656f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016657 typval_T *argvars;
16658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659{
16660 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016661#ifdef HAVE_READLINK
16662 char_u *buf = NULL;
16663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016665 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666#ifdef FEAT_SHORTCUT
16667 {
16668 char_u *v = NULL;
16669
16670 v = mch_resolve_shortcut(p);
16671 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016672 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016674 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675 }
16676#else
16677# ifdef HAVE_READLINK
16678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 char_u *cpy;
16680 int len;
16681 char_u *remain = NULL;
16682 char_u *q;
16683 int is_relative_to_current = FALSE;
16684 int has_trailing_pathsep = FALSE;
16685 int limit = 100;
16686
16687 p = vim_strsave(p);
16688
16689 if (p[0] == '.' && (vim_ispathsep(p[1])
16690 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16691 is_relative_to_current = TRUE;
16692
16693 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016694 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016695 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016697 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016699
16700 q = getnextcomp(p);
16701 if (*q != NUL)
16702 {
16703 /* Separate the first path component in "p", and keep the
16704 * remainder (beginning with the path separator). */
16705 remain = vim_strsave(q - 1);
16706 q[-1] = NUL;
16707 }
16708
Bram Moolenaard9462e32011-04-11 21:35:11 +020016709 buf = alloc(MAXPATHL + 1);
16710 if (buf == NULL)
16711 goto fail;
16712
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713 for (;;)
16714 {
16715 for (;;)
16716 {
16717 len = readlink((char *)p, (char *)buf, MAXPATHL);
16718 if (len <= 0)
16719 break;
16720 buf[len] = NUL;
16721
16722 if (limit-- == 0)
16723 {
16724 vim_free(p);
16725 vim_free(remain);
16726 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016727 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 goto fail;
16729 }
16730
16731 /* Ensure that the result will have a trailing path separator
16732 * if the argument has one. */
16733 if (remain == NULL && has_trailing_pathsep)
16734 add_pathsep(buf);
16735
16736 /* Separate the first path component in the link value and
16737 * concatenate the remainders. */
16738 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16739 if (*q != NUL)
16740 {
16741 if (remain == NULL)
16742 remain = vim_strsave(q - 1);
16743 else
16744 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016745 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 if (cpy != NULL)
16747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 vim_free(remain);
16749 remain = cpy;
16750 }
16751 }
16752 q[-1] = NUL;
16753 }
16754
16755 q = gettail(p);
16756 if (q > p && *q == NUL)
16757 {
16758 /* Ignore trailing path separator. */
16759 q[-1] = NUL;
16760 q = gettail(p);
16761 }
16762 if (q > p && !mch_isFullName(buf))
16763 {
16764 /* symlink is relative to directory of argument */
16765 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16766 if (cpy != NULL)
16767 {
16768 STRCPY(cpy, p);
16769 STRCPY(gettail(cpy), buf);
16770 vim_free(p);
16771 p = cpy;
16772 }
16773 }
16774 else
16775 {
16776 vim_free(p);
16777 p = vim_strsave(buf);
16778 }
16779 }
16780
16781 if (remain == NULL)
16782 break;
16783
16784 /* Append the first path component of "remain" to "p". */
16785 q = getnextcomp(remain + 1);
16786 len = q - remain - (*q != NUL);
16787 cpy = vim_strnsave(p, STRLEN(p) + len);
16788 if (cpy != NULL)
16789 {
16790 STRNCAT(cpy, remain, len);
16791 vim_free(p);
16792 p = cpy;
16793 }
16794 /* Shorten "remain". */
16795 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016796 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 else
16798 {
16799 vim_free(remain);
16800 remain = NULL;
16801 }
16802 }
16803
16804 /* If the result is a relative path name, make it explicitly relative to
16805 * the current directory if and only if the argument had this form. */
16806 if (!vim_ispathsep(*p))
16807 {
16808 if (is_relative_to_current
16809 && *p != NUL
16810 && !(p[0] == '.'
16811 && (p[1] == NUL
16812 || vim_ispathsep(p[1])
16813 || (p[1] == '.'
16814 && (p[2] == NUL
16815 || vim_ispathsep(p[2]))))))
16816 {
16817 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016818 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 if (cpy != NULL)
16820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 vim_free(p);
16822 p = cpy;
16823 }
16824 }
16825 else if (!is_relative_to_current)
16826 {
16827 /* Strip leading "./". */
16828 q = p;
16829 while (q[0] == '.' && vim_ispathsep(q[1]))
16830 q += 2;
16831 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016832 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 }
16834 }
16835
16836 /* Ensure that the result will have no trailing path separator
16837 * if the argument had none. But keep "/" or "//". */
16838 if (!has_trailing_pathsep)
16839 {
16840 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016841 if (after_pathsep(p, q))
16842 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 }
16844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016845 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016846 }
16847# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016848 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849# endif
16850#endif
16851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016852 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853
16854#ifdef HAVE_READLINK
16855fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016856 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016857#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016858 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859}
16860
16861/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016862 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 */
16864 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016866 typval_T *argvars;
16867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868{
Bram Moolenaar33570922005-01-25 22:26:29 +000016869 list_T *l;
16870 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871
Bram Moolenaar0d660222005-01-07 21:51:51 +000016872 if (argvars[0].v_type != VAR_LIST)
16873 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016874 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016875 && !tv_check_lock(l->lv_lock,
16876 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877 {
16878 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016879 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016880 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016881 while (li != NULL)
16882 {
16883 ni = li->li_prev;
16884 list_append(l, li);
16885 li = ni;
16886 }
16887 rettv->vval.v_list = l;
16888 rettv->v_type = VAR_LIST;
16889 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016890 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892}
16893
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016894#define SP_NOMOVE 0x01 /* don't move cursor */
16895#define SP_REPEAT 0x02 /* repeat to find outer pair */
16896#define SP_RETCOUNT 0x04 /* return matchcount */
16897#define SP_SETPCMARK 0x08 /* set previous context mark */
16898#define SP_START 0x10 /* accept match at start position */
16899#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16900#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016901#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016902
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016903static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016904
16905/*
16906 * Get flags for a search function.
16907 * Possibly sets "p_ws".
16908 * Returns BACKWARD, FORWARD or zero (for an error).
16909 */
16910 static int
16911get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016912 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913 int *flagsp;
16914{
16915 int dir = FORWARD;
16916 char_u *flags;
16917 char_u nbuf[NUMBUFLEN];
16918 int mask;
16919
16920 if (varp->v_type != VAR_UNKNOWN)
16921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016922 flags = get_tv_string_buf_chk(varp, nbuf);
16923 if (flags == NULL)
16924 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925 while (*flags != NUL)
16926 {
16927 switch (*flags)
16928 {
16929 case 'b': dir = BACKWARD; break;
16930 case 'w': p_ws = TRUE; break;
16931 case 'W': p_ws = FALSE; break;
16932 default: mask = 0;
16933 if (flagsp != NULL)
16934 switch (*flags)
16935 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016936 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016937 case 'e': mask = SP_END; break;
16938 case 'm': mask = SP_RETCOUNT; break;
16939 case 'n': mask = SP_NOMOVE; break;
16940 case 'p': mask = SP_SUBPAT; break;
16941 case 'r': mask = SP_REPEAT; break;
16942 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016943 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944 }
16945 if (mask == 0)
16946 {
16947 EMSG2(_(e_invarg2), flags);
16948 dir = 0;
16949 }
16950 else
16951 *flagsp |= mask;
16952 }
16953 if (dir == 0)
16954 break;
16955 ++flags;
16956 }
16957 }
16958 return dir;
16959}
16960
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016962 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016964 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016965search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016966 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016967 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016968 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016970 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971 char_u *pat;
16972 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016973 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 int save_p_ws = p_ws;
16975 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016976 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016977 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016978 proftime_T tm;
16979#ifdef FEAT_RELTIME
16980 long time_limit = 0;
16981#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016982 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016983 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016985 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016986 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016987 if (dir == 0)
16988 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016989 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016990 if (flags & SP_START)
16991 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016992 if (flags & SP_END)
16993 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016994 if (flags & SP_COLUMN)
16995 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016996
Bram Moolenaar76929292008-01-06 19:07:36 +000016997 /* Optional arguments: line number to stop searching and timeout. */
16998 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016999 {
17000 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17001 if (lnum_stop < 0)
17002 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017003#ifdef FEAT_RELTIME
17004 if (argvars[3].v_type != VAR_UNKNOWN)
17005 {
17006 time_limit = get_tv_number_chk(&argvars[3], NULL);
17007 if (time_limit < 0)
17008 goto theend;
17009 }
17010#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017011 }
17012
Bram Moolenaar76929292008-01-06 19:07:36 +000017013#ifdef FEAT_RELTIME
17014 /* Set the time limit, if there is one. */
17015 profile_setlimit(time_limit, &tm);
17016#endif
17017
Bram Moolenaar231334e2005-07-25 20:46:57 +000017018 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017019 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017020 * Check to make sure only those flags are set.
17021 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17022 * flags cannot be set. Check for that condition also.
17023 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017024 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017025 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017027 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017028 goto theend;
17029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017031 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017032 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017033 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017034 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017036 if (flags & SP_SUBPAT)
17037 retval = subpatnum;
17038 else
17039 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017040 if (flags & SP_SETPCMARK)
17041 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017043 if (match_pos != NULL)
17044 {
17045 /* Store the match cursor position */
17046 match_pos->lnum = pos.lnum;
17047 match_pos->col = pos.col + 1;
17048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049 /* "/$" will put the cursor after the end of the line, may need to
17050 * correct that here */
17051 check_cursor();
17052 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017053
17054 /* If 'n' flag is used: restore cursor position. */
17055 if (flags & SP_NOMOVE)
17056 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017057 else
17058 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017059theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017061
17062 return retval;
17063}
17064
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017065#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017066
17067/*
17068 * round() is not in C90, use ceil() or floor() instead.
17069 */
17070 float_T
17071vim_round(f)
17072 float_T f;
17073{
17074 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17075}
17076
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017077/*
17078 * "round({float})" function
17079 */
17080 static void
17081f_round(argvars, rettv)
17082 typval_T *argvars;
17083 typval_T *rettv;
17084{
17085 float_T f;
17086
17087 rettv->v_type = VAR_FLOAT;
17088 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017089 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017090 else
17091 rettv->vval.v_float = 0.0;
17092}
17093#endif
17094
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017095/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017096 * "screenattr()" function
17097 */
17098 static void
17099f_screenattr(argvars, rettv)
17100 typval_T *argvars UNUSED;
17101 typval_T *rettv;
17102{
17103 int row;
17104 int col;
17105 int c;
17106
17107 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17108 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17109 if (row < 0 || row >= screen_Rows
17110 || col < 0 || col >= screen_Columns)
17111 c = -1;
17112 else
17113 c = ScreenAttrs[LineOffset[row] + col];
17114 rettv->vval.v_number = c;
17115}
17116
17117/*
17118 * "screenchar()" function
17119 */
17120 static void
17121f_screenchar(argvars, rettv)
17122 typval_T *argvars UNUSED;
17123 typval_T *rettv;
17124{
17125 int row;
17126 int col;
17127 int off;
17128 int c;
17129
17130 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17131 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17132 if (row < 0 || row >= screen_Rows
17133 || col < 0 || col >= screen_Columns)
17134 c = -1;
17135 else
17136 {
17137 off = LineOffset[row] + col;
17138#ifdef FEAT_MBYTE
17139 if (enc_utf8 && ScreenLinesUC[off] != 0)
17140 c = ScreenLinesUC[off];
17141 else
17142#endif
17143 c = ScreenLines[off];
17144 }
17145 rettv->vval.v_number = c;
17146}
17147
17148/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017149 * "screencol()" function
17150 *
17151 * First column is 1 to be consistent with virtcol().
17152 */
17153 static void
17154f_screencol(argvars, rettv)
17155 typval_T *argvars UNUSED;
17156 typval_T *rettv;
17157{
17158 rettv->vval.v_number = screen_screencol() + 1;
17159}
17160
17161/*
17162 * "screenrow()" function
17163 */
17164 static void
17165f_screenrow(argvars, rettv)
17166 typval_T *argvars UNUSED;
17167 typval_T *rettv;
17168{
17169 rettv->vval.v_number = screen_screenrow() + 1;
17170}
17171
17172/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017173 * "search()" function
17174 */
17175 static void
17176f_search(argvars, rettv)
17177 typval_T *argvars;
17178 typval_T *rettv;
17179{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017180 int flags = 0;
17181
17182 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183}
17184
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017186 * "searchdecl()" function
17187 */
17188 static void
17189f_searchdecl(argvars, rettv)
17190 typval_T *argvars;
17191 typval_T *rettv;
17192{
17193 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017194 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017195 int error = FALSE;
17196 char_u *name;
17197
17198 rettv->vval.v_number = 1; /* default: FAIL */
17199
17200 name = get_tv_string_chk(&argvars[0]);
17201 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017202 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017203 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017204 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17205 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17206 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017207 if (!error && name != NULL)
17208 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017209 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017210}
17211
17212/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017213 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017215 static int
17216searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000017217 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017218 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017219{
17220 char_u *spat, *mpat, *epat;
17221 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017223 int dir;
17224 int flags = 0;
17225 char_u nbuf1[NUMBUFLEN];
17226 char_u nbuf2[NUMBUFLEN];
17227 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017228 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017229 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017230 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017233 spat = get_tv_string_chk(&argvars[0]);
17234 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17235 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17236 if (spat == NULL || mpat == NULL || epat == NULL)
17237 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239 /* Handle the optional fourth argument: flags */
17240 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017241 if (dir == 0)
17242 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017243
17244 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017245 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17246 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017247 if ((flags & (SP_END | SP_SUBPAT)) != 0
17248 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017249 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017250 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017251 goto theend;
17252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017254 /* Using 'r' implies 'W', otherwise it doesn't work. */
17255 if (flags & SP_REPEAT)
17256 p_ws = FALSE;
17257
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017258 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017259 if (argvars[3].v_type == VAR_UNKNOWN
17260 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017261 skip = (char_u *)"";
17262 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017264 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017265 if (argvars[5].v_type != VAR_UNKNOWN)
17266 {
17267 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17268 if (lnum_stop < 0)
17269 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017270#ifdef FEAT_RELTIME
17271 if (argvars[6].v_type != VAR_UNKNOWN)
17272 {
17273 time_limit = get_tv_number_chk(&argvars[6], NULL);
17274 if (time_limit < 0)
17275 goto theend;
17276 }
17277#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017278 }
17279 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017280 if (skip == NULL)
17281 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017283 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017284 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017285
17286theend:
17287 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017288
17289 return retval;
17290}
17291
17292/*
17293 * "searchpair()" function
17294 */
17295 static void
17296f_searchpair(argvars, rettv)
17297 typval_T *argvars;
17298 typval_T *rettv;
17299{
17300 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17301}
17302
17303/*
17304 * "searchpairpos()" function
17305 */
17306 static void
17307f_searchpairpos(argvars, rettv)
17308 typval_T *argvars;
17309 typval_T *rettv;
17310{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017311 pos_T match_pos;
17312 int lnum = 0;
17313 int col = 0;
17314
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017315 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017316 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017317
17318 if (searchpair_cmn(argvars, &match_pos) > 0)
17319 {
17320 lnum = match_pos.lnum;
17321 col = match_pos.col;
17322 }
17323
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017324 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17325 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017326}
17327
17328/*
17329 * Search for a start/middle/end thing.
17330 * Used by searchpair(), see its documentation for the details.
17331 * Returns 0 or -1 for no match,
17332 */
17333 long
Bram Moolenaar76929292008-01-06 19:07:36 +000017334do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
17335 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017336 char_u *spat; /* start pattern */
17337 char_u *mpat; /* middle pattern */
17338 char_u *epat; /* end pattern */
17339 int dir; /* BACKWARD or FORWARD */
17340 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017341 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017342 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017343 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017344 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017345{
17346 char_u *save_cpo;
17347 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17348 long retval = 0;
17349 pos_T pos;
17350 pos_T firstpos;
17351 pos_T foundpos;
17352 pos_T save_cursor;
17353 pos_T save_pos;
17354 int n;
17355 int r;
17356 int nest = 1;
17357 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017358 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017359 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017360
17361 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17362 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017363 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017364
Bram Moolenaar76929292008-01-06 19:07:36 +000017365#ifdef FEAT_RELTIME
17366 /* Set the time limit, if there is one. */
17367 profile_setlimit(time_limit, &tm);
17368#endif
17369
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017370 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17371 * start/middle/end (pat3, for the top pair). */
17372 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17373 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17374 if (pat2 == NULL || pat3 == NULL)
17375 goto theend;
17376 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17377 if (*mpat == NUL)
17378 STRCPY(pat3, pat2);
17379 else
17380 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17381 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017382 if (flags & SP_START)
17383 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017384
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 save_cursor = curwin->w_cursor;
17386 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017387 clearpos(&firstpos);
17388 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 pat = pat3;
17390 for (;;)
17391 {
17392 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017393 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17395 /* didn't find it or found the first match again: FAIL */
17396 break;
17397
17398 if (firstpos.lnum == 0)
17399 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017400 if (equalpos(pos, foundpos))
17401 {
17402 /* Found the same position again. Can happen with a pattern that
17403 * has "\zs" at the end and searching backwards. Advance one
17404 * character and try again. */
17405 if (dir == BACKWARD)
17406 decl(&pos);
17407 else
17408 incl(&pos);
17409 }
17410 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017412 /* clear the start flag to avoid getting stuck here */
17413 options &= ~SEARCH_START;
17414
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415 /* If the skip pattern matches, ignore this match. */
17416 if (*skip != NUL)
17417 {
17418 save_pos = curwin->w_cursor;
17419 curwin->w_cursor = pos;
17420 r = eval_to_bool(skip, &err, NULL, FALSE);
17421 curwin->w_cursor = save_pos;
17422 if (err)
17423 {
17424 /* Evaluating {skip} caused an error, break here. */
17425 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017426 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 break;
17428 }
17429 if (r)
17430 continue;
17431 }
17432
17433 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17434 {
17435 /* Found end when searching backwards or start when searching
17436 * forward: nested pair. */
17437 ++nest;
17438 pat = pat2; /* nested, don't search for middle */
17439 }
17440 else
17441 {
17442 /* Found end when searching forward or start when searching
17443 * backward: end of (nested) pair; or found middle in outer pair. */
17444 if (--nest == 1)
17445 pat = pat3; /* outer level, search for middle */
17446 }
17447
17448 if (nest == 0)
17449 {
17450 /* Found the match: return matchcount or line number. */
17451 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017452 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017454 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017455 if (flags & SP_SETPCMARK)
17456 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 curwin->w_cursor = pos;
17458 if (!(flags & SP_REPEAT))
17459 break;
17460 nest = 1; /* search for next unmatched */
17461 }
17462 }
17463
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017464 if (match_pos != NULL)
17465 {
17466 /* Store the match cursor position */
17467 match_pos->lnum = curwin->w_cursor.lnum;
17468 match_pos->col = curwin->w_cursor.col + 1;
17469 }
17470
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017472 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 curwin->w_cursor = save_cursor;
17474
17475theend:
17476 vim_free(pat2);
17477 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017478 if (p_cpo == empty_option)
17479 p_cpo = save_cpo;
17480 else
17481 /* Darn, evaluating the {skip} expression changed the value. */
17482 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017483
17484 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485}
17486
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017487/*
17488 * "searchpos()" function
17489 */
17490 static void
17491f_searchpos(argvars, rettv)
17492 typval_T *argvars;
17493 typval_T *rettv;
17494{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017495 pos_T match_pos;
17496 int lnum = 0;
17497 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017498 int n;
17499 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017500
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017501 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017502 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017503
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017504 n = search_cmn(argvars, &match_pos, &flags);
17505 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017506 {
17507 lnum = match_pos.lnum;
17508 col = match_pos.col;
17509 }
17510
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017511 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17512 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017513 if (flags & SP_SUBPAT)
17514 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017515}
17516
Bram Moolenaar3b5f9292016-01-28 22:37:01 +010017517#ifdef FEAT_CHANNEL
17518/*
17519 * common for "sendexpr()" and "sendraw()"
17520 * Returns the channel index if the caller should read the response.
17521 * Otherwise returns -1.
17522 */
17523 static int
17524send_common(typval_T *argvars, char_u *text, char *fun)
17525{
17526 int ch_idx;
17527 char_u *callback = NULL;
17528
17529 ch_idx = get_channel_arg(&argvars[0]);
17530 if (ch_idx < 0)
17531 return -1;
17532
17533 if (argvars[2].v_type != VAR_UNKNOWN)
17534 {
17535 callback = get_callback(&argvars[2]);
17536 if (callback == NULL)
17537 return -1;
17538 }
17539 /* Set the callback or clear it. An empty callback means no callback and
17540 * not reading the response. */
17541 channel_set_req_callback(ch_idx,
17542 callback != NULL && *callback == NUL ? NULL : callback);
17543 if (callback == NULL)
17544 channel_will_block(ch_idx);
17545
17546 if (channel_send(ch_idx, text, fun) == OK && callback == NULL)
17547 return ch_idx;
17548 return -1;
17549}
17550
17551/*
17552 * "sendexpr()" function
17553 */
17554 static void
17555f_sendexpr(argvars, rettv)
17556 typval_T *argvars;
17557 typval_T *rettv;
17558{
17559 char_u *text;
17560 char_u *resp;
17561 typval_T nrtv;
17562 typval_T listtv;
17563 int ch_idx;
17564
17565 /* return an empty string by default */
17566 rettv->v_type = VAR_STRING;
17567 rettv->vval.v_string = NULL;
17568
17569 nrtv.v_type = VAR_NUMBER;
17570 nrtv.vval.v_number = channel_get_id();
17571 if (rettv_list_alloc(&listtv) == FAIL)
17572 return;
17573 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
17574 || list_append_tv(listtv.vval.v_list, &argvars[1]) == FAIL)
17575 {
17576 list_unref(listtv.vval.v_list);
17577 return;
17578 }
17579
17580 text = json_encode(&listtv);
17581 list_unref(listtv.vval.v_list);
17582
17583 ch_idx = send_common(argvars, text, "sendexpr");
17584 if (ch_idx >= 0)
17585 {
17586 /* TODO: read until the whole JSON message is received */
17587 /* TODO: only use the message with the right message ID */
17588 resp = channel_read_block(ch_idx);
17589 if (resp != NULL)
17590 {
17591 channel_decode_json(resp, rettv);
17592 vim_free(resp);
17593 }
17594 }
17595}
17596
17597/*
17598 * "sendraw()" function
17599 */
17600 static void
17601f_sendraw(argvars, rettv)
17602 typval_T *argvars;
17603 typval_T *rettv;
17604{
17605 char_u buf[NUMBUFLEN];
17606 char_u *text;
17607 int ch_idx;
17608
17609 /* return an empty string by default */
17610 rettv->v_type = VAR_STRING;
17611 rettv->vval.v_string = NULL;
17612
17613 text = get_tv_string_buf(&argvars[1], buf);
17614 ch_idx = send_common(argvars, text, "sendraw");
17615 if (ch_idx >= 0)
17616 rettv->vval.v_string = channel_read_block(ch_idx);
17617}
17618#endif
17619
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017620
Bram Moolenaar0d660222005-01-07 21:51:51 +000017621 static void
17622f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017623 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017626#ifdef FEAT_CLIENTSERVER
17627 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017628 char_u *server = get_tv_string_chk(&argvars[0]);
17629 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630
Bram Moolenaar0d660222005-01-07 21:51:51 +000017631 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017632 if (server == NULL || reply == NULL)
17633 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634 if (check_restricted() || check_secure())
17635 return;
17636# ifdef FEAT_X11
17637 if (check_connection() == FAIL)
17638 return;
17639# endif
17640
17641 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017643 EMSG(_("E258: Unable to send to client"));
17644 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017646 rettv->vval.v_number = 0;
17647#else
17648 rettv->vval.v_number = -1;
17649#endif
17650}
17651
Bram Moolenaar0d660222005-01-07 21:51:51 +000017652 static void
17653f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017654 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017655 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017656{
17657 char_u *r = NULL;
17658
17659#ifdef FEAT_CLIENTSERVER
17660# ifdef WIN32
17661 r = serverGetVimNames();
17662# else
17663 make_connection();
17664 if (X_DISPLAY != NULL)
17665 r = serverGetVimNames(X_DISPLAY);
17666# endif
17667#endif
17668 rettv->v_type = VAR_STRING;
17669 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670}
17671
17672/*
17673 * "setbufvar()" function
17674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017676f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017677 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017678 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679{
17680 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017683 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684 char_u nbuf[NUMBUFLEN];
17685
17686 if (check_restricted() || check_secure())
17687 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017688 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17689 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017690 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 varp = &argvars[2];
17692
17693 if (buf != NULL && varname != NULL && varp != NULL)
17694 {
17695 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017697
17698 if (*varname == '&')
17699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017700 long numval;
17701 char_u *strval;
17702 int error = FALSE;
17703
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017705 numval = get_tv_number_chk(varp, &error);
17706 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017707 if (!error && strval != NULL)
17708 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709 }
17710 else
17711 {
17712 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17713 if (bufvarname != NULL)
17714 {
17715 STRCPY(bufvarname, "b:");
17716 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017717 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 vim_free(bufvarname);
17719 }
17720 }
17721
17722 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725}
17726
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017727 static void
17728f_setcharsearch(argvars, rettv)
17729 typval_T *argvars;
17730 typval_T *rettv UNUSED;
17731{
17732 dict_T *d;
17733 dictitem_T *di;
17734 char_u *csearch;
17735
17736 if (argvars[0].v_type != VAR_DICT)
17737 {
17738 EMSG(_(e_dictreq));
17739 return;
17740 }
17741
17742 if ((d = argvars[0].vval.v_dict) != NULL)
17743 {
17744 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17745 if (csearch != NULL)
17746 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017747#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017748 if (enc_utf8)
17749 {
17750 int pcc[MAX_MCO];
17751 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017752
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017753 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17754 }
17755 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017756#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017757 set_last_csearch(PTR2CHAR(csearch),
17758 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017759 }
17760
17761 di = dict_find(d, (char_u *)"forward", -1);
17762 if (di != NULL)
17763 set_csearch_direction(get_tv_number(&di->di_tv)
17764 ? FORWARD : BACKWARD);
17765
17766 di = dict_find(d, (char_u *)"until", -1);
17767 if (di != NULL)
17768 set_csearch_until(!!get_tv_number(&di->di_tv));
17769 }
17770}
17771
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772/*
17773 * "setcmdpos()" function
17774 */
17775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017776f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017777 typval_T *argvars;
17778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017780 int pos = (int)get_tv_number(&argvars[0]) - 1;
17781
17782 if (pos >= 0)
17783 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784}
17785
17786/*
17787 * "setline()" function
17788 */
17789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017790f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017791 typval_T *argvars;
17792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793{
17794 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017795 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017796 list_T *l = NULL;
17797 listitem_T *li = NULL;
17798 long added = 0;
17799 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017801 lnum = get_tv_lnum(&argvars[0]);
17802 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017804 l = argvars[1].vval.v_list;
17805 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017807 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017808 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017809
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017810 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017811 for (;;)
17812 {
17813 if (l != NULL)
17814 {
17815 /* list argument, get next string */
17816 if (li == NULL)
17817 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017818 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017819 li = li->li_next;
17820 }
17821
17822 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017823 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017824 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017825
17826 /* When coming here from Insert mode, sync undo, so that this can be
17827 * undone separately from what was previously inserted. */
17828 if (u_sync_once == 2)
17829 {
17830 u_sync_once = 1; /* notify that u_sync() was called */
17831 u_sync(TRUE);
17832 }
17833
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017834 if (lnum <= curbuf->b_ml.ml_line_count)
17835 {
17836 /* existing line, replace it */
17837 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17838 {
17839 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017840 if (lnum == curwin->w_cursor.lnum)
17841 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017842 rettv->vval.v_number = 0; /* OK */
17843 }
17844 }
17845 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17846 {
17847 /* lnum is one past the last line, append the line */
17848 ++added;
17849 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17850 rettv->vval.v_number = 0; /* OK */
17851 }
17852
17853 if (l == NULL) /* only one string argument */
17854 break;
17855 ++lnum;
17856 }
17857
17858 if (added > 0)
17859 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860}
17861
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017862static 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 +000017863
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017865 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017866 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017867 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017868set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017869 win_T *wp UNUSED;
17870 typval_T *list_arg UNUSED;
17871 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017872 typval_T *rettv;
17873{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017874#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017875 char_u *act;
17876 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017877#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017878
Bram Moolenaar2641f772005-03-25 21:58:17 +000017879 rettv->vval.v_number = -1;
17880
17881#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017882 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017883 EMSG(_(e_listreq));
17884 else
17885 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017886 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017887
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017888 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017889 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017890 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017891 if (act == NULL)
17892 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017893 if (*act == 'a' || *act == 'r')
17894 action = *act;
17895 }
17896
Bram Moolenaar81484f42012-12-05 15:16:47 +010017897 if (l != NULL && set_errorlist(wp, l, action,
17898 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017899 rettv->vval.v_number = 0;
17900 }
17901#endif
17902}
17903
17904/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017905 * "setloclist()" function
17906 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017907 static void
17908f_setloclist(argvars, rettv)
17909 typval_T *argvars;
17910 typval_T *rettv;
17911{
17912 win_T *win;
17913
17914 rettv->vval.v_number = -1;
17915
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017916 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017917 if (win != NULL)
17918 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17919}
17920
17921/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017922 * "setmatches()" function
17923 */
17924 static void
17925f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010017926 typval_T *argvars UNUSED;
17927 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017928{
17929#ifdef FEAT_SEARCH_EXTRA
17930 list_T *l;
17931 listitem_T *li;
17932 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017933 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017934
17935 rettv->vval.v_number = -1;
17936 if (argvars[0].v_type != VAR_LIST)
17937 {
17938 EMSG(_(e_listreq));
17939 return;
17940 }
17941 if ((l = argvars[0].vval.v_list) != NULL)
17942 {
17943
17944 /* To some extent make sure that we are dealing with a list from
17945 * "getmatches()". */
17946 li = l->lv_first;
17947 while (li != NULL)
17948 {
17949 if (li->li_tv.v_type != VAR_DICT
17950 || (d = li->li_tv.vval.v_dict) == NULL)
17951 {
17952 EMSG(_(e_invarg));
17953 return;
17954 }
17955 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017956 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17957 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017958 && dict_find(d, (char_u *)"priority", -1) != NULL
17959 && dict_find(d, (char_u *)"id", -1) != NULL))
17960 {
17961 EMSG(_(e_invarg));
17962 return;
17963 }
17964 li = li->li_next;
17965 }
17966
17967 clear_matches(curwin);
17968 li = l->lv_first;
17969 while (li != NULL)
17970 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017971 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017972 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017973 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017974 char_u *group;
17975 int priority;
17976 int id;
17977 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017978
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017979 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017980 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17981 {
17982 if (s == NULL)
17983 {
17984 s = list_alloc();
17985 if (s == NULL)
17986 return;
17987 }
17988
17989 /* match from matchaddpos() */
17990 for (i = 1; i < 9; i++)
17991 {
17992 sprintf((char *)buf, (char *)"pos%d", i);
17993 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17994 {
17995 if (di->di_tv.v_type != VAR_LIST)
17996 return;
17997
17998 list_append_tv(s, &di->di_tv);
17999 s->lv_refcount++;
18000 }
18001 else
18002 break;
18003 }
18004 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018005
18006 group = get_dict_string(d, (char_u *)"group", FALSE);
18007 priority = (int)get_dict_number(d, (char_u *)"priority");
18008 id = (int)get_dict_number(d, (char_u *)"id");
18009 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18010 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18011 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018012 if (i == 0)
18013 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018014 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018015 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018016 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018017 }
18018 else
18019 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018020 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018021 list_unref(s);
18022 s = NULL;
18023 }
18024
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018025 li = li->li_next;
18026 }
18027 rettv->vval.v_number = 0;
18028 }
18029#endif
18030}
18031
18032/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018033 * "setpos()" function
18034 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018035 static void
18036f_setpos(argvars, rettv)
18037 typval_T *argvars;
18038 typval_T *rettv;
18039{
18040 pos_T pos;
18041 int fnum;
18042 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018043 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018044
Bram Moolenaar08250432008-02-13 11:42:46 +000018045 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018046 name = get_tv_string_chk(argvars);
18047 if (name != NULL)
18048 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018049 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018050 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018051 if (--pos.col < 0)
18052 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018053 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018054 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018055 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018056 if (fnum == curbuf->b_fnum)
18057 {
18058 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018059 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018060 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018061 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018062 curwin->w_set_curswant = FALSE;
18063 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018064 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018065 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018066 }
18067 else
18068 EMSG(_(e_invarg));
18069 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018070 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18071 {
18072 /* set mark */
18073 if (setmark_pos(name[1], &pos, fnum) == OK)
18074 rettv->vval.v_number = 0;
18075 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018076 else
18077 EMSG(_(e_invarg));
18078 }
18079 }
18080}
18081
18082/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018083 * "setqflist()" function
18084 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018085 static void
18086f_setqflist(argvars, rettv)
18087 typval_T *argvars;
18088 typval_T *rettv;
18089{
18090 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18091}
18092
18093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 * "setreg()" function
18095 */
18096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018097f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018098 typval_T *argvars;
18099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100{
18101 int regname;
18102 char_u *strregname;
18103 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018104 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105 int append;
18106 char_u yank_type;
18107 long block_len;
18108
18109 block_len = -1;
18110 yank_type = MAUTO;
18111 append = FALSE;
18112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018113 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018114 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018116 if (strregname == NULL)
18117 return; /* type error; errmsg already given */
18118 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 if (regname == 0 || regname == '@')
18120 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018122 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018124 stropt = get_tv_string_chk(&argvars[2]);
18125 if (stropt == NULL)
18126 return; /* type error */
18127 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 switch (*stropt)
18129 {
18130 case 'a': case 'A': /* append */
18131 append = TRUE;
18132 break;
18133 case 'v': case 'c': /* character-wise selection */
18134 yank_type = MCHAR;
18135 break;
18136 case 'V': case 'l': /* line-wise selection */
18137 yank_type = MLINE;
18138 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 case 'b': case Ctrl_V: /* block-wise selection */
18140 yank_type = MBLOCK;
18141 if (VIM_ISDIGIT(stropt[1]))
18142 {
18143 ++stropt;
18144 block_len = getdigits(&stropt) - 1;
18145 --stropt;
18146 }
18147 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 }
18149 }
18150
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018151 if (argvars[1].v_type == VAR_LIST)
18152 {
18153 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018154 char_u **allocval;
18155 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018156 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018157 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018158 int len = argvars[1].vval.v_list->lv_len;
18159 listitem_T *li;
18160
Bram Moolenaar7d647822014-04-05 21:28:56 +020018161 /* First half: use for pointers to result lines; second half: use for
18162 * pointers to allocated copies. */
18163 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018164 if (lstval == NULL)
18165 return;
18166 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018167 allocval = lstval + len + 2;
18168 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018169
18170 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18171 li = li->li_next)
18172 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018173 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018174 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018175 goto free_lstval;
18176 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018177 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018178 /* Need to make a copy, next get_tv_string_buf_chk() will
18179 * overwrite the string. */
18180 strval = vim_strsave(buf);
18181 if (strval == NULL)
18182 goto free_lstval;
18183 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018184 }
18185 *curval++ = strval;
18186 }
18187 *curval++ = NULL;
18188
18189 write_reg_contents_lst(regname, lstval, -1,
18190 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018191free_lstval:
18192 while (curallocval > allocval)
18193 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018194 vim_free(lstval);
18195 }
18196 else
18197 {
18198 strval = get_tv_string_chk(&argvars[1]);
18199 if (strval == NULL)
18200 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018201 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018204 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205}
18206
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018207/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018208 * "settabvar()" function
18209 */
18210 static void
18211f_settabvar(argvars, rettv)
18212 typval_T *argvars;
18213 typval_T *rettv;
18214{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018215#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018216 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018217 tabpage_T *tp;
18218#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018219 char_u *varname, *tabvarname;
18220 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018221
18222 rettv->vval.v_number = 0;
18223
18224 if (check_restricted() || check_secure())
18225 return;
18226
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018227#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018228 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018229#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018230 varname = get_tv_string_chk(&argvars[1]);
18231 varp = &argvars[2];
18232
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018233 if (varname != NULL && varp != NULL
18234#ifdef FEAT_WINDOWS
18235 && tp != NULL
18236#endif
18237 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018238 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018239#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018240 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018241 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018242#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018243
18244 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18245 if (tabvarname != NULL)
18246 {
18247 STRCPY(tabvarname, "t:");
18248 STRCPY(tabvarname + 2, varname);
18249 set_var(tabvarname, varp, TRUE);
18250 vim_free(tabvarname);
18251 }
18252
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018253#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018254 /* Restore current tabpage */
18255 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018256 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018257#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018258 }
18259}
18260
18261/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018262 * "settabwinvar()" function
18263 */
18264 static void
18265f_settabwinvar(argvars, rettv)
18266 typval_T *argvars;
18267 typval_T *rettv;
18268{
18269 setwinvar(argvars, rettv, 1);
18270}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271
18272/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018273 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018276f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018277 typval_T *argvars;
18278 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018280 setwinvar(argvars, rettv, 0);
18281}
18282
18283/*
18284 * "setwinvar()" and "settabwinvar()" functions
18285 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018286
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018287 static void
18288setwinvar(argvars, rettv, off)
18289 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018290 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018291 int off;
18292{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293 win_T *win;
18294#ifdef FEAT_WINDOWS
18295 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018296 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018297 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298#endif
18299 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018300 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018302 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303
18304 if (check_restricted() || check_secure())
18305 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018306
18307#ifdef FEAT_WINDOWS
18308 if (off == 1)
18309 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18310 else
18311 tp = curtab;
18312#endif
18313 win = find_win_by_nr(&argvars[off], tp);
18314 varname = get_tv_string_chk(&argvars[off + 1]);
18315 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316
18317 if (win != NULL && varname != NULL && varp != NULL)
18318 {
18319#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018320 need_switch_win = !(tp == curtab && win == curwin);
18321 if (!need_switch_win
18322 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018325 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018327 long numval;
18328 char_u *strval;
18329 int error = FALSE;
18330
18331 ++varname;
18332 numval = get_tv_number_chk(varp, &error);
18333 strval = get_tv_string_buf_chk(varp, nbuf);
18334 if (!error && strval != NULL)
18335 set_option_value(varname, numval, strval, OPT_LOCAL);
18336 }
18337 else
18338 {
18339 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18340 if (winvarname != NULL)
18341 {
18342 STRCPY(winvarname, "w:");
18343 STRCPY(winvarname + 2, varname);
18344 set_var(winvarname, varp, TRUE);
18345 vim_free(winvarname);
18346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347 }
18348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018350 if (need_switch_win)
18351 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352#endif
18353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354}
18355
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018356#ifdef FEAT_CRYPT
18357/*
18358 * "sha256({string})" function
18359 */
18360 static void
18361f_sha256(argvars, rettv)
18362 typval_T *argvars;
18363 typval_T *rettv;
18364{
18365 char_u *p;
18366
18367 p = get_tv_string(&argvars[0]);
18368 rettv->vval.v_string = vim_strsave(
18369 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18370 rettv->v_type = VAR_STRING;
18371}
18372#endif /* FEAT_CRYPT */
18373
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018375 * "shellescape({string})" function
18376 */
18377 static void
18378f_shellescape(argvars, rettv)
18379 typval_T *argvars;
18380 typval_T *rettv;
18381{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018382 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018383 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018384 rettv->v_type = VAR_STRING;
18385}
18386
18387/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018388 * shiftwidth() function
18389 */
18390 static void
18391f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020018392 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018393 typval_T *rettv;
18394{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018395 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018396}
18397
18398/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018399 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018400 */
18401 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000018402f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018403 typval_T *argvars;
18404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018405{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018406 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407
Bram Moolenaar0d660222005-01-07 21:51:51 +000018408 p = get_tv_string(&argvars[0]);
18409 rettv->vval.v_string = vim_strsave(p);
18410 simplify_filename(rettv->vval.v_string); /* simplify in place */
18411 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412}
18413
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018414#ifdef FEAT_FLOAT
18415/*
18416 * "sin()" function
18417 */
18418 static void
18419f_sin(argvars, rettv)
18420 typval_T *argvars;
18421 typval_T *rettv;
18422{
18423 float_T f;
18424
18425 rettv->v_type = VAR_FLOAT;
18426 if (get_float_arg(argvars, &f) == OK)
18427 rettv->vval.v_float = sin(f);
18428 else
18429 rettv->vval.v_float = 0.0;
18430}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018431
18432/*
18433 * "sinh()" function
18434 */
18435 static void
18436f_sinh(argvars, rettv)
18437 typval_T *argvars;
18438 typval_T *rettv;
18439{
18440 float_T f;
18441
18442 rettv->v_type = VAR_FLOAT;
18443 if (get_float_arg(argvars, &f) == OK)
18444 rettv->vval.v_float = sinh(f);
18445 else
18446 rettv->vval.v_float = 0.0;
18447}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018448#endif
18449
Bram Moolenaar0d660222005-01-07 21:51:51 +000018450static int
18451#ifdef __BORLANDC__
18452 _RTLENTRYF
18453#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018454 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018455static int
18456#ifdef __BORLANDC__
18457 _RTLENTRYF
18458#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018459 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018460
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018461/* struct used in the array that's given to qsort() */
18462typedef struct
18463{
18464 listitem_T *item;
18465 int idx;
18466} sortItem_T;
18467
Bram Moolenaar0d660222005-01-07 21:51:51 +000018468static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018469static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018470static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018471#ifdef FEAT_FLOAT
18472static int item_compare_float;
18473#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018474static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018475static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018476static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018477static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018478static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018479#define ITEM_COMPARE_FAIL 999
18480
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018482 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018484 static int
18485#ifdef __BORLANDC__
18486_RTLENTRYF
18487#endif
18488item_compare(s1, s2)
18489 const void *s1;
18490 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018492 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018493 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018494 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018495 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018496 int res;
18497 char_u numbuf1[NUMBUFLEN];
18498 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018500 si1 = (sortItem_T *)s1;
18501 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018502 tv1 = &si1->item->li_tv;
18503 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018504
18505 if (item_compare_numbers)
18506 {
18507 long v1 = get_tv_number(tv1);
18508 long v2 = get_tv_number(tv2);
18509
18510 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18511 }
18512
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018513#ifdef FEAT_FLOAT
18514 if (item_compare_float)
18515 {
18516 float_T v1 = get_tv_float(tv1);
18517 float_T v2 = get_tv_float(tv2);
18518
18519 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18520 }
18521#endif
18522
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018523 /* tv2string() puts quotes around a string and allocates memory. Don't do
18524 * that for string variables. Use a single quote when comparing with a
18525 * non-string to do what the docs promise. */
18526 if (tv1->v_type == VAR_STRING)
18527 {
18528 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18529 p1 = (char_u *)"'";
18530 else
18531 p1 = tv1->vval.v_string;
18532 }
18533 else
18534 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18535 if (tv2->v_type == VAR_STRING)
18536 {
18537 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18538 p2 = (char_u *)"'";
18539 else
18540 p2 = tv2->vval.v_string;
18541 }
18542 else
18543 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018544 if (p1 == NULL)
18545 p1 = (char_u *)"";
18546 if (p2 == NULL)
18547 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018548 if (!item_compare_numeric)
18549 {
18550 if (item_compare_ic)
18551 res = STRICMP(p1, p2);
18552 else
18553 res = STRCMP(p1, p2);
18554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018556 {
18557 double n1, n2;
18558 n1 = strtod((char *)p1, (char **)&p1);
18559 n2 = strtod((char *)p2, (char **)&p2);
18560 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18561 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018562
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018563 /* When the result would be zero, compare the item indexes. Makes the
18564 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018565 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018566 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018567
Bram Moolenaar0d660222005-01-07 21:51:51 +000018568 vim_free(tofree1);
18569 vim_free(tofree2);
18570 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571}
18572
18573 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018574#ifdef __BORLANDC__
18575_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018577item_compare2(s1, s2)
18578 const void *s1;
18579 const void *s2;
18580{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018581 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018582 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018583 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018584 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018585 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018587 /* shortcut after failure in previous call; compare all items equal */
18588 if (item_compare_func_err)
18589 return 0;
18590
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018591 si1 = (sortItem_T *)s1;
18592 si2 = (sortItem_T *)s2;
18593
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018594 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018595 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018596 copy_tv(&si1->item->li_tv, &argv[0]);
18597 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018598
18599 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018600 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018601 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18602 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018603 clear_tv(&argv[0]);
18604 clear_tv(&argv[1]);
18605
18606 if (res == FAIL)
18607 res = ITEM_COMPARE_FAIL;
18608 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018609 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18610 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018611 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018612 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018613
18614 /* When the result would be zero, compare the pointers themselves. Makes
18615 * the sort stable. */
18616 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018617 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018618
Bram Moolenaar0d660222005-01-07 21:51:51 +000018619 return res;
18620}
18621
18622/*
18623 * "sort({list})" function
18624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018625 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010018626do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000018627 typval_T *argvars;
18628 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018629 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630{
Bram Moolenaar33570922005-01-25 22:26:29 +000018631 list_T *l;
18632 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018633 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018634 long len;
18635 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636
Bram Moolenaar0d660222005-01-07 21:51:51 +000018637 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018638 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018639 else
18640 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018641 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018642 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018643 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18644 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018645 return;
18646 rettv->vval.v_list = l;
18647 rettv->v_type = VAR_LIST;
18648 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649
Bram Moolenaar0d660222005-01-07 21:51:51 +000018650 len = list_len(l);
18651 if (len <= 1)
18652 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018655 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018656 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018657#ifdef FEAT_FLOAT
18658 item_compare_float = FALSE;
18659#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018660 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018661 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018662 if (argvars[1].v_type != VAR_UNKNOWN)
18663 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018664 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018665 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018666 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018667 else
18668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018669 int error = FALSE;
18670
18671 i = get_tv_number_chk(&argvars[1], &error);
18672 if (error)
18673 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018674 if (i == 1)
18675 item_compare_ic = TRUE;
18676 else
18677 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018678 if (item_compare_func != NULL)
18679 {
18680 if (STRCMP(item_compare_func, "n") == 0)
18681 {
18682 item_compare_func = NULL;
18683 item_compare_numeric = TRUE;
18684 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018685 else if (STRCMP(item_compare_func, "N") == 0)
18686 {
18687 item_compare_func = NULL;
18688 item_compare_numbers = TRUE;
18689 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018690#ifdef FEAT_FLOAT
18691 else if (STRCMP(item_compare_func, "f") == 0)
18692 {
18693 item_compare_func = NULL;
18694 item_compare_float = TRUE;
18695 }
18696#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018697 else if (STRCMP(item_compare_func, "i") == 0)
18698 {
18699 item_compare_func = NULL;
18700 item_compare_ic = TRUE;
18701 }
18702 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018703 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018704
18705 if (argvars[2].v_type != VAR_UNKNOWN)
18706 {
18707 /* optional third argument: {dict} */
18708 if (argvars[2].v_type != VAR_DICT)
18709 {
18710 EMSG(_(e_dictreq));
18711 return;
18712 }
18713 item_compare_selfdict = argvars[2].vval.v_dict;
18714 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716
Bram Moolenaar0d660222005-01-07 21:51:51 +000018717 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018718 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018719 if (ptrs == NULL)
18720 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721
Bram Moolenaar327aa022014-03-25 18:24:23 +010018722 i = 0;
18723 if (sort)
18724 {
18725 /* sort(): ptrs will be the list to sort */
18726 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018727 {
18728 ptrs[i].item = li;
18729 ptrs[i].idx = i;
18730 ++i;
18731 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018732
18733 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018734 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018735 /* test the compare function */
18736 if (item_compare_func != NULL
18737 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018738 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018739 EMSG(_("E702: Sort compare function failed"));
18740 else
18741 {
18742 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018743 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018744 item_compare_func == NULL ? item_compare : item_compare2);
18745
18746 if (!item_compare_func_err)
18747 {
18748 /* Clear the List and append the items in sorted order. */
18749 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18750 l->lv_len = 0;
18751 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018752 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018753 }
18754 }
18755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018757 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018758 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018759
18760 /* f_uniq(): ptrs will be a stack of items to remove */
18761 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018762 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018763 item_compare_func_ptr = item_compare_func
18764 ? item_compare2 : item_compare;
18765
18766 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18767 li = li->li_next)
18768 {
18769 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18770 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018771 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018772 if (item_compare_func_err)
18773 {
18774 EMSG(_("E882: Uniq compare function failed"));
18775 break;
18776 }
18777 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018779 if (!item_compare_func_err)
18780 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018781 while (--i >= 0)
18782 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018783 li = ptrs[i].item->li_next;
18784 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018785 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018786 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018787 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018788 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018789 list_fix_watch(l, li);
18790 listitem_free(li);
18791 l->lv_len--;
18792 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018793 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018794 }
18795
18796 vim_free(ptrs);
18797 }
18798}
18799
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018800/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018801 * "sort({list})" function
18802 */
18803 static void
18804f_sort(argvars, rettv)
18805 typval_T *argvars;
18806 typval_T *rettv;
18807{
18808 do_sort_uniq(argvars, rettv, TRUE);
18809}
18810
18811/*
18812 * "uniq({list})" function
18813 */
18814 static void
18815f_uniq(argvars, rettv)
18816 typval_T *argvars;
18817 typval_T *rettv;
18818{
18819 do_sort_uniq(argvars, rettv, FALSE);
18820}
18821
18822/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018823 * "soundfold({word})" function
18824 */
18825 static void
18826f_soundfold(argvars, rettv)
18827 typval_T *argvars;
18828 typval_T *rettv;
18829{
18830 char_u *s;
18831
18832 rettv->v_type = VAR_STRING;
18833 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018834#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018835 rettv->vval.v_string = eval_soundfold(s);
18836#else
18837 rettv->vval.v_string = vim_strsave(s);
18838#endif
18839}
18840
18841/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018842 * "spellbadword()" function
18843 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018844 static void
18845f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018846 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018847 typval_T *rettv;
18848{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018849 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018850 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018851 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018852
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018853 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018854 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018855
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018856#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018857 if (argvars[0].v_type == VAR_UNKNOWN)
18858 {
18859 /* Find the start and length of the badly spelled word. */
18860 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18861 if (len != 0)
18862 word = ml_get_cursor();
18863 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018864 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018865 {
18866 char_u *str = get_tv_string_chk(&argvars[0]);
18867 int capcol = -1;
18868
18869 if (str != NULL)
18870 {
18871 /* Check the argument for spelling. */
18872 while (*str != NUL)
18873 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018874 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018875 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018876 {
18877 word = str;
18878 break;
18879 }
18880 str += len;
18881 }
18882 }
18883 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018884#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018885
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018886 list_append_string(rettv->vval.v_list, word, len);
18887 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018888 attr == HLF_SPB ? "bad" :
18889 attr == HLF_SPR ? "rare" :
18890 attr == HLF_SPL ? "local" :
18891 attr == HLF_SPC ? "caps" :
18892 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018893}
18894
18895/*
18896 * "spellsuggest()" function
18897 */
18898 static void
18899f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018900 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018901 typval_T *rettv;
18902{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018903#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018904 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018905 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018906 int maxcount;
18907 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018908 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018909 listitem_T *li;
18910 int need_capital = FALSE;
18911#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018912
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018913 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018914 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018915
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018916#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018917 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018918 {
18919 str = get_tv_string(&argvars[0]);
18920 if (argvars[1].v_type != VAR_UNKNOWN)
18921 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018922 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018923 if (maxcount <= 0)
18924 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018925 if (argvars[2].v_type != VAR_UNKNOWN)
18926 {
18927 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18928 if (typeerr)
18929 return;
18930 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018931 }
18932 else
18933 maxcount = 25;
18934
Bram Moolenaar4770d092006-01-12 23:22:24 +000018935 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018936
18937 for (i = 0; i < ga.ga_len; ++i)
18938 {
18939 str = ((char_u **)ga.ga_data)[i];
18940
18941 li = listitem_alloc();
18942 if (li == NULL)
18943 vim_free(str);
18944 else
18945 {
18946 li->li_tv.v_type = VAR_STRING;
18947 li->li_tv.v_lock = 0;
18948 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018949 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018950 }
18951 }
18952 ga_clear(&ga);
18953 }
18954#endif
18955}
18956
Bram Moolenaar0d660222005-01-07 21:51:51 +000018957 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000018958f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018959 typval_T *argvars;
18960 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018961{
18962 char_u *str;
18963 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018964 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018965 regmatch_T regmatch;
18966 char_u patbuf[NUMBUFLEN];
18967 char_u *save_cpo;
18968 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018969 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018970 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018971 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972
18973 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18974 save_cpo = p_cpo;
18975 p_cpo = (char_u *)"";
18976
18977 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018978 if (argvars[1].v_type != VAR_UNKNOWN)
18979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018980 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18981 if (pat == NULL)
18982 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018983 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018984 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018985 }
18986 if (pat == NULL || *pat == NUL)
18987 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018989 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018991 if (typeerr)
18992 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018993
Bram Moolenaar0d660222005-01-07 21:51:51 +000018994 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18995 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018996 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018997 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018998 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019000 if (*str == NUL)
19001 match = FALSE; /* empty item at the end */
19002 else
19003 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019004 if (match)
19005 end = regmatch.startp[0];
19006 else
19007 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019008 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19009 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019010 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019011 if (list_append_string(rettv->vval.v_list, str,
19012 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014 }
19015 if (!match)
19016 break;
19017 /* Advance to just after the match. */
19018 if (regmatch.endp[0] > str)
19019 col = 0;
19020 else
19021 {
19022 /* Don't get stuck at the same match. */
19023#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019024 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019025#else
19026 col = 1;
19027#endif
19028 }
19029 str = regmatch.endp[0];
19030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031
Bram Moolenaar473de612013-06-08 18:19:48 +020019032 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034
Bram Moolenaar0d660222005-01-07 21:51:51 +000019035 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036}
19037
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019038#ifdef FEAT_FLOAT
19039/*
19040 * "sqrt()" function
19041 */
19042 static void
19043f_sqrt(argvars, rettv)
19044 typval_T *argvars;
19045 typval_T *rettv;
19046{
19047 float_T f;
19048
19049 rettv->v_type = VAR_FLOAT;
19050 if (get_float_arg(argvars, &f) == OK)
19051 rettv->vval.v_float = sqrt(f);
19052 else
19053 rettv->vval.v_float = 0.0;
19054}
19055
19056/*
19057 * "str2float()" function
19058 */
19059 static void
19060f_str2float(argvars, rettv)
19061 typval_T *argvars;
19062 typval_T *rettv;
19063{
19064 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19065
19066 if (*p == '+')
19067 p = skipwhite(p + 1);
19068 (void)string2float(p, &rettv->vval.v_float);
19069 rettv->v_type = VAR_FLOAT;
19070}
19071#endif
19072
Bram Moolenaar2c932302006-03-18 21:42:09 +000019073/*
19074 * "str2nr()" function
19075 */
19076 static void
19077f_str2nr(argvars, rettv)
19078 typval_T *argvars;
19079 typval_T *rettv;
19080{
19081 int base = 10;
19082 char_u *p;
19083 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019084 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019085
19086 if (argvars[1].v_type != VAR_UNKNOWN)
19087 {
19088 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019089 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019090 {
19091 EMSG(_(e_invarg));
19092 return;
19093 }
19094 }
19095
19096 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019097 if (*p == '+')
19098 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019099 switch (base)
19100 {
19101 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19102 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19103 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19104 default: what = 0;
19105 }
19106 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019107 rettv->vval.v_number = n;
19108}
19109
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110#ifdef HAVE_STRFTIME
19111/*
19112 * "strftime({format}[, {time}])" function
19113 */
19114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019115f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019116 typval_T *argvars;
19117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118{
19119 char_u result_buf[256];
19120 struct tm *curtime;
19121 time_t seconds;
19122 char_u *p;
19123
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019126 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019127 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 seconds = time(NULL);
19129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019130 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 curtime = localtime(&seconds);
19132 /* MSVC returns NULL for an invalid value of seconds. */
19133 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019134 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135 else
19136 {
19137# ifdef FEAT_MBYTE
19138 vimconv_T conv;
19139 char_u *enc;
19140
19141 conv.vc_type = CONV_NONE;
19142 enc = enc_locale();
19143 convert_setup(&conv, p_enc, enc);
19144 if (conv.vc_type != CONV_NONE)
19145 p = string_convert(&conv, p, NULL);
19146# endif
19147 if (p != NULL)
19148 (void)strftime((char *)result_buf, sizeof(result_buf),
19149 (char *)p, curtime);
19150 else
19151 result_buf[0] = NUL;
19152
19153# ifdef FEAT_MBYTE
19154 if (conv.vc_type != CONV_NONE)
19155 vim_free(p);
19156 convert_setup(&conv, enc, p_enc);
19157 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019158 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159 else
19160# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019161 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162
19163# ifdef FEAT_MBYTE
19164 /* Release conversion descriptors */
19165 convert_setup(&conv, NULL, NULL);
19166 vim_free(enc);
19167# endif
19168 }
19169}
19170#endif
19171
19172/*
19173 * "stridx()" function
19174 */
19175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019176f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019177 typval_T *argvars;
19178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179{
19180 char_u buf[NUMBUFLEN];
19181 char_u *needle;
19182 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019183 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019185 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019187 needle = get_tv_string_chk(&argvars[1]);
19188 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019189 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019190 if (needle == NULL || haystack == NULL)
19191 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192
Bram Moolenaar33570922005-01-25 22:26:29 +000019193 if (argvars[2].v_type != VAR_UNKNOWN)
19194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019195 int error = FALSE;
19196
19197 start_idx = get_tv_number_chk(&argvars[2], &error);
19198 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019199 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019200 if (start_idx >= 0)
19201 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019202 }
19203
19204 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19205 if (pos != NULL)
19206 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207}
19208
19209/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019210 * "string()" function
19211 */
19212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019213f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019214 typval_T *argvars;
19215 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019216{
19217 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019218 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019220 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019221 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019222 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019223 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019224 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225}
19226
19227/*
19228 * "strlen()" function
19229 */
19230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019231f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019232 typval_T *argvars;
19233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019235 rettv->vval.v_number = (varnumber_T)(STRLEN(
19236 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237}
19238
19239/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019240 * "strchars()" function
19241 */
19242 static void
19243f_strchars(argvars, rettv)
19244 typval_T *argvars;
19245 typval_T *rettv;
19246{
19247 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019248 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019249#ifdef FEAT_MBYTE
19250 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019251 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019252#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019253
19254 if (argvars[1].v_type != VAR_UNKNOWN)
19255 skipcc = get_tv_number_chk(&argvars[1], NULL);
19256 if (skipcc < 0 || skipcc > 1)
19257 EMSG(_(e_invarg));
19258 else
19259 {
19260#ifdef FEAT_MBYTE
19261 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19262 while (*s != NUL)
19263 {
19264 func_mb_ptr2char_adv(&s);
19265 ++len;
19266 }
19267 rettv->vval.v_number = len;
19268#else
19269 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19270#endif
19271 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019272}
19273
19274/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019275 * "strdisplaywidth()" function
19276 */
19277 static void
19278f_strdisplaywidth(argvars, rettv)
19279 typval_T *argvars;
19280 typval_T *rettv;
19281{
19282 char_u *s = get_tv_string(&argvars[0]);
19283 int col = 0;
19284
19285 if (argvars[1].v_type != VAR_UNKNOWN)
19286 col = get_tv_number(&argvars[1]);
19287
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019288 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019289}
19290
19291/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019292 * "strwidth()" function
19293 */
19294 static void
19295f_strwidth(argvars, rettv)
19296 typval_T *argvars;
19297 typval_T *rettv;
19298{
19299 char_u *s = get_tv_string(&argvars[0]);
19300
19301 rettv->vval.v_number = (varnumber_T)(
19302#ifdef FEAT_MBYTE
19303 mb_string2cells(s, -1)
19304#else
19305 STRLEN(s)
19306#endif
19307 );
19308}
19309
19310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 * "strpart()" function
19312 */
19313 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019314f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019315 typval_T *argvars;
19316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317{
19318 char_u *p;
19319 int n;
19320 int len;
19321 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019322 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019324 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325 slen = (int)STRLEN(p);
19326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019327 n = get_tv_number_chk(&argvars[1], &error);
19328 if (error)
19329 len = 0;
19330 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019331 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 else
19333 len = slen - n; /* default len: all bytes that are available. */
19334
19335 /*
19336 * Only return the overlap between the specified part and the actual
19337 * string.
19338 */
19339 if (n < 0)
19340 {
19341 len += n;
19342 n = 0;
19343 }
19344 else if (n > slen)
19345 n = slen;
19346 if (len < 0)
19347 len = 0;
19348 else if (n + len > slen)
19349 len = slen - n;
19350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019351 rettv->v_type = VAR_STRING;
19352 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353}
19354
19355/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019356 * "strridx()" function
19357 */
19358 static void
19359f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019360 typval_T *argvars;
19361 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019362{
19363 char_u buf[NUMBUFLEN];
19364 char_u *needle;
19365 char_u *haystack;
19366 char_u *rest;
19367 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019368 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019370 needle = get_tv_string_chk(&argvars[1]);
19371 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019372
19373 rettv->vval.v_number = -1;
19374 if (needle == NULL || haystack == NULL)
19375 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019376
19377 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019378 if (argvars[2].v_type != VAR_UNKNOWN)
19379 {
19380 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019381 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019382 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019383 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019384 }
19385 else
19386 end_idx = haystack_len;
19387
Bram Moolenaar0d660222005-01-07 21:51:51 +000019388 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019389 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019390 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019391 lastmatch = haystack + end_idx;
19392 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019393 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019394 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019395 for (rest = haystack; *rest != '\0'; ++rest)
19396 {
19397 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019398 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019399 break;
19400 lastmatch = rest;
19401 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019402 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019403
19404 if (lastmatch == NULL)
19405 rettv->vval.v_number = -1;
19406 else
19407 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19408}
19409
19410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 * "strtrans()" function
19412 */
19413 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019414f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019415 typval_T *argvars;
19416 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019418 rettv->v_type = VAR_STRING;
19419 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420}
19421
19422/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019423 * "submatch()" function
19424 */
19425 static void
19426f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019427 typval_T *argvars;
19428 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019429{
Bram Moolenaar41571762014-04-02 19:00:58 +020019430 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019431 int no;
19432 int retList = 0;
19433
19434 no = (int)get_tv_number_chk(&argvars[0], &error);
19435 if (error)
19436 return;
19437 error = FALSE;
19438 if (argvars[1].v_type != VAR_UNKNOWN)
19439 retList = get_tv_number_chk(&argvars[1], &error);
19440 if (error)
19441 return;
19442
19443 if (retList == 0)
19444 {
19445 rettv->v_type = VAR_STRING;
19446 rettv->vval.v_string = reg_submatch(no);
19447 }
19448 else
19449 {
19450 rettv->v_type = VAR_LIST;
19451 rettv->vval.v_list = reg_submatch_list(no);
19452 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019453}
19454
19455/*
19456 * "substitute()" function
19457 */
19458 static void
19459f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019460 typval_T *argvars;
19461 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019462{
19463 char_u patbuf[NUMBUFLEN];
19464 char_u subbuf[NUMBUFLEN];
19465 char_u flagsbuf[NUMBUFLEN];
19466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019467 char_u *str = get_tv_string_chk(&argvars[0]);
19468 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19469 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19470 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19471
Bram Moolenaar0d660222005-01-07 21:51:51 +000019472 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019473 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19474 rettv->vval.v_string = NULL;
19475 else
19476 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019477}
19478
19479/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019480 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019483f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019484 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486{
19487 int id = 0;
19488#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019489 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 long col;
19491 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019492 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019494 lnum = get_tv_lnum(argvars); /* -1 on type error */
19495 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19496 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019498 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019499 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019500 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501#endif
19502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019503 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504}
19505
19506/*
19507 * "synIDattr(id, what [, mode])" function
19508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019510f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019511 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513{
19514 char_u *p = NULL;
19515#ifdef FEAT_SYN_HL
19516 int id;
19517 char_u *what;
19518 char_u *mode;
19519 char_u modebuf[NUMBUFLEN];
19520 int modec;
19521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019522 id = get_tv_number(&argvars[0]);
19523 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019524 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019526 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019528 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 modec = 0; /* replace invalid with current */
19530 }
19531 else
19532 {
19533#ifdef FEAT_GUI
19534 if (gui.in_use)
19535 modec = 'g';
19536 else
19537#endif
19538 if (t_colors > 1)
19539 modec = 'c';
19540 else
19541 modec = 't';
19542 }
19543
19544
19545 switch (TOLOWER_ASC(what[0]))
19546 {
19547 case 'b':
19548 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19549 p = highlight_color(id, what, modec);
19550 else /* bold */
19551 p = highlight_has_attr(id, HL_BOLD, modec);
19552 break;
19553
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019554 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555 p = highlight_color(id, what, modec);
19556 break;
19557
19558 case 'i':
19559 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19560 p = highlight_has_attr(id, HL_INVERSE, modec);
19561 else /* italic */
19562 p = highlight_has_attr(id, HL_ITALIC, modec);
19563 break;
19564
19565 case 'n': /* name */
19566 p = get_highlight_name(NULL, id - 1);
19567 break;
19568
19569 case 'r': /* reverse */
19570 p = highlight_has_attr(id, HL_INVERSE, modec);
19571 break;
19572
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019573 case 's':
19574 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19575 p = highlight_color(id, what, modec);
19576 else /* standout */
19577 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578 break;
19579
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019580 case 'u':
19581 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19582 /* underline */
19583 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19584 else
19585 /* undercurl */
19586 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 break;
19588 }
19589
19590 if (p != NULL)
19591 p = vim_strsave(p);
19592#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019593 rettv->v_type = VAR_STRING;
19594 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595}
19596
19597/*
19598 * "synIDtrans(id)" function
19599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019601f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019602 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604{
19605 int id;
19606
19607#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019608 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609
19610 if (id > 0)
19611 id = syn_get_final_id(id);
19612 else
19613#endif
19614 id = 0;
19615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019616 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617}
19618
19619/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019620 * "synconcealed(lnum, col)" function
19621 */
19622 static void
19623f_synconcealed(argvars, rettv)
19624 typval_T *argvars UNUSED;
19625 typval_T *rettv;
19626{
19627#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19628 long lnum;
19629 long col;
19630 int syntax_flags = 0;
19631 int cchar;
19632 int matchid = 0;
19633 char_u str[NUMBUFLEN];
19634#endif
19635
19636 rettv->v_type = VAR_LIST;
19637 rettv->vval.v_list = NULL;
19638
19639#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19640 lnum = get_tv_lnum(argvars); /* -1 on type error */
19641 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19642
19643 vim_memset(str, NUL, sizeof(str));
19644
19645 if (rettv_list_alloc(rettv) != FAIL)
19646 {
19647 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19648 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19649 && curwin->w_p_cole > 0)
19650 {
19651 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19652 syntax_flags = get_syntax_info(&matchid);
19653
19654 /* get the conceal character */
19655 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19656 {
19657 cchar = syn_get_sub_char();
19658 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19659 cchar = lcs_conceal;
19660 if (cchar != NUL)
19661 {
19662# ifdef FEAT_MBYTE
19663 if (has_mbyte)
19664 (*mb_char2bytes)(cchar, str);
19665 else
19666# endif
19667 str[0] = cchar;
19668 }
19669 }
19670 }
19671
19672 list_append_number(rettv->vval.v_list,
19673 (syntax_flags & HL_CONCEAL) != 0);
19674 /* -1 to auto-determine strlen */
19675 list_append_string(rettv->vval.v_list, str, -1);
19676 list_append_number(rettv->vval.v_list, matchid);
19677 }
19678#endif
19679}
19680
19681/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019682 * "synstack(lnum, col)" function
19683 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019684 static void
19685f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019686 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019687 typval_T *rettv;
19688{
19689#ifdef FEAT_SYN_HL
19690 long lnum;
19691 long col;
19692 int i;
19693 int id;
19694#endif
19695
19696 rettv->v_type = VAR_LIST;
19697 rettv->vval.v_list = NULL;
19698
19699#ifdef FEAT_SYN_HL
19700 lnum = get_tv_lnum(argvars); /* -1 on type error */
19701 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19702
19703 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019704 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019705 && rettv_list_alloc(rettv) != FAIL)
19706 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019707 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019708 for (i = 0; ; ++i)
19709 {
19710 id = syn_get_stack_item(i);
19711 if (id < 0)
19712 break;
19713 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19714 break;
19715 }
19716 }
19717#endif
19718}
19719
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019721get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000019722 typval_T *argvars;
19723 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019724 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019726 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019728 char_u *infile = NULL;
19729 char_u buf[NUMBUFLEN];
19730 int err = FALSE;
19731 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019732 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019733 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019735 rettv->v_type = VAR_STRING;
19736 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019737 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019738 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019739
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019740 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019741 {
19742 /*
19743 * Write the string to a temp file, to be used for input of the shell
19744 * command.
19745 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019746 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019747 {
19748 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019749 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019750 }
19751
19752 fd = mch_fopen((char *)infile, WRITEBIN);
19753 if (fd == NULL)
19754 {
19755 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019756 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019757 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019758 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019759 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019760 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19761 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019762 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019763 else
19764 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019765 size_t len;
19766
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019767 p = get_tv_string_buf_chk(&argvars[1], buf);
19768 if (p == NULL)
19769 {
19770 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019771 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019772 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019773 len = STRLEN(p);
19774 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019775 err = TRUE;
19776 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019777 if (fclose(fd) != 0)
19778 err = TRUE;
19779 if (err)
19780 {
19781 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019782 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019783 }
19784 }
19785
Bram Moolenaar52a72462014-08-29 15:53:52 +020019786 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19787 * echoes typeahead, that messes up the display. */
19788 if (!msg_silent)
19789 flags += SHELL_COOKED;
19790
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019791 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019793 int len;
19794 listitem_T *li;
19795 char_u *s = NULL;
19796 char_u *start;
19797 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019798 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799
Bram Moolenaar52a72462014-08-29 15:53:52 +020019800 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019801 if (res == NULL)
19802 goto errret;
19803
19804 list = list_alloc();
19805 if (list == NULL)
19806 goto errret;
19807
19808 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019810 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019811 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019812 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019813 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019814
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019815 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019816 if (s == NULL)
19817 goto errret;
19818
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019819 for (p = s; start < end; ++p, ++start)
19820 *p = *start == NUL ? NL : *start;
19821 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019822
19823 li = listitem_alloc();
19824 if (li == NULL)
19825 {
19826 vim_free(s);
19827 goto errret;
19828 }
19829 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019830 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019831 li->li_tv.vval.v_string = s;
19832 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019834
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019835 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019836 rettv->v_type = VAR_LIST;
19837 rettv->vval.v_list = list;
19838 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019840 else
19841 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019842 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019843#ifdef USE_CR
19844 /* translate <CR> into <NL> */
19845 if (res != NULL)
19846 {
19847 char_u *s;
19848
19849 for (s = res; *s; ++s)
19850 {
19851 if (*s == CAR)
19852 *s = NL;
19853 }
19854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019855#else
19856# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019857 /* translate <CR><NL> into <NL> */
19858 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019860 char_u *s, *d;
19861
19862 d = res;
19863 for (s = res; *s; ++s)
19864 {
19865 if (s[0] == CAR && s[1] == NL)
19866 ++s;
19867 *d++ = *s;
19868 }
19869 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871# endif
19872#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019873 rettv->vval.v_string = res;
19874 res = NULL;
19875 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019876
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019877errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019878 if (infile != NULL)
19879 {
19880 mch_remove(infile);
19881 vim_free(infile);
19882 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019883 if (res != NULL)
19884 vim_free(res);
19885 if (list != NULL)
19886 list_free(list, TRUE);
19887}
19888
19889/*
19890 * "system()" function
19891 */
19892 static void
19893f_system(argvars, rettv)
19894 typval_T *argvars;
19895 typval_T *rettv;
19896{
19897 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19898}
19899
19900/*
19901 * "systemlist()" function
19902 */
19903 static void
19904f_systemlist(argvars, rettv)
19905 typval_T *argvars;
19906 typval_T *rettv;
19907{
19908 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909}
19910
19911/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019912 * "tabpagebuflist()" function
19913 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019914 static void
19915f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019916 typval_T *argvars UNUSED;
19917 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019918{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019919#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019920 tabpage_T *tp;
19921 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019922
19923 if (argvars[0].v_type == VAR_UNKNOWN)
19924 wp = firstwin;
19925 else
19926 {
19927 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19928 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019929 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019930 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019931 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019932 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019933 for (; wp != NULL; wp = wp->w_next)
19934 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019935 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019936 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019937 }
19938#endif
19939}
19940
19941
19942/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019943 * "tabpagenr()" function
19944 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019945 static void
19946f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019947 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019948 typval_T *rettv;
19949{
19950 int nr = 1;
19951#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019952 char_u *arg;
19953
19954 if (argvars[0].v_type != VAR_UNKNOWN)
19955 {
19956 arg = get_tv_string_chk(&argvars[0]);
19957 nr = 0;
19958 if (arg != NULL)
19959 {
19960 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019961 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019962 else
19963 EMSG2(_(e_invexpr2), arg);
19964 }
19965 }
19966 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019967 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019968#endif
19969 rettv->vval.v_number = nr;
19970}
19971
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019972
19973#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019974static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019975
19976/*
19977 * Common code for tabpagewinnr() and winnr().
19978 */
19979 static int
19980get_winnr(tp, argvar)
19981 tabpage_T *tp;
19982 typval_T *argvar;
19983{
19984 win_T *twin;
19985 int nr = 1;
19986 win_T *wp;
19987 char_u *arg;
19988
19989 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19990 if (argvar->v_type != VAR_UNKNOWN)
19991 {
19992 arg = get_tv_string_chk(argvar);
19993 if (arg == NULL)
19994 nr = 0; /* type error; errmsg already given */
19995 else if (STRCMP(arg, "$") == 0)
19996 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19997 else if (STRCMP(arg, "#") == 0)
19998 {
19999 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20000 if (twin == NULL)
20001 nr = 0;
20002 }
20003 else
20004 {
20005 EMSG2(_(e_invexpr2), arg);
20006 nr = 0;
20007 }
20008 }
20009
20010 if (nr > 0)
20011 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20012 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020013 {
20014 if (wp == NULL)
20015 {
20016 /* didn't find it in this tabpage */
20017 nr = 0;
20018 break;
20019 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020020 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020021 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020022 return nr;
20023}
20024#endif
20025
20026/*
20027 * "tabpagewinnr()" function
20028 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020029 static void
20030f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020031 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020032 typval_T *rettv;
20033{
20034 int nr = 1;
20035#ifdef FEAT_WINDOWS
20036 tabpage_T *tp;
20037
20038 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20039 if (tp == NULL)
20040 nr = 0;
20041 else
20042 nr = get_winnr(tp, &argvars[1]);
20043#endif
20044 rettv->vval.v_number = nr;
20045}
20046
20047
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020048/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020049 * "tagfiles()" function
20050 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020051 static void
20052f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020053 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020054 typval_T *rettv;
20055{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020056 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020057 tagname_T tn;
20058 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020059
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020060 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020061 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020062 fname = alloc(MAXPATHL);
20063 if (fname == NULL)
20064 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020065
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020066 for (first = TRUE; ; first = FALSE)
20067 if (get_tagfname(&tn, first, fname) == FAIL
20068 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020069 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020070 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020071 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020072}
20073
20074/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020075 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020076 */
20077 static void
20078f_taglist(argvars, rettv)
20079 typval_T *argvars;
20080 typval_T *rettv;
20081{
20082 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020083
20084 tag_pattern = get_tv_string(&argvars[0]);
20085
20086 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020087 if (*tag_pattern == NUL)
20088 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020089
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020090 if (rettv_list_alloc(rettv) == OK)
20091 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020092}
20093
20094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 * "tempname()" function
20096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020099 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101{
20102 static int x = 'A';
20103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020104 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020105 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106
20107 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20108 * names. Skip 'I' and 'O', they are used for shell redirection. */
20109 do
20110 {
20111 if (x == 'Z')
20112 x = '0';
20113 else if (x == '9')
20114 x = 'A';
20115 else
20116 {
20117#ifdef EBCDIC
20118 if (x == 'I')
20119 x = 'J';
20120 else if (x == 'R')
20121 x = 'S';
20122 else
20123#endif
20124 ++x;
20125 }
20126 } while (x == 'I' || x == 'O');
20127}
20128
20129/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020130 * "test(list)" function: Just checking the walls...
20131 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020132 static void
20133f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020134 typval_T *argvars UNUSED;
20135 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000020136{
20137 /* Used for unit testing. Change the code below to your liking. */
20138#if 0
20139 listitem_T *li;
20140 list_T *l;
20141 char_u *bad, *good;
20142
20143 if (argvars[0].v_type != VAR_LIST)
20144 return;
20145 l = argvars[0].vval.v_list;
20146 if (l == NULL)
20147 return;
20148 li = l->lv_first;
20149 if (li == NULL)
20150 return;
20151 bad = get_tv_string(&li->li_tv);
20152 li = li->li_next;
20153 if (li == NULL)
20154 return;
20155 good = get_tv_string(&li->li_tv);
20156 rettv->vval.v_number = test_edit_score(bad, good);
20157#endif
20158}
20159
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020160#ifdef FEAT_FLOAT
20161/*
20162 * "tan()" function
20163 */
20164 static void
20165f_tan(argvars, rettv)
20166 typval_T *argvars;
20167 typval_T *rettv;
20168{
20169 float_T f;
20170
20171 rettv->v_type = VAR_FLOAT;
20172 if (get_float_arg(argvars, &f) == OK)
20173 rettv->vval.v_float = tan(f);
20174 else
20175 rettv->vval.v_float = 0.0;
20176}
20177
20178/*
20179 * "tanh()" function
20180 */
20181 static void
20182f_tanh(argvars, rettv)
20183 typval_T *argvars;
20184 typval_T *rettv;
20185{
20186 float_T f;
20187
20188 rettv->v_type = VAR_FLOAT;
20189 if (get_float_arg(argvars, &f) == OK)
20190 rettv->vval.v_float = tanh(f);
20191 else
20192 rettv->vval.v_float = 0.0;
20193}
20194#endif
20195
Bram Moolenaard52d9742005-08-21 22:20:28 +000020196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 * "tolower(string)" function
20198 */
20199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020200f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020201 typval_T *argvars;
20202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203{
20204 char_u *p;
20205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020206 p = vim_strsave(get_tv_string(&argvars[0]));
20207 rettv->v_type = VAR_STRING;
20208 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020209
20210 if (p != NULL)
20211 while (*p != NUL)
20212 {
20213#ifdef FEAT_MBYTE
20214 int l;
20215
20216 if (enc_utf8)
20217 {
20218 int c, lc;
20219
20220 c = utf_ptr2char(p);
20221 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020222 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 /* TODO: reallocate string when byte count changes. */
20224 if (utf_char2len(lc) == l)
20225 utf_char2bytes(lc, p);
20226 p += l;
20227 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020228 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 p += l; /* skip multi-byte character */
20230 else
20231#endif
20232 {
20233 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20234 ++p;
20235 }
20236 }
20237}
20238
20239/*
20240 * "toupper(string)" function
20241 */
20242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020243f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020244 typval_T *argvars;
20245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020247 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020248 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249}
20250
20251/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020252 * "tr(string, fromstr, tostr)" function
20253 */
20254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020256 typval_T *argvars;
20257 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020258{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020259 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020260 char_u *fromstr;
20261 char_u *tostr;
20262 char_u *p;
20263#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020264 int inlen;
20265 int fromlen;
20266 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020267 int idx;
20268 char_u *cpstr;
20269 int cplen;
20270 int first = TRUE;
20271#endif
20272 char_u buf[NUMBUFLEN];
20273 char_u buf2[NUMBUFLEN];
20274 garray_T ga;
20275
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020276 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020277 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20278 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020279
20280 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020281 rettv->v_type = VAR_STRING;
20282 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020283 if (fromstr == NULL || tostr == NULL)
20284 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020285 ga_init2(&ga, (int)sizeof(char), 80);
20286
20287#ifdef FEAT_MBYTE
20288 if (!has_mbyte)
20289#endif
20290 /* not multi-byte: fromstr and tostr must be the same length */
20291 if (STRLEN(fromstr) != STRLEN(tostr))
20292 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020293#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020294error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020295#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020296 EMSG2(_(e_invarg2), fromstr);
20297 ga_clear(&ga);
20298 return;
20299 }
20300
20301 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020302 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020303 {
20304#ifdef FEAT_MBYTE
20305 if (has_mbyte)
20306 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020307 inlen = (*mb_ptr2len)(in_str);
20308 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020309 cplen = inlen;
20310 idx = 0;
20311 for (p = fromstr; *p != NUL; p += fromlen)
20312 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020313 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020314 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020315 {
20316 for (p = tostr; *p != NUL; p += tolen)
20317 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020318 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020319 if (idx-- == 0)
20320 {
20321 cplen = tolen;
20322 cpstr = p;
20323 break;
20324 }
20325 }
20326 if (*p == NUL) /* tostr is shorter than fromstr */
20327 goto error;
20328 break;
20329 }
20330 ++idx;
20331 }
20332
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020333 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020334 {
20335 /* Check that fromstr and tostr have the same number of
20336 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020337 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020338 first = FALSE;
20339 for (p = tostr; *p != NUL; p += tolen)
20340 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020341 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020342 --idx;
20343 }
20344 if (idx != 0)
20345 goto error;
20346 }
20347
Bram Moolenaarcde88542015-08-11 19:14:00 +020020348 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020349 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020350 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020351
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020352 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020353 }
20354 else
20355#endif
20356 {
20357 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020358 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020359 if (p != NULL)
20360 ga_append(&ga, tostr[p - fromstr]);
20361 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020362 ga_append(&ga, *in_str);
20363 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020364 }
20365 }
20366
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020367 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020368 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020369 ga_append(&ga, NUL);
20370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020371 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020372}
20373
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020374#ifdef FEAT_FLOAT
20375/*
20376 * "trunc({float})" function
20377 */
20378 static void
20379f_trunc(argvars, rettv)
20380 typval_T *argvars;
20381 typval_T *rettv;
20382{
20383 float_T f;
20384
20385 rettv->v_type = VAR_FLOAT;
20386 if (get_float_arg(argvars, &f) == OK)
20387 /* trunc() is not in C90, use floor() or ceil() instead. */
20388 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20389 else
20390 rettv->vval.v_float = 0.0;
20391}
20392#endif
20393
Bram Moolenaar8299df92004-07-10 09:47:34 +000020394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395 * "type(expr)" function
20396 */
20397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020398f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020399 typval_T *argvars;
20400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020402 int n;
20403
20404 switch (argvars[0].v_type)
20405 {
20406 case VAR_NUMBER: n = 0; break;
20407 case VAR_STRING: n = 1; break;
20408 case VAR_FUNC: n = 2; break;
20409 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020410 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020411#ifdef FEAT_FLOAT
20412 case VAR_FLOAT: n = 5; break;
20413#endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020414 case VAR_SPECIAL:
20415 if (argvars[0].vval.v_number == VVAL_FALSE
20416 || argvars[0].vval.v_number == VVAL_TRUE)
20417 n = 6;
20418 else
20419 n = 7;
20420 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020421 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
20422 }
20423 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424}
20425
20426/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020427 * "undofile(name)" function
20428 */
20429 static void
20430f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010020431 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020432 typval_T *rettv;
20433{
20434 rettv->v_type = VAR_STRING;
20435#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020436 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020437 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020438
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020439 if (*fname == NUL)
20440 {
20441 /* If there is no file name there will be no undo file. */
20442 rettv->vval.v_string = NULL;
20443 }
20444 else
20445 {
20446 char_u *ffname = FullName_save(fname, FALSE);
20447
20448 if (ffname != NULL)
20449 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20450 vim_free(ffname);
20451 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020452 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020453#else
20454 rettv->vval.v_string = NULL;
20455#endif
20456}
20457
20458/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020459 * "undotree()" function
20460 */
20461 static void
20462f_undotree(argvars, rettv)
20463 typval_T *argvars UNUSED;
20464 typval_T *rettv;
20465{
20466 if (rettv_dict_alloc(rettv) == OK)
20467 {
20468 dict_T *dict = rettv->vval.v_dict;
20469 list_T *list;
20470
Bram Moolenaar730cde92010-06-27 05:18:54 +020020471 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020472 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020473 dict_add_nr_str(dict, "save_last",
20474 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020475 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20476 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020477 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020478
20479 list = list_alloc();
20480 if (list != NULL)
20481 {
20482 u_eval_tree(curbuf->b_u_oldhead, list);
20483 dict_add_list(dict, "entries", list);
20484 }
20485 }
20486}
20487
20488/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020489 * "values(dict)" function
20490 */
20491 static void
20492f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020493 typval_T *argvars;
20494 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020495{
20496 dict_list(argvars, rettv, 1);
20497}
20498
20499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500 * "virtcol(string)" function
20501 */
20502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020503f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020504 typval_T *argvars;
20505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506{
20507 colnr_T vcol = 0;
20508 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020509 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020510
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020511 fp = var2fpos(&argvars[0], FALSE, &fnum);
20512 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20513 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 {
20515 getvvcol(curwin, fp, NULL, NULL, &vcol);
20516 ++vcol;
20517 }
20518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020519 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520}
20521
20522/*
20523 * "visualmode()" function
20524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020526f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020527 typval_T *argvars UNUSED;
20528 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020529{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 char_u str[2];
20531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020532 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 str[0] = curbuf->b_visual_mode_eval;
20534 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020535 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020536
20537 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020538 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020539 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540}
20541
20542/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020543 * "wildmenumode()" function
20544 */
20545 static void
20546f_wildmenumode(argvars, rettv)
20547 typval_T *argvars UNUSED;
20548 typval_T *rettv UNUSED;
20549{
20550#ifdef FEAT_WILDMENU
20551 if (wild_menu_showing)
20552 rettv->vval.v_number = 1;
20553#endif
20554}
20555
20556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 * "winbufnr(nr)" function
20558 */
20559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020560f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020561 typval_T *argvars;
20562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563{
20564 win_T *wp;
20565
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020566 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020568 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020570 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571}
20572
20573/*
20574 * "wincol()" function
20575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020577f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020578 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580{
20581 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020582 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583}
20584
20585/*
20586 * "winheight(nr)" function
20587 */
20588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020589f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020590 typval_T *argvars;
20591 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592{
20593 win_T *wp;
20594
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020595 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020597 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020599 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600}
20601
20602/*
20603 * "winline()" function
20604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020606f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609{
20610 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020611 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612}
20613
20614/*
20615 * "winnr()" function
20616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020618f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020619 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621{
20622 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020623
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020625 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020627 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628}
20629
20630/*
20631 * "winrestcmd()" function
20632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637{
20638#ifdef FEAT_WINDOWS
20639 win_T *wp;
20640 int winnr = 1;
20641 garray_T ga;
20642 char_u buf[50];
20643
20644 ga_init2(&ga, (int)sizeof(char), 70);
20645 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20646 {
20647 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20648 ga_concat(&ga, buf);
20649# ifdef FEAT_VERTSPLIT
20650 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20651 ga_concat(&ga, buf);
20652# endif
20653 ++winnr;
20654 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020655 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020657 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020659 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020661 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662}
20663
20664/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020665 * "winrestview()" function
20666 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020667 static void
20668f_winrestview(argvars, rettv)
20669 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020670 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020671{
20672 dict_T *dict;
20673
20674 if (argvars[0].v_type != VAR_DICT
20675 || (dict = argvars[0].vval.v_dict) == NULL)
20676 EMSG(_(e_invarg));
20677 else
20678 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020679 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20680 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20681 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20682 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020683#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020684 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20685 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020686#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020687 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20688 {
20689 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20690 curwin->w_set_curswant = FALSE;
20691 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020692
Bram Moolenaar82c25852014-05-28 16:47:16 +020020693 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20694 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020695#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020696 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20697 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020698#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020699 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20700 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20701 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20702 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020703
20704 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020705 win_new_height(curwin, curwin->w_height);
20706# ifdef FEAT_VERTSPLIT
20707 win_new_width(curwin, W_WIDTH(curwin));
20708# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020709 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020710
Bram Moolenaarb851a962014-10-31 15:45:52 +010020711 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020712 curwin->w_topline = 1;
20713 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20714 curwin->w_topline = curbuf->b_ml.ml_line_count;
20715#ifdef FEAT_DIFF
20716 check_topfill(curwin, TRUE);
20717#endif
20718 }
20719}
20720
20721/*
20722 * "winsaveview()" function
20723 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020724 static void
20725f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000020726 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020727 typval_T *rettv;
20728{
20729 dict_T *dict;
20730
Bram Moolenaara800b422010-06-27 01:15:55 +020020731 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020732 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020733 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020734
20735 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20736 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20737#ifdef FEAT_VIRTUALEDIT
20738 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20739#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020740 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020741 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20742
20743 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20744#ifdef FEAT_DIFF
20745 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20746#endif
20747 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20748 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20749}
20750
20751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 * "winwidth(nr)" function
20753 */
20754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020755f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000020756 typval_T *argvars;
20757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758{
20759 win_T *wp;
20760
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020761 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020763 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 else
20765#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020766 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020768 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769#endif
20770}
20771
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020773 * "wordcount()" function
20774 */
20775 static void
20776f_wordcount(argvars, rettv)
20777 typval_T *argvars UNUSED;
20778 typval_T *rettv;
20779{
20780 if (rettv_dict_alloc(rettv) == FAIL)
20781 return;
20782 cursor_pos_info(rettv->vval.v_dict);
20783}
20784
20785/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020786 * Write list of strings to file
20787 */
20788 static int
20789write_list(fd, list, binary)
20790 FILE *fd;
20791 list_T *list;
20792 int binary;
20793{
20794 listitem_T *li;
20795 int c;
20796 int ret = OK;
20797 char_u *s;
20798
20799 for (li = list->lv_first; li != NULL; li = li->li_next)
20800 {
20801 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20802 {
20803 if (*s == '\n')
20804 c = putc(NUL, fd);
20805 else
20806 c = putc(*s, fd);
20807 if (c == EOF)
20808 {
20809 ret = FAIL;
20810 break;
20811 }
20812 }
20813 if (!binary || li->li_next != NULL)
20814 if (putc('\n', fd) == EOF)
20815 {
20816 ret = FAIL;
20817 break;
20818 }
20819 if (ret == FAIL)
20820 {
20821 EMSG(_(e_write));
20822 break;
20823 }
20824 }
20825 return ret;
20826}
20827
20828/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020829 * "writefile()" function
20830 */
20831 static void
20832f_writefile(argvars, rettv)
20833 typval_T *argvars;
20834 typval_T *rettv;
20835{
20836 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020837 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020838 char_u *fname;
20839 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020840 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020841
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020842 if (check_restricted() || check_secure())
20843 return;
20844
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020845 if (argvars[0].v_type != VAR_LIST)
20846 {
20847 EMSG2(_(e_listarg), "writefile()");
20848 return;
20849 }
20850 if (argvars[0].vval.v_list == NULL)
20851 return;
20852
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020853 if (argvars[2].v_type != VAR_UNKNOWN)
20854 {
20855 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20856 binary = TRUE;
20857 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20858 append = TRUE;
20859 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020860
20861 /* Always open the file in binary mode, library functions have a mind of
20862 * their own about CR-LF conversion. */
20863 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020864 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20865 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020866 {
20867 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20868 ret = -1;
20869 }
20870 else
20871 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020872 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20873 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020874 fclose(fd);
20875 }
20876
20877 rettv->vval.v_number = ret;
20878}
20879
20880/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020881 * "xor(expr, expr)" function
20882 */
20883 static void
20884f_xor(argvars, rettv)
20885 typval_T *argvars;
20886 typval_T *rettv;
20887{
20888 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20889 ^ get_tv_number_chk(&argvars[1], NULL);
20890}
20891
20892
20893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020895 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 */
20897 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000020898var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000020899 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020900 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020901 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020903 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020905 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906
Bram Moolenaara5525202006-03-02 22:52:09 +000020907 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020908 if (varp->v_type == VAR_LIST)
20909 {
20910 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020911 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020912 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020913 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020914
20915 l = varp->vval.v_list;
20916 if (l == NULL)
20917 return NULL;
20918
20919 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020920 pos.lnum = list_find_nr(l, 0L, &error);
20921 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020922 return NULL; /* invalid line number */
20923
20924 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020925 pos.col = list_find_nr(l, 1L, &error);
20926 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020927 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020928 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020929
20930 /* We accept "$" for the column number: last column. */
20931 li = list_find(l, 1L);
20932 if (li != NULL && li->li_tv.v_type == VAR_STRING
20933 && li->li_tv.vval.v_string != NULL
20934 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20935 pos.col = len + 1;
20936
Bram Moolenaara5525202006-03-02 22:52:09 +000020937 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020938 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020939 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020940 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020941
Bram Moolenaara5525202006-03-02 22:52:09 +000020942#ifdef FEAT_VIRTUALEDIT
20943 /* Get the virtual offset. Defaults to zero. */
20944 pos.coladd = list_find_nr(l, 2L, &error);
20945 if (error)
20946 pos.coladd = 0;
20947#endif
20948
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020949 return &pos;
20950 }
20951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020952 name = get_tv_string_chk(varp);
20953 if (name == NULL)
20954 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020955 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020957 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20958 {
20959 if (VIsual_active)
20960 return &VIsual;
20961 return &curwin->w_cursor;
20962 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020963 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020965 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20967 return NULL;
20968 return pp;
20969 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020970
20971#ifdef FEAT_VIRTUALEDIT
20972 pos.coladd = 0;
20973#endif
20974
Bram Moolenaar477933c2007-07-17 14:32:23 +000020975 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020976 {
20977 pos.col = 0;
20978 if (name[1] == '0') /* "w0": first visible line */
20979 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020980 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020981 pos.lnum = curwin->w_topline;
20982 return &pos;
20983 }
20984 else if (name[1] == '$') /* "w$": last visible line */
20985 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020986 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020987 pos.lnum = curwin->w_botline - 1;
20988 return &pos;
20989 }
20990 }
20991 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020993 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 {
20995 pos.lnum = curbuf->b_ml.ml_line_count;
20996 pos.col = 0;
20997 }
20998 else
20999 {
21000 pos.lnum = curwin->w_cursor.lnum;
21001 pos.col = (colnr_T)STRLEN(ml_get_curline());
21002 }
21003 return &pos;
21004 }
21005 return NULL;
21006}
21007
21008/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021009 * Convert list in "arg" into a position and optional file number.
21010 * When "fnump" is NULL there is no file number, only 3 items.
21011 * Note that the column is passed on as-is, the caller may want to decrement
21012 * it to use 1 for the first column.
21013 * Return FAIL when conversion is not possible, doesn't check the position for
21014 * validity.
21015 */
21016 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020021017list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021018 typval_T *arg;
21019 pos_T *posp;
21020 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020021021 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021022{
21023 list_T *l = arg->vval.v_list;
21024 long i = 0;
21025 long n;
21026
Bram Moolenaar493c1782014-05-28 14:34:46 +020021027 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21028 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021029 if (arg->v_type != VAR_LIST
21030 || l == NULL
21031 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021032 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021033 return FAIL;
21034
21035 if (fnump != NULL)
21036 {
21037 n = list_find_nr(l, i++, NULL); /* fnum */
21038 if (n < 0)
21039 return FAIL;
21040 if (n == 0)
21041 n = curbuf->b_fnum; /* current buffer */
21042 *fnump = n;
21043 }
21044
21045 n = list_find_nr(l, i++, NULL); /* lnum */
21046 if (n < 0)
21047 return FAIL;
21048 posp->lnum = n;
21049
21050 n = list_find_nr(l, i++, NULL); /* col */
21051 if (n < 0)
21052 return FAIL;
21053 posp->col = n;
21054
21055#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021056 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021057 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021058 posp->coladd = 0;
21059 else
21060 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021061#endif
21062
Bram Moolenaar493c1782014-05-28 14:34:46 +020021063 if (curswantp != NULL)
21064 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21065
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021066 return OK;
21067}
21068
21069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 * Get the length of an environment variable name.
21071 * Advance "arg" to the first character after the name.
21072 * Return 0 for error.
21073 */
21074 static int
21075get_env_len(arg)
21076 char_u **arg;
21077{
21078 char_u *p;
21079 int len;
21080
21081 for (p = *arg; vim_isIDc(*p); ++p)
21082 ;
21083 if (p == *arg) /* no name found */
21084 return 0;
21085
21086 len = (int)(p - *arg);
21087 *arg = p;
21088 return len;
21089}
21090
21091/*
21092 * Get the length of the name of a function or internal variable.
21093 * "arg" is advanced to the first non-white character after the name.
21094 * Return 0 if something is wrong.
21095 */
21096 static int
21097get_id_len(arg)
21098 char_u **arg;
21099{
21100 char_u *p;
21101 int len;
21102
21103 /* Find the end of the name. */
21104 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021105 {
21106 if (*p == ':')
21107 {
21108 /* "s:" is start of "s:var", but "n:" is not and can be used in
21109 * slice "[n:]". Also "xx:" is not a namespace. */
21110 len = (int)(p - *arg);
21111 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21112 || len > 1)
21113 break;
21114 }
21115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 if (p == *arg) /* no name found */
21117 return 0;
21118
21119 len = (int)(p - *arg);
21120 *arg = skipwhite(p);
21121
21122 return len;
21123}
21124
21125/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021126 * Get the length of the name of a variable or function.
21127 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021129 * Return -1 if curly braces expansion failed.
21130 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 * If the name contains 'magic' {}'s, expand them and return the
21132 * expanded name in an allocated string via 'alias' - caller must free.
21133 */
21134 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021135get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 char_u **arg;
21137 char_u **alias;
21138 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021139 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140{
21141 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142 char_u *p;
21143 char_u *expr_start;
21144 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145
21146 *alias = NULL; /* default to no alias */
21147
21148 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21149 && (*arg)[2] == (int)KE_SNR)
21150 {
21151 /* hard coded <SNR>, already translated */
21152 *arg += 3;
21153 return get_id_len(arg) + 3;
21154 }
21155 len = eval_fname_script(*arg);
21156 if (len > 0)
21157 {
21158 /* literal "<SID>", "s:" or "<SNR>" */
21159 *arg += len;
21160 }
21161
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021163 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021165 p = find_name_end(*arg, &expr_start, &expr_end,
21166 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167 if (expr_start != NULL)
21168 {
21169 char_u *temp_string;
21170
21171 if (!evaluate)
21172 {
21173 len += (int)(p - *arg);
21174 *arg = skipwhite(p);
21175 return len;
21176 }
21177
21178 /*
21179 * Include any <SID> etc in the expanded string:
21180 * Thus the -len here.
21181 */
21182 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21183 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021184 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 *alias = temp_string;
21186 *arg = skipwhite(p);
21187 return (int)STRLEN(temp_string);
21188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189
21190 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021191 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 EMSG2(_(e_invexpr2), *arg);
21193
21194 return len;
21195}
21196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021197/*
21198 * Find the end of a variable or function name, taking care of magic braces.
21199 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21200 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021201 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021202 * Return a pointer to just after the name. Equal to "arg" if there is no
21203 * valid name.
21204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021206find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 char_u *arg;
21208 char_u **expr_start;
21209 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021210 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021212 int mb_nest = 0;
21213 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021215 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021217 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021219 *expr_start = NULL;
21220 *expr_end = NULL;
21221 }
21222
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021223 /* Quick check for valid starting character. */
21224 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21225 return arg;
21226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021227 for (p = arg; *p != NUL
21228 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021229 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021230 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021231 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021232 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021233 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021234 if (*p == '\'')
21235 {
21236 /* skip over 'string' to avoid counting [ and ] inside it. */
21237 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21238 ;
21239 if (*p == NUL)
21240 break;
21241 }
21242 else if (*p == '"')
21243 {
21244 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21245 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21246 if (*p == '\\' && p[1] != NUL)
21247 ++p;
21248 if (*p == NUL)
21249 break;
21250 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021251 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21252 {
21253 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021254 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021255 len = (int)(p - arg);
21256 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021257 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021258 break;
21259 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021261 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021263 if (*p == '[')
21264 ++br_nest;
21265 else if (*p == ']')
21266 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021269 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021271 if (*p == '{')
21272 {
21273 mb_nest++;
21274 if (expr_start != NULL && *expr_start == NULL)
21275 *expr_start = p;
21276 }
21277 else if (*p == '}')
21278 {
21279 mb_nest--;
21280 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21281 *expr_end = p;
21282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 }
21285
21286 return p;
21287}
21288
21289/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021290 * Expands out the 'magic' {}'s in a variable/function name.
21291 * Note that this can call itself recursively, to deal with
21292 * constructs like foo{bar}{baz}{bam}
21293 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21294 * "in_start" ^
21295 * "expr_start" ^
21296 * "expr_end" ^
21297 * "in_end" ^
21298 *
21299 * Returns a new allocated string, which the caller must free.
21300 * Returns NULL for failure.
21301 */
21302 static char_u *
21303make_expanded_name(in_start, expr_start, expr_end, in_end)
21304 char_u *in_start;
21305 char_u *expr_start;
21306 char_u *expr_end;
21307 char_u *in_end;
21308{
21309 char_u c1;
21310 char_u *retval = NULL;
21311 char_u *temp_result;
21312 char_u *nextcmd = NULL;
21313
21314 if (expr_end == NULL || in_end == NULL)
21315 return NULL;
21316 *expr_start = NUL;
21317 *expr_end = NUL;
21318 c1 = *in_end;
21319 *in_end = NUL;
21320
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021321 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021322 if (temp_result != NULL && nextcmd == NULL)
21323 {
21324 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21325 + (in_end - expr_end) + 1));
21326 if (retval != NULL)
21327 {
21328 STRCPY(retval, in_start);
21329 STRCAT(retval, temp_result);
21330 STRCAT(retval, expr_end + 1);
21331 }
21332 }
21333 vim_free(temp_result);
21334
21335 *in_end = c1; /* put char back for error messages */
21336 *expr_start = '{';
21337 *expr_end = '}';
21338
21339 if (retval != NULL)
21340 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021341 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021342 if (expr_start != NULL)
21343 {
21344 /* Further expansion! */
21345 temp_result = make_expanded_name(retval, expr_start,
21346 expr_end, temp_result);
21347 vim_free(retval);
21348 retval = temp_result;
21349 }
21350 }
21351
21352 return retval;
21353}
21354
21355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021357 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 */
21359 static int
21360eval_isnamec(c)
21361 int c;
21362{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021363 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21364}
21365
21366/*
21367 * Return TRUE if character "c" can be used as the first character in a
21368 * variable or function name (excluding '{' and '}').
21369 */
21370 static int
21371eval_isnamec1(c)
21372 int c;
21373{
21374 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375}
21376
21377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 * Set number v: variable to "val".
21379 */
21380 void
21381set_vim_var_nr(idx, val)
21382 int idx;
21383 long val;
21384{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021385 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386}
21387
21388/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021389 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 */
21391 long
21392get_vim_var_nr(idx)
21393 int idx;
21394{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021395 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396}
21397
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021398/*
21399 * Get string v: variable value. Uses a static buffer, can only be used once.
21400 */
21401 char_u *
21402get_vim_var_str(idx)
21403 int idx;
21404{
21405 return get_tv_string(&vimvars[idx].vv_tv);
21406}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021407
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021409 * Get List v: variable value. Caller must take care of reference count when
21410 * needed.
21411 */
21412 list_T *
21413get_vim_var_list(idx)
21414 int idx;
21415{
21416 return vimvars[idx].vv_list;
21417}
21418
21419/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021420 * Set v:char to character "c".
21421 */
21422 void
21423set_vim_var_char(c)
21424 int c;
21425{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021426 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021427
21428#ifdef FEAT_MBYTE
21429 if (has_mbyte)
21430 buf[(*mb_char2bytes)(c, buf)] = NUL;
21431 else
21432#endif
21433 {
21434 buf[0] = c;
21435 buf[1] = NUL;
21436 }
21437 set_vim_var_string(VV_CHAR, buf, -1);
21438}
21439
21440/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021441 * Set v:count to "count" and v:count1 to "count1".
21442 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 */
21444 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021445set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 long count;
21447 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021448 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021450 if (set_prevcount)
21451 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021452 vimvars[VV_COUNT].vv_nr = count;
21453 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454}
21455
21456/*
21457 * Set string v: variable to a copy of "val".
21458 */
21459 void
21460set_vim_var_string(idx, val, len)
21461 int idx;
21462 char_u *val;
21463 int len; /* length of "val" to use or -1 (whole string) */
21464{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021465 /* Need to do this (at least) once, since we can't initialize a union.
21466 * Will always be invoked when "v:progname" is set. */
21467 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
21468
Bram Moolenaare9a41262005-01-15 22:18:47 +000021469 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021471 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021473 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021475 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476}
21477
21478/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021479 * Set List v: variable to "val".
21480 */
21481 void
21482set_vim_var_list(idx, val)
21483 int idx;
21484 list_T *val;
21485{
21486 list_unref(vimvars[idx].vv_list);
21487 vimvars[idx].vv_list = val;
21488 if (val != NULL)
21489 ++val->lv_refcount;
21490}
21491
21492/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021493 * Set Dictionary v: variable to "val".
21494 */
21495 void
21496set_vim_var_dict(idx, val)
21497 int idx;
21498 dict_T *val;
21499{
21500 int todo;
21501 hashitem_T *hi;
21502
21503 dict_unref(vimvars[idx].vv_dict);
21504 vimvars[idx].vv_dict = val;
21505 if (val != NULL)
21506 {
21507 ++val->dv_refcount;
21508
21509 /* Set readonly */
21510 todo = (int)val->dv_hashtab.ht_used;
21511 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21512 {
21513 if (HASHITEM_EMPTY(hi))
21514 continue;
21515 --todo;
21516 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21517 }
21518 }
21519}
21520
21521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522 * Set v:register if needed.
21523 */
21524 void
21525set_reg_var(c)
21526 int c;
21527{
21528 char_u regname;
21529
21530 if (c == 0 || c == ' ')
21531 regname = '"';
21532 else
21533 regname = c;
21534 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021535 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 set_vim_var_string(VV_REG, &regname, 1);
21537}
21538
21539/*
21540 * Get or set v:exception. If "oldval" == NULL, return the current value.
21541 * Otherwise, restore the value to "oldval" and return NULL.
21542 * Must always be called in pairs to save and restore v:exception! Does not
21543 * take care of memory allocations.
21544 */
21545 char_u *
21546v_exception(oldval)
21547 char_u *oldval;
21548{
21549 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021550 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551
Bram Moolenaare9a41262005-01-15 22:18:47 +000021552 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 return NULL;
21554}
21555
21556/*
21557 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21558 * Otherwise, restore the value to "oldval" and return NULL.
21559 * Must always be called in pairs to save and restore v:throwpoint! Does not
21560 * take care of memory allocations.
21561 */
21562 char_u *
21563v_throwpoint(oldval)
21564 char_u *oldval;
21565{
21566 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021567 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568
Bram Moolenaare9a41262005-01-15 22:18:47 +000021569 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 return NULL;
21571}
21572
21573#if defined(FEAT_AUTOCMD) || defined(PROTO)
21574/*
21575 * Set v:cmdarg.
21576 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21577 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21578 * Must always be called in pairs!
21579 */
21580 char_u *
21581set_cmdarg(eap, oldarg)
21582 exarg_T *eap;
21583 char_u *oldarg;
21584{
21585 char_u *oldval;
21586 char_u *newval;
21587 unsigned len;
21588
Bram Moolenaare9a41262005-01-15 22:18:47 +000021589 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021590 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021592 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021593 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021594 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 }
21596
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021597 if (eap->force_bin == FORCE_BIN)
21598 len = 6;
21599 else if (eap->force_bin == FORCE_NOBIN)
21600 len = 8;
21601 else
21602 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021603
21604 if (eap->read_edit)
21605 len += 7;
21606
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021607 if (eap->force_ff != 0)
21608 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21609# ifdef FEAT_MBYTE
21610 if (eap->force_enc != 0)
21611 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021612 if (eap->bad_char != 0)
21613 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021614# endif
21615
21616 newval = alloc(len + 1);
21617 if (newval == NULL)
21618 return NULL;
21619
21620 if (eap->force_bin == FORCE_BIN)
21621 sprintf((char *)newval, " ++bin");
21622 else if (eap->force_bin == FORCE_NOBIN)
21623 sprintf((char *)newval, " ++nobin");
21624 else
21625 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021626
21627 if (eap->read_edit)
21628 STRCAT(newval, " ++edit");
21629
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021630 if (eap->force_ff != 0)
21631 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21632 eap->cmd + eap->force_ff);
21633# ifdef FEAT_MBYTE
21634 if (eap->force_enc != 0)
21635 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21636 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021637 if (eap->bad_char == BAD_KEEP)
21638 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21639 else if (eap->bad_char == BAD_DROP)
21640 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21641 else if (eap->bad_char != 0)
21642 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021643# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021644 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021645 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646}
21647#endif
21648
21649/*
21650 * Get the value of internal variable "name".
21651 * Return OK or FAIL.
21652 */
21653 static int
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021654get_var_tv(name, len, rettv, dip, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 char_u *name;
21656 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000021657 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021658 dictitem_T **dip; /* non-NULL when typval's dict item is needed */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021659 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021660 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661{
21662 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021663 typval_T *tv = NULL;
21664 typval_T atv;
21665 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667
21668 /* truncate the name, so that we can use strcmp() */
21669 cc = name[len];
21670 name[len] = NUL;
21671
21672 /*
21673 * Check for "b:changedtick".
21674 */
21675 if (STRCMP(name, "b:changedtick") == 0)
21676 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021677 atv.v_type = VAR_NUMBER;
21678 atv.vval.v_number = curbuf->b_changedtick;
21679 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 }
21681
21682 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 * Check for user-defined variables.
21684 */
21685 else
21686 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021687 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021689 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021690 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021691 if (dip != NULL)
21692 *dip = v;
21693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 }
21695
Bram Moolenaare9a41262005-01-15 22:18:47 +000021696 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021698 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021699 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 ret = FAIL;
21701 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021702 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021703 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704
21705 name[len] = cc;
21706
21707 return ret;
21708}
21709
21710/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021711 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21712 * Also handle function call with Funcref variable: func(expr)
21713 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21714 */
21715 static int
21716handle_subscript(arg, rettv, evaluate, verbose)
21717 char_u **arg;
21718 typval_T *rettv;
21719 int evaluate; /* do more than finding the end */
21720 int verbose; /* give error messages */
21721{
21722 int ret = OK;
21723 dict_T *selfdict = NULL;
21724 char_u *s;
21725 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021726 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021727
21728 while (ret == OK
21729 && (**arg == '['
21730 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021731 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021732 && !vim_iswhite(*(*arg - 1)))
21733 {
21734 if (**arg == '(')
21735 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021736 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021737 if (evaluate)
21738 {
21739 functv = *rettv;
21740 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021741
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021742 /* Invoke the function. Recursive! */
21743 s = functv.vval.v_string;
21744 }
21745 else
21746 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021747 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021748 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21749 &len, evaluate, selfdict);
21750
21751 /* Clear the funcref afterwards, so that deleting it while
21752 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021753 if (evaluate)
21754 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021755
21756 /* Stop the expression evaluation when immediately aborting on
21757 * error, or when an interrupt occurred or an exception was thrown
21758 * but not caught. */
21759 if (aborting())
21760 {
21761 if (ret == OK)
21762 clear_tv(rettv);
21763 ret = FAIL;
21764 }
21765 dict_unref(selfdict);
21766 selfdict = NULL;
21767 }
21768 else /* **arg == '[' || **arg == '.' */
21769 {
21770 dict_unref(selfdict);
21771 if (rettv->v_type == VAR_DICT)
21772 {
21773 selfdict = rettv->vval.v_dict;
21774 if (selfdict != NULL)
21775 ++selfdict->dv_refcount;
21776 }
21777 else
21778 selfdict = NULL;
21779 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21780 {
21781 clear_tv(rettv);
21782 ret = FAIL;
21783 }
21784 }
21785 }
21786 dict_unref(selfdict);
21787 return ret;
21788}
21789
21790/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021791 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021792 * value).
21793 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021794 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021795alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021796{
Bram Moolenaar33570922005-01-25 22:26:29 +000021797 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021798}
21799
21800/*
21801 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 * The string "s" must have been allocated, it is consumed.
21803 * Return NULL for out of memory, the variable otherwise.
21804 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021806alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807 char_u *s;
21808{
Bram Moolenaar33570922005-01-25 22:26:29 +000021809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021811 rettv = alloc_tv();
21812 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021814 rettv->v_type = VAR_STRING;
21815 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 }
21817 else
21818 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021819 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820}
21821
21822/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021823 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021825 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021826free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828{
21829 if (varp != NULL)
21830 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021831 switch (varp->v_type)
21832 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021833 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021834 func_unref(varp->vval.v_string);
21835 /*FALLTHROUGH*/
21836 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021837 vim_free(varp->vval.v_string);
21838 break;
21839 case VAR_LIST:
21840 list_unref(varp->vval.v_list);
21841 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021842 case VAR_DICT:
21843 dict_unref(varp->vval.v_dict);
21844 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021845 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021846#ifdef FEAT_FLOAT
21847 case VAR_FLOAT:
21848#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000021849 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021850 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021851 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021852 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021853 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021854 break;
21855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 vim_free(varp);
21857 }
21858}
21859
21860/*
21861 * Free the memory for a variable value and set the value to NULL or 0.
21862 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021863 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021864clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021865 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866{
21867 if (varp != NULL)
21868 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021869 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021871 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021872 func_unref(varp->vval.v_string);
21873 /*FALLTHROUGH*/
21874 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021875 vim_free(varp->vval.v_string);
21876 varp->vval.v_string = NULL;
21877 break;
21878 case VAR_LIST:
21879 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021880 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021881 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021882 case VAR_DICT:
21883 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021884 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021885 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021886 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021887 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021888 varp->vval.v_number = 0;
21889 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021890#ifdef FEAT_FLOAT
21891 case VAR_FLOAT:
21892 varp->vval.v_float = 0.0;
21893 break;
21894#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021895 case VAR_UNKNOWN:
21896 break;
21897 default:
21898 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021900 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021901 }
21902}
21903
21904/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021905 * Set the value of a variable to NULL without freeing items.
21906 */
21907 static void
21908init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021909 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021910{
21911 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021912 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021913}
21914
21915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 * Get the number value of a variable.
21917 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021918 * For incompatible types, return 0.
21919 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21920 * caller of incompatible types: it sets *denote to TRUE if "denote"
21921 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 */
21923 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021924get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000021925 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021927 int error = FALSE;
21928
21929 return get_tv_number_chk(varp, &error); /* return 0L on error */
21930}
21931
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021932 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021933get_tv_number_chk(varp, denote)
21934 typval_T *varp;
21935 int *denote;
21936{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021937 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021939 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021940 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021941 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021942 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021943#ifdef FEAT_FLOAT
21944 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021945 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021946 break;
21947#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021948 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021949 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021950 break;
21951 case VAR_STRING:
21952 if (varp->vval.v_string != NULL)
21953 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021954 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021955 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021956 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021957 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021958 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021959 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021960 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021961 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021962 case VAR_SPECIAL:
21963 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21964 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021965 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021966 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021967 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021969 if (denote == NULL) /* useful for values that must be unsigned */
21970 n = -1;
21971 else
21972 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021973 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974}
21975
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021976#ifdef FEAT_FLOAT
21977 static float_T
21978get_tv_float(varp)
21979 typval_T *varp;
21980{
21981 switch (varp->v_type)
21982 {
21983 case VAR_NUMBER:
21984 return (float_T)(varp->vval.v_number);
21985#ifdef FEAT_FLOAT
21986 case VAR_FLOAT:
21987 return varp->vval.v_float;
21988 break;
21989#endif
21990 case VAR_FUNC:
21991 EMSG(_("E891: Using a Funcref as a Float"));
21992 break;
21993 case VAR_STRING:
21994 EMSG(_("E892: Using a String as a Float"));
21995 break;
21996 case VAR_LIST:
21997 EMSG(_("E893: Using a List as a Float"));
21998 break;
21999 case VAR_DICT:
22000 EMSG(_("E894: Using a Dictionary as a Float"));
22001 break;
22002 default:
22003 EMSG2(_(e_intern2), "get_tv_float()");
22004 break;
22005 }
22006 return 0;
22007}
22008#endif
22009
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022011 * Get the lnum from the first argument.
22012 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022013 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 */
22015 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022016get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000022017 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018{
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 linenr_T lnum;
22021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022022 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 if (lnum == 0) /* no valid number, try using line() */
22024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022025 rettv.v_type = VAR_NUMBER;
22026 f_line(argvars, &rettv);
22027 lnum = rettv.vval.v_number;
22028 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 }
22030 return lnum;
22031}
22032
22033/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022034 * Get the lnum from the first argument.
22035 * Also accepts "$", then "buf" is used.
22036 * Returns 0 on error.
22037 */
22038 static linenr_T
22039get_tv_lnum_buf(argvars, buf)
22040 typval_T *argvars;
22041 buf_T *buf;
22042{
22043 if (argvars[0].v_type == VAR_STRING
22044 && argvars[0].vval.v_string != NULL
22045 && argvars[0].vval.v_string[0] == '$'
22046 && buf != NULL)
22047 return buf->b_ml.ml_line_count;
22048 return get_tv_number_chk(&argvars[0], NULL);
22049}
22050
22051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 * Get the string value of a variable.
22053 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022054 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22055 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022056 * If the String variable has never been set, return an empty string.
22057 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022058 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22059 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 */
22061 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022062get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000022063 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064{
22065 static char_u mybuf[NUMBUFLEN];
22066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022067 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068}
22069
22070 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022071get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000022072 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022073 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022075 char_u *res = get_tv_string_buf_chk(varp, buf);
22076
22077 return res != NULL ? res : (char_u *)"";
22078}
22079
Bram Moolenaar7d647822014-04-05 21:28:56 +020022080/*
22081 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22082 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022083 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022084get_tv_string_chk(varp)
22085 typval_T *varp;
22086{
22087 static char_u mybuf[NUMBUFLEN];
22088
22089 return get_tv_string_buf_chk(varp, mybuf);
22090}
22091
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022092 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022093get_tv_string_buf_chk(varp, buf)
22094 typval_T *varp;
22095 char_u *buf;
22096{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022097 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022099 case VAR_NUMBER:
22100 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22101 return buf;
22102 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022103 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022104 break;
22105 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022106 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022107 break;
22108 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022109 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022110 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022111#ifdef FEAT_FLOAT
22112 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022113 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022114 break;
22115#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022116 case VAR_STRING:
22117 if (varp->vval.v_string != NULL)
22118 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022119 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022120 case VAR_SPECIAL:
22121 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22122 return buf;
22123
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022124 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022125 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022128 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129}
22130
22131/*
22132 * Find variable "name" in the list of variables.
22133 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022134 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022135 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022136 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022138 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022139find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022141 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022142 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022145 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146
Bram Moolenaara7043832005-01-21 11:56:39 +000022147 ht = find_var_ht(name, &varname);
22148 if (htp != NULL)
22149 *htp = ht;
22150 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022152 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153}
22154
22155/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022156 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022157 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022159 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022160find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000022161 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020022162 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000022163 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022164 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000022165{
Bram Moolenaar33570922005-01-25 22:26:29 +000022166 hashitem_T *hi;
22167
22168 if (*varname == NUL)
22169 {
22170 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022171 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022172 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022173 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 case 'g': return &globvars_var;
22175 case 'v': return &vimvars_var;
22176 case 'b': return &curbuf->b_bufvar;
22177 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022178#ifdef FEAT_WINDOWS
22179 case 't': return &curtab->tp_winvar;
22180#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022181 case 'l': return current_funccal == NULL
22182 ? NULL : &current_funccal->l_vars_var;
22183 case 'a': return current_funccal == NULL
22184 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022185 }
22186 return NULL;
22187 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022188
22189 hi = hash_find(ht, varname);
22190 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022191 {
22192 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022193 * worked find the variable again. Don't auto-load a script if it was
22194 * loaded already, otherwise it would be loaded every time when
22195 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022196 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022197 {
22198 /* Note: script_autoload() may make "hi" invalid. It must either
22199 * be obtained again or not used. */
22200 if (!script_autoload(varname, FALSE) || aborting())
22201 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022202 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022203 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022204 if (HASHITEM_EMPTY(hi))
22205 return NULL;
22206 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022207 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022208}
22209
22210/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022211 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022212 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022213 * Set "varname" to the start of name without ':'.
22214 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022215 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000022216find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 char_u *name;
22218 char_u **varname;
22219{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022220 hashitem_T *hi;
22221
Bram Moolenaar73627d02015-08-11 15:46:09 +020022222 if (name[0] == NUL)
22223 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 if (name[1] != ':')
22225 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022226 /* The name must not start with a colon or #. */
22227 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022228 return NULL;
22229 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022230
22231 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022232 hi = hash_find(&compat_hashtab, name);
22233 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022234 return &compat_hashtab;
22235
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022237 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022238 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 }
22240 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022241 if (*name == 'g') /* global variable */
22242 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022243 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22244 */
22245 if (vim_strchr(name + 2, ':') != NULL
22246 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022247 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022249 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022251 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022252#ifdef FEAT_WINDOWS
22253 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022254 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022255#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022256 if (*name == 'v') /* v: variable */
22257 return &vimvarht;
22258 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022259 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022260 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022261 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 if (*name == 's' /* script variable */
22263 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22264 return &SCRIPT_VARS(current_SID);
22265 return NULL;
22266}
22267
22268/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022269 * Get function call environment based on bactrace debug level
22270 */
22271 static funccall_T *
22272get_funccal()
22273{
22274 int i;
22275 funccall_T *funccal;
22276 funccall_T *temp_funccal;
22277
22278 funccal = current_funccal;
22279 if (debug_backtrace_level > 0)
22280 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022281 for (i = 0; i < debug_backtrace_level; i++)
22282 {
22283 temp_funccal = funccal->caller;
22284 if (temp_funccal)
22285 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022286 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022287 /* backtrace level overflow. reset to max */
22288 debug_backtrace_level = i;
22289 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022290 }
22291 return funccal;
22292}
22293
22294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022296 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297 * Returns NULL when it doesn't exist.
22298 */
22299 char_u *
22300get_var_value(name)
22301 char_u *name;
22302{
Bram Moolenaar33570922005-01-25 22:26:29 +000022303 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022305 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306 if (v == NULL)
22307 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309}
22310
22311/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022312 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 * sourcing this script and when executing functions defined in the script.
22314 */
22315 void
22316new_script_vars(id)
22317 scid_T id;
22318{
Bram Moolenaara7043832005-01-21 11:56:39 +000022319 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022320 hashtab_T *ht;
22321 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022322
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22324 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022325 /* Re-allocating ga_data means that an ht_array pointing to
22326 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022328 for (i = 1; i <= ga_scripts.ga_len; ++i)
22329 {
22330 ht = &SCRIPT_VARS(i);
22331 if (ht->ht_mask == HT_INIT_SIZE - 1)
22332 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022333 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022334 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022335 }
22336
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 while (ga_scripts.ga_len < id)
22338 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022339 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022340 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022341 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 }
22344 }
22345}
22346
22347/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022348 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22349 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 */
22351 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022352init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000022353 dict_T *dict;
22354 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022355 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356{
Bram Moolenaar33570922005-01-25 22:26:29 +000022357 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022358 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022359 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022360 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022361 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022362 dict_var->di_tv.vval.v_dict = dict;
22363 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022364 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022365 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22366 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367}
22368
22369/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022370 * Unreference a dictionary initialized by init_var_dict().
22371 */
22372 void
22373unref_var_dict(dict)
22374 dict_T *dict;
22375{
22376 /* Now the dict needs to be freed if no one else is using it, go back to
22377 * normal reference counting. */
22378 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22379 dict_unref(dict);
22380}
22381
22382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022384 * Frees all allocated variables and the value they contain.
22385 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 */
22387 void
Bram Moolenaara7043832005-01-21 11:56:39 +000022388vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022389 hashtab_T *ht;
22390{
22391 vars_clear_ext(ht, TRUE);
22392}
22393
22394/*
22395 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22396 */
22397 static void
22398vars_clear_ext(ht, free_val)
22399 hashtab_T *ht;
22400 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401{
Bram Moolenaara7043832005-01-21 11:56:39 +000022402 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022403 hashitem_T *hi;
22404 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405
Bram Moolenaar33570922005-01-25 22:26:29 +000022406 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022407 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022408 for (hi = ht->ht_array; todo > 0; ++hi)
22409 {
22410 if (!HASHITEM_EMPTY(hi))
22411 {
22412 --todo;
22413
Bram Moolenaar33570922005-01-25 22:26:29 +000022414 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022415 * ht_array might change then. hash_clear() takes care of it
22416 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022417 v = HI2DI(hi);
22418 if (free_val)
22419 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022420 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022421 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022422 }
22423 }
22424 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022425 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426}
22427
Bram Moolenaara7043832005-01-21 11:56:39 +000022428/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022429 * Delete a variable from hashtab "ht" at item "hi".
22430 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000022433delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000022434 hashtab_T *ht;
22435 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436{
Bram Moolenaar33570922005-01-25 22:26:29 +000022437 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022438
22439 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022440 clear_tv(&di->di_tv);
22441 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442}
22443
22444/*
22445 * List the value of one internal variable.
22446 */
22447 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022448list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000022449 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022451 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022453 char_u *tofree;
22454 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022455 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022456
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022457 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022459 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022460 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461}
22462
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022464list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 char_u *prefix;
22466 char_u *name;
22467 int type;
22468 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022469 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470{
Bram Moolenaar31859182007-08-14 20:41:13 +000022471 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22472 msg_start();
22473 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 if (name != NULL) /* "a:" vars don't have a name stored */
22475 msg_puts(name);
22476 msg_putchar(' ');
22477 msg_advance(22);
22478 if (type == VAR_NUMBER)
22479 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022480 else if (type == VAR_FUNC)
22481 msg_putchar('*');
22482 else if (type == VAR_LIST)
22483 {
22484 msg_putchar('[');
22485 if (*string == '[')
22486 ++string;
22487 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022488 else if (type == VAR_DICT)
22489 {
22490 msg_putchar('{');
22491 if (*string == '{')
22492 ++string;
22493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 else
22495 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022496
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022498
22499 if (type == VAR_FUNC)
22500 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022501 if (*first)
22502 {
22503 msg_clr_eos();
22504 *first = FALSE;
22505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506}
22507
22508/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022509 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 * If the variable already exists, the value is updated.
22511 * Otherwise the variable is created.
22512 */
22513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022514set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022516 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022517 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518{
Bram Moolenaar33570922005-01-25 22:26:29 +000022519 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022521 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022523 ht = find_var_ht(name, &varname);
22524 if (ht == NULL || *varname == NUL)
22525 {
22526 EMSG2(_(e_illvar), name);
22527 return;
22528 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022529 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022530
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022531 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22532 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022533
Bram Moolenaar33570922005-01-25 22:26:29 +000022534 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022536 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022537 if (var_check_ro(v->di_flags, name, FALSE)
22538 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022539 return;
22540 if (v->di_tv.v_type != tv->v_type
22541 && !((v->di_tv.v_type == VAR_STRING
22542 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022543 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022544 || tv->v_type == VAR_NUMBER))
22545#ifdef FEAT_FLOAT
22546 && !((v->di_tv.v_type == VAR_NUMBER
22547 || v->di_tv.v_type == VAR_FLOAT)
22548 && (tv->v_type == VAR_NUMBER
22549 || tv->v_type == VAR_FLOAT))
22550#endif
22551 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022552 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022553 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022554 return;
22555 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022556
22557 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022558 * Handle setting internal v: variables separately where needed to
22559 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 */
22561 if (ht == &vimvarht)
22562 {
22563 if (v->di_tv.v_type == VAR_STRING)
22564 {
22565 vim_free(v->di_tv.vval.v_string);
22566 if (copy || tv->v_type != VAR_STRING)
22567 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22568 else
22569 {
22570 /* Take over the string to avoid an extra alloc/free. */
22571 v->di_tv.vval.v_string = tv->vval.v_string;
22572 tv->vval.v_string = NULL;
22573 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022574 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022576 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022579 if (STRCMP(varname, "searchforward") == 0)
22580 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022581#ifdef FEAT_SEARCH_EXTRA
22582 else if (STRCMP(varname, "hlsearch") == 0)
22583 {
22584 no_hlsearch = !v->di_tv.vval.v_number;
22585 redraw_all_later(SOME_VALID);
22586 }
22587#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022588 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022589 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022590 else if (v->di_tv.v_type != tv->v_type)
22591 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 }
22593
22594 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 }
22596 else /* add a new variable */
22597 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022598 /* Can't add "v:" variable. */
22599 if (ht == &vimvarht)
22600 {
22601 EMSG2(_(e_illvar), name);
22602 return;
22603 }
22604
Bram Moolenaar92124a32005-06-17 22:03:40 +000022605 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022606 if (!valid_varname(varname))
22607 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022608
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022609 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22610 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022611 if (v == NULL)
22612 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022613 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022614 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022616 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 return;
22618 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022619 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022621
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022622 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022623 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022624 else
22625 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022626 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022627 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022628 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630}
22631
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022632/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022633 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022634 * Also give an error message.
22635 */
22636 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022637var_check_ro(flags, name, use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022638 int flags;
22639 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022640 int use_gettext;
Bram Moolenaar33570922005-01-25 22:26:29 +000022641{
22642 if (flags & DI_FLAGS_RO)
22643 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022644 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022645 return TRUE;
22646 }
22647 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22648 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022649 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022650 return TRUE;
22651 }
22652 return FALSE;
22653}
22654
22655/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022656 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22657 * Also give an error message.
22658 */
22659 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022660var_check_fixed(flags, name, use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022661 int flags;
22662 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022663 int use_gettext;
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022664{
22665 if (flags & DI_FLAGS_FIX)
22666 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022667 EMSG2(_("E795: Cannot delete variable %s"),
22668 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022669 return TRUE;
22670 }
22671 return FALSE;
22672}
22673
22674/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022675 * Check if a funcref is assigned to a valid variable name.
22676 * Return TRUE and give an error if not.
22677 */
22678 static int
22679var_check_func_name(name, new_var)
22680 char_u *name; /* points to start of variable name */
22681 int new_var; /* TRUE when creating the variable */
22682{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022683 /* Allow for w: b: s: and t:. */
22684 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022685 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22686 ? name[2] : name[0]))
22687 {
22688 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22689 name);
22690 return TRUE;
22691 }
22692 /* Don't allow hiding a function. When "v" is not NULL we might be
22693 * assigning another function to the same var, the type is checked
22694 * below. */
22695 if (new_var && function_exists(name))
22696 {
22697 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22698 name);
22699 return TRUE;
22700 }
22701 return FALSE;
22702}
22703
22704/*
22705 * Check if a variable name is valid.
22706 * Return FALSE and give an error if not.
22707 */
22708 static int
22709valid_varname(varname)
22710 char_u *varname;
22711{
22712 char_u *p;
22713
22714 for (p = varname; *p != NUL; ++p)
22715 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22716 && *p != AUTOLOAD_CHAR)
22717 {
22718 EMSG2(_(e_illvar), varname);
22719 return FALSE;
22720 }
22721 return TRUE;
22722}
22723
22724/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022725 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022726 * Also give an error message, using "name" or _("name") when use_gettext is
22727 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022728 */
22729 static int
Bram Moolenaar77354e72015-04-21 16:49:05 +020022730tv_check_lock(lock, name, use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022731 int lock;
22732 char_u *name;
Bram Moolenaar77354e72015-04-21 16:49:05 +020022733 int use_gettext;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022734{
22735 if (lock & VAR_LOCKED)
22736 {
22737 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022738 name == NULL ? (char_u *)_("Unknown")
22739 : use_gettext ? (char_u *)_(name)
22740 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022741 return TRUE;
22742 }
22743 if (lock & VAR_FIXED)
22744 {
22745 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022746 name == NULL ? (char_u *)_("Unknown")
22747 : use_gettext ? (char_u *)_(name)
22748 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022749 return TRUE;
22750 }
22751 return FALSE;
22752}
22753
22754/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022755 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022756 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022757 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022758 * It is OK for "from" and "to" to point to the same item. This is used to
22759 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022760 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022761 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022762copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000022763 typval_T *from;
22764 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022765{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022766 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022767 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022768 switch (from->v_type)
22769 {
22770 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022771 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022772 to->vval.v_number = from->vval.v_number;
22773 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022774#ifdef FEAT_FLOAT
22775 case VAR_FLOAT:
22776 to->vval.v_float = from->vval.v_float;
22777 break;
22778#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022779 case VAR_STRING:
22780 case VAR_FUNC:
22781 if (from->vval.v_string == NULL)
22782 to->vval.v_string = NULL;
22783 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022784 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022785 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022786 if (from->v_type == VAR_FUNC)
22787 func_ref(to->vval.v_string);
22788 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022789 break;
22790 case VAR_LIST:
22791 if (from->vval.v_list == NULL)
22792 to->vval.v_list = NULL;
22793 else
22794 {
22795 to->vval.v_list = from->vval.v_list;
22796 ++to->vval.v_list->lv_refcount;
22797 }
22798 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022799 case VAR_DICT:
22800 if (from->vval.v_dict == NULL)
22801 to->vval.v_dict = NULL;
22802 else
22803 {
22804 to->vval.v_dict = from->vval.v_dict;
22805 ++to->vval.v_dict->dv_refcount;
22806 }
22807 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022808 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022809 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022810 break;
22811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812}
22813
22814/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022815 * Make a copy of an item.
22816 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022817 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22818 * reference to an already copied list/dict can be used.
22819 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022820 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022821 static int
22822item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000022823 typval_T *from;
22824 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022825 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022826 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022827{
22828 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022829 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022830
Bram Moolenaar33570922005-01-25 22:26:29 +000022831 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022832 {
22833 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022834 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022835 }
22836 ++recurse;
22837
22838 switch (from->v_type)
22839 {
22840 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022841#ifdef FEAT_FLOAT
22842 case VAR_FLOAT:
22843#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022844 case VAR_STRING:
22845 case VAR_FUNC:
22846 copy_tv(from, to);
22847 break;
22848 case VAR_LIST:
22849 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022850 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022851 if (from->vval.v_list == NULL)
22852 to->vval.v_list = NULL;
22853 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22854 {
22855 /* use the copy made earlier */
22856 to->vval.v_list = from->vval.v_list->lv_copylist;
22857 ++to->vval.v_list->lv_refcount;
22858 }
22859 else
22860 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22861 if (to->vval.v_list == NULL)
22862 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022863 break;
22864 case VAR_DICT:
22865 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022866 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022867 if (from->vval.v_dict == NULL)
22868 to->vval.v_dict = NULL;
22869 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22870 {
22871 /* use the copy made earlier */
22872 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22873 ++to->vval.v_dict->dv_refcount;
22874 }
22875 else
22876 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22877 if (to->vval.v_dict == NULL)
22878 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022879 break;
22880 default:
22881 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022882 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022883 }
22884 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022885 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022886}
22887
22888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 * ":echo expr1 ..." print each argument separated with a space, add a
22890 * newline at the end.
22891 * ":echon expr1 ..." print each argument plain.
22892 */
22893 void
22894ex_echo(eap)
22895 exarg_T *eap;
22896{
22897 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022898 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022899 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 char_u *p;
22901 int needclr = TRUE;
22902 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022903 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904
22905 if (eap->skip)
22906 ++emsg_skip;
22907 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22908 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022909 /* If eval1() causes an error message the text from the command may
22910 * still need to be cleared. E.g., "echo 22,44". */
22911 need_clr_eos = needclr;
22912
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022914 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 {
22916 /*
22917 * Report the invalid expression unless the expression evaluation
22918 * has been cancelled due to an aborting error, an interrupt, or an
22919 * exception.
22920 */
22921 if (!aborting())
22922 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022923 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924 break;
22925 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022926 need_clr_eos = FALSE;
22927
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 if (!eap->skip)
22929 {
22930 if (atstart)
22931 {
22932 atstart = FALSE;
22933 /* Call msg_start() after eval1(), evaluating the expression
22934 * may cause a message to appear. */
22935 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022936 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022937 /* Mark the saved text as finishing the line, so that what
22938 * follows is displayed on a new line when scrolling back
22939 * at the more prompt. */
22940 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022941 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022943 }
22944 else if (eap->cmdidx == CMD_echo)
22945 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022946 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022947 if (p != NULL)
22948 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022950 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022952 if (*p != TAB && needclr)
22953 {
22954 /* remove any text still there from the command */
22955 msg_clr_eos();
22956 needclr = FALSE;
22957 }
22958 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 }
22960 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022961 {
22962#ifdef FEAT_MBYTE
22963 if (has_mbyte)
22964 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022965 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022966
22967 (void)msg_outtrans_len_attr(p, i, echo_attr);
22968 p += i - 1;
22969 }
22970 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022972 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022975 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022977 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 arg = skipwhite(arg);
22979 }
22980 eap->nextcmd = check_nextcmd(arg);
22981
22982 if (eap->skip)
22983 --emsg_skip;
22984 else
22985 {
22986 /* remove text that may still be there from the command */
22987 if (needclr)
22988 msg_clr_eos();
22989 if (eap->cmdidx == CMD_echo)
22990 msg_end();
22991 }
22992}
22993
22994/*
22995 * ":echohl {name}".
22996 */
22997 void
22998ex_echohl(eap)
22999 exarg_T *eap;
23000{
23001 int id;
23002
23003 id = syn_name2id(eap->arg);
23004 if (id == 0)
23005 echo_attr = 0;
23006 else
23007 echo_attr = syn_id2attr(id);
23008}
23009
23010/*
23011 * ":execute expr1 ..." execute the result of an expression.
23012 * ":echomsg expr1 ..." Print a message
23013 * ":echoerr expr1 ..." Print an error
23014 * Each gets spaces around each argument and a newline at the end for
23015 * echo commands
23016 */
23017 void
23018ex_execute(eap)
23019 exarg_T *eap;
23020{
23021 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023022 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023 int ret = OK;
23024 char_u *p;
23025 garray_T ga;
23026 int len;
23027 int save_did_emsg;
23028
23029 ga_init2(&ga, 1, 80);
23030
23031 if (eap->skip)
23032 ++emsg_skip;
23033 while (*arg != NUL && *arg != '|' && *arg != '\n')
23034 {
23035 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023036 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 {
23038 /*
23039 * Report the invalid expression unless the expression evaluation
23040 * has been cancelled due to an aborting error, an interrupt, or an
23041 * exception.
23042 */
23043 if (!aborting())
23044 EMSG2(_(e_invexpr2), p);
23045 ret = FAIL;
23046 break;
23047 }
23048
23049 if (!eap->skip)
23050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023051 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 len = (int)STRLEN(p);
23053 if (ga_grow(&ga, len + 2) == FAIL)
23054 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023055 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 ret = FAIL;
23057 break;
23058 }
23059 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062 ga.ga_len += len;
23063 }
23064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023065 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066 arg = skipwhite(arg);
23067 }
23068
23069 if (ret != FAIL && ga.ga_data != NULL)
23070 {
23071 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023074 out_flush();
23075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076 else if (eap->cmdidx == CMD_echoerr)
23077 {
23078 /* We don't want to abort following commands, restore did_emsg. */
23079 save_did_emsg = did_emsg;
23080 EMSG((char_u *)ga.ga_data);
23081 if (!force_abort)
23082 did_emsg = save_did_emsg;
23083 }
23084 else if (eap->cmdidx == CMD_execute)
23085 do_cmdline((char_u *)ga.ga_data,
23086 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23087 }
23088
23089 ga_clear(&ga);
23090
23091 if (eap->skip)
23092 --emsg_skip;
23093
23094 eap->nextcmd = check_nextcmd(arg);
23095}
23096
23097/*
23098 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23099 * "arg" points to the "&" or '+' when called, to "option" when returning.
23100 * Returns NULL when no option name found. Otherwise pointer to the char
23101 * after the option name.
23102 */
23103 static char_u *
23104find_option_end(arg, opt_flags)
23105 char_u **arg;
23106 int *opt_flags;
23107{
23108 char_u *p = *arg;
23109
23110 ++p;
23111 if (*p == 'g' && p[1] == ':')
23112 {
23113 *opt_flags = OPT_GLOBAL;
23114 p += 2;
23115 }
23116 else if (*p == 'l' && p[1] == ':')
23117 {
23118 *opt_flags = OPT_LOCAL;
23119 p += 2;
23120 }
23121 else
23122 *opt_flags = 0;
23123
23124 if (!ASCII_ISALPHA(*p))
23125 return NULL;
23126 *arg = p;
23127
23128 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23129 p += 4; /* termcap option */
23130 else
23131 while (ASCII_ISALPHA(*p))
23132 ++p;
23133 return p;
23134}
23135
23136/*
23137 * ":function"
23138 */
23139 void
23140ex_function(eap)
23141 exarg_T *eap;
23142{
23143 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023144 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 int j;
23146 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023148 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 char_u *name = NULL;
23150 char_u *p;
23151 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023152 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 garray_T newargs;
23154 garray_T newlines;
23155 int varargs = FALSE;
23156 int mustend = FALSE;
23157 int flags = 0;
23158 ufunc_T *fp;
23159 int indent;
23160 int nesting;
23161 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 dictitem_T *v;
23163 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023164 static int func_nr = 0; /* number for nameless function */
23165 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023166 hashtab_T *ht;
23167 int todo;
23168 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023169 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170
23171 /*
23172 * ":function" without argument: list functions.
23173 */
23174 if (ends_excmd(*eap->arg))
23175 {
23176 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023177 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023178 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023179 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023180 {
23181 if (!HASHITEM_EMPTY(hi))
23182 {
23183 --todo;
23184 fp = HI2UF(hi);
23185 if (!isdigit(*fp->uf_name))
23186 list_func_head(fp, FALSE);
23187 }
23188 }
23189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 eap->nextcmd = check_nextcmd(eap->arg);
23191 return;
23192 }
23193
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023194 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023195 * ":function /pat": list functions matching pattern.
23196 */
23197 if (*eap->arg == '/')
23198 {
23199 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23200 if (!eap->skip)
23201 {
23202 regmatch_T regmatch;
23203
23204 c = *p;
23205 *p = NUL;
23206 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23207 *p = c;
23208 if (regmatch.regprog != NULL)
23209 {
23210 regmatch.rm_ic = p_ic;
23211
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023212 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023213 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23214 {
23215 if (!HASHITEM_EMPTY(hi))
23216 {
23217 --todo;
23218 fp = HI2UF(hi);
23219 if (!isdigit(*fp->uf_name)
23220 && vim_regexec(&regmatch, fp->uf_name, 0))
23221 list_func_head(fp, FALSE);
23222 }
23223 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023224 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023225 }
23226 }
23227 if (*p == '/')
23228 ++p;
23229 eap->nextcmd = check_nextcmd(p);
23230 return;
23231 }
23232
23233 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023234 * Get the function name. There are these situations:
23235 * func normal function name
23236 * "name" == func, "fudi.fd_dict" == NULL
23237 * dict.func new dictionary entry
23238 * "name" == NULL, "fudi.fd_dict" set,
23239 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23240 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023241 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023242 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23243 * dict.func existing dict entry that's not a Funcref
23244 * "name" == NULL, "fudi.fd_dict" set,
23245 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023246 * s:func script-local function name
23247 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023250 name = trans_function_name(&p, eap->skip, 0, &fudi);
23251 paren = (vim_strchr(p, '(') != NULL);
23252 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253 {
23254 /*
23255 * Return on an invalid expression in braces, unless the expression
23256 * evaluation has been cancelled due to an aborting error, an
23257 * interrupt, or an exception.
23258 */
23259 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023260 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023261 if (!eap->skip && fudi.fd_newkey != NULL)
23262 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023263 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266 else
23267 eap->skip = TRUE;
23268 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023269
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 /* An error in a function call during evaluation of an expression in magic
23271 * braces should not cause the function not to be defined. */
23272 saved_did_emsg = did_emsg;
23273 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274
23275 /*
23276 * ":function func" with only function name: list function.
23277 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023278 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 {
23280 if (!ends_excmd(*skipwhite(p)))
23281 {
23282 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023283 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284 }
23285 eap->nextcmd = check_nextcmd(p);
23286 if (eap->nextcmd != NULL)
23287 *p = NUL;
23288 if (!eap->skip && !got_int)
23289 {
23290 fp = find_func(name);
23291 if (fp != NULL)
23292 {
23293 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023294 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023296 if (FUNCLINE(fp, j) == NULL)
23297 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 msg_putchar('\n');
23299 msg_outnum((long)(j + 1));
23300 if (j < 9)
23301 msg_putchar(' ');
23302 if (j < 99)
23303 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023304 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 out_flush(); /* show a line at a time */
23306 ui_breakcheck();
23307 }
23308 if (!got_int)
23309 {
23310 msg_putchar('\n');
23311 msg_puts((char_u *)" endfunction");
23312 }
23313 }
23314 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023315 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023317 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023318 }
23319
23320 /*
23321 * ":function name(arg1, arg2)" Define function.
23322 */
23323 p = skipwhite(p);
23324 if (*p != '(')
23325 {
23326 if (!eap->skip)
23327 {
23328 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023329 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 }
23331 /* attempt to continue by skipping some text */
23332 if (vim_strchr(p, '(') != NULL)
23333 p = vim_strchr(p, '(');
23334 }
23335 p = skipwhite(p + 1);
23336
23337 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23338 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23339
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023340 if (!eap->skip)
23341 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023342 /* Check the name of the function. Unless it's a dictionary function
23343 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023344 if (name != NULL)
23345 arg = name;
23346 else
23347 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023348 if (arg != NULL && (fudi.fd_di == NULL
23349 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023350 {
23351 if (*arg == K_SPECIAL)
23352 j = 3;
23353 else
23354 j = 0;
23355 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23356 : eval_isnamec(arg[j])))
23357 ++j;
23358 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023359 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023360 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023361 /* Disallow using the g: dict. */
23362 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23363 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023364 }
23365
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 /*
23367 * Isolate the arguments: "arg1, arg2, ...)"
23368 */
23369 while (*p != ')')
23370 {
23371 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23372 {
23373 varargs = TRUE;
23374 p += 3;
23375 mustend = TRUE;
23376 }
23377 else
23378 {
23379 arg = p;
23380 while (ASCII_ISALNUM(*p) || *p == '_')
23381 ++p;
23382 if (arg == p || isdigit(*arg)
23383 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23384 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23385 {
23386 if (!eap->skip)
23387 EMSG2(_("E125: Illegal argument: %s"), arg);
23388 break;
23389 }
23390 if (ga_grow(&newargs, 1) == FAIL)
23391 goto erret;
23392 c = *p;
23393 *p = NUL;
23394 arg = vim_strsave(arg);
23395 if (arg == NULL)
23396 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023397
23398 /* Check for duplicate argument name. */
23399 for (i = 0; i < newargs.ga_len; ++i)
23400 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23401 {
23402 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023403 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023404 goto erret;
23405 }
23406
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23408 *p = c;
23409 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 if (*p == ',')
23411 ++p;
23412 else
23413 mustend = TRUE;
23414 }
23415 p = skipwhite(p);
23416 if (mustend && *p != ')')
23417 {
23418 if (!eap->skip)
23419 EMSG2(_(e_invarg2), eap->arg);
23420 break;
23421 }
23422 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023423 if (*p != ')')
23424 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 ++p; /* skip the ')' */
23426
Bram Moolenaare9a41262005-01-15 22:18:47 +000023427 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428 for (;;)
23429 {
23430 p = skipwhite(p);
23431 if (STRNCMP(p, "range", 5) == 0)
23432 {
23433 flags |= FC_RANGE;
23434 p += 5;
23435 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023436 else if (STRNCMP(p, "dict", 4) == 0)
23437 {
23438 flags |= FC_DICT;
23439 p += 4;
23440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 else if (STRNCMP(p, "abort", 5) == 0)
23442 {
23443 flags |= FC_ABORT;
23444 p += 5;
23445 }
23446 else
23447 break;
23448 }
23449
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023450 /* When there is a line break use what follows for the function body.
23451 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23452 if (*p == '\n')
23453 line_arg = p + 1;
23454 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 EMSG(_(e_trailing));
23456
23457 /*
23458 * Read the body of the function, until ":endfunction" is found.
23459 */
23460 if (KeyTyped)
23461 {
23462 /* Check if the function already exists, don't let the user type the
23463 * whole function before telling him it doesn't work! For a script we
23464 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023465 if (!eap->skip && !eap->forceit)
23466 {
23467 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23468 EMSG(_(e_funcdict));
23469 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023470 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023473 if (!eap->skip && did_emsg)
23474 goto erret;
23475
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 msg_putchar('\n'); /* don't overwrite the function name */
23477 cmdline_row = msg_row;
23478 }
23479
23480 indent = 2;
23481 nesting = 0;
23482 for (;;)
23483 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023484 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023485 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023486 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023487 saved_wait_return = FALSE;
23488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023490 sourcing_lnum_off = sourcing_lnum;
23491
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023492 if (line_arg != NULL)
23493 {
23494 /* Use eap->arg, split up in parts by line breaks. */
23495 theline = line_arg;
23496 p = vim_strchr(theline, '\n');
23497 if (p == NULL)
23498 line_arg += STRLEN(line_arg);
23499 else
23500 {
23501 *p = NUL;
23502 line_arg = p + 1;
23503 }
23504 }
23505 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 theline = getcmdline(':', 0L, indent);
23507 else
23508 theline = eap->getline(':', eap->cookie, indent);
23509 if (KeyTyped)
23510 lines_left = Rows - 1;
23511 if (theline == NULL)
23512 {
23513 EMSG(_("E126: Missing :endfunction"));
23514 goto erret;
23515 }
23516
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023517 /* Detect line continuation: sourcing_lnum increased more than one. */
23518 if (sourcing_lnum > sourcing_lnum_off + 1)
23519 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23520 else
23521 sourcing_lnum_off = 0;
23522
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 if (skip_until != NULL)
23524 {
23525 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23526 * don't check for ":endfunc". */
23527 if (STRCMP(theline, skip_until) == 0)
23528 {
23529 vim_free(skip_until);
23530 skip_until = NULL;
23531 }
23532 }
23533 else
23534 {
23535 /* skip ':' and blanks*/
23536 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23537 ;
23538
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023539 /* Check for "endfunction". */
23540 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023542 if (line_arg == NULL)
23543 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544 break;
23545 }
23546
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023547 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548 * at "end". */
23549 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23550 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023551 else if (STRNCMP(p, "if", 2) == 0
23552 || STRNCMP(p, "wh", 2) == 0
23553 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 || STRNCMP(p, "try", 3) == 0)
23555 indent += 2;
23556
23557 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023558 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023560 if (*p == '!')
23561 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023563 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23564 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023566 ++nesting;
23567 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 }
23569 }
23570
23571 /* Check for ":append" or ":insert". */
23572 p = skip_range(p, NULL);
23573 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23574 || (p[0] == 'i'
23575 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23576 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23577 skip_until = vim_strsave((char_u *)".");
23578
23579 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23580 arg = skipwhite(skiptowhite(p));
23581 if (arg[0] == '<' && arg[1] =='<'
23582 && ((p[0] == 'p' && p[1] == 'y'
23583 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23584 || (p[0] == 'p' && p[1] == 'e'
23585 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23586 || (p[0] == 't' && p[1] == 'c'
23587 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023588 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23589 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023590 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23591 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023592 || (p[0] == 'm' && p[1] == 'z'
23593 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023594 ))
23595 {
23596 /* ":python <<" continues until a dot, like ":append" */
23597 p = skipwhite(arg + 2);
23598 if (*p == NUL)
23599 skip_until = vim_strsave((char_u *)".");
23600 else
23601 skip_until = vim_strsave(p);
23602 }
23603 }
23604
23605 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023606 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023607 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023608 if (line_arg == NULL)
23609 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023611 }
23612
23613 /* Copy the line to newly allocated memory. get_one_sourceline()
23614 * allocates 250 bytes per line, this saves 80% on average. The cost
23615 * is an extra alloc/free. */
23616 p = vim_strsave(theline);
23617 if (p != NULL)
23618 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023619 if (line_arg == NULL)
23620 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023621 theline = p;
23622 }
23623
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023624 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23625
23626 /* Add NULL lines for continuation lines, so that the line count is
23627 * equal to the index in the growarray. */
23628 while (sourcing_lnum_off-- > 0)
23629 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023630
23631 /* Check for end of eap->arg. */
23632 if (line_arg != NULL && *line_arg == NUL)
23633 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 }
23635
23636 /* Don't define the function when skipping commands or when an error was
23637 * detected. */
23638 if (eap->skip || did_emsg)
23639 goto erret;
23640
23641 /*
23642 * If there are no errors, add the function
23643 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023644 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023645 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023646 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023647 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023648 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023649 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023650 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023651 goto erret;
23652 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023653
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023654 fp = find_func(name);
23655 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023657 if (!eap->forceit)
23658 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023659 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023660 goto erret;
23661 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023662 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023663 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023664 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023665 name);
23666 goto erret;
23667 }
23668 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023669 ga_clear_strings(&(fp->uf_args));
23670 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023671 vim_free(name);
23672 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023674 }
23675 else
23676 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023677 char numbuf[20];
23678
23679 fp = NULL;
23680 if (fudi.fd_newkey == NULL && !eap->forceit)
23681 {
23682 EMSG(_(e_funcdict));
23683 goto erret;
23684 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023685 if (fudi.fd_di == NULL)
23686 {
23687 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023688 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023689 goto erret;
23690 }
23691 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023692 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023693 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023694
23695 /* Give the function a sequential number. Can only be used with a
23696 * Funcref! */
23697 vim_free(name);
23698 sprintf(numbuf, "%d", ++func_nr);
23699 name = vim_strsave((char_u *)numbuf);
23700 if (name == NULL)
23701 goto erret;
23702 }
23703
23704 if (fp == NULL)
23705 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023706 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023707 {
23708 int slen, plen;
23709 char_u *scriptname;
23710
23711 /* Check that the autoload name matches the script name. */
23712 j = FAIL;
23713 if (sourcing_name != NULL)
23714 {
23715 scriptname = autoload_name(name);
23716 if (scriptname != NULL)
23717 {
23718 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023719 plen = (int)STRLEN(p);
23720 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023721 if (slen > plen && fnamecmp(p,
23722 sourcing_name + slen - plen) == 0)
23723 j = OK;
23724 vim_free(scriptname);
23725 }
23726 }
23727 if (j == FAIL)
23728 {
23729 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23730 goto erret;
23731 }
23732 }
23733
23734 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023735 if (fp == NULL)
23736 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023737
23738 if (fudi.fd_dict != NULL)
23739 {
23740 if (fudi.fd_di == NULL)
23741 {
23742 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023743 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023744 if (fudi.fd_di == NULL)
23745 {
23746 vim_free(fp);
23747 goto erret;
23748 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023749 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23750 {
23751 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023752 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023753 goto erret;
23754 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023755 }
23756 else
23757 /* overwrite existing dict entry */
23758 clear_tv(&fudi.fd_di->di_tv);
23759 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023760 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023761 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023762 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023763
23764 /* behave like "dict" was used */
23765 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023766 }
23767
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023769 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023770 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23771 {
23772 vim_free(fp);
23773 goto erret;
23774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023775 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023776 fp->uf_args = newargs;
23777 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023778#ifdef FEAT_PROFILE
23779 fp->uf_tml_count = NULL;
23780 fp->uf_tml_total = NULL;
23781 fp->uf_tml_self = NULL;
23782 fp->uf_profiling = FALSE;
23783 if (prof_def_func())
23784 func_do_profile(fp);
23785#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023786 fp->uf_varargs = varargs;
23787 fp->uf_flags = flags;
23788 fp->uf_calls = 0;
23789 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023790 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791
23792erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793 ga_clear_strings(&newargs);
23794 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023795ret_free:
23796 vim_free(skip_until);
23797 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023800 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801}
23802
23803/*
23804 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023805 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023806 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023807 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023808 * TFN_INT: internal function name OK
23809 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023810 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 * Advances "pp" to just after the function name (if no error).
23812 */
23813 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023814trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815 char_u **pp;
23816 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023817 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000023818 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023820 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821 char_u *start;
23822 char_u *end;
23823 int lead;
23824 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023826 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023827
23828 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023829 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023831
23832 /* Check for hard coded <SNR>: already translated function ID (from a user
23833 * command). */
23834 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23835 && (*pp)[2] == (int)KE_SNR)
23836 {
23837 *pp += 3;
23838 len = get_id_len(pp) + 3;
23839 return vim_strnsave(start, len);
23840 }
23841
23842 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23843 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023845 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023847
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023848 /* Note that TFN_ flags use the same values as GLV_ flags. */
23849 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023850 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023851 if (end == start)
23852 {
23853 if (!skip)
23854 EMSG(_("E129: Function name required"));
23855 goto theend;
23856 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023857 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023858 {
23859 /*
23860 * Report an invalid expression in braces, unless the expression
23861 * evaluation has been cancelled due to an aborting error, an
23862 * interrupt, or an exception.
23863 */
23864 if (!aborting())
23865 {
23866 if (end != NULL)
23867 EMSG2(_(e_invarg2), start);
23868 }
23869 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023870 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023871 goto theend;
23872 }
23873
23874 if (lv.ll_tv != NULL)
23875 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023876 if (fdp != NULL)
23877 {
23878 fdp->fd_dict = lv.ll_dict;
23879 fdp->fd_newkey = lv.ll_newkey;
23880 lv.ll_newkey = NULL;
23881 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023882 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023883 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23884 {
23885 name = vim_strsave(lv.ll_tv->vval.v_string);
23886 *pp = end;
23887 }
23888 else
23889 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023890 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23891 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023892 EMSG(_(e_funcref));
23893 else
23894 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023895 name = NULL;
23896 }
23897 goto theend;
23898 }
23899
23900 if (lv.ll_name == NULL)
23901 {
23902 /* Error found, but continue after the function name. */
23903 *pp = end;
23904 goto theend;
23905 }
23906
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023907 /* Check if the name is a Funcref. If so, use the value. */
23908 if (lv.ll_exp_name != NULL)
23909 {
23910 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023911 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023912 if (name == lv.ll_exp_name)
23913 name = NULL;
23914 }
23915 else
23916 {
23917 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023918 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023919 if (name == *pp)
23920 name = NULL;
23921 }
23922 if (name != NULL)
23923 {
23924 name = vim_strsave(name);
23925 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023926 if (STRNCMP(name, "<SNR>", 5) == 0)
23927 {
23928 /* Change "<SNR>" to the byte sequence. */
23929 name[0] = K_SPECIAL;
23930 name[1] = KS_EXTRA;
23931 name[2] = (int)KE_SNR;
23932 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23933 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023934 goto theend;
23935 }
23936
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023937 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023938 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023939 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023940 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23941 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23942 {
23943 /* When there was "s:" already or the name expanded to get a
23944 * leading "s:" then remove it. */
23945 lv.ll_name += 2;
23946 len -= 2;
23947 lead = 2;
23948 }
23949 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023950 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023951 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023952 /* skip over "s:" and "g:" */
23953 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023954 lv.ll_name += 2;
23955 len = (int)(end - lv.ll_name);
23956 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023957
23958 /*
23959 * Copy the function name to allocated memory.
23960 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23961 * Accept <SNR>123_name() outside a script.
23962 */
23963 if (skip)
23964 lead = 0; /* do nothing */
23965 else if (lead > 0)
23966 {
23967 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023968 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23969 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023970 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023971 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023972 if (current_SID <= 0)
23973 {
23974 EMSG(_(e_usingsid));
23975 goto theend;
23976 }
23977 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23978 lead += (int)STRLEN(sid_buf);
23979 }
23980 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023981 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023982 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023983 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023984 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023985 goto theend;
23986 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023987 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023988 {
23989 char_u *cp = vim_strchr(lv.ll_name, ':');
23990
23991 if (cp != NULL && cp < end)
23992 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023993 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023994 goto theend;
23995 }
23996 }
23997
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023998 name = alloc((unsigned)(len + lead + 1));
23999 if (name != NULL)
24000 {
24001 if (lead > 0)
24002 {
24003 name[0] = K_SPECIAL;
24004 name[1] = KS_EXTRA;
24005 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024006 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024007 STRCPY(name + 3, sid_buf);
24008 }
24009 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024010 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024011 }
24012 *pp = end;
24013
24014theend:
24015 clear_lval(&lv);
24016 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024017}
24018
24019/*
24020 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24021 * Return 2 if "p" starts with "s:".
24022 * Return 0 otherwise.
24023 */
24024 static int
24025eval_fname_script(p)
24026 char_u *p;
24027{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024028 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24029 * the standard library function. */
24030 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24031 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 return 5;
24033 if (p[0] == 's' && p[1] == ':')
24034 return 2;
24035 return 0;
24036}
24037
24038/*
24039 * Return TRUE if "p" starts with "<SID>" or "s:".
24040 * Only works if eval_fname_script() returned non-zero for "p"!
24041 */
24042 static int
24043eval_fname_sid(p)
24044 char_u *p;
24045{
24046 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24047}
24048
24049/*
24050 * List the head of the function: "name(arg1, arg2)".
24051 */
24052 static void
24053list_func_head(fp, indent)
24054 ufunc_T *fp;
24055 int indent;
24056{
24057 int j;
24058
24059 msg_start();
24060 if (indent)
24061 MSG_PUTS(" ");
24062 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024063 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 {
24065 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024066 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067 }
24068 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024069 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024071 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072 {
24073 if (j)
24074 MSG_PUTS(", ");
24075 msg_puts(FUNCARG(fp, j));
24076 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024077 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 {
24079 if (j)
24080 MSG_PUTS(", ");
24081 MSG_PUTS("...");
24082 }
24083 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024084 if (fp->uf_flags & FC_ABORT)
24085 MSG_PUTS(" abort");
24086 if (fp->uf_flags & FC_RANGE)
24087 MSG_PUTS(" range");
24088 if (fp->uf_flags & FC_DICT)
24089 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024090 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024091 if (p_verbose > 0)
24092 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093}
24094
24095/*
24096 * Find a function by name, return pointer to it in ufuncs.
24097 * Return NULL for unknown function.
24098 */
24099 static ufunc_T *
24100find_func(name)
24101 char_u *name;
24102{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024103 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024105 hi = hash_find(&func_hashtab, name);
24106 if (!HASHITEM_EMPTY(hi))
24107 return HI2UF(hi);
24108 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109}
24110
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024111#if defined(EXITFREE) || defined(PROTO)
24112 void
24113free_all_functions()
24114{
24115 hashitem_T *hi;
24116
24117 /* Need to start all over every time, because func_free() may change the
24118 * hash table. */
24119 while (func_hashtab.ht_used > 0)
24120 for (hi = func_hashtab.ht_array; ; ++hi)
24121 if (!HASHITEM_EMPTY(hi))
24122 {
24123 func_free(HI2UF(hi));
24124 break;
24125 }
24126}
24127#endif
24128
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024129 int
24130translated_function_exists(name)
24131 char_u *name;
24132{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024133 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024134 return find_internal_func(name) >= 0;
24135 return find_func(name) != NULL;
24136}
24137
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024138/*
24139 * Return TRUE if a function "name" exists.
24140 */
24141 static int
24142function_exists(name)
24143 char_u *name;
24144{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024145 char_u *nm = name;
24146 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024147 int n = FALSE;
24148
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024149 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24150 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024151 nm = skipwhite(nm);
24152
24153 /* Only accept "funcname", "funcname ", "funcname (..." and
24154 * "funcname(...", not "funcname!...". */
24155 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024156 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024157 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024158 return n;
24159}
24160
Bram Moolenaara1544c02013-05-30 12:35:52 +020024161 char_u *
24162get_expanded_name(name, check)
24163 char_u *name;
24164 int check;
24165{
24166 char_u *nm = name;
24167 char_u *p;
24168
24169 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24170
24171 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024172 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024173 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024174
Bram Moolenaara1544c02013-05-30 12:35:52 +020024175 vim_free(p);
24176 return NULL;
24177}
24178
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024179/*
24180 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024181 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24182 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024183 */
24184 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024185builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024186 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024187 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024188{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024189 char_u *p;
24190
24191 if (!ASCII_ISLOWER(name[0]))
24192 return FALSE;
24193 p = vim_strchr(name, AUTOLOAD_CHAR);
24194 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024195}
24196
Bram Moolenaar05159a02005-02-26 23:04:13 +000024197#if defined(FEAT_PROFILE) || defined(PROTO)
24198/*
24199 * Start profiling function "fp".
24200 */
24201 static void
24202func_do_profile(fp)
24203 ufunc_T *fp;
24204{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024205 int len = fp->uf_lines.ga_len;
24206
24207 if (len == 0)
24208 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024209 fp->uf_tm_count = 0;
24210 profile_zero(&fp->uf_tm_self);
24211 profile_zero(&fp->uf_tm_total);
24212 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024213 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024214 if (fp->uf_tml_total == NULL)
24215 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024216 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024217 if (fp->uf_tml_self == NULL)
24218 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024219 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024220 fp->uf_tml_idx = -1;
24221 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24222 || fp->uf_tml_self == NULL)
24223 return; /* out of memory */
24224
24225 fp->uf_profiling = TRUE;
24226}
24227
24228/*
24229 * Dump the profiling results for all functions in file "fd".
24230 */
24231 void
24232func_dump_profile(fd)
24233 FILE *fd;
24234{
24235 hashitem_T *hi;
24236 int todo;
24237 ufunc_T *fp;
24238 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024239 ufunc_T **sorttab;
24240 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024241
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024242 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024243 if (todo == 0)
24244 return; /* nothing to dump */
24245
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024246 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024247
Bram Moolenaar05159a02005-02-26 23:04:13 +000024248 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24249 {
24250 if (!HASHITEM_EMPTY(hi))
24251 {
24252 --todo;
24253 fp = HI2UF(hi);
24254 if (fp->uf_profiling)
24255 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024256 if (sorttab != NULL)
24257 sorttab[st_len++] = fp;
24258
Bram Moolenaar05159a02005-02-26 23:04:13 +000024259 if (fp->uf_name[0] == K_SPECIAL)
24260 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24261 else
24262 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24263 if (fp->uf_tm_count == 1)
24264 fprintf(fd, "Called 1 time\n");
24265 else
24266 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24267 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24268 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24269 fprintf(fd, "\n");
24270 fprintf(fd, "count total (s) self (s)\n");
24271
24272 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24273 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024274 if (FUNCLINE(fp, i) == NULL)
24275 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024276 prof_func_line(fd, fp->uf_tml_count[i],
24277 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024278 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24279 }
24280 fprintf(fd, "\n");
24281 }
24282 }
24283 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024284
24285 if (sorttab != NULL && st_len > 0)
24286 {
24287 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24288 prof_total_cmp);
24289 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24290 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24291 prof_self_cmp);
24292 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24293 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024294
24295 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024296}
Bram Moolenaar73830342005-02-28 22:48:19 +000024297
24298 static void
24299prof_sort_list(fd, sorttab, st_len, title, prefer_self)
24300 FILE *fd;
24301 ufunc_T **sorttab;
24302 int st_len;
24303 char *title;
24304 int prefer_self; /* when equal print only self time */
24305{
24306 int i;
24307 ufunc_T *fp;
24308
24309 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24310 fprintf(fd, "count total (s) self (s) function\n");
24311 for (i = 0; i < 20 && i < st_len; ++i)
24312 {
24313 fp = sorttab[i];
24314 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24315 prefer_self);
24316 if (fp->uf_name[0] == K_SPECIAL)
24317 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24318 else
24319 fprintf(fd, " %s()\n", fp->uf_name);
24320 }
24321 fprintf(fd, "\n");
24322}
24323
24324/*
24325 * Print the count and times for one function or function line.
24326 */
24327 static void
24328prof_func_line(fd, count, total, self, prefer_self)
24329 FILE *fd;
24330 int count;
24331 proftime_T *total;
24332 proftime_T *self;
24333 int prefer_self; /* when equal print only self time */
24334{
24335 if (count > 0)
24336 {
24337 fprintf(fd, "%5d ", count);
24338 if (prefer_self && profile_equal(total, self))
24339 fprintf(fd, " ");
24340 else
24341 fprintf(fd, "%s ", profile_msg(total));
24342 if (!prefer_self && profile_equal(total, self))
24343 fprintf(fd, " ");
24344 else
24345 fprintf(fd, "%s ", profile_msg(self));
24346 }
24347 else
24348 fprintf(fd, " ");
24349}
24350
24351/*
24352 * Compare function for total time sorting.
24353 */
24354 static int
24355#ifdef __BORLANDC__
24356_RTLENTRYF
24357#endif
24358prof_total_cmp(s1, s2)
24359 const void *s1;
24360 const void *s2;
24361{
24362 ufunc_T *p1, *p2;
24363
24364 p1 = *(ufunc_T **)s1;
24365 p2 = *(ufunc_T **)s2;
24366 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24367}
24368
24369/*
24370 * Compare function for self time sorting.
24371 */
24372 static int
24373#ifdef __BORLANDC__
24374_RTLENTRYF
24375#endif
24376prof_self_cmp(s1, s2)
24377 const void *s1;
24378 const void *s2;
24379{
24380 ufunc_T *p1, *p2;
24381
24382 p1 = *(ufunc_T **)s1;
24383 p2 = *(ufunc_T **)s2;
24384 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24385}
24386
Bram Moolenaar05159a02005-02-26 23:04:13 +000024387#endif
24388
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024389/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024390 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024391 * Return TRUE if a package was loaded.
24392 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024393 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024394script_autoload(name, reload)
24395 char_u *name;
24396 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024397{
24398 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024399 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024400 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024401 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024402
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024403 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024404 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024405 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024406 return FALSE;
24407
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024408 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024409
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024410 /* Find the name in the list of previously loaded package names. Skip
24411 * "autoload/", it's always the same. */
24412 for (i = 0; i < ga_loaded.ga_len; ++i)
24413 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24414 break;
24415 if (!reload && i < ga_loaded.ga_len)
24416 ret = FALSE; /* was loaded already */
24417 else
24418 {
24419 /* Remember the name if it wasn't loaded already. */
24420 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24421 {
24422 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24423 tofree = NULL;
24424 }
24425
24426 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024427 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024428 ret = TRUE;
24429 }
24430
24431 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024432 return ret;
24433}
24434
24435/*
24436 * Return the autoload script name for a function or variable name.
24437 * Returns NULL when out of memory.
24438 */
24439 static char_u *
24440autoload_name(name)
24441 char_u *name;
24442{
24443 char_u *p;
24444 char_u *scriptname;
24445
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024446 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024447 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24448 if (scriptname == NULL)
24449 return FALSE;
24450 STRCPY(scriptname, "autoload/");
24451 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024452 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024453 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024454 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024455 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024456 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024457}
24458
Bram Moolenaar071d4272004-06-13 20:20:40 +000024459#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24460
24461/*
24462 * Function given to ExpandGeneric() to obtain the list of user defined
24463 * function names.
24464 */
24465 char_u *
24466get_user_func_name(xp, idx)
24467 expand_T *xp;
24468 int idx;
24469{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024470 static long_u done;
24471 static hashitem_T *hi;
24472 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473
24474 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024475 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024476 done = 0;
24477 hi = func_hashtab.ht_array;
24478 }
24479 if (done < func_hashtab.ht_used)
24480 {
24481 if (done++ > 0)
24482 ++hi;
24483 while (HASHITEM_EMPTY(hi))
24484 ++hi;
24485 fp = HI2UF(hi);
24486
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024487 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024488 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024489
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024490 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24491 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492
24493 cat_func_name(IObuff, fp);
24494 if (xp->xp_context != EXPAND_USER_FUNC)
24495 {
24496 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024497 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024498 STRCAT(IObuff, ")");
24499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024500 return IObuff;
24501 }
24502 return NULL;
24503}
24504
24505#endif /* FEAT_CMDL_COMPL */
24506
24507/*
24508 * Copy the function name of "fp" to buffer "buf".
24509 * "buf" must be able to hold the function name plus three bytes.
24510 * Takes care of script-local function names.
24511 */
24512 static void
24513cat_func_name(buf, fp)
24514 char_u *buf;
24515 ufunc_T *fp;
24516{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024517 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518 {
24519 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024520 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521 }
24522 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024523 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024524}
24525
24526/*
24527 * ":delfunction {name}"
24528 */
24529 void
24530ex_delfunction(eap)
24531 exarg_T *eap;
24532{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024533 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 char_u *p;
24535 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024536 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537
24538 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024539 name = trans_function_name(&p, eap->skip, 0, &fudi);
24540 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024542 {
24543 if (fudi.fd_dict != NULL && !eap->skip)
24544 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024547 if (!ends_excmd(*skipwhite(p)))
24548 {
24549 vim_free(name);
24550 EMSG(_(e_trailing));
24551 return;
24552 }
24553 eap->nextcmd = check_nextcmd(p);
24554 if (eap->nextcmd != NULL)
24555 *p = NUL;
24556
24557 if (!eap->skip)
24558 fp = find_func(name);
24559 vim_free(name);
24560
24561 if (!eap->skip)
24562 {
24563 if (fp == NULL)
24564 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024565 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024566 return;
24567 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024568 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569 {
24570 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24571 return;
24572 }
24573
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024574 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024576 /* Delete the dict item that refers to the function, it will
24577 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024578 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024579 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024580 else
24581 func_free(fp);
24582 }
24583}
24584
24585/*
24586 * Free a function and remove it from the list of functions.
24587 */
24588 static void
24589func_free(fp)
24590 ufunc_T *fp;
24591{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024592 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024593
24594 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024595 ga_clear_strings(&(fp->uf_args));
24596 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024597#ifdef FEAT_PROFILE
24598 vim_free(fp->uf_tml_count);
24599 vim_free(fp->uf_tml_total);
24600 vim_free(fp->uf_tml_self);
24601#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024602
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024603 /* remove the function from the function hashtable */
24604 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24605 if (HASHITEM_EMPTY(hi))
24606 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024607 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024608 hash_remove(&func_hashtab, hi);
24609
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024610 vim_free(fp);
24611}
24612
24613/*
24614 * Unreference a Function: decrement the reference count and free it when it
24615 * becomes zero. Only for numbered functions.
24616 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024617 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024618func_unref(name)
24619 char_u *name;
24620{
24621 ufunc_T *fp;
24622
24623 if (name != NULL && isdigit(*name))
24624 {
24625 fp = find_func(name);
24626 if (fp == NULL)
24627 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024628 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024629 {
24630 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024631 * when "uf_calls" becomes zero. */
24632 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024633 func_free(fp);
24634 }
24635 }
24636}
24637
24638/*
24639 * Count a reference to a Function.
24640 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024641 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024642func_ref(name)
24643 char_u *name;
24644{
24645 ufunc_T *fp;
24646
24647 if (name != NULL && isdigit(*name))
24648 {
24649 fp = find_func(name);
24650 if (fp == NULL)
24651 EMSG2(_(e_intern2), "func_ref()");
24652 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024653 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024654 }
24655}
24656
24657/*
24658 * Call a user function.
24659 */
24660 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000024661call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662 ufunc_T *fp; /* pointer to function */
24663 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000024664 typval_T *argvars; /* arguments */
24665 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024666 linenr_T firstline; /* first line of range */
24667 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000024668 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669{
Bram Moolenaar33570922005-01-25 22:26:29 +000024670 char_u *save_sourcing_name;
24671 linenr_T save_sourcing_lnum;
24672 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024673 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024674 int save_did_emsg;
24675 static int depth = 0;
24676 dictitem_T *v;
24677 int fixvar_idx = 0; /* index in fixvar[] */
24678 int i;
24679 int ai;
24680 char_u numbuf[NUMBUFLEN];
24681 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024682 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024683#ifdef FEAT_PROFILE
24684 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024685 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687
24688 /* If depth of calling is getting too high, don't execute the function */
24689 if (depth >= p_mfd)
24690 {
24691 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024692 rettv->v_type = VAR_NUMBER;
24693 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694 return;
24695 }
24696 ++depth;
24697
24698 line_breakcheck(); /* check for CTRL-C hit */
24699
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024700 fc = (funccall_T *)alloc(sizeof(funccall_T));
24701 fc->caller = current_funccal;
24702 current_funccal = fc;
24703 fc->func = fp;
24704 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024705 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024706 fc->linenr = 0;
24707 fc->returned = FALSE;
24708 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024710 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24711 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024712
Bram Moolenaar33570922005-01-25 22:26:29 +000024713 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024714 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024715 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24716 * each argument variable and saves a lot of time.
24717 */
24718 /*
24719 * Init l: variables.
24720 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024721 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024722 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024723 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024724 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24725 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024726 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024727 name = v->di_key;
24728 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024729 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024730 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024731 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024732 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024733 v->di_tv.vval.v_dict = selfdict;
24734 ++selfdict->dv_refcount;
24735 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024736
Bram Moolenaar33570922005-01-25 22:26:29 +000024737 /*
24738 * Init a: variables.
24739 * Set a:0 to "argcount".
24740 * Set a:000 to a list with room for the "..." arguments.
24741 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024742 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024743 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024744 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024745 /* Use "name" to avoid a warning from some compiler that checks the
24746 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024747 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024748 name = v->di_key;
24749 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024750 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024751 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024752 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024753 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024754 v->di_tv.vval.v_list = &fc->l_varlist;
24755 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24756 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24757 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024758
24759 /*
24760 * Set a:firstline to "firstline" and a:lastline to "lastline".
24761 * Set a:name to named arguments.
24762 * Set a:N to the "..." arguments.
24763 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024764 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024765 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024766 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024767 (varnumber_T)lastline);
24768 for (i = 0; i < argcount; ++i)
24769 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024770 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024771 if (ai < 0)
24772 /* named argument a:name */
24773 name = FUNCARG(fp, i);
24774 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024775 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024776 /* "..." argument a:1, a:2, etc. */
24777 sprintf((char *)numbuf, "%d", ai + 1);
24778 name = numbuf;
24779 }
24780 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24781 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024782 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024783 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24784 }
24785 else
24786 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024787 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24788 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024789 if (v == NULL)
24790 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024791 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024792 }
24793 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024794 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024795
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024796 /* Note: the values are copied directly to avoid alloc/free.
24797 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024798 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024799 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024800
24801 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24802 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024803 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24804 fc->l_listitems[ai].li_tv = argvars[i];
24805 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024806 }
24807 }
24808
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809 /* Don't redraw while executing the function. */
24810 ++RedrawingDisabled;
24811 save_sourcing_name = sourcing_name;
24812 save_sourcing_lnum = sourcing_lnum;
24813 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024814 /* need space for function name + ("function " + 3) or "[number]" */
24815 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24816 + STRLEN(fp->uf_name) + 20;
24817 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024818 if (sourcing_name != NULL)
24819 {
24820 if (save_sourcing_name != NULL
24821 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024822 sprintf((char *)sourcing_name, "%s[%d]..",
24823 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824 else
24825 STRCPY(sourcing_name, "function ");
24826 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24827
24828 if (p_verbose >= 12)
24829 {
24830 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024831 verbose_enter_scroll();
24832
Bram Moolenaar555b2802005-05-19 21:08:39 +000024833 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834 if (p_verbose >= 14)
24835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024836 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024837 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024838 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024839 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840
24841 msg_puts((char_u *)"(");
24842 for (i = 0; i < argcount; ++i)
24843 {
24844 if (i > 0)
24845 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024846 if (argvars[i].v_type == VAR_NUMBER)
24847 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848 else
24849 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024850 /* Do not want errors such as E724 here. */
24851 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024852 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024853 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024854 if (s != NULL)
24855 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024856 if (vim_strsize(s) > MSG_BUF_CLEN)
24857 {
24858 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24859 s = buf;
24860 }
24861 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024862 vim_free(tofree);
24863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 }
24865 }
24866 msg_puts((char_u *)")");
24867 }
24868 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024869
24870 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024871 --no_wait_return;
24872 }
24873 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024874#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024875 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024876 {
24877 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24878 func_do_profile(fp);
24879 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024880 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024881 {
24882 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024883 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024884 profile_zero(&fp->uf_tm_children);
24885 }
24886 script_prof_save(&wait_start);
24887 }
24888#endif
24889
Bram Moolenaar071d4272004-06-13 20:20:40 +000024890 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024891 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892 save_did_emsg = did_emsg;
24893 did_emsg = FALSE;
24894
24895 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024896 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24898
24899 --RedrawingDisabled;
24900
24901 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024902 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024904 clear_tv(rettv);
24905 rettv->v_type = VAR_NUMBER;
24906 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024907 }
24908
Bram Moolenaar05159a02005-02-26 23:04:13 +000024909#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024910 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024911 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024912 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024913 profile_end(&call_start);
24914 profile_sub_wait(&wait_start, &call_start);
24915 profile_add(&fp->uf_tm_total, &call_start);
24916 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024917 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024918 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024919 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24920 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024921 }
24922 }
24923#endif
24924
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 /* when being verbose, mention the return value */
24926 if (p_verbose >= 12)
24927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024929 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024930
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024932 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024933 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024934 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024935 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024938 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024939 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024940 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024941 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024942
Bram Moolenaar555b2802005-05-19 21:08:39 +000024943 /* The value may be very long. Skip the middle part, so that we
24944 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024945 * truncate it at the end. Don't want errors such as E724 here. */
24946 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024947 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024948 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024949 if (s != NULL)
24950 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024951 if (vim_strsize(s) > MSG_BUF_CLEN)
24952 {
24953 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24954 s = buf;
24955 }
24956 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024957 vim_free(tofree);
24958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959 }
24960 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024961
24962 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963 --no_wait_return;
24964 }
24965
24966 vim_free(sourcing_name);
24967 sourcing_name = save_sourcing_name;
24968 sourcing_lnum = save_sourcing_lnum;
24969 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024970#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024971 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024972 script_prof_restore(&wait_start);
24973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024974
24975 if (p_verbose >= 12 && sourcing_name != NULL)
24976 {
24977 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024978 verbose_enter_scroll();
24979
Bram Moolenaar555b2802005-05-19 21:08:39 +000024980 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024982
24983 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024984 --no_wait_return;
24985 }
24986
24987 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024988 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024989 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024990
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024991 /* If the a:000 list and the l: and a: dicts are not referenced we can
24992 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024993 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24994 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24995 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24996 {
24997 free_funccal(fc, FALSE);
24998 }
24999 else
25000 {
25001 hashitem_T *hi;
25002 listitem_T *li;
25003 int todo;
25004
25005 /* "fc" is still in use. This can happen when returning "a:000" or
25006 * assigning "l:" to a global variable.
25007 * Link "fc" in the list for garbage collection later. */
25008 fc->caller = previous_funccal;
25009 previous_funccal = fc;
25010
25011 /* Make a copy of the a: variables, since we didn't do that above. */
25012 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25013 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25014 {
25015 if (!HASHITEM_EMPTY(hi))
25016 {
25017 --todo;
25018 v = HI2DI(hi);
25019 copy_tv(&v->di_tv, &v->di_tv);
25020 }
25021 }
25022
25023 /* Make a copy of the a:000 items, since we didn't do that above. */
25024 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25025 copy_tv(&li->li_tv, &li->li_tv);
25026 }
25027}
25028
25029/*
25030 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025031 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025032 */
25033 static int
25034can_free_funccal(fc, copyID)
25035 funccall_T *fc;
25036 int copyID;
25037{
25038 return (fc->l_varlist.lv_copyID != copyID
25039 && fc->l_vars.dv_copyID != copyID
25040 && fc->l_avars.dv_copyID != copyID);
25041}
25042
25043/*
25044 * Free "fc" and what it contains.
25045 */
25046 static void
25047free_funccal(fc, free_val)
25048 funccall_T *fc;
25049 int free_val; /* a: vars were allocated */
25050{
25051 listitem_T *li;
25052
25053 /* The a: variables typevals may not have been allocated, only free the
25054 * allocated variables. */
25055 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25056
25057 /* free all l: variables */
25058 vars_clear(&fc->l_vars.dv_hashtab);
25059
25060 /* Free the a:000 variables if they were allocated. */
25061 if (free_val)
25062 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25063 clear_tv(&li->li_tv);
25064
25065 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025066}
25067
25068/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025069 * Add a number variable "name" to dict "dp" with value "nr".
25070 */
25071 static void
25072add_nr_var(dp, v, name, nr)
25073 dict_T *dp;
25074 dictitem_T *v;
25075 char *name;
25076 varnumber_T nr;
25077{
25078 STRCPY(v->di_key, name);
25079 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25080 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25081 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025082 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025083 v->di_tv.vval.v_number = nr;
25084}
25085
25086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025087 * ":return [expr]"
25088 */
25089 void
25090ex_return(eap)
25091 exarg_T *eap;
25092{
25093 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025094 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095 int returning = FALSE;
25096
25097 if (current_funccal == NULL)
25098 {
25099 EMSG(_("E133: :return not inside a function"));
25100 return;
25101 }
25102
25103 if (eap->skip)
25104 ++emsg_skip;
25105
25106 eap->nextcmd = NULL;
25107 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025108 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109 {
25110 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025111 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025113 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025114 }
25115 /* It's safer to return also on error. */
25116 else if (!eap->skip)
25117 {
25118 /*
25119 * Return unless the expression evaluation has been cancelled due to an
25120 * aborting error, an interrupt, or an exception.
25121 */
25122 if (!aborting())
25123 returning = do_return(eap, FALSE, TRUE, NULL);
25124 }
25125
25126 /* When skipping or the return gets pending, advance to the next command
25127 * in this line (!returning). Otherwise, ignore the rest of the line.
25128 * Following lines will be ignored by get_func_line(). */
25129 if (returning)
25130 eap->nextcmd = NULL;
25131 else if (eap->nextcmd == NULL) /* no argument */
25132 eap->nextcmd = check_nextcmd(arg);
25133
25134 if (eap->skip)
25135 --emsg_skip;
25136}
25137
25138/*
25139 * Return from a function. Possibly makes the return pending. Also called
25140 * for a pending return at the ":endtry" or after returning from an extra
25141 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025142 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025143 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144 * FALSE when the return gets pending.
25145 */
25146 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025147do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025148 exarg_T *eap;
25149 int reanimate;
25150 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025151 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025152{
25153 int idx;
25154 struct condstack *cstack = eap->cstack;
25155
25156 if (reanimate)
25157 /* Undo the return. */
25158 current_funccal->returned = FALSE;
25159
25160 /*
25161 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25162 * not in its finally clause (which then is to be executed next) is found.
25163 * In this case, make the ":return" pending for execution at the ":endtry".
25164 * Otherwise, return normally.
25165 */
25166 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25167 if (idx >= 0)
25168 {
25169 cstack->cs_pending[idx] = CSTP_RETURN;
25170
25171 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025172 /* A pending return again gets pending. "rettv" points to an
25173 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025174 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025175 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025176 else
25177 {
25178 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025179 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025181 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025183 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025184 {
25185 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025186 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025187 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025188 else
25189 EMSG(_(e_outofmem));
25190 }
25191 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025192 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193
25194 if (reanimate)
25195 {
25196 /* The pending return value could be overwritten by a ":return"
25197 * without argument in a finally clause; reset the default
25198 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025199 current_funccal->rettv->v_type = VAR_NUMBER;
25200 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201 }
25202 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025203 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 }
25205 else
25206 {
25207 current_funccal->returned = TRUE;
25208
25209 /* If the return is carried out now, store the return value. For
25210 * a return immediately after reanimation, the value is already
25211 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025212 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025214 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025215 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025216 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025217 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025218 }
25219 }
25220
25221 return idx < 0;
25222}
25223
25224/*
25225 * Free the variable with a pending return value.
25226 */
25227 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025228discard_pending_return(rettv)
25229 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230{
Bram Moolenaar33570922005-01-25 22:26:29 +000025231 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025232}
25233
25234/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025235 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 * is an allocated string. Used by report_pending() for verbose messages.
25237 */
25238 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025239get_return_cmd(rettv)
25240 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025241{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025242 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025243 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025244 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025246 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025247 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025248 if (s == NULL)
25249 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025250
25251 STRCPY(IObuff, ":return ");
25252 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25253 if (STRLEN(s) + 8 >= IOSIZE)
25254 STRCPY(IObuff + IOSIZE - 4, "...");
25255 vim_free(tofree);
25256 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257}
25258
25259/*
25260 * Get next function line.
25261 * Called by do_cmdline() to get the next line.
25262 * Returns allocated string, or NULL for end of function.
25263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 char_u *
25265get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025266 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025268 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269{
Bram Moolenaar33570922005-01-25 22:26:29 +000025270 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025271 ufunc_T *fp = fcp->func;
25272 char_u *retval;
25273 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274
25275 /* If breakpoints have been added/deleted need to check for it. */
25276 if (fcp->dbg_tick != debug_tick)
25277 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025278 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025279 sourcing_lnum);
25280 fcp->dbg_tick = debug_tick;
25281 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025282#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025283 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025284 func_line_end(cookie);
25285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286
Bram Moolenaar05159a02005-02-26 23:04:13 +000025287 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025288 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25289 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290 retval = NULL;
25291 else
25292 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025293 /* Skip NULL lines (continuation lines). */
25294 while (fcp->linenr < gap->ga_len
25295 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25296 ++fcp->linenr;
25297 if (fcp->linenr >= gap->ga_len)
25298 retval = NULL;
25299 else
25300 {
25301 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25302 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025303#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025304 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025305 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025306#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 }
25309
25310 /* Did we encounter a breakpoint? */
25311 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25312 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025313 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025315 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316 sourcing_lnum);
25317 fcp->dbg_tick = debug_tick;
25318 }
25319
25320 return retval;
25321}
25322
Bram Moolenaar05159a02005-02-26 23:04:13 +000025323#if defined(FEAT_PROFILE) || defined(PROTO)
25324/*
25325 * Called when starting to read a function line.
25326 * "sourcing_lnum" must be correct!
25327 * When skipping lines it may not actually be executed, but we won't find out
25328 * until later and we need to store the time now.
25329 */
25330 void
25331func_line_start(cookie)
25332 void *cookie;
25333{
25334 funccall_T *fcp = (funccall_T *)cookie;
25335 ufunc_T *fp = fcp->func;
25336
25337 if (fp->uf_profiling && sourcing_lnum >= 1
25338 && sourcing_lnum <= fp->uf_lines.ga_len)
25339 {
25340 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025341 /* Skip continuation lines. */
25342 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25343 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025344 fp->uf_tml_execed = FALSE;
25345 profile_start(&fp->uf_tml_start);
25346 profile_zero(&fp->uf_tml_children);
25347 profile_get_wait(&fp->uf_tml_wait);
25348 }
25349}
25350
25351/*
25352 * Called when actually executing a function line.
25353 */
25354 void
25355func_line_exec(cookie)
25356 void *cookie;
25357{
25358 funccall_T *fcp = (funccall_T *)cookie;
25359 ufunc_T *fp = fcp->func;
25360
25361 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25362 fp->uf_tml_execed = TRUE;
25363}
25364
25365/*
25366 * Called when done with a function line.
25367 */
25368 void
25369func_line_end(cookie)
25370 void *cookie;
25371{
25372 funccall_T *fcp = (funccall_T *)cookie;
25373 ufunc_T *fp = fcp->func;
25374
25375 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25376 {
25377 if (fp->uf_tml_execed)
25378 {
25379 ++fp->uf_tml_count[fp->uf_tml_idx];
25380 profile_end(&fp->uf_tml_start);
25381 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025382 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025383 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25384 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025385 }
25386 fp->uf_tml_idx = -1;
25387 }
25388}
25389#endif
25390
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391/*
25392 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025393 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394 */
25395 int
25396func_has_ended(cookie)
25397 void *cookie;
25398{
Bram Moolenaar33570922005-01-25 22:26:29 +000025399 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025400
25401 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25402 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025403 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 || fcp->returned);
25405}
25406
25407/*
25408 * return TRUE if cookie indicates a function which "abort"s on errors.
25409 */
25410 int
25411func_has_abort(cookie)
25412 void *cookie;
25413{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025414 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025415}
25416
25417#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25418typedef enum
25419{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025420 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25421 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25422 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025423} var_flavour_T;
25424
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025425static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426
25427 static var_flavour_T
25428var_flavour(varname)
25429 char_u *varname;
25430{
25431 char_u *p = varname;
25432
25433 if (ASCII_ISUPPER(*p))
25434 {
25435 while (*(++p))
25436 if (ASCII_ISLOWER(*p))
25437 return VAR_FLAVOUR_SESSION;
25438 return VAR_FLAVOUR_VIMINFO;
25439 }
25440 else
25441 return VAR_FLAVOUR_DEFAULT;
25442}
25443#endif
25444
25445#if defined(FEAT_VIMINFO) || defined(PROTO)
25446/*
25447 * Restore global vars that start with a capital from the viminfo file
25448 */
25449 int
25450read_viminfo_varlist(virp, writing)
25451 vir_T *virp;
25452 int writing;
25453{
25454 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025455 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025456 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025457 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458
25459 if (!writing && (find_viminfo_parameter('!') != NULL))
25460 {
25461 tab = vim_strchr(virp->vir_line + 1, '\t');
25462 if (tab != NULL)
25463 {
25464 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025465 switch (*tab)
25466 {
25467 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025468#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025469 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025470#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025471 case 'D': type = VAR_DICT; break;
25472 case 'L': type = VAR_LIST; break;
25473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474
25475 tab = vim_strchr(tab, '\t');
25476 if (tab != NULL)
25477 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025478 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025479 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025480 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025482#ifdef FEAT_FLOAT
25483 else if (type == VAR_FLOAT)
25484 (void)string2float(tab + 1, &tv.vval.v_float);
25485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025487 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025488 if (type == VAR_DICT || type == VAR_LIST)
25489 {
25490 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25491
25492 if (etv == NULL)
25493 /* Failed to parse back the dict or list, use it as a
25494 * string. */
25495 tv.v_type = VAR_STRING;
25496 else
25497 {
25498 vim_free(tv.vval.v_string);
25499 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025500 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025501 }
25502 }
25503
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025504 /* when in a function use global variables */
25505 save_funccal = current_funccal;
25506 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025507 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025508 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025509
25510 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025511 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025512 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25513 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025514 }
25515 }
25516 }
25517
25518 return viminfo_readline(virp);
25519}
25520
25521/*
25522 * Write global vars that start with a capital to the viminfo file
25523 */
25524 void
25525write_viminfo_varlist(fp)
25526 FILE *fp;
25527{
Bram Moolenaar33570922005-01-25 22:26:29 +000025528 hashitem_T *hi;
25529 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025530 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025531 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025532 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025533 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025534 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535
25536 if (find_viminfo_parameter('!') == NULL)
25537 return;
25538
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025539 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025540
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025541 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025542 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025544 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025546 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025547 this_var = HI2DI(hi);
25548 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025549 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025550 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025551 {
25552 case VAR_STRING: s = "STR"; break;
25553 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025554#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025555 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025556#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025557 case VAR_DICT: s = "DIC"; break;
25558 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000025559 default: continue;
25560 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025561 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025562 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025563 if (p != NULL)
25564 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025565 vim_free(tofree);
25566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567 }
25568 }
25569}
25570#endif
25571
25572#if defined(FEAT_SESSION) || defined(PROTO)
25573 int
25574store_session_globals(fd)
25575 FILE *fd;
25576{
Bram Moolenaar33570922005-01-25 22:26:29 +000025577 hashitem_T *hi;
25578 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025579 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025580 char_u *p, *t;
25581
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025582 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025583 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025585 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025587 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025588 this_var = HI2DI(hi);
25589 if ((this_var->di_tv.v_type == VAR_NUMBER
25590 || this_var->di_tv.v_type == VAR_STRING)
25591 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025592 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025593 /* Escape special characters with a backslash. Turn a LF and
25594 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025595 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025596 (char_u *)"\\\"\n\r");
25597 if (p == NULL) /* out of memory */
25598 break;
25599 for (t = p; *t != NUL; ++t)
25600 if (*t == '\n')
25601 *t = 'n';
25602 else if (*t == '\r')
25603 *t = 'r';
25604 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025605 this_var->di_key,
25606 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25607 : ' ',
25608 p,
25609 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25610 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025611 || put_eol(fd) == FAIL)
25612 {
25613 vim_free(p);
25614 return FAIL;
25615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616 vim_free(p);
25617 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025618#ifdef FEAT_FLOAT
25619 else if (this_var->di_tv.v_type == VAR_FLOAT
25620 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25621 {
25622 float_T f = this_var->di_tv.vval.v_float;
25623 int sign = ' ';
25624
25625 if (f < 0)
25626 {
25627 f = -f;
25628 sign = '-';
25629 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025630 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025631 this_var->di_key, sign, f) < 0)
25632 || put_eol(fd) == FAIL)
25633 return FAIL;
25634 }
25635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 }
25637 }
25638 return OK;
25639}
25640#endif
25641
Bram Moolenaar661b1822005-07-28 22:36:45 +000025642/*
25643 * Display script name where an item was last set.
25644 * Should only be invoked when 'verbose' is non-zero.
25645 */
25646 void
25647last_set_msg(scriptID)
25648 scid_T scriptID;
25649{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025650 char_u *p;
25651
Bram Moolenaar661b1822005-07-28 22:36:45 +000025652 if (scriptID != 0)
25653 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025654 p = home_replace_save(NULL, get_scriptname(scriptID));
25655 if (p != NULL)
25656 {
25657 verbose_enter();
25658 MSG_PUTS(_("\n\tLast set from "));
25659 MSG_PUTS(p);
25660 vim_free(p);
25661 verbose_leave();
25662 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025663 }
25664}
25665
Bram Moolenaard812df62008-11-09 12:46:09 +000025666/*
25667 * List v:oldfiles in a nice way.
25668 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025669 void
25670ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000025671 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000025672{
25673 list_T *l = vimvars[VV_OLDFILES].vv_list;
25674 listitem_T *li;
25675 int nr = 0;
25676
25677 if (l == NULL)
25678 msg((char_u *)_("No old files"));
25679 else
25680 {
25681 msg_start();
25682 msg_scroll = TRUE;
25683 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25684 {
25685 msg_outnum((long)++nr);
25686 MSG_PUTS(": ");
25687 msg_outtrans(get_tv_string(&li->li_tv));
25688 msg_putchar('\n');
25689 out_flush(); /* output one line at a time */
25690 ui_breakcheck();
25691 }
25692 /* Assume "got_int" was set to truncate the listing. */
25693 got_int = FALSE;
25694
25695#ifdef FEAT_BROWSE_CMD
25696 if (cmdmod.browse)
25697 {
25698 quit_more = FALSE;
25699 nr = prompt_for_number(FALSE);
25700 msg_starthere();
25701 if (nr > 0)
25702 {
25703 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25704 (long)nr);
25705
25706 if (p != NULL)
25707 {
25708 p = expand_env_save(p);
25709 eap->arg = p;
25710 eap->cmdidx = CMD_edit;
25711 cmdmod.browse = FALSE;
25712 do_exedit(eap, NULL);
25713 vim_free(p);
25714 }
25715 }
25716 }
25717#endif
25718 }
25719}
25720
Bram Moolenaar53744302015-07-17 17:38:22 +020025721/* reset v:option_new, v:option_old and v:option_type */
25722 void
25723reset_v_option_vars()
25724{
25725 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25726 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25727 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25728}
25729
25730
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731#endif /* FEAT_EVAL */
25732
Bram Moolenaar071d4272004-06-13 20:20:40 +000025733
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025734#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025735
25736#ifdef WIN3264
25737/*
25738 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25739 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025740static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25741static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25742static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025743
25744/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025745 * Get the short path (8.3) for the filename in "fnamep".
25746 * Only works for a valid file name.
25747 * When the path gets longer "fnamep" is changed and the allocated buffer
25748 * is put in "bufp".
25749 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25750 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751 */
25752 static int
25753get_short_pathname(fnamep, bufp, fnamelen)
25754 char_u **fnamep;
25755 char_u **bufp;
25756 int *fnamelen;
25757{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025758 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025759 char_u *newbuf;
25760
25761 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762 l = GetShortPathName(*fnamep, *fnamep, len);
25763 if (l > len - 1)
25764 {
25765 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025766 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025767 newbuf = vim_strnsave(*fnamep, l);
25768 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025769 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025770
25771 vim_free(*bufp);
25772 *fnamep = *bufp = newbuf;
25773
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025774 /* Really should always succeed, as the buffer is big enough. */
25775 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776 }
25777
25778 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025779 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780}
25781
25782/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025783 * Get the short path (8.3) for the filename in "fname". The converted
25784 * path is returned in "bufp".
25785 *
25786 * Some of the directories specified in "fname" may not exist. This function
25787 * will shorten the existing directories at the beginning of the path and then
25788 * append the remaining non-existing path.
25789 *
25790 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025791 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025792 * bufp - Pointer to an allocated buffer for the filename.
25793 * fnamelen - Length of the filename pointed to by fname
25794 *
25795 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796 */
25797 static int
25798shortpath_for_invalid_fname(fname, bufp, fnamelen)
25799 char_u **fname;
25800 char_u **bufp;
25801 int *fnamelen;
25802{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025803 char_u *short_fname, *save_fname, *pbuf_unused;
25804 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025805 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025806 int old_len, len;
25807 int new_len, sfx_len;
25808 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809
25810 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025811 old_len = *fnamelen;
25812 save_fname = vim_strnsave(*fname, old_len);
25813 pbuf_unused = NULL;
25814 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025815
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025816 endp = save_fname + old_len - 1; /* Find the end of the copy */
25817 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025819 /*
25820 * Try shortening the supplied path till it succeeds by removing one
25821 * directory at a time from the tail of the path.
25822 */
25823 len = 0;
25824 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025826 /* go back one path-separator */
25827 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25828 --endp;
25829 if (endp <= save_fname)
25830 break; /* processed the complete path */
25831
25832 /*
25833 * Replace the path separator with a NUL and try to shorten the
25834 * resulting path.
25835 */
25836 ch = *endp;
25837 *endp = 0;
25838 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025839 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025840 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25841 {
25842 retval = FAIL;
25843 goto theend;
25844 }
25845 *endp = ch; /* preserve the string */
25846
25847 if (len > 0)
25848 break; /* successfully shortened the path */
25849
25850 /* failed to shorten the path. Skip the path separator */
25851 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852 }
25853
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025854 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025855 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025856 /*
25857 * Succeeded in shortening the path. Now concatenate the shortened
25858 * path with the remaining path at the tail.
25859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025861 /* Compute the length of the new path. */
25862 sfx_len = (int)(save_endp - endp) + 1;
25863 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025865 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025867 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025869 /* There is not enough space in the currently allocated string,
25870 * copy it to a buffer big enough. */
25871 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025873 {
25874 retval = FAIL;
25875 goto theend;
25876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025877 }
25878 else
25879 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025880 /* Transfer short_fname to the main buffer (it's big enough),
25881 * unless get_short_pathname() did its work in-place. */
25882 *fname = *bufp = save_fname;
25883 if (short_fname != save_fname)
25884 vim_strncpy(save_fname, short_fname, len);
25885 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025886 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025887
25888 /* concat the not-shortened part of the path */
25889 vim_strncpy(*fname + len, endp, sfx_len);
25890 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025892
25893theend:
25894 vim_free(pbuf_unused);
25895 vim_free(save_fname);
25896
25897 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025898}
25899
25900/*
25901 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025902 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903 */
25904 static int
25905shortpath_for_partial(fnamep, bufp, fnamelen)
25906 char_u **fnamep;
25907 char_u **bufp;
25908 int *fnamelen;
25909{
25910 int sepcount, len, tflen;
25911 char_u *p;
25912 char_u *pbuf, *tfname;
25913 int hasTilde;
25914
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025915 /* Count up the path separators from the RHS.. so we know which part
25916 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025917 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025918 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919 if (vim_ispathsep(*p))
25920 ++sepcount;
25921
25922 /* Need full path first (use expand_env() to remove a "~/") */
25923 hasTilde = (**fnamep == '~');
25924 if (hasTilde)
25925 pbuf = tfname = expand_env_save(*fnamep);
25926 else
25927 pbuf = tfname = FullName_save(*fnamep, FALSE);
25928
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025929 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025930
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025931 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25932 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025933
25934 if (len == 0)
25935 {
25936 /* Don't have a valid filename, so shorten the rest of the
25937 * path if we can. This CAN give us invalid 8.3 filenames, but
25938 * there's not a lot of point in guessing what it might be.
25939 */
25940 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025941 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25942 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025943 }
25944
25945 /* Count the paths backward to find the beginning of the desired string. */
25946 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025947 {
25948#ifdef FEAT_MBYTE
25949 if (has_mbyte)
25950 p -= mb_head_off(tfname, p);
25951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952 if (vim_ispathsep(*p))
25953 {
25954 if (sepcount == 0 || (hasTilde && sepcount == 1))
25955 break;
25956 else
25957 sepcount --;
25958 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960 if (hasTilde)
25961 {
25962 --p;
25963 if (p >= tfname)
25964 *p = '~';
25965 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025966 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025967 }
25968 else
25969 ++p;
25970
25971 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25972 vim_free(*bufp);
25973 *fnamelen = (int)STRLEN(p);
25974 *bufp = pbuf;
25975 *fnamep = p;
25976
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025977 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025978}
25979#endif /* WIN3264 */
25980
25981/*
25982 * Adjust a filename, according to a string of modifiers.
25983 * *fnamep must be NUL terminated when called. When returning, the length is
25984 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025985 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025986 * When there is an error, *fnamep is set to NULL.
25987 */
25988 int
25989modify_fname(src, usedlen, fnamep, bufp, fnamelen)
25990 char_u *src; /* string with modifiers */
25991 int *usedlen; /* characters after src that are used */
25992 char_u **fnamep; /* file name so far */
25993 char_u **bufp; /* buffer for allocated file name or NULL */
25994 int *fnamelen; /* length of fnamep */
25995{
25996 int valid = 0;
25997 char_u *tail;
25998 char_u *s, *p, *pbuf;
25999 char_u dirname[MAXPATHL];
26000 int c;
26001 int has_fullname = 0;
26002#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026003 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026004 int has_shortname = 0;
26005#endif
26006
26007repeat:
26008 /* ":p" - full path/file_name */
26009 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26010 {
26011 has_fullname = 1;
26012
26013 valid |= VALID_PATH;
26014 *usedlen += 2;
26015
26016 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26017 if ((*fnamep)[0] == '~'
26018#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26019 && ((*fnamep)[1] == '/'
26020# ifdef BACKSLASH_IN_FILENAME
26021 || (*fnamep)[1] == '\\'
26022# endif
26023 || (*fnamep)[1] == NUL)
26024
26025#endif
26026 )
26027 {
26028 *fnamep = expand_env_save(*fnamep);
26029 vim_free(*bufp); /* free any allocated file name */
26030 *bufp = *fnamep;
26031 if (*fnamep == NULL)
26032 return -1;
26033 }
26034
26035 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026036 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026037 {
26038 if (vim_ispathsep(*p)
26039 && p[1] == '.'
26040 && (p[2] == NUL
26041 || vim_ispathsep(p[2])
26042 || (p[2] == '.'
26043 && (p[3] == NUL || vim_ispathsep(p[3])))))
26044 break;
26045 }
26046
26047 /* FullName_save() is slow, don't use it when not needed. */
26048 if (*p != NUL || !vim_isAbsName(*fnamep))
26049 {
26050 *fnamep = FullName_save(*fnamep, *p != NUL);
26051 vim_free(*bufp); /* free any allocated file name */
26052 *bufp = *fnamep;
26053 if (*fnamep == NULL)
26054 return -1;
26055 }
26056
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026057#ifdef WIN3264
26058# if _WIN32_WINNT >= 0x0500
26059 if (vim_strchr(*fnamep, '~') != NULL)
26060 {
26061 /* Expand 8.3 filename to full path. Needed to make sure the same
26062 * file does not have two different names.
26063 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26064 p = alloc(_MAX_PATH + 1);
26065 if (p != NULL)
26066 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010026067 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026068 {
26069 vim_free(*bufp);
26070 *bufp = *fnamep = p;
26071 }
26072 else
26073 vim_free(p);
26074 }
26075 }
26076# endif
26077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026078 /* Append a path separator to a directory. */
26079 if (mch_isdir(*fnamep))
26080 {
26081 /* Make room for one or two extra characters. */
26082 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26083 vim_free(*bufp); /* free any allocated file name */
26084 *bufp = *fnamep;
26085 if (*fnamep == NULL)
26086 return -1;
26087 add_pathsep(*fnamep);
26088 }
26089 }
26090
26091 /* ":." - path relative to the current directory */
26092 /* ":~" - path relative to the home directory */
26093 /* ":8" - shortname path - postponed till after */
26094 while (src[*usedlen] == ':'
26095 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26096 {
26097 *usedlen += 2;
26098 if (c == '8')
26099 {
26100#ifdef WIN3264
26101 has_shortname = 1; /* Postpone this. */
26102#endif
26103 continue;
26104 }
26105 pbuf = NULL;
26106 /* Need full path first (use expand_env() to remove a "~/") */
26107 if (!has_fullname)
26108 {
26109 if (c == '.' && **fnamep == '~')
26110 p = pbuf = expand_env_save(*fnamep);
26111 else
26112 p = pbuf = FullName_save(*fnamep, FALSE);
26113 }
26114 else
26115 p = *fnamep;
26116
26117 has_fullname = 0;
26118
26119 if (p != NULL)
26120 {
26121 if (c == '.')
26122 {
26123 mch_dirname(dirname, MAXPATHL);
26124 s = shorten_fname(p, dirname);
26125 if (s != NULL)
26126 {
26127 *fnamep = s;
26128 if (pbuf != NULL)
26129 {
26130 vim_free(*bufp); /* free any allocated file name */
26131 *bufp = pbuf;
26132 pbuf = NULL;
26133 }
26134 }
26135 }
26136 else
26137 {
26138 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26139 /* Only replace it when it starts with '~' */
26140 if (*dirname == '~')
26141 {
26142 s = vim_strsave(dirname);
26143 if (s != NULL)
26144 {
26145 *fnamep = s;
26146 vim_free(*bufp);
26147 *bufp = s;
26148 }
26149 }
26150 }
26151 vim_free(pbuf);
26152 }
26153 }
26154
26155 tail = gettail(*fnamep);
26156 *fnamelen = (int)STRLEN(*fnamep);
26157
26158 /* ":h" - head, remove "/file_name", can be repeated */
26159 /* Don't remove the first "/" or "c:\" */
26160 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26161 {
26162 valid |= VALID_HEAD;
26163 *usedlen += 2;
26164 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026165 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026166 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026167 *fnamelen = (int)(tail - *fnamep);
26168#ifdef VMS
26169 if (*fnamelen > 0)
26170 *fnamelen += 1; /* the path separator is part of the path */
26171#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026172 if (*fnamelen == 0)
26173 {
26174 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26175 p = vim_strsave((char_u *)".");
26176 if (p == NULL)
26177 return -1;
26178 vim_free(*bufp);
26179 *bufp = *fnamep = tail = p;
26180 *fnamelen = 1;
26181 }
26182 else
26183 {
26184 while (tail > s && !after_pathsep(s, tail))
26185 mb_ptr_back(*fnamep, tail);
26186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026187 }
26188
26189 /* ":8" - shortname */
26190 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26191 {
26192 *usedlen += 2;
26193#ifdef WIN3264
26194 has_shortname = 1;
26195#endif
26196 }
26197
26198#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026199 /*
26200 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026201 */
26202 if (has_shortname)
26203 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026204 /* Copy the string if it is shortened by :h and when it wasn't copied
26205 * yet, because we are going to change it in place. Avoids changing
26206 * the buffer name for "%:8". */
26207 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026208 {
26209 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026210 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026211 return -1;
26212 vim_free(*bufp);
26213 *bufp = *fnamep = p;
26214 }
26215
26216 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026217 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026218 if (!has_fullname && !vim_isAbsName(*fnamep))
26219 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026220 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026221 return -1;
26222 }
26223 else
26224 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026225 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026226
Bram Moolenaardc935552011-08-17 15:23:23 +020026227 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026228 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026229 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026230 return -1;
26231
26232 if (l == 0)
26233 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026234 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026235 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026236 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026237 return -1;
26238 }
26239 *fnamelen = l;
26240 }
26241 }
26242#endif /* WIN3264 */
26243
26244 /* ":t" - tail, just the basename */
26245 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26246 {
26247 *usedlen += 2;
26248 *fnamelen -= (int)(tail - *fnamep);
26249 *fnamep = tail;
26250 }
26251
26252 /* ":e" - extension, can be repeated */
26253 /* ":r" - root, without extension, can be repeated */
26254 while (src[*usedlen] == ':'
26255 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26256 {
26257 /* find a '.' in the tail:
26258 * - for second :e: before the current fname
26259 * - otherwise: The last '.'
26260 */
26261 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26262 s = *fnamep - 2;
26263 else
26264 s = *fnamep + *fnamelen - 1;
26265 for ( ; s > tail; --s)
26266 if (s[0] == '.')
26267 break;
26268 if (src[*usedlen + 1] == 'e') /* :e */
26269 {
26270 if (s > tail)
26271 {
26272 *fnamelen += (int)(*fnamep - (s + 1));
26273 *fnamep = s + 1;
26274#ifdef VMS
26275 /* cut version from the extension */
26276 s = *fnamep + *fnamelen - 1;
26277 for ( ; s > *fnamep; --s)
26278 if (s[0] == ';')
26279 break;
26280 if (s > *fnamep)
26281 *fnamelen = s - *fnamep;
26282#endif
26283 }
26284 else if (*fnamep <= tail)
26285 *fnamelen = 0;
26286 }
26287 else /* :r */
26288 {
26289 if (s > tail) /* remove one extension */
26290 *fnamelen = (int)(s - *fnamep);
26291 }
26292 *usedlen += 2;
26293 }
26294
26295 /* ":s?pat?foo?" - substitute */
26296 /* ":gs?pat?foo?" - global substitute */
26297 if (src[*usedlen] == ':'
26298 && (src[*usedlen + 1] == 's'
26299 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26300 {
26301 char_u *str;
26302 char_u *pat;
26303 char_u *sub;
26304 int sep;
26305 char_u *flags;
26306 int didit = FALSE;
26307
26308 flags = (char_u *)"";
26309 s = src + *usedlen + 2;
26310 if (src[*usedlen + 1] == 'g')
26311 {
26312 flags = (char_u *)"g";
26313 ++s;
26314 }
26315
26316 sep = *s++;
26317 if (sep)
26318 {
26319 /* find end of pattern */
26320 p = vim_strchr(s, sep);
26321 if (p != NULL)
26322 {
26323 pat = vim_strnsave(s, (int)(p - s));
26324 if (pat != NULL)
26325 {
26326 s = p + 1;
26327 /* find end of substitution */
26328 p = vim_strchr(s, sep);
26329 if (p != NULL)
26330 {
26331 sub = vim_strnsave(s, (int)(p - s));
26332 str = vim_strnsave(*fnamep, *fnamelen);
26333 if (sub != NULL && str != NULL)
26334 {
26335 *usedlen = (int)(p + 1 - src);
26336 s = do_string_sub(str, pat, sub, flags);
26337 if (s != NULL)
26338 {
26339 *fnamep = s;
26340 *fnamelen = (int)STRLEN(s);
26341 vim_free(*bufp);
26342 *bufp = s;
26343 didit = TRUE;
26344 }
26345 }
26346 vim_free(sub);
26347 vim_free(str);
26348 }
26349 vim_free(pat);
26350 }
26351 }
26352 /* after using ":s", repeat all the modifiers */
26353 if (didit)
26354 goto repeat;
26355 }
26356 }
26357
Bram Moolenaar26df0922014-02-23 23:39:13 +010026358 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26359 {
26360 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26361 if (p == NULL)
26362 return -1;
26363 vim_free(*bufp);
26364 *bufp = *fnamep = p;
26365 *fnamelen = (int)STRLEN(p);
26366 *usedlen += 2;
26367 }
26368
Bram Moolenaar071d4272004-06-13 20:20:40 +000026369 return valid;
26370}
26371
26372/*
26373 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26374 * "flags" can be "g" to do a global substitute.
26375 * Returns an allocated string, NULL for error.
26376 */
26377 char_u *
26378do_string_sub(str, pat, sub, flags)
26379 char_u *str;
26380 char_u *pat;
26381 char_u *sub;
26382 char_u *flags;
26383{
26384 int sublen;
26385 regmatch_T regmatch;
26386 int i;
26387 int do_all;
26388 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026389 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026390 garray_T ga;
26391 char_u *ret;
26392 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026393 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026394
26395 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26396 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026397 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026398
26399 ga_init2(&ga, 1, 200);
26400
26401 do_all = (flags[0] == 'g');
26402
26403 regmatch.rm_ic = p_ic;
26404 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26405 if (regmatch.regprog != NULL)
26406 {
26407 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026408 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026409 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26410 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026411 /* Skip empty match except for first match. */
26412 if (regmatch.startp[0] == regmatch.endp[0])
26413 {
26414 if (zero_width == regmatch.startp[0])
26415 {
26416 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026417 i = MB_PTR2LEN(tail);
26418 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26419 (size_t)i);
26420 ga.ga_len += i;
26421 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026422 continue;
26423 }
26424 zero_width = regmatch.startp[0];
26425 }
26426
Bram Moolenaar071d4272004-06-13 20:20:40 +000026427 /*
26428 * Get some space for a temporary buffer to do the substitution
26429 * into. It will contain:
26430 * - The text up to where the match is.
26431 * - The substituted text.
26432 * - The text after the match.
26433 */
26434 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026435 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026436 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26437 {
26438 ga_clear(&ga);
26439 break;
26440 }
26441
26442 /* copy the text up to where the match is */
26443 i = (int)(regmatch.startp[0] - tail);
26444 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26445 /* add the substituted text */
26446 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26447 + ga.ga_len + i, TRUE, TRUE, FALSE);
26448 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026449 tail = regmatch.endp[0];
26450 if (*tail == NUL)
26451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452 if (!do_all)
26453 break;
26454 }
26455
26456 if (ga.ga_data != NULL)
26457 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26458
Bram Moolenaar473de612013-06-08 18:19:48 +020026459 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460 }
26461
26462 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26463 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026464 if (p_cpo == empty_option)
26465 p_cpo = save_cpo;
26466 else
26467 /* Darn, evaluating {sub} expression changed the value. */
26468 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469
26470 return ret;
26471}
26472
26473#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */