blob: cc3030849537e0102ae45d7f946fa64014761971 [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 Moolenaarf57969a2016-02-02 20:47:49 +0100502#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100503static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100504# ifdef FEAT_JOB
505static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
506# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100507static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
509static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100510static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100511static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100514static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100516#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_changenr(typval_T *argvars, typval_T *rettv);
518static void f_char2nr(typval_T *argvars, typval_T *rettv);
519static void f_cindent(typval_T *argvars, typval_T *rettv);
520static void f_clearmatches(typval_T *argvars, typval_T *rettv);
521static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000522#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_complete(typval_T *argvars, typval_T *rettv);
524static void f_complete_add(typval_T *argvars, typval_T *rettv);
525static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_confirm(typval_T *argvars, typval_T *rettv);
528static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_cos(typval_T *argvars, typval_T *rettv);
531static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_count(typval_T *argvars, typval_T *rettv);
534static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
535static void f_cursor(typval_T *argsvars, typval_T *rettv);
536static void f_deepcopy(typval_T *argvars, typval_T *rettv);
537static void f_delete(typval_T *argvars, typval_T *rettv);
538static void f_did_filetype(typval_T *argvars, typval_T *rettv);
539static void f_diff_filler(typval_T *argvars, typval_T *rettv);
540static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100541static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_empty(typval_T *argvars, typval_T *rettv);
543static void f_escape(typval_T *argvars, typval_T *rettv);
544static void f_eval(typval_T *argvars, typval_T *rettv);
545static void f_eventhandler(typval_T *argvars, typval_T *rettv);
546static void f_executable(typval_T *argvars, typval_T *rettv);
547static void f_exepath(typval_T *argvars, typval_T *rettv);
548static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200551#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100552static void f_expand(typval_T *argvars, typval_T *rettv);
553static void f_extend(typval_T *argvars, typval_T *rettv);
554static void f_feedkeys(typval_T *argvars, typval_T *rettv);
555static void f_filereadable(typval_T *argvars, typval_T *rettv);
556static void f_filewritable(typval_T *argvars, typval_T *rettv);
557static void f_filter(typval_T *argvars, typval_T *rettv);
558static void f_finddir(typval_T *argvars, typval_T *rettv);
559static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_float2nr(typval_T *argvars, typval_T *rettv);
562static void f_floor(typval_T *argvars, typval_T *rettv);
563static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000564#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100565static void f_fnameescape(typval_T *argvars, typval_T *rettv);
566static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
567static void f_foldclosed(typval_T *argvars, typval_T *rettv);
568static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
569static void f_foldlevel(typval_T *argvars, typval_T *rettv);
570static void f_foldtext(typval_T *argvars, typval_T *rettv);
571static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
572static void f_foreground(typval_T *argvars, typval_T *rettv);
573static void f_function(typval_T *argvars, typval_T *rettv);
574static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
575static void f_get(typval_T *argvars, typval_T *rettv);
576static void f_getbufline(typval_T *argvars, typval_T *rettv);
577static void f_getbufvar(typval_T *argvars, typval_T *rettv);
578static void f_getchar(typval_T *argvars, typval_T *rettv);
579static void f_getcharmod(typval_T *argvars, typval_T *rettv);
580static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
581static void f_getcmdline(typval_T *argvars, typval_T *rettv);
582static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
583static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
584static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
585static void f_getcwd(typval_T *argvars, typval_T *rettv);
586static void f_getfontname(typval_T *argvars, typval_T *rettv);
587static void f_getfperm(typval_T *argvars, typval_T *rettv);
588static void f_getfsize(typval_T *argvars, typval_T *rettv);
589static void f_getftime(typval_T *argvars, typval_T *rettv);
590static void f_getftype(typval_T *argvars, typval_T *rettv);
591static void f_getline(typval_T *argvars, typval_T *rettv);
592static void f_getmatches(typval_T *argvars, typval_T *rettv);
593static void f_getpid(typval_T *argvars, typval_T *rettv);
594static void f_getcurpos(typval_T *argvars, typval_T *rettv);
595static void f_getpos(typval_T *argvars, typval_T *rettv);
596static void f_getqflist(typval_T *argvars, typval_T *rettv);
597static void f_getreg(typval_T *argvars, typval_T *rettv);
598static void f_getregtype(typval_T *argvars, typval_T *rettv);
599static void f_gettabvar(typval_T *argvars, typval_T *rettv);
600static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
601static void f_getwinposx(typval_T *argvars, typval_T *rettv);
602static void f_getwinposy(typval_T *argvars, typval_T *rettv);
603static void f_getwinvar(typval_T *argvars, typval_T *rettv);
604static void f_glob(typval_T *argvars, typval_T *rettv);
605static void f_globpath(typval_T *argvars, typval_T *rettv);
606static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
607static void f_has(typval_T *argvars, typval_T *rettv);
608static void f_has_key(typval_T *argvars, typval_T *rettv);
609static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
610static void f_hasmapto(typval_T *argvars, typval_T *rettv);
611static void f_histadd(typval_T *argvars, typval_T *rettv);
612static void f_histdel(typval_T *argvars, typval_T *rettv);
613static void f_histget(typval_T *argvars, typval_T *rettv);
614static void f_histnr(typval_T *argvars, typval_T *rettv);
615static void f_hlID(typval_T *argvars, typval_T *rettv);
616static void f_hlexists(typval_T *argvars, typval_T *rettv);
617static void f_hostname(typval_T *argvars, typval_T *rettv);
618static void f_iconv(typval_T *argvars, typval_T *rettv);
619static void f_indent(typval_T *argvars, typval_T *rettv);
620static void f_index(typval_T *argvars, typval_T *rettv);
621static void f_input(typval_T *argvars, typval_T *rettv);
622static void f_inputdialog(typval_T *argvars, typval_T *rettv);
623static void f_inputlist(typval_T *argvars, typval_T *rettv);
624static void f_inputrestore(typval_T *argvars, typval_T *rettv);
625static void f_inputsave(typval_T *argvars, typval_T *rettv);
626static void f_inputsecret(typval_T *argvars, typval_T *rettv);
627static void f_insert(typval_T *argvars, typval_T *rettv);
628static void f_invert(typval_T *argvars, typval_T *rettv);
629static void f_isdirectory(typval_T *argvars, typval_T *rettv);
630static void f_islocked(typval_T *argvars, typval_T *rettv);
631static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100632#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100633# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100634static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100635# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100636static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100637static void f_job_start(typval_T *argvars, typval_T *rettv);
638static void f_job_stop(typval_T *argvars, typval_T *rettv);
639static void f_job_status(typval_T *argvars, typval_T *rettv);
640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100642static void f_js_decode(typval_T *argvars, typval_T *rettv);
643static void f_js_encode(typval_T *argvars, typval_T *rettv);
644static void f_json_decode(typval_T *argvars, typval_T *rettv);
645static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_keys(typval_T *argvars, typval_T *rettv);
647static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
648static void f_len(typval_T *argvars, typval_T *rettv);
649static void f_libcall(typval_T *argvars, typval_T *rettv);
650static void f_libcallnr(typval_T *argvars, typval_T *rettv);
651static void f_line(typval_T *argvars, typval_T *rettv);
652static void f_line2byte(typval_T *argvars, typval_T *rettv);
653static void f_lispindent(typval_T *argvars, typval_T *rettv);
654static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_log(typval_T *argvars, typval_T *rettv);
657static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200659#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_map(typval_T *argvars, typval_T *rettv);
663static void f_maparg(typval_T *argvars, typval_T *rettv);
664static void f_mapcheck(typval_T *argvars, typval_T *rettv);
665static void f_match(typval_T *argvars, typval_T *rettv);
666static void f_matchadd(typval_T *argvars, typval_T *rettv);
667static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
668static void f_matcharg(typval_T *argvars, typval_T *rettv);
669static void f_matchdelete(typval_T *argvars, typval_T *rettv);
670static void f_matchend(typval_T *argvars, typval_T *rettv);
671static void f_matchlist(typval_T *argvars, typval_T *rettv);
672static void f_matchstr(typval_T *argvars, typval_T *rettv);
673static void f_max(typval_T *argvars, typval_T *rettv);
674static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000675#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100679#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100681#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
683static void f_nr2char(typval_T *argvars, typval_T *rettv);
684static void f_or(typval_T *argvars, typval_T *rettv);
685static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100686#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100688#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
693static void f_printf(typval_T *argvars, typval_T *rettv);
694static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200695#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200697#endif
698#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_range(typval_T *argvars, typval_T *rettv);
702static void f_readfile(typval_T *argvars, typval_T *rettv);
703static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100704#ifdef FEAT_FLOAT
705static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_reltimestr(typval_T *argvars, typval_T *rettv);
708static void f_remote_expr(typval_T *argvars, typval_T *rettv);
709static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
710static void f_remote_peek(typval_T *argvars, typval_T *rettv);
711static void f_remote_read(typval_T *argvars, typval_T *rettv);
712static void f_remote_send(typval_T *argvars, typval_T *rettv);
713static void f_remove(typval_T *argvars, typval_T *rettv);
714static void f_rename(typval_T *argvars, typval_T *rettv);
715static void f_repeat(typval_T *argvars, typval_T *rettv);
716static void f_resolve(typval_T *argvars, typval_T *rettv);
717static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_screenattr(typval_T *argvars, typval_T *rettv);
722static void f_screenchar(typval_T *argvars, typval_T *rettv);
723static void f_screencol(typval_T *argvars, typval_T *rettv);
724static void f_screenrow(typval_T *argvars, typval_T *rettv);
725static void f_search(typval_T *argvars, typval_T *rettv);
726static void f_searchdecl(typval_T *argvars, typval_T *rettv);
727static void f_searchpair(typval_T *argvars, typval_T *rettv);
728static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
729static void f_searchpos(typval_T *argvars, typval_T *rettv);
730static void f_server2client(typval_T *argvars, typval_T *rettv);
731static void f_serverlist(typval_T *argvars, typval_T *rettv);
732static void f_setbufvar(typval_T *argvars, typval_T *rettv);
733static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
734static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
735static void f_setline(typval_T *argvars, typval_T *rettv);
736static void f_setloclist(typval_T *argvars, typval_T *rettv);
737static void f_setmatches(typval_T *argvars, typval_T *rettv);
738static void f_setpos(typval_T *argvars, typval_T *rettv);
739static void f_setqflist(typval_T *argvars, typval_T *rettv);
740static void f_setreg(typval_T *argvars, typval_T *rettv);
741static void f_settabvar(typval_T *argvars, typval_T *rettv);
742static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
743static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100744#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100746#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_shellescape(typval_T *argvars, typval_T *rettv);
748static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
749static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_sin(typval_T *argvars, typval_T *rettv);
752static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sort(typval_T *argvars, typval_T *rettv);
755static void f_soundfold(typval_T *argvars, typval_T *rettv);
756static void f_spellbadword(typval_T *argvars, typval_T *rettv);
757static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
758static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sqrt(typval_T *argvars, typval_T *rettv);
761static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_str2nr(typval_T *argvars, typval_T *rettv);
764static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000765#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_stridx(typval_T *argvars, typval_T *rettv);
769static void f_string(typval_T *argvars, typval_T *rettv);
770static void f_strlen(typval_T *argvars, typval_T *rettv);
771static void f_strpart(typval_T *argvars, typval_T *rettv);
772static void f_strridx(typval_T *argvars, typval_T *rettv);
773static void f_strtrans(typval_T *argvars, typval_T *rettv);
774static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
775static void f_strwidth(typval_T *argvars, typval_T *rettv);
776static void f_submatch(typval_T *argvars, typval_T *rettv);
777static void f_substitute(typval_T *argvars, typval_T *rettv);
778static void f_synID(typval_T *argvars, typval_T *rettv);
779static void f_synIDattr(typval_T *argvars, typval_T *rettv);
780static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
781static void f_synstack(typval_T *argvars, typval_T *rettv);
782static void f_synconcealed(typval_T *argvars, typval_T *rettv);
783static void f_system(typval_T *argvars, typval_T *rettv);
784static void f_systemlist(typval_T *argvars, typval_T *rettv);
785static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
787static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
788static void f_taglist(typval_T *argvars, typval_T *rettv);
789static void f_tagfiles(typval_T *argvars, typval_T *rettv);
790static void f_tempname(typval_T *argvars, typval_T *rettv);
791static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200792#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100793static void f_tan(typval_T *argvars, typval_T *rettv);
794static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tolower(typval_T *argvars, typval_T *rettv);
797static void f_toupper(typval_T *argvars, typval_T *rettv);
798static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000799#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000801#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100802static void f_type(typval_T *argvars, typval_T *rettv);
803static void f_undofile(typval_T *argvars, typval_T *rettv);
804static void f_undotree(typval_T *argvars, typval_T *rettv);
805static void f_uniq(typval_T *argvars, typval_T *rettv);
806static void f_values(typval_T *argvars, typval_T *rettv);
807static void f_virtcol(typval_T *argvars, typval_T *rettv);
808static void f_visualmode(typval_T *argvars, typval_T *rettv);
809static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
810static void f_winbufnr(typval_T *argvars, typval_T *rettv);
811static void f_wincol(typval_T *argvars, typval_T *rettv);
812static void f_winheight(typval_T *argvars, typval_T *rettv);
813static void f_winline(typval_T *argvars, typval_T *rettv);
814static void f_winnr(typval_T *argvars, typval_T *rettv);
815static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
816static void f_winrestview(typval_T *argvars, typval_T *rettv);
817static void f_winsaveview(typval_T *argvars, typval_T *rettv);
818static void f_winwidth(typval_T *argvars, typval_T *rettv);
819static void f_writefile(typval_T *argvars, typval_T *rettv);
820static void f_wordcount(typval_T *argvars, typval_T *rettv);
821static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000822
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100823static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
824static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
825static int get_env_len(char_u **arg);
826static int get_id_len(char_u **arg);
827static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
828static 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 +0000829#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
830#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
831 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
833static int eval_isnamec(int c);
834static int eval_isnamec1(int c);
835static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
836static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static typval_T *alloc_string_tv(char_u *string);
838static void init_tv(typval_T *varp);
839static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100840#ifdef FEAT_FLOAT
841static float_T get_tv_float(typval_T *varp);
842#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100843static linenr_T get_tv_lnum(typval_T *argvars);
844static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
845static char_u *get_tv_string(typval_T *varp);
846static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
847static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
848static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
849static hashtab_T *find_var_ht(char_u *name, char_u **varname);
850static funccall_T *get_funccal(void);
851static void vars_clear_ext(hashtab_T *ht, int free_val);
852static void delete_var(hashtab_T *ht, hashitem_T *hi);
853static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
854static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
855static void set_var(char_u *name, typval_T *varp, int copy);
856static int var_check_ro(int flags, char_u *name, int use_gettext);
857static int var_check_fixed(int flags, char_u *name, int use_gettext);
858static int var_check_func_name(char_u *name, int new_var);
859static int valid_varname(char_u *varname);
860static int tv_check_lock(int lock, char_u *name, int use_gettext);
861static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
862static char_u *find_option_end(char_u **arg, int *opt_flags);
863static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
864static int eval_fname_script(char_u *p);
865static int eval_fname_sid(char_u *p);
866static void list_func_head(ufunc_T *fp, int indent);
867static ufunc_T *find_func(char_u *name);
868static int function_exists(char_u *name);
869static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000870#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871static void func_do_profile(ufunc_T *fp);
872static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
873static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000874static int
875# ifdef __BORLANDC__
876 _RTLENTRYF
877# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000879static int
880# ifdef __BORLANDC__
881 _RTLENTRYF
882# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000884#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885static int script_autoload(char_u *name, int reload);
886static char_u *autoload_name(char_u *name);
887static void cat_func_name(char_u *buf, ufunc_T *fp);
888static void func_free(ufunc_T *fp);
889static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
890static int can_free_funccal(funccall_T *fc, int copyID) ;
891static void free_funccal(funccall_T *fc, int free_val);
892static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
893static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
894static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
895static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
896static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
897static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
898static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
899static int write_list(FILE *fd, list_T *list, int binary);
900static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000901
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200902
903#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100904static int compare_func_name(const void *s1, const void *s2);
905static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200906#endif
907
Bram Moolenaar33570922005-01-25 22:26:29 +0000908/*
909 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000910 */
911 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100912eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000913{
Bram Moolenaar33570922005-01-25 22:26:29 +0000914 int i;
915 struct vimvar *p;
916
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200917 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
918 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200919 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000920 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000921 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000922
923 for (i = 0; i < VV_LEN; ++i)
924 {
925 p = &vimvars[i];
926 STRCPY(p->vv_di.di_key, p->vv_name);
927 if (p->vv_flags & VV_RO)
928 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
929 else if (p->vv_flags & VV_RO_SBX)
930 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
931 else
932 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000933
934 /* add to v: scope dict, unless the value is not always available */
935 if (p->vv_type != VAR_UNKNOWN)
936 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000937 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000938 /* add to compat scope dict */
939 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100941 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
942
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000943 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100944 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200945 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100946 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100947
948 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
949 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
950 set_vim_var_nr(VV_NONE, VVAL_NONE);
951 set_vim_var_nr(VV_NULL, VVAL_NULL);
952
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200953 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200954
955#ifdef EBCDIC
956 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100957 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200958 */
959 sortFunctions();
960#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000961}
962
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000963#if defined(EXITFREE) || defined(PROTO)
964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100965eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966{
967 int i;
968 struct vimvar *p;
969
970 for (i = 0; i < VV_LEN; ++i)
971 {
972 p = &vimvars[i];
973 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000974 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000975 vim_free(p->vv_str);
976 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000977 }
978 else if (p->vv_di.di_tv.v_type == VAR_LIST)
979 {
980 list_unref(p->vv_list);
981 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000982 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000983 }
984 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000985 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000986 hash_clear(&compat_hashtab);
987
Bram Moolenaard9fba312005-06-26 22:34:35 +0000988 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100989# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200990 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100991# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992
993 /* global variables */
994 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000995
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000996 /* autoloaded script names */
997 ga_clear_strings(&ga_loaded);
998
Bram Moolenaarcca74132013-09-25 21:00:28 +0200999 /* Script-local variables. First clear all the variables and in a second
1000 * loop free the scriptvar_T, because a variable in one script might hold
1001 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001002 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001003 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001004 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001005 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 ga_clear(&ga_scripts);
1007
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001008 /* unreferenced lists and dicts */
1009 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001010
1011 /* functions */
1012 free_all_functions();
1013 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001014}
1015#endif
1016
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 * Return the name of the executed function.
1019 */
1020 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001021func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024}
1025
1026/*
1027 * Return the address holding the next breakpoint line for a funccall cookie.
1028 */
1029 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001030func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
Bram Moolenaar33570922005-01-25 22:26:29 +00001032 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033}
1034
1035/*
1036 * Return the address holding the debug tick for a funccall cookie.
1037 */
1038 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001039func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar33570922005-01-25 22:26:29 +00001041 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042}
1043
1044/*
1045 * Return the nesting level for a funccall cookie.
1046 */
1047 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001048func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar33570922005-01-25 22:26:29 +00001050 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
1052
1053/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001054funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001056/* pointer to list of previously used funccal, still around because some
1057 * item in it is still being used. */
1058funccall_T *previous_funccal = NULL;
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060/*
1061 * Return TRUE when a function was ended by a ":return" command.
1062 */
1063 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001064current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
1066 return current_funccal->returned;
1067}
1068
1069
1070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 * Set an internal variable to a string value. Creates the variable if it does
1072 * not already exist.
1073 */
1074 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001075set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001077 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001078 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
1080 val = vim_strsave(value);
1081 if (val != NULL)
1082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001083 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001084 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001086 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001087 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 }
1090}
1091
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001093static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094static char_u *redir_endp = NULL;
1095static char_u *redir_varname = NULL;
1096
1097/*
1098 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001099 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100 * Returns OK if successfully completed the setup. FAIL otherwise.
1101 */
1102 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001103var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104{
1105 int save_emsg;
1106 int err;
1107 typval_T tv;
1108
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001109 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001110 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 {
1112 EMSG(_(e_invarg));
1113 return FAIL;
1114 }
1115
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001116 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 redir_varname = vim_strsave(name);
1118 if (redir_varname == NULL)
1119 return FAIL;
1120
1121 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1122 if (redir_lval == NULL)
1123 {
1124 var_redir_stop();
1125 return FAIL;
1126 }
1127
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001128 /* The output is stored in growarray "redir_ga" until redirection ends. */
1129 ga_init2(&redir_ga, (int)sizeof(char), 500);
1130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001132 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001133 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1135 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001136 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_endp != NULL && *redir_endp != NUL)
1138 /* Trailing characters are present after the variable name */
1139 EMSG(_(e_trailing));
1140 else
1141 EMSG(_(e_invarg));
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 }
1146
1147 /* check if we can write to the variable: set it to or append an empty
1148 * string */
1149 save_emsg = did_emsg;
1150 did_emsg = FALSE;
1151 tv.v_type = VAR_STRING;
1152 tv.vval.v_string = (char_u *)"";
1153 if (append)
1154 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1155 else
1156 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001157 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001159 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 if (err)
1161 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001162 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 var_redir_stop();
1164 return FAIL;
1165 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166
1167 return OK;
1168}
1169
1170/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001171 * Append "value[value_len]" to the variable set by var_redir_start().
1172 * The actual appending is postponed until redirection ends, because the value
1173 * appended may in fact be the string we write to, changing it may cause freed
1174 * memory to be used:
1175 * :redir => foo
1176 * :let foo
1177 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 */
1179 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001180var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001182 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183
1184 if (redir_lval == NULL)
1185 return;
1186
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001187 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001190 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001192 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193 {
1194 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 }
1197 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199}
1200
1201/*
1202 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001203 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 */
1205 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001206var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001208 typval_T tv;
1209
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210 if (redir_lval != NULL)
1211 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001212 /* If there was no error: assign the text to the variable. */
1213 if (redir_endp != NULL)
1214 {
1215 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1216 tv.v_type = VAR_STRING;
1217 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001218 /* Call get_lval() again, if it's inside a Dict or List it may
1219 * have changed. */
1220 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001221 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001222 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1223 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1224 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001225 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001227 /* free the collected output */
1228 vim_free(redir_ga.ga_data);
1229 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001230
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001231 vim_free(redir_lval);
1232 redir_lval = NULL;
1233 }
1234 vim_free(redir_varname);
1235 redir_varname = NULL;
1236}
1237
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238# if defined(FEAT_MBYTE) || defined(PROTO)
1239 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001240eval_charconvert(
1241 char_u *enc_from,
1242 char_u *enc_to,
1243 char_u *fname_from,
1244 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245{
1246 int err = FALSE;
1247
1248 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1249 set_vim_var_string(VV_CC_TO, enc_to, -1);
1250 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1251 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1252 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1253 err = TRUE;
1254 set_vim_var_string(VV_CC_FROM, NULL, -1);
1255 set_vim_var_string(VV_CC_TO, NULL, -1);
1256 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1257 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1258
1259 if (err)
1260 return FAIL;
1261 return OK;
1262}
1263# endif
1264
1265# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001267eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 int err = FALSE;
1270
1271 set_vim_var_string(VV_FNAME_IN, fname, -1);
1272 set_vim_var_string(VV_CMDARG, args, -1);
1273 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1274 err = TRUE;
1275 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1276 set_vim_var_string(VV_CMDARG, NULL, -1);
1277
1278 if (err)
1279 {
1280 mch_remove(fname);
1281 return FAIL;
1282 }
1283 return OK;
1284}
1285# endif
1286
1287# if defined(FEAT_DIFF) || defined(PROTO)
1288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001289eval_diff(
1290 char_u *origfile,
1291 char_u *newfile,
1292 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 int err = FALSE;
1295
1296 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1297 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1298 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1299 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1300 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1301 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1302 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1303}
1304
1305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001306eval_patch(
1307 char_u *origfile,
1308 char_u *difffile,
1309 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 int err;
1312
1313 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1314 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1315 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1316 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1317 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1318 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1319 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1320}
1321# endif
1322
1323/*
1324 * Top level evaluation function, returning a boolean.
1325 * Sets "error" to TRUE if there was an error.
1326 * Return TRUE or FALSE.
1327 */
1328 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001329eval_to_bool(
1330 char_u *arg,
1331 int *error,
1332 char_u **nextcmd,
1333 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334{
Bram Moolenaar33570922005-01-25 22:26:29 +00001335 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 int retval = FALSE;
1337
1338 if (skip)
1339 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001340 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 else
1343 {
1344 *error = FALSE;
1345 if (!skip)
1346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001347 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350 }
1351 if (skip)
1352 --emsg_skip;
1353
1354 return retval;
1355}
1356
1357/*
1358 * Top level evaluation function, returning a string. If "skip" is TRUE,
1359 * only parsing to "nextcmd" is done, without reporting errors. Return
1360 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1361 */
1362 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001363eval_to_string_skip(
1364 char_u *arg,
1365 char_u **nextcmd,
1366 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367{
Bram Moolenaar33570922005-01-25 22:26:29 +00001368 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 char_u *retval;
1370
1371 if (skip)
1372 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 retval = NULL;
1375 else
1376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 retval = vim_strsave(get_tv_string(&tv));
1378 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380 if (skip)
1381 --emsg_skip;
1382
1383 return retval;
1384}
1385
1386/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001387 * Skip over an expression at "*pp".
1388 * Return FAIL for an error, OK otherwise.
1389 */
1390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001391skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001392{
Bram Moolenaar33570922005-01-25 22:26:29 +00001393 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001394
1395 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001396 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001397}
1398
1399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001401 * When "convert" is TRUE convert a List into a sequence of lines and convert
1402 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 * Return pointer to allocated memory, or NULL for failure.
1404 */
1405 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001406eval_to_string(
1407 char_u *arg,
1408 char_u **nextcmd,
1409 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001413 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001414#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001415 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 retval = NULL;
1420 else
1421 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001422 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001423 {
1424 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001425 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001426 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001427 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001428 if (tv.vval.v_list->lv_len > 0)
1429 ga_append(&ga, NL);
1430 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001431 ga_append(&ga, NUL);
1432 retval = (char_u *)ga.ga_data;
1433 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001434#ifdef FEAT_FLOAT
1435 else if (convert && tv.v_type == VAR_FLOAT)
1436 {
1437 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1438 retval = vim_strsave(numbuf);
1439 }
1440#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001441 else
1442 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001443 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445
1446 return retval;
1447}
1448
1449/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001450 * Call eval_to_string() without using current local variables and using
1451 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 */
1453 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001454eval_to_string_safe(
1455 char_u *arg,
1456 char_u **nextcmd,
1457 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 char_u *retval;
1460 void *save_funccalp;
1461
1462 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001463 if (use_sandbox)
1464 ++sandbox;
1465 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001466 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001467 if (use_sandbox)
1468 --sandbox;
1469 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 restore_funccal(save_funccalp);
1471 return retval;
1472}
1473
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474/*
1475 * Top level evaluation function, returning a number.
1476 * Evaluates "expr" silently.
1477 * Returns -1 for an error.
1478 */
1479 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001480eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481{
Bram Moolenaar33570922005-01-25 22:26:29 +00001482 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001484 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 ++emsg_off;
1487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001488 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 retval = -1;
1490 else
1491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001492 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001493 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 }
1495 --emsg_off;
1496
1497 return retval;
1498}
1499
Bram Moolenaara40058a2005-07-11 22:42:07 +00001500/*
1501 * Prepare v: variable "idx" to be used.
1502 * Save the current typeval in "save_tv".
1503 * When not used yet add the variable to the v: hashtable.
1504 */
1505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001506prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001507{
1508 *save_tv = vimvars[idx].vv_tv;
1509 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1510 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1511}
1512
1513/*
1514 * Restore v: variable "idx" to typeval "save_tv".
1515 * When no longer defined, remove the variable from the v: hashtable.
1516 */
1517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001518restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001519{
1520 hashitem_T *hi;
1521
Bram Moolenaara40058a2005-07-11 22:42:07 +00001522 vimvars[idx].vv_tv = *save_tv;
1523 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1524 {
1525 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1526 if (HASHITEM_EMPTY(hi))
1527 EMSG2(_(e_intern2), "restore_vimvar()");
1528 else
1529 hash_remove(&vimvarht, hi);
1530 }
1531}
1532
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001533#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001534/*
1535 * Evaluate an expression to a list with suggestions.
1536 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001537 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001538 */
1539 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001540eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541{
1542 typval_T save_val;
1543 typval_T rettv;
1544 list_T *list = NULL;
1545 char_u *p = skipwhite(expr);
1546
1547 /* Set "v:val" to the bad word. */
1548 prepare_vimvar(VV_VAL, &save_val);
1549 vimvars[VV_VAL].vv_type = VAR_STRING;
1550 vimvars[VV_VAL].vv_str = badword;
1551 if (p_verbose == 0)
1552 ++emsg_off;
1553
1554 if (eval1(&p, &rettv, TRUE) == OK)
1555 {
1556 if (rettv.v_type != VAR_LIST)
1557 clear_tv(&rettv);
1558 else
1559 list = rettv.vval.v_list;
1560 }
1561
1562 if (p_verbose == 0)
1563 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001564 restore_vimvar(VV_VAL, &save_val);
1565
1566 return list;
1567}
1568
1569/*
1570 * "list" is supposed to contain two items: a word and a number. Return the
1571 * word in "pp" and the number as the return value.
1572 * Return -1 if anything isn't right.
1573 * Used to get the good word and score from the eval_spell_expr() result.
1574 */
1575 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001576get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001577{
1578 listitem_T *li;
1579
1580 li = list->lv_first;
1581 if (li == NULL)
1582 return -1;
1583 *pp = get_tv_string(&li->li_tv);
1584
1585 li = li->li_next;
1586 if (li == NULL)
1587 return -1;
1588 return get_tv_number(&li->li_tv);
1589}
1590#endif
1591
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001592/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001593 * Top level evaluation function.
1594 * Returns an allocated typval_T with the result.
1595 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001596 */
1597 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001598eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001599{
1600 typval_T *tv;
1601
1602 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001603 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001604 {
1605 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001606 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001607 }
1608
1609 return tv;
1610}
1611
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001615 * Uses argv[argc] for the function arguments. Only Number and String
1616 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001619 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001620call_vim_function(
1621 char_u *func,
1622 int argc,
1623 char_u **argv,
1624 int safe, /* use the sandbox */
1625 int str_arg_only, /* all arguments are strings */
1626 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
Bram Moolenaar33570922005-01-25 22:26:29 +00001628 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 long n;
1630 int len;
1631 int i;
1632 int doesrange;
1633 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001636 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
1640 for (i = 0; i < argc; i++)
1641 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001642 /* Pass a NULL or empty argument as an empty string */
1643 if (argv[i] == NULL || *argv[i] == NUL)
1644 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001645 argvars[i].v_type = VAR_STRING;
1646 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001647 continue;
1648 }
1649
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001650 if (str_arg_only)
1651 len = 0;
1652 else
1653 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001654 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (len != 0 && len == (int)STRLEN(argv[i]))
1656 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001657 argvars[i].v_type = VAR_NUMBER;
1658 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
1660 else
1661 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001662 argvars[i].v_type = VAR_STRING;
1663 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 }
1665 }
1666
1667 if (safe)
1668 {
1669 save_funccalp = save_funccal();
1670 ++sandbox;
1671 }
1672
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1674 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (safe)
1678 {
1679 --sandbox;
1680 restore_funccal(save_funccalp);
1681 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 vim_free(argvars);
1683
1684 if (ret == FAIL)
1685 clear_tv(rettv);
1686
1687 return ret;
1688}
1689
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001690/*
1691 * Call vimL function "func" and return the result as a number.
1692 * Returns -1 when calling the function fails.
1693 * Uses argv[argc] for the function arguments.
1694 */
1695 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001696call_func_retnr(
1697 char_u *func,
1698 int argc,
1699 char_u **argv,
1700 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001701{
1702 typval_T rettv;
1703 long retval;
1704
1705 /* All arguments are passed as strings, no conversion to number. */
1706 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1707 return -1;
1708
1709 retval = get_tv_number_chk(&rettv, NULL);
1710 clear_tv(&rettv);
1711 return retval;
1712}
1713
1714#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1715 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1716
Bram Moolenaar4f688582007-07-24 12:34:30 +00001717# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001718/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001719 * Call vimL function "func" and return the result as a string.
1720 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 * Uses argv[argc] for the function arguments.
1722 */
1723 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001724call_func_retstr(
1725 char_u *func,
1726 int argc,
1727 char_u **argv,
1728 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001729{
1730 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001731 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001733 /* All arguments are passed as strings, no conversion to number. */
1734 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735 return NULL;
1736
1737 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 return retval;
1740}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001741# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001742
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001743/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001744 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001746 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747 */
1748 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001749call_func_retlist(
1750 char_u *func,
1751 int argc,
1752 char_u **argv,
1753 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754{
1755 typval_T rettv;
1756
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001757 /* All arguments are passed as strings, no conversion to number. */
1758 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001759 return NULL;
1760
1761 if (rettv.v_type != VAR_LIST)
1762 {
1763 clear_tv(&rettv);
1764 return NULL;
1765 }
1766
1767 return rettv.vval.v_list;
1768}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769#endif
1770
1771/*
1772 * Save the current function call pointer, and set it to NULL.
1773 * Used when executing autocommands and for ":source".
1774 */
1775 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001778 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 current_funccal = NULL;
1781 return (void *)fc;
1782}
1783
1784 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001787 funccall_T *fc = (funccall_T *)vfc;
1788
1789 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790}
1791
Bram Moolenaar05159a02005-02-26 23:04:13 +00001792#if defined(FEAT_PROFILE) || defined(PROTO)
1793/*
1794 * Prepare profiling for entering a child or something else that is not
1795 * counted for the script/function itself.
1796 * Should always be called in pair with prof_child_exit().
1797 */
1798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799prof_child_enter(
1800 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001801{
1802 funccall_T *fc = current_funccal;
1803
1804 if (fc != NULL && fc->func->uf_profiling)
1805 profile_start(&fc->prof_child);
1806 script_prof_save(tm);
1807}
1808
1809/*
1810 * Take care of time spent in a child.
1811 * Should always be called after prof_child_enter().
1812 */
1813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814prof_child_exit(
1815 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816{
1817 funccall_T *fc = current_funccal;
1818
1819 if (fc != NULL && fc->func->uf_profiling)
1820 {
1821 profile_end(&fc->prof_child);
1822 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1823 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1824 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1825 }
1826 script_prof_restore(tm);
1827}
1828#endif
1829
1830
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#ifdef FEAT_FOLDING
1832/*
1833 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1834 * it in "*cp". Doesn't give error messages.
1835 */
1836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838{
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 int retval;
1841 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001842 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1843 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
1845 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001846 if (use_sandbox)
1847 ++sandbox;
1848 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 retval = 0;
1852 else
1853 {
1854 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 if (tv.v_type == VAR_NUMBER)
1856 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001857 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 retval = 0;
1859 else
1860 {
1861 /* If the result is a string, check if there is a non-digit before
1862 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (!VIM_ISDIGIT(*s) && *s != '-')
1865 *cp = *s++;
1866 retval = atol((char *)s);
1867 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
1870 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001871 if (use_sandbox)
1872 --sandbox;
1873 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874
1875 return retval;
1876}
1877#endif
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880 * ":let" list all variable values
1881 * ":let var1 var2" list variable values
1882 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 * ":let var += expr" assignment command.
1884 * ":let var -= expr" assignment command.
1885 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 */
1888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001889ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001893 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 int var_count = 0;
1896 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001898 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001899 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
Bram Moolenaardb552d602006-03-23 22:59:57 +00001901 argend = skip_var_list(arg, &var_count, &semicolon);
1902 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001903 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001904 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1905 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001906 expr = skipwhite(argend);
1907 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1908 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001910 /*
1911 * ":let" without "=": list variables
1912 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 if (*arg == '[')
1914 EMSG(_(e_invarg));
1915 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001917 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001921 list_glob_vars(&first);
1922 list_buf_vars(&first);
1923 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001924#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001926#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001927 list_script_vars(&first);
1928 list_func_vars(&first);
1929 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 eap->nextcmd = check_nextcmd(arg);
1932 }
1933 else
1934 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 op[0] = '=';
1936 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001937 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001939 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1940 op[0] = *expr; /* +=, -= or .= */
1941 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001943 else
1944 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (eap->skip)
1947 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (eap->skip)
1950 {
1951 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 --emsg_skip;
1954 }
1955 else if (i != FAIL)
1956 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 }
1961 }
1962}
1963
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964/*
1965 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1966 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 * When "nextchars" is not NULL it points to a string with characters that
1968 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1969 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970 * Returns OK or FAIL;
1971 */
1972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001973ex_let_vars(
1974 char_u *arg_start,
1975 typval_T *tv,
1976 int copy, /* copy values from "tv", don't move */
1977 int semicolon, /* from skip_var_list() */
1978 int var_count, /* from skip_var_list() */
1979 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980{
1981 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001984 listitem_T *item;
1985 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986
1987 if (*arg != '[')
1988 {
1989 /*
1990 * ":let var = expr" or ":for var in list"
1991 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001992 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 return FAIL;
1994 return OK;
1995 }
1996
1997 /*
1998 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1999 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002000 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 {
2002 EMSG(_(e_listreq));
2003 return FAIL;
2004 }
2005
2006 i = list_len(l);
2007 if (semicolon == 0 && var_count < i)
2008 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002009 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 return FAIL;
2011 }
2012 if (var_count - semicolon > i)
2013 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002014 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002015 return FAIL;
2016 }
2017
2018 item = l->lv_first;
2019 while (*arg != ']')
2020 {
2021 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002022 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 item = item->li_next;
2024 if (arg == NULL)
2025 return FAIL;
2026
2027 arg = skipwhite(arg);
2028 if (*arg == ';')
2029 {
2030 /* Put the rest of the list (may be empty) in the var after ';'.
2031 * Create a new list for this. */
2032 l = list_alloc();
2033 if (l == NULL)
2034 return FAIL;
2035 while (item != NULL)
2036 {
2037 list_append_tv(l, &item->li_tv);
2038 item = item->li_next;
2039 }
2040
2041 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002042 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002043 ltv.vval.v_list = l;
2044 l->lv_refcount = 1;
2045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002046 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2047 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 clear_tv(&ltv);
2049 if (arg == NULL)
2050 return FAIL;
2051 break;
2052 }
2053 else if (*arg != ',' && *arg != ']')
2054 {
2055 EMSG2(_(e_intern2), "ex_let_vars()");
2056 return FAIL;
2057 }
2058 }
2059
2060 return OK;
2061}
2062
2063/*
2064 * Skip over assignable variable "var" or list of variables "[var, var]".
2065 * Used for ":let varvar = expr" and ":for varvar in expr".
2066 * For "[var, var]" increment "*var_count" for each variable.
2067 * for "[var, var; var]" set "semicolon".
2068 * Return NULL for an error.
2069 */
2070 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002071skip_var_list(
2072 char_u *arg,
2073 int *var_count,
2074 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002075{
2076 char_u *p, *s;
2077
2078 if (*arg == '[')
2079 {
2080 /* "[var, var]": find the matching ']'. */
2081 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002082 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002083 {
2084 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2085 s = skip_var_one(p);
2086 if (s == p)
2087 {
2088 EMSG2(_(e_invarg2), p);
2089 return NULL;
2090 }
2091 ++*var_count;
2092
2093 p = skipwhite(s);
2094 if (*p == ']')
2095 break;
2096 else if (*p == ';')
2097 {
2098 if (*semicolon == 1)
2099 {
2100 EMSG(_("Double ; in list of variables"));
2101 return NULL;
2102 }
2103 *semicolon = 1;
2104 }
2105 else if (*p != ',')
2106 {
2107 EMSG2(_(e_invarg2), p);
2108 return NULL;
2109 }
2110 }
2111 return p + 1;
2112 }
2113 else
2114 return skip_var_one(arg);
2115}
2116
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002118 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002119 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002122skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002123{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002124 if (*arg == '@' && arg[1] != NUL)
2125 return arg + 2;
2126 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2127 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002128}
2129
Bram Moolenaara7043832005-01-21 11:56:39 +00002130/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002131 * List variables for hashtab "ht" with prefix "prefix".
2132 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002133 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002135list_hashtable_vars(
2136 hashtab_T *ht,
2137 char_u *prefix,
2138 int empty,
2139 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002140{
Bram Moolenaar33570922005-01-25 22:26:29 +00002141 hashitem_T *hi;
2142 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002143 int todo;
2144
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002145 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002146 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2147 {
2148 if (!HASHITEM_EMPTY(hi))
2149 {
2150 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002151 di = HI2DI(hi);
2152 if (empty || di->di_tv.v_type != VAR_STRING
2153 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002155 }
2156 }
2157}
2158
2159/*
2160 * List global variables.
2161 */
2162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002163list_glob_vars(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 Moolenaar7454a062016-01-30 15:14:10 +01002172list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002173{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002174 char_u numbuf[NUMBUFLEN];
2175
Bram Moolenaar429fa852013-04-15 12:27:36 +02002176 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002178
2179 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2181 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002182}
2183
2184/*
2185 * List window variables.
2186 */
2187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002188list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002189{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002190 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002192}
2193
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002194#ifdef FEAT_WINDOWS
2195/*
2196 * List tab page variables.
2197 */
2198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002200{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002201 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203}
2204#endif
2205
Bram Moolenaara7043832005-01-21 11:56:39 +00002206/*
2207 * List Vim variables.
2208 */
2209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002212 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002213}
2214
2215/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002216 * List script-local variables, if there is a script.
2217 */
2218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002219list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220{
2221 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2223 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224}
2225
2226/*
2227 * List function variables, if there is a function.
2228 */
2229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002230list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002231{
2232 if (current_funccal != NULL)
2233 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002234 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002235}
2236
2237/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 * List variables in "arg".
2239 */
2240 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002241list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242{
2243 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 char_u *name_start;
2247 char_u *arg_subsc;
2248 char_u *tofree;
2249 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250
2251 while (!ends_excmd(*arg) && !got_int)
2252 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002255 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2257 {
2258 emsg_severe = TRUE;
2259 EMSG(_(e_trailing));
2260 break;
2261 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 /* get_name_len() takes care of expanding curly braces */
2266 name_start = name = arg;
2267 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2268 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 /* This is mainly to keep test 49 working: when expanding
2271 * curly braces fails overrule the exception error message. */
2272 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 emsg_severe = TRUE;
2275 EMSG2(_(e_invarg2), arg);
2276 break;
2277 }
2278 error = TRUE;
2279 }
2280 else
2281 {
2282 if (tofree != NULL)
2283 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002284 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 else
2287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 /* handle d.key, l[idx], f(expr) */
2289 arg_subsc = arg;
2290 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002298 case 'g': list_glob_vars(first); break;
2299 case 'b': list_buf_vars(first); break;
2300 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002303#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 case 'v': list_vim_vars(first); break;
2305 case 's': list_script_vars(first); break;
2306 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 default:
2308 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002310 }
2311 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 {
2313 char_u numbuf[NUMBUFLEN];
2314 char_u *tf;
2315 int c;
2316 char_u *s;
2317
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002318 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 c = *arg;
2320 *arg = NUL;
2321 list_one_var_a((char_u *)"",
2322 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002323 tv.v_type,
2324 s == NULL ? (char_u *)"" : s,
2325 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 *arg = c;
2327 vim_free(tf);
2328 }
2329 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 }
2332 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333
2334 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336
2337 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
2339
2340 return arg;
2341}
2342
2343/*
2344 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2345 * Returns a pointer to the char just after the var name.
2346 * Returns NULL if there is an error.
2347 */
2348 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002349ex_let_one(
2350 char_u *arg, /* points to variable name */
2351 typval_T *tv, /* value to assign to variable */
2352 int copy, /* copy value from "tv" */
2353 char_u *endchars, /* valid chars after variable name or NULL */
2354 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355{
2356 int c1;
2357 char_u *name;
2358 char_u *p;
2359 char_u *arg_end = NULL;
2360 int len;
2361 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002362 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002363
2364 /*
2365 * ":let $VAR = expr": Set environment variable.
2366 */
2367 if (*arg == '$')
2368 {
2369 /* Find the end of the name. */
2370 ++arg;
2371 name = arg;
2372 len = get_env_len(&arg);
2373 if (len == 0)
2374 EMSG2(_(e_invarg2), name - 1);
2375 else
2376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 if (op != NULL && (*op == '+' || *op == '-'))
2378 EMSG2(_(e_letwrong), op);
2379 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002382 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 {
2384 c1 = name[len];
2385 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002386 p = get_tv_string_chk(tv);
2387 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 {
2389 int mustfree = FALSE;
2390 char_u *s = vim_getenv(name, &mustfree);
2391
2392 if (s != NULL)
2393 {
2394 p = tofree = concat_str(s, p);
2395 if (mustfree)
2396 vim_free(s);
2397 }
2398 }
2399 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002402 if (STRICMP(name, "HOME") == 0)
2403 init_homedir();
2404 else if (didset_vim && STRICMP(name, "VIM") == 0)
2405 didset_vim = FALSE;
2406 else if (didset_vimruntime
2407 && STRICMP(name, "VIMRUNTIME") == 0)
2408 didset_vimruntime = FALSE;
2409 arg_end = arg;
2410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 }
2414 }
2415 }
2416
2417 /*
2418 * ":let &option = expr": Set option value.
2419 * ":let &l:option = expr": Set local option value.
2420 * ":let &g:option = expr": Set global option value.
2421 */
2422 else if (*arg == '&')
2423 {
2424 /* Find the end of the name. */
2425 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002426 if (p == NULL || (endchars != NULL
2427 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002428 EMSG(_(e_letunexp));
2429 else
2430 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002431 long n;
2432 int opt_type;
2433 long numval;
2434 char_u *stringval = NULL;
2435 char_u *s;
2436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 c1 = *p;
2438 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439
2440 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 s = get_tv_string_chk(tv); /* != NULL if number or string */
2442 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 {
2444 opt_type = get_option_value(arg, &numval,
2445 &stringval, opt_flags);
2446 if ((opt_type == 1 && *op == '.')
2447 || (opt_type == 0 && *op != '.'))
2448 EMSG2(_(e_letwrong), op);
2449 else
2450 {
2451 if (opt_type == 1) /* number */
2452 {
2453 if (*op == '+')
2454 n = numval + n;
2455 else
2456 n = numval - n;
2457 }
2458 else if (opt_type == 0 && stringval != NULL) /* string */
2459 {
2460 s = concat_str(stringval, s);
2461 vim_free(stringval);
2462 stringval = s;
2463 }
2464 }
2465 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002466 if (s != NULL)
2467 {
2468 set_option_value(arg, n, s, opt_flags);
2469 arg_end = p;
2470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002473 }
2474 }
2475
2476 /*
2477 * ":let @r = expr": Set register contents.
2478 */
2479 else if (*arg == '@')
2480 {
2481 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 if (op != NULL && (*op == '+' || *op == '-'))
2483 EMSG2(_(e_letwrong), op);
2484 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002485 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002486 EMSG(_(e_letunexp));
2487 else
2488 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 char_u *s;
2491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 p = get_tv_string_chk(tv);
2493 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002495 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 if (s != NULL)
2497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002498 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 vim_free(s);
2500 }
2501 }
2502 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002504 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 arg_end = arg + 1;
2506 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002508 }
2509 }
2510
2511 /*
2512 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002515 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002519 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2523 EMSG(_(e_letunexp));
2524 else
2525 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002526 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 arg_end = p;
2528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 }
2532
2533 else
2534 EMSG2(_(e_invarg2), arg);
2535
2536 return arg_end;
2537}
2538
2539/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2541 */
2542 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002543check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544{
2545 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2546 {
2547 EMSG2(_(e_readonlyvar), arg);
2548 return TRUE;
2549 }
2550 return FALSE;
2551}
2552
2553/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 * Get an lval: variable, Dict item or List item that can be assigned a value
2555 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2556 * "name.key", "name.key[expr]" etc.
2557 * Indexing only works if "name" is an existing List or Dictionary.
2558 * "name" points to the start of the name.
2559 * If "rettv" is not NULL it points to the value to be assigned.
2560 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2561 * wrong; must end in space or cmd separator.
2562 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002563 * flags:
2564 * GLV_QUIET: do not give error messages
2565 * GLV_NO_AUTOLOAD: do not use script autoloading
2566 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002568 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 */
2571 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002572get_lval(
2573 char_u *name,
2574 typval_T *rettv,
2575 lval_T *lp,
2576 int unlet,
2577 int skip,
2578 int flags, /* GLV_ values */
2579 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 char_u *expr_start, *expr_end;
2583 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002584 dictitem_T *v;
2585 typval_T var1;
2586 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002589 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002592 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596
2597 if (skip)
2598 {
2599 /* When skipping just find the end of the name. */
2600 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002601 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 }
2603
2604 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002605 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (expr_start != NULL)
2607 {
2608 /* Don't expand the name when we already know there is an error. */
2609 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2610 && *p != '[' && *p != '.')
2611 {
2612 EMSG(_(e_trailing));
2613 return NULL;
2614 }
2615
2616 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2617 if (lp->ll_exp_name == NULL)
2618 {
2619 /* Report an invalid expression in braces, unless the
2620 * expression evaluation has been cancelled due to an
2621 * aborting error, an interrupt, or an exception. */
2622 if (!aborting() && !quiet)
2623 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002624 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 EMSG2(_(e_invarg2), name);
2626 return NULL;
2627 }
2628 }
2629 lp->ll_name = lp->ll_exp_name;
2630 }
2631 else
2632 lp->ll_name = name;
2633
2634 /* Without [idx] or .key we are done. */
2635 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2636 return p;
2637
2638 cc = *p;
2639 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002640 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (v == NULL && !quiet)
2642 EMSG2(_(e_undefvar), lp->ll_name);
2643 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644 if (v == NULL)
2645 return NULL;
2646
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 /*
2648 * Loop until no more [idx] or .key is following.
2649 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002650 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2654 && !(lp->ll_tv->v_type == VAR_DICT
2655 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!quiet)
2658 EMSG(_("E689: Can only index a List or Dictionary"));
2659 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E708: [:] must come last"));
2665 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002667
Bram Moolenaar8c711452005-01-14 21:53:12 +00002668 len = -1;
2669 if (*p == '.')
2670 {
2671 key = p + 1;
2672 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2673 ;
2674 if (len == 0)
2675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
2677 EMSG(_(e_emptykey));
2678 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 }
2680 p = key + len;
2681 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002682 else
2683 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 if (*p == ':')
2687 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 else
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 empty1 = FALSE;
2691 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002693 if (get_tv_string_chk(&var1) == NULL)
2694 {
2695 /* not a number or string */
2696 clear_tv(&var1);
2697 return NULL;
2698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700
2701 /* Optionally get the second index [ :expr]. */
2702 if (*p == ':')
2703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002707 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708 if (!empty1)
2709 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (rettv != NULL && (rettv->v_type != VAR_LIST
2713 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (!quiet)
2716 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (!empty1)
2718 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 }
2721 p = skipwhite(p + 1);
2722 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 else
2725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (!empty1)
2730 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002733 if (get_tv_string_chk(&var2) == NULL)
2734 {
2735 /* not a number or string */
2736 if (!empty1)
2737 clear_tv(&var1);
2738 clear_tv(&var2);
2739 return NULL;
2740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 if (!quiet)
2750 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 if (!empty1)
2752 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 }
2757
2758 /* Skip to past ']'. */
2759 ++p;
2760 }
2761
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 {
2764 if (len == -1)
2765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002767 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (*key == NUL)
2769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 if (!quiet)
2771 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002776 lp->ll_list = NULL;
2777 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002778 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002780 /* When assigning to a scope dictionary check that a function and
2781 * variable name is valid (only variable name unless it is l: or
2782 * g: dictionary). Disallow overwriting a builtin function. */
2783 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002785 int prevval;
2786 int wrong;
2787
2788 if (len != -1)
2789 {
2790 prevval = key[len];
2791 key[len] = NUL;
2792 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002793 else
2794 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002795 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2796 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002797 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002798 || !valid_varname(key);
2799 if (len != -1)
2800 key[len] = prevval;
2801 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002802 return NULL;
2803 }
2804
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002807 /* Can't add "v:" variable. */
2808 if (lp->ll_dict == &vimvardict)
2809 {
2810 EMSG2(_(e_illvar), name);
2811 return NULL;
2812 }
2813
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002814 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002818 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 if (len == -1)
2820 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 }
2823 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 if (len == -1)
2828 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 p = NULL;
2831 break;
2832 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002834 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002835 return NULL;
2836
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 if (len == -1)
2838 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 }
2841 else
2842 {
2843 /*
2844 * Get the number and item for the only or first index of the List.
2845 */
2846 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 else
2849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002850 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 clear_tv(&var1);
2852 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002853 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 lp->ll_list = lp->ll_tv->vval.v_list;
2855 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2856 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002858 if (lp->ll_n1 < 0)
2859 {
2860 lp->ll_n1 = 0;
2861 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2862 }
2863 }
2864 if (lp->ll_li == NULL)
2865 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 if (!quiet)
2869 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 }
2872
2873 /*
2874 * May need to find the item or absolute index for the second
2875 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 * When no index given: "lp->ll_empty2" is TRUE.
2877 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002881 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002887 {
2888 if (!quiet)
2889 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002891 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 }
2894
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2896 if (lp->ll_n1 < 0)
2897 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2898 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002899 {
2900 if (!quiet)
2901 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002903 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002904 }
2905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002907 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002908 }
2909
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 return p;
2911}
2912
2913/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002914 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 */
2916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002917clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918{
2919 vim_free(lp->ll_exp_name);
2920 vim_free(lp->ll_newkey);
2921}
2922
2923/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002924 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 */
2928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002929set_var_lval(
2930 lval_T *lp,
2931 char_u *endp,
2932 typval_T *rettv,
2933 int copy,
2934 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935{
2936 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002937 listitem_T *ri;
2938 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939
2940 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 cc = *endp;
2945 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002946 if (op != NULL && *op != '=')
2947 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002948 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002950 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002951 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002952 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002953 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002955 if ((di == NULL
2956 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2957 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2958 FALSE)))
2959 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 set_var(lp->ll_name, &tv, FALSE);
2961 clear_tv(&tv);
2962 }
2963 }
2964 else
2965 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002967 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002969 else if (tv_check_lock(lp->ll_newkey == NULL
2970 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002971 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002972 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 else if (lp->ll_range)
2974 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002975 listitem_T *ll_li = lp->ll_li;
2976 int ll_n1 = lp->ll_n1;
2977
2978 /*
2979 * Check whether any of the list items is locked
2980 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002981 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002982 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002983 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002984 return;
2985 ri = ri->li_next;
2986 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2987 break;
2988 ll_li = ll_li->li_next;
2989 ++ll_n1;
2990 }
2991
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002992 /*
2993 * Assign the List values to the list items.
2994 */
2995 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002996 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 if (op != NULL && *op != '=')
2998 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2999 else
3000 {
3001 clear_tv(&lp->ll_li->li_tv);
3002 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 ri = ri->li_next;
3005 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3006 break;
3007 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003010 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 ri = NULL;
3013 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 lp->ll_li = lp->ll_li->li_next;
3017 ++lp->ll_n1;
3018 }
3019 if (ri != NULL)
3020 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003021 else if (lp->ll_empty2
3022 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 : lp->ll_n1 != lp->ll_n2)
3024 EMSG(_("E711: List value has not enough items"));
3025 }
3026 else
3027 {
3028 /*
3029 * Assign to a List or Dictionary item.
3030 */
3031 if (lp->ll_newkey != NULL)
3032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 if (op != NULL && *op != '=')
3034 {
3035 EMSG2(_(e_letwrong), op);
3036 return;
3037 }
3038
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003040 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 if (di == NULL)
3042 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003043 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3044 {
3045 vim_free(di);
3046 return;
3047 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003049 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003050 else if (op != NULL && *op != '=')
3051 {
3052 tv_op(lp->ll_tv, rettv, op);
3053 return;
3054 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003055 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003057
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 /*
3059 * Assign the value to the variable or list item.
3060 */
3061 if (copy)
3062 copy_tv(rettv, lp->ll_tv);
3063 else
3064 {
3065 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003066 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003068 }
3069 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070}
3071
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003072/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3074 * Returns OK or FAIL.
3075 */
3076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003077tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003078{
3079 long n;
3080 char_u numbuf[NUMBUFLEN];
3081 char_u *s;
3082
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003083 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3084 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3085 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 {
3087 switch (tv1->v_type)
3088 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003089 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 case VAR_DICT:
3091 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003092 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003093 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003094 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 break;
3096
3097 case VAR_LIST:
3098 if (*op != '+' || tv2->v_type != VAR_LIST)
3099 break;
3100 /* List += List */
3101 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3102 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3103 return OK;
3104
3105 case VAR_NUMBER:
3106 case VAR_STRING:
3107 if (tv2->v_type == VAR_LIST)
3108 break;
3109 if (*op == '+' || *op == '-')
3110 {
3111 /* nr += nr or nr -= nr*/
3112 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003113#ifdef FEAT_FLOAT
3114 if (tv2->v_type == VAR_FLOAT)
3115 {
3116 float_T f = n;
3117
3118 if (*op == '+')
3119 f += tv2->vval.v_float;
3120 else
3121 f -= tv2->vval.v_float;
3122 clear_tv(tv1);
3123 tv1->v_type = VAR_FLOAT;
3124 tv1->vval.v_float = f;
3125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003127#endif
3128 {
3129 if (*op == '+')
3130 n += get_tv_number(tv2);
3131 else
3132 n -= get_tv_number(tv2);
3133 clear_tv(tv1);
3134 tv1->v_type = VAR_NUMBER;
3135 tv1->vval.v_number = n;
3136 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 }
3138 else
3139 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003140 if (tv2->v_type == VAR_FLOAT)
3141 break;
3142
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 /* str .= str */
3144 s = get_tv_string(tv1);
3145 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3146 clear_tv(tv1);
3147 tv1->v_type = VAR_STRING;
3148 tv1->vval.v_string = s;
3149 }
3150 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151
3152#ifdef FEAT_FLOAT
3153 case VAR_FLOAT:
3154 {
3155 float_T f;
3156
3157 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3158 && tv2->v_type != VAR_NUMBER
3159 && tv2->v_type != VAR_STRING))
3160 break;
3161 if (tv2->v_type == VAR_FLOAT)
3162 f = tv2->vval.v_float;
3163 else
3164 f = get_tv_number(tv2);
3165 if (*op == '+')
3166 tv1->vval.v_float += f;
3167 else
3168 tv1->vval.v_float -= f;
3169 }
3170 return OK;
3171#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003172 }
3173 }
3174
3175 EMSG2(_(e_letwrong), op);
3176 return FAIL;
3177}
3178
3179/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180 * Add a watcher to a list.
3181 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003183list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184{
3185 lw->lw_next = l->lv_watch;
3186 l->lv_watch = lw;
3187}
3188
3189/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003190 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 * No warning when it isn't found...
3192 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003194list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195{
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197
3198 lwp = &l->lv_watch;
3199 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3200 {
3201 if (lw == lwrem)
3202 {
3203 *lwp = lw->lw_next;
3204 break;
3205 }
3206 lwp = &lw->lw_next;
3207 }
3208}
3209
3210/*
3211 * Just before removing an item from a list: advance watchers to the next
3212 * item.
3213 */
3214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003215list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216{
Bram Moolenaar33570922005-01-25 22:26:29 +00003217 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218
3219 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3220 if (lw->lw_item == item)
3221 lw->lw_item = item->li_next;
3222}
3223
3224/*
3225 * Evaluate the expression used in a ":for var in expr" command.
3226 * "arg" points to "var".
3227 * Set "*errp" to TRUE for an error, FALSE otherwise;
3228 * Return a pointer that holds the info. Null when there is an error.
3229 */
3230 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003231eval_for_line(
3232 char_u *arg,
3233 int *errp,
3234 char_u **nextcmdp,
3235 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236{
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 typval_T tv;
3240 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241
3242 *errp = TRUE; /* default: there is an error */
3243
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 if (fi == NULL)
3246 return NULL;
3247
3248 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3249 if (expr == NULL)
3250 return fi;
3251
3252 expr = skipwhite(expr);
3253 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3254 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003255 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 return fi;
3257 }
3258
3259 if (skip)
3260 ++emsg_skip;
3261 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3262 {
3263 *errp = FALSE;
3264 if (!skip)
3265 {
3266 l = tv.vval.v_list;
3267 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003270 clear_tv(&tv);
3271 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 else
3273 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003274 /* No need to increment the refcount, it's already set for the
3275 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 fi->fi_list = l;
3277 list_add_watch(l, &fi->fi_lw);
3278 fi->fi_lw.lw_item = l->lv_first;
3279 }
3280 }
3281 }
3282 if (skip)
3283 --emsg_skip;
3284
3285 return fi;
3286}
3287
3288/*
3289 * Use the first item in a ":for" list. Advance to the next.
3290 * Assign the values to the variable (list). "arg" points to the first one.
3291 * Return TRUE when a valid item was found, FALSE when at end of list or
3292 * something wrong.
3293 */
3294 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003295next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003297 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
3301 item = fi->fi_lw.lw_item;
3302 if (item == NULL)
3303 result = FALSE;
3304 else
3305 {
3306 fi->fi_lw.lw_item = item->li_next;
3307 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3308 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3309 }
3310 return result;
3311}
3312
3313/*
3314 * Free the structure used to store info used by ":for".
3315 */
3316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318{
Bram Moolenaar33570922005-01-25 22:26:29 +00003319 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003321 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003324 list_unref(fi->fi_list);
3325 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 vim_free(fi);
3327}
3328
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3330
3331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003332set_context_for_expression(
3333 expand_T *xp,
3334 char_u *arg,
3335 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336{
3337 int got_eq = FALSE;
3338 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 if (cmdidx == CMD_let)
3342 {
3343 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003344 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 {
3346 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003347 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 {
3349 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 if (vim_iswhite(*p))
3352 break;
3353 }
3354 return;
3355 }
3356 }
3357 else
3358 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3359 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 while ((xp->xp_pattern = vim_strpbrk(arg,
3361 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3362 {
3363 c = *xp->xp_pattern;
3364 if (c == '&')
3365 {
3366 c = xp->xp_pattern[1];
3367 if (c == '&')
3368 {
3369 ++xp->xp_pattern;
3370 xp->xp_context = cmdidx != CMD_let || got_eq
3371 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3372 }
3373 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003376 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3377 xp->xp_pattern += 2;
3378
3379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 else if (c == '$')
3382 {
3383 /* environment variable */
3384 xp->xp_context = EXPAND_ENV_VARS;
3385 }
3386 else if (c == '=')
3387 {
3388 got_eq = TRUE;
3389 xp->xp_context = EXPAND_EXPRESSION;
3390 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003391 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 && xp->xp_context == EXPAND_FUNCTIONS
3393 && vim_strchr(xp->xp_pattern, '(') == NULL)
3394 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003395 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 break;
3397 }
3398 else if (cmdidx != CMD_let || got_eq)
3399 {
3400 if (c == '"') /* string */
3401 {
3402 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3403 if (c == '\\' && xp->xp_pattern[1] != NUL)
3404 ++xp->xp_pattern;
3405 xp->xp_context = EXPAND_NOTHING;
3406 }
3407 else if (c == '\'') /* literal string */
3408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003409 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3411 /* skip */ ;
3412 xp->xp_context = EXPAND_NOTHING;
3413 }
3414 else if (c == '|')
3415 {
3416 if (xp->xp_pattern[1] == '|')
3417 {
3418 ++xp->xp_pattern;
3419 xp->xp_context = EXPAND_EXPRESSION;
3420 }
3421 else
3422 xp->xp_context = EXPAND_COMMANDS;
3423 }
3424 else
3425 xp->xp_context = EXPAND_EXPRESSION;
3426 }
3427 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003428 /* Doesn't look like something valid, expand as an expression
3429 * anyway. */
3430 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 arg = xp->xp_pattern;
3432 if (*arg != NUL)
3433 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3434 /* skip */ ;
3435 }
3436 xp->xp_pattern = arg;
3437}
3438
3439#endif /* FEAT_CMDL_COMPL */
3440
3441/*
3442 * ":1,25call func(arg1, arg2)" function call.
3443 */
3444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003445ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 char_u *arg = eap->arg;
3448 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003450 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003452 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 linenr_T lnum;
3454 int doesrange;
3455 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003458 if (eap->skip)
3459 {
3460 /* trans_function_name() doesn't work well when skipping, use eval0()
3461 * instead to skip to any following command, e.g. for:
3462 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003463 ++emsg_skip;
3464 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3465 clear_tv(&rettv);
3466 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003467 return;
3468 }
3469
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003470 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003471 if (fudi.fd_newkey != NULL)
3472 {
3473 /* Still need to give an error message for missing key. */
3474 EMSG2(_(e_dictkey), fudi.fd_newkey);
3475 vim_free(fudi.fd_newkey);
3476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003477 if (tofree == NULL)
3478 return;
3479
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003480 /* Increase refcount on dictionary, it could get deleted when evaluating
3481 * the arguments. */
3482 if (fudi.fd_dict != NULL)
3483 ++fudi.fd_dict->dv_refcount;
3484
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003485 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003486 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003487 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
Bram Moolenaar532c7802005-01-27 14:44:31 +00003489 /* Skip white space to allow ":call func ()". Not good, but required for
3490 * backward compatibility. */
3491 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003492 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 if (*startarg != '(')
3495 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003496 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 goto end;
3498 }
3499
3500 /*
3501 * When skipping, evaluate the function once, to find the end of the
3502 * arguments.
3503 * When the function takes a range, this is discovered after the first
3504 * call, and the loop is broken.
3505 */
3506 if (eap->skip)
3507 {
3508 ++emsg_skip;
3509 lnum = eap->line2; /* do it once, also with an invalid range */
3510 }
3511 else
3512 lnum = eap->line1;
3513 for ( ; lnum <= eap->line2; ++lnum)
3514 {
3515 if (!eap->skip && eap->addr_count > 0)
3516 {
3517 curwin->w_cursor.lnum = lnum;
3518 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003519#ifdef FEAT_VIRTUALEDIT
3520 curwin->w_cursor.coladd = 0;
3521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003524 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003525 eap->line1, eap->line2, &doesrange,
3526 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 failed = TRUE;
3529 break;
3530 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003531
3532 /* Handle a function returning a Funcref, Dictionary or List. */
3533 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3534 {
3535 failed = TRUE;
3536 break;
3537 }
3538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003539 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 if (doesrange || eap->skip)
3541 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003542
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003546 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (aborting())
3548 break;
3549 }
3550 if (eap->skip)
3551 --emsg_skip;
3552
3553 if (!failed)
3554 {
3555 /* Check for trailing illegal characters and a following command. */
3556 if (!ends_excmd(*arg))
3557 {
3558 emsg_severe = TRUE;
3559 EMSG(_(e_trailing));
3560 }
3561 else
3562 eap->nextcmd = check_nextcmd(arg);
3563 }
3564
3565end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003566 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003567 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568}
3569
3570/*
3571 * ":unlet[!] var1 ... " command.
3572 */
3573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003574ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003576 ex_unletlock(eap, eap->arg, 0);
3577}
3578
3579/*
3580 * ":lockvar" and ":unlockvar" commands
3581 */
3582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003583ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003586 int deep = 2;
3587
3588 if (eap->forceit)
3589 deep = -1;
3590 else if (vim_isdigit(*arg))
3591 {
3592 deep = getdigits(&arg);
3593 arg = skipwhite(arg);
3594 }
3595
3596 ex_unletlock(eap, arg, deep);
3597}
3598
3599/*
3600 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3601 */
3602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003603ex_unletlock(
3604 exarg_T *eap,
3605 char_u *argstart,
3606 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607{
3608 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613 do
3614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003616 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003617 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 if (lv.ll_name == NULL)
3619 error = TRUE; /* error but continue parsing */
3620 if (name_end == NULL || (!vim_iswhite(*name_end)
3621 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003623 if (name_end != NULL)
3624 {
3625 emsg_severe = TRUE;
3626 EMSG(_(e_trailing));
3627 }
3628 if (!(eap->skip || error))
3629 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 break;
3631 }
3632
3633 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634 {
3635 if (eap->cmdidx == CMD_unlet)
3636 {
3637 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3638 error = TRUE;
3639 }
3640 else
3641 {
3642 if (do_lock_var(&lv, name_end, deep,
3643 eap->cmdidx == CMD_lockvar) == FAIL)
3644 error = TRUE;
3645 }
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 if (!eap->skip)
3649 clear_lval(&lv);
3650
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 arg = skipwhite(name_end);
3652 } while (!ends_excmd(*arg));
3653
3654 eap->nextcmd = check_nextcmd(arg);
3655}
3656
Bram Moolenaar8c711452005-01-14 21:53:12 +00003657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003658do_unlet_var(
3659 lval_T *lp,
3660 char_u *name_end,
3661 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 int ret = OK;
3664 int cc;
3665
3666 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 cc = *name_end;
3669 *name_end = NUL;
3670
3671 /* Normal name or expanded name. */
3672 if (check_changedtick(lp->ll_name))
3673 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003677 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003678 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003679 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003680 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003681 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003682 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003683 else if (lp->ll_range)
3684 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003685 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003686 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003687 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003688
3689 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3690 {
3691 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003692 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003693 return FAIL;
3694 ll_li = li;
3695 ++ll_n1;
3696 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697
3698 /* Delete a range of List items. */
3699 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3700 {
3701 li = lp->ll_li->li_next;
3702 listitem_remove(lp->ll_list, lp->ll_li);
3703 lp->ll_li = li;
3704 ++lp->ll_n1;
3705 }
3706 }
3707 else
3708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 /* unlet a List item. */
3711 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003714 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 }
3716
3717 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003718}
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720/*
3721 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003722 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 */
3724 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003725do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
Bram Moolenaar33570922005-01-25 22:26:29 +00003727 hashtab_T *ht;
3728 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003729 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003730 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003731 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 ht = find_var_ht(name, &varname);
3734 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003736 if (ht == &globvarht)
3737 d = &globvardict;
3738 else if (current_funccal != NULL
3739 && ht == &current_funccal->l_vars.dv_hashtab)
3740 d = &current_funccal->l_vars;
3741 else if (ht == &compat_hashtab)
3742 d = &vimvardict;
3743 else
3744 {
3745 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3746 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3747 }
3748 if (d == NULL)
3749 {
3750 EMSG2(_(e_intern2), "do_unlet()");
3751 return FAIL;
3752 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003753 hi = hash_find(ht, varname);
3754 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003755 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003756 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003757 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003758 || var_check_ro(di->di_flags, name, FALSE)
3759 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003760 return FAIL;
3761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003762 delete_var(ht, hi);
3763 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766 if (forceit)
3767 return OK;
3768 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return FAIL;
3770}
3771
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003772/*
3773 * Lock or unlock variable indicated by "lp".
3774 * "deep" is the levels to go (-1 for unlimited);
3775 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3776 */
3777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003778do_lock_var(
3779 lval_T *lp,
3780 char_u *name_end,
3781 int deep,
3782 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003783{
3784 int ret = OK;
3785 int cc;
3786 dictitem_T *di;
3787
3788 if (deep == 0) /* nothing to do */
3789 return OK;
3790
3791 if (lp->ll_tv == NULL)
3792 {
3793 cc = *name_end;
3794 *name_end = NUL;
3795
3796 /* Normal name or expanded name. */
3797 if (check_changedtick(lp->ll_name))
3798 ret = FAIL;
3799 else
3800 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003801 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003802 if (di == NULL)
3803 ret = FAIL;
3804 else
3805 {
3806 if (lock)
3807 di->di_flags |= DI_FLAGS_LOCK;
3808 else
3809 di->di_flags &= ~DI_FLAGS_LOCK;
3810 item_lock(&di->di_tv, deep, lock);
3811 }
3812 }
3813 *name_end = cc;
3814 }
3815 else if (lp->ll_range)
3816 {
3817 listitem_T *li = lp->ll_li;
3818
3819 /* (un)lock a range of List items. */
3820 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3821 {
3822 item_lock(&li->li_tv, deep, lock);
3823 li = li->li_next;
3824 ++lp->ll_n1;
3825 }
3826 }
3827 else if (lp->ll_list != NULL)
3828 /* (un)lock a List item. */
3829 item_lock(&lp->ll_li->li_tv, deep, lock);
3830 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003831 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003832 item_lock(&lp->ll_di->di_tv, deep, lock);
3833
3834 return ret;
3835}
3836
3837/*
3838 * Lock or unlock an item. "deep" is nr of levels to go.
3839 */
3840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003841item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003842{
3843 static int recurse = 0;
3844 list_T *l;
3845 listitem_T *li;
3846 dict_T *d;
3847 hashitem_T *hi;
3848 int todo;
3849
3850 if (recurse >= DICT_MAXNEST)
3851 {
3852 EMSG(_("E743: variable nested too deep for (un)lock"));
3853 return;
3854 }
3855 if (deep == 0)
3856 return;
3857 ++recurse;
3858
3859 /* lock/unlock the item itself */
3860 if (lock)
3861 tv->v_lock |= VAR_LOCKED;
3862 else
3863 tv->v_lock &= ~VAR_LOCKED;
3864
3865 switch (tv->v_type)
3866 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003867 case VAR_UNKNOWN:
3868 case VAR_NUMBER:
3869 case VAR_STRING:
3870 case VAR_FUNC:
3871 case VAR_FLOAT:
3872 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003873 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003874 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003875 break;
3876
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003877 case VAR_LIST:
3878 if ((l = tv->vval.v_list) != NULL)
3879 {
3880 if (lock)
3881 l->lv_lock |= VAR_LOCKED;
3882 else
3883 l->lv_lock &= ~VAR_LOCKED;
3884 if (deep < 0 || deep > 1)
3885 /* recursive: lock/unlock the items the List contains */
3886 for (li = l->lv_first; li != NULL; li = li->li_next)
3887 item_lock(&li->li_tv, deep - 1, lock);
3888 }
3889 break;
3890 case VAR_DICT:
3891 if ((d = tv->vval.v_dict) != NULL)
3892 {
3893 if (lock)
3894 d->dv_lock |= VAR_LOCKED;
3895 else
3896 d->dv_lock &= ~VAR_LOCKED;
3897 if (deep < 0 || deep > 1)
3898 {
3899 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003900 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003901 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3902 {
3903 if (!HASHITEM_EMPTY(hi))
3904 {
3905 --todo;
3906 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3907 }
3908 }
3909 }
3910 }
3911 }
3912 --recurse;
3913}
3914
Bram Moolenaara40058a2005-07-11 22:42:07 +00003915/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003916 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3917 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003918 */
3919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003920tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003921{
3922 return (tv->v_lock & VAR_LOCKED)
3923 || (tv->v_type == VAR_LIST
3924 && tv->vval.v_list != NULL
3925 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3926 || (tv->v_type == VAR_DICT
3927 && tv->vval.v_dict != NULL
3928 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3929}
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3932/*
3933 * Delete all "menutrans_" variables.
3934 */
3935 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003936del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
Bram Moolenaar33570922005-01-25 22:26:29 +00003938 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003939 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003942 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003943 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 {
3945 if (!HASHITEM_EMPTY(hi))
3946 {
3947 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003948 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3949 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 }
3951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953}
3954#endif
3955
3956#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3957
3958/*
3959 * Local string buffer for the next two functions to store a variable name
3960 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3961 * get_user_var_name().
3962 */
3963
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003964static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965
3966static char_u *varnamebuf = NULL;
3967static int varnamebuflen = 0;
3968
3969/*
3970 * Function to concatenate a prefix and a variable name.
3971 */
3972 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974{
3975 int len;
3976
3977 len = (int)STRLEN(name) + 3;
3978 if (len > varnamebuflen)
3979 {
3980 vim_free(varnamebuf);
3981 len += 10; /* some additional space */
3982 varnamebuf = alloc(len);
3983 if (varnamebuf == NULL)
3984 {
3985 varnamebuflen = 0;
3986 return NULL;
3987 }
3988 varnamebuflen = len;
3989 }
3990 *varnamebuf = prefix;
3991 varnamebuf[1] = ':';
3992 STRCPY(varnamebuf + 2, name);
3993 return varnamebuf;
3994}
3995
3996/*
3997 * Function given to ExpandGeneric() to obtain the list of user defined
3998 * (global/buffer/window/built-in) variable names.
3999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004003 static long_u gdone;
4004 static long_u bdone;
4005 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004006#ifdef FEAT_WINDOWS
4007 static long_u tdone;
4008#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004009 static int vidx;
4010 static hashitem_T *hi;
4011 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004014 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004015 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004016#ifdef FEAT_WINDOWS
4017 tdone = 0;
4018#endif
4019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004020
4021 /* Global variables */
4022 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004025 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004026 else
4027 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 while (HASHITEM_EMPTY(hi))
4029 ++hi;
4030 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4031 return cat_prefix_varname('g', hi->hi_key);
4032 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004034
4035 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004036 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004037 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004039 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004041 else
4042 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 while (HASHITEM_EMPTY(hi))
4044 ++hi;
4045 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004049 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 return (char_u *)"b:changedtick";
4051 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004052
4053 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004054 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004057 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004059 else
4060 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 while (HASHITEM_EMPTY(hi))
4062 ++hi;
4063 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004065
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004066#ifdef FEAT_WINDOWS
4067 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004068 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069 if (tdone < ht->ht_used)
4070 {
4071 if (tdone++ == 0)
4072 hi = ht->ht_array;
4073 else
4074 ++hi;
4075 while (HASHITEM_EMPTY(hi))
4076 ++hi;
4077 return cat_prefix_varname('t', hi->hi_key);
4078 }
4079#endif
4080
Bram Moolenaar33570922005-01-25 22:26:29 +00004081 /* v: variables */
4082 if (vidx < VV_LEN)
4083 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 vim_free(varnamebuf);
4086 varnamebuf = NULL;
4087 varnamebuflen = 0;
4088 return NULL;
4089}
4090
4091#endif /* FEAT_CMDL_COMPL */
4092
4093/*
4094 * types for expressions.
4095 */
4096typedef enum
4097{
4098 TYPE_UNKNOWN = 0
4099 , TYPE_EQUAL /* == */
4100 , TYPE_NEQUAL /* != */
4101 , TYPE_GREATER /* > */
4102 , TYPE_GEQUAL /* >= */
4103 , TYPE_SMALLER /* < */
4104 , TYPE_SEQUAL /* <= */
4105 , TYPE_MATCH /* =~ */
4106 , TYPE_NOMATCH /* !~ */
4107} exptype_T;
4108
4109/*
4110 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4113 */
4114
4115/*
4116 * Handle zero level expression.
4117 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004119 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 * Return OK or FAIL.
4121 */
4122 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004123eval0(
4124 char_u *arg,
4125 typval_T *rettv,
4126 char_u **nextcmd,
4127 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129 int ret;
4130 char_u *p;
4131
4132 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 if (ret == FAIL || !ends_excmd(*p))
4135 {
4136 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 /*
4139 * Report the invalid expression unless the expression evaluation has
4140 * been cancelled due to an aborting error, an interrupt, or an
4141 * exception.
4142 */
4143 if (!aborting())
4144 EMSG2(_(e_invexpr2), arg);
4145 ret = FAIL;
4146 }
4147 if (nextcmd != NULL)
4148 *nextcmd = check_nextcmd(p);
4149
4150 return ret;
4151}
4152
4153/*
4154 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004155 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 *
4157 * "arg" must point to the first non-white of the expression.
4158 * "arg" is advanced to the next non-white after the recognized expression.
4159 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004160 * Note: "rettv.v_lock" is not set.
4161 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 * Return OK or FAIL.
4163 */
4164 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004165eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166{
4167 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004168 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
4170 /*
4171 * Get the first variable.
4172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return FAIL;
4175
4176 if ((*arg)[0] == '?')
4177 {
4178 result = FALSE;
4179 if (evaluate)
4180 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004181 int error = FALSE;
4182
4183 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004185 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 if (error)
4187 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189
4190 /*
4191 * Get the second variable.
4192 */
4193 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 return FAIL;
4196
4197 /*
4198 * Check for the ":".
4199 */
4200 if ((*arg)[0] != ':')
4201 {
4202 EMSG(_("E109: Missing ':' after '?'"));
4203 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 return FAIL;
4206 }
4207
4208 /*
4209 * Get the third variable.
4210 */
4211 *arg = skipwhite(*arg + 1);
4212 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4213 {
4214 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return FAIL;
4217 }
4218 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 }
4221
4222 return OK;
4223}
4224
4225/*
4226 * Handle first level expression:
4227 * expr2 || expr2 || expr2 logical OR
4228 *
4229 * "arg" must point to the first non-white of the expression.
4230 * "arg" is advanced to the next non-white after the recognized expression.
4231 *
4232 * Return OK or FAIL.
4233 */
4234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004235eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
Bram Moolenaar33570922005-01-25 22:26:29 +00004237 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 long result;
4239 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004240 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 /*
4243 * Get the first variable.
4244 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 return FAIL;
4247
4248 /*
4249 * Repeat until there is no following "||".
4250 */
4251 first = TRUE;
4252 result = FALSE;
4253 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4254 {
4255 if (evaluate && first)
4256 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004257 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (error)
4261 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 first = FALSE;
4263 }
4264
4265 /*
4266 * Get the second variable.
4267 */
4268 *arg = skipwhite(*arg + 2);
4269 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4270 return FAIL;
4271
4272 /*
4273 * Compute the result.
4274 */
4275 if (evaluate && !result)
4276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (error)
4281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 if (evaluate)
4284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004285 rettv->v_type = VAR_NUMBER;
4286 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 }
4289
4290 return OK;
4291}
4292
4293/*
4294 * Handle second level expression:
4295 * expr3 && expr3 && expr3 logical AND
4296 *
4297 * "arg" must point to the first non-white of the expression.
4298 * "arg" is advanced to the next non-white after the recognized expression.
4299 *
4300 * Return OK or FAIL.
4301 */
4302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004303eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
Bram Moolenaar33570922005-01-25 22:26:29 +00004305 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 long result;
4307 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004308 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 /*
4311 * Get the first variable.
4312 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004313 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 return FAIL;
4315
4316 /*
4317 * Repeat until there is no following "&&".
4318 */
4319 first = TRUE;
4320 result = TRUE;
4321 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4322 {
4323 if (evaluate && first)
4324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004325 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004327 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004328 if (error)
4329 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 first = FALSE;
4331 }
4332
4333 /*
4334 * Get the second variable.
4335 */
4336 *arg = skipwhite(*arg + 2);
4337 if (eval4(arg, &var2, evaluate && result) == FAIL)
4338 return FAIL;
4339
4340 /*
4341 * Compute the result.
4342 */
4343 if (evaluate && result)
4344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004345 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004347 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (error)
4349 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
4351 if (evaluate)
4352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004353 rettv->v_type = VAR_NUMBER;
4354 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 }
4356 }
4357
4358 return OK;
4359}
4360
4361/*
4362 * Handle third level expression:
4363 * var1 == var2
4364 * var1 =~ var2
4365 * var1 != var2
4366 * var1 !~ var2
4367 * var1 > var2
4368 * var1 >= var2
4369 * var1 < var2
4370 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004371 * var1 is var2
4372 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 *
4374 * "arg" must point to the first non-white of the expression.
4375 * "arg" is advanced to the next non-white after the recognized expression.
4376 *
4377 * Return OK or FAIL.
4378 */
4379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004380eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
Bram Moolenaar33570922005-01-25 22:26:29 +00004382 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 char_u *p;
4384 int i;
4385 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 int len = 2;
4388 long n1, n2;
4389 char_u *s1, *s2;
4390 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4391 regmatch_T regmatch;
4392 int ic;
4393 char_u *save_cpo;
4394
4395 /*
4396 * Get the first variable.
4397 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400
4401 p = *arg;
4402 switch (p[0])
4403 {
4404 case '=': if (p[1] == '=')
4405 type = TYPE_EQUAL;
4406 else if (p[1] == '~')
4407 type = TYPE_MATCH;
4408 break;
4409 case '!': if (p[1] == '=')
4410 type = TYPE_NEQUAL;
4411 else if (p[1] == '~')
4412 type = TYPE_NOMATCH;
4413 break;
4414 case '>': if (p[1] != '=')
4415 {
4416 type = TYPE_GREATER;
4417 len = 1;
4418 }
4419 else
4420 type = TYPE_GEQUAL;
4421 break;
4422 case '<': if (p[1] != '=')
4423 {
4424 type = TYPE_SMALLER;
4425 len = 1;
4426 }
4427 else
4428 type = TYPE_SEQUAL;
4429 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004430 case 'i': if (p[1] == 's')
4431 {
4432 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4433 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004434 i = p[len];
4435 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004436 {
4437 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4438 type_is = TRUE;
4439 }
4440 }
4441 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443
4444 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004445 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 */
4447 if (type != TYPE_UNKNOWN)
4448 {
4449 /* extra question mark appended: ignore case */
4450 if (p[len] == '?')
4451 {
4452 ic = TRUE;
4453 ++len;
4454 }
4455 /* extra '#' appended: match case */
4456 else if (p[len] == '#')
4457 {
4458 ic = FALSE;
4459 ++len;
4460 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004461 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 else
4463 ic = p_ic;
4464
4465 /*
4466 * Get the second variable.
4467 */
4468 *arg = skipwhite(p + len);
4469 if (eval5(arg, &var2, evaluate) == FAIL)
4470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004471 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 return FAIL;
4473 }
4474
4475 if (evaluate)
4476 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 if (type_is && rettv->v_type != var2.v_type)
4478 {
4479 /* For "is" a different type always means FALSE, for "notis"
4480 * it means TRUE. */
4481 n1 = (type == TYPE_NEQUAL);
4482 }
4483 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4484 {
4485 if (type_is)
4486 {
4487 n1 = (rettv->v_type == var2.v_type
4488 && rettv->vval.v_list == var2.vval.v_list);
4489 if (type == TYPE_NEQUAL)
4490 n1 = !n1;
4491 }
4492 else if (rettv->v_type != var2.v_type
4493 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4494 {
4495 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004496 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004498 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004499 clear_tv(rettv);
4500 clear_tv(&var2);
4501 return FAIL;
4502 }
4503 else
4504 {
4505 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004506 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4507 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 if (type == TYPE_NEQUAL)
4509 n1 = !n1;
4510 }
4511 }
4512
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004513 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4514 {
4515 if (type_is)
4516 {
4517 n1 = (rettv->v_type == var2.v_type
4518 && rettv->vval.v_dict == var2.vval.v_dict);
4519 if (type == TYPE_NEQUAL)
4520 n1 = !n1;
4521 }
4522 else if (rettv->v_type != var2.v_type
4523 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4524 {
4525 if (rettv->v_type != var2.v_type)
4526 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4527 else
4528 EMSG(_("E736: Invalid operation for Dictionary"));
4529 clear_tv(rettv);
4530 clear_tv(&var2);
4531 return FAIL;
4532 }
4533 else
4534 {
4535 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004536 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4537 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004538 if (type == TYPE_NEQUAL)
4539 n1 = !n1;
4540 }
4541 }
4542
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004543 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4544 {
4545 if (rettv->v_type != var2.v_type
4546 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4547 {
4548 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004549 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004551 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 clear_tv(rettv);
4553 clear_tv(&var2);
4554 return FAIL;
4555 }
4556 else
4557 {
4558 /* Compare two Funcrefs for being equal or unequal. */
4559 if (rettv->vval.v_string == NULL
4560 || var2.vval.v_string == NULL)
4561 n1 = FALSE;
4562 else
4563 n1 = STRCMP(rettv->vval.v_string,
4564 var2.vval.v_string) == 0;
4565 if (type == TYPE_NEQUAL)
4566 n1 = !n1;
4567 }
4568 }
4569
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004570#ifdef FEAT_FLOAT
4571 /*
4572 * If one of the two variables is a float, compare as a float.
4573 * When using "=~" or "!~", always compare as string.
4574 */
4575 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4576 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4577 {
4578 float_T f1, f2;
4579
4580 if (rettv->v_type == VAR_FLOAT)
4581 f1 = rettv->vval.v_float;
4582 else
4583 f1 = get_tv_number(rettv);
4584 if (var2.v_type == VAR_FLOAT)
4585 f2 = var2.vval.v_float;
4586 else
4587 f2 = get_tv_number(&var2);
4588 n1 = FALSE;
4589 switch (type)
4590 {
4591 case TYPE_EQUAL: n1 = (f1 == f2); break;
4592 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4593 case TYPE_GREATER: n1 = (f1 > f2); break;
4594 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4595 case TYPE_SMALLER: n1 = (f1 < f2); break;
4596 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4597 case TYPE_UNKNOWN:
4598 case TYPE_MATCH:
4599 case TYPE_NOMATCH: break; /* avoid gcc warning */
4600 }
4601 }
4602#endif
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 /*
4605 * If one of the two variables is a number, compare as a number.
4606 * When using "=~" or "!~", always compare as string.
4607 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004608 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 n1 = get_tv_number(rettv);
4612 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 switch (type)
4614 {
4615 case TYPE_EQUAL: n1 = (n1 == n2); break;
4616 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4617 case TYPE_GREATER: n1 = (n1 > n2); break;
4618 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4619 case TYPE_SMALLER: n1 = (n1 < n2); break;
4620 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4621 case TYPE_UNKNOWN:
4622 case TYPE_MATCH:
4623 case TYPE_NOMATCH: break; /* avoid gcc warning */
4624 }
4625 }
4626 else
4627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004628 s1 = get_tv_string_buf(rettv, buf1);
4629 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4631 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4632 else
4633 i = 0;
4634 n1 = FALSE;
4635 switch (type)
4636 {
4637 case TYPE_EQUAL: n1 = (i == 0); break;
4638 case TYPE_NEQUAL: n1 = (i != 0); break;
4639 case TYPE_GREATER: n1 = (i > 0); break;
4640 case TYPE_GEQUAL: n1 = (i >= 0); break;
4641 case TYPE_SMALLER: n1 = (i < 0); break;
4642 case TYPE_SEQUAL: n1 = (i <= 0); break;
4643
4644 case TYPE_MATCH:
4645 case TYPE_NOMATCH:
4646 /* avoid 'l' flag in 'cpoptions' */
4647 save_cpo = p_cpo;
4648 p_cpo = (char_u *)"";
4649 regmatch.regprog = vim_regcomp(s2,
4650 RE_MAGIC + RE_STRING);
4651 regmatch.rm_ic = ic;
4652 if (regmatch.regprog != NULL)
4653 {
4654 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004655 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (type == TYPE_NOMATCH)
4657 n1 = !n1;
4658 }
4659 p_cpo = save_cpo;
4660 break;
4661
4662 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4663 }
4664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004665 clear_tv(rettv);
4666 clear_tv(&var2);
4667 rettv->v_type = VAR_NUMBER;
4668 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
4670 }
4671
4672 return OK;
4673}
4674
4675/*
4676 * Handle fourth level expression:
4677 * + number addition
4678 * - number subtraction
4679 * . string concatenation
4680 *
4681 * "arg" must point to the first non-white of the expression.
4682 * "arg" is advanced to the next non-white after the recognized expression.
4683 *
4684 * Return OK or FAIL.
4685 */
4686 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004687eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
Bram Moolenaar33570922005-01-25 22:26:29 +00004689 typval_T var2;
4690 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 int op;
4692 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004693#ifdef FEAT_FLOAT
4694 float_T f1 = 0, f2 = 0;
4695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 char_u *s1, *s2;
4697 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4698 char_u *p;
4699
4700 /*
4701 * Get the first variable.
4702 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004703 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return FAIL;
4705
4706 /*
4707 * Repeat computing, until no '+', '-' or '.' is following.
4708 */
4709 for (;;)
4710 {
4711 op = **arg;
4712 if (op != '+' && op != '-' && op != '.')
4713 break;
4714
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715 if ((op != '+' || rettv->v_type != VAR_LIST)
4716#ifdef FEAT_FLOAT
4717 && (op == '.' || rettv->v_type != VAR_FLOAT)
4718#endif
4719 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 {
4721 /* For "list + ...", an illegal use of the first operand as
4722 * a number cannot be determined before evaluating the 2nd
4723 * operand: if this is also a list, all is ok.
4724 * For "something . ...", "something - ..." or "non-list + ...",
4725 * we know that the first operand needs to be a string or number
4726 * without evaluating the 2nd operand. So check before to avoid
4727 * side effects after an error. */
4728 if (evaluate && get_tv_string_chk(rettv) == NULL)
4729 {
4730 clear_tv(rettv);
4731 return FAIL;
4732 }
4733 }
4734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 /*
4736 * Get the second variable.
4737 */
4738 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004739 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004741 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 return FAIL;
4743 }
4744
4745 if (evaluate)
4746 {
4747 /*
4748 * Compute the result.
4749 */
4750 if (op == '.')
4751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004752 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4753 s2 = get_tv_string_buf_chk(&var2, buf2);
4754 if (s2 == NULL) /* type error ? */
4755 {
4756 clear_tv(rettv);
4757 clear_tv(&var2);
4758 return FAIL;
4759 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004760 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 clear_tv(rettv);
4762 rettv->v_type = VAR_STRING;
4763 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004765 else if (op == '+' && rettv->v_type == VAR_LIST
4766 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004767 {
4768 /* concatenate Lists */
4769 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4770 &var3) == FAIL)
4771 {
4772 clear_tv(rettv);
4773 clear_tv(&var2);
4774 return FAIL;
4775 }
4776 clear_tv(rettv);
4777 *rettv = var3;
4778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 else
4780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 int error = FALSE;
4782
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004783#ifdef FEAT_FLOAT
4784 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786 f1 = rettv->vval.v_float;
4787 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 else
4790#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 n1 = get_tv_number_chk(rettv, &error);
4793 if (error)
4794 {
4795 /* This can only happen for "list + non-list". For
4796 * "non-list + ..." or "something - ...", we returned
4797 * before evaluating the 2nd operand. */
4798 clear_tv(rettv);
4799 return FAIL;
4800 }
4801#ifdef FEAT_FLOAT
4802 if (var2.v_type == VAR_FLOAT)
4803 f1 = n1;
4804#endif
4805 }
4806#ifdef FEAT_FLOAT
4807 if (var2.v_type == VAR_FLOAT)
4808 {
4809 f2 = var2.vval.v_float;
4810 n2 = 0;
4811 }
4812 else
4813#endif
4814 {
4815 n2 = get_tv_number_chk(&var2, &error);
4816 if (error)
4817 {
4818 clear_tv(rettv);
4819 clear_tv(&var2);
4820 return FAIL;
4821 }
4822#ifdef FEAT_FLOAT
4823 if (rettv->v_type == VAR_FLOAT)
4824 f2 = n2;
4825#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828
4829#ifdef FEAT_FLOAT
4830 /* If there is a float on either side the result is a float. */
4831 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4832 {
4833 if (op == '+')
4834 f1 = f1 + f2;
4835 else
4836 f1 = f1 - f2;
4837 rettv->v_type = VAR_FLOAT;
4838 rettv->vval.v_float = f1;
4839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004841#endif
4842 {
4843 if (op == '+')
4844 n1 = n1 + n2;
4845 else
4846 n1 = n1 - n2;
4847 rettv->v_type = VAR_NUMBER;
4848 rettv->vval.v_number = n1;
4849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004851 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 }
4854 return OK;
4855}
4856
4857/*
4858 * Handle fifth level expression:
4859 * * number multiplication
4860 * / number division
4861 * % number modulo
4862 *
4863 * "arg" must point to the first non-white of the expression.
4864 * "arg" is advanced to the next non-white after the recognized expression.
4865 *
4866 * Return OK or FAIL.
4867 */
4868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004869eval6(
4870 char_u **arg,
4871 typval_T *rettv,
4872 int evaluate,
4873 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874{
Bram Moolenaar33570922005-01-25 22:26:29 +00004875 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 int op;
4877 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004878#ifdef FEAT_FLOAT
4879 int use_float = FALSE;
4880 float_T f1 = 0, f2;
4881#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004882 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 /*
4885 * Get the first variable.
4886 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004887 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 return FAIL;
4889
4890 /*
4891 * Repeat computing, until no '*', '/' or '%' is following.
4892 */
4893 for (;;)
4894 {
4895 op = **arg;
4896 if (op != '*' && op != '/' && op != '%')
4897 break;
4898
4899 if (evaluate)
4900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#ifdef FEAT_FLOAT
4902 if (rettv->v_type == VAR_FLOAT)
4903 {
4904 f1 = rettv->vval.v_float;
4905 use_float = TRUE;
4906 n1 = 0;
4907 }
4908 else
4909#endif
4910 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004911 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912 if (error)
4913 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 else
4916 n1 = 0;
4917
4918 /*
4919 * Get the second variable.
4920 */
4921 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004922 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
4924
4925 if (evaluate)
4926 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927#ifdef FEAT_FLOAT
4928 if (var2.v_type == VAR_FLOAT)
4929 {
4930 if (!use_float)
4931 {
4932 f1 = n1;
4933 use_float = TRUE;
4934 }
4935 f2 = var2.vval.v_float;
4936 n2 = 0;
4937 }
4938 else
4939#endif
4940 {
4941 n2 = get_tv_number_chk(&var2, &error);
4942 clear_tv(&var2);
4943 if (error)
4944 return FAIL;
4945#ifdef FEAT_FLOAT
4946 if (use_float)
4947 f2 = n2;
4948#endif
4949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 /*
4952 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 if (op == '*')
4959 f1 = f1 * f2;
4960 else if (op == '/')
4961 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004962# ifdef VMS
4963 /* VMS crashes on divide by zero, work around it */
4964 if (f2 == 0.0)
4965 {
4966 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 }
4973 else
4974 f1 = f1 / f2;
4975# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004976 /* We rely on the floating point library to handle divide
4977 * by zero to result in "inf" and not a crash. */
4978 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004983 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 return FAIL;
4985 }
4986 rettv->v_type = VAR_FLOAT;
4987 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 if (op == '*')
4993 n1 = n1 * n2;
4994 else if (op == '/')
4995 {
4996 if (n2 == 0) /* give an error message? */
4997 {
4998 if (n1 == 0)
4999 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5000 else if (n1 < 0)
5001 n1 = -0x7fffffffL;
5002 else
5003 n1 = 0x7fffffffL;
5004 }
5005 else
5006 n1 = n1 / n2;
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 {
5010 if (n2 == 0) /* give an error message? */
5011 n1 = 0;
5012 else
5013 n1 = n1 % n2;
5014 }
5015 rettv->v_type = VAR_NUMBER;
5016 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 }
5020
5021 return OK;
5022}
5023
5024/*
5025 * Handle sixth level expression:
5026 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005027 * "string" string constant
5028 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 * &option-name option value
5030 * @r register contents
5031 * identifier variable value
5032 * function() function call
5033 * $VAR environment variable
5034 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005035 * [expr, expr] List
5036 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 *
5038 * Also handle:
5039 * ! in front logical NOT
5040 * - in front unary minus
5041 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 * trailing [] subscript in String or List
5043 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 *
5045 * "arg" must point to the first non-white of the expression.
5046 * "arg" is advanced to the next non-white after the recognized expression.
5047 *
5048 * Return OK or FAIL.
5049 */
5050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005051eval7(
5052 char_u **arg,
5053 typval_T *rettv,
5054 int evaluate,
5055 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 long n;
5058 int len;
5059 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 char_u *start_leader, *end_leader;
5061 int ret = OK;
5062 char_u *alias;
5063
5064 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005065 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005066 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
5070 /*
5071 * Skip '!' and '-' characters. They are handled later.
5072 */
5073 start_leader = *arg;
5074 while (**arg == '!' || **arg == '-' || **arg == '+')
5075 *arg = skipwhite(*arg + 1);
5076 end_leader = *arg;
5077
5078 switch (**arg)
5079 {
5080 /*
5081 * Number constant.
5082 */
5083 case '0':
5084 case '1':
5085 case '2':
5086 case '3':
5087 case '4':
5088 case '5':
5089 case '6':
5090 case '7':
5091 case '8':
5092 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 {
5094#ifdef FEAT_FLOAT
5095 char_u *p = skipdigits(*arg + 1);
5096 int get_float = FALSE;
5097
5098 /* We accept a float when the format matches
5099 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005100 * strict to avoid backwards compatibility problems.
5101 * Don't look for a float after the "." operator, so that
5102 * ":let vers = 1.2.3" doesn't fail. */
5103 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 get_float = TRUE;
5106 p = skipdigits(p + 2);
5107 if (*p == 'e' || *p == 'E')
5108 {
5109 ++p;
5110 if (*p == '-' || *p == '+')
5111 ++p;
5112 if (!vim_isdigit(*p))
5113 get_float = FALSE;
5114 else
5115 p = skipdigits(p + 1);
5116 }
5117 if (ASCII_ISALPHA(*p) || *p == '.')
5118 get_float = FALSE;
5119 }
5120 if (get_float)
5121 {
5122 float_T f;
5123
5124 *arg += string2float(*arg, &f);
5125 if (evaluate)
5126 {
5127 rettv->v_type = VAR_FLOAT;
5128 rettv->vval.v_float = f;
5129 }
5130 }
5131 else
5132#endif
5133 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005134 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005135 *arg += len;
5136 if (evaluate)
5137 {
5138 rettv->v_type = VAR_NUMBER;
5139 rettv->vval.v_number = n;
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /*
5146 * String constant: "string".
5147 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005148 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 break;
5150
5151 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005152 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005155 break;
5156
5157 /*
5158 * List: [expr, expr]
5159 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 break;
5162
5163 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 * Dictionary: {key: val, key: val}
5165 */
5166 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5167 break;
5168
5169 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005172 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 break;
5174
5175 /*
5176 * Environment variable: $VAR.
5177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 break;
5180
5181 /*
5182 * Register contents: @r.
5183 */
5184 case '@': ++*arg;
5185 if (evaluate)
5186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005188 rettv->vval.v_string = get_reg_contents(**arg,
5189 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 if (**arg != NUL)
5192 ++*arg;
5193 break;
5194
5195 /*
5196 * nested expression: (expression).
5197 */
5198 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (**arg == ')')
5201 ++*arg;
5202 else if (ret == OK)
5203 {
5204 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005205 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 ret = FAIL;
5207 }
5208 break;
5209
Bram Moolenaar8c711452005-01-14 21:53:12 +00005210 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 break;
5212 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213
5214 if (ret == NOTDONE)
5215 {
5216 /*
5217 * Must be a variable or function name.
5218 * Can also be a curly-braces kind of name: {expr}.
5219 */
5220 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005221 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222 if (alias != NULL)
5223 s = alias;
5224
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005225 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 ret = FAIL;
5227 else
5228 {
5229 if (**arg == '(') /* recursive! */
5230 {
5231 /* If "s" is the name of a variable of type VAR_FUNC
5232 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005233 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234
5235 /* Invoke the function. */
5236 ret = get_func_tv(s, len, rettv, arg,
5237 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005238 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005239
5240 /* If evaluate is FALSE rettv->v_type was not set in
5241 * get_func_tv, but it's needed in handle_subscript() to parse
5242 * what follows. So set it here. */
5243 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5244 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005245 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005246 rettv->v_type = VAR_FUNC;
5247 }
5248
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 /* Stop the expression evaluation when immediately
5250 * aborting on error, or when an interrupt occurred or
5251 * an exception was thrown but not caught. */
5252 if (aborting())
5253 {
5254 if (ret == OK)
5255 clear_tv(rettv);
5256 ret = FAIL;
5257 }
5258 }
5259 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005260 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005261 else
5262 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005264 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 }
5266
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 *arg = skipwhite(*arg);
5268
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5270 * expr(expr). */
5271 if (ret == OK)
5272 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273
5274 /*
5275 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5276 */
5277 if (ret == OK && evaluate && end_leader > start_leader)
5278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005280 int val = 0;
5281#ifdef FEAT_FLOAT
5282 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284 if (rettv->v_type == VAR_FLOAT)
5285 f = rettv->vval.v_float;
5286 else
5287#endif
5288 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 clear_tv(rettv);
5292 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 else
5295 {
5296 while (end_leader > start_leader)
5297 {
5298 --end_leader;
5299 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005300 {
5301#ifdef FEAT_FLOAT
5302 if (rettv->v_type == VAR_FLOAT)
5303 f = !f;
5304 else
5305#endif
5306 val = !val;
5307 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005308 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309 {
5310#ifdef FEAT_FLOAT
5311 if (rettv->v_type == VAR_FLOAT)
5312 f = -f;
5313 else
5314#endif
5315 val = -val;
5316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318#ifdef FEAT_FLOAT
5319 if (rettv->v_type == VAR_FLOAT)
5320 {
5321 clear_tv(rettv);
5322 rettv->vval.v_float = f;
5323 }
5324 else
5325#endif
5326 {
5327 clear_tv(rettv);
5328 rettv->v_type = VAR_NUMBER;
5329 rettv->vval.v_number = val;
5330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333
5334 return ret;
5335}
5336
5337/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005338 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5339 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5341 */
5342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005343eval_index(
5344 char_u **arg,
5345 typval_T *rettv,
5346 int evaluate,
5347 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348{
5349 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005352 long len = -1;
5353 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356
Bram Moolenaara03f2332016-02-06 18:09:59 +01005357 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005359 case VAR_FUNC:
5360 if (verbose)
5361 EMSG(_("E695: Cannot index a Funcref"));
5362 return FAIL;
5363 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005364#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005365 if (verbose)
5366 EMSG(_(e_float_as_string));
5367 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005368#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005369 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005370 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005371 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005372 if (verbose)
5373 EMSG(_("E909: Cannot index a special variable"));
5374 return FAIL;
5375 case VAR_UNKNOWN:
5376 if (evaluate)
5377 return FAIL;
5378 /* FALLTHROUGH */
5379
5380 case VAR_STRING:
5381 case VAR_NUMBER:
5382 case VAR_LIST:
5383 case VAR_DICT:
5384 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005385 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005387 init_tv(&var1);
5388 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005391 /*
5392 * dict.name
5393 */
5394 key = *arg + 1;
5395 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5396 ;
5397 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 }
5401 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 /*
5404 * something[idx]
5405 *
5406 * Get the (first) variable from inside the [].
5407 */
5408 *arg = skipwhite(*arg + 1);
5409 if (**arg == ':')
5410 empty1 = TRUE;
5411 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5412 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005413 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5414 {
5415 /* not a number or string */
5416 clear_tv(&var1);
5417 return FAIL;
5418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419
5420 /*
5421 * Get the second variable from inside the [:].
5422 */
5423 if (**arg == ':')
5424 {
5425 range = TRUE;
5426 *arg = skipwhite(*arg + 1);
5427 if (**arg == ']')
5428 empty2 = TRUE;
5429 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005431 if (!empty1)
5432 clear_tv(&var1);
5433 return FAIL;
5434 }
5435 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5436 {
5437 /* not a number or string */
5438 if (!empty1)
5439 clear_tv(&var1);
5440 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 return FAIL;
5442 }
5443 }
5444
5445 /* Check for the ']'. */
5446 if (**arg != ']')
5447 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005448 if (verbose)
5449 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 clear_tv(&var1);
5451 if (range)
5452 clear_tv(&var2);
5453 return FAIL;
5454 }
5455 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457
5458 if (evaluate)
5459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 n1 = 0;
5461 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005463 n1 = get_tv_number(&var1);
5464 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 }
5466 if (range)
5467 {
5468 if (empty2)
5469 n2 = -1;
5470 else
5471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 n2 = get_tv_number(&var2);
5473 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475 }
5476
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005479 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005480 case VAR_FUNC:
5481 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005482 case VAR_SPECIAL:
5483 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005484 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005485 break; /* not evaluating, skipping over subscript */
5486
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 case VAR_NUMBER:
5488 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 len = (long)STRLEN(s);
5491 if (range)
5492 {
5493 /* The resulting variable is a substring. If the indexes
5494 * are out of range the result is empty. */
5495 if (n1 < 0)
5496 {
5497 n1 = len + n1;
5498 if (n1 < 0)
5499 n1 = 0;
5500 }
5501 if (n2 < 0)
5502 n2 = len + n2;
5503 else if (n2 >= len)
5504 n2 = len;
5505 if (n1 >= len || n2 < 0 || n1 > n2)
5506 s = NULL;
5507 else
5508 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5509 }
5510 else
5511 {
5512 /* The resulting variable is a string of a single
5513 * character. If the index is too big or negative the
5514 * result is empty. */
5515 if (n1 >= len || n1 < 0)
5516 s = NULL;
5517 else
5518 s = vim_strnsave(s + n1, 1);
5519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520 clear_tv(rettv);
5521 rettv->v_type = VAR_STRING;
5522 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 break;
5524
5525 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 if (n1 < 0)
5528 n1 = len + n1;
5529 if (!empty1 && (n1 < 0 || n1 >= len))
5530 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005531 /* For a range we allow invalid values and return an empty
5532 * list. A list index out of range is an error. */
5533 if (!range)
5534 {
5535 if (verbose)
5536 EMSGN(_(e_listidx), n1);
5537 return FAIL;
5538 }
5539 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540 }
5541 if (range)
5542 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005543 list_T *l;
5544 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545
5546 if (n2 < 0)
5547 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005548 else if (n2 >= len)
5549 n2 = len - 1;
5550 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005551 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 l = list_alloc();
5553 if (l == NULL)
5554 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 n1 <= n2; ++n1)
5557 {
5558 if (list_append_tv(l, &item->li_tv) == FAIL)
5559 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005560 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 return FAIL;
5562 }
5563 item = item->li_next;
5564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005565 clear_tv(rettv);
5566 rettv->v_type = VAR_LIST;
5567 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005568 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 }
5570 else
5571 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005572 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 clear_tv(rettv);
5574 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575 }
5576 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005577
5578 case VAR_DICT:
5579 if (range)
5580 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005581 if (verbose)
5582 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005583 if (len == -1)
5584 clear_tv(&var1);
5585 return FAIL;
5586 }
5587 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005588 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005589
5590 if (len == -1)
5591 {
5592 key = get_tv_string(&var1);
5593 if (*key == NUL)
5594 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005595 if (verbose)
5596 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 clear_tv(&var1);
5598 return FAIL;
5599 }
5600 }
5601
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005602 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005604 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005605 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 if (len == -1)
5607 clear_tv(&var1);
5608 if (item == NULL)
5609 return FAIL;
5610
5611 copy_tv(&item->di_tv, &var1);
5612 clear_tv(rettv);
5613 *rettv = var1;
5614 }
5615 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005616 }
5617 }
5618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 return OK;
5620}
5621
5622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 * Get an option value.
5624 * "arg" points to the '&' or '+' before the option name.
5625 * "arg" is advanced to character after the option name.
5626 * Return OK or FAIL.
5627 */
5628 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005629get_option_tv(
5630 char_u **arg,
5631 typval_T *rettv, /* when NULL, only check if option exists */
5632 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 char_u *option_end;
5635 long numval;
5636 char_u *stringval;
5637 int opt_type;
5638 int c;
5639 int working = (**arg == '+'); /* has("+option") */
5640 int ret = OK;
5641 int opt_flags;
5642
5643 /*
5644 * Isolate the option name and find its value.
5645 */
5646 option_end = find_option_end(arg, &opt_flags);
5647 if (option_end == NULL)
5648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 EMSG2(_("E112: Option name missing: %s"), *arg);
5651 return FAIL;
5652 }
5653
5654 if (!evaluate)
5655 {
5656 *arg = option_end;
5657 return OK;
5658 }
5659
5660 c = *option_end;
5661 *option_end = NUL;
5662 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
5665 if (opt_type == -3) /* invalid name */
5666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 EMSG2(_("E113: Unknown option: %s"), *arg);
5669 ret = FAIL;
5670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
5673 if (opt_type == -2) /* hidden string option */
5674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 rettv->v_type = VAR_STRING;
5676 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 else if (opt_type == -1) /* hidden number option */
5679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 rettv->v_type = VAR_NUMBER;
5681 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683 else if (opt_type == 1) /* number option */
5684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685 rettv->v_type = VAR_NUMBER;
5686 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 else /* string option */
5689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 rettv->v_type = VAR_STRING;
5691 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693 }
5694 else if (working && (opt_type == -2 || opt_type == -1))
5695 ret = FAIL;
5696
5697 *option_end = c; /* put back for error messages */
5698 *arg = option_end;
5699
5700 return ret;
5701}
5702
5703/*
5704 * Allocate a variable for a string constant.
5705 * Return OK or FAIL.
5706 */
5707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005708get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710 char_u *p;
5711 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 int extra = 0;
5713
5714 /*
5715 * Find the end of the string, skipping backslashed characters.
5716 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005717 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 if (*p == '\\' && p[1] != NUL)
5720 {
5721 ++p;
5722 /* A "\<x>" form occupies at least 4 characters, and produces up
5723 * to 6 characters: reserve space for 2 extra */
5724 if (*p == '<')
5725 extra += 2;
5726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
5728
5729 if (*p != '"')
5730 {
5731 EMSG2(_("E114: Missing quote: %s"), *arg);
5732 return FAIL;
5733 }
5734
5735 /* If only parsing, set *arg and return here */
5736 if (!evaluate)
5737 {
5738 *arg = p + 1;
5739 return OK;
5740 }
5741
5742 /*
5743 * Copy the string into allocated memory, handling backslashed
5744 * characters.
5745 */
5746 name = alloc((unsigned)(p - *arg + extra));
5747 if (name == NULL)
5748 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005749 rettv->v_type = VAR_STRING;
5750 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 if (*p == '\\')
5755 {
5756 switch (*++p)
5757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 case 'b': *name++ = BS; ++p; break;
5759 case 'e': *name++ = ESC; ++p; break;
5760 case 'f': *name++ = FF; ++p; break;
5761 case 'n': *name++ = NL; ++p; break;
5762 case 'r': *name++ = CAR; ++p; break;
5763 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765 case 'X': /* hex: "\x1", "\x12" */
5766 case 'x':
5767 case 'u': /* Unicode: "\u0023" */
5768 case 'U':
5769 if (vim_isxdigit(p[1]))
5770 {
5771 int n, nr;
5772 int c = toupper(*p);
5773
5774 if (c == 'X')
5775 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005776 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005778 else
5779 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 nr = 0;
5781 while (--n >= 0 && vim_isxdigit(p[1]))
5782 {
5783 ++p;
5784 nr = (nr << 4) + hex2nr(*p);
5785 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787#ifdef FEAT_MBYTE
5788 /* For "\u" store the number according to
5789 * 'encoding'. */
5790 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 else
5793#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 break;
5797
5798 /* octal: "\1", "\12", "\123" */
5799 case '0':
5800 case '1':
5801 case '2':
5802 case '3':
5803 case '4':
5804 case '5':
5805 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005806 case '7': *name = *p++ - '0';
5807 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 *name = (*name << 3) + *p++ - '0';
5810 if (*p >= '0' && *p <= '7')
5811 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 break;
5815
5816 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 if (extra != 0)
5819 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 break;
5822 }
5823 /* FALLTHROUGH */
5824
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 break;
5827 }
5828 }
5829 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 *arg = p + 1;
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 return OK;
5837}
5838
5839/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 * Return OK or FAIL.
5842 */
5843 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005844get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
5846 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005847 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 int reduce = 0;
5849
5850 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 */
5853 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 break;
5859 ++reduce;
5860 ++p;
5861 }
5862 }
5863
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 return FAIL;
5868 }
5869
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 if (!evaluate)
5872 {
5873 *arg = p + 1;
5874 return OK;
5875 }
5876
5877 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005878 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 */
5880 str = alloc((unsigned)((p - *arg) - reduce));
5881 if (str == NULL)
5882 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 rettv->v_type = VAR_STRING;
5884 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 break;
5892 ++p;
5893 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 *arg = p + 1;
5898
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 return OK;
5900}
5901
5902/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005903 * Allocate a variable for a List and fill it from "*arg".
5904 * Return OK or FAIL.
5905 */
5906 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005907get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908{
Bram Moolenaar33570922005-01-25 22:26:29 +00005909 list_T *l = NULL;
5910 typval_T tv;
5911 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912
5913 if (evaluate)
5914 {
5915 l = list_alloc();
5916 if (l == NULL)
5917 return FAIL;
5918 }
5919
5920 *arg = skipwhite(*arg + 1);
5921 while (**arg != ']' && **arg != NUL)
5922 {
5923 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5924 goto failret;
5925 if (evaluate)
5926 {
5927 item = listitem_alloc();
5928 if (item != NULL)
5929 {
5930 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005931 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 list_append(l, item);
5933 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005934 else
5935 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 }
5937
5938 if (**arg == ']')
5939 break;
5940 if (**arg != ',')
5941 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 goto failret;
5944 }
5945 *arg = skipwhite(*arg + 1);
5946 }
5947
5948 if (**arg != ']')
5949 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005950 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951failret:
5952 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005953 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 return FAIL;
5955 }
5956
5957 *arg = skipwhite(*arg + 1);
5958 if (evaluate)
5959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005960 rettv->v_type = VAR_LIST;
5961 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 ++l->lv_refcount;
5963 }
5964
5965 return OK;
5966}
5967
5968/*
5969 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005970 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005972 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005973list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005975 list_T *l;
5976
5977 l = (list_T *)alloc_clear(sizeof(list_T));
5978 if (l != NULL)
5979 {
5980 /* Prepend the list to the list of lists for garbage collection. */
5981 if (first_list != NULL)
5982 first_list->lv_used_prev = l;
5983 l->lv_used_prev = NULL;
5984 l->lv_used_next = first_list;
5985 first_list = l;
5986 }
5987 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988}
5989
5990/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005991 * Allocate an empty list for a return value.
5992 * Returns OK or FAIL.
5993 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005995rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005996{
5997 list_T *l = list_alloc();
5998
5999 if (l == NULL)
6000 return FAIL;
6001
6002 rettv->vval.v_list = l;
6003 rettv->v_type = VAR_LIST;
6004 ++l->lv_refcount;
6005 return OK;
6006}
6007
6008/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 * Unreference a list: decrement the reference count and free it when it
6010 * becomes zero.
6011 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006013list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006015 if (l != NULL && --l->lv_refcount <= 0)
6016 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017}
6018
6019/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006020 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 * Ignores the reference count.
6022 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006024list_free(
6025 list_T *l,
6026 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027{
Bram Moolenaar33570922005-01-25 22:26:29 +00006028 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006029
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006030 /* Remove the list from the list of lists for garbage collection. */
6031 if (l->lv_used_prev == NULL)
6032 first_list = l->lv_used_next;
6033 else
6034 l->lv_used_prev->lv_used_next = l->lv_used_next;
6035 if (l->lv_used_next != NULL)
6036 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6037
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006040 /* Remove the item before deleting it. */
6041 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006042 if (recurse || (item->li_tv.v_type != VAR_LIST
6043 && item->li_tv.v_type != VAR_DICT))
6044 clear_tv(&item->li_tv);
6045 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 }
6047 vim_free(l);
6048}
6049
6050/*
6051 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006052 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006054 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006055listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058}
6059
6060/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006061 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006064listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006066 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 vim_free(item);
6068}
6069
6070/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006071 * Remove a list item from a List and free it. Also clears the value.
6072 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006074listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006076 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006077 listitem_free(item);
6078}
6079
6080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 * Get the number of items in a list.
6082 */
6083 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006084list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 if (l == NULL)
6087 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006088 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089}
6090
6091/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092 * Return TRUE when two lists have exactly the same values.
6093 */
6094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095list_equal(
6096 list_T *l1,
6097 list_T *l2,
6098 int ic, /* ignore case for strings */
6099 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100{
Bram Moolenaar33570922005-01-25 22:26:29 +00006101 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006103 if (l1 == NULL || l2 == NULL)
6104 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006105 if (l1 == l2)
6106 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006107 if (list_len(l1) != list_len(l2))
6108 return FALSE;
6109
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 for (item1 = l1->lv_first, item2 = l2->lv_first;
6111 item1 != NULL && item2 != NULL;
6112 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006113 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 return FALSE;
6115 return item1 == NULL && item2 == NULL;
6116}
6117
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006118/*
6119 * Return the dictitem that an entry in a hashtable points to.
6120 */
6121 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006122dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006123{
6124 return HI2DI(hi);
6125}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006126
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006127/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006128 * Return TRUE when two dictionaries have exactly the same key/values.
6129 */
6130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006131dict_equal(
6132 dict_T *d1,
6133 dict_T *d2,
6134 int ic, /* ignore case for strings */
6135 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136{
Bram Moolenaar33570922005-01-25 22:26:29 +00006137 hashitem_T *hi;
6138 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006139 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006140
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006141 if (d1 == NULL || d2 == NULL)
6142 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006143 if (d1 == d2)
6144 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145 if (dict_len(d1) != dict_len(d2))
6146 return FALSE;
6147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006148 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006150 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006151 if (!HASHITEM_EMPTY(hi))
6152 {
6153 item2 = dict_find(d2, hi->hi_key, -1);
6154 if (item2 == NULL)
6155 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006156 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157 return FALSE;
6158 --todo;
6159 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006160 }
6161 return TRUE;
6162}
6163
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006164static int tv_equal_recurse_limit;
6165
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006166/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006168 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006169 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 */
6171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006172tv_equal(
6173 typval_T *tv1,
6174 typval_T *tv2,
6175 int ic, /* ignore case */
6176 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006177{
6178 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006179 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006180 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006181 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006183 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006184 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006186 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187 * recursiveness to a limit. We guess they are equal then.
6188 * A fixed limit has the problem of still taking an awful long time.
6189 * Reduce the limit every time running into it. That should work fine for
6190 * deeply linked structures that are not recursively linked and catch
6191 * recursiveness quickly. */
6192 if (!recursive)
6193 tv_equal_recurse_limit = 1000;
6194 if (recursive_cnt >= tv_equal_recurse_limit)
6195 {
6196 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006198 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199
6200 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006201 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203 ++recursive_cnt;
6204 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6205 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006206 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006207
6208 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006209 ++recursive_cnt;
6210 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6211 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006212 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213
6214 case VAR_FUNC:
6215 return (tv1->vval.v_string != NULL
6216 && tv2->vval.v_string != NULL
6217 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6218
6219 case VAR_NUMBER:
6220 return tv1->vval.v_number == tv2->vval.v_number;
6221
6222 case VAR_STRING:
6223 s1 = get_tv_string_buf(tv1, buf1);
6224 s2 = get_tv_string_buf(tv2, buf2);
6225 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006226
6227 case VAR_SPECIAL:
6228 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006229
6230 case VAR_FLOAT:
6231#ifdef FEAT_FLOAT
6232 return tv1->vval.v_float == tv2->vval.v_float;
6233#endif
6234 case VAR_JOB:
6235#ifdef FEAT_JOB
6236 return tv1->vval.v_job == tv2->vval.v_job;
6237#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006238 case VAR_CHANNEL:
6239#ifdef FEAT_CHANNEL
6240 return tv1->vval.v_channel == tv2->vval.v_channel;
6241#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006242 case VAR_UNKNOWN:
6243 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006244 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006245
Bram Moolenaara03f2332016-02-06 18:09:59 +01006246 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6247 * does not equal anything, not even itself. */
6248 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249}
6250
6251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 * Locate item with index "n" in list "l" and return it.
6253 * A negative index is counted from the end; -1 is the last item.
6254 * Returns NULL when "n" is out of range.
6255 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006256 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006257list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258{
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006260 long idx;
6261
6262 if (l == NULL)
6263 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006264
6265 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006266 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267 n = l->lv_len + n;
6268
6269 /* Check for index out of range. */
6270 if (n < 0 || n >= l->lv_len)
6271 return NULL;
6272
6273 /* When there is a cached index may start search from there. */
6274 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006276 if (n < l->lv_idx / 2)
6277 {
6278 /* closest to the start of the list */
6279 item = l->lv_first;
6280 idx = 0;
6281 }
6282 else if (n > (l->lv_idx + l->lv_len) / 2)
6283 {
6284 /* closest to the end of the list */
6285 item = l->lv_last;
6286 idx = l->lv_len - 1;
6287 }
6288 else
6289 {
6290 /* closest to the cached index */
6291 item = l->lv_idx_item;
6292 idx = l->lv_idx;
6293 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294 }
6295 else
6296 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006297 if (n < l->lv_len / 2)
6298 {
6299 /* closest to the start of the list */
6300 item = l->lv_first;
6301 idx = 0;
6302 }
6303 else
6304 {
6305 /* closest to the end of the list */
6306 item = l->lv_last;
6307 idx = l->lv_len - 1;
6308 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310
6311 while (n > idx)
6312 {
6313 /* search forward */
6314 item = item->li_next;
6315 ++idx;
6316 }
6317 while (n < idx)
6318 {
6319 /* search backward */
6320 item = item->li_prev;
6321 --idx;
6322 }
6323
6324 /* cache the used index */
6325 l->lv_idx = idx;
6326 l->lv_idx_item = item;
6327
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006328 return item;
6329}
6330
6331/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006332 * Get list item "l[idx]" as a number.
6333 */
6334 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006335list_find_nr(
6336 list_T *l,
6337 long idx,
6338 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006339{
6340 listitem_T *li;
6341
6342 li = list_find(l, idx);
6343 if (li == NULL)
6344 {
6345 if (errorp != NULL)
6346 *errorp = TRUE;
6347 return -1L;
6348 }
6349 return get_tv_number_chk(&li->li_tv, errorp);
6350}
6351
6352/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006353 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6354 */
6355 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006356list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006357{
6358 listitem_T *li;
6359
6360 li = list_find(l, idx - 1);
6361 if (li == NULL)
6362 {
6363 EMSGN(_(e_listidx), idx);
6364 return NULL;
6365 }
6366 return get_tv_string(&li->li_tv);
6367}
6368
6369/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006370 * Locate "item" list "l" and return its index.
6371 * Returns -1 when "item" is not in the list.
6372 */
6373 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006374list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006375{
6376 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378
6379 if (l == NULL)
6380 return -1;
6381 idx = 0;
6382 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6383 ++idx;
6384 if (li == NULL)
6385 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006386 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006387}
6388
6389/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006390 * Append item "item" to the end of list "l".
6391 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006393list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394{
6395 if (l->lv_last == NULL)
6396 {
6397 /* empty list */
6398 l->lv_first = item;
6399 l->lv_last = item;
6400 item->li_prev = NULL;
6401 }
6402 else
6403 {
6404 l->lv_last->li_next = item;
6405 item->li_prev = l->lv_last;
6406 l->lv_last = item;
6407 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006408 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409 item->li_next = NULL;
6410}
6411
6412/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006415 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006417list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 copy_tv(tv, &li->li_tv);
6424 list_append(l, li);
6425 return OK;
6426}
6427
6428/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006429 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 * Return FAIL when out of memory.
6431 */
6432 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006433list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434{
6435 listitem_T *li = listitem_alloc();
6436
6437 if (li == NULL)
6438 return FAIL;
6439 li->li_tv.v_type = VAR_DICT;
6440 li->li_tv.v_lock = 0;
6441 li->li_tv.vval.v_dict = dict;
6442 list_append(list, li);
6443 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006444 return OK;
6445}
6446
6447/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006449 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006450 * Returns FAIL when out of memory.
6451 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006453list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006454{
6455 listitem_T *li = listitem_alloc();
6456
6457 if (li == NULL)
6458 return FAIL;
6459 list_append(l, li);
6460 li->li_tv.v_type = VAR_STRING;
6461 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006462 if (str == NULL)
6463 li->li_tv.vval.v_string = NULL;
6464 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006465 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006466 return FAIL;
6467 return OK;
6468}
6469
6470/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006471 * Append "n" to list "l".
6472 * Returns FAIL when out of memory.
6473 */
6474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006475list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006476{
6477 listitem_T *li;
6478
6479 li = listitem_alloc();
6480 if (li == NULL)
6481 return FAIL;
6482 li->li_tv.v_type = VAR_NUMBER;
6483 li->li_tv.v_lock = 0;
6484 li->li_tv.vval.v_number = n;
6485 list_append(l, li);
6486 return OK;
6487}
6488
6489/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006490 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006491 * If "item" is NULL append at the end.
6492 * Return FAIL when out of memory.
6493 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496{
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498
6499 if (ni == NULL)
6500 return FAIL;
6501 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006502 list_insert(l, ni, item);
6503 return OK;
6504}
6505
6506 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006507list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006508{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509 if (item == NULL)
6510 /* Append new item at end of list. */
6511 list_append(l, ni);
6512 else
6513 {
6514 /* Insert new item before existing item. */
6515 ni->li_prev = item->li_prev;
6516 ni->li_next = item;
6517 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006518 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006520 ++l->lv_idx;
6521 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006525 l->lv_idx_item = NULL;
6526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530}
6531
6532/*
6533 * Extend "l1" with "l2".
6534 * If "bef" is NULL append at the end, otherwise insert before this item.
6535 * Returns FAIL when out of memory.
6536 */
6537 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539{
Bram Moolenaar33570922005-01-25 22:26:29 +00006540 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006541 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006543 /* We also quit the loop when we have inserted the original item count of
6544 * the list, avoid a hang when we extend a list with itself. */
6545 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6547 return FAIL;
6548 return OK;
6549}
6550
6551/*
6552 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6553 * Return FAIL when out of memory.
6554 */
6555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006556list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557{
Bram Moolenaar33570922005-01-25 22:26:29 +00006558 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006560 if (l1 == NULL || l2 == NULL)
6561 return FAIL;
6562
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006564 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 if (l == NULL)
6566 return FAIL;
6567 tv->v_type = VAR_LIST;
6568 tv->vval.v_list = l;
6569
6570 /* append all items from the second list */
6571 return list_extend(l, l2, NULL);
6572}
6573
6574/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006575 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006577 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578 * Returns NULL when out of memory.
6579 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006580 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006581list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582{
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 list_T *copy;
6584 listitem_T *item;
6585 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586
6587 if (orig == NULL)
6588 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589
6590 copy = list_alloc();
6591 if (copy != NULL)
6592 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006593 if (copyID != 0)
6594 {
6595 /* Do this before adding the items, because one of the items may
6596 * refer back to this list. */
6597 orig->lv_copyID = copyID;
6598 orig->lv_copylist = copy;
6599 }
6600 for (item = orig->lv_first; item != NULL && !got_int;
6601 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602 {
6603 ni = listitem_alloc();
6604 if (ni == NULL)
6605 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006606 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 {
6608 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6609 {
6610 vim_free(ni);
6611 break;
6612 }
6613 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006615 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006616 list_append(copy, ni);
6617 }
6618 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006619 if (item != NULL)
6620 {
6621 list_unref(copy);
6622 copy = NULL;
6623 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 }
6625
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626 return copy;
6627}
6628
6629/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006630 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006631 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006632 * This used to be called list_remove, but that conflicts with a Sun header
6633 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006636vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006637{
Bram Moolenaar33570922005-01-25 22:26:29 +00006638 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006640 /* notify watchers */
6641 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006643 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 list_fix_watch(l, ip);
6645 if (ip == item2)
6646 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648
6649 if (item2->li_next == NULL)
6650 l->lv_last = item->li_prev;
6651 else
6652 item2->li_next->li_prev = item->li_prev;
6653 if (item->li_prev == NULL)
6654 l->lv_first = item2->li_next;
6655 else
6656 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006657 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658}
6659
6660/*
6661 * Return an allocated string with the string representation of a list.
6662 * May return NULL.
6663 */
6664 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006665list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666{
6667 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668
6669 if (tv->vval.v_list == NULL)
6670 return NULL;
6671 ga_init2(&ga, (int)sizeof(char), 80);
6672 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006673 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006674 {
6675 vim_free(ga.ga_data);
6676 return NULL;
6677 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 ga_append(&ga, ']');
6679 ga_append(&ga, NUL);
6680 return (char_u *)ga.ga_data;
6681}
6682
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006683typedef struct join_S {
6684 char_u *s;
6685 char_u *tofree;
6686} join_T;
6687
6688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006689list_join_inner(
6690 garray_T *gap, /* to store the result in */
6691 list_T *l,
6692 char_u *sep,
6693 int echo_style,
6694 int copyID,
6695 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006696{
6697 int i;
6698 join_T *p;
6699 int len;
6700 int sumlen = 0;
6701 int first = TRUE;
6702 char_u *tofree;
6703 char_u numbuf[NUMBUFLEN];
6704 listitem_T *item;
6705 char_u *s;
6706
6707 /* Stringify each item in the list. */
6708 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6709 {
6710 if (echo_style)
6711 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6712 else
6713 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6714 if (s == NULL)
6715 return FAIL;
6716
6717 len = (int)STRLEN(s);
6718 sumlen += len;
6719
Bram Moolenaarcde88542015-08-11 19:14:00 +02006720 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006721 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6722 if (tofree != NULL || s != numbuf)
6723 {
6724 p->s = s;
6725 p->tofree = tofree;
6726 }
6727 else
6728 {
6729 p->s = vim_strnsave(s, len);
6730 p->tofree = p->s;
6731 }
6732
6733 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006734 if (did_echo_string_emsg) /* recursion error, bail out */
6735 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006736 }
6737
6738 /* Allocate result buffer with its total size, avoid re-allocation and
6739 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6740 if (join_gap->ga_len >= 2)
6741 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6742 if (ga_grow(gap, sumlen + 2) == FAIL)
6743 return FAIL;
6744
6745 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6746 {
6747 if (first)
6748 first = FALSE;
6749 else
6750 ga_concat(gap, sep);
6751 p = ((join_T *)join_gap->ga_data) + i;
6752
6753 if (p->s != NULL)
6754 ga_concat(gap, p->s);
6755 line_breakcheck();
6756 }
6757
6758 return OK;
6759}
6760
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006761/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006763 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006764 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006765 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006766 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006767list_join(
6768 garray_T *gap,
6769 list_T *l,
6770 char_u *sep,
6771 int echo_style,
6772 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006774 garray_T join_ga;
6775 int retval;
6776 join_T *p;
6777 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006778
Bram Moolenaard39a7512015-04-16 22:51:22 +02006779 if (l->lv_len < 1)
6780 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006781 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6782 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6783
6784 /* Dispose each item in join_ga. */
6785 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006786 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006787 p = (join_T *)join_ga.ga_data;
6788 for (i = 0; i < join_ga.ga_len; ++i)
6789 {
6790 vim_free(p->tofree);
6791 ++p;
6792 }
6793 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006794 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006795
6796 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797}
6798
6799/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006800 * Return the next (unique) copy ID.
6801 * Used for serializing nested structures.
6802 */
6803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006804get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006805{
6806 current_copyID += COPYID_INC;
6807 return current_copyID;
6808}
6809
6810/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006811 * Garbage collection for lists and dictionaries.
6812 *
6813 * We use reference counts to be able to free most items right away when they
6814 * are no longer used. But for composite items it's possible that it becomes
6815 * unused while the reference count is > 0: When there is a recursive
6816 * reference. Example:
6817 * :let l = [1, 2, 3]
6818 * :let d = {9: l}
6819 * :let l[1] = d
6820 *
6821 * Since this is quite unusual we handle this with garbage collection: every
6822 * once in a while find out which lists and dicts are not referenced from any
6823 * variable.
6824 *
6825 * Here is a good reference text about garbage collection (refers to Python
6826 * but it applies to all reference-counting mechanisms):
6827 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006828 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829
6830/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006831 * Do garbage collection for lists and dicts.
6832 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006835garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006837 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006838 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006839 buf_T *buf;
6840 win_T *wp;
6841 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006842 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006843 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006844 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006845#ifdef FEAT_WINDOWS
6846 tabpage_T *tp;
6847#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006849 /* Only do this once. */
6850 want_garbage_collect = FALSE;
6851 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006852 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006853
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 /* We advance by two because we add one for items referenced through
6855 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006856 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006858 /*
6859 * 1. Go through all accessible variables and mark all lists and dicts
6860 * with copyID.
6861 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006862
6863 /* Don't free variables in the previous_funccal list unless they are only
6864 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006865 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6867 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006868 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6869 NULL);
6870 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6871 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872 }
6873
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 /* script-local variables */
6875 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006876 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877
6878 /* buffer-local variables */
6879 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006880 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6881 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882
6883 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006884 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6886 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006887#ifdef FEAT_AUTOCMD
6888 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006891#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006893#ifdef FEAT_WINDOWS
6894 /* tabpage-local variables */
6895 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6897 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006898#endif
6899
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902
6903 /* function-local variables */
6904 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6905 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6907 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006908 }
6909
Bram Moolenaard812df62008-11-09 12:46:09 +00006910 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006911 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006912
Bram Moolenaar1dced572012-04-05 16:54:08 +02006913#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006915#endif
6916
Bram Moolenaardb913952012-06-29 12:54:53 +02006917#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006919#endif
6920
6921#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006923#endif
6924
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006925#ifdef FEAT_CHANNEL
6926 abort = abort || set_ref_in_channel(copyID);
6927#endif
6928
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006930 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 /*
6932 * 2. Free lists and dictionaries that are not referenced.
6933 */
6934 did_free = free_unref_items(copyID);
6935
6936 /*
6937 * 3. Check if any funccal can be freed now.
6938 */
6939 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006940 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006941 if (can_free_funccal(*pfc, copyID))
6942 {
6943 fc = *pfc;
6944 *pfc = fc->caller;
6945 free_funccal(fc, TRUE);
6946 did_free = TRUE;
6947 did_free_funccal = TRUE;
6948 }
6949 else
6950 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 if (did_free_funccal)
6953 /* When a funccal was freed some more items might be garbage
6954 * collected, so run again. */
6955 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006956 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 else if (p_verbose > 0)
6958 {
6959 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6960 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961
6962 return did_free;
6963}
6964
6965/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006966 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 */
6968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006969free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006971 dict_T *dd, *dd_next;
6972 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 int did_free = FALSE;
6974
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977 */
6978 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006979 {
6980 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006983 /* Free the Dictionary and ordinary items it contains, but don't
6984 * recurse into Lists and Dictionaries, they will be in the list
6985 * of dicts or list of lists. */
6986 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006989 dd = dd_next;
6990 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991
6992 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006993 * Go through the list of lists and free items without the copyID.
6994 * But don't free a list that has a watcher (used in a for loop), these
6995 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 */
6997 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006998 {
6999 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007000 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7001 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007003 /* Free the List and ordinary items it contains, but don't recurse
7004 * into Lists and Dictionaries, they will be in the list of dicts
7005 * or list of lists. */
7006 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007009 ll = ll_next;
7010 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007011
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 return did_free;
7013}
7014
7015/*
7016 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007017 * "list_stack" is used to add lists to be marked. Can be NULL.
7018 *
7019 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007022set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023{
7024 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 hashtab_T *cur_ht;
7028 ht_stack_T *ht_stack = NULL;
7029 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 cur_ht = ht;
7032 for (;;)
7033 {
7034 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 /* Mark each item in the hashtab. If the item contains a hashtab
7037 * it is added to ht_stack, if it contains a list it is added to
7038 * list_stack. */
7039 todo = (int)cur_ht->ht_used;
7040 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7041 if (!HASHITEM_EMPTY(hi))
7042 {
7043 --todo;
7044 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7045 &ht_stack, list_stack);
7046 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048
7049 if (ht_stack == NULL)
7050 break;
7051
7052 /* take an item from the stack */
7053 cur_ht = ht_stack->ht;
7054 tempitem = ht_stack;
7055 ht_stack = ht_stack->prev;
7056 free(tempitem);
7057 }
7058
7059 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060}
7061
7062/*
7063 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7065 *
7066 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007069set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 listitem_T *li;
7072 int abort = FALSE;
7073 list_T *cur_l;
7074 list_stack_T *list_stack = NULL;
7075 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007077 cur_l = l;
7078 for (;;)
7079 {
7080 if (!abort)
7081 /* Mark each item in the list. If the item contains a hashtab
7082 * it is added to ht_stack, if it contains a list it is added to
7083 * list_stack. */
7084 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7085 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7086 ht_stack, &list_stack);
7087 if (list_stack == NULL)
7088 break;
7089
7090 /* take an item from the stack */
7091 cur_l = list_stack->list;
7092 tempitem = list_stack;
7093 list_stack = list_stack->prev;
7094 free(tempitem);
7095 }
7096
7097 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007098}
7099
7100/*
7101 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007102 * "list_stack" is used to add lists to be marked. Can be NULL.
7103 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7104 *
7105 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007106 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007108set_ref_in_item(
7109 typval_T *tv,
7110 int copyID,
7111 ht_stack_T **ht_stack,
7112 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113{
7114 dict_T *dd;
7115 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007117
Bram Moolenaara03f2332016-02-06 18:09:59 +01007118 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007119 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007120 dd = tv->vval.v_dict;
7121 if (dd != NULL && dd->dv_copyID != copyID)
7122 {
7123 /* Didn't see this dict yet. */
7124 dd->dv_copyID = copyID;
7125 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007126 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007127 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7128 }
7129 else
7130 {
7131 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7132 if (newitem == NULL)
7133 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007134 else
7135 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007136 newitem->ht = &dd->dv_hashtab;
7137 newitem->prev = *ht_stack;
7138 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007140 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007141 }
7142 }
7143 else if (tv->v_type == VAR_LIST)
7144 {
7145 ll = tv->vval.v_list;
7146 if (ll != NULL && ll->lv_copyID != copyID)
7147 {
7148 /* Didn't see this list yet. */
7149 ll->lv_copyID = copyID;
7150 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007152 abort = set_ref_in_list(ll, copyID, ht_stack);
7153 }
7154 else
7155 {
7156 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007157 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007158 if (newitem == NULL)
7159 abort = TRUE;
7160 else
7161 {
7162 newitem->list = ll;
7163 newitem->prev = *list_stack;
7164 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007167 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007169 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007170}
7171
7172/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007173 * Allocate an empty header for a dictionary.
7174 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007175 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007176dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177{
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179
Bram Moolenaar33570922005-01-25 22:26:29 +00007180 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007182 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007183 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007184 if (first_dict != NULL)
7185 first_dict->dv_used_prev = d;
7186 d->dv_used_next = first_dict;
7187 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007188 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007189
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007192 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007193 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007194 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007195 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197}
7198
7199/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007200 * Allocate an empty dict for a return value.
7201 * Returns OK or FAIL.
7202 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007203 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007204rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007205{
7206 dict_T *d = dict_alloc();
7207
7208 if (d == NULL)
7209 return FAIL;
7210
7211 rettv->vval.v_dict = d;
7212 rettv->v_type = VAR_DICT;
7213 ++d->dv_refcount;
7214 return OK;
7215}
7216
7217
7218/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 * Unreference a Dictionary: decrement the reference count and free it when it
7220 * becomes zero.
7221 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007222 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007223dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007225 if (d != NULL && --d->dv_refcount <= 0)
7226 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227}
7228
7229/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007230 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231 * Ignores the reference count.
7232 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234dict_free(
7235 dict_T *d,
7236 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007238 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007239 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007240 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007242 /* Remove the dict from the list of dicts for garbage collection. */
7243 if (d->dv_used_prev == NULL)
7244 first_dict = d->dv_used_next;
7245 else
7246 d->dv_used_prev->dv_used_next = d->dv_used_next;
7247 if (d->dv_used_next != NULL)
7248 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7249
7250 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007251 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007252 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007253 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007254 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 if (!HASHITEM_EMPTY(hi))
7256 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007257 /* Remove the item before deleting it, just in case there is
7258 * something recursive causing trouble. */
7259 di = HI2DI(hi);
7260 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007261 if (recurse || (di->di_tv.v_type != VAR_LIST
7262 && di->di_tv.v_type != VAR_DICT))
7263 clear_tv(&di->di_tv);
7264 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007265 --todo;
7266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 vim_free(d);
7270}
7271
7272/*
7273 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 * The "key" is copied to the new item.
7275 * Note that the value of the item "di_tv" still needs to be initialized!
7276 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007277 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007278 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007279dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280{
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007283 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007285 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007287 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290}
7291
7292/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293 * Make a copy of a Dictionary item.
7294 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007296dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297{
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7301 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 if (di != NULL)
7303 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007305 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 copy_tv(&org->di_tv, &di->di_tv);
7307 }
7308 return di;
7309}
7310
7311/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312 * Remove item "item" from Dictionary "dict" and free it.
7313 */
7314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007315dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316{
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (HASHITEM_EMPTY(hi))
7321 EMSG2(_(e_intern2), "dictitem_remove()");
7322 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 dictitem_free(item);
7325}
7326
7327/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328 * Free a dict item. Also clears the value.
7329 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007331dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007334 if (item->di_flags & DI_FLAGS_ALLOC)
7335 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336}
7337
7338/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7340 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007341 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 * Returns NULL when out of memory.
7343 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007345dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346{
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 dict_T *copy;
7348 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351
7352 if (orig == NULL)
7353 return NULL;
7354
7355 copy = dict_alloc();
7356 if (copy != NULL)
7357 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007358 if (copyID != 0)
7359 {
7360 orig->dv_copyID = copyID;
7361 orig->dv_copydict = copy;
7362 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007363 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007364 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368 --todo;
7369
7370 di = dictitem_alloc(hi->hi_key);
7371 if (di == NULL)
7372 break;
7373 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 {
7375 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7376 copyID) == FAIL)
7377 {
7378 vim_free(di);
7379 break;
7380 }
7381 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 else
7383 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7384 if (dict_add(copy, di) == FAIL)
7385 {
7386 dictitem_free(di);
7387 break;
7388 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007393 if (todo > 0)
7394 {
7395 dict_unref(copy);
7396 copy = NULL;
7397 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 }
7399
7400 return copy;
7401}
7402
7403/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007405 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007408dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409{
Bram Moolenaar33570922005-01-25 22:26:29 +00007410 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411}
7412
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007414 * Add a number or string entry to dictionary "d".
7415 * When "str" is NULL use number "nr", otherwise use "str".
7416 * Returns FAIL when out of memory and when key already exists.
7417 */
7418 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007419dict_add_nr_str(
7420 dict_T *d,
7421 char *key,
7422 long nr,
7423 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007424{
7425 dictitem_T *item;
7426
7427 item = dictitem_alloc((char_u *)key);
7428 if (item == NULL)
7429 return FAIL;
7430 item->di_tv.v_lock = 0;
7431 if (str == NULL)
7432 {
7433 item->di_tv.v_type = VAR_NUMBER;
7434 item->di_tv.vval.v_number = nr;
7435 }
7436 else
7437 {
7438 item->di_tv.v_type = VAR_STRING;
7439 item->di_tv.vval.v_string = vim_strsave(str);
7440 }
7441 if (dict_add(d, item) == FAIL)
7442 {
7443 dictitem_free(item);
7444 return FAIL;
7445 }
7446 return OK;
7447}
7448
7449/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007450 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007451 * Returns FAIL when out of memory and when key already exists.
7452 */
7453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007454dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007455{
7456 dictitem_T *item;
7457
7458 item = dictitem_alloc((char_u *)key);
7459 if (item == NULL)
7460 return FAIL;
7461 item->di_tv.v_lock = 0;
7462 item->di_tv.v_type = VAR_LIST;
7463 item->di_tv.vval.v_list = list;
7464 if (dict_add(d, item) == FAIL)
7465 {
7466 dictitem_free(item);
7467 return FAIL;
7468 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007469 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007470 return OK;
7471}
7472
7473/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474 * Get the number of items in a Dictionary.
7475 */
7476 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007477dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007479 if (d == NULL)
7480 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007481 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482}
7483
7484/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485 * Find item "key[len]" in Dictionary "d".
7486 * If "len" is negative use strlen(key).
7487 * Returns NULL when not found.
7488 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007489 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007490dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007492#define AKEYLEN 200
7493 char_u buf[AKEYLEN];
7494 char_u *akey;
7495 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007498 if (len < 0)
7499 akey = key;
7500 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007501 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 tofree = akey = vim_strnsave(key, len);
7503 if (akey == NULL)
7504 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007505 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 else
7507 {
7508 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007509 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 akey = buf;
7511 }
7512
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 vim_free(tofree);
7515 if (HASHITEM_EMPTY(hi))
7516 return NULL;
7517 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518}
7519
7520/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007521 * Get a string item from a dictionary.
7522 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007523 * Returns NULL if the entry doesn't exist or out of memory.
7524 */
7525 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007526get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007527{
7528 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007529 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007530
7531 di = dict_find(d, key, -1);
7532 if (di == NULL)
7533 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007534 s = get_tv_string(&di->di_tv);
7535 if (save && s != NULL)
7536 s = vim_strsave(s);
7537 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007538}
7539
7540/*
7541 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007542 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007543 */
7544 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007545get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007546{
7547 dictitem_T *di;
7548
7549 di = dict_find(d, key, -1);
7550 if (di == NULL)
7551 return 0;
7552 return get_tv_number(&di->di_tv);
7553}
7554
7555/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 * Return an allocated string with the string representation of a Dictionary.
7557 * May return NULL.
7558 */
7559 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007560dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561{
7562 garray_T ga;
7563 int first = TRUE;
7564 char_u *tofree;
7565 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007568 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007571 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572 return NULL;
7573 ga_init2(&ga, (int)sizeof(char), 80);
7574 ga_append(&ga, '{');
7575
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007576 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007577 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007579 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007581 --todo;
7582
7583 if (first)
7584 first = FALSE;
7585 else
7586 ga_concat(&ga, (char_u *)", ");
7587
7588 tofree = string_quote(hi->hi_key, FALSE);
7589 if (tofree != NULL)
7590 {
7591 ga_concat(&ga, tofree);
7592 vim_free(tofree);
7593 }
7594 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007595 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007596 if (s != NULL)
7597 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007599 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007600 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007601 line_breakcheck();
7602
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007605 if (todo > 0)
7606 {
7607 vim_free(ga.ga_data);
7608 return NULL;
7609 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610
7611 ga_append(&ga, '}');
7612 ga_append(&ga, NUL);
7613 return (char_u *)ga.ga_data;
7614}
7615
7616/*
7617 * Allocate a variable for a Dictionary and fill it from "*arg".
7618 * Return OK or FAIL. Returns NOTDONE for {expr}.
7619 */
7620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007621get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622{
Bram Moolenaar33570922005-01-25 22:26:29 +00007623 dict_T *d = NULL;
7624 typval_T tvkey;
7625 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007626 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630
7631 /*
7632 * First check if it's not a curly-braces thing: {expr}.
7633 * Must do this without evaluating, otherwise a function may be called
7634 * twice. Unfortunately this means we need to call eval1() twice for the
7635 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 if (*start != '}')
7639 {
7640 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7641 return FAIL;
7642 if (*start == '}')
7643 return NOTDONE;
7644 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645
7646 if (evaluate)
7647 {
7648 d = dict_alloc();
7649 if (d == NULL)
7650 return FAIL;
7651 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007652 tvkey.v_type = VAR_UNKNOWN;
7653 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654
7655 *arg = skipwhite(*arg + 1);
7656 while (**arg != '}' && **arg != NUL)
7657 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007658 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659 goto failret;
7660 if (**arg != ':')
7661 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007662 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007663 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 goto failret;
7665 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007666 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007668 key = get_tv_string_buf_chk(&tvkey, buf);
7669 if (key == NULL || *key == NUL)
7670 {
7671 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7672 if (key != NULL)
7673 EMSG(_(e_emptykey));
7674 clear_tv(&tvkey);
7675 goto failret;
7676 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678
7679 *arg = skipwhite(*arg + 1);
7680 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7681 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007682 if (evaluate)
7683 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 goto failret;
7685 }
7686 if (evaluate)
7687 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007688 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 if (item != NULL)
7690 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007691 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 clear_tv(&tv);
7694 goto failret;
7695 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 item = dictitem_alloc(key);
7697 clear_tv(&tvkey);
7698 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007701 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007702 if (dict_add(d, item) == FAIL)
7703 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 }
7705 }
7706
7707 if (**arg == '}')
7708 break;
7709 if (**arg != ',')
7710 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007711 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 goto failret;
7713 }
7714 *arg = skipwhite(*arg + 1);
7715 }
7716
7717 if (**arg != '}')
7718 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007719 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007720failret:
7721 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007722 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 return FAIL;
7724 }
7725
7726 *arg = skipwhite(*arg + 1);
7727 if (evaluate)
7728 {
7729 rettv->v_type = VAR_DICT;
7730 rettv->vval.v_dict = d;
7731 ++d->dv_refcount;
7732 }
7733
7734 return OK;
7735}
7736
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007737#if defined(FEAT_CHANNEL) || defined(PROTO)
7738/*
7739 * Decrement the reference count on "channel" and free it when it goes down to
7740 * zero.
7741 * Returns TRUE when the channel was freed.
7742 */
7743 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007744channel_unref(channel_T *channel)
7745{
7746 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007747 {
Bram Moolenaar77073442016-02-13 23:23:53 +01007748 channel_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007749 return TRUE;
7750 }
7751 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007752}
7753#endif
7754
Bram Moolenaar65edff82016-02-21 16:40:11 +01007755#if defined(FEAT_JOB) || defined(PROTO)
7756static job_T *first_job = NULL;
7757
Bram Moolenaar835dc632016-02-07 14:27:38 +01007758 static void
7759job_free(job_T *job)
7760{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007761# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007762 if (job->jv_channel != NULL)
7763 {
7764 /* The channel doesn't count as a references for the job, we need to
7765 * NULL the reference when the job is freed. */
7766 job->jv_channel->ch_job = NULL;
7767 channel_unref(job->jv_channel);
7768 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007769# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007770 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007771
7772 if (job->jv_next != NULL)
7773 job->jv_next->jv_prev = job->jv_prev;
7774 if (job->jv_prev == NULL)
7775 first_job = job->jv_next;
7776 else
7777 job->jv_prev->jv_next = job->jv_next;
7778
7779 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007780 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007781 vim_free(job);
7782}
7783
7784 static void
7785job_unref(job_T *job)
7786{
7787 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007788 {
7789 /* Do not free the job when it has not ended yet and there is a
7790 * "stoponexit" flag or an exit callback. */
7791 if (job->jv_status != JOB_STARTED
7792 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
7793 job_free(job);
7794 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007795}
7796
7797/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007798 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007799 */
7800 static job_T *
7801job_alloc(void)
7802{
7803 job_T *job;
7804
7805 job = (job_T *)alloc_clear(sizeof(job_T));
7806 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007807 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007808 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007809 job->jv_stoponexit = vim_strsave((char_u *)"term");
7810
7811 if (first_job != NULL)
7812 {
7813 first_job->jv_prev = job;
7814 job->jv_next = first_job;
7815 }
7816 first_job = job;
7817 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007818 return job;
7819}
7820
Bram Moolenaar65edff82016-02-21 16:40:11 +01007821 static void
7822job_set_options(job_T *job, jobopt_T *opt)
7823{
7824 if (opt->jo_set & JO_STOPONEXIT)
7825 {
7826 vim_free(job->jv_stoponexit);
7827 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7828 job->jv_stoponexit = NULL;
7829 else
7830 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7831 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007832 if (opt->jo_set & JO_EXIT_CB)
7833 {
7834 vim_free(job->jv_exit_cb);
7835 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7836 job->jv_exit_cb = NULL;
7837 else
7838 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7839 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007840}
7841
7842/*
7843 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7844 */
7845 void
7846job_stop_on_exit()
7847{
7848 job_T *job;
7849
7850 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007851 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007852 mch_stop_job(job, job->jv_stoponexit);
7853}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007854#endif
7855
Bram Moolenaar17a13432016-01-24 14:22:10 +01007856 static char *
7857get_var_special_name(int nr)
7858{
7859 switch (nr)
7860 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007861 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007862 case VVAL_TRUE: return "v:true";
7863 case VVAL_NONE: return "v:none";
7864 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007865 }
7866 EMSG2(_(e_intern2), "get_var_special_name()");
7867 return "42";
7868}
7869
Bram Moolenaar8c711452005-01-14 21:53:12 +00007870/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007871 * Return a string with the string representation of a variable.
7872 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007873 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007874 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007875 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007876 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007877 */
7878 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007879echo_string(
7880 typval_T *tv,
7881 char_u **tofree,
7882 char_u *numbuf,
7883 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007884{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007885 static int recurse = 0;
7886 char_u *r = NULL;
7887
Bram Moolenaar33570922005-01-25 22:26:29 +00007888 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007889 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007890 if (!did_echo_string_emsg)
7891 {
7892 /* Only give this message once for a recursive call to avoid
7893 * flooding the user with errors. And stop iterating over lists
7894 * and dicts. */
7895 did_echo_string_emsg = TRUE;
7896 EMSG(_("E724: variable nested too deep for displaying"));
7897 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007898 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007899 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007900 }
7901 ++recurse;
7902
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007903 switch (tv->v_type)
7904 {
7905 case VAR_FUNC:
7906 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007907 r = tv->vval.v_string;
7908 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007909
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007910 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007911 if (tv->vval.v_list == NULL)
7912 {
7913 *tofree = NULL;
7914 r = NULL;
7915 }
7916 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7917 {
7918 *tofree = NULL;
7919 r = (char_u *)"[...]";
7920 }
7921 else
7922 {
7923 tv->vval.v_list->lv_copyID = copyID;
7924 *tofree = list2string(tv, copyID);
7925 r = *tofree;
7926 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007927 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007928
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007930 if (tv->vval.v_dict == NULL)
7931 {
7932 *tofree = NULL;
7933 r = NULL;
7934 }
7935 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7936 {
7937 *tofree = NULL;
7938 r = (char_u *)"{...}";
7939 }
7940 else
7941 {
7942 tv->vval.v_dict->dv_copyID = copyID;
7943 *tofree = dict2string(tv, copyID);
7944 r = *tofree;
7945 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007946 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007947
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007948 case VAR_STRING:
7949 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007950 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007951 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007952 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007953 *tofree = NULL;
7954 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007955 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007956
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007957 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007958#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007959 *tofree = NULL;
7960 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7961 r = numbuf;
7962 break;
7963#endif
7964
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007965 case VAR_SPECIAL:
7966 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007967 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007968 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007970
Bram Moolenaar8502c702014-06-17 12:51:16 +02007971 if (--recurse == 0)
7972 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007973 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007974}
7975
7976/*
7977 * Return a string with the string representation of a variable.
7978 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7979 * "numbuf" is used for a number.
7980 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007981 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007982 */
7983 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007984tv2string(
7985 typval_T *tv,
7986 char_u **tofree,
7987 char_u *numbuf,
7988 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007989{
7990 switch (tv->v_type)
7991 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007992 case VAR_FUNC:
7993 *tofree = string_quote(tv->vval.v_string, TRUE);
7994 return *tofree;
7995 case VAR_STRING:
7996 *tofree = string_quote(tv->vval.v_string, FALSE);
7997 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007998#ifdef FEAT_FLOAT
7999 case VAR_FLOAT:
8000 *tofree = NULL;
8001 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8002 return numbuf;
8003#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008004 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008005 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008006 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008007 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008008 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008009 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008010 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008011 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008012 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008013 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008014}
8015
8016/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 * Return string "str" in ' quotes, doubling ' characters.
8018 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008019 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008020 */
8021 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008022string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008023{
Bram Moolenaar33570922005-01-25 22:26:29 +00008024 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008025 char_u *p, *r, *s;
8026
Bram Moolenaar33570922005-01-25 22:26:29 +00008027 len = (function ? 13 : 3);
8028 if (str != NULL)
8029 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008030 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008031 for (p = str; *p != NUL; mb_ptr_adv(p))
8032 if (*p == '\'')
8033 ++len;
8034 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008035 s = r = alloc(len);
8036 if (r != NULL)
8037 {
8038 if (function)
8039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008040 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008041 r += 10;
8042 }
8043 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008044 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 if (str != NULL)
8046 for (p = str; *p != NUL; )
8047 {
8048 if (*p == '\'')
8049 *r++ = '\'';
8050 MB_COPY_CHAR(p, r);
8051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008052 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008053 if (function)
8054 *r++ = ')';
8055 *r++ = NUL;
8056 }
8057 return s;
8058}
8059
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008060#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008061/*
8062 * Convert the string "text" to a floating point number.
8063 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8064 * this always uses a decimal point.
8065 * Returns the length of the text that was consumed.
8066 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008068string2float(
8069 char_u *text,
8070 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008071{
8072 char *s = (char *)text;
8073 float_T f;
8074
8075 f = strtod(s, &s);
8076 *value = f;
8077 return (int)((char_u *)s - text);
8078}
8079#endif
8080
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 * Get the value of an environment variable.
8083 * "arg" is pointing to the '$'. It is advanced to after the name.
8084 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008085 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 */
8087 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008088get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089{
8090 char_u *string = NULL;
8091 int len;
8092 int cc;
8093 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008094 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095
8096 ++*arg;
8097 name = *arg;
8098 len = get_env_len(arg);
8099 if (evaluate)
8100 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008101 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008102 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008103
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008104 cc = name[len];
8105 name[len] = NUL;
8106 /* first try vim_getenv(), fast for normal environment vars */
8107 string = vim_getenv(name, &mustfree);
8108 if (string != NULL && *string != NUL)
8109 {
8110 if (!mustfree)
8111 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008113 else
8114 {
8115 if (mustfree)
8116 vim_free(string);
8117
8118 /* next try expanding things like $VIM and ${HOME} */
8119 string = expand_env_save(name - 1);
8120 if (string != NULL && *string == '$')
8121 {
8122 vim_free(string);
8123 string = NULL;
8124 }
8125 }
8126 name[len] = cc;
8127
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008128 rettv->v_type = VAR_STRING;
8129 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 }
8131
8132 return OK;
8133}
8134
8135/*
8136 * Array with names and number of arguments of all internal functions
8137 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8138 */
8139static struct fst
8140{
8141 char *f_name; /* function name */
8142 char f_min_argc; /* minimal number of arguments */
8143 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008144 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008145 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146} functions[] =
8147{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008148#ifdef FEAT_FLOAT
8149 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008150 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008151#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008152 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008153 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008154 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"append", 2, 2, f_append},
8156 {"argc", 0, 0, f_argc},
8157 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008158 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008159 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008160#ifdef FEAT_FLOAT
8161 {"asin", 1, 1, f_asin}, /* WJMc */
8162#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008163 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008164 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008165 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008166 {"assert_false", 1, 2, f_assert_false},
8167 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#ifdef FEAT_FLOAT
8169 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008170 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008173 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"bufexists", 1, 1, f_bufexists},
8175 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8176 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8177 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8178 {"buflisted", 1, 1, f_buflisted},
8179 {"bufloaded", 1, 1, f_bufloaded},
8180 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008181 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"bufwinnr", 1, 1, f_bufwinnr},
8183 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008184 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008185 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008186 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008187#ifdef FEAT_FLOAT
8188 {"ceil", 1, 1, f_ceil},
8189#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008190#ifdef FEAT_CHANNEL
8191 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008192# ifdef FEAT_JOB
8193 {"ch_getjob", 1, 1, f_ch_getjob},
8194# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008195 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008196 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008197 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008198 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008199 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008200 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8201 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008202 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008203 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008204#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008205 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008206 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008208 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008210#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008211 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008212 {"complete_add", 1, 1, f_complete_add},
8213 {"complete_check", 0, 0, f_complete_check},
8214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008216 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008217#ifdef FEAT_FLOAT
8218 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008219 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008220#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008221 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008223 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008224 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008225 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008227 {"diff_filler", 1, 1, f_diff_filler},
8228 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008229 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008230 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008232 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"eventhandler", 0, 0, f_eventhandler},
8234 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008235 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008237#ifdef FEAT_FLOAT
8238 {"exp", 1, 1, f_exp},
8239#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008240 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008241 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008242 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8244 {"filereadable", 1, 1, f_filereadable},
8245 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008246 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008247 {"finddir", 1, 3, f_finddir},
8248 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008249#ifdef FEAT_FLOAT
8250 {"float2nr", 1, 1, f_float2nr},
8251 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008252 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008253#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008254 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"fnamemodify", 2, 2, f_fnamemodify},
8256 {"foldclosed", 1, 1, f_foldclosed},
8257 {"foldclosedend", 1, 1, f_foldclosedend},
8258 {"foldlevel", 1, 1, f_foldlevel},
8259 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008260 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008262 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008263 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008264 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008265 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008266 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"getchar", 0, 1, f_getchar},
8268 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008269 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"getcmdline", 0, 0, f_getcmdline},
8271 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008272 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008273 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008274 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008275 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008276 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008277 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {"getfsize", 1, 1, f_getfsize},
8279 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008280 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008281 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008282 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008283 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008284 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008285 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008286 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008287 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008289 {"gettabvar", 2, 3, f_gettabvar},
8290 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"getwinposx", 0, 0, f_getwinposx},
8292 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008293 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008294 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008295 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008296 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008298 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008299 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008300 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8302 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8303 {"histadd", 2, 2, f_histadd},
8304 {"histdel", 1, 2, f_histdel},
8305 {"histget", 1, 2, f_histget},
8306 {"histnr", 1, 1, f_histnr},
8307 {"hlID", 1, 1, f_hlID},
8308 {"hlexists", 1, 1, f_hlexists},
8309 {"hostname", 0, 0, f_hostname},
8310 {"iconv", 3, 3, f_iconv},
8311 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008312 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008313 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008315 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"inputrestore", 0, 0, f_inputrestore},
8317 {"inputsave", 0, 0, f_inputsave},
8318 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008320 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008322 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008323 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008324#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008325# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008326 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008327# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008328 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008329 {"job_start", 1, 2, f_job_start},
8330 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008331 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008332#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008333 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008334 {"js_decode", 1, 1, f_js_decode},
8335 {"js_encode", 1, 1, f_js_encode},
8336 {"json_decode", 1, 1, f_json_decode},
8337 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008338 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008340 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"libcall", 3, 3, f_libcall},
8342 {"libcallnr", 3, 3, f_libcallnr},
8343 {"line", 1, 1, f_line},
8344 {"line2byte", 1, 1, f_line2byte},
8345 {"lispindent", 1, 1, f_lispindent},
8346 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008347#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008348 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008349 {"log10", 1, 1, f_log10},
8350#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008351#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008352 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008353#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008354 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008355 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008356 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008357 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008358 {"matchadd", 2, 5, f_matchadd},
8359 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008360 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008361 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008362 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008363 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008364 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008365 {"max", 1, 1, f_max},
8366 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008367#ifdef vim_mkdir
8368 {"mkdir", 1, 3, f_mkdir},
8369#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008371#ifdef FEAT_MZSCHEME
8372 {"mzeval", 1, 1, f_mzeval},
8373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008375 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008376 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008377 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008378#ifdef FEAT_PERL
8379 {"perleval", 1, 1, f_perleval},
8380#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008381#ifdef FEAT_FLOAT
8382 {"pow", 2, 2, f_pow},
8383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008385 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008386 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008387#ifdef FEAT_PYTHON3
8388 {"py3eval", 1, 1, f_py3eval},
8389#endif
8390#ifdef FEAT_PYTHON
8391 {"pyeval", 1, 1, f_pyeval},
8392#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008393 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008394 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008395 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008396 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008397 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 {"remote_expr", 2, 3, f_remote_expr},
8399 {"remote_foreground", 1, 1, f_remote_foreground},
8400 {"remote_peek", 1, 2, f_remote_peek},
8401 {"remote_read", 1, 1, f_remote_read},
8402 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008403 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008405 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008407 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008408#ifdef FEAT_FLOAT
8409 {"round", 1, 1, f_round},
8410#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008411 {"screenattr", 2, 2, f_screenattr},
8412 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008413 {"screencol", 0, 0, f_screencol},
8414 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008415 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008416 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008417 {"searchpair", 3, 7, f_searchpair},
8418 {"searchpairpos", 3, 7, f_searchpairpos},
8419 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"server2client", 2, 2, f_server2client},
8421 {"serverlist", 0, 0, f_serverlist},
8422 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008423 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"setcmdpos", 1, 1, f_setcmdpos},
8425 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008426 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008427 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008428 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008429 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008431 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008432 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008434#ifdef FEAT_CRYPT
8435 {"sha256", 1, 1, f_sha256},
8436#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008437 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008438 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008440#ifdef FEAT_FLOAT
8441 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008442 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008443#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008444 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008445 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008446 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008447 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008448 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008449#ifdef FEAT_FLOAT
8450 {"sqrt", 1, 1, f_sqrt},
8451 {"str2float", 1, 1, f_str2float},
8452#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008453 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008454 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008455 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456#ifdef HAVE_STRFTIME
8457 {"strftime", 1, 2, f_strftime},
8458#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008459 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008460 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {"strlen", 1, 1, f_strlen},
8462 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008463 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008465 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008466 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {"substitute", 4, 4, f_substitute},
8468 {"synID", 3, 3, f_synID},
8469 {"synIDattr", 2, 3, f_synIDattr},
8470 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008471 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008472 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008473 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008474 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008475 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008476 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008477 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008478 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008479 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008480#ifdef FEAT_FLOAT
8481 {"tan", 1, 1, f_tan},
8482 {"tanh", 1, 1, f_tanh},
8483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008485 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {"tolower", 1, 1, f_tolower},
8487 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008488 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008489#ifdef FEAT_FLOAT
8490 {"trunc", 1, 1, f_trunc},
8491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008493 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008494 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008495 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008496 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 {"virtcol", 1, 1, f_virtcol},
8498 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008499 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {"winbufnr", 1, 1, f_winbufnr},
8501 {"wincol", 0, 0, f_wincol},
8502 {"winheight", 1, 1, f_winheight},
8503 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008504 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008506 {"winrestview", 1, 1, f_winrestview},
8507 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008509 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008510 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008511 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512};
8513
8514#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8515
8516/*
8517 * Function given to ExpandGeneric() to obtain the list of internal
8518 * or user defined function names.
8519 */
8520 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008521get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 static int intidx = -1;
8524 char_u *name;
8525
8526 if (idx == 0)
8527 intidx = -1;
8528 if (intidx < 0)
8529 {
8530 name = get_user_func_name(xp, idx);
8531 if (name != NULL)
8532 return name;
8533 }
8534 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8535 {
8536 STRCPY(IObuff, functions[intidx].f_name);
8537 STRCAT(IObuff, "(");
8538 if (functions[intidx].f_max_argc == 0)
8539 STRCAT(IObuff, ")");
8540 return IObuff;
8541 }
8542
8543 return NULL;
8544}
8545
8546/*
8547 * Function given to ExpandGeneric() to obtain the list of internal or
8548 * user defined variable or function names.
8549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008551get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 static int intidx = -1;
8554 char_u *name;
8555
8556 if (idx == 0)
8557 intidx = -1;
8558 if (intidx < 0)
8559 {
8560 name = get_function_name(xp, idx);
8561 if (name != NULL)
8562 return name;
8563 }
8564 return get_user_var_name(xp, ++intidx);
8565}
8566
8567#endif /* FEAT_CMDL_COMPL */
8568
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008569#if defined(EBCDIC) || defined(PROTO)
8570/*
8571 * Compare struct fst by function name.
8572 */
8573 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008574compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008575{
8576 struct fst *p1 = (struct fst *)s1;
8577 struct fst *p2 = (struct fst *)s2;
8578
8579 return STRCMP(p1->f_name, p2->f_name);
8580}
8581
8582/*
8583 * Sort the function table by function name.
8584 * The sorting of the table above is ASCII dependant.
8585 * On machines using EBCDIC we have to sort it.
8586 */
8587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008588sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008589{
8590 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8591
8592 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8593}
8594#endif
8595
8596
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597/*
8598 * Find internal function in table above.
8599 * Return index, or -1 if not found
8600 */
8601 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008602find_internal_func(
8603 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604{
8605 int first = 0;
8606 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8607 int cmp;
8608 int x;
8609
8610 /*
8611 * Find the function name in the table. Binary search.
8612 */
8613 while (first <= last)
8614 {
8615 x = first + ((unsigned)(last - first) >> 1);
8616 cmp = STRCMP(name, functions[x].f_name);
8617 if (cmp < 0)
8618 last = x - 1;
8619 else if (cmp > 0)
8620 first = x + 1;
8621 else
8622 return x;
8623 }
8624 return -1;
8625}
8626
8627/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008628 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8629 * name it contains, otherwise return "name".
8630 */
8631 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008632deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008633{
Bram Moolenaar33570922005-01-25 22:26:29 +00008634 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008635 int cc;
8636
8637 cc = name[*lenp];
8638 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008639 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008640 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008641 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008642 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008643 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008644 {
8645 *lenp = 0;
8646 return (char_u *)""; /* just in case */
8647 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008648 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008649 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 }
8651
8652 return name;
8653}
8654
8655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 * Allocate a variable for the result of a function.
8657 * Return OK or FAIL.
8658 */
8659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008660get_func_tv(
8661 char_u *name, /* name of the function */
8662 int len, /* length of "name" */
8663 typval_T *rettv,
8664 char_u **arg, /* argument, pointing to the '(' */
8665 linenr_T firstline, /* first line of range */
8666 linenr_T lastline, /* last line of range */
8667 int *doesrange, /* return: function handled range */
8668 int evaluate,
8669 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670{
8671 char_u *argp;
8672 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008673 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 int argcount = 0; /* number of arguments found */
8675
8676 /*
8677 * Get the arguments.
8678 */
8679 argp = *arg;
8680 while (argcount < MAX_FUNC_ARGS)
8681 {
8682 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8683 if (*argp == ')' || *argp == ',' || *argp == NUL)
8684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8686 {
8687 ret = FAIL;
8688 break;
8689 }
8690 ++argcount;
8691 if (*argp != ',')
8692 break;
8693 }
8694 if (*argp == ')')
8695 ++argp;
8696 else
8697 ret = FAIL;
8698
8699 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008700 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008701 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008703 {
8704 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008705 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008707 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
8710 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712
8713 *arg = skipwhite(argp);
8714 return ret;
8715}
8716
8717
8718/*
8719 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008720 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008721 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008723 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008724call_func(
8725 char_u *funcname, /* name of the function */
8726 int len, /* length of "name" */
8727 typval_T *rettv, /* return value goes here */
8728 int argcount, /* number of "argvars" */
8729 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008730 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008731 linenr_T firstline, /* first line of range */
8732 linenr_T lastline, /* last line of range */
8733 int *doesrange, /* return: function handled range */
8734 int evaluate,
8735 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736{
8737 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738#define ERROR_UNKNOWN 0
8739#define ERROR_TOOMANY 1
8740#define ERROR_TOOFEW 2
8741#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008742#define ERROR_DICT 4
8743#define ERROR_NONE 5
8744#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 int error = ERROR_NONE;
8746 int i;
8747 int llen;
8748 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749#define FLEN_FIXED 40
8750 char_u fname_buf[FLEN_FIXED + 1];
8751 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008752 char_u *name;
8753
8754 /* Make a copy of the name, if it comes from a funcref variable it could
8755 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008756 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008757 if (name == NULL)
8758 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759
8760 /*
8761 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8762 * Change <SNR>123_name() to K_SNR 123_name().
8763 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 llen = eval_fname_script(name);
8766 if (llen > 0)
8767 {
8768 fname_buf[0] = K_SPECIAL;
8769 fname_buf[1] = KS_EXTRA;
8770 fname_buf[2] = (int)KE_SNR;
8771 i = 3;
8772 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8773 {
8774 if (current_SID <= 0)
8775 error = ERROR_SCRIPT;
8776 else
8777 {
8778 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8779 i = (int)STRLEN(fname_buf);
8780 }
8781 }
8782 if (i + STRLEN(name + llen) < FLEN_FIXED)
8783 {
8784 STRCPY(fname_buf + i, name + llen);
8785 fname = fname_buf;
8786 }
8787 else
8788 {
8789 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8790 if (fname == NULL)
8791 error = ERROR_OTHER;
8792 else
8793 {
8794 mch_memmove(fname, fname_buf, (size_t)i);
8795 STRCPY(fname + i, name + llen);
8796 }
8797 }
8798 }
8799 else
8800 fname = name;
8801
8802 *doesrange = FALSE;
8803
8804
8805 /* execute the function if no errors detected and executing */
8806 if (evaluate && error == ERROR_NONE)
8807 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008808 char_u *rfname = fname;
8809
8810 /* Ignore "g:" before a function name. */
8811 if (fname[0] == 'g' && fname[1] == ':')
8812 rfname = fname + 2;
8813
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008814 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8815 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 error = ERROR_UNKNOWN;
8817
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008818 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 {
8820 /*
8821 * User defined function.
8822 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008823 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008824
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008826 /* Trigger FuncUndefined event, may load the function. */
8827 if (fp == NULL
8828 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008829 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008830 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008832 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008833 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 }
8835#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008836 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008837 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008838 {
8839 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008840 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008841 }
8842
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (fp != NULL)
8844 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008845 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008847 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008849 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008851 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008852 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 else
8854 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008855 int did_save_redo = FALSE;
8856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 /*
8858 * Call the user function.
8859 * Save and restore search patterns, script variables and
8860 * redo buffer.
8861 */
8862 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008863#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008864 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008865#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008866 {
8867 saveRedobuff();
8868 did_save_redo = TRUE;
8869 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008870 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008871 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008872 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008873 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8874 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8875 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008876 /* Function was unreferenced while being used, free it
8877 * now. */
8878 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008879 if (did_save_redo)
8880 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 restore_search_patterns();
8882 error = ERROR_NONE;
8883 }
8884 }
8885 }
8886 else
8887 {
8888 /*
8889 * Find the function name in the table, call its implementation.
8890 */
8891 i = find_internal_func(fname);
8892 if (i >= 0)
8893 {
8894 if (argcount < functions[i].f_min_argc)
8895 error = ERROR_TOOFEW;
8896 else if (argcount > functions[i].f_max_argc)
8897 error = ERROR_TOOMANY;
8898 else
8899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008900 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 error = ERROR_NONE;
8903 }
8904 }
8905 }
8906 /*
8907 * The function call (or "FuncUndefined" autocommand sequence) might
8908 * have been aborted by an error, an interrupt, or an explicitly thrown
8909 * exception that has not been caught so far. This situation can be
8910 * tested for by calling aborting(). For an error in an internal
8911 * function or for the "E132" error in call_user_func(), however, the
8912 * throw point at which the "force_abort" flag (temporarily reset by
8913 * emsg()) is normally updated has not been reached yet. We need to
8914 * update that flag first to make aborting() reliable.
8915 */
8916 update_force_abort();
8917 }
8918 if (error == ERROR_NONE)
8919 ret = OK;
8920
8921 /*
8922 * Report an error unless the argument evaluation or function call has been
8923 * cancelled due to an aborting error, an interrupt, or an exception.
8924 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008925 if (!aborting())
8926 {
8927 switch (error)
8928 {
8929 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008930 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008931 break;
8932 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008933 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008934 break;
8935 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008936 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008937 name);
8938 break;
8939 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008940 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008941 name);
8942 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008943 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008944 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008945 name);
8946 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008947 }
8948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 if (fname != name && fname != fname_buf)
8951 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008952 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
8954 return ret;
8955}
8956
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008957/*
8958 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008959 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008960 */
8961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008962emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008963{
8964 char_u *p;
8965
8966 if (*name == K_SPECIAL)
8967 p = concat_str((char_u *)"<SNR>", name + 3);
8968 else
8969 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008970 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008971 if (p != name)
8972 vim_free(p);
8973}
8974
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008975/*
8976 * Return TRUE for a non-zero Number and a non-empty String.
8977 */
8978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008979non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008980{
8981 return ((argvars[0].v_type == VAR_NUMBER
8982 && argvars[0].vval.v_number != 0)
8983 || (argvars[0].v_type == VAR_STRING
8984 && argvars[0].vval.v_string != NULL
8985 && *argvars[0].vval.v_string != NUL));
8986}
8987
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988/*********************************************
8989 * Implementation of the built-in functions
8990 */
8991
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008992#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008993static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008994
8995/*
8996 * Get the float value of "argvars[0]" into "f".
8997 * Returns FAIL when the argument is not a Number or Float.
8998 */
8999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009000get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009001{
9002 if (argvars[0].v_type == VAR_FLOAT)
9003 {
9004 *f = argvars[0].vval.v_float;
9005 return OK;
9006 }
9007 if (argvars[0].v_type == VAR_NUMBER)
9008 {
9009 *f = (float_T)argvars[0].vval.v_number;
9010 return OK;
9011 }
9012 EMSG(_("E808: Number or Float required"));
9013 return FAIL;
9014}
9015
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009016/*
9017 * "abs(expr)" function
9018 */
9019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009020f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009021{
9022 if (argvars[0].v_type == VAR_FLOAT)
9023 {
9024 rettv->v_type = VAR_FLOAT;
9025 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9026 }
9027 else
9028 {
9029 varnumber_T n;
9030 int error = FALSE;
9031
9032 n = get_tv_number_chk(&argvars[0], &error);
9033 if (error)
9034 rettv->vval.v_number = -1;
9035 else if (n > 0)
9036 rettv->vval.v_number = n;
9037 else
9038 rettv->vval.v_number = -n;
9039 }
9040}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009041
9042/*
9043 * "acos()" function
9044 */
9045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009046f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009047{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009048 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009049
9050 rettv->v_type = VAR_FLOAT;
9051 if (get_float_arg(argvars, &f) == OK)
9052 rettv->vval.v_float = acos(f);
9053 else
9054 rettv->vval.v_float = 0.0;
9055}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009056#endif
9057
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009059 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 */
9061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009062f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
Bram Moolenaar33570922005-01-25 22:26:29 +00009064 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009067 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009069 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009070 && !tv_check_lock(l->lv_lock,
9071 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009072 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009073 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009074 }
9075 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009076 EMSG(_(e_listreq));
9077}
9078
9079/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009080 * "alloc_fail(id, countdown, repeat)" function
9081 */
9082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009083f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009084{
9085 if (argvars[0].v_type != VAR_NUMBER
9086 || argvars[0].vval.v_number <= 0
9087 || argvars[1].v_type != VAR_NUMBER
9088 || argvars[1].vval.v_number < 0
9089 || argvars[2].v_type != VAR_NUMBER)
9090 EMSG(_(e_invarg));
9091 else
9092 {
9093 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009094 if (alloc_fail_id >= aid_last)
9095 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009096 alloc_fail_countdown = argvars[1].vval.v_number;
9097 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009098 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009099 }
9100}
9101
9102/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009103 * "and(expr, expr)" function
9104 */
9105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009106f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009107{
9108 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9109 & get_tv_number_chk(&argvars[1], NULL);
9110}
9111
9112/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009113 * "append(lnum, string/list)" function
9114 */
9115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009116f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009117{
9118 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009119 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 list_T *l = NULL;
9121 listitem_T *li = NULL;
9122 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009123 long added = 0;
9124
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009125 /* When coming here from Insert mode, sync undo, so that this can be
9126 * undone separately from what was previously inserted. */
9127 if (u_sync_once == 2)
9128 {
9129 u_sync_once = 1; /* notify that u_sync() was called */
9130 u_sync(TRUE);
9131 }
9132
Bram Moolenaar0d660222005-01-07 21:51:51 +00009133 lnum = get_tv_lnum(argvars);
9134 if (lnum >= 0
9135 && lnum <= curbuf->b_ml.ml_line_count
9136 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009137 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009138 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009139 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009140 l = argvars[1].vval.v_list;
9141 if (l == NULL)
9142 return;
9143 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009144 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009145 for (;;)
9146 {
9147 if (l == NULL)
9148 tv = &argvars[1]; /* append a string */
9149 else if (li == NULL)
9150 break; /* end of list */
9151 else
9152 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009153 line = get_tv_string_chk(tv);
9154 if (line == NULL) /* type error */
9155 {
9156 rettv->vval.v_number = 1; /* Failed */
9157 break;
9158 }
9159 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009160 ++added;
9161 if (l == NULL)
9162 break;
9163 li = li->li_next;
9164 }
9165
9166 appended_lines_mark(lnum, added);
9167 if (curwin->w_cursor.lnum > lnum)
9168 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009170 else
9171 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172}
9173
9174/*
9175 * "argc()" function
9176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009178f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009180 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181}
9182
9183/*
9184 * "argidx()" function
9185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009187f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009189 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190}
9191
9192/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009193 * "arglistid()" function
9194 */
9195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009196f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009197{
9198 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009199
9200 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009201 wp = find_tabwin(&argvars[0], &argvars[1]);
9202 if (wp != NULL)
9203 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009204}
9205
9206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 * "argv(nr)" function
9208 */
9209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009210f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 int idx;
9213
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009214 if (argvars[0].v_type != VAR_UNKNOWN)
9215 {
9216 idx = get_tv_number_chk(&argvars[0], NULL);
9217 if (idx >= 0 && idx < ARGCOUNT)
9218 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9219 else
9220 rettv->vval.v_string = NULL;
9221 rettv->v_type = VAR_STRING;
9222 }
9223 else if (rettv_list_alloc(rettv) == OK)
9224 for (idx = 0; idx < ARGCOUNT; ++idx)
9225 list_append_string(rettv->vval.v_list,
9226 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227}
9228
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009229static void prepare_assert_error(garray_T*gap);
9230static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9231static void assert_error(garray_T *gap);
9232static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009233
9234/*
9235 * Prepare "gap" for an assert error and add the sourcing position.
9236 */
9237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009238prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009239{
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/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009259 * Append "str" to "gap", escaping unprintable characters.
9260 * Changes NL to \n, CR to \r, etc.
9261 */
9262 static void
9263ga_concat_esc(garray_T *gap, char_u *str)
9264{
9265 char_u *p;
9266 char_u buf[NUMBUFLEN];
9267
9268 for (p = str; *p != NUL; ++p)
9269 switch (*p)
9270 {
9271 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9272 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9273 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9274 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9275 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9276 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9277 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9278 default:
9279 if (*p < ' ')
9280 {
9281 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9282 ga_concat(gap, buf);
9283 }
9284 else
9285 ga_append(gap, *p);
9286 break;
9287 }
9288}
9289
9290/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009291 * Fill "gap" with information about an assert error.
9292 */
9293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009294fill_assert_error(
9295 garray_T *gap,
9296 typval_T *opt_msg_tv,
9297 char_u *exp_str,
9298 typval_T *exp_tv,
9299 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009300{
9301 char_u numbuf[NUMBUFLEN];
9302 char_u *tofree;
9303
9304 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9305 {
9306 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9307 vim_free(tofree);
9308 }
9309 else
9310 {
9311 ga_concat(gap, (char_u *)"Expected ");
9312 if (exp_str == NULL)
9313 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009314 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009315 vim_free(tofree);
9316 }
9317 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009318 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009319 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009320 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009321 vim_free(tofree);
9322 }
9323}
Bram Moolenaar43345542015-11-29 17:35:35 +01009324
9325/*
9326 * Add an assert error to v:errors.
9327 */
9328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009329assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009330{
9331 struct vimvar *vp = &vimvars[VV_ERRORS];
9332
9333 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9334 /* Make sure v:errors is a list. */
9335 set_vim_var_list(VV_ERRORS, list_alloc());
9336 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9337}
9338
Bram Moolenaar43345542015-11-29 17:35:35 +01009339/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009340 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009341 */
9342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009343f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009344{
9345 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009346
9347 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9348 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009349 prepare_assert_error(&ga);
9350 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9351 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009352 ga_clear(&ga);
9353 }
9354}
9355
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009356/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009357 * "assert_exception(string[, msg])" function
9358 */
9359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009360f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009361{
9362 garray_T ga;
9363 char *error;
9364
9365 error = (char *)get_tv_string_chk(&argvars[0]);
9366 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9367 {
9368 prepare_assert_error(&ga);
9369 ga_concat(&ga, (char_u *)"v:exception is not set");
9370 assert_error(&ga);
9371 ga_clear(&ga);
9372 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009373 else if (error != NULL
9374 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009375 {
9376 prepare_assert_error(&ga);
9377 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9378 &vimvars[VV_EXCEPTION].vv_tv);
9379 assert_error(&ga);
9380 ga_clear(&ga);
9381 }
9382}
9383
9384/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009385 * "assert_fails(cmd [, error])" function
9386 */
9387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009388f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009389{
9390 char_u *cmd = get_tv_string_chk(&argvars[0]);
9391 garray_T ga;
9392
9393 called_emsg = FALSE;
9394 suppress_errthrow = TRUE;
9395 emsg_silent = TRUE;
9396 do_cmdline_cmd(cmd);
9397 if (!called_emsg)
9398 {
9399 prepare_assert_error(&ga);
9400 ga_concat(&ga, (char_u *)"command did not fail: ");
9401 ga_concat(&ga, cmd);
9402 assert_error(&ga);
9403 ga_clear(&ga);
9404 }
9405 else if (argvars[1].v_type != VAR_UNKNOWN)
9406 {
9407 char_u buf[NUMBUFLEN];
9408 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9409
9410 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9411 {
9412 prepare_assert_error(&ga);
9413 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9414 &vimvars[VV_ERRMSG].vv_tv);
9415 assert_error(&ga);
9416 ga_clear(&ga);
9417 }
9418 }
9419
9420 called_emsg = FALSE;
9421 suppress_errthrow = FALSE;
9422 emsg_silent = FALSE;
9423 emsg_on_display = FALSE;
9424 set_vim_var_string(VV_ERRMSG, NULL, 0);
9425}
9426
9427/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009428 * Common for assert_true() and assert_false().
9429 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009431assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009432{
9433 int error = FALSE;
9434 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009435
Bram Moolenaar37127922016-02-06 20:29:28 +01009436 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009437 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009438 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009439 if (argvars[0].v_type != VAR_NUMBER
9440 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9441 || error)
9442 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009443 prepare_assert_error(&ga);
9444 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009445 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009446 NULL, &argvars[0]);
9447 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009448 ga_clear(&ga);
9449 }
9450}
9451
9452/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009453 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009454 */
9455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009456f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009457{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009458 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009459}
9460
9461/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009462 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009463 */
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009466{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009467 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009468}
9469
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009470#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009471/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009472 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009473 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009475f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009476{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009477 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009478
9479 rettv->v_type = VAR_FLOAT;
9480 if (get_float_arg(argvars, &f) == OK)
9481 rettv->vval.v_float = asin(f);
9482 else
9483 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009484}
9485
9486/*
9487 * "atan()" function
9488 */
9489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009490f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009491{
9492 float_T f;
9493
9494 rettv->v_type = VAR_FLOAT;
9495 if (get_float_arg(argvars, &f) == OK)
9496 rettv->vval.v_float = atan(f);
9497 else
9498 rettv->vval.v_float = 0.0;
9499}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009500
9501/*
9502 * "atan2()" function
9503 */
9504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009505f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009506{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009507 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009508
9509 rettv->v_type = VAR_FLOAT;
9510 if (get_float_arg(argvars, &fx) == OK
9511 && get_float_arg(&argvars[1], &fy) == OK)
9512 rettv->vval.v_float = atan2(fx, fy);
9513 else
9514 rettv->vval.v_float = 0.0;
9515}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009516#endif
9517
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518/*
9519 * "browse(save, title, initdir, default)" function
9520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009522f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523{
9524#ifdef FEAT_BROWSE
9525 int save;
9526 char_u *title;
9527 char_u *initdir;
9528 char_u *defname;
9529 char_u buf[NUMBUFLEN];
9530 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 save = get_tv_number_chk(&argvars[0], &error);
9534 title = get_tv_string_chk(&argvars[1]);
9535 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9536 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 if (error || title == NULL || initdir == NULL || defname == NULL)
9539 rettv->vval.v_string = NULL;
9540 else
9541 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009542 do_browse(save ? BROWSE_SAVE : 0,
9543 title, defname, NULL, initdir, NULL, curbuf);
9544#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009546#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009548}
9549
9550/*
9551 * "browsedir(title, initdir)" function
9552 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009554f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009555{
9556#ifdef FEAT_BROWSE
9557 char_u *title;
9558 char_u *initdir;
9559 char_u buf[NUMBUFLEN];
9560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009561 title = get_tv_string_chk(&argvars[0]);
9562 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 if (title == NULL || initdir == NULL)
9565 rettv->vval.v_string = NULL;
9566 else
9567 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009568 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573}
9574
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009575static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009576
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577/*
9578 * Find a buffer by number or exact name.
9579 */
9580 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009581find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582{
9583 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009585 if (avar->v_type == VAR_NUMBER)
9586 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009587 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009589 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009590 if (buf == NULL)
9591 {
9592 /* No full path name match, try a match with a URL or a "nofile"
9593 * buffer, these don't use the full path. */
9594 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9595 if (buf->b_fname != NULL
9596 && (path_with_url(buf->b_fname)
9597#ifdef FEAT_QUICKFIX
9598 || bt_nofile(buf)
9599#endif
9600 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009601 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009602 break;
9603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 }
9605 return buf;
9606}
9607
9608/*
9609 * "bufexists(expr)" function
9610 */
9611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009612f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615}
9616
9617/*
9618 * "buflisted(expr)" function
9619 */
9620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009621f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622{
9623 buf_T *buf;
9624
9625 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009626 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627}
9628
9629/*
9630 * "bufloaded(expr)" function
9631 */
9632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009633f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634{
9635 buf_T *buf;
9636
9637 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639}
9640
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009641static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009642
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643/*
9644 * Get buffer by number or pattern.
9645 */
9646 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009647get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 int save_magic;
9651 char_u *save_cpo;
9652 buf_T *buf;
9653
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009654 if (tv->v_type == VAR_NUMBER)
9655 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009656 if (tv->v_type != VAR_STRING)
9657 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 if (name == NULL || *name == NUL)
9659 return curbuf;
9660 if (name[0] == '$' && name[1] == NUL)
9661 return lastbuf;
9662
9663 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9664 save_magic = p_magic;
9665 p_magic = TRUE;
9666 save_cpo = p_cpo;
9667 p_cpo = (char_u *)"";
9668
9669 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009670 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671
9672 p_magic = save_magic;
9673 p_cpo = save_cpo;
9674
9675 /* If not found, try expanding the name, like done for bufexists(). */
9676 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009677 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678
9679 return buf;
9680}
9681
9682/*
9683 * "bufname(expr)" function
9684 */
9685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009686f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687{
9688 buf_T *buf;
9689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009690 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009692 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009693 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 --emsg_off;
9699}
9700
9701/*
9702 * "bufnr(expr)" function
9703 */
9704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009705f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009708 int error = FALSE;
9709 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009711 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009713 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009714 --emsg_off;
9715
9716 /* If the buffer isn't found and the second argument is not zero create a
9717 * new buffer. */
9718 if (buf == NULL
9719 && argvars[1].v_type != VAR_UNKNOWN
9720 && get_tv_number_chk(&argvars[1], &error) != 0
9721 && !error
9722 && (name = get_tv_string_chk(&argvars[0])) != NULL
9723 && !error)
9724 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9725
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730}
9731
9732/*
9733 * "bufwinnr(nr)" function
9734 */
9735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009736f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737{
9738#ifdef FEAT_WINDOWS
9739 win_T *wp;
9740 int winnr = 0;
9741#endif
9742 buf_T *buf;
9743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009746 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747#ifdef FEAT_WINDOWS
9748 for (wp = firstwin; wp; wp = wp->w_next)
9749 {
9750 ++winnr;
9751 if (wp->w_buffer == buf)
9752 break;
9753 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757#endif
9758 --emsg_off;
9759}
9760
9761/*
9762 * "byte2line(byte)" function
9763 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009765f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
9767#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769#else
9770 long boff = 0;
9771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 (linenr_T)0, &boff);
9778#endif
9779}
9780
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009782byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009783{
9784#ifdef FEAT_MBYTE
9785 char_u *t;
9786#endif
9787 char_u *str;
9788 long idx;
9789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790 str = get_tv_string_chk(&argvars[0]);
9791 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009793 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009794 return;
9795
9796#ifdef FEAT_MBYTE
9797 t = str;
9798 for ( ; idx > 0; idx--)
9799 {
9800 if (*t == NUL) /* EOL reached */
9801 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009802 if (enc_utf8 && comp)
9803 t += utf_ptr2len(t);
9804 else
9805 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009806 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009807 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009808#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009809 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009811#endif
9812}
9813
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009814/*
9815 * "byteidx()" function
9816 */
9817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009818f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009819{
9820 byteidx(argvars, rettv, FALSE);
9821}
9822
9823/*
9824 * "byteidxcomp()" function
9825 */
9826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009827f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009828{
9829 byteidx(argvars, rettv, TRUE);
9830}
9831
Bram Moolenaardb913952012-06-29 12:54:53 +02009832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009833func_call(
9834 char_u *name,
9835 typval_T *args,
9836 dict_T *selfdict,
9837 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009838{
9839 listitem_T *item;
9840 typval_T argv[MAX_FUNC_ARGS + 1];
9841 int argc = 0;
9842 int dummy;
9843 int r = 0;
9844
9845 for (item = args->vval.v_list->lv_first; item != NULL;
9846 item = item->li_next)
9847 {
9848 if (argc == MAX_FUNC_ARGS)
9849 {
9850 EMSG(_("E699: Too many arguments"));
9851 break;
9852 }
9853 /* Make a copy of each argument. This is needed to be able to set
9854 * v_lock to VAR_FIXED in the copy without changing the original list.
9855 */
9856 copy_tv(&item->li_tv, &argv[argc++]);
9857 }
9858
9859 if (item == NULL)
9860 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9861 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9862 &dummy, TRUE, selfdict);
9863
9864 /* Free the arguments. */
9865 while (argc > 0)
9866 clear_tv(&argv[--argc]);
9867
9868 return r;
9869}
9870
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009871/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009872 * "call(func, arglist)" function
9873 */
9874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009875f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009876{
9877 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009878 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009879
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009880 if (argvars[1].v_type != VAR_LIST)
9881 {
9882 EMSG(_(e_listreq));
9883 return;
9884 }
9885 if (argvars[1].vval.v_list == NULL)
9886 return;
9887
9888 if (argvars[0].v_type == VAR_FUNC)
9889 func = argvars[0].vval.v_string;
9890 else
9891 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009892 if (*func == NUL)
9893 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009894
Bram Moolenaare9a41262005-01-15 22:18:47 +00009895 if (argvars[2].v_type != VAR_UNKNOWN)
9896 {
9897 if (argvars[2].v_type != VAR_DICT)
9898 {
9899 EMSG(_(e_dictreq));
9900 return;
9901 }
9902 selfdict = argvars[2].vval.v_dict;
9903 }
9904
Bram Moolenaardb913952012-06-29 12:54:53 +02009905 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009906}
9907
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009908#ifdef FEAT_FLOAT
9909/*
9910 * "ceil({float})" function
9911 */
9912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009913f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009914{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009915 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009916
9917 rettv->v_type = VAR_FLOAT;
9918 if (get_float_arg(argvars, &f) == OK)
9919 rettv->vval.v_float = ceil(f);
9920 else
9921 rettv->vval.v_float = 0.0;
9922}
9923#endif
9924
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009925#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009926/*
9927 * Get a callback from "arg". It can be a Funcref or a function name.
9928 * When "arg" is zero return an empty string.
9929 * Return NULL for an invalid argument.
9930 */
9931 static char_u *
9932get_callback(typval_T *arg)
9933{
9934 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9935 return arg->vval.v_string;
9936 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9937 return (char_u *)"";
9938 EMSG(_("E999: Invalid callback argument"));
9939 return NULL;
9940}
9941
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009942 static int
9943handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9944{
9945 char_u *val = get_tv_string(item);
9946
9947 opt->jo_set |= jo;
9948 if (STRCMP(val, "nl") == 0)
9949 *modep = MODE_NL;
9950 else if (STRCMP(val, "raw") == 0)
9951 *modep = MODE_RAW;
9952 else if (STRCMP(val, "js") == 0)
9953 *modep = MODE_JS;
9954 else if (STRCMP(val, "json") == 0)
9955 *modep = MODE_JSON;
9956 else
9957 {
9958 EMSG2(_(e_invarg2), val);
9959 return FAIL;
9960 }
9961 return OK;
9962}
9963
9964 static void
9965clear_job_options(jobopt_T *opt)
9966{
9967 vim_memset(opt, 0, sizeof(jobopt_T));
9968}
9969
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009970/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009971 * Get the option entries from the dict in "tv", parse them and put the result
9972 * in "opt".
9973 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009974 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009975 */
9976 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009977get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009978{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009979 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01009980 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009981 dict_T *dict;
9982 int todo;
9983 hashitem_T *hi;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009984
Bram Moolenaar8600ace2016-02-19 23:31:40 +01009985 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009986 if (tv->v_type == VAR_UNKNOWN)
9987 return OK;
9988 if (tv->v_type != VAR_DICT)
9989 {
9990 EMSG(_(e_invarg));
9991 return FAIL;
9992 }
9993 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009994 if (dict == NULL)
9995 return OK;
9996
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009997 todo = (int)dict->dv_hashtab.ht_used;
9998 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
9999 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010000 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010001 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010002
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010003 if (STRCMP(hi->hi_key, "mode") == 0)
10004 {
10005 if (!(supported & JO_MODE))
10006 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010007 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010008 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010009 }
10010 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10011 {
10012 if (!(supported & JO_IN_MODE))
10013 break;
10014 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10015 == FAIL)
10016 return FAIL;
10017 }
10018 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10019 {
10020 if (!(supported & JO_OUT_MODE))
10021 break;
10022 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10023 == FAIL)
10024 return FAIL;
10025 }
10026 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10027 {
10028 if (!(supported & JO_ERR_MODE))
10029 break;
10030 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10031 == FAIL)
10032 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010033 }
10034 else if (STRCMP(hi->hi_key, "callback") == 0)
10035 {
10036 if (!(supported & JO_CALLBACK))
10037 break;
10038 opt->jo_set |= JO_CALLBACK;
10039 opt->jo_callback = get_callback(item);
10040 if (opt->jo_callback == NULL)
10041 {
10042 EMSG2(_(e_invarg2), "callback");
10043 return FAIL;
10044 }
10045 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010046 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10047 {
10048 if (!(supported & JO_OUT_CALLBACK))
10049 break;
10050 opt->jo_set |= JO_OUT_CALLBACK;
10051 opt->jo_out_cb = get_callback(item);
10052 if (opt->jo_out_cb == NULL)
10053 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010054 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010055 return FAIL;
10056 }
10057 }
10058 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10059 {
10060 if (!(supported & JO_ERR_CALLBACK))
10061 break;
10062 opt->jo_set |= JO_ERR_CALLBACK;
10063 opt->jo_err_cb = get_callback(item);
10064 if (opt->jo_err_cb == NULL)
10065 {
10066 EMSG2(_(e_invarg2), "err-cb");
10067 return FAIL;
10068 }
10069 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010070 else if (STRCMP(hi->hi_key, "waittime") == 0)
10071 {
10072 if (!(supported & JO_WAITTIME))
10073 break;
10074 opt->jo_set |= JO_WAITTIME;
10075 opt->jo_waittime = get_tv_number(item);
10076 }
10077 else if (STRCMP(hi->hi_key, "timeout") == 0)
10078 {
10079 if (!(supported & JO_TIMEOUT))
10080 break;
10081 opt->jo_set |= JO_TIMEOUT;
10082 opt->jo_timeout = get_tv_number(item);
10083 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010084 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10085 {
10086 if (!(supported & JO_OUT_TIMEOUT))
10087 break;
10088 opt->jo_set |= JO_OUT_TIMEOUT;
10089 opt->jo_out_timeout = get_tv_number(item);
10090 }
10091 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10092 {
10093 if (!(supported & JO_ERR_TIMEOUT))
10094 break;
10095 opt->jo_set |= JO_ERR_TIMEOUT;
10096 opt->jo_err_timeout = get_tv_number(item);
10097 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010098 else if (STRCMP(hi->hi_key, "part") == 0)
10099 {
10100 if (!(supported & JO_PART))
10101 break;
10102 opt->jo_set |= JO_PART;
10103 val = get_tv_string(item);
10104 if (STRCMP(val, "err") == 0)
10105 opt->jo_part = PART_ERR;
10106 else
10107 {
10108 EMSG2(_(e_invarg2), val);
10109 return FAIL;
10110 }
10111 }
10112 else if (STRCMP(hi->hi_key, "id") == 0)
10113 {
10114 if (!(supported & JO_ID))
10115 break;
10116 opt->jo_set |= JO_ID;
10117 opt->jo_id = get_tv_number(item);
10118 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010119 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10120 {
10121 if (!(supported & JO_STOPONEXIT))
10122 break;
10123 opt->jo_set |= JO_STOPONEXIT;
10124 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10125 opt->jo_soe_buf);
10126 if (opt->jo_stoponexit == NULL)
10127 {
10128 EMSG2(_(e_invarg2), "stoponexit");
10129 return FAIL;
10130 }
10131 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010132 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10133 {
10134 if (!(supported & JO_EXIT_CB))
10135 break;
10136 opt->jo_set |= JO_EXIT_CB;
10137 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010138 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010139 {
10140 EMSG2(_(e_invarg2), "exit-cb");
10141 return FAIL;
10142 }
10143 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010144 else
10145 break;
10146 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010147 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010148 if (todo > 0)
10149 {
10150 EMSG2(_(e_invarg2), hi->hi_key);
10151 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010152 }
10153
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010154 return OK;
10155}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010156#endif
10157
10158#ifdef FEAT_CHANNEL
10159/*
10160 * Get the channel from the argument.
10161 * Returns NULL if the handle is invalid.
10162 */
10163 static channel_T *
10164get_channel_arg(typval_T *tv)
10165{
10166 channel_T *channel;
10167
10168 if (tv->v_type != VAR_CHANNEL)
10169 {
10170 EMSG2(_(e_invarg2), get_tv_string(tv));
10171 return NULL;
10172 }
10173 channel = tv->vval.v_channel;
10174
10175 if (channel == NULL || !channel_is_open(channel))
10176 {
10177 EMSG(_("E906: not an open channel"));
10178 return NULL;
10179 }
10180 return channel;
10181}
10182
10183/*
10184 * "ch_close()" function
10185 */
10186 static void
10187f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10188{
10189 channel_T *channel = get_channel_arg(&argvars[0]);
10190
10191 if (channel != NULL)
10192 channel_close(channel);
10193}
10194
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010195# ifdef FEAT_JOB
10196/*
10197 * "ch_getjob()" function
10198 */
10199 static void
10200f_ch_getjob(typval_T *argvars, typval_T *rettv)
10201{
10202 channel_T *channel = get_channel_arg(&argvars[0]);
10203
10204 if (channel != NULL)
10205 {
10206 rettv->v_type = VAR_JOB;
10207 rettv->vval.v_job = channel->ch_job;
10208 if (channel->ch_job != NULL)
10209 ++channel->ch_job->jv_refcount;
10210 }
10211}
10212# endif
10213
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010214/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010215 * "ch_log()" function
10216 */
10217 static void
10218f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10219{
10220 char_u *msg = get_tv_string(&argvars[0]);
10221 channel_T *channel = NULL;
10222
10223 if (argvars[1].v_type != VAR_UNKNOWN)
10224 channel = get_channel_arg(&argvars[1]);
10225
10226 ch_log(channel, (char *)msg);
10227}
10228
10229/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010230 * "ch_logfile()" function
10231 */
10232 static void
10233f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10234{
10235 char_u *fname;
10236 char_u *opt = (char_u *)"";
10237 char_u buf[NUMBUFLEN];
10238 FILE *file = NULL;
10239
10240 fname = get_tv_string(&argvars[0]);
10241 if (argvars[1].v_type == VAR_STRING)
10242 opt = get_tv_string_buf(&argvars[1], buf);
10243 if (*fname != NUL)
10244 {
10245 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10246 if (file == NULL)
10247 {
10248 EMSG2(_(e_notopen), fname);
10249 return;
10250 }
10251 }
10252 ch_logfile(file);
10253}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010254
10255/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010256 * "ch_open()" function
10257 */
10258 static void
10259f_ch_open(typval_T *argvars, typval_T *rettv)
10260{
10261 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010262 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010263 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010264 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010265 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010266 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010267
10268 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010269 rettv->v_type = VAR_CHANNEL;
10270 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010271
10272 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010273 if (argvars[1].v_type != VAR_UNKNOWN
10274 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010275 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010276 EMSG(_(e_invarg));
10277 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010278 }
10279
10280 /* parse address */
10281 p = vim_strchr(address, ':');
10282 if (p == NULL)
10283 {
10284 EMSG2(_(e_invarg2), address);
10285 return;
10286 }
10287 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010288 port = strtol((char *)p, &rest, 10);
10289 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010290 {
10291 p[-1] = ':';
10292 EMSG2(_(e_invarg2), address);
10293 return;
10294 }
10295
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010296 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010297 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010298 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010299 opt.jo_timeout = 2000;
10300 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010301 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010302 return;
10303 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010304 {
10305 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010306 return;
10307 }
10308
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010309 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010310 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010311 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010312 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010313 opt.jo_set = JO_ALL;
10314 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010315 }
10316}
10317
10318/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010319 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010320 */
10321 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010322common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010323{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010324 channel_T *channel;
10325 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010326 jobopt_T opt;
10327 int mode;
10328 int timeout;
10329 int id = -1;
10330 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010331
10332 /* return an empty string by default */
10333 rettv->v_type = VAR_STRING;
10334 rettv->vval.v_string = NULL;
10335
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010336 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010337 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10338 == FAIL)
10339 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010340
Bram Moolenaar77073442016-02-13 23:23:53 +010010341 channel = get_channel_arg(&argvars[0]);
10342 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010343 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010344 if (opt.jo_set & JO_PART)
10345 part = opt.jo_part;
10346 else
10347 part = channel_part_read(channel);
10348 mode = channel_get_mode(channel, part);
10349 timeout = channel_get_timeout(channel, part);
10350 if (opt.jo_set & JO_TIMEOUT)
10351 timeout = opt.jo_timeout;
10352
10353 if (raw || mode == MODE_RAW || mode == MODE_NL)
10354 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10355 else
10356 {
10357 if (opt.jo_set & JO_ID)
10358 id = opt.jo_id;
10359 channel_read_json_block(channel, part, timeout, id, &listtv);
10360 if (listtv != NULL)
10361 *rettv = *listtv;
10362 else
10363 {
10364 rettv->v_type = VAR_SPECIAL;
10365 rettv->vval.v_number = VVAL_NONE;
10366 }
10367 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010368 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010369}
10370
10371/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010372 * "ch_read()" function
10373 */
10374 static void
10375f_ch_read(typval_T *argvars, typval_T *rettv)
10376{
10377 common_channel_read(argvars, rettv, FALSE);
10378}
10379
10380/*
10381 * "ch_readraw()" function
10382 */
10383 static void
10384f_ch_readraw(typval_T *argvars, typval_T *rettv)
10385{
10386 common_channel_read(argvars, rettv, TRUE);
10387}
10388
10389/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010390 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010391 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010392 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010393 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010394 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010395 static channel_T *
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010396send_common(typval_T *argvars, char_u *text, int id, char *fun, int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010397{
Bram Moolenaar77073442016-02-13 23:23:53 +010010398 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010399 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010400 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010401
Bram Moolenaar77073442016-02-13 23:23:53 +010010402 channel = get_channel_arg(&argvars[0]);
10403 if (channel == NULL)
10404 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010405 part_send = channel_part_send(channel);
10406 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010407
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010408 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010409 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10410 return NULL;
10411
Bram Moolenaara07fec92016-02-05 21:04:08 +010010412 /* Set the callback. An empty callback means no callback and not reading
10413 * the response. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010414 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010415 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010416
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010417 if (channel_send(channel, part_send, text, fun) == OK
10418 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010419 return channel;
10420 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010421}
10422
10423/*
10424 * "ch_sendexpr()" function
10425 */
10426 static void
10427f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10428{
10429 char_u *text;
10430 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010431 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010432 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010433 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010434 int part_send;
10435 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010436 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010437
10438 /* return an empty string by default */
10439 rettv->v_type = VAR_STRING;
10440 rettv->vval.v_string = NULL;
10441
Bram Moolenaar77073442016-02-13 23:23:53 +010010442 channel = get_channel_arg(&argvars[0]);
10443 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010444 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010445 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010446
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010447 ch_mode = channel_get_mode(channel, part_send);
10448 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010449 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010450 EMSG(_("E912: cannot use ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010451 return;
10452 }
10453
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010454 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010455 text = json_encode_nr_expr(id, &argvars[1],
10456 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010457 if (text == NULL)
10458 return;
10459
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010460 channel = send_common(argvars, text, id, "sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010461 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010462 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010463 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010464 /* TODO: timeout from options */
10465 timeout = channel_get_timeout(channel, part_read);
10466 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10467 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010468 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010469 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010470
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010471 /* Move the item from the list and then change the type to
10472 * avoid the value being freed. */
10473 *rettv = list->lv_last->li_tv;
10474 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010475 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010476 }
10477 }
10478}
10479
10480/*
10481 * "ch_sendraw()" function
10482 */
10483 static void
10484f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10485{
10486 char_u buf[NUMBUFLEN];
10487 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010488 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010489 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010490 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010491
10492 /* return an empty string by default */
10493 rettv->v_type = VAR_STRING;
10494 rettv->vval.v_string = NULL;
10495
10496 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010497 channel = send_common(argvars, text, 0, "sendraw", &part_read);
Bram Moolenaar77073442016-02-13 23:23:53 +010010498 if (channel != NULL)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010499 {
10500 /* TODO: timeout from options */
10501 timeout = channel_get_timeout(channel, part_read);
10502 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10503 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010504}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010505
10506/*
10507 * "ch_setoptions()" function
10508 */
10509 static void
10510f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10511{
10512 channel_T *channel;
10513 jobopt_T opt;
10514
10515 channel = get_channel_arg(&argvars[0]);
10516 if (channel == NULL)
10517 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010518 clear_job_options(&opt);
10519 if (get_job_options(&argvars[1], &opt,
10520 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010521 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010522 channel_set_options(channel, &opt);
10523}
10524
10525/*
10526 * "ch_status()" function
10527 */
10528 static void
10529f_ch_status(typval_T *argvars, typval_T *rettv)
10530{
10531 /* return an empty string by default */
10532 rettv->v_type = VAR_STRING;
10533
10534 if (argvars[0].v_type != VAR_CHANNEL)
10535 {
10536 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10537 rettv->vval.v_string = NULL;
10538 }
10539 else
10540 rettv->vval.v_string = vim_strsave(
10541 (char_u *)channel_status(argvars[0].vval.v_channel));
10542}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010543#endif
10544
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010545/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010546 * "changenr()" function
10547 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010549f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010550{
10551 rettv->vval.v_number = curbuf->b_u_seq_cur;
10552}
10553
10554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 * "char2nr(string)" function
10556 */
10557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010558f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559{
10560#ifdef FEAT_MBYTE
10561 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010562 {
10563 int utf8 = 0;
10564
10565 if (argvars[1].v_type != VAR_UNKNOWN)
10566 utf8 = get_tv_number_chk(&argvars[1], NULL);
10567
10568 if (utf8)
10569 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10570 else
10571 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 else
10574#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576}
10577
10578/*
10579 * "cindent(lnum)" function
10580 */
10581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010582f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583{
10584#ifdef FEAT_CINDENT
10585 pos_T pos;
10586 linenr_T lnum;
10587
10588 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010589 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10591 {
10592 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 curwin->w_cursor = pos;
10595 }
10596 else
10597#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599}
10600
10601/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010602 * "clearmatches()" function
10603 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010605f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010606{
10607#ifdef FEAT_SEARCH_EXTRA
10608 clear_matches(curwin);
10609#endif
10610}
10611
10612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 * "col(string)" function
10614 */
10615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010616f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617{
10618 colnr_T col = 0;
10619 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010620 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010621
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010622 fp = var2fpos(&argvars[0], FALSE, &fnum);
10623 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624 {
10625 if (fp->col == MAXCOL)
10626 {
10627 /* '> can be MAXCOL, get the length of the line then */
10628 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010629 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 else
10631 col = MAXCOL;
10632 }
10633 else
10634 {
10635 col = fp->col + 1;
10636#ifdef FEAT_VIRTUALEDIT
10637 /* col(".") when the cursor is on the NUL at the end of the line
10638 * because of "coladd" can be seen as an extra column. */
10639 if (virtual_active() && fp == &curwin->w_cursor)
10640 {
10641 char_u *p = ml_get_cursor();
10642
10643 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10644 curwin->w_virtcol - curwin->w_cursor.coladd))
10645 {
10646# ifdef FEAT_MBYTE
10647 int l;
10648
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010649 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 col += l;
10651# else
10652 if (*p != NUL && p[1] == NUL)
10653 ++col;
10654# endif
10655 }
10656 }
10657#endif
10658 }
10659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010660 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661}
10662
Bram Moolenaar572cb562005-08-05 21:35:02 +000010663#if defined(FEAT_INS_EXPAND)
10664/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010665 * "complete()" function
10666 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010668f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010669{
10670 int startcol;
10671
10672 if ((State & INSERT) == 0)
10673 {
10674 EMSG(_("E785: complete() can only be used in Insert mode"));
10675 return;
10676 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010677
10678 /* Check for undo allowed here, because if something was already inserted
10679 * the line was already saved for undo and this check isn't done. */
10680 if (!undo_allowed())
10681 return;
10682
Bram Moolenaarade00832006-03-10 21:46:58 +000010683 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10684 {
10685 EMSG(_(e_invarg));
10686 return;
10687 }
10688
10689 startcol = get_tv_number_chk(&argvars[0], NULL);
10690 if (startcol <= 0)
10691 return;
10692
10693 set_completion(startcol - 1, argvars[1].vval.v_list);
10694}
10695
10696/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010697 * "complete_add()" function
10698 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010700f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010701{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010702 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010703}
10704
10705/*
10706 * "complete_check()" function
10707 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010709f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010710{
10711 int saved = RedrawingDisabled;
10712
10713 RedrawingDisabled = 0;
10714 ins_compl_check_keys(0);
10715 rettv->vval.v_number = compl_interrupted;
10716 RedrawingDisabled = saved;
10717}
10718#endif
10719
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720/*
10721 * "confirm(message, buttons[, default [, type]])" function
10722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010724f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725{
10726#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10727 char_u *message;
10728 char_u *buttons = NULL;
10729 char_u buf[NUMBUFLEN];
10730 char_u buf2[NUMBUFLEN];
10731 int def = 1;
10732 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010733 char_u *typestr;
10734 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010736 message = get_tv_string_chk(&argvars[0]);
10737 if (message == NULL)
10738 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010739 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10742 if (buttons == NULL)
10743 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010744 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010747 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010749 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10750 if (typestr == NULL)
10751 error = TRUE;
10752 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 switch (TOUPPER_ASC(*typestr))
10755 {
10756 case 'E': type = VIM_ERROR; break;
10757 case 'Q': type = VIM_QUESTION; break;
10758 case 'I': type = VIM_INFO; break;
10759 case 'W': type = VIM_WARNING; break;
10760 case 'G': type = VIM_GENERIC; break;
10761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 }
10763 }
10764 }
10765 }
10766
10767 if (buttons == NULL || *buttons == NUL)
10768 buttons = (char_u *)_("&Ok");
10769
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010770 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010772 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773#endif
10774}
10775
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010776/*
10777 * "copy()" function
10778 */
10779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010780f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010781{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010782 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010783}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010784
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010785#ifdef FEAT_FLOAT
10786/*
10787 * "cos()" function
10788 */
10789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010790f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010791{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010792 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010793
10794 rettv->v_type = VAR_FLOAT;
10795 if (get_float_arg(argvars, &f) == OK)
10796 rettv->vval.v_float = cos(f);
10797 else
10798 rettv->vval.v_float = 0.0;
10799}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010800
10801/*
10802 * "cosh()" function
10803 */
10804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010805f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010806{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010807 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010808
10809 rettv->v_type = VAR_FLOAT;
10810 if (get_float_arg(argvars, &f) == OK)
10811 rettv->vval.v_float = cosh(f);
10812 else
10813 rettv->vval.v_float = 0.0;
10814}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010815#endif
10816
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010818 * "count()" function
10819 */
10820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010821f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010822{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010823 long n = 0;
10824 int ic = FALSE;
10825
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010827 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010828 listitem_T *li;
10829 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010830 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010831
Bram Moolenaare9a41262005-01-15 22:18:47 +000010832 if ((l = argvars[0].vval.v_list) != NULL)
10833 {
10834 li = l->lv_first;
10835 if (argvars[2].v_type != VAR_UNKNOWN)
10836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010837 int error = FALSE;
10838
10839 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010840 if (argvars[3].v_type != VAR_UNKNOWN)
10841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 idx = get_tv_number_chk(&argvars[3], &error);
10843 if (!error)
10844 {
10845 li = list_find(l, idx);
10846 if (li == NULL)
10847 EMSGN(_(e_listidx), idx);
10848 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010849 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010850 if (error)
10851 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010852 }
10853
10854 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010855 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010856 ++n;
10857 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010859 else if (argvars[0].v_type == VAR_DICT)
10860 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010861 int todo;
10862 dict_T *d;
10863 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010865 if ((d = argvars[0].vval.v_dict) != NULL)
10866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010867 int error = FALSE;
10868
Bram Moolenaare9a41262005-01-15 22:18:47 +000010869 if (argvars[2].v_type != VAR_UNKNOWN)
10870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010872 if (argvars[3].v_type != VAR_UNKNOWN)
10873 EMSG(_(e_invarg));
10874 }
10875
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010876 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010877 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010878 {
10879 if (!HASHITEM_EMPTY(hi))
10880 {
10881 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010882 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010883 ++n;
10884 }
10885 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010886 }
10887 }
10888 else
10889 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010890 rettv->vval.v_number = n;
10891}
10892
10893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10895 *
10896 * Checks the existence of a cscope connection.
10897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010899f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901#ifdef FEAT_CSCOPE
10902 int num = 0;
10903 char_u *dbpath = NULL;
10904 char_u *prepend = NULL;
10905 char_u buf[NUMBUFLEN];
10906
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010907 if (argvars[0].v_type != VAR_UNKNOWN
10908 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010910 num = (int)get_tv_number(&argvars[0]);
10911 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010912 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 }
10915
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010916 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917#endif
10918}
10919
10920/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010921 * "cursor(lnum, col)" function, or
10922 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010924 * Moves the cursor to the specified line and column.
10925 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010928f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929{
10930 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010931#ifdef FEAT_VIRTUALEDIT
10932 long coladd = 0;
10933#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010934 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010936 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010937 if (argvars[1].v_type == VAR_UNKNOWN)
10938 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010939 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010940 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010941
Bram Moolenaar493c1782014-05-28 14:34:46 +020010942 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010943 {
10944 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010945 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010946 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010947 line = pos.lnum;
10948 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010949#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010950 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010951#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010952 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010953 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010954 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010955 set_curswant = FALSE;
10956 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010957 }
10958 else
10959 {
10960 line = get_tv_lnum(argvars);
10961 col = get_tv_number_chk(&argvars[1], NULL);
10962#ifdef FEAT_VIRTUALEDIT
10963 if (argvars[2].v_type != VAR_UNKNOWN)
10964 coladd = get_tv_number_chk(&argvars[2], NULL);
10965#endif
10966 }
10967 if (line < 0 || col < 0
10968#ifdef FEAT_VIRTUALEDIT
10969 || coladd < 0
10970#endif
10971 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 if (line > 0)
10974 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 if (col > 0)
10976 curwin->w_cursor.col = col - 1;
10977#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010978 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979#endif
10980
10981 /* Make sure the cursor is in a valid position. */
10982 check_cursor();
10983#ifdef FEAT_MBYTE
10984 /* Correct cursor for multi-byte character. */
10985 if (has_mbyte)
10986 mb_adjust_cursor();
10987#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010988
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010989 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010990 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991}
10992
10993/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010994 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 */
10996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010997f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010999 int noref = 0;
11000
11001 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011002 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011003 if (noref < 0 || noref > 1)
11004 EMSG(_(e_invarg));
11005 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011006 {
11007 current_copyID += COPYID_INC;
11008 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010}
11011
11012/*
11013 * "delete()" function
11014 */
11015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011016f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011018 char_u nbuf[NUMBUFLEN];
11019 char_u *name;
11020 char_u *flags;
11021
11022 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011024 return;
11025
11026 name = get_tv_string(&argvars[0]);
11027 if (name == NULL || *name == NUL)
11028 {
11029 EMSG(_(e_invarg));
11030 return;
11031 }
11032
11033 if (argvars[1].v_type != VAR_UNKNOWN)
11034 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011036 flags = (char_u *)"";
11037
11038 if (*flags == NUL)
11039 /* delete a file */
11040 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11041 else if (STRCMP(flags, "d") == 0)
11042 /* delete an empty directory */
11043 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11044 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011045 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011046 rettv->vval.v_number = delete_recursive(name);
11047 else
11048 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049}
11050
11051/*
11052 * "did_filetype()" function
11053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011055f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056{
11057#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059#endif
11060}
11061
11062/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011063 * "diff_filler()" function
11064 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011066f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011067{
11068#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011069 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011070#endif
11071}
11072
11073/*
11074 * "diff_hlID()" function
11075 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011077f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011078{
11079#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011081 static linenr_T prev_lnum = 0;
11082 static int changedtick = 0;
11083 static int fnum = 0;
11084 static int change_start = 0;
11085 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011086 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011087 int filler_lines;
11088 int col;
11089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011090 if (lnum < 0) /* ignore type error in {lnum} arg */
11091 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011092 if (lnum != prev_lnum
11093 || changedtick != curbuf->b_changedtick
11094 || fnum != curbuf->b_fnum)
11095 {
11096 /* New line, buffer, change: need to get the values. */
11097 filler_lines = diff_check(curwin, lnum);
11098 if (filler_lines < 0)
11099 {
11100 if (filler_lines == -1)
11101 {
11102 change_start = MAXCOL;
11103 change_end = -1;
11104 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11105 hlID = HLF_ADD; /* added line */
11106 else
11107 hlID = HLF_CHD; /* changed line */
11108 }
11109 else
11110 hlID = HLF_ADD; /* added line */
11111 }
11112 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011113 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011114 prev_lnum = lnum;
11115 changedtick = curbuf->b_changedtick;
11116 fnum = curbuf->b_fnum;
11117 }
11118
11119 if (hlID == HLF_CHD || hlID == HLF_TXD)
11120 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011121 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011122 if (col >= change_start && col <= change_end)
11123 hlID = HLF_TXD; /* changed text */
11124 else
11125 hlID = HLF_CHD; /* changed line */
11126 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011127 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011128#endif
11129}
11130
11131/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011132 * "disable_char_avail_for_testing({expr})" function
11133 */
11134 static void
11135f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11136{
11137 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11138}
11139
11140/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011141 * "empty({expr})" function
11142 */
11143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011144f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011145{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011146 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011147
11148 switch (argvars[0].v_type)
11149 {
11150 case VAR_STRING:
11151 case VAR_FUNC:
11152 n = argvars[0].vval.v_string == NULL
11153 || *argvars[0].vval.v_string == NUL;
11154 break;
11155 case VAR_NUMBER:
11156 n = argvars[0].vval.v_number == 0;
11157 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011158 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011159#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011160 n = argvars[0].vval.v_float == 0.0;
11161 break;
11162#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011163 case VAR_LIST:
11164 n = argvars[0].vval.v_list == NULL
11165 || argvars[0].vval.v_list->lv_first == NULL;
11166 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011167 case VAR_DICT:
11168 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011169 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011170 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011171 case VAR_SPECIAL:
11172 n = argvars[0].vval.v_number != VVAL_TRUE;
11173 break;
11174
Bram Moolenaar835dc632016-02-07 14:27:38 +010011175 case VAR_JOB:
11176#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011177 n = argvars[0].vval.v_job == NULL
11178 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11179 break;
11180#endif
11181 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011182#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011183 n = argvars[0].vval.v_channel == NULL
11184 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011185 break;
11186#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011187 case VAR_UNKNOWN:
11188 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11189 n = TRUE;
11190 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011191 }
11192
11193 rettv->vval.v_number = n;
11194}
11195
11196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 * "escape({string}, {chars})" function
11198 */
11199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011200f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201{
11202 char_u buf[NUMBUFLEN];
11203
Bram Moolenaar758711c2005-02-02 23:11:38 +000011204 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11205 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207}
11208
11209/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011210 * "eval()" function
11211 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011213f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011214{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011215 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011217 s = get_tv_string_chk(&argvars[0]);
11218 if (s != NULL)
11219 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011220
Bram Moolenaar615b9972015-01-14 17:15:05 +010011221 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11223 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011224 if (p != NULL && !aborting())
11225 EMSG2(_(e_invexpr2), p);
11226 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011227 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011228 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011230 else if (*s != NUL)
11231 EMSG(_(e_trailing));
11232}
11233
11234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 * "eventhandler()" function
11236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011238f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011240 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241}
11242
11243/*
11244 * "executable()" function
11245 */
11246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011247f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011249 char_u *name = get_tv_string(&argvars[0]);
11250
11251 /* Check in $PATH and also check directly if there is a directory name. */
11252 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11253 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011254}
11255
11256/*
11257 * "exepath()" function
11258 */
11259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011260f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011261{
11262 char_u *p = NULL;
11263
Bram Moolenaarb5971142015-03-21 17:32:19 +010011264 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011265 rettv->v_type = VAR_STRING;
11266 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267}
11268
11269/*
11270 * "exists()" function
11271 */
11272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011273f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274{
11275 char_u *p;
11276 char_u *name;
11277 int n = FALSE;
11278 int len = 0;
11279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 if (*p == '$') /* environment variable */
11282 {
11283 /* first try "normal" environment variables (fast) */
11284 if (mch_getenv(p + 1) != NULL)
11285 n = TRUE;
11286 else
11287 {
11288 /* try expanding things like $VIM and ${HOME} */
11289 p = expand_env_save(p);
11290 if (p != NULL && *p != '$')
11291 n = TRUE;
11292 vim_free(p);
11293 }
11294 }
11295 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011297 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011298 if (*skipwhite(p) != NUL)
11299 n = FALSE; /* trailing garbage */
11300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301 else if (*p == '*') /* internal or user defined function */
11302 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011303 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 }
11305 else if (*p == ':')
11306 {
11307 n = cmd_exists(p + 1);
11308 }
11309 else if (*p == '#')
11310 {
11311#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011312 if (p[1] == '#')
11313 n = autocmd_supported(p + 2);
11314 else
11315 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316#endif
11317 }
11318 else /* internal variable */
11319 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011320 char_u *tofree;
11321 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011323 /* get_name_len() takes care of expanding curly braces */
11324 name = p;
11325 len = get_name_len(&p, &tofree, TRUE, FALSE);
11326 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011328 if (tofree != NULL)
11329 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011330 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011331 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011333 /* handle d.key, l[idx], f(expr) */
11334 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11335 if (n)
11336 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 }
11338 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011339 if (*p != NUL)
11340 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011342 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343 }
11344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011345 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346}
11347
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011348#ifdef FEAT_FLOAT
11349/*
11350 * "exp()" function
11351 */
11352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011353f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011354{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011355 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011356
11357 rettv->v_type = VAR_FLOAT;
11358 if (get_float_arg(argvars, &f) == OK)
11359 rettv->vval.v_float = exp(f);
11360 else
11361 rettv->vval.v_float = 0.0;
11362}
11363#endif
11364
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365/*
11366 * "expand()" function
11367 */
11368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011369f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370{
11371 char_u *s;
11372 int len;
11373 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011374 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011376 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011377 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011380 if (argvars[1].v_type != VAR_UNKNOWN
11381 && argvars[2].v_type != VAR_UNKNOWN
11382 && get_tv_number_chk(&argvars[2], &error)
11383 && !error)
11384 {
11385 rettv->v_type = VAR_LIST;
11386 rettv->vval.v_list = NULL;
11387 }
11388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 if (*s == '%' || *s == '#' || *s == '<')
11391 {
11392 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011393 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011395 if (rettv->v_type == VAR_LIST)
11396 {
11397 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11398 list_append_string(rettv->vval.v_list, result, -1);
11399 else
11400 vim_free(result);
11401 }
11402 else
11403 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 }
11405 else
11406 {
11407 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011408 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011409 if (argvars[1].v_type != VAR_UNKNOWN
11410 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011411 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412 if (!error)
11413 {
11414 ExpandInit(&xpc);
11415 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011416 if (p_wic)
11417 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011418 if (rettv->v_type == VAR_STRING)
11419 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11420 options, WILD_ALL);
11421 else if (rettv_list_alloc(rettv) != FAIL)
11422 {
11423 int i;
11424
11425 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11426 for (i = 0; i < xpc.xp_numfiles; i++)
11427 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11428 ExpandCleanup(&xpc);
11429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 }
11431 else
11432 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 }
11434}
11435
11436/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011437 * Go over all entries in "d2" and add them to "d1".
11438 * When "action" is "error" then a duplicate key is an error.
11439 * When "action" is "force" then a duplicate key is overwritten.
11440 * Otherwise duplicate keys are ignored ("action" is "keep").
11441 */
11442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011443dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011444{
11445 dictitem_T *di1;
11446 hashitem_T *hi2;
11447 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011448 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011449
11450 todo = (int)d2->dv_hashtab.ht_used;
11451 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11452 {
11453 if (!HASHITEM_EMPTY(hi2))
11454 {
11455 --todo;
11456 di1 = dict_find(d1, hi2->hi_key, -1);
11457 if (d1->dv_scope != 0)
11458 {
11459 /* Disallow replacing a builtin function in l: and g:.
11460 * Check the key to be valid when adding to any
11461 * scope. */
11462 if (d1->dv_scope == VAR_DEF_SCOPE
11463 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11464 && var_check_func_name(hi2->hi_key,
11465 di1 == NULL))
11466 break;
11467 if (!valid_varname(hi2->hi_key))
11468 break;
11469 }
11470 if (di1 == NULL)
11471 {
11472 di1 = dictitem_copy(HI2DI(hi2));
11473 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11474 dictitem_free(di1);
11475 }
11476 else if (*action == 'e')
11477 {
11478 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11479 break;
11480 }
11481 else if (*action == 'f' && HI2DI(hi2) != di1)
11482 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011483 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11484 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011485 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011486 clear_tv(&di1->di_tv);
11487 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11488 }
11489 }
11490 }
11491}
11492
11493/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011494 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011495 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011496 */
11497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011498f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011499{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011500 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011501
Bram Moolenaare9a41262005-01-15 22:18:47 +000011502 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011503 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011504 list_T *l1, *l2;
11505 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011506 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011507 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011508
Bram Moolenaare9a41262005-01-15 22:18:47 +000011509 l1 = argvars[0].vval.v_list;
11510 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011511 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011512 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513 {
11514 if (argvars[2].v_type != VAR_UNKNOWN)
11515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011516 before = get_tv_number_chk(&argvars[2], &error);
11517 if (error)
11518 return; /* type error; errmsg already given */
11519
Bram Moolenaar758711c2005-02-02 23:11:38 +000011520 if (before == l1->lv_len)
11521 item = NULL;
11522 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011523 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011524 item = list_find(l1, before);
11525 if (item == NULL)
11526 {
11527 EMSGN(_(e_listidx), before);
11528 return;
11529 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011530 }
11531 }
11532 else
11533 item = NULL;
11534 list_extend(l1, l2, item);
11535
Bram Moolenaare9a41262005-01-15 22:18:47 +000011536 copy_tv(&argvars[0], rettv);
11537 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011538 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011539 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11540 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011541 dict_T *d1, *d2;
11542 char_u *action;
11543 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011544
11545 d1 = argvars[0].vval.v_dict;
11546 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011547 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011548 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011549 {
11550 /* Check the third argument. */
11551 if (argvars[2].v_type != VAR_UNKNOWN)
11552 {
11553 static char *(av[]) = {"keep", "force", "error"};
11554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011555 action = get_tv_string_chk(&argvars[2]);
11556 if (action == NULL)
11557 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011558 for (i = 0; i < 3; ++i)
11559 if (STRCMP(action, av[i]) == 0)
11560 break;
11561 if (i == 3)
11562 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011563 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011564 return;
11565 }
11566 }
11567 else
11568 action = (char_u *)"force";
11569
Bram Moolenaara9922d62013-05-30 13:01:18 +020011570 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011571
Bram Moolenaare9a41262005-01-15 22:18:47 +000011572 copy_tv(&argvars[0], rettv);
11573 }
11574 }
11575 else
11576 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011577}
11578
11579/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011580 * "feedkeys()" function
11581 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011583f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011584{
11585 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011586 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011587 char_u *keys, *flags;
11588 char_u nbuf[NUMBUFLEN];
11589 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011590 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011591 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011592
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011593 /* This is not allowed in the sandbox. If the commands would still be
11594 * executed in the sandbox it would be OK, but it probably happens later,
11595 * when "sandbox" is no longer set. */
11596 if (check_secure())
11597 return;
11598
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011599 keys = get_tv_string(&argvars[0]);
11600 if (*keys != NUL)
11601 {
11602 if (argvars[1].v_type != VAR_UNKNOWN)
11603 {
11604 flags = get_tv_string_buf(&argvars[1], nbuf);
11605 for ( ; *flags != NUL; ++flags)
11606 {
11607 switch (*flags)
11608 {
11609 case 'n': remap = FALSE; break;
11610 case 'm': remap = TRUE; break;
11611 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011612 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011613 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011614 }
11615 }
11616 }
11617
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011618 /* Need to escape K_SPECIAL and CSI before putting the string in the
11619 * typeahead buffer. */
11620 keys_esc = vim_strsave_escape_csi(keys);
11621 if (keys_esc != NULL)
11622 {
11623 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011624 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011625 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011626 if (vgetc_busy)
11627 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011628 if (execute)
11629 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011630 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011631 }
11632}
11633
11634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 * "filereadable()" function
11636 */
11637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011638f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011640 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 char_u *p;
11642 int n;
11643
Bram Moolenaarc236c162008-07-13 17:41:49 +000011644#ifndef O_NONBLOCK
11645# define O_NONBLOCK 0
11646#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011648 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11649 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 {
11651 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011652 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 }
11654 else
11655 n = FALSE;
11656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658}
11659
11660/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011661 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 * rights to write into.
11663 */
11664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011665f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011667 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668}
11669
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011670 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011671findfilendir(
11672 typval_T *argvars UNUSED,
11673 typval_T *rettv,
11674 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011675{
11676#ifdef FEAT_SEARCHPATH
11677 char_u *fname;
11678 char_u *fresult = NULL;
11679 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11680 char_u *p;
11681 char_u pathbuf[NUMBUFLEN];
11682 int count = 1;
11683 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011684 int error = FALSE;
11685#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011686
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011687 rettv->vval.v_string = NULL;
11688 rettv->v_type = VAR_STRING;
11689
11690#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011692
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011693 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11696 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011697 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011698 else
11699 {
11700 if (*p != NUL)
11701 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011703 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011704 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706 }
11707
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011708 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11709 error = TRUE;
11710
11711 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011713 do
11714 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011715 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011716 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 fresult = find_file_in_path_option(first ? fname : NULL,
11718 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011719 0, first, path,
11720 find_what,
11721 curbuf->b_ffname,
11722 find_what == FINDFILE_DIR
11723 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011724 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011725
11726 if (fresult != NULL && rettv->v_type == VAR_LIST)
11727 list_append_string(rettv->vval.v_list, fresult, -1);
11728
11729 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011730 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011731
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011732 if (rettv->v_type == VAR_STRING)
11733 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011734#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011735}
11736
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011737static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11738static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011739
11740/*
11741 * Implementation of map() and filter().
11742 */
11743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011744filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011745{
11746 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011747 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011748 listitem_T *li, *nli;
11749 list_T *l = NULL;
11750 dictitem_T *di;
11751 hashtab_T *ht;
11752 hashitem_T *hi;
11753 dict_T *d = NULL;
11754 typval_T save_val;
11755 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011756 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011757 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011758 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011759 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011760 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011761 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011762 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011763
Bram Moolenaare9a41262005-01-15 22:18:47 +000011764 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011765 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011766 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011767 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011768 return;
11769 }
11770 else if (argvars[0].v_type == VAR_DICT)
11771 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011772 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011773 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011774 return;
11775 }
11776 else
11777 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011778 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011779 return;
11780 }
11781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 expr = get_tv_string_buf_chk(&argvars[1], buf);
11783 /* On type errors, the preceding call has already displayed an error
11784 * message. Avoid a misleading error message for an empty string that
11785 * was not passed as argument. */
11786 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011788 prepare_vimvar(VV_VAL, &save_val);
11789 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011790
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011791 /* We reset "did_emsg" to be able to detect whether an error
11792 * occurred during evaluation of the expression. */
11793 save_did_emsg = did_emsg;
11794 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011795
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011796 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011797 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011799 vimvars[VV_KEY].vv_type = VAR_STRING;
11800
11801 ht = &d->dv_hashtab;
11802 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011803 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011804 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 if (!HASHITEM_EMPTY(hi))
11807 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011808 int r;
11809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 --todo;
11811 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011812 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011813 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11814 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 break;
11816 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011817 r = filter_map_one(&di->di_tv, expr, map, &rem);
11818 clear_tv(&vimvars[VV_KEY].vv_tv);
11819 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 break;
11821 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011822 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011823 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11824 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011825 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011826 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011827 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 }
11829 }
11830 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011831 }
11832 else
11833 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011834 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011836 for (li = l->lv_first; li != NULL; li = nli)
11837 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011838 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011839 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011840 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011841 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011842 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011843 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011844 break;
11845 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011846 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011847 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011848 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011849 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011850
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011851 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011852 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011853
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011854 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011856
11857 copy_tv(&argvars[0], rettv);
11858}
11859
11860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011861filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011862{
Bram Moolenaar33570922005-01-25 22:26:29 +000011863 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011864 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011865 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011866
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011868 s = expr;
11869 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011870 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011871 if (*s != NUL) /* check for trailing chars after expr */
11872 {
11873 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011874 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011875 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011876 }
11877 if (map)
11878 {
11879 /* map(): replace the list item value */
11880 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011881 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011882 *tv = rettv;
11883 }
11884 else
11885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011886 int error = FALSE;
11887
Bram Moolenaare9a41262005-01-15 22:18:47 +000011888 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011889 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011890 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 /* On type error, nothing has been removed; return FAIL to stop the
11892 * loop. The error message was given by get_tv_number_chk(). */
11893 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011894 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011895 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011896 retval = OK;
11897theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011898 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011899 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011900}
11901
11902/*
11903 * "filter()" function
11904 */
11905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011906f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011907{
11908 filter_map(argvars, rettv, FALSE);
11909}
11910
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011911/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011912 * "finddir({fname}[, {path}[, {count}]])" function
11913 */
11914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011915f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011916{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011917 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011918}
11919
11920/*
11921 * "findfile({fname}[, {path}[, {count}]])" function
11922 */
11923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011924f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011925{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011926 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011927}
11928
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011929#ifdef FEAT_FLOAT
11930/*
11931 * "float2nr({float})" function
11932 */
11933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011934f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011935{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011936 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011937
11938 if (get_float_arg(argvars, &f) == OK)
11939 {
11940 if (f < -0x7fffffff)
11941 rettv->vval.v_number = -0x7fffffff;
11942 else if (f > 0x7fffffff)
11943 rettv->vval.v_number = 0x7fffffff;
11944 else
11945 rettv->vval.v_number = (varnumber_T)f;
11946 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011947}
11948
11949/*
11950 * "floor({float})" function
11951 */
11952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011953f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011954{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011955 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011956
11957 rettv->v_type = VAR_FLOAT;
11958 if (get_float_arg(argvars, &f) == OK)
11959 rettv->vval.v_float = floor(f);
11960 else
11961 rettv->vval.v_float = 0.0;
11962}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011963
11964/*
11965 * "fmod()" function
11966 */
11967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011968f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011969{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011970 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011971
11972 rettv->v_type = VAR_FLOAT;
11973 if (get_float_arg(argvars, &fx) == OK
11974 && get_float_arg(&argvars[1], &fy) == OK)
11975 rettv->vval.v_float = fmod(fx, fy);
11976 else
11977 rettv->vval.v_float = 0.0;
11978}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011979#endif
11980
Bram Moolenaar0d660222005-01-07 21:51:51 +000011981/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011982 * "fnameescape({string})" function
11983 */
11984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011985f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011986{
11987 rettv->vval.v_string = vim_strsave_fnameescape(
11988 get_tv_string(&argvars[0]), FALSE);
11989 rettv->v_type = VAR_STRING;
11990}
11991
11992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 * "fnamemodify({fname}, {mods})" function
11994 */
11995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011996f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997{
11998 char_u *fname;
11999 char_u *mods;
12000 int usedlen = 0;
12001 int len;
12002 char_u *fbuf = NULL;
12003 char_u buf[NUMBUFLEN];
12004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012005 fname = get_tv_string_chk(&argvars[0]);
12006 mods = get_tv_string_buf_chk(&argvars[1], buf);
12007 if (fname == NULL || mods == NULL)
12008 fname = NULL;
12009 else
12010 {
12011 len = (int)STRLEN(fname);
12012 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012017 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 vim_free(fbuf);
12021}
12022
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012023static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024
12025/*
12026 * "foldclosed()" function
12027 */
12028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012029foldclosed_both(
12030 typval_T *argvars UNUSED,
12031 typval_T *rettv,
12032 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033{
12034#ifdef FEAT_FOLDING
12035 linenr_T lnum;
12036 linenr_T first, last;
12037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012038 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12040 {
12041 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12042 {
12043 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012044 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012046 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047 return;
12048 }
12049 }
12050#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012052}
12053
12054/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012055 * "foldclosed()" function
12056 */
12057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012058f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012059{
12060 foldclosed_both(argvars, rettv, FALSE);
12061}
12062
12063/*
12064 * "foldclosedend()" function
12065 */
12066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012067f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012068{
12069 foldclosed_both(argvars, rettv, TRUE);
12070}
12071
12072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073 * "foldlevel()" function
12074 */
12075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012076f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077{
12078#ifdef FEAT_FOLDING
12079 linenr_T lnum;
12080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012081 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012083 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085}
12086
12087/*
12088 * "foldtext()" function
12089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012091f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012092{
12093#ifdef FEAT_FOLDING
12094 linenr_T lnum;
12095 char_u *s;
12096 char_u *r;
12097 int len;
12098 char *txt;
12099#endif
12100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 rettv->v_type = VAR_STRING;
12102 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012104 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12105 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12106 <= curbuf->b_ml.ml_line_count
12107 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108 {
12109 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012110 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12111 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 {
12113 if (!linewhite(lnum))
12114 break;
12115 ++lnum;
12116 }
12117
12118 /* Find interesting text in this line. */
12119 s = skipwhite(ml_get(lnum));
12120 /* skip C comment-start */
12121 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012122 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012124 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012125 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012126 {
12127 s = skipwhite(ml_get(lnum + 1));
12128 if (*s == '*')
12129 s = skipwhite(s + 1);
12130 }
12131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 txt = _("+-%s%3ld lines: ");
12133 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012134 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 + 20 /* for %3ld */
12136 + STRLEN(s))); /* concatenated */
12137 if (r != NULL)
12138 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012139 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12140 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12141 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 len = (int)STRLEN(r);
12143 STRCAT(r, s);
12144 /* remove 'foldmarker' and 'commentstring' */
12145 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147 }
12148 }
12149#endif
12150}
12151
12152/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012153 * "foldtextresult(lnum)" function
12154 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012156f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012157{
12158#ifdef FEAT_FOLDING
12159 linenr_T lnum;
12160 char_u *text;
12161 char_u buf[51];
12162 foldinfo_T foldinfo;
12163 int fold_count;
12164#endif
12165
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012166 rettv->v_type = VAR_STRING;
12167 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012168#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012170 /* treat illegal types and illegal string values for {lnum} the same */
12171 if (lnum < 0)
12172 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012173 fold_count = foldedCount(curwin, lnum, &foldinfo);
12174 if (fold_count > 0)
12175 {
12176 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12177 &foldinfo, buf);
12178 if (text == buf)
12179 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012181 }
12182#endif
12183}
12184
12185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 * "foreground()" function
12187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012189f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191#ifdef FEAT_GUI
12192 if (gui.in_use)
12193 gui_mch_set_foreground();
12194#else
12195# ifdef WIN32
12196 win32_set_foreground();
12197# endif
12198#endif
12199}
12200
12201/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012202 * "function()" function
12203 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012205f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012206{
12207 char_u *s;
12208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012209 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012210 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012211 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012212 /* Don't check an autoload name for existence here. */
12213 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012214 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012215 else
12216 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012217 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012218 {
12219 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012220 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012221
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012222 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12223 * also be called from another script. Using trans_function_name()
12224 * would also work, but some plugins depend on the name being
12225 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012226 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012227 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012228 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012229 if (rettv->vval.v_string != NULL)
12230 {
12231 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012232 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012233 }
12234 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012235 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012236 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012237 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012238 }
12239}
12240
12241/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012242 * "garbagecollect()" function
12243 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012245f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012246{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012247 /* This is postponed until we are back at the toplevel, because we may be
12248 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12249 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012250
12251 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12252 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012253}
12254
12255/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012256 * "get()" function
12257 */
12258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012259f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012260{
Bram Moolenaar33570922005-01-25 22:26:29 +000012261 listitem_T *li;
12262 list_T *l;
12263 dictitem_T *di;
12264 dict_T *d;
12265 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012266
Bram Moolenaare9a41262005-01-15 22:18:47 +000012267 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012268 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012269 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012271 int error = FALSE;
12272
12273 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12274 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012275 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012276 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012277 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012278 else if (argvars[0].v_type == VAR_DICT)
12279 {
12280 if ((d = argvars[0].vval.v_dict) != NULL)
12281 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012282 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012283 if (di != NULL)
12284 tv = &di->di_tv;
12285 }
12286 }
12287 else
12288 EMSG2(_(e_listdictarg), "get()");
12289
12290 if (tv == NULL)
12291 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012292 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012293 copy_tv(&argvars[2], rettv);
12294 }
12295 else
12296 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012297}
12298
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012299static 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 +000012300
12301/*
12302 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012303 * Return a range (from start to end) of lines in rettv from the specified
12304 * buffer.
12305 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012306 */
12307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012308get_buffer_lines(
12309 buf_T *buf,
12310 linenr_T start,
12311 linenr_T end,
12312 int retlist,
12313 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012314{
12315 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012316
Bram Moolenaar959a1432013-12-14 12:17:38 +010012317 rettv->v_type = VAR_STRING;
12318 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012319 if (retlist && rettv_list_alloc(rettv) == FAIL)
12320 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012321
12322 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12323 return;
12324
12325 if (!retlist)
12326 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012327 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12328 p = ml_get_buf(buf, start, FALSE);
12329 else
12330 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012331 rettv->vval.v_string = vim_strsave(p);
12332 }
12333 else
12334 {
12335 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012336 return;
12337
12338 if (start < 1)
12339 start = 1;
12340 if (end > buf->b_ml.ml_line_count)
12341 end = buf->b_ml.ml_line_count;
12342 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012343 if (list_append_string(rettv->vval.v_list,
12344 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012345 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012346 }
12347}
12348
12349/*
12350 * "getbufline()" function
12351 */
12352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012353f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012354{
12355 linenr_T lnum;
12356 linenr_T end;
12357 buf_T *buf;
12358
12359 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12360 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012361 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012362 --emsg_off;
12363
Bram Moolenaar661b1822005-07-28 22:36:45 +000012364 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012365 if (argvars[2].v_type == VAR_UNKNOWN)
12366 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012367 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012368 end = get_tv_lnum_buf(&argvars[2], buf);
12369
Bram Moolenaar342337a2005-07-21 21:11:17 +000012370 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012371}
12372
Bram Moolenaar0d660222005-01-07 21:51:51 +000012373/*
12374 * "getbufvar()" function
12375 */
12376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012377f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012378{
12379 buf_T *buf;
12380 buf_T *save_curbuf;
12381 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012382 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012383 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012384
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012385 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12386 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012387 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012388 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012389
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012390 rettv->v_type = VAR_STRING;
12391 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012392
12393 if (buf != NULL && varname != NULL)
12394 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012395 /* set curbuf to be our buf, temporarily */
12396 save_curbuf = curbuf;
12397 curbuf = buf;
12398
Bram Moolenaar0d660222005-01-07 21:51:51 +000012399 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012400 {
12401 if (get_option_tv(&varname, rettv, TRUE) == OK)
12402 done = TRUE;
12403 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012404 else if (STRCMP(varname, "changedtick") == 0)
12405 {
12406 rettv->v_type = VAR_NUMBER;
12407 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012408 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012409 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012410 else
12411 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012412 /* Look up the variable. */
12413 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12414 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12415 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012417 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012418 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012419 done = TRUE;
12420 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012421 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012422
12423 /* restore previous notion of curbuf */
12424 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012425 }
12426
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012427 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12428 /* use the default value */
12429 copy_tv(&argvars[2], rettv);
12430
Bram Moolenaar0d660222005-01-07 21:51:51 +000012431 --emsg_off;
12432}
12433
12434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 * "getchar()" function
12436 */
12437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012438f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439{
12440 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012441 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012443 /* Position the cursor. Needed after a message that ends in a space. */
12444 windgoto(msg_row, msg_col);
12445
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 ++no_mapping;
12447 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012448 for (;;)
12449 {
12450 if (argvars[0].v_type == VAR_UNKNOWN)
12451 /* getchar(): blocking wait. */
12452 n = safe_vgetc();
12453 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12454 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012455 n = vpeekc_any();
12456 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012457 /* illegal argument or getchar(0) and no char avail: return zero */
12458 n = 0;
12459 else
12460 /* getchar(0) and char avail: return char */
12461 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012462
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012463 if (n == K_IGNORE)
12464 continue;
12465 break;
12466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 --no_mapping;
12468 --allow_keys;
12469
Bram Moolenaar219b8702006-11-01 14:32:36 +000012470 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12471 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12472 vimvars[VV_MOUSE_COL].vv_nr = 0;
12473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 if (IS_SPECIAL(n) || mod_mask != 0)
12476 {
12477 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12478 int i = 0;
12479
12480 /* Turn a special key into three bytes, plus modifier. */
12481 if (mod_mask != 0)
12482 {
12483 temp[i++] = K_SPECIAL;
12484 temp[i++] = KS_MODIFIER;
12485 temp[i++] = mod_mask;
12486 }
12487 if (IS_SPECIAL(n))
12488 {
12489 temp[i++] = K_SPECIAL;
12490 temp[i++] = K_SECOND(n);
12491 temp[i++] = K_THIRD(n);
12492 }
12493#ifdef FEAT_MBYTE
12494 else if (has_mbyte)
12495 i += (*mb_char2bytes)(n, temp + i);
12496#endif
12497 else
12498 temp[i++] = n;
12499 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500 rettv->v_type = VAR_STRING;
12501 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012502
12503#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012504 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012505 {
12506 int row = mouse_row;
12507 int col = mouse_col;
12508 win_T *win;
12509 linenr_T lnum;
12510# ifdef FEAT_WINDOWS
12511 win_T *wp;
12512# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012513 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012514
12515 if (row >= 0 && col >= 0)
12516 {
12517 /* Find the window at the mouse coordinates and compute the
12518 * text position. */
12519 win = mouse_find_win(&row, &col);
12520 (void)mouse_comp_pos(win, &row, &col, &lnum);
12521# ifdef FEAT_WINDOWS
12522 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012523 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012524# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012525 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012526 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12527 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12528 }
12529 }
12530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531 }
12532}
12533
12534/*
12535 * "getcharmod()" function
12536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012538f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012540 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541}
12542
12543/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012544 * "getcharsearch()" function
12545 */
12546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012547f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012548{
12549 if (rettv_dict_alloc(rettv) != FAIL)
12550 {
12551 dict_T *dict = rettv->vval.v_dict;
12552
12553 dict_add_nr_str(dict, "char", 0L, last_csearch());
12554 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12555 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12556 }
12557}
12558
12559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560 * "getcmdline()" function
12561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012563f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012565 rettv->v_type = VAR_STRING;
12566 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567}
12568
12569/*
12570 * "getcmdpos()" function
12571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012573f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012575 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576}
12577
12578/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012579 * "getcmdtype()" function
12580 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012582f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012583{
12584 rettv->v_type = VAR_STRING;
12585 rettv->vval.v_string = alloc(2);
12586 if (rettv->vval.v_string != NULL)
12587 {
12588 rettv->vval.v_string[0] = get_cmdline_type();
12589 rettv->vval.v_string[1] = NUL;
12590 }
12591}
12592
12593/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012594 * "getcmdwintype()" function
12595 */
12596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012597f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012598{
12599 rettv->v_type = VAR_STRING;
12600 rettv->vval.v_string = NULL;
12601#ifdef FEAT_CMDWIN
12602 rettv->vval.v_string = alloc(2);
12603 if (rettv->vval.v_string != NULL)
12604 {
12605 rettv->vval.v_string[0] = cmdwin_type;
12606 rettv->vval.v_string[1] = NUL;
12607 }
12608#endif
12609}
12610
12611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612 * "getcwd()" function
12613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012615f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012617 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012618 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012620 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012621 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012622
12623 wp = find_tabwin(&argvars[0], &argvars[1]);
12624 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012626 if (wp->w_localdir != NULL)
12627 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12628 else if(globaldir != NULL)
12629 rettv->vval.v_string = vim_strsave(globaldir);
12630 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012631 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012632 cwd = alloc(MAXPATHL);
12633 if (cwd != NULL)
12634 {
12635 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12636 rettv->vval.v_string = vim_strsave(cwd);
12637 vim_free(cwd);
12638 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012639 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012640#ifdef BACKSLASH_IN_FILENAME
12641 if (rettv->vval.v_string != NULL)
12642 slash_adjust(rettv->vval.v_string);
12643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 }
12645}
12646
12647/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012648 * "getfontname()" function
12649 */
12650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012651f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012652{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653 rettv->v_type = VAR_STRING;
12654 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012655#ifdef FEAT_GUI
12656 if (gui.in_use)
12657 {
12658 GuiFont font;
12659 char_u *name = NULL;
12660
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012661 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012662 {
12663 /* Get the "Normal" font. Either the name saved by
12664 * hl_set_font_name() or from the font ID. */
12665 font = gui.norm_font;
12666 name = hl_get_font_name();
12667 }
12668 else
12669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012671 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12672 return;
12673 font = gui_mch_get_font(name, FALSE);
12674 if (font == NOFONT)
12675 return; /* Invalid font name, return empty string. */
12676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012677 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012678 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012679 gui_mch_free_font(font);
12680 }
12681#endif
12682}
12683
12684/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012685 * "getfperm({fname})" function
12686 */
12687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012688f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012689{
12690 char_u *fname;
12691 struct stat st;
12692 char_u *perm = NULL;
12693 char_u flags[] = "rwx";
12694 int i;
12695
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012698 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012699 if (mch_stat((char *)fname, &st) >= 0)
12700 {
12701 perm = vim_strsave((char_u *)"---------");
12702 if (perm != NULL)
12703 {
12704 for (i = 0; i < 9; i++)
12705 {
12706 if (st.st_mode & (1 << (8 - i)))
12707 perm[i] = flags[i % 3];
12708 }
12709 }
12710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012712}
12713
12714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 * "getfsize({fname})" function
12716 */
12717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012718f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719{
12720 char_u *fname;
12721 struct stat st;
12722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012725 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726
12727 if (mch_stat((char *)fname, &st) >= 0)
12728 {
12729 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012732 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012733 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012734
12735 /* non-perfect check for overflow */
12736 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12737 rettv->vval.v_number = -2;
12738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 }
12740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742}
12743
12744/*
12745 * "getftime({fname})" function
12746 */
12747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012748f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749{
12750 char_u *fname;
12751 struct stat st;
12752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754
12755 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012758 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759}
12760
12761/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012762 * "getftype({fname})" function
12763 */
12764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012765f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012766{
12767 char_u *fname;
12768 struct stat st;
12769 char_u *type = NULL;
12770 char *t;
12771
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012775 if (mch_lstat((char *)fname, &st) >= 0)
12776 {
12777#ifdef S_ISREG
12778 if (S_ISREG(st.st_mode))
12779 t = "file";
12780 else if (S_ISDIR(st.st_mode))
12781 t = "dir";
12782# ifdef S_ISLNK
12783 else if (S_ISLNK(st.st_mode))
12784 t = "link";
12785# endif
12786# ifdef S_ISBLK
12787 else if (S_ISBLK(st.st_mode))
12788 t = "bdev";
12789# endif
12790# ifdef S_ISCHR
12791 else if (S_ISCHR(st.st_mode))
12792 t = "cdev";
12793# endif
12794# ifdef S_ISFIFO
12795 else if (S_ISFIFO(st.st_mode))
12796 t = "fifo";
12797# endif
12798# ifdef S_ISSOCK
12799 else if (S_ISSOCK(st.st_mode))
12800 t = "fifo";
12801# endif
12802 else
12803 t = "other";
12804#else
12805# ifdef S_IFMT
12806 switch (st.st_mode & S_IFMT)
12807 {
12808 case S_IFREG: t = "file"; break;
12809 case S_IFDIR: t = "dir"; break;
12810# ifdef S_IFLNK
12811 case S_IFLNK: t = "link"; break;
12812# endif
12813# ifdef S_IFBLK
12814 case S_IFBLK: t = "bdev"; break;
12815# endif
12816# ifdef S_IFCHR
12817 case S_IFCHR: t = "cdev"; break;
12818# endif
12819# ifdef S_IFIFO
12820 case S_IFIFO: t = "fifo"; break;
12821# endif
12822# ifdef S_IFSOCK
12823 case S_IFSOCK: t = "socket"; break;
12824# endif
12825 default: t = "other";
12826 }
12827# else
12828 if (mch_isdir(fname))
12829 t = "dir";
12830 else
12831 t = "file";
12832# endif
12833#endif
12834 type = vim_strsave((char_u *)t);
12835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012837}
12838
12839/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012840 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012841 */
12842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012843f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012844{
12845 linenr_T lnum;
12846 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012847 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012848
12849 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012850 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012851 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012852 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012853 retlist = FALSE;
12854 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012855 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012856 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012857 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012858 retlist = TRUE;
12859 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012860
Bram Moolenaar342337a2005-07-21 21:11:17 +000012861 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012862}
12863
12864/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012865 * "getmatches()" function
12866 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012868f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012869{
12870#ifdef FEAT_SEARCH_EXTRA
12871 dict_T *dict;
12872 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012873 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012874
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012875 if (rettv_list_alloc(rettv) == OK)
12876 {
12877 while (cur != NULL)
12878 {
12879 dict = dict_alloc();
12880 if (dict == NULL)
12881 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012882 if (cur->match.regprog == NULL)
12883 {
12884 /* match added with matchaddpos() */
12885 for (i = 0; i < MAXPOSMATCH; ++i)
12886 {
12887 llpos_T *llpos;
12888 char buf[6];
12889 list_T *l;
12890
12891 llpos = &cur->pos.pos[i];
12892 if (llpos->lnum == 0)
12893 break;
12894 l = list_alloc();
12895 if (l == NULL)
12896 break;
12897 list_append_number(l, (varnumber_T)llpos->lnum);
12898 if (llpos->col > 0)
12899 {
12900 list_append_number(l, (varnumber_T)llpos->col);
12901 list_append_number(l, (varnumber_T)llpos->len);
12902 }
12903 sprintf(buf, "pos%d", i + 1);
12904 dict_add_list(dict, buf, l);
12905 }
12906 }
12907 else
12908 {
12909 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12910 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012911 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012912 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12913 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012914# ifdef FEAT_CONCEAL
12915 if (cur->conceal_char)
12916 {
12917 char_u buf[MB_MAXBYTES + 1];
12918
12919 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12920 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12921 }
12922# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012923 list_append_dict(rettv->vval.v_list, dict);
12924 cur = cur->next;
12925 }
12926 }
12927#endif
12928}
12929
12930/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012931 * "getpid()" function
12932 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012934f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012935{
12936 rettv->vval.v_number = mch_get_pid();
12937}
12938
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012939static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012940
12941/*
12942 * "getcurpos()" function
12943 */
12944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012945f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012946{
12947 getpos_both(argvars, rettv, TRUE);
12948}
12949
Bram Moolenaar18081e32008-02-20 19:11:07 +000012950/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012951 * "getpos(string)" function
12952 */
12953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012954f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012955{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012956 getpos_both(argvars, rettv, FALSE);
12957}
12958
12959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012960getpos_both(
12961 typval_T *argvars,
12962 typval_T *rettv,
12963 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012964{
Bram Moolenaara5525202006-03-02 22:52:09 +000012965 pos_T *fp;
12966 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012967 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012968
12969 if (rettv_list_alloc(rettv) == OK)
12970 {
12971 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012972 if (getcurpos)
12973 fp = &curwin->w_cursor;
12974 else
12975 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012976 if (fnum != -1)
12977 list_append_number(l, (varnumber_T)fnum);
12978 else
12979 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012980 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12981 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012982 list_append_number(l, (fp != NULL)
12983 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012984 : (varnumber_T)0);
12985 list_append_number(l,
12986#ifdef FEAT_VIRTUALEDIT
12987 (fp != NULL) ? (varnumber_T)fp->coladd :
12988#endif
12989 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012990 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012991 {
12992 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012993 list_append_number(l, curwin->w_curswant == MAXCOL ?
12994 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012995 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012996 }
12997 else
12998 rettv->vval.v_number = FALSE;
12999}
13000
13001/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013002 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013003 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013005f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013006{
13007#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013008 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013009#endif
13010
Bram Moolenaar2641f772005-03-25 21:58:17 +000013011#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013012 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013013 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013014 wp = NULL;
13015 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13016 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013017 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013018 if (wp == NULL)
13019 return;
13020 }
13021
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013022 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013023 }
13024#endif
13025}
13026
13027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028 * "getreg()" function
13029 */
13030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013031f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032{
13033 char_u *strregname;
13034 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013035 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013036 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013037 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013039 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013041 strregname = get_tv_string_chk(&argvars[0]);
13042 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013043 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013045 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013046 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13047 return_list = get_tv_number_chk(&argvars[2], &error);
13048 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013051 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013052
13053 if (error)
13054 return;
13055
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056 regname = (strregname == NULL ? '"' : *strregname);
13057 if (regname == 0)
13058 regname = '"';
13059
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013060 if (return_list)
13061 {
13062 rettv->v_type = VAR_LIST;
13063 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13064 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013065 if (rettv->vval.v_list != NULL)
13066 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013067 }
13068 else
13069 {
13070 rettv->v_type = VAR_STRING;
13071 rettv->vval.v_string = get_reg_contents(regname,
13072 arg2 ? GREG_EXPR_SRC : 0);
13073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074}
13075
13076/*
13077 * "getregtype()" function
13078 */
13079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013080f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081{
13082 char_u *strregname;
13083 int regname;
13084 char_u buf[NUMBUFLEN + 2];
13085 long reglen = 0;
13086
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013087 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013088 {
13089 strregname = get_tv_string_chk(&argvars[0]);
13090 if (strregname == NULL) /* type error; errmsg already given */
13091 {
13092 rettv->v_type = VAR_STRING;
13093 rettv->vval.v_string = NULL;
13094 return;
13095 }
13096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097 else
13098 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013099 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100
13101 regname = (strregname == NULL ? '"' : *strregname);
13102 if (regname == 0)
13103 regname = '"';
13104
13105 buf[0] = NUL;
13106 buf[1] = NUL;
13107 switch (get_reg_type(regname, &reglen))
13108 {
13109 case MLINE: buf[0] = 'V'; break;
13110 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111 case MBLOCK:
13112 buf[0] = Ctrl_V;
13113 sprintf((char *)buf + 1, "%ld", reglen + 1);
13114 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013116 rettv->v_type = VAR_STRING;
13117 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118}
13119
13120/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013121 * "gettabvar()" function
13122 */
13123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013124f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013125{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013126 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013127 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013128 dictitem_T *v;
13129 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013130 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013131
13132 rettv->v_type = VAR_STRING;
13133 rettv->vval.v_string = NULL;
13134
13135 varname = get_tv_string_chk(&argvars[1]);
13136 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13137 if (tp != NULL && varname != NULL)
13138 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013139 /* Set tp to be our tabpage, temporarily. Also set the window to the
13140 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013141 if (switch_win(&oldcurwin, &oldtabpage,
13142 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013143 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013144 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013145 /* look up the variable */
13146 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13147 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13148 if (v != NULL)
13149 {
13150 copy_tv(&v->di_tv, rettv);
13151 done = TRUE;
13152 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013153 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013154
13155 /* restore previous notion of curwin */
13156 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013157 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013158
13159 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013160 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013161}
13162
13163/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013164 * "gettabwinvar()" function
13165 */
13166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013167f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013168{
13169 getwinvar(argvars, rettv, 1);
13170}
13171
13172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 * "getwinposx()" function
13174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013176f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179#ifdef FEAT_GUI
13180 if (gui.in_use)
13181 {
13182 int x, y;
13183
13184 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 }
13187#endif
13188}
13189
13190/*
13191 * "getwinposy()" function
13192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013194f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013196 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197#ifdef FEAT_GUI
13198 if (gui.in_use)
13199 {
13200 int x, y;
13201
13202 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013203 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 }
13205#endif
13206}
13207
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013208/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013209 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013210 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013211 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013212find_win_by_nr(
13213 typval_T *vp,
13214 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013215{
13216#ifdef FEAT_WINDOWS
13217 win_T *wp;
13218#endif
13219 int nr;
13220
13221 nr = get_tv_number_chk(vp, NULL);
13222
13223#ifdef FEAT_WINDOWS
13224 if (nr < 0)
13225 return NULL;
13226 if (nr == 0)
13227 return curwin;
13228
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013229 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13230 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013231 if (--nr <= 0)
13232 break;
13233 return wp;
13234#else
13235 if (nr == 0 || nr == 1)
13236 return curwin;
13237 return NULL;
13238#endif
13239}
13240
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013242 * Find window specified by "wvp" in tabpage "tvp".
13243 */
13244 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013245find_tabwin(
13246 typval_T *wvp, /* VAR_UNKNOWN for current window */
13247 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013248{
13249 win_T *wp = NULL;
13250 tabpage_T *tp = NULL;
13251 long n;
13252
13253 if (wvp->v_type != VAR_UNKNOWN)
13254 {
13255 if (tvp->v_type != VAR_UNKNOWN)
13256 {
13257 n = get_tv_number(tvp);
13258 if (n >= 0)
13259 tp = find_tabpage(n);
13260 }
13261 else
13262 tp = curtab;
13263
13264 if (tp != NULL)
13265 wp = find_win_by_nr(wvp, tp);
13266 }
13267 else
13268 wp = curwin;
13269
13270 return wp;
13271}
13272
13273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 * "getwinvar()" function
13275 */
13276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013277f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013279 getwinvar(argvars, rettv, 0);
13280}
13281
13282/*
13283 * getwinvar() and gettabwinvar()
13284 */
13285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013286getwinvar(
13287 typval_T *argvars,
13288 typval_T *rettv,
13289 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013290{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013291 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013293 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013294 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013295 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013296#ifdef FEAT_WINDOWS
13297 win_T *oldcurwin;
13298 tabpage_T *oldtabpage;
13299 int need_switch_win;
13300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013302#ifdef FEAT_WINDOWS
13303 if (off == 1)
13304 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13305 else
13306 tp = curtab;
13307#endif
13308 win = find_win_by_nr(&argvars[off], tp);
13309 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013310 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013312 rettv->v_type = VAR_STRING;
13313 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314
13315 if (win != NULL && varname != NULL)
13316 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013317#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013318 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013319 * otherwise the window is not valid. Only do this when needed,
13320 * autocommands get blocked. */
13321 need_switch_win = !(tp == curtab && win == curwin);
13322 if (!need_switch_win
13323 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13324#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013325 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013326 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013327 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013328 if (get_option_tv(&varname, rettv, 1) == OK)
13329 done = TRUE;
13330 }
13331 else
13332 {
13333 /* Look up the variable. */
13334 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13335 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13336 varname, FALSE);
13337 if (v != NULL)
13338 {
13339 copy_tv(&v->di_tv, rettv);
13340 done = TRUE;
13341 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013344
Bram Moolenaarba117c22015-09-29 16:53:22 +020013345#ifdef FEAT_WINDOWS
13346 if (need_switch_win)
13347 /* restore previous notion of curwin */
13348 restore_win(oldcurwin, oldtabpage, TRUE);
13349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350 }
13351
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013352 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13353 /* use the default return value */
13354 copy_tv(&argvars[off + 2], rettv);
13355
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356 --emsg_off;
13357}
13358
13359/*
13360 * "glob()" function
13361 */
13362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013363f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013365 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013367 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013369 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013370 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013372 if (argvars[1].v_type != VAR_UNKNOWN)
13373 {
13374 if (get_tv_number_chk(&argvars[1], &error))
13375 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013376 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013377 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013378 if (get_tv_number_chk(&argvars[2], &error))
13379 {
13380 rettv->v_type = VAR_LIST;
13381 rettv->vval.v_list = NULL;
13382 }
13383 if (argvars[3].v_type != VAR_UNKNOWN
13384 && get_tv_number_chk(&argvars[3], &error))
13385 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013386 }
13387 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013388 if (!error)
13389 {
13390 ExpandInit(&xpc);
13391 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013392 if (p_wic)
13393 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013394 if (rettv->v_type == VAR_STRING)
13395 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013396 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013397 else if (rettv_list_alloc(rettv) != FAIL)
13398 {
13399 int i;
13400
13401 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13402 NULL, options, WILD_ALL_KEEP);
13403 for (i = 0; i < xpc.xp_numfiles; i++)
13404 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13405
13406 ExpandCleanup(&xpc);
13407 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013408 }
13409 else
13410 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411}
13412
13413/*
13414 * "globpath()" function
13415 */
13416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013417f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013419 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013421 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013422 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013423 garray_T ga;
13424 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013426 /* When the optional second argument is non-zero, don't remove matches
13427 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013428 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013429 if (argvars[2].v_type != VAR_UNKNOWN)
13430 {
13431 if (get_tv_number_chk(&argvars[2], &error))
13432 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013433 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013434 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013435 if (get_tv_number_chk(&argvars[3], &error))
13436 {
13437 rettv->v_type = VAR_LIST;
13438 rettv->vval.v_list = NULL;
13439 }
13440 if (argvars[4].v_type != VAR_UNKNOWN
13441 && get_tv_number_chk(&argvars[4], &error))
13442 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013443 }
13444 }
13445 if (file != NULL && !error)
13446 {
13447 ga_init2(&ga, (int)sizeof(char_u *), 10);
13448 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13449 if (rettv->v_type == VAR_STRING)
13450 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13451 else if (rettv_list_alloc(rettv) != FAIL)
13452 for (i = 0; i < ga.ga_len; ++i)
13453 list_append_string(rettv->vval.v_list,
13454 ((char_u **)(ga.ga_data))[i], -1);
13455 ga_clear_strings(&ga);
13456 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013457 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013458 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459}
13460
13461/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013462 * "glob2regpat()" function
13463 */
13464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013465f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013466{
13467 char_u *pat = get_tv_string_chk(&argvars[0]);
13468
13469 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013470 rettv->vval.v_string = (pat == NULL)
13471 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013472}
13473
13474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475 * "has()" function
13476 */
13477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013478f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479{
13480 int i;
13481 char_u *name;
13482 int n = FALSE;
13483 static char *(has_list[]) =
13484 {
13485#ifdef AMIGA
13486 "amiga",
13487# ifdef FEAT_ARP
13488 "arp",
13489# endif
13490#endif
13491#ifdef __BEOS__
13492 "beos",
13493#endif
13494#ifdef MSDOS
13495# ifdef DJGPP
13496 "dos32",
13497# else
13498 "dos16",
13499# endif
13500#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013501#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 "mac",
13503#endif
13504#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013505 "macunix", /* built with 'darwin' enabled */
13506#endif
13507#if defined(__APPLE__) && __APPLE__ == 1
13508 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510#ifdef __QNX__
13511 "qnx",
13512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513#ifdef UNIX
13514 "unix",
13515#endif
13516#ifdef VMS
13517 "vms",
13518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013519#ifdef WIN32
13520 "win32",
13521#endif
13522#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13523 "win32unix",
13524#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013525#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 "win64",
13527#endif
13528#ifdef EBCDIC
13529 "ebcdic",
13530#endif
13531#ifndef CASE_INSENSITIVE_FILENAME
13532 "fname_case",
13533#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013534#ifdef HAVE_ACL
13535 "acl",
13536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537#ifdef FEAT_ARABIC
13538 "arabic",
13539#endif
13540#ifdef FEAT_AUTOCMD
13541 "autocmd",
13542#endif
13543#ifdef FEAT_BEVAL
13544 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013545# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13546 "balloon_multiline",
13547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548#endif
13549#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13550 "builtin_terms",
13551# ifdef ALL_BUILTIN_TCAPS
13552 "all_builtin_terms",
13553# endif
13554#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013555#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13556 || defined(FEAT_GUI_W32) \
13557 || defined(FEAT_GUI_MOTIF))
13558 "browsefilter",
13559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560#ifdef FEAT_BYTEOFF
13561 "byte_offset",
13562#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013563#ifdef FEAT_CHANNEL
13564 "channel",
13565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566#ifdef FEAT_CINDENT
13567 "cindent",
13568#endif
13569#ifdef FEAT_CLIENTSERVER
13570 "clientserver",
13571#endif
13572#ifdef FEAT_CLIPBOARD
13573 "clipboard",
13574#endif
13575#ifdef FEAT_CMDL_COMPL
13576 "cmdline_compl",
13577#endif
13578#ifdef FEAT_CMDHIST
13579 "cmdline_hist",
13580#endif
13581#ifdef FEAT_COMMENTS
13582 "comments",
13583#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013584#ifdef FEAT_CONCEAL
13585 "conceal",
13586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587#ifdef FEAT_CRYPT
13588 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013589 "crypt-blowfish",
13590 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591#endif
13592#ifdef FEAT_CSCOPE
13593 "cscope",
13594#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013595#ifdef FEAT_CURSORBIND
13596 "cursorbind",
13597#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013598#ifdef CURSOR_SHAPE
13599 "cursorshape",
13600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601#ifdef DEBUG
13602 "debug",
13603#endif
13604#ifdef FEAT_CON_DIALOG
13605 "dialog_con",
13606#endif
13607#ifdef FEAT_GUI_DIALOG
13608 "dialog_gui",
13609#endif
13610#ifdef FEAT_DIFF
13611 "diff",
13612#endif
13613#ifdef FEAT_DIGRAPHS
13614 "digraphs",
13615#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013616#ifdef FEAT_DIRECTX
13617 "directx",
13618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619#ifdef FEAT_DND
13620 "dnd",
13621#endif
13622#ifdef FEAT_EMACS_TAGS
13623 "emacs_tags",
13624#endif
13625 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013626 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627#ifdef FEAT_SEARCH_EXTRA
13628 "extra_search",
13629#endif
13630#ifdef FEAT_FKMAP
13631 "farsi",
13632#endif
13633#ifdef FEAT_SEARCHPATH
13634 "file_in_path",
13635#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013636#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013637 "filterpipe",
13638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639#ifdef FEAT_FIND_ID
13640 "find_in_path",
13641#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013642#ifdef FEAT_FLOAT
13643 "float",
13644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645#ifdef FEAT_FOLDING
13646 "folding",
13647#endif
13648#ifdef FEAT_FOOTER
13649 "footer",
13650#endif
13651#if !defined(USE_SYSTEM) && defined(UNIX)
13652 "fork",
13653#endif
13654#ifdef FEAT_GETTEXT
13655 "gettext",
13656#endif
13657#ifdef FEAT_GUI
13658 "gui",
13659#endif
13660#ifdef FEAT_GUI_ATHENA
13661# ifdef FEAT_GUI_NEXTAW
13662 "gui_neXtaw",
13663# else
13664 "gui_athena",
13665# endif
13666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667#ifdef FEAT_GUI_GTK
13668 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013671#ifdef FEAT_GUI_GNOME
13672 "gui_gnome",
13673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674#ifdef FEAT_GUI_MAC
13675 "gui_mac",
13676#endif
13677#ifdef FEAT_GUI_MOTIF
13678 "gui_motif",
13679#endif
13680#ifdef FEAT_GUI_PHOTON
13681 "gui_photon",
13682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683#ifdef FEAT_GUI_W32
13684 "gui_win32",
13685#endif
13686#ifdef FEAT_HANGULIN
13687 "hangul_input",
13688#endif
13689#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13690 "iconv",
13691#endif
13692#ifdef FEAT_INS_EXPAND
13693 "insert_expand",
13694#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013695#ifdef FEAT_JOB
13696 "job",
13697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698#ifdef FEAT_JUMPLIST
13699 "jumplist",
13700#endif
13701#ifdef FEAT_KEYMAP
13702 "keymap",
13703#endif
13704#ifdef FEAT_LANGMAP
13705 "langmap",
13706#endif
13707#ifdef FEAT_LIBCALL
13708 "libcall",
13709#endif
13710#ifdef FEAT_LINEBREAK
13711 "linebreak",
13712#endif
13713#ifdef FEAT_LISP
13714 "lispindent",
13715#endif
13716#ifdef FEAT_LISTCMDS
13717 "listcmds",
13718#endif
13719#ifdef FEAT_LOCALMAP
13720 "localmap",
13721#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013722#ifdef FEAT_LUA
13723# ifndef DYNAMIC_LUA
13724 "lua",
13725# endif
13726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727#ifdef FEAT_MENU
13728 "menu",
13729#endif
13730#ifdef FEAT_SESSION
13731 "mksession",
13732#endif
13733#ifdef FEAT_MODIFY_FNAME
13734 "modify_fname",
13735#endif
13736#ifdef FEAT_MOUSE
13737 "mouse",
13738#endif
13739#ifdef FEAT_MOUSESHAPE
13740 "mouseshape",
13741#endif
13742#if defined(UNIX) || defined(VMS)
13743# ifdef FEAT_MOUSE_DEC
13744 "mouse_dec",
13745# endif
13746# ifdef FEAT_MOUSE_GPM
13747 "mouse_gpm",
13748# endif
13749# ifdef FEAT_MOUSE_JSB
13750 "mouse_jsbterm",
13751# endif
13752# ifdef FEAT_MOUSE_NET
13753 "mouse_netterm",
13754# endif
13755# ifdef FEAT_MOUSE_PTERM
13756 "mouse_pterm",
13757# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013758# ifdef FEAT_MOUSE_SGR
13759 "mouse_sgr",
13760# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013761# ifdef FEAT_SYSMOUSE
13762 "mouse_sysmouse",
13763# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013764# ifdef FEAT_MOUSE_URXVT
13765 "mouse_urxvt",
13766# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767# ifdef FEAT_MOUSE_XTERM
13768 "mouse_xterm",
13769# endif
13770#endif
13771#ifdef FEAT_MBYTE
13772 "multi_byte",
13773#endif
13774#ifdef FEAT_MBYTE_IME
13775 "multi_byte_ime",
13776#endif
13777#ifdef FEAT_MULTI_LANG
13778 "multi_lang",
13779#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013780#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013781#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013782 "mzscheme",
13783#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785#ifdef FEAT_OLE
13786 "ole",
13787#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013788 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789#ifdef FEAT_PATH_EXTRA
13790 "path_extra",
13791#endif
13792#ifdef FEAT_PERL
13793#ifndef DYNAMIC_PERL
13794 "perl",
13795#endif
13796#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013797#ifdef FEAT_PERSISTENT_UNDO
13798 "persistent_undo",
13799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800#ifdef FEAT_PYTHON
13801#ifndef DYNAMIC_PYTHON
13802 "python",
13803#endif
13804#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013805#ifdef FEAT_PYTHON3
13806#ifndef DYNAMIC_PYTHON3
13807 "python3",
13808#endif
13809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810#ifdef FEAT_POSTSCRIPT
13811 "postscript",
13812#endif
13813#ifdef FEAT_PRINTER
13814 "printer",
13815#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013816#ifdef FEAT_PROFILE
13817 "profile",
13818#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013819#ifdef FEAT_RELTIME
13820 "reltime",
13821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822#ifdef FEAT_QUICKFIX
13823 "quickfix",
13824#endif
13825#ifdef FEAT_RIGHTLEFT
13826 "rightleft",
13827#endif
13828#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13829 "ruby",
13830#endif
13831#ifdef FEAT_SCROLLBIND
13832 "scrollbind",
13833#endif
13834#ifdef FEAT_CMDL_INFO
13835 "showcmd",
13836 "cmdline_info",
13837#endif
13838#ifdef FEAT_SIGNS
13839 "signs",
13840#endif
13841#ifdef FEAT_SMARTINDENT
13842 "smartindent",
13843#endif
13844#ifdef FEAT_SNIFF
13845 "sniff",
13846#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013847#ifdef STARTUPTIME
13848 "startuptime",
13849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850#ifdef FEAT_STL_OPT
13851 "statusline",
13852#endif
13853#ifdef FEAT_SUN_WORKSHOP
13854 "sun_workshop",
13855#endif
13856#ifdef FEAT_NETBEANS_INTG
13857 "netbeans_intg",
13858#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013859#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013860 "spell",
13861#endif
13862#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 "syntax",
13864#endif
13865#if defined(USE_SYSTEM) || !defined(UNIX)
13866 "system",
13867#endif
13868#ifdef FEAT_TAG_BINS
13869 "tag_binary",
13870#endif
13871#ifdef FEAT_TAG_OLDSTATIC
13872 "tag_old_static",
13873#endif
13874#ifdef FEAT_TAG_ANYWHITE
13875 "tag_any_white",
13876#endif
13877#ifdef FEAT_TCL
13878# ifndef DYNAMIC_TCL
13879 "tcl",
13880# endif
13881#endif
13882#ifdef TERMINFO
13883 "terminfo",
13884#endif
13885#ifdef FEAT_TERMRESPONSE
13886 "termresponse",
13887#endif
13888#ifdef FEAT_TEXTOBJ
13889 "textobjects",
13890#endif
13891#ifdef HAVE_TGETENT
13892 "tgetent",
13893#endif
13894#ifdef FEAT_TITLE
13895 "title",
13896#endif
13897#ifdef FEAT_TOOLBAR
13898 "toolbar",
13899#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013900#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13901 "unnamedplus",
13902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903#ifdef FEAT_USR_CMDS
13904 "user-commands", /* was accidentally included in 5.4 */
13905 "user_commands",
13906#endif
13907#ifdef FEAT_VIMINFO
13908 "viminfo",
13909#endif
13910#ifdef FEAT_VERTSPLIT
13911 "vertsplit",
13912#endif
13913#ifdef FEAT_VIRTUALEDIT
13914 "virtualedit",
13915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917#ifdef FEAT_VISUALEXTRA
13918 "visualextra",
13919#endif
13920#ifdef FEAT_VREPLACE
13921 "vreplace",
13922#endif
13923#ifdef FEAT_WILDIGN
13924 "wildignore",
13925#endif
13926#ifdef FEAT_WILDMENU
13927 "wildmenu",
13928#endif
13929#ifdef FEAT_WINDOWS
13930 "windows",
13931#endif
13932#ifdef FEAT_WAK
13933 "winaltkeys",
13934#endif
13935#ifdef FEAT_WRITEBACKUP
13936 "writebackup",
13937#endif
13938#ifdef FEAT_XIM
13939 "xim",
13940#endif
13941#ifdef FEAT_XFONTSET
13942 "xfontset",
13943#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013944#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013945 "xpm",
13946 "xpm_w32", /* for backward compatibility */
13947#else
13948# if defined(HAVE_XPM)
13949 "xpm",
13950# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952#ifdef USE_XSMP
13953 "xsmp",
13954#endif
13955#ifdef USE_XSMP_INTERACT
13956 "xsmp_interact",
13957#endif
13958#ifdef FEAT_XCLIPBOARD
13959 "xterm_clipboard",
13960#endif
13961#ifdef FEAT_XTERM_SAVE
13962 "xterm_save",
13963#endif
13964#if defined(UNIX) && defined(FEAT_X11)
13965 "X11",
13966#endif
13967 NULL
13968 };
13969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013970 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 for (i = 0; has_list[i] != NULL; ++i)
13972 if (STRICMP(name, has_list[i]) == 0)
13973 {
13974 n = TRUE;
13975 break;
13976 }
13977
13978 if (n == FALSE)
13979 {
13980 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013981 {
13982 if (name[5] == '-'
13983 && STRLEN(name) > 11
13984 && vim_isdigit(name[6])
13985 && vim_isdigit(name[8])
13986 && vim_isdigit(name[10]))
13987 {
13988 int major = atoi((char *)name + 6);
13989 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013990
13991 /* Expect "patch-9.9.01234". */
13992 n = (major < VIM_VERSION_MAJOR
13993 || (major == VIM_VERSION_MAJOR
13994 && (minor < VIM_VERSION_MINOR
13995 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013996 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013997 }
13998 else
13999 n = has_patch(atoi((char *)name + 5));
14000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 else if (STRICMP(name, "vim_starting") == 0)
14002 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014003#ifdef FEAT_MBYTE
14004 else if (STRICMP(name, "multi_byte_encoding") == 0)
14005 n = has_mbyte;
14006#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014007#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14008 else if (STRICMP(name, "balloon_multiline") == 0)
14009 n = multiline_balloon_available();
14010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011#ifdef DYNAMIC_TCL
14012 else if (STRICMP(name, "tcl") == 0)
14013 n = tcl_enabled(FALSE);
14014#endif
14015#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14016 else if (STRICMP(name, "iconv") == 0)
14017 n = iconv_enabled(FALSE);
14018#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014019#ifdef DYNAMIC_LUA
14020 else if (STRICMP(name, "lua") == 0)
14021 n = lua_enabled(FALSE);
14022#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014023#ifdef DYNAMIC_MZSCHEME
14024 else if (STRICMP(name, "mzscheme") == 0)
14025 n = mzscheme_enabled(FALSE);
14026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027#ifdef DYNAMIC_RUBY
14028 else if (STRICMP(name, "ruby") == 0)
14029 n = ruby_enabled(FALSE);
14030#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014031#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032#ifdef DYNAMIC_PYTHON
14033 else if (STRICMP(name, "python") == 0)
14034 n = python_enabled(FALSE);
14035#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014036#endif
14037#ifdef FEAT_PYTHON3
14038#ifdef DYNAMIC_PYTHON3
14039 else if (STRICMP(name, "python3") == 0)
14040 n = python3_enabled(FALSE);
14041#endif
14042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043#ifdef DYNAMIC_PERL
14044 else if (STRICMP(name, "perl") == 0)
14045 n = perl_enabled(FALSE);
14046#endif
14047#ifdef FEAT_GUI
14048 else if (STRICMP(name, "gui_running") == 0)
14049 n = (gui.in_use || gui.starting);
14050# ifdef FEAT_GUI_W32
14051 else if (STRICMP(name, "gui_win32s") == 0)
14052 n = gui_is_win32s();
14053# endif
14054# ifdef FEAT_BROWSE
14055 else if (STRICMP(name, "browse") == 0)
14056 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14057# endif
14058#endif
14059#ifdef FEAT_SYN_HL
14060 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014061 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062#endif
14063#if defined(WIN3264)
14064 else if (STRICMP(name, "win95") == 0)
14065 n = mch_windows95();
14066#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014067#ifdef FEAT_NETBEANS_INTG
14068 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014069 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071 }
14072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014073 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014074}
14075
14076/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014077 * "has_key()" function
14078 */
14079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014080f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014081{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014082 if (argvars[0].v_type != VAR_DICT)
14083 {
14084 EMSG(_(e_dictreq));
14085 return;
14086 }
14087 if (argvars[0].vval.v_dict == NULL)
14088 return;
14089
14090 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014091 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014092}
14093
14094/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014095 * "haslocaldir()" function
14096 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014098f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014099{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014100 win_T *wp = NULL;
14101
14102 wp = find_tabwin(&argvars[0], &argvars[1]);
14103 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014104}
14105
14106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 * "hasmapto()" function
14108 */
14109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014110f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111{
14112 char_u *name;
14113 char_u *mode;
14114 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014115 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014117 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014118 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119 mode = (char_u *)"nvo";
14120 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014122 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014123 if (argvars[2].v_type != VAR_UNKNOWN)
14124 abbr = get_tv_number(&argvars[2]);
14125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126
Bram Moolenaar2c932302006-03-18 21:42:09 +000014127 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014128 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014130 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131}
14132
14133/*
14134 * "histadd()" function
14135 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014137f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138{
14139#ifdef FEAT_CMDHIST
14140 int histype;
14141 char_u *str;
14142 char_u buf[NUMBUFLEN];
14143#endif
14144
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014145 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 if (check_restricted() || check_secure())
14147 return;
14148#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14150 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 if (histype >= 0)
14152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014153 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154 if (*str != NUL)
14155 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014156 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014158 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159 return;
14160 }
14161 }
14162#endif
14163}
14164
14165/*
14166 * "histdel()" function
14167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014169f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170{
14171#ifdef FEAT_CMDHIST
14172 int n;
14173 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14177 if (str == NULL)
14178 n = 0;
14179 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014181 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014182 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014185 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 else
14187 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014188 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014189 get_tv_string_buf(&argvars[1], buf));
14190 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191#endif
14192}
14193
14194/*
14195 * "histget()" function
14196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014198f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199{
14200#ifdef FEAT_CMDHIST
14201 int type;
14202 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014203 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14206 if (str == NULL)
14207 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 {
14210 type = get_histtype(str);
14211 if (argvars[1].v_type == VAR_UNKNOWN)
14212 idx = get_history_idx(type);
14213 else
14214 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14215 /* -1 on type error */
14216 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014221 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222}
14223
14224/*
14225 * "histnr()" function
14226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014228f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229{
14230 int i;
14231
14232#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 char_u *history = get_tv_string_chk(&argvars[0]);
14234
14235 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014236 if (i >= HIST_CMD && i < HIST_COUNT)
14237 i = get_history_idx(i);
14238 else
14239#endif
14240 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242}
14243
14244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 * "highlightID(name)" function
14246 */
14247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014248f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014250 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251}
14252
14253/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014254 * "highlight_exists()" function
14255 */
14256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014257f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014258{
14259 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14260}
14261
14262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263 * "hostname()" function
14264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014266f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267{
14268 char_u hostname[256];
14269
14270 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271 rettv->v_type = VAR_STRING;
14272 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273}
14274
14275/*
14276 * iconv() function
14277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014279f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280{
14281#ifdef FEAT_MBYTE
14282 char_u buf1[NUMBUFLEN];
14283 char_u buf2[NUMBUFLEN];
14284 char_u *from, *to, *str;
14285 vimconv_T vimconv;
14286#endif
14287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 rettv->v_type = VAR_STRING;
14289 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290
14291#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014292 str = get_tv_string(&argvars[0]);
14293 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14294 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 vimconv.vc_type = CONV_NONE;
14296 convert_setup(&vimconv, from, to);
14297
14298 /* If the encodings are equal, no conversion needed. */
14299 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303
14304 convert_setup(&vimconv, NULL, NULL);
14305 vim_free(from);
14306 vim_free(to);
14307#endif
14308}
14309
14310/*
14311 * "indent()" function
14312 */
14313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014314f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315{
14316 linenr_T lnum;
14317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014318 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014320 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014322 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323}
14324
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014325/*
14326 * "index()" function
14327 */
14328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014329f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014330{
Bram Moolenaar33570922005-01-25 22:26:29 +000014331 list_T *l;
14332 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014333 long idx = 0;
14334 int ic = FALSE;
14335
14336 rettv->vval.v_number = -1;
14337 if (argvars[0].v_type != VAR_LIST)
14338 {
14339 EMSG(_(e_listreq));
14340 return;
14341 }
14342 l = argvars[0].vval.v_list;
14343 if (l != NULL)
14344 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014345 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014346 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 int error = FALSE;
14349
Bram Moolenaar758711c2005-02-02 23:11:38 +000014350 /* Start at specified item. Use the cached index that list_find()
14351 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014353 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014354 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014355 ic = get_tv_number_chk(&argvars[3], &error);
14356 if (error)
14357 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014358 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014359
Bram Moolenaar758711c2005-02-02 23:11:38 +000014360 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014361 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014362 {
14363 rettv->vval.v_number = idx;
14364 break;
14365 }
14366 }
14367}
14368
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369static int inputsecret_flag = 0;
14370
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014371static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014372
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014374 * This function is used by f_input() and f_inputdialog() functions. The third
14375 * argument to f_input() specifies the type of completion to use at the
14376 * prompt. The third argument to f_inputdialog() specifies the value to return
14377 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 */
14379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014380get_user_input(
14381 typval_T *argvars,
14382 typval_T *rettv,
14383 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 char_u *p = NULL;
14387 int c;
14388 char_u buf[NUMBUFLEN];
14389 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014390 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014391 int xp_type = EXPAND_NOTHING;
14392 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014394 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014395 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396
14397#ifdef NO_CONSOLE_INPUT
14398 /* While starting up, there is no place to enter text. */
14399 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401#endif
14402
14403 cmd_silent = FALSE; /* Want to see the prompt. */
14404 if (prompt != NULL)
14405 {
14406 /* Only the part of the message after the last NL is considered as
14407 * prompt for the command line */
14408 p = vim_strrchr(prompt, '\n');
14409 if (p == NULL)
14410 p = prompt;
14411 else
14412 {
14413 ++p;
14414 c = *p;
14415 *p = NUL;
14416 msg_start();
14417 msg_clr_eos();
14418 msg_puts_attr(prompt, echo_attr);
14419 msg_didout = FALSE;
14420 msg_starthere();
14421 *p = c;
14422 }
14423 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014425 if (argvars[1].v_type != VAR_UNKNOWN)
14426 {
14427 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14428 if (defstr != NULL)
14429 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014431 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014432 {
14433 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014434 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014435 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014436
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014437 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014438 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014439
Bram Moolenaar4463f292005-09-25 22:20:24 +000014440 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14441 if (xp_name == NULL)
14442 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014443
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014444 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014445
Bram Moolenaar4463f292005-09-25 22:20:24 +000014446 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14447 &xp_arg) == FAIL)
14448 return;
14449 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014450 }
14451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014452 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014453 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014454 int save_ex_normal_busy = ex_normal_busy;
14455 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014456 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014457 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14458 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014459 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014460 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014461 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014462 && argvars[1].v_type != VAR_UNKNOWN
14463 && argvars[2].v_type != VAR_UNKNOWN)
14464 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14465 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014466
14467 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014469 /* since the user typed this, no need to wait for return */
14470 need_wait_return = FALSE;
14471 msg_didout = FALSE;
14472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 cmd_silent = cmd_silent_save;
14474}
14475
14476/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014477 * "input()" function
14478 * Also handles inputsecret() when inputsecret is set.
14479 */
14480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014481f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014482{
14483 get_user_input(argvars, rettv, FALSE);
14484}
14485
14486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 * "inputdialog()" function
14488 */
14489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014490f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491{
14492#if defined(FEAT_GUI_TEXTDIALOG)
14493 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14494 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14495 {
14496 char_u *message;
14497 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014498 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014500 message = get_tv_string_chk(&argvars[0]);
14501 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014502 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014503 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 else
14505 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014506 if (message != NULL && defstr != NULL
14507 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014508 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510 else
14511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014512 if (message != NULL && defstr != NULL
14513 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014514 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515 rettv->vval.v_string = vim_strsave(
14516 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014520 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 }
14522 else
14523#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014524 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525}
14526
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014527/*
14528 * "inputlist()" function
14529 */
14530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014531f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014532{
14533 listitem_T *li;
14534 int selected;
14535 int mouse_used;
14536
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014537#ifdef NO_CONSOLE_INPUT
14538 /* While starting up, there is no place to enter text. */
14539 if (no_console_input())
14540 return;
14541#endif
14542 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14543 {
14544 EMSG2(_(e_listarg), "inputlist()");
14545 return;
14546 }
14547
14548 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014549 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014550 lines_left = Rows; /* avoid more prompt */
14551 msg_scroll = TRUE;
14552 msg_clr_eos();
14553
14554 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14555 {
14556 msg_puts(get_tv_string(&li->li_tv));
14557 msg_putchar('\n');
14558 }
14559
14560 /* Ask for choice. */
14561 selected = prompt_for_number(&mouse_used);
14562 if (mouse_used)
14563 selected -= lines_left;
14564
14565 rettv->vval.v_number = selected;
14566}
14567
14568
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14570
14571/*
14572 * "inputrestore()" function
14573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014575f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576{
14577 if (ga_userinput.ga_len > 0)
14578 {
14579 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14581 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014582 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 }
14584 else if (p_verbose > 1)
14585 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014586 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014587 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 }
14589}
14590
14591/*
14592 * "inputsave()" function
14593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014595f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014597 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 if (ga_grow(&ga_userinput, 1) == OK)
14599 {
14600 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14601 + ga_userinput.ga_len);
14602 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014603 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014604 }
14605 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607}
14608
14609/*
14610 * "inputsecret()" function
14611 */
14612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014613f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614{
14615 ++cmdline_star;
14616 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014617 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618 --cmdline_star;
14619 --inputsecret_flag;
14620}
14621
14622/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014623 * "insert()" function
14624 */
14625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014626f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014627{
14628 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014629 listitem_T *item;
14630 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014631 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014632
14633 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014635 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014636 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014637 {
14638 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014639 before = get_tv_number_chk(&argvars[2], &error);
14640 if (error)
14641 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014642
Bram Moolenaar758711c2005-02-02 23:11:38 +000014643 if (before == l->lv_len)
14644 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014645 else
14646 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014647 item = list_find(l, before);
14648 if (item == NULL)
14649 {
14650 EMSGN(_(e_listidx), before);
14651 l = NULL;
14652 }
14653 }
14654 if (l != NULL)
14655 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014656 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014657 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014658 }
14659 }
14660}
14661
14662/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014663 * "invert(expr)" function
14664 */
14665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014666f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014667{
14668 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14669}
14670
14671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 * "isdirectory()" function
14673 */
14674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014675f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014677 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678}
14679
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014680/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014681 * "islocked()" function
14682 */
14683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014684f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014685{
14686 lval_T lv;
14687 char_u *end;
14688 dictitem_T *di;
14689
14690 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014691 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14692 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014693 if (end != NULL && lv.ll_name != NULL)
14694 {
14695 if (*end != NUL)
14696 EMSG(_(e_trailing));
14697 else
14698 {
14699 if (lv.ll_tv == NULL)
14700 {
14701 if (check_changedtick(lv.ll_name))
14702 rettv->vval.v_number = 1; /* always locked */
14703 else
14704 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014705 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014706 if (di != NULL)
14707 {
14708 /* Consider a variable locked when:
14709 * 1. the variable itself is locked
14710 * 2. the value of the variable is locked.
14711 * 3. the List or Dict value is locked.
14712 */
14713 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14714 || tv_islocked(&di->di_tv));
14715 }
14716 }
14717 }
14718 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014719 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014720 else if (lv.ll_newkey != NULL)
14721 EMSG2(_(e_dictkey), lv.ll_newkey);
14722 else if (lv.ll_list != NULL)
14723 /* List item. */
14724 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14725 else
14726 /* Dictionary item. */
14727 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14728 }
14729 }
14730
14731 clear_lval(&lv);
14732}
14733
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014734static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014735
14736/*
14737 * Turn a dict into a list:
14738 * "what" == 0: list of keys
14739 * "what" == 1: list of values
14740 * "what" == 2: list of items
14741 */
14742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014743dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014744{
Bram Moolenaar33570922005-01-25 22:26:29 +000014745 list_T *l2;
14746 dictitem_T *di;
14747 hashitem_T *hi;
14748 listitem_T *li;
14749 listitem_T *li2;
14750 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014751 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014752
Bram Moolenaar8c711452005-01-14 21:53:12 +000014753 if (argvars[0].v_type != VAR_DICT)
14754 {
14755 EMSG(_(e_dictreq));
14756 return;
14757 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014758 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014759 return;
14760
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014761 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014762 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014763
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014764 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014765 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014766 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014767 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014768 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014769 --todo;
14770 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014771
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014772 li = listitem_alloc();
14773 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014774 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014775 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014776
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014777 if (what == 0)
14778 {
14779 /* keys() */
14780 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014781 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014782 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14783 }
14784 else if (what == 1)
14785 {
14786 /* values() */
14787 copy_tv(&di->di_tv, &li->li_tv);
14788 }
14789 else
14790 {
14791 /* items() */
14792 l2 = list_alloc();
14793 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014794 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014795 li->li_tv.vval.v_list = l2;
14796 if (l2 == NULL)
14797 break;
14798 ++l2->lv_refcount;
14799
14800 li2 = listitem_alloc();
14801 if (li2 == NULL)
14802 break;
14803 list_append(l2, li2);
14804 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014805 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014806 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14807
14808 li2 = listitem_alloc();
14809 if (li2 == NULL)
14810 break;
14811 list_append(l2, li2);
14812 copy_tv(&di->di_tv, &li2->li_tv);
14813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014814 }
14815 }
14816}
14817
14818/*
14819 * "items(dict)" function
14820 */
14821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014822f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014823{
14824 dict_list(argvars, rettv, 2);
14825}
14826
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014827#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014828/*
14829 * Get the job from the argument.
14830 * Returns NULL if the job is invalid.
14831 */
14832 static job_T *
14833get_job_arg(typval_T *tv)
14834{
14835 job_T *job;
14836
14837 if (tv->v_type != VAR_JOB)
14838 {
14839 EMSG2(_(e_invarg2), get_tv_string(tv));
14840 return NULL;
14841 }
14842 job = tv->vval.v_job;
14843
14844 if (job == NULL)
14845 EMSG(_("E916: not a valid job"));
14846 return job;
14847}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014848
14849# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014850/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014851 * "job_getchannel()" function
14852 */
14853 static void
14854f_job_getchannel(typval_T *argvars, typval_T *rettv)
14855{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014856 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014857
Bram Moolenaar65edff82016-02-21 16:40:11 +010014858 if (job != NULL)
14859 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014860 rettv->v_type = VAR_CHANNEL;
14861 rettv->vval.v_channel = job->jv_channel;
14862 if (job->jv_channel != NULL)
14863 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014864 }
14865}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014866# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014867
14868/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014869 * "job_setoptions()" function
14870 */
14871 static void
14872f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14873{
14874 job_T *job = get_job_arg(&argvars[0]);
14875 jobopt_T opt;
14876
14877 if (job == NULL)
14878 return;
14879 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014880 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014881 return;
14882 job_set_options(job, &opt);
14883}
14884
14885/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014886 * "job_start()" function
14887 */
14888 static void
14889f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14890{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014891 job_T *job;
14892 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014893#if defined(UNIX)
14894# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014895 char **argv = NULL;
14896 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014897#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014898 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014899#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014900 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014901
14902 rettv->v_type = VAR_JOB;
14903 job = job_alloc();
14904 rettv->vval.v_job = job;
14905 if (job == NULL)
14906 return;
14907
14908 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014909
14910 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014911 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014912 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014913 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014914 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
14915 + JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014916 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010014917 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014918
Bram Moolenaar835dc632016-02-07 14:27:38 +010014919#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014920 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014921#endif
14922
14923 if (argvars[0].v_type == VAR_STRING)
14924 {
14925 /* Command is a string. */
14926 cmd = argvars[0].vval.v_string;
14927#ifdef USE_ARGV
14928 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14929 return;
14930 argv[argc] = NULL;
14931#endif
14932 }
14933 else if (argvars[0].v_type != VAR_LIST
14934 || argvars[0].vval.v_list == NULL
14935 || argvars[0].vval.v_list->lv_len < 1)
14936 {
14937 EMSG(_(e_invarg));
14938 return;
14939 }
14940 else
14941 {
14942 list_T *l = argvars[0].vval.v_list;
14943 listitem_T *li;
14944 char_u *s;
14945
14946#ifdef USE_ARGV
14947 /* Pass argv[] to mch_call_shell(). */
14948 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14949 if (argv == NULL)
14950 return;
14951#endif
14952 for (li = l->lv_first; li != NULL; li = li->li_next)
14953 {
14954 s = get_tv_string_chk(&li->li_tv);
14955 if (s == NULL)
14956 goto theend;
14957#ifdef USE_ARGV
14958 argv[argc++] = (char *)s;
14959#else
14960 if (li != l->lv_first)
14961 {
14962 s = vim_strsave_shellescape(s, FALSE, TRUE);
14963 if (s == NULL)
14964 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014965 ga_concat(&ga, s);
14966 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014967 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014968 else
14969 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014970 if (li->li_next != NULL)
14971 ga_append(&ga, ' ');
14972#endif
14973 }
14974#ifdef USE_ARGV
14975 argv[argc] = NULL;
14976#else
14977 cmd = ga.ga_data;
14978#endif
14979 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014980
Bram Moolenaar835dc632016-02-07 14:27:38 +010014981#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014982# ifdef FEAT_CHANNEL
14983 if (ch_log_active())
14984 {
14985 garray_T ga;
14986 int i;
14987
14988 ga_init2(&ga, (int)sizeof(char), 200);
14989 for (i = 0; i < argc; ++i)
14990 {
14991 if (i > 0)
14992 ga_concat(&ga, (char_u *)" ");
14993 ga_concat(&ga, (char_u *)argv[i]);
14994 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010014995 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014996 ga_clear(&ga);
14997 }
14998# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014999 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015000#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015001# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015002 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015003# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015004 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015005#endif
15006
15007theend:
15008#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015009 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015010#else
15011 vim_free(ga.ga_data);
15012#endif
15013}
15014
15015/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015016 * Get the status of "job" and invoke the exit callback when needed.
15017 * The returned string is not allocated.
15018 */
15019 static char *
15020job_status(job_T *job)
15021{
15022 char *result;
15023
15024 if (job->jv_status == JOB_ENDED)
15025 /* No need to check, dead is dead. */
15026 result = "dead";
15027 else if (job->jv_status == JOB_FAILED)
15028 result = "fail";
15029 else
15030 {
15031 result = mch_job_status(job);
15032# ifdef FEAT_CHANNEL
15033 if (job->jv_status == JOB_ENDED)
15034 ch_log(job->jv_channel, "Job ended");
15035# endif
15036 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15037 {
15038 typval_T argv[3];
15039 typval_T rettv;
15040 int dummy;
15041
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015042 /* invoke the exit callback; make sure the refcount is > 0 */
15043 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015044 argv[0].v_type = VAR_JOB;
15045 argv[0].vval.v_job = job;
15046 argv[1].v_type = VAR_NUMBER;
15047 argv[1].vval.v_number = job->jv_exitval;
15048 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15049 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15050 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015051 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015052 }
15053 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15054 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015055 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015056 * freed. Careful: caller must not use "job" after this! */
15057 job_free(job);
15058 }
15059 }
15060 return result;
15061}
15062
15063/*
15064 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15065 */
15066 void
15067job_check_ended()
15068{
15069 static time_t last_check = 0;
15070 time_t now;
15071 job_T *job;
15072 job_T *next;
15073
15074 /* Only do this once in 10 seconds. */
15075 now = time(NULL);
15076 if (last_check + 10 < now)
15077 {
15078 last_check = now;
15079 for (job = first_job; job != NULL; job = next)
15080 {
15081 next = job->jv_next;
15082 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15083 job_status(job); /* may free "job" */
15084 }
15085 }
15086}
15087
15088/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015089 * "job_status()" function
15090 */
15091 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015092f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015093{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015094 job_T *job = get_job_arg(&argvars[0]);
15095 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015096
Bram Moolenaar65edff82016-02-21 16:40:11 +010015097 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015098 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015099 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015100 rettv->v_type = VAR_STRING;
15101 rettv->vval.v_string = vim_strsave((char_u *)result);
15102 }
15103}
15104
15105/*
15106 * "job_stop()" function
15107 */
15108 static void
15109f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15110{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015111 job_T *job = get_job_arg(&argvars[0]);
15112
15113 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015114 {
15115 char_u *arg;
15116
15117 if (argvars[1].v_type == VAR_UNKNOWN)
15118 arg = (char_u *)"";
15119 else
15120 {
15121 arg = get_tv_string_chk(&argvars[1]);
15122 if (arg == NULL)
15123 {
15124 EMSG(_(e_invarg));
15125 return;
15126 }
15127 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015128 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015129 rettv->vval.v_number = 0;
15130 else
15131 rettv->vval.v_number = 1;
15132 }
15133}
15134#endif
15135
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015137 * "join()" function
15138 */
15139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015140f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015141{
15142 garray_T ga;
15143 char_u *sep;
15144
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015145 if (argvars[0].v_type != VAR_LIST)
15146 {
15147 EMSG(_(e_listreq));
15148 return;
15149 }
15150 if (argvars[0].vval.v_list == NULL)
15151 return;
15152 if (argvars[1].v_type == VAR_UNKNOWN)
15153 sep = (char_u *)" ";
15154 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015155 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015156
15157 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015158
15159 if (sep != NULL)
15160 {
15161 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015162 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015163 ga_append(&ga, NUL);
15164 rettv->vval.v_string = (char_u *)ga.ga_data;
15165 }
15166 else
15167 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015168}
15169
15170/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015171 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015172 */
15173 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015174f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015175{
15176 js_read_T reader;
15177
15178 reader.js_buf = get_tv_string(&argvars[0]);
15179 reader.js_fill = NULL;
15180 reader.js_used = 0;
15181 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15182 EMSG(_(e_invarg));
15183}
15184
15185/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015186 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015187 */
15188 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015189f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015190{
15191 rettv->v_type = VAR_STRING;
15192 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15193}
15194
15195/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015196 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015197 */
15198 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015199f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015200{
15201 js_read_T reader;
15202
15203 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015204 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015205 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015206 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015207 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015208}
15209
15210/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015211 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015212 */
15213 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015214f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015215{
15216 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015217 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015218}
15219
15220/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015221 * "keys()" function
15222 */
15223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015224f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015225{
15226 dict_list(argvars, rettv, 0);
15227}
15228
15229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 * "last_buffer_nr()" function.
15231 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015233f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234{
15235 int n = 0;
15236 buf_T *buf;
15237
15238 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15239 if (n < buf->b_fnum)
15240 n = buf->b_fnum;
15241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015242 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015243}
15244
15245/*
15246 * "len()" function
15247 */
15248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015249f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015250{
15251 switch (argvars[0].v_type)
15252 {
15253 case VAR_STRING:
15254 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015255 rettv->vval.v_number = (varnumber_T)STRLEN(
15256 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015257 break;
15258 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015259 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015260 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015261 case VAR_DICT:
15262 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15263 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015264 case VAR_UNKNOWN:
15265 case VAR_SPECIAL:
15266 case VAR_FLOAT:
15267 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015268 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015269 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015270 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015271 break;
15272 }
15273}
15274
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015275static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015276
15277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015278libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015279{
15280#ifdef FEAT_LIBCALL
15281 char_u *string_in;
15282 char_u **string_result;
15283 int nr_result;
15284#endif
15285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015286 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015287 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015288 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015289
15290 if (check_restricted() || check_secure())
15291 return;
15292
15293#ifdef FEAT_LIBCALL
15294 /* The first two args must be strings, otherwise its meaningless */
15295 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15296 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015297 string_in = NULL;
15298 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015299 string_in = argvars[2].vval.v_string;
15300 if (type == VAR_NUMBER)
15301 string_result = NULL;
15302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015303 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015304 if (mch_libcall(argvars[0].vval.v_string,
15305 argvars[1].vval.v_string,
15306 string_in,
15307 argvars[2].vval.v_number,
15308 string_result,
15309 &nr_result) == OK
15310 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015311 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015312 }
15313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314}
15315
15316/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015317 * "libcall()" function
15318 */
15319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015320f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015321{
15322 libcall_common(argvars, rettv, VAR_STRING);
15323}
15324
15325/*
15326 * "libcallnr()" function
15327 */
15328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015329f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015330{
15331 libcall_common(argvars, rettv, VAR_NUMBER);
15332}
15333
15334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 * "line(string)" function
15336 */
15337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015338f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339{
15340 linenr_T lnum = 0;
15341 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015342 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015344 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 if (fp != NULL)
15346 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015347 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348}
15349
15350/*
15351 * "line2byte(lnum)" function
15352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015354f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355{
15356#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358#else
15359 linenr_T lnum;
15360
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015361 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015365 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15366 if (rettv->vval.v_number >= 0)
15367 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368#endif
15369}
15370
15371/*
15372 * "lispindent(lnum)" function
15373 */
15374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015375f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376{
15377#ifdef FEAT_LISP
15378 pos_T pos;
15379 linenr_T lnum;
15380
15381 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015382 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15384 {
15385 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 curwin->w_cursor = pos;
15388 }
15389 else
15390#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392}
15393
15394/*
15395 * "localtime()" function
15396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015398f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401}
15402
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015403static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404
15405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015406get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407{
15408 char_u *keys;
15409 char_u *which;
15410 char_u buf[NUMBUFLEN];
15411 char_u *keys_buf = NULL;
15412 char_u *rhs;
15413 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015414 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015415 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015416 mapblock_T *mp;
15417 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418
15419 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 rettv->v_type = VAR_STRING;
15421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424 if (*keys == NUL)
15425 return;
15426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015427 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015429 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015430 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015431 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015432 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015433 if (argvars[3].v_type != VAR_UNKNOWN)
15434 get_dict = get_tv_number(&argvars[3]);
15435 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 else
15438 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015439 if (which == NULL)
15440 return;
15441
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 mode = get_map_mode(&which, 0);
15443
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015444 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015445 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015447
15448 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015450 /* Return a string. */
15451 if (rhs != NULL)
15452 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453
Bram Moolenaarbd743252010-10-20 21:23:33 +020015454 }
15455 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15456 {
15457 /* Return a dictionary. */
15458 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15459 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15460 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461
Bram Moolenaarbd743252010-10-20 21:23:33 +020015462 dict_add_nr_str(dict, "lhs", 0L, lhs);
15463 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15464 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15465 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15466 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15467 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15468 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015469 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015470 dict_add_nr_str(dict, "mode", 0L, mapmode);
15471
15472 vim_free(lhs);
15473 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474 }
15475}
15476
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015477#ifdef FEAT_FLOAT
15478/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015479 * "log()" function
15480 */
15481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015482f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015483{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015484 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015485
15486 rettv->v_type = VAR_FLOAT;
15487 if (get_float_arg(argvars, &f) == OK)
15488 rettv->vval.v_float = log(f);
15489 else
15490 rettv->vval.v_float = 0.0;
15491}
15492
15493/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015494 * "log10()" function
15495 */
15496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015497f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015498{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015499 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015500
15501 rettv->v_type = VAR_FLOAT;
15502 if (get_float_arg(argvars, &f) == OK)
15503 rettv->vval.v_float = log10(f);
15504 else
15505 rettv->vval.v_float = 0.0;
15506}
15507#endif
15508
Bram Moolenaar1dced572012-04-05 16:54:08 +020015509#ifdef FEAT_LUA
15510/*
15511 * "luaeval()" function
15512 */
15513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015514f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015515{
15516 char_u *str;
15517 char_u buf[NUMBUFLEN];
15518
15519 str = get_tv_string_buf(&argvars[0], buf);
15520 do_luaeval(str, argvars + 1, rettv);
15521}
15522#endif
15523
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015525 * "map()" function
15526 */
15527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015528f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015529{
15530 filter_map(argvars, rettv, TRUE);
15531}
15532
15533/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015534 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 */
15536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015537f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015539 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540}
15541
15542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015543 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 */
15545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015546f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015548 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549}
15550
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015551static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552
15553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015554find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015555{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015556 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015557 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015558 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 char_u *pat;
15560 regmatch_T regmatch;
15561 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015562 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015563 char_u *save_cpo;
15564 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015565 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015566 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015567 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015568 list_T *l = NULL;
15569 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015570 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015571 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572
15573 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15574 save_cpo = p_cpo;
15575 p_cpo = (char_u *)"";
15576
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015577 rettv->vval.v_number = -1;
15578 if (type == 3)
15579 {
15580 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015581 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015582 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015583 }
15584 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015586 rettv->v_type = VAR_STRING;
15587 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015590 if (argvars[0].v_type == VAR_LIST)
15591 {
15592 if ((l = argvars[0].vval.v_list) == NULL)
15593 goto theend;
15594 li = l->lv_first;
15595 }
15596 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015597 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015598 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015599 len = (long)STRLEN(str);
15600 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015602 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15603 if (pat == NULL)
15604 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015605
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015606 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015608 int error = FALSE;
15609
15610 start = get_tv_number_chk(&argvars[2], &error);
15611 if (error)
15612 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015613 if (l != NULL)
15614 {
15615 li = list_find(l, start);
15616 if (li == NULL)
15617 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015618 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015619 }
15620 else
15621 {
15622 if (start < 0)
15623 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015624 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015625 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015626 /* When "count" argument is there ignore matches before "start",
15627 * otherwise skip part of the string. Differs when pattern is "^"
15628 * or "\<". */
15629 if (argvars[3].v_type != VAR_UNKNOWN)
15630 startcol = start;
15631 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015632 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015633 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015634 len -= start;
15635 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015636 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015637
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015638 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015639 nth = get_tv_number_chk(&argvars[3], &error);
15640 if (error)
15641 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 }
15643
15644 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15645 if (regmatch.regprog != NULL)
15646 {
15647 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015648
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015649 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015650 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015651 if (l != NULL)
15652 {
15653 if (li == NULL)
15654 {
15655 match = FALSE;
15656 break;
15657 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015658 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015659 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015660 if (str == NULL)
15661 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015662 }
15663
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015664 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015665
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015666 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015667 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015668 if (l == NULL && !match)
15669 break;
15670
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015671 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015672 if (l != NULL)
15673 {
15674 li = li->li_next;
15675 ++idx;
15676 }
15677 else
15678 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015679#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015680 startcol = (colnr_T)(regmatch.startp[0]
15681 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015682#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015683 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015684#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015685 if (startcol > (colnr_T)len
15686 || str + startcol <= regmatch.startp[0])
15687 {
15688 match = FALSE;
15689 break;
15690 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015691 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015692 }
15693
15694 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015696 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015697 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015698 int i;
15699
15700 /* return list with matched string and submatches */
15701 for (i = 0; i < NSUBEXP; ++i)
15702 {
15703 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015704 {
15705 if (list_append_string(rettv->vval.v_list,
15706 (char_u *)"", 0) == FAIL)
15707 break;
15708 }
15709 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015710 regmatch.startp[i],
15711 (int)(regmatch.endp[i] - regmatch.startp[i]))
15712 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015713 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015714 }
15715 }
15716 else if (type == 2)
15717 {
15718 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015719 if (l != NULL)
15720 copy_tv(&li->li_tv, rettv);
15721 else
15722 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015724 }
15725 else if (l != NULL)
15726 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727 else
15728 {
15729 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 (varnumber_T)(regmatch.startp[0] - str);
15732 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015733 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015735 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 }
15737 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015738 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 }
15740
15741theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015742 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743 p_cpo = save_cpo;
15744}
15745
15746/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015747 * "match()" function
15748 */
15749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015750f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015751{
15752 find_some_match(argvars, rettv, 1);
15753}
15754
15755/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015756 * "matchadd()" function
15757 */
15758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015759f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015760{
15761#ifdef FEAT_SEARCH_EXTRA
15762 char_u buf[NUMBUFLEN];
15763 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15764 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15765 int prio = 10; /* default priority */
15766 int id = -1;
15767 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015768 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015769
15770 rettv->vval.v_number = -1;
15771
15772 if (grp == NULL || pat == NULL)
15773 return;
15774 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015775 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015776 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015777 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015778 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015779 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015780 if (argvars[4].v_type != VAR_UNKNOWN)
15781 {
15782 if (argvars[4].v_type != VAR_DICT)
15783 {
15784 EMSG(_(e_dictreq));
15785 return;
15786 }
15787 if (dict_find(argvars[4].vval.v_dict,
15788 (char_u *)"conceal", -1) != NULL)
15789 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15790 (char_u *)"conceal", FALSE);
15791 }
15792 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015793 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015794 if (error == TRUE)
15795 return;
15796 if (id >= 1 && id <= 3)
15797 {
15798 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15799 return;
15800 }
15801
Bram Moolenaar6561d522015-07-21 15:48:27 +020015802 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15803 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015804#endif
15805}
15806
15807/*
15808 * "matchaddpos()" function
15809 */
15810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015811f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015812{
15813#ifdef FEAT_SEARCH_EXTRA
15814 char_u buf[NUMBUFLEN];
15815 char_u *group;
15816 int prio = 10;
15817 int id = -1;
15818 int error = FALSE;
15819 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015820 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015821
15822 rettv->vval.v_number = -1;
15823
15824 group = get_tv_string_buf_chk(&argvars[0], buf);
15825 if (group == NULL)
15826 return;
15827
15828 if (argvars[1].v_type != VAR_LIST)
15829 {
15830 EMSG2(_(e_listarg), "matchaddpos()");
15831 return;
15832 }
15833 l = argvars[1].vval.v_list;
15834 if (l == NULL)
15835 return;
15836
15837 if (argvars[2].v_type != VAR_UNKNOWN)
15838 {
15839 prio = get_tv_number_chk(&argvars[2], &error);
15840 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015841 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015842 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015843 if (argvars[4].v_type != VAR_UNKNOWN)
15844 {
15845 if (argvars[4].v_type != VAR_DICT)
15846 {
15847 EMSG(_(e_dictreq));
15848 return;
15849 }
15850 if (dict_find(argvars[4].vval.v_dict,
15851 (char_u *)"conceal", -1) != NULL)
15852 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15853 (char_u *)"conceal", FALSE);
15854 }
15855 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015856 }
15857 if (error == TRUE)
15858 return;
15859
15860 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15861 if (id == 1 || id == 2)
15862 {
15863 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15864 return;
15865 }
15866
Bram Moolenaar6561d522015-07-21 15:48:27 +020015867 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15868 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015869#endif
15870}
15871
15872/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015873 * "matcharg()" function
15874 */
15875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015876f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015877{
15878 if (rettv_list_alloc(rettv) == OK)
15879 {
15880#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015881 int id = get_tv_number(&argvars[0]);
15882 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015883
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015884 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015885 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015886 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15887 {
15888 list_append_string(rettv->vval.v_list,
15889 syn_id2name(m->hlg_id), -1);
15890 list_append_string(rettv->vval.v_list, m->pattern, -1);
15891 }
15892 else
15893 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015894 list_append_string(rettv->vval.v_list, NULL, -1);
15895 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015896 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015897 }
15898#endif
15899 }
15900}
15901
15902/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015903 * "matchdelete()" function
15904 */
15905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015906f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015907{
15908#ifdef FEAT_SEARCH_EXTRA
15909 rettv->vval.v_number = match_delete(curwin,
15910 (int)get_tv_number(&argvars[0]), TRUE);
15911#endif
15912}
15913
15914/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015915 * "matchend()" function
15916 */
15917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015918f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015919{
15920 find_some_match(argvars, rettv, 0);
15921}
15922
15923/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015924 * "matchlist()" function
15925 */
15926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015927f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015928{
15929 find_some_match(argvars, rettv, 3);
15930}
15931
15932/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015933 * "matchstr()" function
15934 */
15935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015936f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015937{
15938 find_some_match(argvars, rettv, 2);
15939}
15940
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015941static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015942
15943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015944max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015945{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015946 long n = 0;
15947 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015948 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015949
15950 if (argvars[0].v_type == VAR_LIST)
15951 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015952 list_T *l;
15953 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015954
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015955 l = argvars[0].vval.v_list;
15956 if (l != NULL)
15957 {
15958 li = l->lv_first;
15959 if (li != NULL)
15960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015961 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015962 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015963 {
15964 li = li->li_next;
15965 if (li == NULL)
15966 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015967 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015968 if (domax ? i > n : i < n)
15969 n = i;
15970 }
15971 }
15972 }
15973 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015974 else if (argvars[0].v_type == VAR_DICT)
15975 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015976 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015977 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015979 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015980
15981 d = argvars[0].vval.v_dict;
15982 if (d != NULL)
15983 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015984 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015985 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015986 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015987 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015988 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015989 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015990 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015991 if (first)
15992 {
15993 n = i;
15994 first = FALSE;
15995 }
15996 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015997 n = i;
15998 }
15999 }
16000 }
16001 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016002 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016003 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016004 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016005}
16006
16007/*
16008 * "max()" function
16009 */
16010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016011f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016012{
16013 max_min(argvars, rettv, TRUE);
16014}
16015
16016/*
16017 * "min()" function
16018 */
16019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016020f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016021{
16022 max_min(argvars, rettv, FALSE);
16023}
16024
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016025static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016026
16027/*
16028 * Create the directory in which "dir" is located, and higher levels when
16029 * needed.
16030 */
16031 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016032mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016033{
16034 char_u *p;
16035 char_u *updir;
16036 int r = FAIL;
16037
16038 /* Get end of directory name in "dir".
16039 * We're done when it's "/" or "c:/". */
16040 p = gettail_sep(dir);
16041 if (p <= get_past_head(dir))
16042 return OK;
16043
16044 /* If the directory exists we're done. Otherwise: create it.*/
16045 updir = vim_strnsave(dir, (int)(p - dir));
16046 if (updir == NULL)
16047 return FAIL;
16048 if (mch_isdir(updir))
16049 r = OK;
16050 else if (mkdir_recurse(updir, prot) == OK)
16051 r = vim_mkdir_emsg(updir, prot);
16052 vim_free(updir);
16053 return r;
16054}
16055
16056#ifdef vim_mkdir
16057/*
16058 * "mkdir()" function
16059 */
16060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016061f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016062{
16063 char_u *dir;
16064 char_u buf[NUMBUFLEN];
16065 int prot = 0755;
16066
16067 rettv->vval.v_number = FAIL;
16068 if (check_restricted() || check_secure())
16069 return;
16070
16071 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016072 if (*dir == NUL)
16073 rettv->vval.v_number = FAIL;
16074 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016075 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016076 if (*gettail(dir) == NUL)
16077 /* remove trailing slashes */
16078 *gettail_sep(dir) = NUL;
16079
16080 if (argvars[1].v_type != VAR_UNKNOWN)
16081 {
16082 if (argvars[2].v_type != VAR_UNKNOWN)
16083 prot = get_tv_number_chk(&argvars[2], NULL);
16084 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16085 mkdir_recurse(dir, prot);
16086 }
16087 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016088 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016089}
16090#endif
16091
Bram Moolenaar0d660222005-01-07 21:51:51 +000016092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016093 * "mode()" function
16094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016096f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016097{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016098 char_u buf[3];
16099
16100 buf[1] = NUL;
16101 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 if (VIsual_active)
16104 {
16105 if (VIsual_select)
16106 buf[0] = VIsual_mode + 's' - 'v';
16107 else
16108 buf[0] = VIsual_mode;
16109 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016110 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016111 || State == CONFIRM)
16112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016114 if (State == ASKMORE)
16115 buf[1] = 'm';
16116 else if (State == CONFIRM)
16117 buf[1] = '?';
16118 }
16119 else if (State == EXTERNCMD)
16120 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016121 else if (State & INSERT)
16122 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016123#ifdef FEAT_VREPLACE
16124 if (State & VREPLACE_FLAG)
16125 {
16126 buf[0] = 'R';
16127 buf[1] = 'v';
16128 }
16129 else
16130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131 if (State & REPLACE_FLAG)
16132 buf[0] = 'R';
16133 else
16134 buf[0] = 'i';
16135 }
16136 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016137 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016139 if (exmode_active)
16140 buf[1] = 'v';
16141 }
16142 else if (exmode_active)
16143 {
16144 buf[0] = 'c';
16145 buf[1] = 'e';
16146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016150 if (finish_op)
16151 buf[1] = 'o';
16152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016154 /* Clear out the minor mode when the argument is not a non-zero number or
16155 * non-empty string. */
16156 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016157 buf[1] = NUL;
16158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016159 rettv->vval.v_string = vim_strsave(buf);
16160 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161}
16162
Bram Moolenaar429fa852013-04-15 12:27:36 +020016163#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016164/*
16165 * "mzeval()" function
16166 */
16167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016168f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016169{
16170 char_u *str;
16171 char_u buf[NUMBUFLEN];
16172
16173 str = get_tv_string_buf(&argvars[0], buf);
16174 do_mzeval(str, rettv);
16175}
Bram Moolenaar75676462013-01-30 14:55:42 +010016176
16177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016178mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016179{
16180 typval_T argvars[3];
16181
16182 argvars[0].v_type = VAR_STRING;
16183 argvars[0].vval.v_string = name;
16184 copy_tv(args, &argvars[1]);
16185 argvars[2].v_type = VAR_UNKNOWN;
16186 f_call(argvars, rettv);
16187 clear_tv(&argvars[1]);
16188}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016189#endif
16190
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016192 * "nextnonblank()" function
16193 */
16194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016195f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016196{
16197 linenr_T lnum;
16198
16199 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016201 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016202 {
16203 lnum = 0;
16204 break;
16205 }
16206 if (*skipwhite(ml_get(lnum)) != NUL)
16207 break;
16208 }
16209 rettv->vval.v_number = lnum;
16210}
16211
16212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 * "nr2char()" function
16214 */
16215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016216f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217{
16218 char_u buf[NUMBUFLEN];
16219
16220#ifdef FEAT_MBYTE
16221 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016222 {
16223 int utf8 = 0;
16224
16225 if (argvars[1].v_type != VAR_UNKNOWN)
16226 utf8 = get_tv_number_chk(&argvars[1], NULL);
16227 if (utf8)
16228 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16229 else
16230 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 else
16233#endif
16234 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016235 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 buf[1] = NUL;
16237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016238 rettv->v_type = VAR_STRING;
16239 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016240}
16241
16242/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016243 * "or(expr, expr)" function
16244 */
16245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016246f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016247{
16248 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16249 | get_tv_number_chk(&argvars[1], NULL);
16250}
16251
16252/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016253 * "pathshorten()" function
16254 */
16255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016256f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016257{
16258 char_u *p;
16259
16260 rettv->v_type = VAR_STRING;
16261 p = get_tv_string_chk(&argvars[0]);
16262 if (p == NULL)
16263 rettv->vval.v_string = NULL;
16264 else
16265 {
16266 p = vim_strsave(p);
16267 rettv->vval.v_string = p;
16268 if (p != NULL)
16269 shorten_dir(p);
16270 }
16271}
16272
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016273#ifdef FEAT_PERL
16274/*
16275 * "perleval()" function
16276 */
16277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016278f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016279{
16280 char_u *str;
16281 char_u buf[NUMBUFLEN];
16282
16283 str = get_tv_string_buf(&argvars[0], buf);
16284 do_perleval(str, rettv);
16285}
16286#endif
16287
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016288#ifdef FEAT_FLOAT
16289/*
16290 * "pow()" function
16291 */
16292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016293f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016294{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016295 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016296
16297 rettv->v_type = VAR_FLOAT;
16298 if (get_float_arg(argvars, &fx) == OK
16299 && get_float_arg(&argvars[1], &fy) == OK)
16300 rettv->vval.v_float = pow(fx, fy);
16301 else
16302 rettv->vval.v_float = 0.0;
16303}
16304#endif
16305
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016306/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016307 * "prevnonblank()" function
16308 */
16309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016310f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016311{
16312 linenr_T lnum;
16313
16314 lnum = get_tv_lnum(argvars);
16315 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16316 lnum = 0;
16317 else
16318 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16319 --lnum;
16320 rettv->vval.v_number = lnum;
16321}
16322
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016323/* This dummy va_list is here because:
16324 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16325 * - locally in the function results in a "used before set" warning
16326 * - using va_start() to initialize it gives "function with fixed args" error */
16327static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016328
Bram Moolenaar8c711452005-01-14 21:53:12 +000016329/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016330 * "printf()" function
16331 */
16332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016333f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016334{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016335 char_u buf[NUMBUFLEN];
16336 int len;
16337 char_u *s;
16338 int saved_did_emsg = did_emsg;
16339 char *fmt;
16340
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016341 rettv->v_type = VAR_STRING;
16342 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016343
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016344 /* Get the required length, allocate the buffer and do it for real. */
16345 did_emsg = FALSE;
16346 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16347 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16348 if (!did_emsg)
16349 {
16350 s = alloc(len + 1);
16351 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016352 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016353 rettv->vval.v_string = s;
16354 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016355 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016356 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016357 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016358}
16359
16360/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016361 * "pumvisible()" function
16362 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016364f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016365{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016366#ifdef FEAT_INS_EXPAND
16367 if (pum_visible())
16368 rettv->vval.v_number = 1;
16369#endif
16370}
16371
Bram Moolenaardb913952012-06-29 12:54:53 +020016372#ifdef FEAT_PYTHON3
16373/*
16374 * "py3eval()" function
16375 */
16376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016377f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016378{
16379 char_u *str;
16380 char_u buf[NUMBUFLEN];
16381
16382 str = get_tv_string_buf(&argvars[0], buf);
16383 do_py3eval(str, rettv);
16384}
16385#endif
16386
16387#ifdef FEAT_PYTHON
16388/*
16389 * "pyeval()" function
16390 */
16391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016392f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016393{
16394 char_u *str;
16395 char_u buf[NUMBUFLEN];
16396
16397 str = get_tv_string_buf(&argvars[0], buf);
16398 do_pyeval(str, rettv);
16399}
16400#endif
16401
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016402/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016403 * "range()" function
16404 */
16405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016406f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016407{
16408 long start;
16409 long end;
16410 long stride = 1;
16411 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016412 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016414 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016415 if (argvars[1].v_type == VAR_UNKNOWN)
16416 {
16417 end = start - 1;
16418 start = 0;
16419 }
16420 else
16421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016422 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016423 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016424 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016425 }
16426
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016427 if (error)
16428 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016429 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016430 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016431 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016432 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016433 else
16434 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016435 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016436 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016437 if (list_append_number(rettv->vval.v_list,
16438 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016439 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016440 }
16441}
16442
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016443/*
16444 * "readfile()" function
16445 */
16446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016447f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016448{
16449 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016450 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016451 char_u *fname;
16452 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016453 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16454 int io_size = sizeof(buf);
16455 int readlen; /* size of last fread() */
16456 char_u *prev = NULL; /* previously read bytes, if any */
16457 long prevlen = 0; /* length of data in prev */
16458 long prevsize = 0; /* size of prev buffer */
16459 long maxline = MAXLNUM;
16460 long cnt = 0;
16461 char_u *p; /* position in buf */
16462 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016463
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016464 if (argvars[1].v_type != VAR_UNKNOWN)
16465 {
16466 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16467 binary = TRUE;
16468 if (argvars[2].v_type != VAR_UNKNOWN)
16469 maxline = get_tv_number(&argvars[2]);
16470 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016471
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016472 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016473 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016474
16475 /* Always open the file in binary mode, library functions have a mind of
16476 * their own about CR-LF conversion. */
16477 fname = get_tv_string(&argvars[0]);
16478 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16479 {
16480 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16481 return;
16482 }
16483
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016484 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016485 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016486 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016487
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016488 /* This for loop processes what was read, but is also entered at end
16489 * of file so that either:
16490 * - an incomplete line gets written
16491 * - a "binary" file gets an empty line at the end if it ends in a
16492 * newline. */
16493 for (p = buf, start = buf;
16494 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16495 ++p)
16496 {
16497 if (*p == '\n' || readlen <= 0)
16498 {
16499 listitem_T *li;
16500 char_u *s = NULL;
16501 long_u len = p - start;
16502
16503 /* Finished a line. Remove CRs before NL. */
16504 if (readlen > 0 && !binary)
16505 {
16506 while (len > 0 && start[len - 1] == '\r')
16507 --len;
16508 /* removal may cross back to the "prev" string */
16509 if (len == 0)
16510 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16511 --prevlen;
16512 }
16513 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016514 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016515 else
16516 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016517 /* Change "prev" buffer to be the right size. This way
16518 * the bytes are only copied once, and very long lines are
16519 * allocated only once. */
16520 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016521 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016522 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016523 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016524 prev = NULL; /* the list will own the string */
16525 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016526 }
16527 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016528 if (s == NULL)
16529 {
16530 do_outofmem_msg((long_u) prevlen + len + 1);
16531 failed = TRUE;
16532 break;
16533 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016534
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016535 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016536 {
16537 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016538 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016539 break;
16540 }
16541 li->li_tv.v_type = VAR_STRING;
16542 li->li_tv.v_lock = 0;
16543 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016544 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016545
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016546 start = p + 1; /* step over newline */
16547 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016548 break;
16549 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016550 else if (*p == NUL)
16551 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016552#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016553 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16554 * when finding the BF and check the previous two bytes. */
16555 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016556 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016557 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16558 * + 1, these may be in the "prev" string. */
16559 char_u back1 = p >= buf + 1 ? p[-1]
16560 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16561 char_u back2 = p >= buf + 2 ? p[-2]
16562 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16563 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016564
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016565 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016566 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016567 char_u *dest = p - 2;
16568
16569 /* Usually a BOM is at the beginning of a file, and so at
16570 * the beginning of a line; then we can just step over it.
16571 */
16572 if (start == dest)
16573 start = p + 1;
16574 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016575 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016576 /* have to shuffle buf to close gap */
16577 int adjust_prevlen = 0;
16578
16579 if (dest < buf)
16580 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016581 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016582 dest = buf;
16583 }
16584 if (readlen > p - buf + 1)
16585 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16586 readlen -= 3 - adjust_prevlen;
16587 prevlen -= adjust_prevlen;
16588 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016589 }
16590 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016591 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016592#endif
16593 } /* for */
16594
16595 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16596 break;
16597 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016598 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016599 /* There's part of a line in buf, store it in "prev". */
16600 if (p - start + prevlen >= prevsize)
16601 {
16602 /* need bigger "prev" buffer */
16603 char_u *newprev;
16604
16605 /* A common use case is ordinary text files and "prev" gets a
16606 * fragment of a line, so the first allocation is made
16607 * small, to avoid repeatedly 'allocing' large and
16608 * 'reallocing' small. */
16609 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016610 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016611 else
16612 {
16613 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016614 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016615 prevsize = grow50pc > growmin ? grow50pc : growmin;
16616 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016617 newprev = prev == NULL ? alloc(prevsize)
16618 : vim_realloc(prev, prevsize);
16619 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016620 {
16621 do_outofmem_msg((long_u)prevsize);
16622 failed = TRUE;
16623 break;
16624 }
16625 prev = newprev;
16626 }
16627 /* Add the line part to end of "prev". */
16628 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016629 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016630 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016631 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016632
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016633 /*
16634 * For a negative line count use only the lines at the end of the file,
16635 * free the rest.
16636 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016637 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016638 while (cnt > -maxline)
16639 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016640 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016641 --cnt;
16642 }
16643
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016644 if (failed)
16645 {
16646 list_free(rettv->vval.v_list, TRUE);
16647 /* readfile doc says an empty list is returned on error */
16648 rettv->vval.v_list = list_alloc();
16649 }
16650
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016651 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016652 fclose(fd);
16653}
16654
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016655#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016656static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016657
16658/*
16659 * Convert a List to proftime_T.
16660 * Return FAIL when there is something wrong.
16661 */
16662 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016663list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016664{
16665 long n1, n2;
16666 int error = FALSE;
16667
16668 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16669 || arg->vval.v_list->lv_len != 2)
16670 return FAIL;
16671 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16672 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16673# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016674 tm->HighPart = n1;
16675 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016676# else
16677 tm->tv_sec = n1;
16678 tm->tv_usec = n2;
16679# endif
16680 return error ? FAIL : OK;
16681}
16682#endif /* FEAT_RELTIME */
16683
16684/*
16685 * "reltime()" function
16686 */
16687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016688f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016689{
16690#ifdef FEAT_RELTIME
16691 proftime_T res;
16692 proftime_T start;
16693
16694 if (argvars[0].v_type == VAR_UNKNOWN)
16695 {
16696 /* No arguments: get current time. */
16697 profile_start(&res);
16698 }
16699 else if (argvars[1].v_type == VAR_UNKNOWN)
16700 {
16701 if (list2proftime(&argvars[0], &res) == FAIL)
16702 return;
16703 profile_end(&res);
16704 }
16705 else
16706 {
16707 /* Two arguments: compute the difference. */
16708 if (list2proftime(&argvars[0], &start) == FAIL
16709 || list2proftime(&argvars[1], &res) == FAIL)
16710 return;
16711 profile_sub(&res, &start);
16712 }
16713
16714 if (rettv_list_alloc(rettv) == OK)
16715 {
16716 long n1, n2;
16717
16718# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016719 n1 = res.HighPart;
16720 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016721# else
16722 n1 = res.tv_sec;
16723 n2 = res.tv_usec;
16724# endif
16725 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16726 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16727 }
16728#endif
16729}
16730
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016731#ifdef FEAT_FLOAT
16732/*
16733 * "reltimefloat()" function
16734 */
16735 static void
16736f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16737{
16738# ifdef FEAT_RELTIME
16739 proftime_T tm;
16740# endif
16741
16742 rettv->v_type = VAR_FLOAT;
16743 rettv->vval.v_float = 0;
16744# ifdef FEAT_RELTIME
16745 if (list2proftime(&argvars[0], &tm) == OK)
16746 rettv->vval.v_float = profile_float(&tm);
16747# endif
16748}
16749#endif
16750
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016751/*
16752 * "reltimestr()" function
16753 */
16754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016755f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016756{
16757#ifdef FEAT_RELTIME
16758 proftime_T tm;
16759#endif
16760
16761 rettv->v_type = VAR_STRING;
16762 rettv->vval.v_string = NULL;
16763#ifdef FEAT_RELTIME
16764 if (list2proftime(&argvars[0], &tm) == OK)
16765 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16766#endif
16767}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016768
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016770static void make_connection(void);
16771static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016772
16773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016774make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016775{
16776 if (X_DISPLAY == NULL
16777# ifdef FEAT_GUI
16778 && !gui.in_use
16779# endif
16780 )
16781 {
16782 x_force_connect = TRUE;
16783 setup_term_clip();
16784 x_force_connect = FALSE;
16785 }
16786}
16787
16788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016789check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016790{
16791 make_connection();
16792 if (X_DISPLAY == NULL)
16793 {
16794 EMSG(_("E240: No connection to Vim server"));
16795 return FAIL;
16796 }
16797 return OK;
16798}
16799#endif
16800
16801#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016802static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016803
16804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016805remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016806{
16807 char_u *server_name;
16808 char_u *keys;
16809 char_u *r = NULL;
16810 char_u buf[NUMBUFLEN];
16811# ifdef WIN32
16812 HWND w;
16813# else
16814 Window w;
16815# endif
16816
16817 if (check_restricted() || check_secure())
16818 return;
16819
16820# ifdef FEAT_X11
16821 if (check_connection() == FAIL)
16822 return;
16823# endif
16824
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016825 server_name = get_tv_string_chk(&argvars[0]);
16826 if (server_name == NULL)
16827 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016828 keys = get_tv_string_buf(&argvars[1], buf);
16829# ifdef WIN32
16830 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16831# else
16832 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16833 < 0)
16834# endif
16835 {
16836 if (r != NULL)
16837 EMSG(r); /* sending worked but evaluation failed */
16838 else
16839 EMSG2(_("E241: Unable to send to %s"), server_name);
16840 return;
16841 }
16842
16843 rettv->vval.v_string = r;
16844
16845 if (argvars[2].v_type != VAR_UNKNOWN)
16846 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016847 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016848 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016849 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016850
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016851 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016852 v.di_tv.v_type = VAR_STRING;
16853 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016854 idvar = get_tv_string_chk(&argvars[2]);
16855 if (idvar != NULL)
16856 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016857 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016858 }
16859}
16860#endif
16861
16862/*
16863 * "remote_expr()" function
16864 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016866f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016867{
16868 rettv->v_type = VAR_STRING;
16869 rettv->vval.v_string = NULL;
16870#ifdef FEAT_CLIENTSERVER
16871 remote_common(argvars, rettv, TRUE);
16872#endif
16873}
16874
16875/*
16876 * "remote_foreground()" function
16877 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016879f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016880{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016881#ifdef FEAT_CLIENTSERVER
16882# ifdef WIN32
16883 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016884 {
16885 char_u *server_name = get_tv_string_chk(&argvars[0]);
16886
16887 if (server_name != NULL)
16888 serverForeground(server_name);
16889 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016890# else
16891 /* Send a foreground() expression to the server. */
16892 argvars[1].v_type = VAR_STRING;
16893 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16894 argvars[2].v_type = VAR_UNKNOWN;
16895 remote_common(argvars, rettv, TRUE);
16896 vim_free(argvars[1].vval.v_string);
16897# endif
16898#endif
16899}
16900
Bram Moolenaar0d660222005-01-07 21:51:51 +000016901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016902f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016903{
16904#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016905 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906 char_u *s = NULL;
16907# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016908 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016910 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911
16912 if (check_restricted() || check_secure())
16913 {
16914 rettv->vval.v_number = -1;
16915 return;
16916 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016917 serverid = get_tv_string_chk(&argvars[0]);
16918 if (serverid == NULL)
16919 {
16920 rettv->vval.v_number = -1;
16921 return; /* type error; errmsg already given */
16922 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016924 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925 if (n == 0)
16926 rettv->vval.v_number = -1;
16927 else
16928 {
16929 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16930 rettv->vval.v_number = (s != NULL);
16931 }
16932# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 if (check_connection() == FAIL)
16934 return;
16935
16936 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016937 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938# endif
16939
16940 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016942 char_u *retvar;
16943
Bram Moolenaar33570922005-01-25 22:26:29 +000016944 v.di_tv.v_type = VAR_STRING;
16945 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016946 retvar = get_tv_string_chk(&argvars[1]);
16947 if (retvar != NULL)
16948 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016949 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950 }
16951#else
16952 rettv->vval.v_number = -1;
16953#endif
16954}
16955
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016957f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958{
16959 char_u *r = NULL;
16960
16961#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016962 char_u *serverid = get_tv_string_chk(&argvars[0]);
16963
16964 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 {
16966# ifdef WIN32
16967 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016968 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016970 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971 if (n != 0)
16972 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16973 if (r == NULL)
16974# else
16975 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016976 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977# endif
16978 EMSG(_("E277: Unable to read a server reply"));
16979 }
16980#endif
16981 rettv->v_type = VAR_STRING;
16982 rettv->vval.v_string = r;
16983}
16984
16985/*
16986 * "remote_send()" function
16987 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016989f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016990{
16991 rettv->v_type = VAR_STRING;
16992 rettv->vval.v_string = NULL;
16993#ifdef FEAT_CLIENTSERVER
16994 remote_common(argvars, rettv, FALSE);
16995#endif
16996}
16997
16998/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016999 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017000 */
17001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017002f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017003{
Bram Moolenaar33570922005-01-25 22:26:29 +000017004 list_T *l;
17005 listitem_T *item, *item2;
17006 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017007 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017008 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017009 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017010 dict_T *d;
17011 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017012 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017013
Bram Moolenaar8c711452005-01-14 21:53:12 +000017014 if (argvars[0].v_type == VAR_DICT)
17015 {
17016 if (argvars[2].v_type != VAR_UNKNOWN)
17017 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017018 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017019 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 key = get_tv_string_chk(&argvars[1]);
17022 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017024 di = dict_find(d, key, -1);
17025 if (di == NULL)
17026 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017027 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17028 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017029 {
17030 *rettv = di->di_tv;
17031 init_tv(&di->di_tv);
17032 dictitem_remove(d, di);
17033 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017034 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017035 }
17036 }
17037 else if (argvars[0].v_type != VAR_LIST)
17038 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017039 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017040 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017042 int error = FALSE;
17043
17044 idx = get_tv_number_chk(&argvars[1], &error);
17045 if (error)
17046 ; /* type error: do nothing, errmsg already given */
17047 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017048 EMSGN(_(e_listidx), idx);
17049 else
17050 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017051 if (argvars[2].v_type == VAR_UNKNOWN)
17052 {
17053 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017054 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017055 *rettv = item->li_tv;
17056 vim_free(item);
17057 }
17058 else
17059 {
17060 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017061 end = get_tv_number_chk(&argvars[2], &error);
17062 if (error)
17063 ; /* type error: do nothing */
17064 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017065 EMSGN(_(e_listidx), end);
17066 else
17067 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017068 int cnt = 0;
17069
17070 for (li = item; li != NULL; li = li->li_next)
17071 {
17072 ++cnt;
17073 if (li == item2)
17074 break;
17075 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017076 if (li == NULL) /* didn't find "item2" after "item" */
17077 EMSG(_(e_invrange));
17078 else
17079 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017080 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017081 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017082 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017083 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017084 l->lv_first = item;
17085 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017086 item->li_prev = NULL;
17087 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017088 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017089 }
17090 }
17091 }
17092 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017093 }
17094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095}
17096
17097/*
17098 * "rename({from}, {to})" function
17099 */
17100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017101f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102{
17103 char_u buf[NUMBUFLEN];
17104
17105 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017106 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017108 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17109 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110}
17111
17112/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017113 * "repeat()" function
17114 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017116f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017117{
17118 char_u *p;
17119 int n;
17120 int slen;
17121 int len;
17122 char_u *r;
17123 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017124
17125 n = get_tv_number(&argvars[1]);
17126 if (argvars[0].v_type == VAR_LIST)
17127 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017128 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017129 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017130 if (list_extend(rettv->vval.v_list,
17131 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017132 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017133 }
17134 else
17135 {
17136 p = get_tv_string(&argvars[0]);
17137 rettv->v_type = VAR_STRING;
17138 rettv->vval.v_string = NULL;
17139
17140 slen = (int)STRLEN(p);
17141 len = slen * n;
17142 if (len <= 0)
17143 return;
17144
17145 r = alloc(len + 1);
17146 if (r != NULL)
17147 {
17148 for (i = 0; i < n; i++)
17149 mch_memmove(r + i * slen, p, (size_t)slen);
17150 r[len] = NUL;
17151 }
17152
17153 rettv->vval.v_string = r;
17154 }
17155}
17156
17157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158 * "resolve()" function
17159 */
17160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017161f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162{
17163 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017164#ifdef HAVE_READLINK
17165 char_u *buf = NULL;
17166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017168 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169#ifdef FEAT_SHORTCUT
17170 {
17171 char_u *v = NULL;
17172
17173 v = mch_resolve_shortcut(p);
17174 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017175 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017176 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017177 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 }
17179#else
17180# ifdef HAVE_READLINK
17181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 char_u *cpy;
17183 int len;
17184 char_u *remain = NULL;
17185 char_u *q;
17186 int is_relative_to_current = FALSE;
17187 int has_trailing_pathsep = FALSE;
17188 int limit = 100;
17189
17190 p = vim_strsave(p);
17191
17192 if (p[0] == '.' && (vim_ispathsep(p[1])
17193 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17194 is_relative_to_current = TRUE;
17195
17196 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017197 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017200 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202
17203 q = getnextcomp(p);
17204 if (*q != NUL)
17205 {
17206 /* Separate the first path component in "p", and keep the
17207 * remainder (beginning with the path separator). */
17208 remain = vim_strsave(q - 1);
17209 q[-1] = NUL;
17210 }
17211
Bram Moolenaard9462e32011-04-11 21:35:11 +020017212 buf = alloc(MAXPATHL + 1);
17213 if (buf == NULL)
17214 goto fail;
17215
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216 for (;;)
17217 {
17218 for (;;)
17219 {
17220 len = readlink((char *)p, (char *)buf, MAXPATHL);
17221 if (len <= 0)
17222 break;
17223 buf[len] = NUL;
17224
17225 if (limit-- == 0)
17226 {
17227 vim_free(p);
17228 vim_free(remain);
17229 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017230 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017231 goto fail;
17232 }
17233
17234 /* Ensure that the result will have a trailing path separator
17235 * if the argument has one. */
17236 if (remain == NULL && has_trailing_pathsep)
17237 add_pathsep(buf);
17238
17239 /* Separate the first path component in the link value and
17240 * concatenate the remainders. */
17241 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17242 if (*q != NUL)
17243 {
17244 if (remain == NULL)
17245 remain = vim_strsave(q - 1);
17246 else
17247 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017248 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 if (cpy != NULL)
17250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 vim_free(remain);
17252 remain = cpy;
17253 }
17254 }
17255 q[-1] = NUL;
17256 }
17257
17258 q = gettail(p);
17259 if (q > p && *q == NUL)
17260 {
17261 /* Ignore trailing path separator. */
17262 q[-1] = NUL;
17263 q = gettail(p);
17264 }
17265 if (q > p && !mch_isFullName(buf))
17266 {
17267 /* symlink is relative to directory of argument */
17268 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17269 if (cpy != NULL)
17270 {
17271 STRCPY(cpy, p);
17272 STRCPY(gettail(cpy), buf);
17273 vim_free(p);
17274 p = cpy;
17275 }
17276 }
17277 else
17278 {
17279 vim_free(p);
17280 p = vim_strsave(buf);
17281 }
17282 }
17283
17284 if (remain == NULL)
17285 break;
17286
17287 /* Append the first path component of "remain" to "p". */
17288 q = getnextcomp(remain + 1);
17289 len = q - remain - (*q != NUL);
17290 cpy = vim_strnsave(p, STRLEN(p) + len);
17291 if (cpy != NULL)
17292 {
17293 STRNCAT(cpy, remain, len);
17294 vim_free(p);
17295 p = cpy;
17296 }
17297 /* Shorten "remain". */
17298 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017299 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 else
17301 {
17302 vim_free(remain);
17303 remain = NULL;
17304 }
17305 }
17306
17307 /* If the result is a relative path name, make it explicitly relative to
17308 * the current directory if and only if the argument had this form. */
17309 if (!vim_ispathsep(*p))
17310 {
17311 if (is_relative_to_current
17312 && *p != NUL
17313 && !(p[0] == '.'
17314 && (p[1] == NUL
17315 || vim_ispathsep(p[1])
17316 || (p[1] == '.'
17317 && (p[2] == NUL
17318 || vim_ispathsep(p[2]))))))
17319 {
17320 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017321 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 if (cpy != NULL)
17323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 vim_free(p);
17325 p = cpy;
17326 }
17327 }
17328 else if (!is_relative_to_current)
17329 {
17330 /* Strip leading "./". */
17331 q = p;
17332 while (q[0] == '.' && vim_ispathsep(q[1]))
17333 q += 2;
17334 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017335 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336 }
17337 }
17338
17339 /* Ensure that the result will have no trailing path separator
17340 * if the argument had none. But keep "/" or "//". */
17341 if (!has_trailing_pathsep)
17342 {
17343 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017344 if (after_pathsep(p, q))
17345 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 }
17347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 }
17350# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017351 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352# endif
17353#endif
17354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017355 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356
17357#ifdef HAVE_READLINK
17358fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017359 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017361 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362}
17363
17364/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017365 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 */
17367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017368f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369{
Bram Moolenaar33570922005-01-25 22:26:29 +000017370 list_T *l;
17371 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372
Bram Moolenaar0d660222005-01-07 21:51:51 +000017373 if (argvars[0].v_type != VAR_LIST)
17374 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017375 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017376 && !tv_check_lock(l->lv_lock,
17377 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017378 {
17379 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017380 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017381 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017382 while (li != NULL)
17383 {
17384 ni = li->li_prev;
17385 list_append(l, li);
17386 li = ni;
17387 }
17388 rettv->vval.v_list = l;
17389 rettv->v_type = VAR_LIST;
17390 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017391 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393}
17394
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017395#define SP_NOMOVE 0x01 /* don't move cursor */
17396#define SP_REPEAT 0x02 /* repeat to find outer pair */
17397#define SP_RETCOUNT 0x04 /* return matchcount */
17398#define SP_SETPCMARK 0x08 /* set previous context mark */
17399#define SP_START 0x10 /* accept match at start position */
17400#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17401#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017402#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017403
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017404static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017405
17406/*
17407 * Get flags for a search function.
17408 * Possibly sets "p_ws".
17409 * Returns BACKWARD, FORWARD or zero (for an error).
17410 */
17411 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017412get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017413{
17414 int dir = FORWARD;
17415 char_u *flags;
17416 char_u nbuf[NUMBUFLEN];
17417 int mask;
17418
17419 if (varp->v_type != VAR_UNKNOWN)
17420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017421 flags = get_tv_string_buf_chk(varp, nbuf);
17422 if (flags == NULL)
17423 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017424 while (*flags != NUL)
17425 {
17426 switch (*flags)
17427 {
17428 case 'b': dir = BACKWARD; break;
17429 case 'w': p_ws = TRUE; break;
17430 case 'W': p_ws = FALSE; break;
17431 default: mask = 0;
17432 if (flagsp != NULL)
17433 switch (*flags)
17434 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017435 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017436 case 'e': mask = SP_END; break;
17437 case 'm': mask = SP_RETCOUNT; break;
17438 case 'n': mask = SP_NOMOVE; break;
17439 case 'p': mask = SP_SUBPAT; break;
17440 case 'r': mask = SP_REPEAT; break;
17441 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017442 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017443 }
17444 if (mask == 0)
17445 {
17446 EMSG2(_(e_invarg2), flags);
17447 dir = 0;
17448 }
17449 else
17450 *flagsp |= mask;
17451 }
17452 if (dir == 0)
17453 break;
17454 ++flags;
17455 }
17456 }
17457 return dir;
17458}
17459
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017461 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017463 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017464search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017466 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 char_u *pat;
17468 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017469 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470 int save_p_ws = p_ws;
17471 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017472 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017473 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017474 proftime_T tm;
17475#ifdef FEAT_RELTIME
17476 long time_limit = 0;
17477#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017478 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017479 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017481 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017482 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017483 if (dir == 0)
17484 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017485 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017486 if (flags & SP_START)
17487 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017488 if (flags & SP_END)
17489 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017490 if (flags & SP_COLUMN)
17491 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017492
Bram Moolenaar76929292008-01-06 19:07:36 +000017493 /* Optional arguments: line number to stop searching and timeout. */
17494 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017495 {
17496 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17497 if (lnum_stop < 0)
17498 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017499#ifdef FEAT_RELTIME
17500 if (argvars[3].v_type != VAR_UNKNOWN)
17501 {
17502 time_limit = get_tv_number_chk(&argvars[3], NULL);
17503 if (time_limit < 0)
17504 goto theend;
17505 }
17506#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017507 }
17508
Bram Moolenaar76929292008-01-06 19:07:36 +000017509#ifdef FEAT_RELTIME
17510 /* Set the time limit, if there is one. */
17511 profile_setlimit(time_limit, &tm);
17512#endif
17513
Bram Moolenaar231334e2005-07-25 20:46:57 +000017514 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017515 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017516 * Check to make sure only those flags are set.
17517 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17518 * flags cannot be set. Check for that condition also.
17519 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017520 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017521 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017522 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017524 goto theend;
17525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017527 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017528 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017529 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017530 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017532 if (flags & SP_SUBPAT)
17533 retval = subpatnum;
17534 else
17535 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017536 if (flags & SP_SETPCMARK)
17537 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017539 if (match_pos != NULL)
17540 {
17541 /* Store the match cursor position */
17542 match_pos->lnum = pos.lnum;
17543 match_pos->col = pos.col + 1;
17544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 /* "/$" will put the cursor after the end of the line, may need to
17546 * correct that here */
17547 check_cursor();
17548 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017549
17550 /* If 'n' flag is used: restore cursor position. */
17551 if (flags & SP_NOMOVE)
17552 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017553 else
17554 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017555theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017557
17558 return retval;
17559}
17560
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017561#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017562
17563/*
17564 * round() is not in C90, use ceil() or floor() instead.
17565 */
17566 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017567vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017568{
17569 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17570}
17571
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017572/*
17573 * "round({float})" function
17574 */
17575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017576f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017577{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017578 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017579
17580 rettv->v_type = VAR_FLOAT;
17581 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017582 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017583 else
17584 rettv->vval.v_float = 0.0;
17585}
17586#endif
17587
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017588/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017589 * "screenattr()" function
17590 */
17591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017592f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017593{
17594 int row;
17595 int col;
17596 int c;
17597
17598 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17599 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17600 if (row < 0 || row >= screen_Rows
17601 || col < 0 || col >= screen_Columns)
17602 c = -1;
17603 else
17604 c = ScreenAttrs[LineOffset[row] + col];
17605 rettv->vval.v_number = c;
17606}
17607
17608/*
17609 * "screenchar()" function
17610 */
17611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017612f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017613{
17614 int row;
17615 int col;
17616 int off;
17617 int c;
17618
17619 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17620 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17621 if (row < 0 || row >= screen_Rows
17622 || col < 0 || col >= screen_Columns)
17623 c = -1;
17624 else
17625 {
17626 off = LineOffset[row] + col;
17627#ifdef FEAT_MBYTE
17628 if (enc_utf8 && ScreenLinesUC[off] != 0)
17629 c = ScreenLinesUC[off];
17630 else
17631#endif
17632 c = ScreenLines[off];
17633 }
17634 rettv->vval.v_number = c;
17635}
17636
17637/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017638 * "screencol()" function
17639 *
17640 * First column is 1 to be consistent with virtcol().
17641 */
17642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017643f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017644{
17645 rettv->vval.v_number = screen_screencol() + 1;
17646}
17647
17648/*
17649 * "screenrow()" function
17650 */
17651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017652f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017653{
17654 rettv->vval.v_number = screen_screenrow() + 1;
17655}
17656
17657/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017658 * "search()" function
17659 */
17660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017661f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017662{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017663 int flags = 0;
17664
17665 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666}
17667
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017669 * "searchdecl()" function
17670 */
17671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017672f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017673{
17674 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017675 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017676 int error = FALSE;
17677 char_u *name;
17678
17679 rettv->vval.v_number = 1; /* default: FAIL */
17680
17681 name = get_tv_string_chk(&argvars[0]);
17682 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017683 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017684 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017685 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17686 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17687 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017688 if (!error && name != NULL)
17689 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017690 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017691}
17692
17693/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017694 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017696 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017697searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698{
17699 char_u *spat, *mpat, *epat;
17700 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702 int dir;
17703 int flags = 0;
17704 char_u nbuf1[NUMBUFLEN];
17705 char_u nbuf2[NUMBUFLEN];
17706 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017707 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017708 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017709 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017712 spat = get_tv_string_chk(&argvars[0]);
17713 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17714 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17715 if (spat == NULL || mpat == NULL || epat == NULL)
17716 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 /* Handle the optional fourth argument: flags */
17719 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017720 if (dir == 0)
17721 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017722
17723 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017724 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17725 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017726 if ((flags & (SP_END | SP_SUBPAT)) != 0
17727 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017728 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017729 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017730 goto theend;
17731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017733 /* Using 'r' implies 'W', otherwise it doesn't work. */
17734 if (flags & SP_REPEAT)
17735 p_ws = FALSE;
17736
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017737 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017738 if (argvars[3].v_type == VAR_UNKNOWN
17739 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740 skip = (char_u *)"";
17741 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017742 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017743 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017744 if (argvars[5].v_type != VAR_UNKNOWN)
17745 {
17746 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17747 if (lnum_stop < 0)
17748 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017749#ifdef FEAT_RELTIME
17750 if (argvars[6].v_type != VAR_UNKNOWN)
17751 {
17752 time_limit = get_tv_number_chk(&argvars[6], NULL);
17753 if (time_limit < 0)
17754 goto theend;
17755 }
17756#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017757 }
17758 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017759 if (skip == NULL)
17760 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017762 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017763 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017764
17765theend:
17766 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017767
17768 return retval;
17769}
17770
17771/*
17772 * "searchpair()" function
17773 */
17774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017775f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017776{
17777 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17778}
17779
17780/*
17781 * "searchpairpos()" function
17782 */
17783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017784f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017785{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017786 pos_T match_pos;
17787 int lnum = 0;
17788 int col = 0;
17789
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017790 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017791 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017792
17793 if (searchpair_cmn(argvars, &match_pos) > 0)
17794 {
17795 lnum = match_pos.lnum;
17796 col = match_pos.col;
17797 }
17798
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017799 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17800 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017801}
17802
17803/*
17804 * Search for a start/middle/end thing.
17805 * Used by searchpair(), see its documentation for the details.
17806 * Returns 0 or -1 for no match,
17807 */
17808 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017809do_searchpair(
17810 char_u *spat, /* start pattern */
17811 char_u *mpat, /* middle pattern */
17812 char_u *epat, /* end pattern */
17813 int dir, /* BACKWARD or FORWARD */
17814 char_u *skip, /* skip expression */
17815 int flags, /* SP_SETPCMARK and other SP_ values */
17816 pos_T *match_pos,
17817 linenr_T lnum_stop, /* stop at this line if not zero */
17818 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017819{
17820 char_u *save_cpo;
17821 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17822 long retval = 0;
17823 pos_T pos;
17824 pos_T firstpos;
17825 pos_T foundpos;
17826 pos_T save_cursor;
17827 pos_T save_pos;
17828 int n;
17829 int r;
17830 int nest = 1;
17831 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017832 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017833 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017834
17835 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17836 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017837 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017838
Bram Moolenaar76929292008-01-06 19:07:36 +000017839#ifdef FEAT_RELTIME
17840 /* Set the time limit, if there is one. */
17841 profile_setlimit(time_limit, &tm);
17842#endif
17843
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017844 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17845 * start/middle/end (pat3, for the top pair). */
17846 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17847 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17848 if (pat2 == NULL || pat3 == NULL)
17849 goto theend;
17850 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17851 if (*mpat == NUL)
17852 STRCPY(pat3, pat2);
17853 else
17854 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17855 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017856 if (flags & SP_START)
17857 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017858
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 save_cursor = curwin->w_cursor;
17860 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017861 clearpos(&firstpos);
17862 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 pat = pat3;
17864 for (;;)
17865 {
17866 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017867 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17869 /* didn't find it or found the first match again: FAIL */
17870 break;
17871
17872 if (firstpos.lnum == 0)
17873 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017874 if (equalpos(pos, foundpos))
17875 {
17876 /* Found the same position again. Can happen with a pattern that
17877 * has "\zs" at the end and searching backwards. Advance one
17878 * character and try again. */
17879 if (dir == BACKWARD)
17880 decl(&pos);
17881 else
17882 incl(&pos);
17883 }
17884 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017886 /* clear the start flag to avoid getting stuck here */
17887 options &= ~SEARCH_START;
17888
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 /* If the skip pattern matches, ignore this match. */
17890 if (*skip != NUL)
17891 {
17892 save_pos = curwin->w_cursor;
17893 curwin->w_cursor = pos;
17894 r = eval_to_bool(skip, &err, NULL, FALSE);
17895 curwin->w_cursor = save_pos;
17896 if (err)
17897 {
17898 /* Evaluating {skip} caused an error, break here. */
17899 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017900 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 break;
17902 }
17903 if (r)
17904 continue;
17905 }
17906
17907 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17908 {
17909 /* Found end when searching backwards or start when searching
17910 * forward: nested pair. */
17911 ++nest;
17912 pat = pat2; /* nested, don't search for middle */
17913 }
17914 else
17915 {
17916 /* Found end when searching forward or start when searching
17917 * backward: end of (nested) pair; or found middle in outer pair. */
17918 if (--nest == 1)
17919 pat = pat3; /* outer level, search for middle */
17920 }
17921
17922 if (nest == 0)
17923 {
17924 /* Found the match: return matchcount or line number. */
17925 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017926 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017928 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017929 if (flags & SP_SETPCMARK)
17930 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 curwin->w_cursor = pos;
17932 if (!(flags & SP_REPEAT))
17933 break;
17934 nest = 1; /* search for next unmatched */
17935 }
17936 }
17937
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017938 if (match_pos != NULL)
17939 {
17940 /* Store the match cursor position */
17941 match_pos->lnum = curwin->w_cursor.lnum;
17942 match_pos->col = curwin->w_cursor.col + 1;
17943 }
17944
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017946 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947 curwin->w_cursor = save_cursor;
17948
17949theend:
17950 vim_free(pat2);
17951 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017952 if (p_cpo == empty_option)
17953 p_cpo = save_cpo;
17954 else
17955 /* Darn, evaluating the {skip} expression changed the value. */
17956 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017957
17958 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959}
17960
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017961/*
17962 * "searchpos()" function
17963 */
17964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017965f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017966{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017967 pos_T match_pos;
17968 int lnum = 0;
17969 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017970 int n;
17971 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017972
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017973 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017974 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017975
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017976 n = search_cmn(argvars, &match_pos, &flags);
17977 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017978 {
17979 lnum = match_pos.lnum;
17980 col = match_pos.col;
17981 }
17982
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017983 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17984 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017985 if (flags & SP_SUBPAT)
17986 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017987}
17988
Bram Moolenaar0d660222005-01-07 21:51:51 +000017989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017990f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017992#ifdef FEAT_CLIENTSERVER
17993 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017994 char_u *server = get_tv_string_chk(&argvars[0]);
17995 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996
Bram Moolenaar0d660222005-01-07 21:51:51 +000017997 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017998 if (server == NULL || reply == NULL)
17999 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018000 if (check_restricted() || check_secure())
18001 return;
18002# ifdef FEAT_X11
18003 if (check_connection() == FAIL)
18004 return;
18005# endif
18006
18007 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018009 EMSG(_("E258: Unable to send to client"));
18010 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018012 rettv->vval.v_number = 0;
18013#else
18014 rettv->vval.v_number = -1;
18015#endif
18016}
18017
Bram Moolenaar0d660222005-01-07 21:51:51 +000018018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018019f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018020{
18021 char_u *r = NULL;
18022
18023#ifdef FEAT_CLIENTSERVER
18024# ifdef WIN32
18025 r = serverGetVimNames();
18026# else
18027 make_connection();
18028 if (X_DISPLAY != NULL)
18029 r = serverGetVimNames(X_DISPLAY);
18030# endif
18031#endif
18032 rettv->v_type = VAR_STRING;
18033 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034}
18035
18036/*
18037 * "setbufvar()" function
18038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018040f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041{
18042 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 char_u nbuf[NUMBUFLEN];
18047
18048 if (check_restricted() || check_secure())
18049 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018050 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18051 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018052 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 varp = &argvars[2];
18054
18055 if (buf != NULL && varname != NULL && varp != NULL)
18056 {
18057 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059
18060 if (*varname == '&')
18061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018062 long numval;
18063 char_u *strval;
18064 int error = FALSE;
18065
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018067 numval = get_tv_number_chk(varp, &error);
18068 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018069 if (!error && strval != NULL)
18070 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 }
18072 else
18073 {
18074 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18075 if (bufvarname != NULL)
18076 {
18077 STRCPY(bufvarname, "b:");
18078 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018079 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 vim_free(bufvarname);
18081 }
18082 }
18083
18084 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087}
18088
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018090f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018091{
18092 dict_T *d;
18093 dictitem_T *di;
18094 char_u *csearch;
18095
18096 if (argvars[0].v_type != VAR_DICT)
18097 {
18098 EMSG(_(e_dictreq));
18099 return;
18100 }
18101
18102 if ((d = argvars[0].vval.v_dict) != NULL)
18103 {
18104 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18105 if (csearch != NULL)
18106 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018107#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018108 if (enc_utf8)
18109 {
18110 int pcc[MAX_MCO];
18111 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018112
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018113 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18114 }
18115 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018116#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018117 set_last_csearch(PTR2CHAR(csearch),
18118 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018119 }
18120
18121 di = dict_find(d, (char_u *)"forward", -1);
18122 if (di != NULL)
18123 set_csearch_direction(get_tv_number(&di->di_tv)
18124 ? FORWARD : BACKWARD);
18125
18126 di = dict_find(d, (char_u *)"until", -1);
18127 if (di != NULL)
18128 set_csearch_until(!!get_tv_number(&di->di_tv));
18129 }
18130}
18131
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132/*
18133 * "setcmdpos()" function
18134 */
18135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018136f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018138 int pos = (int)get_tv_number(&argvars[0]) - 1;
18139
18140 if (pos >= 0)
18141 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142}
18143
18144/*
18145 * "setline()" function
18146 */
18147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018148f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149{
18150 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018151 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018152 list_T *l = NULL;
18153 listitem_T *li = NULL;
18154 long added = 0;
18155 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018157 lnum = get_tv_lnum(&argvars[0]);
18158 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018160 l = argvars[1].vval.v_list;
18161 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018163 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018164 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018165
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018166 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018167 for (;;)
18168 {
18169 if (l != NULL)
18170 {
18171 /* list argument, get next string */
18172 if (li == NULL)
18173 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018174 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018175 li = li->li_next;
18176 }
18177
18178 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018179 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018180 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018181
18182 /* When coming here from Insert mode, sync undo, so that this can be
18183 * undone separately from what was previously inserted. */
18184 if (u_sync_once == 2)
18185 {
18186 u_sync_once = 1; /* notify that u_sync() was called */
18187 u_sync(TRUE);
18188 }
18189
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018190 if (lnum <= curbuf->b_ml.ml_line_count)
18191 {
18192 /* existing line, replace it */
18193 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18194 {
18195 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018196 if (lnum == curwin->w_cursor.lnum)
18197 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018198 rettv->vval.v_number = 0; /* OK */
18199 }
18200 }
18201 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18202 {
18203 /* lnum is one past the last line, append the line */
18204 ++added;
18205 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18206 rettv->vval.v_number = 0; /* OK */
18207 }
18208
18209 if (l == NULL) /* only one string argument */
18210 break;
18211 ++lnum;
18212 }
18213
18214 if (added > 0)
18215 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216}
18217
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018218static 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 +000018219
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018221 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018222 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018224set_qf_ll_list(
18225 win_T *wp UNUSED,
18226 typval_T *list_arg UNUSED,
18227 typval_T *action_arg UNUSED,
18228 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018229{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018230#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018231 char_u *act;
18232 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018233#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018234
Bram Moolenaar2641f772005-03-25 21:58:17 +000018235 rettv->vval.v_number = -1;
18236
18237#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018238 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018239 EMSG(_(e_listreq));
18240 else
18241 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018242 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018243
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018244 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018245 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018246 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018247 if (act == NULL)
18248 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018249 if (*act == 'a' || *act == 'r')
18250 action = *act;
18251 }
18252
Bram Moolenaar81484f42012-12-05 15:16:47 +010018253 if (l != NULL && set_errorlist(wp, l, action,
18254 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018255 rettv->vval.v_number = 0;
18256 }
18257#endif
18258}
18259
18260/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018261 * "setloclist()" function
18262 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018264f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018265{
18266 win_T *win;
18267
18268 rettv->vval.v_number = -1;
18269
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018270 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018271 if (win != NULL)
18272 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18273}
18274
18275/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018276 * "setmatches()" function
18277 */
18278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018279f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018280{
18281#ifdef FEAT_SEARCH_EXTRA
18282 list_T *l;
18283 listitem_T *li;
18284 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018285 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018286
18287 rettv->vval.v_number = -1;
18288 if (argvars[0].v_type != VAR_LIST)
18289 {
18290 EMSG(_(e_listreq));
18291 return;
18292 }
18293 if ((l = argvars[0].vval.v_list) != NULL)
18294 {
18295
18296 /* To some extent make sure that we are dealing with a list from
18297 * "getmatches()". */
18298 li = l->lv_first;
18299 while (li != NULL)
18300 {
18301 if (li->li_tv.v_type != VAR_DICT
18302 || (d = li->li_tv.vval.v_dict) == NULL)
18303 {
18304 EMSG(_(e_invarg));
18305 return;
18306 }
18307 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018308 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18309 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018310 && dict_find(d, (char_u *)"priority", -1) != NULL
18311 && dict_find(d, (char_u *)"id", -1) != NULL))
18312 {
18313 EMSG(_(e_invarg));
18314 return;
18315 }
18316 li = li->li_next;
18317 }
18318
18319 clear_matches(curwin);
18320 li = l->lv_first;
18321 while (li != NULL)
18322 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018323 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018324 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018325 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018326 char_u *group;
18327 int priority;
18328 int id;
18329 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018330
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018331 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018332 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18333 {
18334 if (s == NULL)
18335 {
18336 s = list_alloc();
18337 if (s == NULL)
18338 return;
18339 }
18340
18341 /* match from matchaddpos() */
18342 for (i = 1; i < 9; i++)
18343 {
18344 sprintf((char *)buf, (char *)"pos%d", i);
18345 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18346 {
18347 if (di->di_tv.v_type != VAR_LIST)
18348 return;
18349
18350 list_append_tv(s, &di->di_tv);
18351 s->lv_refcount++;
18352 }
18353 else
18354 break;
18355 }
18356 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018357
18358 group = get_dict_string(d, (char_u *)"group", FALSE);
18359 priority = (int)get_dict_number(d, (char_u *)"priority");
18360 id = (int)get_dict_number(d, (char_u *)"id");
18361 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18362 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18363 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018364 if (i == 0)
18365 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018366 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018367 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018368 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018369 }
18370 else
18371 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018372 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018373 list_unref(s);
18374 s = NULL;
18375 }
18376
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018377 li = li->li_next;
18378 }
18379 rettv->vval.v_number = 0;
18380 }
18381#endif
18382}
18383
18384/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018385 * "setpos()" function
18386 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018388f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018389{
18390 pos_T pos;
18391 int fnum;
18392 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018393 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018394
Bram Moolenaar08250432008-02-13 11:42:46 +000018395 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018396 name = get_tv_string_chk(argvars);
18397 if (name != NULL)
18398 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018399 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018400 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018401 if (--pos.col < 0)
18402 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018403 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018404 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018405 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018406 if (fnum == curbuf->b_fnum)
18407 {
18408 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018409 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018410 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018411 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018412 curwin->w_set_curswant = FALSE;
18413 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018414 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018415 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018416 }
18417 else
18418 EMSG(_(e_invarg));
18419 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018420 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18421 {
18422 /* set mark */
18423 if (setmark_pos(name[1], &pos, fnum) == OK)
18424 rettv->vval.v_number = 0;
18425 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018426 else
18427 EMSG(_(e_invarg));
18428 }
18429 }
18430}
18431
18432/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018433 * "setqflist()" function
18434 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018436f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018437{
18438 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18439}
18440
18441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 * "setreg()" function
18443 */
18444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018445f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446{
18447 int regname;
18448 char_u *strregname;
18449 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018450 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 int append;
18452 char_u yank_type;
18453 long block_len;
18454
18455 block_len = -1;
18456 yank_type = MAUTO;
18457 append = FALSE;
18458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018459 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018460 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018462 if (strregname == NULL)
18463 return; /* type error; errmsg already given */
18464 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 if (regname == 0 || regname == '@')
18466 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018468 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018470 stropt = get_tv_string_chk(&argvars[2]);
18471 if (stropt == NULL)
18472 return; /* type error */
18473 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 switch (*stropt)
18475 {
18476 case 'a': case 'A': /* append */
18477 append = TRUE;
18478 break;
18479 case 'v': case 'c': /* character-wise selection */
18480 yank_type = MCHAR;
18481 break;
18482 case 'V': case 'l': /* line-wise selection */
18483 yank_type = MLINE;
18484 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 case 'b': case Ctrl_V: /* block-wise selection */
18486 yank_type = MBLOCK;
18487 if (VIM_ISDIGIT(stropt[1]))
18488 {
18489 ++stropt;
18490 block_len = getdigits(&stropt) - 1;
18491 --stropt;
18492 }
18493 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 }
18495 }
18496
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018497 if (argvars[1].v_type == VAR_LIST)
18498 {
18499 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018500 char_u **allocval;
18501 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018502 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018503 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018504 int len = argvars[1].vval.v_list->lv_len;
18505 listitem_T *li;
18506
Bram Moolenaar7d647822014-04-05 21:28:56 +020018507 /* First half: use for pointers to result lines; second half: use for
18508 * pointers to allocated copies. */
18509 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018510 if (lstval == NULL)
18511 return;
18512 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018513 allocval = lstval + len + 2;
18514 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018515
18516 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18517 li = li->li_next)
18518 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018519 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018520 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018521 goto free_lstval;
18522 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018523 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018524 /* Need to make a copy, next get_tv_string_buf_chk() will
18525 * overwrite the string. */
18526 strval = vim_strsave(buf);
18527 if (strval == NULL)
18528 goto free_lstval;
18529 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018530 }
18531 *curval++ = strval;
18532 }
18533 *curval++ = NULL;
18534
18535 write_reg_contents_lst(regname, lstval, -1,
18536 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018537free_lstval:
18538 while (curallocval > allocval)
18539 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018540 vim_free(lstval);
18541 }
18542 else
18543 {
18544 strval = get_tv_string_chk(&argvars[1]);
18545 if (strval == NULL)
18546 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018547 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018550 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551}
18552
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018553/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018554 * "settabvar()" function
18555 */
18556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018557f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018558{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018559#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018560 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018561 tabpage_T *tp;
18562#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018563 char_u *varname, *tabvarname;
18564 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018565
18566 rettv->vval.v_number = 0;
18567
18568 if (check_restricted() || check_secure())
18569 return;
18570
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018571#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018572 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018573#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018574 varname = get_tv_string_chk(&argvars[1]);
18575 varp = &argvars[2];
18576
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018577 if (varname != NULL && varp != NULL
18578#ifdef FEAT_WINDOWS
18579 && tp != NULL
18580#endif
18581 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018582 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018583#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018584 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018585 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018586#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018587
18588 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18589 if (tabvarname != NULL)
18590 {
18591 STRCPY(tabvarname, "t:");
18592 STRCPY(tabvarname + 2, varname);
18593 set_var(tabvarname, varp, TRUE);
18594 vim_free(tabvarname);
18595 }
18596
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018597#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018598 /* Restore current tabpage */
18599 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018600 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018601#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018602 }
18603}
18604
18605/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018606 * "settabwinvar()" function
18607 */
18608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018609f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018610{
18611 setwinvar(argvars, rettv, 1);
18612}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613
18614/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018615 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018618f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018620 setwinvar(argvars, rettv, 0);
18621}
18622
18623/*
18624 * "setwinvar()" and "settabwinvar()" functions
18625 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018626
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018628setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018629{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 win_T *win;
18631#ifdef FEAT_WINDOWS
18632 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018633 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018634 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635#endif
18636 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018637 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018639 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640
18641 if (check_restricted() || check_secure())
18642 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018643
18644#ifdef FEAT_WINDOWS
18645 if (off == 1)
18646 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18647 else
18648 tp = curtab;
18649#endif
18650 win = find_win_by_nr(&argvars[off], tp);
18651 varname = get_tv_string_chk(&argvars[off + 1]);
18652 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653
18654 if (win != NULL && varname != NULL && varp != NULL)
18655 {
18656#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018657 need_switch_win = !(tp == curtab && win == curwin);
18658 if (!need_switch_win
18659 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018662 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018664 long numval;
18665 char_u *strval;
18666 int error = FALSE;
18667
18668 ++varname;
18669 numval = get_tv_number_chk(varp, &error);
18670 strval = get_tv_string_buf_chk(varp, nbuf);
18671 if (!error && strval != NULL)
18672 set_option_value(varname, numval, strval, OPT_LOCAL);
18673 }
18674 else
18675 {
18676 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18677 if (winvarname != NULL)
18678 {
18679 STRCPY(winvarname, "w:");
18680 STRCPY(winvarname + 2, varname);
18681 set_var(winvarname, varp, TRUE);
18682 vim_free(winvarname);
18683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 }
18685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018687 if (need_switch_win)
18688 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689#endif
18690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691}
18692
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018693#ifdef FEAT_CRYPT
18694/*
18695 * "sha256({string})" function
18696 */
18697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018698f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018699{
18700 char_u *p;
18701
18702 p = get_tv_string(&argvars[0]);
18703 rettv->vval.v_string = vim_strsave(
18704 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18705 rettv->v_type = VAR_STRING;
18706}
18707#endif /* FEAT_CRYPT */
18708
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018710 * "shellescape({string})" function
18711 */
18712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018713f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018714{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018715 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018716 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018717 rettv->v_type = VAR_STRING;
18718}
18719
18720/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018721 * shiftwidth() function
18722 */
18723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018724f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018725{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018726 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018727}
18728
18729/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018730 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 */
18732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018733f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018735 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736
Bram Moolenaar0d660222005-01-07 21:51:51 +000018737 p = get_tv_string(&argvars[0]);
18738 rettv->vval.v_string = vim_strsave(p);
18739 simplify_filename(rettv->vval.v_string); /* simplify in place */
18740 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741}
18742
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018743#ifdef FEAT_FLOAT
18744/*
18745 * "sin()" function
18746 */
18747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018748f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018749{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018750 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018751
18752 rettv->v_type = VAR_FLOAT;
18753 if (get_float_arg(argvars, &f) == OK)
18754 rettv->vval.v_float = sin(f);
18755 else
18756 rettv->vval.v_float = 0.0;
18757}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018758
18759/*
18760 * "sinh()" function
18761 */
18762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018763f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018764{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018765 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018766
18767 rettv->v_type = VAR_FLOAT;
18768 if (get_float_arg(argvars, &f) == OK)
18769 rettv->vval.v_float = sinh(f);
18770 else
18771 rettv->vval.v_float = 0.0;
18772}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018773#endif
18774
Bram Moolenaar0d660222005-01-07 21:51:51 +000018775static int
18776#ifdef __BORLANDC__
18777 _RTLENTRYF
18778#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018779 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018780static int
18781#ifdef __BORLANDC__
18782 _RTLENTRYF
18783#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018784 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018785
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018786/* struct used in the array that's given to qsort() */
18787typedef struct
18788{
18789 listitem_T *item;
18790 int idx;
18791} sortItem_T;
18792
Bram Moolenaar0d660222005-01-07 21:51:51 +000018793static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018794static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018795static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018796#ifdef FEAT_FLOAT
18797static int item_compare_float;
18798#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018799static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018800static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018801static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018802static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018803static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018804#define ITEM_COMPARE_FAIL 999
18805
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018807 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018809 static int
18810#ifdef __BORLANDC__
18811_RTLENTRYF
18812#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018813item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018815 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018816 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018817 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018818 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018819 int res;
18820 char_u numbuf1[NUMBUFLEN];
18821 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018823 si1 = (sortItem_T *)s1;
18824 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018825 tv1 = &si1->item->li_tv;
18826 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018827
18828 if (item_compare_numbers)
18829 {
18830 long v1 = get_tv_number(tv1);
18831 long v2 = get_tv_number(tv2);
18832
18833 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18834 }
18835
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018836#ifdef FEAT_FLOAT
18837 if (item_compare_float)
18838 {
18839 float_T v1 = get_tv_float(tv1);
18840 float_T v2 = get_tv_float(tv2);
18841
18842 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18843 }
18844#endif
18845
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018846 /* tv2string() puts quotes around a string and allocates memory. Don't do
18847 * that for string variables. Use a single quote when comparing with a
18848 * non-string to do what the docs promise. */
18849 if (tv1->v_type == VAR_STRING)
18850 {
18851 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18852 p1 = (char_u *)"'";
18853 else
18854 p1 = tv1->vval.v_string;
18855 }
18856 else
18857 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18858 if (tv2->v_type == VAR_STRING)
18859 {
18860 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18861 p2 = (char_u *)"'";
18862 else
18863 p2 = tv2->vval.v_string;
18864 }
18865 else
18866 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018867 if (p1 == NULL)
18868 p1 = (char_u *)"";
18869 if (p2 == NULL)
18870 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018871 if (!item_compare_numeric)
18872 {
18873 if (item_compare_ic)
18874 res = STRICMP(p1, p2);
18875 else
18876 res = STRCMP(p1, p2);
18877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018879 {
18880 double n1, n2;
18881 n1 = strtod((char *)p1, (char **)&p1);
18882 n2 = strtod((char *)p2, (char **)&p2);
18883 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18884 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018885
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018886 /* When the result would be zero, compare the item indexes. Makes the
18887 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018888 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018889 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018890
Bram Moolenaar0d660222005-01-07 21:51:51 +000018891 vim_free(tofree1);
18892 vim_free(tofree2);
18893 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894}
18895
18896 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018897#ifdef __BORLANDC__
18898_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018900item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018901{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018902 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018903 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018904 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018905 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018906 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018908 /* shortcut after failure in previous call; compare all items equal */
18909 if (item_compare_func_err)
18910 return 0;
18911
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018912 si1 = (sortItem_T *)s1;
18913 si2 = (sortItem_T *)s2;
18914
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018915 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018916 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018917 copy_tv(&si1->item->li_tv, &argv[0]);
18918 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018919
18920 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018921 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018922 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18923 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018924 clear_tv(&argv[0]);
18925 clear_tv(&argv[1]);
18926
18927 if (res == FAIL)
18928 res = ITEM_COMPARE_FAIL;
18929 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018930 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18931 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018932 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018933 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018934
18935 /* When the result would be zero, compare the pointers themselves. Makes
18936 * the sort stable. */
18937 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018938 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018939
Bram Moolenaar0d660222005-01-07 21:51:51 +000018940 return res;
18941}
18942
18943/*
18944 * "sort({list})" function
18945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018947do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948{
Bram Moolenaar33570922005-01-25 22:26:29 +000018949 list_T *l;
18950 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018951 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018952 long len;
18953 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954
Bram Moolenaar0d660222005-01-07 21:51:51 +000018955 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018956 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957 else
18958 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018959 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018960 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018961 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18962 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963 return;
18964 rettv->vval.v_list = l;
18965 rettv->v_type = VAR_LIST;
18966 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967
Bram Moolenaar0d660222005-01-07 21:51:51 +000018968 len = list_len(l);
18969 if (len <= 1)
18970 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018973 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018974 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018975#ifdef FEAT_FLOAT
18976 item_compare_float = FALSE;
18977#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018978 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018979 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018980 if (argvars[1].v_type != VAR_UNKNOWN)
18981 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018982 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018983 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018984 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018985 else
18986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018987 int error = FALSE;
18988
18989 i = get_tv_number_chk(&argvars[1], &error);
18990 if (error)
18991 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018992 if (i == 1)
18993 item_compare_ic = TRUE;
18994 else
18995 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018996 if (item_compare_func != NULL)
18997 {
18998 if (STRCMP(item_compare_func, "n") == 0)
18999 {
19000 item_compare_func = NULL;
19001 item_compare_numeric = TRUE;
19002 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019003 else if (STRCMP(item_compare_func, "N") == 0)
19004 {
19005 item_compare_func = NULL;
19006 item_compare_numbers = TRUE;
19007 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019008#ifdef FEAT_FLOAT
19009 else if (STRCMP(item_compare_func, "f") == 0)
19010 {
19011 item_compare_func = NULL;
19012 item_compare_float = TRUE;
19013 }
19014#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020019015 else if (STRCMP(item_compare_func, "i") == 0)
19016 {
19017 item_compare_func = NULL;
19018 item_compare_ic = TRUE;
19019 }
19020 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019022
19023 if (argvars[2].v_type != VAR_UNKNOWN)
19024 {
19025 /* optional third argument: {dict} */
19026 if (argvars[2].v_type != VAR_DICT)
19027 {
19028 EMSG(_(e_dictreq));
19029 return;
19030 }
19031 item_compare_selfdict = argvars[2].vval.v_dict;
19032 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034
Bram Moolenaar0d660222005-01-07 21:51:51 +000019035 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019036 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019037 if (ptrs == NULL)
19038 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039
Bram Moolenaar327aa022014-03-25 18:24:23 +010019040 i = 0;
19041 if (sort)
19042 {
19043 /* sort(): ptrs will be the list to sort */
19044 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019045 {
19046 ptrs[i].item = li;
19047 ptrs[i].idx = i;
19048 ++i;
19049 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019050
19051 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019052 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019053 /* test the compare function */
19054 if (item_compare_func != NULL
19055 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019056 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019057 EMSG(_("E702: Sort compare function failed"));
19058 else
19059 {
19060 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019061 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010019062 item_compare_func == NULL ? item_compare : item_compare2);
19063
19064 if (!item_compare_func_err)
19065 {
19066 /* Clear the List and append the items in sorted order. */
19067 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19068 l->lv_len = 0;
19069 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019070 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019071 }
19072 }
19073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019075 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019076 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019077
19078 /* f_uniq(): ptrs will be a stack of items to remove */
19079 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019080 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019081 item_compare_func_ptr = item_compare_func
19082 ? item_compare2 : item_compare;
19083
19084 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19085 li = li->li_next)
19086 {
19087 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19088 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019089 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019090 if (item_compare_func_err)
19091 {
19092 EMSG(_("E882: Uniq compare function failed"));
19093 break;
19094 }
19095 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019097 if (!item_compare_func_err)
19098 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019099 while (--i >= 0)
19100 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019101 li = ptrs[i].item->li_next;
19102 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019103 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019104 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019105 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019106 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019107 list_fix_watch(l, li);
19108 listitem_free(li);
19109 l->lv_len--;
19110 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019111 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019112 }
19113
19114 vim_free(ptrs);
19115 }
19116}
19117
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019118/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019119 * "sort({list})" function
19120 */
19121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019122f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019123{
19124 do_sort_uniq(argvars, rettv, TRUE);
19125}
19126
19127/*
19128 * "uniq({list})" function
19129 */
19130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019131f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019132{
19133 do_sort_uniq(argvars, rettv, FALSE);
19134}
19135
19136/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019137 * "soundfold({word})" function
19138 */
19139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019140f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019141{
19142 char_u *s;
19143
19144 rettv->v_type = VAR_STRING;
19145 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019146#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019147 rettv->vval.v_string = eval_soundfold(s);
19148#else
19149 rettv->vval.v_string = vim_strsave(s);
19150#endif
19151}
19152
19153/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019154 * "spellbadword()" function
19155 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019157f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019158{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019159 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019160 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019161 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019162
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019163 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019164 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019165
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019166#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019167 if (argvars[0].v_type == VAR_UNKNOWN)
19168 {
19169 /* Find the start and length of the badly spelled word. */
19170 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19171 if (len != 0)
19172 word = ml_get_cursor();
19173 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019174 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019175 {
19176 char_u *str = get_tv_string_chk(&argvars[0]);
19177 int capcol = -1;
19178
19179 if (str != NULL)
19180 {
19181 /* Check the argument for spelling. */
19182 while (*str != NUL)
19183 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019184 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019185 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019186 {
19187 word = str;
19188 break;
19189 }
19190 str += len;
19191 }
19192 }
19193 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019194#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019195
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019196 list_append_string(rettv->vval.v_list, word, len);
19197 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019198 attr == HLF_SPB ? "bad" :
19199 attr == HLF_SPR ? "rare" :
19200 attr == HLF_SPL ? "local" :
19201 attr == HLF_SPC ? "caps" :
19202 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019203}
19204
19205/*
19206 * "spellsuggest()" function
19207 */
19208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019209f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019210{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019211#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019212 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019213 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019214 int maxcount;
19215 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019216 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019217 listitem_T *li;
19218 int need_capital = FALSE;
19219#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019220
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019221 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019222 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019223
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019224#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019225 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019226 {
19227 str = get_tv_string(&argvars[0]);
19228 if (argvars[1].v_type != VAR_UNKNOWN)
19229 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019230 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019231 if (maxcount <= 0)
19232 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019233 if (argvars[2].v_type != VAR_UNKNOWN)
19234 {
19235 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19236 if (typeerr)
19237 return;
19238 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019239 }
19240 else
19241 maxcount = 25;
19242
Bram Moolenaar4770d092006-01-12 23:22:24 +000019243 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019244
19245 for (i = 0; i < ga.ga_len; ++i)
19246 {
19247 str = ((char_u **)ga.ga_data)[i];
19248
19249 li = listitem_alloc();
19250 if (li == NULL)
19251 vim_free(str);
19252 else
19253 {
19254 li->li_tv.v_type = VAR_STRING;
19255 li->li_tv.v_lock = 0;
19256 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019257 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019258 }
19259 }
19260 ga_clear(&ga);
19261 }
19262#endif
19263}
19264
Bram Moolenaar0d660222005-01-07 21:51:51 +000019265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019266f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019267{
19268 char_u *str;
19269 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019270 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019271 regmatch_T regmatch;
19272 char_u patbuf[NUMBUFLEN];
19273 char_u *save_cpo;
19274 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019275 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019276 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019277 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019278
19279 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19280 save_cpo = p_cpo;
19281 p_cpo = (char_u *)"";
19282
19283 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019284 if (argvars[1].v_type != VAR_UNKNOWN)
19285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019286 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19287 if (pat == NULL)
19288 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019289 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019290 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019291 }
19292 if (pat == NULL || *pat == NUL)
19293 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019294
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019295 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019297 if (typeerr)
19298 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299
Bram Moolenaar0d660222005-01-07 21:51:51 +000019300 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19301 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019303 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019304 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019305 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019306 if (*str == NUL)
19307 match = FALSE; /* empty item at the end */
19308 else
19309 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019310 if (match)
19311 end = regmatch.startp[0];
19312 else
19313 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019314 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19315 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019316 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019317 if (list_append_string(rettv->vval.v_list, str,
19318 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019319 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019320 }
19321 if (!match)
19322 break;
19323 /* Advance to just after the match. */
19324 if (regmatch.endp[0] > str)
19325 col = 0;
19326 else
19327 {
19328 /* Don't get stuck at the same match. */
19329#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019330 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019331#else
19332 col = 1;
19333#endif
19334 }
19335 str = regmatch.endp[0];
19336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337
Bram Moolenaar473de612013-06-08 18:19:48 +020019338 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340
Bram Moolenaar0d660222005-01-07 21:51:51 +000019341 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342}
19343
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019344#ifdef FEAT_FLOAT
19345/*
19346 * "sqrt()" function
19347 */
19348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019349f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019350{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019351 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019352
19353 rettv->v_type = VAR_FLOAT;
19354 if (get_float_arg(argvars, &f) == OK)
19355 rettv->vval.v_float = sqrt(f);
19356 else
19357 rettv->vval.v_float = 0.0;
19358}
19359
19360/*
19361 * "str2float()" function
19362 */
19363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019364f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019365{
19366 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19367
19368 if (*p == '+')
19369 p = skipwhite(p + 1);
19370 (void)string2float(p, &rettv->vval.v_float);
19371 rettv->v_type = VAR_FLOAT;
19372}
19373#endif
19374
Bram Moolenaar2c932302006-03-18 21:42:09 +000019375/*
19376 * "str2nr()" function
19377 */
19378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019379f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019380{
19381 int base = 10;
19382 char_u *p;
19383 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019384 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019385
19386 if (argvars[1].v_type != VAR_UNKNOWN)
19387 {
19388 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019389 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019390 {
19391 EMSG(_(e_invarg));
19392 return;
19393 }
19394 }
19395
19396 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019397 if (*p == '+')
19398 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019399 switch (base)
19400 {
19401 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19402 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19403 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19404 default: what = 0;
19405 }
19406 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019407 rettv->vval.v_number = n;
19408}
19409
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410#ifdef HAVE_STRFTIME
19411/*
19412 * "strftime({format}[, {time}])" function
19413 */
19414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019415f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416{
19417 char_u result_buf[256];
19418 struct tm *curtime;
19419 time_t seconds;
19420 char_u *p;
19421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019422 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019424 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019425 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 seconds = time(NULL);
19427 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019428 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 curtime = localtime(&seconds);
19430 /* MSVC returns NULL for an invalid value of seconds. */
19431 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019432 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 else
19434 {
19435# ifdef FEAT_MBYTE
19436 vimconv_T conv;
19437 char_u *enc;
19438
19439 conv.vc_type = CONV_NONE;
19440 enc = enc_locale();
19441 convert_setup(&conv, p_enc, enc);
19442 if (conv.vc_type != CONV_NONE)
19443 p = string_convert(&conv, p, NULL);
19444# endif
19445 if (p != NULL)
19446 (void)strftime((char *)result_buf, sizeof(result_buf),
19447 (char *)p, curtime);
19448 else
19449 result_buf[0] = NUL;
19450
19451# ifdef FEAT_MBYTE
19452 if (conv.vc_type != CONV_NONE)
19453 vim_free(p);
19454 convert_setup(&conv, enc, p_enc);
19455 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019456 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 else
19458# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019459 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460
19461# ifdef FEAT_MBYTE
19462 /* Release conversion descriptors */
19463 convert_setup(&conv, NULL, NULL);
19464 vim_free(enc);
19465# endif
19466 }
19467}
19468#endif
19469
19470/*
19471 * "stridx()" function
19472 */
19473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019474f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475{
19476 char_u buf[NUMBUFLEN];
19477 char_u *needle;
19478 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019479 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019481 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019483 needle = get_tv_string_chk(&argvars[1]);
19484 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019485 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019486 if (needle == NULL || haystack == NULL)
19487 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488
Bram Moolenaar33570922005-01-25 22:26:29 +000019489 if (argvars[2].v_type != VAR_UNKNOWN)
19490 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019491 int error = FALSE;
19492
19493 start_idx = get_tv_number_chk(&argvars[2], &error);
19494 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019495 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019496 if (start_idx >= 0)
19497 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019498 }
19499
19500 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19501 if (pos != NULL)
19502 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503}
19504
19505/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019506 * "string()" function
19507 */
19508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019509f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019510{
19511 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019512 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019514 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019515 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019516 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019517 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019518 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519}
19520
19521/*
19522 * "strlen()" function
19523 */
19524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019525f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019527 rettv->vval.v_number = (varnumber_T)(STRLEN(
19528 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529}
19530
19531/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019532 * "strchars()" function
19533 */
19534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019535f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019536{
19537 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019538 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019539#ifdef FEAT_MBYTE
19540 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019541 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019542#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019543
19544 if (argvars[1].v_type != VAR_UNKNOWN)
19545 skipcc = get_tv_number_chk(&argvars[1], NULL);
19546 if (skipcc < 0 || skipcc > 1)
19547 EMSG(_(e_invarg));
19548 else
19549 {
19550#ifdef FEAT_MBYTE
19551 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19552 while (*s != NUL)
19553 {
19554 func_mb_ptr2char_adv(&s);
19555 ++len;
19556 }
19557 rettv->vval.v_number = len;
19558#else
19559 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19560#endif
19561 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019562}
19563
19564/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019565 * "strdisplaywidth()" function
19566 */
19567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019568f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019569{
19570 char_u *s = get_tv_string(&argvars[0]);
19571 int col = 0;
19572
19573 if (argvars[1].v_type != VAR_UNKNOWN)
19574 col = get_tv_number(&argvars[1]);
19575
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019576 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019577}
19578
19579/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019580 * "strwidth()" function
19581 */
19582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019583f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019584{
19585 char_u *s = get_tv_string(&argvars[0]);
19586
19587 rettv->vval.v_number = (varnumber_T)(
19588#ifdef FEAT_MBYTE
19589 mb_string2cells(s, -1)
19590#else
19591 STRLEN(s)
19592#endif
19593 );
19594}
19595
19596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 * "strpart()" function
19598 */
19599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019600f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601{
19602 char_u *p;
19603 int n;
19604 int len;
19605 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019606 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019608 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 slen = (int)STRLEN(p);
19610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019611 n = get_tv_number_chk(&argvars[1], &error);
19612 if (error)
19613 len = 0;
19614 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019615 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 else
19617 len = slen - n; /* default len: all bytes that are available. */
19618
19619 /*
19620 * Only return the overlap between the specified part and the actual
19621 * string.
19622 */
19623 if (n < 0)
19624 {
19625 len += n;
19626 n = 0;
19627 }
19628 else if (n > slen)
19629 n = slen;
19630 if (len < 0)
19631 len = 0;
19632 else if (n + len > slen)
19633 len = slen - n;
19634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019635 rettv->v_type = VAR_STRING;
19636 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637}
19638
19639/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019640 * "strridx()" function
19641 */
19642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019643f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019644{
19645 char_u buf[NUMBUFLEN];
19646 char_u *needle;
19647 char_u *haystack;
19648 char_u *rest;
19649 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019650 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019652 needle = get_tv_string_chk(&argvars[1]);
19653 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019654
19655 rettv->vval.v_number = -1;
19656 if (needle == NULL || haystack == NULL)
19657 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019658
19659 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019660 if (argvars[2].v_type != VAR_UNKNOWN)
19661 {
19662 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019663 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019664 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019665 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019666 }
19667 else
19668 end_idx = haystack_len;
19669
Bram Moolenaar0d660222005-01-07 21:51:51 +000019670 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019671 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019672 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019673 lastmatch = haystack + end_idx;
19674 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019675 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019676 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019677 for (rest = haystack; *rest != '\0'; ++rest)
19678 {
19679 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019680 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019681 break;
19682 lastmatch = rest;
19683 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019684 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019685
19686 if (lastmatch == NULL)
19687 rettv->vval.v_number = -1;
19688 else
19689 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19690}
19691
19692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 * "strtrans()" function
19694 */
19695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019696f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019698 rettv->v_type = VAR_STRING;
19699 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700}
19701
19702/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019703 * "submatch()" function
19704 */
19705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019706f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019707{
Bram Moolenaar41571762014-04-02 19:00:58 +020019708 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019709 int no;
19710 int retList = 0;
19711
19712 no = (int)get_tv_number_chk(&argvars[0], &error);
19713 if (error)
19714 return;
19715 error = FALSE;
19716 if (argvars[1].v_type != VAR_UNKNOWN)
19717 retList = get_tv_number_chk(&argvars[1], &error);
19718 if (error)
19719 return;
19720
19721 if (retList == 0)
19722 {
19723 rettv->v_type = VAR_STRING;
19724 rettv->vval.v_string = reg_submatch(no);
19725 }
19726 else
19727 {
19728 rettv->v_type = VAR_LIST;
19729 rettv->vval.v_list = reg_submatch_list(no);
19730 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019731}
19732
19733/*
19734 * "substitute()" function
19735 */
19736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019737f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019738{
19739 char_u patbuf[NUMBUFLEN];
19740 char_u subbuf[NUMBUFLEN];
19741 char_u flagsbuf[NUMBUFLEN];
19742
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019743 char_u *str = get_tv_string_chk(&argvars[0]);
19744 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19745 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19746 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19747
Bram Moolenaar0d660222005-01-07 21:51:51 +000019748 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019749 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19750 rettv->vval.v_string = NULL;
19751 else
19752 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019753}
19754
19755/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019756 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019759f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760{
19761 int id = 0;
19762#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019763 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 long col;
19765 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019766 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019768 lnum = get_tv_lnum(argvars); /* -1 on type error */
19769 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19770 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019772 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019773 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019774 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775#endif
19776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019777 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778}
19779
19780/*
19781 * "synIDattr(id, what [, mode])" function
19782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019784f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785{
19786 char_u *p = NULL;
19787#ifdef FEAT_SYN_HL
19788 int id;
19789 char_u *what;
19790 char_u *mode;
19791 char_u modebuf[NUMBUFLEN];
19792 int modec;
19793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019794 id = get_tv_number(&argvars[0]);
19795 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019796 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019798 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019800 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 modec = 0; /* replace invalid with current */
19802 }
19803 else
19804 {
19805#ifdef FEAT_GUI
19806 if (gui.in_use)
19807 modec = 'g';
19808 else
19809#endif
19810 if (t_colors > 1)
19811 modec = 'c';
19812 else
19813 modec = 't';
19814 }
19815
19816
19817 switch (TOLOWER_ASC(what[0]))
19818 {
19819 case 'b':
19820 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19821 p = highlight_color(id, what, modec);
19822 else /* bold */
19823 p = highlight_has_attr(id, HL_BOLD, modec);
19824 break;
19825
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019826 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827 p = highlight_color(id, what, modec);
19828 break;
19829
19830 case 'i':
19831 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19832 p = highlight_has_attr(id, HL_INVERSE, modec);
19833 else /* italic */
19834 p = highlight_has_attr(id, HL_ITALIC, modec);
19835 break;
19836
19837 case 'n': /* name */
19838 p = get_highlight_name(NULL, id - 1);
19839 break;
19840
19841 case 'r': /* reverse */
19842 p = highlight_has_attr(id, HL_INVERSE, modec);
19843 break;
19844
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019845 case 's':
19846 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19847 p = highlight_color(id, what, modec);
19848 else /* standout */
19849 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850 break;
19851
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019852 case 'u':
19853 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19854 /* underline */
19855 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19856 else
19857 /* undercurl */
19858 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 break;
19860 }
19861
19862 if (p != NULL)
19863 p = vim_strsave(p);
19864#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019865 rettv->v_type = VAR_STRING;
19866 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867}
19868
19869/*
19870 * "synIDtrans(id)" function
19871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019873f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874{
19875 int id;
19876
19877#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019878 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879
19880 if (id > 0)
19881 id = syn_get_final_id(id);
19882 else
19883#endif
19884 id = 0;
19885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019886 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887}
19888
19889/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019890 * "synconcealed(lnum, col)" function
19891 */
19892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019893f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019894{
19895#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19896 long lnum;
19897 long col;
19898 int syntax_flags = 0;
19899 int cchar;
19900 int matchid = 0;
19901 char_u str[NUMBUFLEN];
19902#endif
19903
19904 rettv->v_type = VAR_LIST;
19905 rettv->vval.v_list = NULL;
19906
19907#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19908 lnum = get_tv_lnum(argvars); /* -1 on type error */
19909 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19910
19911 vim_memset(str, NUL, sizeof(str));
19912
19913 if (rettv_list_alloc(rettv) != FAIL)
19914 {
19915 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19916 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19917 && curwin->w_p_cole > 0)
19918 {
19919 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19920 syntax_flags = get_syntax_info(&matchid);
19921
19922 /* get the conceal character */
19923 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19924 {
19925 cchar = syn_get_sub_char();
19926 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19927 cchar = lcs_conceal;
19928 if (cchar != NUL)
19929 {
19930# ifdef FEAT_MBYTE
19931 if (has_mbyte)
19932 (*mb_char2bytes)(cchar, str);
19933 else
19934# endif
19935 str[0] = cchar;
19936 }
19937 }
19938 }
19939
19940 list_append_number(rettv->vval.v_list,
19941 (syntax_flags & HL_CONCEAL) != 0);
19942 /* -1 to auto-determine strlen */
19943 list_append_string(rettv->vval.v_list, str, -1);
19944 list_append_number(rettv->vval.v_list, matchid);
19945 }
19946#endif
19947}
19948
19949/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019950 * "synstack(lnum, col)" function
19951 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019953f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019954{
19955#ifdef FEAT_SYN_HL
19956 long lnum;
19957 long col;
19958 int i;
19959 int id;
19960#endif
19961
19962 rettv->v_type = VAR_LIST;
19963 rettv->vval.v_list = NULL;
19964
19965#ifdef FEAT_SYN_HL
19966 lnum = get_tv_lnum(argvars); /* -1 on type error */
19967 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19968
19969 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019970 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019971 && rettv_list_alloc(rettv) != FAIL)
19972 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019973 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019974 for (i = 0; ; ++i)
19975 {
19976 id = syn_get_stack_item(i);
19977 if (id < 0)
19978 break;
19979 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19980 break;
19981 }
19982 }
19983#endif
19984}
19985
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019987get_cmd_output_as_rettv(
19988 typval_T *argvars,
19989 typval_T *rettv,
19990 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019992 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019994 char_u *infile = NULL;
19995 char_u buf[NUMBUFLEN];
19996 int err = FALSE;
19997 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019998 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019999 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020001 rettv->v_type = VAR_STRING;
20002 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020003 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020004 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020005
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020006 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020007 {
20008 /*
20009 * Write the string to a temp file, to be used for input of the shell
20010 * command.
20011 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020012 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020013 {
20014 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020015 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020016 }
20017
20018 fd = mch_fopen((char *)infile, WRITEBIN);
20019 if (fd == NULL)
20020 {
20021 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020022 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020023 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020024 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020025 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020026 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20027 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020028 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020029 else
20030 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020031 size_t len;
20032
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020033 p = get_tv_string_buf_chk(&argvars[1], buf);
20034 if (p == NULL)
20035 {
20036 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020037 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020038 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020039 len = STRLEN(p);
20040 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020041 err = TRUE;
20042 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020043 if (fclose(fd) != 0)
20044 err = TRUE;
20045 if (err)
20046 {
20047 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020048 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020049 }
20050 }
20051
Bram Moolenaar52a72462014-08-29 15:53:52 +020020052 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20053 * echoes typeahead, that messes up the display. */
20054 if (!msg_silent)
20055 flags += SHELL_COOKED;
20056
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020057 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020059 int len;
20060 listitem_T *li;
20061 char_u *s = NULL;
20062 char_u *start;
20063 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020064 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065
Bram Moolenaar52a72462014-08-29 15:53:52 +020020066 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020067 if (res == NULL)
20068 goto errret;
20069
20070 list = list_alloc();
20071 if (list == NULL)
20072 goto errret;
20073
20074 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020075 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020076 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020077 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020078 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020079 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020080
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020081 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020082 if (s == NULL)
20083 goto errret;
20084
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020085 for (p = s; start < end; ++p, ++start)
20086 *p = *start == NUL ? NL : *start;
20087 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020088
20089 li = listitem_alloc();
20090 if (li == NULL)
20091 {
20092 vim_free(s);
20093 goto errret;
20094 }
20095 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020096 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020097 li->li_tv.vval.v_string = s;
20098 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020100
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020101 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020102 rettv->v_type = VAR_LIST;
20103 rettv->vval.v_list = list;
20104 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020106 else
20107 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020108 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020109#ifdef USE_CR
20110 /* translate <CR> into <NL> */
20111 if (res != NULL)
20112 {
20113 char_u *s;
20114
20115 for (s = res; *s; ++s)
20116 {
20117 if (*s == CAR)
20118 *s = NL;
20119 }
20120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121#else
20122# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020123 /* translate <CR><NL> into <NL> */
20124 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020126 char_u *s, *d;
20127
20128 d = res;
20129 for (s = res; *s; ++s)
20130 {
20131 if (s[0] == CAR && s[1] == NL)
20132 ++s;
20133 *d++ = *s;
20134 }
20135 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137# endif
20138#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020139 rettv->vval.v_string = res;
20140 res = NULL;
20141 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020142
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020143errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020144 if (infile != NULL)
20145 {
20146 mch_remove(infile);
20147 vim_free(infile);
20148 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020149 if (res != NULL)
20150 vim_free(res);
20151 if (list != NULL)
20152 list_free(list, TRUE);
20153}
20154
20155/*
20156 * "system()" function
20157 */
20158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020159f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020160{
20161 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20162}
20163
20164/*
20165 * "systemlist()" function
20166 */
20167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020168f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020169{
20170 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171}
20172
20173/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020174 * "tabpagebuflist()" function
20175 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020177f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020178{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020179#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020180 tabpage_T *tp;
20181 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020182
20183 if (argvars[0].v_type == VAR_UNKNOWN)
20184 wp = firstwin;
20185 else
20186 {
20187 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20188 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020189 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020190 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020191 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020192 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020193 for (; wp != NULL; wp = wp->w_next)
20194 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020195 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020196 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020197 }
20198#endif
20199}
20200
20201
20202/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020203 * "tabpagenr()" function
20204 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020206f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020207{
20208 int nr = 1;
20209#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020210 char_u *arg;
20211
20212 if (argvars[0].v_type != VAR_UNKNOWN)
20213 {
20214 arg = get_tv_string_chk(&argvars[0]);
20215 nr = 0;
20216 if (arg != NULL)
20217 {
20218 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020219 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020220 else
20221 EMSG2(_(e_invexpr2), arg);
20222 }
20223 }
20224 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020225 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020226#endif
20227 rettv->vval.v_number = nr;
20228}
20229
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020230
20231#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020232static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020233
20234/*
20235 * Common code for tabpagewinnr() and winnr().
20236 */
20237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020238get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020239{
20240 win_T *twin;
20241 int nr = 1;
20242 win_T *wp;
20243 char_u *arg;
20244
20245 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20246 if (argvar->v_type != VAR_UNKNOWN)
20247 {
20248 arg = get_tv_string_chk(argvar);
20249 if (arg == NULL)
20250 nr = 0; /* type error; errmsg already given */
20251 else if (STRCMP(arg, "$") == 0)
20252 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20253 else if (STRCMP(arg, "#") == 0)
20254 {
20255 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20256 if (twin == NULL)
20257 nr = 0;
20258 }
20259 else
20260 {
20261 EMSG2(_(e_invexpr2), arg);
20262 nr = 0;
20263 }
20264 }
20265
20266 if (nr > 0)
20267 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20268 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020269 {
20270 if (wp == NULL)
20271 {
20272 /* didn't find it in this tabpage */
20273 nr = 0;
20274 break;
20275 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020276 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020277 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020278 return nr;
20279}
20280#endif
20281
20282/*
20283 * "tabpagewinnr()" function
20284 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020286f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020287{
20288 int nr = 1;
20289#ifdef FEAT_WINDOWS
20290 tabpage_T *tp;
20291
20292 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20293 if (tp == NULL)
20294 nr = 0;
20295 else
20296 nr = get_winnr(tp, &argvars[1]);
20297#endif
20298 rettv->vval.v_number = nr;
20299}
20300
20301
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020302/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020303 * "tagfiles()" function
20304 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020306f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020307{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020308 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020309 tagname_T tn;
20310 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020311
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020312 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020313 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020314 fname = alloc(MAXPATHL);
20315 if (fname == NULL)
20316 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020317
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020318 for (first = TRUE; ; first = FALSE)
20319 if (get_tagfname(&tn, first, fname) == FAIL
20320 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020321 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020322 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020323 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020324}
20325
20326/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020327 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020328 */
20329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020330f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020331{
20332 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020333
20334 tag_pattern = get_tv_string(&argvars[0]);
20335
20336 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020337 if (*tag_pattern == NUL)
20338 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020339
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020340 if (rettv_list_alloc(rettv) == OK)
20341 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020342}
20343
20344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 * "tempname()" function
20346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020348f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349{
20350 static int x = 'A';
20351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020352 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020353 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354
20355 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20356 * names. Skip 'I' and 'O', they are used for shell redirection. */
20357 do
20358 {
20359 if (x == 'Z')
20360 x = '0';
20361 else if (x == '9')
20362 x = 'A';
20363 else
20364 {
20365#ifdef EBCDIC
20366 if (x == 'I')
20367 x = 'J';
20368 else if (x == 'R')
20369 x = 'S';
20370 else
20371#endif
20372 ++x;
20373 }
20374 } while (x == 'I' || x == 'O');
20375}
20376
20377/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020378 * "test(list)" function: Just checking the walls...
20379 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020381f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020382{
20383 /* Used for unit testing. Change the code below to your liking. */
20384#if 0
20385 listitem_T *li;
20386 list_T *l;
20387 char_u *bad, *good;
20388
20389 if (argvars[0].v_type != VAR_LIST)
20390 return;
20391 l = argvars[0].vval.v_list;
20392 if (l == NULL)
20393 return;
20394 li = l->lv_first;
20395 if (li == NULL)
20396 return;
20397 bad = get_tv_string(&li->li_tv);
20398 li = li->li_next;
20399 if (li == NULL)
20400 return;
20401 good = get_tv_string(&li->li_tv);
20402 rettv->vval.v_number = test_edit_score(bad, good);
20403#endif
20404}
20405
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020406#ifdef FEAT_FLOAT
20407/*
20408 * "tan()" function
20409 */
20410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020411f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020412{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020413 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020414
20415 rettv->v_type = VAR_FLOAT;
20416 if (get_float_arg(argvars, &f) == OK)
20417 rettv->vval.v_float = tan(f);
20418 else
20419 rettv->vval.v_float = 0.0;
20420}
20421
20422/*
20423 * "tanh()" function
20424 */
20425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020426f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020427{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020428 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020429
20430 rettv->v_type = VAR_FLOAT;
20431 if (get_float_arg(argvars, &f) == OK)
20432 rettv->vval.v_float = tanh(f);
20433 else
20434 rettv->vval.v_float = 0.0;
20435}
20436#endif
20437
Bram Moolenaard52d9742005-08-21 22:20:28 +000020438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439 * "tolower(string)" function
20440 */
20441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020442f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443{
20444 char_u *p;
20445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020446 p = vim_strsave(get_tv_string(&argvars[0]));
20447 rettv->v_type = VAR_STRING;
20448 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449
20450 if (p != NULL)
20451 while (*p != NUL)
20452 {
20453#ifdef FEAT_MBYTE
20454 int l;
20455
20456 if (enc_utf8)
20457 {
20458 int c, lc;
20459
20460 c = utf_ptr2char(p);
20461 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020462 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 /* TODO: reallocate string when byte count changes. */
20464 if (utf_char2len(lc) == l)
20465 utf_char2bytes(lc, p);
20466 p += l;
20467 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020468 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 p += l; /* skip multi-byte character */
20470 else
20471#endif
20472 {
20473 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20474 ++p;
20475 }
20476 }
20477}
20478
20479/*
20480 * "toupper(string)" function
20481 */
20482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020483f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020485 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020486 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487}
20488
20489/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020490 * "tr(string, fromstr, tostr)" function
20491 */
20492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020493f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020494{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020495 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020496 char_u *fromstr;
20497 char_u *tostr;
20498 char_u *p;
20499#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020500 int inlen;
20501 int fromlen;
20502 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020503 int idx;
20504 char_u *cpstr;
20505 int cplen;
20506 int first = TRUE;
20507#endif
20508 char_u buf[NUMBUFLEN];
20509 char_u buf2[NUMBUFLEN];
20510 garray_T ga;
20511
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020512 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020513 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20514 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020515
20516 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020517 rettv->v_type = VAR_STRING;
20518 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020519 if (fromstr == NULL || tostr == NULL)
20520 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020521 ga_init2(&ga, (int)sizeof(char), 80);
20522
20523#ifdef FEAT_MBYTE
20524 if (!has_mbyte)
20525#endif
20526 /* not multi-byte: fromstr and tostr must be the same length */
20527 if (STRLEN(fromstr) != STRLEN(tostr))
20528 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020529#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020530error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020531#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020532 EMSG2(_(e_invarg2), fromstr);
20533 ga_clear(&ga);
20534 return;
20535 }
20536
20537 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020538 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020539 {
20540#ifdef FEAT_MBYTE
20541 if (has_mbyte)
20542 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020543 inlen = (*mb_ptr2len)(in_str);
20544 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020545 cplen = inlen;
20546 idx = 0;
20547 for (p = fromstr; *p != NUL; p += fromlen)
20548 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020549 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020550 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020551 {
20552 for (p = tostr; *p != NUL; p += tolen)
20553 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020554 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020555 if (idx-- == 0)
20556 {
20557 cplen = tolen;
20558 cpstr = p;
20559 break;
20560 }
20561 }
20562 if (*p == NUL) /* tostr is shorter than fromstr */
20563 goto error;
20564 break;
20565 }
20566 ++idx;
20567 }
20568
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020569 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020570 {
20571 /* Check that fromstr and tostr have the same number of
20572 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020573 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020574 first = FALSE;
20575 for (p = tostr; *p != NUL; p += tolen)
20576 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020577 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020578 --idx;
20579 }
20580 if (idx != 0)
20581 goto error;
20582 }
20583
Bram Moolenaarcde88542015-08-11 19:14:00 +020020584 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020585 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020586 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020587
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020588 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020589 }
20590 else
20591#endif
20592 {
20593 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020594 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020595 if (p != NULL)
20596 ga_append(&ga, tostr[p - fromstr]);
20597 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020598 ga_append(&ga, *in_str);
20599 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020600 }
20601 }
20602
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020603 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020604 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020605 ga_append(&ga, NUL);
20606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020607 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020608}
20609
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020610#ifdef FEAT_FLOAT
20611/*
20612 * "trunc({float})" function
20613 */
20614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020615f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020616{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020617 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020618
20619 rettv->v_type = VAR_FLOAT;
20620 if (get_float_arg(argvars, &f) == OK)
20621 /* trunc() is not in C90, use floor() or ceil() instead. */
20622 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20623 else
20624 rettv->vval.v_float = 0.0;
20625}
20626#endif
20627
Bram Moolenaar8299df92004-07-10 09:47:34 +000020628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 * "type(expr)" function
20630 */
20631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020632f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020634 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020635
20636 switch (argvars[0].v_type)
20637 {
20638 case VAR_NUMBER: n = 0; break;
20639 case VAR_STRING: n = 1; break;
20640 case VAR_FUNC: n = 2; break;
20641 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020642 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020643 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020644 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020645 if (argvars[0].vval.v_number == VVAL_FALSE
20646 || argvars[0].vval.v_number == VVAL_TRUE)
20647 n = 6;
20648 else
20649 n = 7;
20650 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020651 case VAR_JOB: n = 8; break;
20652 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020653 case VAR_UNKNOWN:
20654 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20655 n = -1;
20656 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020657 }
20658 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020659}
20660
20661/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020662 * "undofile(name)" function
20663 */
20664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020665f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020666{
20667 rettv->v_type = VAR_STRING;
20668#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020669 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020670 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020671
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020672 if (*fname == NUL)
20673 {
20674 /* If there is no file name there will be no undo file. */
20675 rettv->vval.v_string = NULL;
20676 }
20677 else
20678 {
20679 char_u *ffname = FullName_save(fname, FALSE);
20680
20681 if (ffname != NULL)
20682 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20683 vim_free(ffname);
20684 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020685 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020686#else
20687 rettv->vval.v_string = NULL;
20688#endif
20689}
20690
20691/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020692 * "undotree()" function
20693 */
20694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020695f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020696{
20697 if (rettv_dict_alloc(rettv) == OK)
20698 {
20699 dict_T *dict = rettv->vval.v_dict;
20700 list_T *list;
20701
Bram Moolenaar730cde92010-06-27 05:18:54 +020020702 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020703 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020704 dict_add_nr_str(dict, "save_last",
20705 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020706 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20707 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020708 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020709
20710 list = list_alloc();
20711 if (list != NULL)
20712 {
20713 u_eval_tree(curbuf->b_u_oldhead, list);
20714 dict_add_list(dict, "entries", list);
20715 }
20716 }
20717}
20718
20719/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020720 * "values(dict)" function
20721 */
20722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020723f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020724{
20725 dict_list(argvars, rettv, 1);
20726}
20727
20728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 * "virtcol(string)" function
20730 */
20731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020732f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733{
20734 colnr_T vcol = 0;
20735 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020736 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020738 fp = var2fpos(&argvars[0], FALSE, &fnum);
20739 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20740 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 {
20742 getvvcol(curwin, fp, NULL, NULL, &vcol);
20743 ++vcol;
20744 }
20745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020746 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747}
20748
20749/*
20750 * "visualmode()" function
20751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020753f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755 char_u str[2];
20756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020757 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758 str[0] = curbuf->b_visual_mode_eval;
20759 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020760 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761
20762 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020763 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020764 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765}
20766
20767/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020768 * "wildmenumode()" function
20769 */
20770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020771f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020772{
20773#ifdef FEAT_WILDMENU
20774 if (wild_menu_showing)
20775 rettv->vval.v_number = 1;
20776#endif
20777}
20778
20779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 * "winbufnr(nr)" function
20781 */
20782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020783f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784{
20785 win_T *wp;
20786
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020787 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020791 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792}
20793
20794/*
20795 * "wincol()" function
20796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020798f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799{
20800 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020801 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020802}
20803
20804/*
20805 * "winheight(nr)" function
20806 */
20807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020808f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809{
20810 win_T *wp;
20811
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020812 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020814 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020816 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817}
20818
20819/*
20820 * "winline()" function
20821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020823f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824{
20825 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020826 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827}
20828
20829/*
20830 * "winnr()" function
20831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020833f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834{
20835 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020836
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020838 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020840 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841}
20842
20843/*
20844 * "winrestcmd()" function
20845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020847f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848{
20849#ifdef FEAT_WINDOWS
20850 win_T *wp;
20851 int winnr = 1;
20852 garray_T ga;
20853 char_u buf[50];
20854
20855 ga_init2(&ga, (int)sizeof(char), 70);
20856 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20857 {
20858 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20859 ga_concat(&ga, buf);
20860# ifdef FEAT_VERTSPLIT
20861 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20862 ga_concat(&ga, buf);
20863# endif
20864 ++winnr;
20865 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020866 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020868 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020870 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020872 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873}
20874
20875/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020876 * "winrestview()" function
20877 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020879f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020880{
20881 dict_T *dict;
20882
20883 if (argvars[0].v_type != VAR_DICT
20884 || (dict = argvars[0].vval.v_dict) == NULL)
20885 EMSG(_(e_invarg));
20886 else
20887 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020888 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20889 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20890 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20891 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020892#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020893 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20894 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020895#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020896 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20897 {
20898 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20899 curwin->w_set_curswant = FALSE;
20900 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020901
Bram Moolenaar82c25852014-05-28 16:47:16 +020020902 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20903 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020904#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020905 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20906 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020907#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020908 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20909 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20910 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20911 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020912
20913 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020914 win_new_height(curwin, curwin->w_height);
20915# ifdef FEAT_VERTSPLIT
20916 win_new_width(curwin, W_WIDTH(curwin));
20917# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020918 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020919
Bram Moolenaarb851a962014-10-31 15:45:52 +010020920 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020921 curwin->w_topline = 1;
20922 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20923 curwin->w_topline = curbuf->b_ml.ml_line_count;
20924#ifdef FEAT_DIFF
20925 check_topfill(curwin, TRUE);
20926#endif
20927 }
20928}
20929
20930/*
20931 * "winsaveview()" function
20932 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020934f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020935{
20936 dict_T *dict;
20937
Bram Moolenaara800b422010-06-27 01:15:55 +020020938 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020939 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020940 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020941
20942 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20943 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20944#ifdef FEAT_VIRTUALEDIT
20945 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20946#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020947 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020948 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20949
20950 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20951#ifdef FEAT_DIFF
20952 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20953#endif
20954 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20955 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20956}
20957
20958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 * "winwidth(nr)" function
20960 */
20961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020962f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963{
20964 win_T *wp;
20965
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020966 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020968 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 else
20970#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020971 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020973 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974#endif
20975}
20976
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020978 * "wordcount()" function
20979 */
20980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020981f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020982{
20983 if (rettv_dict_alloc(rettv) == FAIL)
20984 return;
20985 cursor_pos_info(rettv->vval.v_dict);
20986}
20987
20988/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020989 * Write list of strings to file
20990 */
20991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020992write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020993{
20994 listitem_T *li;
20995 int c;
20996 int ret = OK;
20997 char_u *s;
20998
20999 for (li = list->lv_first; li != NULL; li = li->li_next)
21000 {
21001 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21002 {
21003 if (*s == '\n')
21004 c = putc(NUL, fd);
21005 else
21006 c = putc(*s, fd);
21007 if (c == EOF)
21008 {
21009 ret = FAIL;
21010 break;
21011 }
21012 }
21013 if (!binary || li->li_next != NULL)
21014 if (putc('\n', fd) == EOF)
21015 {
21016 ret = FAIL;
21017 break;
21018 }
21019 if (ret == FAIL)
21020 {
21021 EMSG(_(e_write));
21022 break;
21023 }
21024 }
21025 return ret;
21026}
21027
21028/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021029 * "writefile()" function
21030 */
21031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021032f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021033{
21034 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021035 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021036 char_u *fname;
21037 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021038 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021039
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021040 if (check_restricted() || check_secure())
21041 return;
21042
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021043 if (argvars[0].v_type != VAR_LIST)
21044 {
21045 EMSG2(_(e_listarg), "writefile()");
21046 return;
21047 }
21048 if (argvars[0].vval.v_list == NULL)
21049 return;
21050
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021051 if (argvars[2].v_type != VAR_UNKNOWN)
21052 {
21053 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21054 binary = TRUE;
21055 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21056 append = TRUE;
21057 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021058
21059 /* Always open the file in binary mode, library functions have a mind of
21060 * their own about CR-LF conversion. */
21061 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021062 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21063 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021064 {
21065 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21066 ret = -1;
21067 }
21068 else
21069 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021070 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21071 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021072 fclose(fd);
21073 }
21074
21075 rettv->vval.v_number = ret;
21076}
21077
21078/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021079 * "xor(expr, expr)" function
21080 */
21081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021082f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021083{
21084 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21085 ^ get_tv_number_chk(&argvars[1], NULL);
21086}
21087
21088
21089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021091 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 */
21093 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021094var2fpos(
21095 typval_T *varp,
21096 int dollar_lnum, /* TRUE when $ is last line */
21097 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021099 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021101 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102
Bram Moolenaara5525202006-03-02 22:52:09 +000021103 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021104 if (varp->v_type == VAR_LIST)
21105 {
21106 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021107 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021108 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021109 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021110
21111 l = varp->vval.v_list;
21112 if (l == NULL)
21113 return NULL;
21114
21115 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021116 pos.lnum = list_find_nr(l, 0L, &error);
21117 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021118 return NULL; /* invalid line number */
21119
21120 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021121 pos.col = list_find_nr(l, 1L, &error);
21122 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021123 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021124 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021125
21126 /* We accept "$" for the column number: last column. */
21127 li = list_find(l, 1L);
21128 if (li != NULL && li->li_tv.v_type == VAR_STRING
21129 && li->li_tv.vval.v_string != NULL
21130 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21131 pos.col = len + 1;
21132
Bram Moolenaara5525202006-03-02 22:52:09 +000021133 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021134 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021135 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021136 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021137
Bram Moolenaara5525202006-03-02 22:52:09 +000021138#ifdef FEAT_VIRTUALEDIT
21139 /* Get the virtual offset. Defaults to zero. */
21140 pos.coladd = list_find_nr(l, 2L, &error);
21141 if (error)
21142 pos.coladd = 0;
21143#endif
21144
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021145 return &pos;
21146 }
21147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021148 name = get_tv_string_chk(varp);
21149 if (name == NULL)
21150 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021151 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021153 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21154 {
21155 if (VIsual_active)
21156 return &VIsual;
21157 return &curwin->w_cursor;
21158 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021159 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021161 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21163 return NULL;
21164 return pp;
21165 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021166
21167#ifdef FEAT_VIRTUALEDIT
21168 pos.coladd = 0;
21169#endif
21170
Bram Moolenaar477933c2007-07-17 14:32:23 +000021171 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021172 {
21173 pos.col = 0;
21174 if (name[1] == '0') /* "w0": first visible line */
21175 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021176 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021177 pos.lnum = curwin->w_topline;
21178 return &pos;
21179 }
21180 else if (name[1] == '$') /* "w$": last visible line */
21181 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021182 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021183 pos.lnum = curwin->w_botline - 1;
21184 return &pos;
21185 }
21186 }
21187 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021189 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 {
21191 pos.lnum = curbuf->b_ml.ml_line_count;
21192 pos.col = 0;
21193 }
21194 else
21195 {
21196 pos.lnum = curwin->w_cursor.lnum;
21197 pos.col = (colnr_T)STRLEN(ml_get_curline());
21198 }
21199 return &pos;
21200 }
21201 return NULL;
21202}
21203
21204/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021205 * Convert list in "arg" into a position and optional file number.
21206 * When "fnump" is NULL there is no file number, only 3 items.
21207 * Note that the column is passed on as-is, the caller may want to decrement
21208 * it to use 1 for the first column.
21209 * Return FAIL when conversion is not possible, doesn't check the position for
21210 * validity.
21211 */
21212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021213list2fpos(
21214 typval_T *arg,
21215 pos_T *posp,
21216 int *fnump,
21217 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021218{
21219 list_T *l = arg->vval.v_list;
21220 long i = 0;
21221 long n;
21222
Bram Moolenaar493c1782014-05-28 14:34:46 +020021223 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21224 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021225 if (arg->v_type != VAR_LIST
21226 || l == NULL
21227 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021228 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021229 return FAIL;
21230
21231 if (fnump != NULL)
21232 {
21233 n = list_find_nr(l, i++, NULL); /* fnum */
21234 if (n < 0)
21235 return FAIL;
21236 if (n == 0)
21237 n = curbuf->b_fnum; /* current buffer */
21238 *fnump = n;
21239 }
21240
21241 n = list_find_nr(l, i++, NULL); /* lnum */
21242 if (n < 0)
21243 return FAIL;
21244 posp->lnum = n;
21245
21246 n = list_find_nr(l, i++, NULL); /* col */
21247 if (n < 0)
21248 return FAIL;
21249 posp->col = n;
21250
21251#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021252 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021253 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021254 posp->coladd = 0;
21255 else
21256 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021257#endif
21258
Bram Moolenaar493c1782014-05-28 14:34:46 +020021259 if (curswantp != NULL)
21260 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21261
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021262 return OK;
21263}
21264
21265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 * Get the length of an environment variable name.
21267 * Advance "arg" to the first character after the name.
21268 * Return 0 for error.
21269 */
21270 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021271get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272{
21273 char_u *p;
21274 int len;
21275
21276 for (p = *arg; vim_isIDc(*p); ++p)
21277 ;
21278 if (p == *arg) /* no name found */
21279 return 0;
21280
21281 len = (int)(p - *arg);
21282 *arg = p;
21283 return len;
21284}
21285
21286/*
21287 * Get the length of the name of a function or internal variable.
21288 * "arg" is advanced to the first non-white character after the name.
21289 * Return 0 if something is wrong.
21290 */
21291 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021292get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293{
21294 char_u *p;
21295 int len;
21296
21297 /* Find the end of the name. */
21298 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021299 {
21300 if (*p == ':')
21301 {
21302 /* "s:" is start of "s:var", but "n:" is not and can be used in
21303 * slice "[n:]". Also "xx:" is not a namespace. */
21304 len = (int)(p - *arg);
21305 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21306 || len > 1)
21307 break;
21308 }
21309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 if (p == *arg) /* no name found */
21311 return 0;
21312
21313 len = (int)(p - *arg);
21314 *arg = skipwhite(p);
21315
21316 return len;
21317}
21318
21319/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021320 * Get the length of the name of a variable or function.
21321 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021323 * Return -1 if curly braces expansion failed.
21324 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 * If the name contains 'magic' {}'s, expand them and return the
21326 * expanded name in an allocated string via 'alias' - caller must free.
21327 */
21328 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021329get_name_len(
21330 char_u **arg,
21331 char_u **alias,
21332 int evaluate,
21333 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334{
21335 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 char_u *p;
21337 char_u *expr_start;
21338 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339
21340 *alias = NULL; /* default to no alias */
21341
21342 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21343 && (*arg)[2] == (int)KE_SNR)
21344 {
21345 /* hard coded <SNR>, already translated */
21346 *arg += 3;
21347 return get_id_len(arg) + 3;
21348 }
21349 len = eval_fname_script(*arg);
21350 if (len > 0)
21351 {
21352 /* literal "<SID>", "s:" or "<SNR>" */
21353 *arg += len;
21354 }
21355
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021357 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021359 p = find_name_end(*arg, &expr_start, &expr_end,
21360 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 if (expr_start != NULL)
21362 {
21363 char_u *temp_string;
21364
21365 if (!evaluate)
21366 {
21367 len += (int)(p - *arg);
21368 *arg = skipwhite(p);
21369 return len;
21370 }
21371
21372 /*
21373 * Include any <SID> etc in the expanded string:
21374 * Thus the -len here.
21375 */
21376 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21377 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021378 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 *alias = temp_string;
21380 *arg = skipwhite(p);
21381 return (int)STRLEN(temp_string);
21382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383
21384 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021385 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 EMSG2(_(e_invexpr2), *arg);
21387
21388 return len;
21389}
21390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021391/*
21392 * Find the end of a variable or function name, taking care of magic braces.
21393 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21394 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021395 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021396 * Return a pointer to just after the name. Equal to "arg" if there is no
21397 * valid name.
21398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021400find_name_end(
21401 char_u *arg,
21402 char_u **expr_start,
21403 char_u **expr_end,
21404 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021406 int mb_nest = 0;
21407 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021409 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021411 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021413 *expr_start = NULL;
21414 *expr_end = NULL;
21415 }
21416
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021417 /* Quick check for valid starting character. */
21418 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21419 return arg;
21420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021421 for (p = arg; *p != NUL
21422 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021423 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021424 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021425 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021426 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021427 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021428 if (*p == '\'')
21429 {
21430 /* skip over 'string' to avoid counting [ and ] inside it. */
21431 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21432 ;
21433 if (*p == NUL)
21434 break;
21435 }
21436 else if (*p == '"')
21437 {
21438 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21439 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21440 if (*p == '\\' && p[1] != NUL)
21441 ++p;
21442 if (*p == NUL)
21443 break;
21444 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021445 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21446 {
21447 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021448 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021449 len = (int)(p - arg);
21450 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021451 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021452 break;
21453 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021455 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021457 if (*p == '[')
21458 ++br_nest;
21459 else if (*p == ']')
21460 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021463 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021465 if (*p == '{')
21466 {
21467 mb_nest++;
21468 if (expr_start != NULL && *expr_start == NULL)
21469 *expr_start = p;
21470 }
21471 else if (*p == '}')
21472 {
21473 mb_nest--;
21474 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21475 *expr_end = p;
21476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 }
21479
21480 return p;
21481}
21482
21483/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021484 * Expands out the 'magic' {}'s in a variable/function name.
21485 * Note that this can call itself recursively, to deal with
21486 * constructs like foo{bar}{baz}{bam}
21487 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21488 * "in_start" ^
21489 * "expr_start" ^
21490 * "expr_end" ^
21491 * "in_end" ^
21492 *
21493 * Returns a new allocated string, which the caller must free.
21494 * Returns NULL for failure.
21495 */
21496 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021497make_expanded_name(
21498 char_u *in_start,
21499 char_u *expr_start,
21500 char_u *expr_end,
21501 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021502{
21503 char_u c1;
21504 char_u *retval = NULL;
21505 char_u *temp_result;
21506 char_u *nextcmd = NULL;
21507
21508 if (expr_end == NULL || in_end == NULL)
21509 return NULL;
21510 *expr_start = NUL;
21511 *expr_end = NUL;
21512 c1 = *in_end;
21513 *in_end = NUL;
21514
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021515 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021516 if (temp_result != NULL && nextcmd == NULL)
21517 {
21518 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21519 + (in_end - expr_end) + 1));
21520 if (retval != NULL)
21521 {
21522 STRCPY(retval, in_start);
21523 STRCAT(retval, temp_result);
21524 STRCAT(retval, expr_end + 1);
21525 }
21526 }
21527 vim_free(temp_result);
21528
21529 *in_end = c1; /* put char back for error messages */
21530 *expr_start = '{';
21531 *expr_end = '}';
21532
21533 if (retval != NULL)
21534 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021535 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021536 if (expr_start != NULL)
21537 {
21538 /* Further expansion! */
21539 temp_result = make_expanded_name(retval, expr_start,
21540 expr_end, temp_result);
21541 vim_free(retval);
21542 retval = temp_result;
21543 }
21544 }
21545
21546 return retval;
21547}
21548
21549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021551 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552 */
21553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021554eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021556 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21557}
21558
21559/*
21560 * Return TRUE if character "c" can be used as the first character in a
21561 * variable or function name (excluding '{' and '}').
21562 */
21563 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021564eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021565{
21566 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567}
21568
21569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 * Set number v: variable to "val".
21571 */
21572 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021573set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021575 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576}
21577
21578/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021579 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580 */
21581 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021582get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021584 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585}
21586
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021587/*
21588 * Get string v: variable value. Uses a static buffer, can only be used once.
21589 */
21590 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021591get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021592{
21593 return get_tv_string(&vimvars[idx].vv_tv);
21594}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021595
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021597 * Get List v: variable value. Caller must take care of reference count when
21598 * needed.
21599 */
21600 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021601get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021602{
21603 return vimvars[idx].vv_list;
21604}
21605
21606/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021607 * Set v:char to character "c".
21608 */
21609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021610set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021611{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021612 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021613
21614#ifdef FEAT_MBYTE
21615 if (has_mbyte)
21616 buf[(*mb_char2bytes)(c, buf)] = NUL;
21617 else
21618#endif
21619 {
21620 buf[0] = c;
21621 buf[1] = NUL;
21622 }
21623 set_vim_var_string(VV_CHAR, buf, -1);
21624}
21625
21626/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021627 * Set v:count to "count" and v:count1 to "count1".
21628 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 */
21630 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021631set_vcount(
21632 long count,
21633 long count1,
21634 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021636 if (set_prevcount)
21637 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021638 vimvars[VV_COUNT].vv_nr = count;
21639 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640}
21641
21642/*
21643 * Set string v: variable to a copy of "val".
21644 */
21645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021646set_vim_var_string(
21647 int idx,
21648 char_u *val,
21649 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650{
Bram Moolenaara542c682016-01-31 16:28:04 +010021651 clear_tv(&vimvars[idx].vv_di.di_tv);
21652 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021654 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021656 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021658 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659}
21660
21661/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021662 * Set List v: variable to "val".
21663 */
21664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021665set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021666{
Bram Moolenaara542c682016-01-31 16:28:04 +010021667 clear_tv(&vimvars[idx].vv_di.di_tv);
21668 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021669 vimvars[idx].vv_list = val;
21670 if (val != NULL)
21671 ++val->lv_refcount;
21672}
21673
21674/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021675 * Set Dictionary v: variable to "val".
21676 */
21677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021678set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021679{
21680 int todo;
21681 hashitem_T *hi;
21682
Bram Moolenaara542c682016-01-31 16:28:04 +010021683 clear_tv(&vimvars[idx].vv_di.di_tv);
21684 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021685 vimvars[idx].vv_dict = val;
21686 if (val != NULL)
21687 {
21688 ++val->dv_refcount;
21689
21690 /* Set readonly */
21691 todo = (int)val->dv_hashtab.ht_used;
21692 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21693 {
21694 if (HASHITEM_EMPTY(hi))
21695 continue;
21696 --todo;
21697 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21698 }
21699 }
21700}
21701
21702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 * Set v:register if needed.
21704 */
21705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021706set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707{
21708 char_u regname;
21709
21710 if (c == 0 || c == ' ')
21711 regname = '"';
21712 else
21713 regname = c;
21714 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021715 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 set_vim_var_string(VV_REG, &regname, 1);
21717}
21718
21719/*
21720 * Get or set v:exception. If "oldval" == NULL, return the current value.
21721 * Otherwise, restore the value to "oldval" and return NULL.
21722 * Must always be called in pairs to save and restore v:exception! Does not
21723 * take care of memory allocations.
21724 */
21725 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021726v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727{
21728 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021729 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730
Bram Moolenaare9a41262005-01-15 22:18:47 +000021731 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 return NULL;
21733}
21734
21735/*
21736 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21737 * Otherwise, restore the value to "oldval" and return NULL.
21738 * Must always be called in pairs to save and restore v:throwpoint! Does not
21739 * take care of memory allocations.
21740 */
21741 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021742v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743{
21744 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021745 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746
Bram Moolenaare9a41262005-01-15 22:18:47 +000021747 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 return NULL;
21749}
21750
21751#if defined(FEAT_AUTOCMD) || defined(PROTO)
21752/*
21753 * Set v:cmdarg.
21754 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21755 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21756 * Must always be called in pairs!
21757 */
21758 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021759set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760{
21761 char_u *oldval;
21762 char_u *newval;
21763 unsigned len;
21764
Bram Moolenaare9a41262005-01-15 22:18:47 +000021765 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021766 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021768 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021769 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021770 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 }
21772
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021773 if (eap->force_bin == FORCE_BIN)
21774 len = 6;
21775 else if (eap->force_bin == FORCE_NOBIN)
21776 len = 8;
21777 else
21778 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021779
21780 if (eap->read_edit)
21781 len += 7;
21782
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021783 if (eap->force_ff != 0)
21784 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21785# ifdef FEAT_MBYTE
21786 if (eap->force_enc != 0)
21787 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021788 if (eap->bad_char != 0)
21789 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021790# endif
21791
21792 newval = alloc(len + 1);
21793 if (newval == NULL)
21794 return NULL;
21795
21796 if (eap->force_bin == FORCE_BIN)
21797 sprintf((char *)newval, " ++bin");
21798 else if (eap->force_bin == FORCE_NOBIN)
21799 sprintf((char *)newval, " ++nobin");
21800 else
21801 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021802
21803 if (eap->read_edit)
21804 STRCAT(newval, " ++edit");
21805
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021806 if (eap->force_ff != 0)
21807 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21808 eap->cmd + eap->force_ff);
21809# ifdef FEAT_MBYTE
21810 if (eap->force_enc != 0)
21811 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21812 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021813 if (eap->bad_char == BAD_KEEP)
21814 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21815 else if (eap->bad_char == BAD_DROP)
21816 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21817 else if (eap->bad_char != 0)
21818 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021819# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021820 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021821 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021822}
21823#endif
21824
21825/*
21826 * Get the value of internal variable "name".
21827 * Return OK or FAIL.
21828 */
21829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021830get_var_tv(
21831 char_u *name,
21832 int len, /* length of "name" */
21833 typval_T *rettv, /* NULL when only checking existence */
21834 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21835 int verbose, /* may give error message */
21836 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837{
21838 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 typval_T *tv = NULL;
21840 typval_T atv;
21841 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843
21844 /* truncate the name, so that we can use strcmp() */
21845 cc = name[len];
21846 name[len] = NUL;
21847
21848 /*
21849 * Check for "b:changedtick".
21850 */
21851 if (STRCMP(name, "b:changedtick") == 0)
21852 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021853 atv.v_type = VAR_NUMBER;
21854 atv.vval.v_number = curbuf->b_changedtick;
21855 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 }
21857
21858 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 * Check for user-defined variables.
21860 */
21861 else
21862 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021863 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021865 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021866 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021867 if (dip != NULL)
21868 *dip = v;
21869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 }
21871
Bram Moolenaare9a41262005-01-15 22:18:47 +000021872 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021874 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021875 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 ret = FAIL;
21877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021878 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021879 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880
21881 name[len] = cc;
21882
21883 return ret;
21884}
21885
21886/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021887 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21888 * Also handle function call with Funcref variable: func(expr)
21889 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21890 */
21891 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021892handle_subscript(
21893 char_u **arg,
21894 typval_T *rettv,
21895 int evaluate, /* do more than finding the end */
21896 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021897{
21898 int ret = OK;
21899 dict_T *selfdict = NULL;
21900 char_u *s;
21901 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021902 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021903
21904 while (ret == OK
21905 && (**arg == '['
21906 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021907 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021908 && !vim_iswhite(*(*arg - 1)))
21909 {
21910 if (**arg == '(')
21911 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021912 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021913 if (evaluate)
21914 {
21915 functv = *rettv;
21916 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021917
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021918 /* Invoke the function. Recursive! */
21919 s = functv.vval.v_string;
21920 }
21921 else
21922 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021923 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021924 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21925 &len, evaluate, selfdict);
21926
21927 /* Clear the funcref afterwards, so that deleting it while
21928 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021929 if (evaluate)
21930 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021931
21932 /* Stop the expression evaluation when immediately aborting on
21933 * error, or when an interrupt occurred or an exception was thrown
21934 * but not caught. */
21935 if (aborting())
21936 {
21937 if (ret == OK)
21938 clear_tv(rettv);
21939 ret = FAIL;
21940 }
21941 dict_unref(selfdict);
21942 selfdict = NULL;
21943 }
21944 else /* **arg == '[' || **arg == '.' */
21945 {
21946 dict_unref(selfdict);
21947 if (rettv->v_type == VAR_DICT)
21948 {
21949 selfdict = rettv->vval.v_dict;
21950 if (selfdict != NULL)
21951 ++selfdict->dv_refcount;
21952 }
21953 else
21954 selfdict = NULL;
21955 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21956 {
21957 clear_tv(rettv);
21958 ret = FAIL;
21959 }
21960 }
21961 }
21962 dict_unref(selfdict);
21963 return ret;
21964}
21965
21966/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021967 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021968 * value).
21969 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021970 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021971alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021972{
Bram Moolenaar33570922005-01-25 22:26:29 +000021973 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021974}
21975
21976/*
21977 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 * The string "s" must have been allocated, it is consumed.
21979 * Return NULL for out of memory, the variable otherwise.
21980 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021981 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021982alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983{
Bram Moolenaar33570922005-01-25 22:26:29 +000021984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021986 rettv = alloc_tv();
21987 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021989 rettv->v_type = VAR_STRING;
21990 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 }
21992 else
21993 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021994 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995}
21996
21997/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021998 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022000 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022001free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002{
22003 if (varp != NULL)
22004 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022005 switch (varp->v_type)
22006 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022007 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022008 func_unref(varp->vval.v_string);
22009 /*FALLTHROUGH*/
22010 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022011 vim_free(varp->vval.v_string);
22012 break;
22013 case VAR_LIST:
22014 list_unref(varp->vval.v_list);
22015 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022016 case VAR_DICT:
22017 dict_unref(varp->vval.v_dict);
22018 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022019 case VAR_JOB:
22020#ifdef FEAT_JOB
22021 job_unref(varp->vval.v_job);
22022 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022023#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022024 case VAR_CHANNEL:
22025#ifdef FEAT_CHANNEL
22026 channel_unref(varp->vval.v_channel);
22027 break;
22028#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022029 case VAR_NUMBER:
22030 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022031 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022032 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022033 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 vim_free(varp);
22036 }
22037}
22038
22039/*
22040 * Free the memory for a variable value and set the value to NULL or 0.
22041 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022043clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044{
22045 if (varp != NULL)
22046 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022047 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022049 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022050 func_unref(varp->vval.v_string);
22051 /*FALLTHROUGH*/
22052 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022053 vim_free(varp->vval.v_string);
22054 varp->vval.v_string = NULL;
22055 break;
22056 case VAR_LIST:
22057 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022058 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022059 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022060 case VAR_DICT:
22061 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022062 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022063 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022064 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022065 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022066 varp->vval.v_number = 0;
22067 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022068 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022069#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022070 varp->vval.v_float = 0.0;
22071 break;
22072#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022073 case VAR_JOB:
22074#ifdef FEAT_JOB
22075 job_unref(varp->vval.v_job);
22076 varp->vval.v_job = NULL;
22077#endif
22078 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022079 case VAR_CHANNEL:
22080#ifdef FEAT_CHANNEL
22081 channel_unref(varp->vval.v_channel);
22082 varp->vval.v_channel = NULL;
22083#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022084 case VAR_UNKNOWN:
22085 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022087 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 }
22089}
22090
22091/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022092 * Set the value of a variable to NULL without freeing items.
22093 */
22094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022095init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022096{
22097 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022098 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022099}
22100
22101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 * Get the number value of a variable.
22103 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022104 * For incompatible types, return 0.
22105 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22106 * caller of incompatible types: it sets *denote to TRUE if "denote"
22107 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 */
22109 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022110get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022112 int error = FALSE;
22113
22114 return get_tv_number_chk(varp, &error); /* return 0L on error */
22115}
22116
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022117 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022118get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022119{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022120 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022121
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022122 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022124 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022125 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022126 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022127#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022128 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022129 break;
22130#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022131 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022132 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022133 break;
22134 case VAR_STRING:
22135 if (varp->vval.v_string != NULL)
22136 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022137 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022138 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022139 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022140 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022141 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022142 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022143 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022144 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022145 case VAR_SPECIAL:
22146 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22147 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022148 case VAR_JOB:
22149#ifdef FEAT_JOB
22150 EMSG(_("E910: Using a Job as a Number"));
22151 break;
22152#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022153 case VAR_CHANNEL:
22154#ifdef FEAT_CHANNEL
22155 EMSG(_("E913: Using a Channel as a Number"));
22156 break;
22157#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022158 case VAR_UNKNOWN:
22159 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022160 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022162 if (denote == NULL) /* useful for values that must be unsigned */
22163 n = -1;
22164 else
22165 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022166 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167}
22168
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022169#ifdef FEAT_FLOAT
22170 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022171get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022172{
22173 switch (varp->v_type)
22174 {
22175 case VAR_NUMBER:
22176 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022177 case VAR_FLOAT:
22178 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022179 case VAR_FUNC:
22180 EMSG(_("E891: Using a Funcref as a Float"));
22181 break;
22182 case VAR_STRING:
22183 EMSG(_("E892: Using a String as a Float"));
22184 break;
22185 case VAR_LIST:
22186 EMSG(_("E893: Using a List as a Float"));
22187 break;
22188 case VAR_DICT:
22189 EMSG(_("E894: Using a Dictionary as a Float"));
22190 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022191 case VAR_SPECIAL:
22192 EMSG(_("E907: Using a special value as a Float"));
22193 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022194 case VAR_JOB:
22195# ifdef FEAT_JOB
22196 EMSG(_("E911: Using a Job as a Float"));
22197 break;
22198# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022199 case VAR_CHANNEL:
22200# ifdef FEAT_CHANNEL
22201 EMSG(_("E914: Using a Channel as a Float"));
22202 break;
22203# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022204 case VAR_UNKNOWN:
22205 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022206 break;
22207 }
22208 return 0;
22209}
22210#endif
22211
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022213 * Get the lnum from the first argument.
22214 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022215 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216 */
22217 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022218get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219{
Bram Moolenaar33570922005-01-25 22:26:29 +000022220 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 linenr_T lnum;
22222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022223 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 if (lnum == 0) /* no valid number, try using line() */
22225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022226 rettv.v_type = VAR_NUMBER;
22227 f_line(argvars, &rettv);
22228 lnum = rettv.vval.v_number;
22229 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022230 }
22231 return lnum;
22232}
22233
22234/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022235 * Get the lnum from the first argument.
22236 * Also accepts "$", then "buf" is used.
22237 * Returns 0 on error.
22238 */
22239 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022240get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022241{
22242 if (argvars[0].v_type == VAR_STRING
22243 && argvars[0].vval.v_string != NULL
22244 && argvars[0].vval.v_string[0] == '$'
22245 && buf != NULL)
22246 return buf->b_ml.ml_line_count;
22247 return get_tv_number_chk(&argvars[0], NULL);
22248}
22249
22250/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251 * Get the string value of a variable.
22252 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022253 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22254 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 * If the String variable has never been set, return an empty string.
22256 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022257 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22258 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 */
22260 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022261get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262{
22263 static char_u mybuf[NUMBUFLEN];
22264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022265 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266}
22267
22268 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022269get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022271 char_u *res = get_tv_string_buf_chk(varp, buf);
22272
22273 return res != NULL ? res : (char_u *)"";
22274}
22275
Bram Moolenaar7d647822014-04-05 21:28:56 +020022276/*
22277 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22278 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022279 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022280get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022281{
22282 static char_u mybuf[NUMBUFLEN];
22283
22284 return get_tv_string_buf_chk(varp, mybuf);
22285}
22286
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022287 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022288get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022289{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022290 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022292 case VAR_NUMBER:
22293 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22294 return buf;
22295 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022296 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022297 break;
22298 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022299 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022300 break;
22301 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022302 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022303 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022304 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022305#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022306 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022307 break;
22308#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022309 case VAR_STRING:
22310 if (varp->vval.v_string != NULL)
22311 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022312 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022313 case VAR_SPECIAL:
22314 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22315 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022316 case VAR_JOB:
22317#ifdef FEAT_JOB
22318 {
22319 job_T *job = varp->vval.v_job;
22320 char *status = job->jv_status == JOB_FAILED ? "fail"
22321 : job->jv_status == JOB_ENDED ? "dead"
22322 : "run";
22323# ifdef UNIX
22324 vim_snprintf((char *)buf, NUMBUFLEN,
22325 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022326# elif defined(WIN32)
22327 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022328 "process %ld %s",
22329 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022330 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022331# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022332 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022333 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22334# endif
22335 return buf;
22336 }
22337#endif
22338 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022339 case VAR_CHANNEL:
22340#ifdef FEAT_CHANNEL
22341 {
22342 channel_T *channel = varp->vval.v_channel;
22343 char *status = channel_status(channel);
22344
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022345 if (channel == NULL)
22346 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22347 else
22348 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022349 "channel %d %s", channel->ch_id, status);
22350 return buf;
22351 }
22352#endif
22353 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022354 case VAR_UNKNOWN:
22355 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022358 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022359}
22360
22361/*
22362 * Find variable "name" in the list of variables.
22363 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022364 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022365 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022366 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022368 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022369find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022371 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022372 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373
Bram Moolenaara7043832005-01-21 11:56:39 +000022374 ht = find_var_ht(name, &varname);
22375 if (htp != NULL)
22376 *htp = ht;
22377 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022379 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380}
22381
22382/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022383 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022384 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022386 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022387find_var_in_ht(
22388 hashtab_T *ht,
22389 int htname,
22390 char_u *varname,
22391 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022392{
Bram Moolenaar33570922005-01-25 22:26:29 +000022393 hashitem_T *hi;
22394
22395 if (*varname == NUL)
22396 {
22397 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022398 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022399 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022400 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022401 case 'g': return &globvars_var;
22402 case 'v': return &vimvars_var;
22403 case 'b': return &curbuf->b_bufvar;
22404 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022405#ifdef FEAT_WINDOWS
22406 case 't': return &curtab->tp_winvar;
22407#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022408 case 'l': return current_funccal == NULL
22409 ? NULL : &current_funccal->l_vars_var;
22410 case 'a': return current_funccal == NULL
22411 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022412 }
22413 return NULL;
22414 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022415
22416 hi = hash_find(ht, varname);
22417 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022418 {
22419 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022420 * worked find the variable again. Don't auto-load a script if it was
22421 * loaded already, otherwise it would be loaded every time when
22422 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022423 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022424 {
22425 /* Note: script_autoload() may make "hi" invalid. It must either
22426 * be obtained again or not used. */
22427 if (!script_autoload(varname, FALSE) || aborting())
22428 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022429 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022430 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022431 if (HASHITEM_EMPTY(hi))
22432 return NULL;
22433 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022434 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022435}
22436
22437/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022438 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022439 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022440 * Set "varname" to the start of name without ':'.
22441 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022442 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022443find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022445 hashitem_T *hi;
22446
Bram Moolenaar73627d02015-08-11 15:46:09 +020022447 if (name[0] == NUL)
22448 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 if (name[1] != ':')
22450 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022451 /* The name must not start with a colon or #. */
22452 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 return NULL;
22454 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022455
22456 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022457 hi = hash_find(&compat_hashtab, name);
22458 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022459 return &compat_hashtab;
22460
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022462 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022463 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 }
22465 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022466 if (*name == 'g') /* global variable */
22467 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022468 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22469 */
22470 if (vim_strchr(name + 2, ':') != NULL
22471 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022472 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022474 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022476 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022477#ifdef FEAT_WINDOWS
22478 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022479 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022480#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022481 if (*name == 'v') /* v: variable */
22482 return &vimvarht;
22483 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022484 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022485 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022486 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487 if (*name == 's' /* script variable */
22488 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22489 return &SCRIPT_VARS(current_SID);
22490 return NULL;
22491}
22492
22493/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022494 * Get function call environment based on bactrace debug level
22495 */
22496 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022497get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022498{
22499 int i;
22500 funccall_T *funccal;
22501 funccall_T *temp_funccal;
22502
22503 funccal = current_funccal;
22504 if (debug_backtrace_level > 0)
22505 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022506 for (i = 0; i < debug_backtrace_level; i++)
22507 {
22508 temp_funccal = funccal->caller;
22509 if (temp_funccal)
22510 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022511 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022512 /* backtrace level overflow. reset to max */
22513 debug_backtrace_level = i;
22514 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022515 }
22516 return funccal;
22517}
22518
22519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022521 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 * Returns NULL when it doesn't exist.
22523 */
22524 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022525get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526{
Bram Moolenaar33570922005-01-25 22:26:29 +000022527 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022529 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 if (v == NULL)
22531 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022532 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533}
22534
22535/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022536 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 * sourcing this script and when executing functions defined in the script.
22538 */
22539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022540new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541{
Bram Moolenaara7043832005-01-21 11:56:39 +000022542 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022543 hashtab_T *ht;
22544 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022545
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22547 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022548 /* Re-allocating ga_data means that an ht_array pointing to
22549 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022550 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022551 for (i = 1; i <= ga_scripts.ga_len; ++i)
22552 {
22553 ht = &SCRIPT_VARS(i);
22554 if (ht->ht_mask == HT_INIT_SIZE - 1)
22555 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022556 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022557 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022558 }
22559
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 while (ga_scripts.ga_len < id)
22561 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022562 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022563 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022564 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566 }
22567 }
22568}
22569
22570/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022571 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22572 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 */
22574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022575init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576{
Bram Moolenaar33570922005-01-25 22:26:29 +000022577 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022578 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022579 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022580 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022581 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022582 dict_var->di_tv.vval.v_dict = dict;
22583 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022584 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022585 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22586 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587}
22588
22589/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022590 * Unreference a dictionary initialized by init_var_dict().
22591 */
22592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022593unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022594{
22595 /* Now the dict needs to be freed if no one else is using it, go back to
22596 * normal reference counting. */
22597 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22598 dict_unref(dict);
22599}
22600
22601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022603 * Frees all allocated variables and the value they contain.
22604 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 */
22606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022607vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022608{
22609 vars_clear_ext(ht, TRUE);
22610}
22611
22612/*
22613 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22614 */
22615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022616vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617{
Bram Moolenaara7043832005-01-21 11:56:39 +000022618 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022619 hashitem_T *hi;
22620 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621
Bram Moolenaar33570922005-01-25 22:26:29 +000022622 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022623 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022624 for (hi = ht->ht_array; todo > 0; ++hi)
22625 {
22626 if (!HASHITEM_EMPTY(hi))
22627 {
22628 --todo;
22629
Bram Moolenaar33570922005-01-25 22:26:29 +000022630 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022631 * ht_array might change then. hash_clear() takes care of it
22632 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022633 v = HI2DI(hi);
22634 if (free_val)
22635 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022636 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022637 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022638 }
22639 }
22640 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022641 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642}
22643
Bram Moolenaara7043832005-01-21 11:56:39 +000022644/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022645 * Delete a variable from hashtab "ht" at item "hi".
22646 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022649delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650{
Bram Moolenaar33570922005-01-25 22:26:29 +000022651 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022652
22653 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022654 clear_tv(&di->di_tv);
22655 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656}
22657
22658/*
22659 * List the value of one internal variable.
22660 */
22661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022662list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022664 char_u *tofree;
22665 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022666 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022667
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022668 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022669 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022670 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022671 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672}
22673
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022675list_one_var_a(
22676 char_u *prefix,
22677 char_u *name,
22678 int type,
22679 char_u *string,
22680 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681{
Bram Moolenaar31859182007-08-14 20:41:13 +000022682 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22683 msg_start();
22684 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 if (name != NULL) /* "a:" vars don't have a name stored */
22686 msg_puts(name);
22687 msg_putchar(' ');
22688 msg_advance(22);
22689 if (type == VAR_NUMBER)
22690 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022691 else if (type == VAR_FUNC)
22692 msg_putchar('*');
22693 else if (type == VAR_LIST)
22694 {
22695 msg_putchar('[');
22696 if (*string == '[')
22697 ++string;
22698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022699 else if (type == VAR_DICT)
22700 {
22701 msg_putchar('{');
22702 if (*string == '{')
22703 ++string;
22704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 else
22706 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022707
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022709
22710 if (type == VAR_FUNC)
22711 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022712 if (*first)
22713 {
22714 msg_clr_eos();
22715 *first = FALSE;
22716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717}
22718
22719/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022720 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 * If the variable already exists, the value is updated.
22722 * Otherwise the variable is created.
22723 */
22724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022725set_var(
22726 char_u *name,
22727 typval_T *tv,
22728 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729{
Bram Moolenaar33570922005-01-25 22:26:29 +000022730 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022732 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022734 ht = find_var_ht(name, &varname);
22735 if (ht == NULL || *varname == NUL)
22736 {
22737 EMSG2(_(e_illvar), name);
22738 return;
22739 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022740 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022741
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022742 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22743 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022744
Bram Moolenaar33570922005-01-25 22:26:29 +000022745 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022747 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022748 if (var_check_ro(v->di_flags, name, FALSE)
22749 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022750 return;
22751 if (v->di_tv.v_type != tv->v_type
22752 && !((v->di_tv.v_type == VAR_STRING
22753 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022754 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022755 || tv->v_type == VAR_NUMBER))
22756#ifdef FEAT_FLOAT
22757 && !((v->di_tv.v_type == VAR_NUMBER
22758 || v->di_tv.v_type == VAR_FLOAT)
22759 && (tv->v_type == VAR_NUMBER
22760 || tv->v_type == VAR_FLOAT))
22761#endif
22762 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022763 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022764 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022765 return;
22766 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022767
22768 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022769 * Handle setting internal v: variables separately where needed to
22770 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022771 */
22772 if (ht == &vimvarht)
22773 {
22774 if (v->di_tv.v_type == VAR_STRING)
22775 {
22776 vim_free(v->di_tv.vval.v_string);
22777 if (copy || tv->v_type != VAR_STRING)
22778 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22779 else
22780 {
22781 /* Take over the string to avoid an extra alloc/free. */
22782 v->di_tv.vval.v_string = tv->vval.v_string;
22783 tv->vval.v_string = NULL;
22784 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022785 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022786 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022787 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022788 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022789 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022790 if (STRCMP(varname, "searchforward") == 0)
22791 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022792#ifdef FEAT_SEARCH_EXTRA
22793 else if (STRCMP(varname, "hlsearch") == 0)
22794 {
22795 no_hlsearch = !v->di_tv.vval.v_number;
22796 redraw_all_later(SOME_VALID);
22797 }
22798#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022799 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022800 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022801 else if (v->di_tv.v_type != tv->v_type)
22802 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022803 }
22804
22805 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 }
22807 else /* add a new variable */
22808 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022809 /* Can't add "v:" variable. */
22810 if (ht == &vimvarht)
22811 {
22812 EMSG2(_(e_illvar), name);
22813 return;
22814 }
22815
Bram Moolenaar92124a32005-06-17 22:03:40 +000022816 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022817 if (!valid_varname(varname))
22818 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022819
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022820 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22821 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022822 if (v == NULL)
22823 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022824 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022825 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022827 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 return;
22829 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022830 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022832
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022833 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022834 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022835 else
22836 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022837 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022838 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022839 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841}
22842
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022843/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022844 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022845 * Also give an error message.
22846 */
22847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022848var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022849{
22850 if (flags & DI_FLAGS_RO)
22851 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022852 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022853 return TRUE;
22854 }
22855 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22856 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022857 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 return TRUE;
22859 }
22860 return FALSE;
22861}
22862
22863/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022864 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22865 * Also give an error message.
22866 */
22867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022868var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022869{
22870 if (flags & DI_FLAGS_FIX)
22871 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022872 EMSG2(_("E795: Cannot delete variable %s"),
22873 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022874 return TRUE;
22875 }
22876 return FALSE;
22877}
22878
22879/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022880 * Check if a funcref is assigned to a valid variable name.
22881 * Return TRUE and give an error if not.
22882 */
22883 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022884var_check_func_name(
22885 char_u *name, /* points to start of variable name */
22886 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022887{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022888 /* Allow for w: b: s: and t:. */
22889 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022890 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22891 ? name[2] : name[0]))
22892 {
22893 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22894 name);
22895 return TRUE;
22896 }
22897 /* Don't allow hiding a function. When "v" is not NULL we might be
22898 * assigning another function to the same var, the type is checked
22899 * below. */
22900 if (new_var && function_exists(name))
22901 {
22902 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22903 name);
22904 return TRUE;
22905 }
22906 return FALSE;
22907}
22908
22909/*
22910 * Check if a variable name is valid.
22911 * Return FALSE and give an error if not.
22912 */
22913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022914valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022915{
22916 char_u *p;
22917
22918 for (p = varname; *p != NUL; ++p)
22919 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22920 && *p != AUTOLOAD_CHAR)
22921 {
22922 EMSG2(_(e_illvar), varname);
22923 return FALSE;
22924 }
22925 return TRUE;
22926}
22927
22928/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022929 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022930 * Also give an error message, using "name" or _("name") when use_gettext is
22931 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022932 */
22933 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022934tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022935{
22936 if (lock & VAR_LOCKED)
22937 {
22938 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022939 name == NULL ? (char_u *)_("Unknown")
22940 : use_gettext ? (char_u *)_(name)
22941 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022942 return TRUE;
22943 }
22944 if (lock & VAR_FIXED)
22945 {
22946 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022947 name == NULL ? (char_u *)_("Unknown")
22948 : use_gettext ? (char_u *)_(name)
22949 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022950 return TRUE;
22951 }
22952 return FALSE;
22953}
22954
22955/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022956 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022957 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022958 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022959 * It is OK for "from" and "to" to point to the same item. This is used to
22960 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022961 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022963copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022965 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022966 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022967 switch (from->v_type)
22968 {
22969 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022970 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022971 to->vval.v_number = from->vval.v_number;
22972 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022973 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022974#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022975 to->vval.v_float = from->vval.v_float;
22976 break;
22977#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022978 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022979#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022980 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022981 if (to->vval.v_job != NULL)
22982 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022983 break;
22984#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022985 case VAR_CHANNEL:
22986#ifdef FEAT_CHANNEL
22987 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022988 if (to->vval.v_channel != NULL)
22989 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022990 break;
22991#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022992 case VAR_STRING:
22993 case VAR_FUNC:
22994 if (from->vval.v_string == NULL)
22995 to->vval.v_string = NULL;
22996 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022997 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022998 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022999 if (from->v_type == VAR_FUNC)
23000 func_ref(to->vval.v_string);
23001 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023002 break;
23003 case VAR_LIST:
23004 if (from->vval.v_list == NULL)
23005 to->vval.v_list = NULL;
23006 else
23007 {
23008 to->vval.v_list = from->vval.v_list;
23009 ++to->vval.v_list->lv_refcount;
23010 }
23011 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023012 case VAR_DICT:
23013 if (from->vval.v_dict == NULL)
23014 to->vval.v_dict = NULL;
23015 else
23016 {
23017 to->vval.v_dict = from->vval.v_dict;
23018 ++to->vval.v_dict->dv_refcount;
23019 }
23020 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023021 case VAR_UNKNOWN:
23022 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023023 break;
23024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025}
23026
23027/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023028 * Make a copy of an item.
23029 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023030 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23031 * reference to an already copied list/dict can be used.
23032 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023033 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023035item_copy(
23036 typval_T *from,
23037 typval_T *to,
23038 int deep,
23039 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023040{
23041 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023042 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023043
Bram Moolenaar33570922005-01-25 22:26:29 +000023044 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023045 {
23046 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023047 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023048 }
23049 ++recurse;
23050
23051 switch (from->v_type)
23052 {
23053 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023054 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023055 case VAR_STRING:
23056 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023057 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023058 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023059 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023060 copy_tv(from, to);
23061 break;
23062 case VAR_LIST:
23063 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023064 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023065 if (from->vval.v_list == NULL)
23066 to->vval.v_list = NULL;
23067 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23068 {
23069 /* use the copy made earlier */
23070 to->vval.v_list = from->vval.v_list->lv_copylist;
23071 ++to->vval.v_list->lv_refcount;
23072 }
23073 else
23074 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23075 if (to->vval.v_list == NULL)
23076 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023077 break;
23078 case VAR_DICT:
23079 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023080 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023081 if (from->vval.v_dict == NULL)
23082 to->vval.v_dict = NULL;
23083 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23084 {
23085 /* use the copy made earlier */
23086 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23087 ++to->vval.v_dict->dv_refcount;
23088 }
23089 else
23090 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23091 if (to->vval.v_dict == NULL)
23092 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023093 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023094 case VAR_UNKNOWN:
23095 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023096 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023097 }
23098 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023099 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023100}
23101
23102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 * ":echo expr1 ..." print each argument separated with a space, add a
23104 * newline at the end.
23105 * ":echon expr1 ..." print each argument plain.
23106 */
23107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023108ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109{
23110 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023111 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023112 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 char_u *p;
23114 int needclr = TRUE;
23115 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023116 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117
23118 if (eap->skip)
23119 ++emsg_skip;
23120 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23121 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023122 /* If eval1() causes an error message the text from the command may
23123 * still need to be cleared. E.g., "echo 22,44". */
23124 need_clr_eos = needclr;
23125
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023127 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 {
23129 /*
23130 * Report the invalid expression unless the expression evaluation
23131 * has been cancelled due to an aborting error, an interrupt, or an
23132 * exception.
23133 */
23134 if (!aborting())
23135 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023136 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 break;
23138 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023139 need_clr_eos = FALSE;
23140
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 if (!eap->skip)
23142 {
23143 if (atstart)
23144 {
23145 atstart = FALSE;
23146 /* Call msg_start() after eval1(), evaluating the expression
23147 * may cause a message to appear. */
23148 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023149 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023150 /* Mark the saved text as finishing the line, so that what
23151 * follows is displayed on a new line when scrolling back
23152 * at the more prompt. */
23153 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 }
23157 else if (eap->cmdidx == CMD_echo)
23158 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023159 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023160 if (p != NULL)
23161 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023163 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023165 if (*p != TAB && needclr)
23166 {
23167 /* remove any text still there from the command */
23168 msg_clr_eos();
23169 needclr = FALSE;
23170 }
23171 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 }
23173 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023174 {
23175#ifdef FEAT_MBYTE
23176 if (has_mbyte)
23177 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023178 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023179
23180 (void)msg_outtrans_len_attr(p, i, echo_attr);
23181 p += i - 1;
23182 }
23183 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023185 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023188 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023190 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 arg = skipwhite(arg);
23192 }
23193 eap->nextcmd = check_nextcmd(arg);
23194
23195 if (eap->skip)
23196 --emsg_skip;
23197 else
23198 {
23199 /* remove text that may still be there from the command */
23200 if (needclr)
23201 msg_clr_eos();
23202 if (eap->cmdidx == CMD_echo)
23203 msg_end();
23204 }
23205}
23206
23207/*
23208 * ":echohl {name}".
23209 */
23210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023211ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212{
23213 int id;
23214
23215 id = syn_name2id(eap->arg);
23216 if (id == 0)
23217 echo_attr = 0;
23218 else
23219 echo_attr = syn_id2attr(id);
23220}
23221
23222/*
23223 * ":execute expr1 ..." execute the result of an expression.
23224 * ":echomsg expr1 ..." Print a message
23225 * ":echoerr expr1 ..." Print an error
23226 * Each gets spaces around each argument and a newline at the end for
23227 * echo commands
23228 */
23229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023230ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231{
23232 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023233 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234 int ret = OK;
23235 char_u *p;
23236 garray_T ga;
23237 int len;
23238 int save_did_emsg;
23239
23240 ga_init2(&ga, 1, 80);
23241
23242 if (eap->skip)
23243 ++emsg_skip;
23244 while (*arg != NUL && *arg != '|' && *arg != '\n')
23245 {
23246 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023247 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 {
23249 /*
23250 * Report the invalid expression unless the expression evaluation
23251 * has been cancelled due to an aborting error, an interrupt, or an
23252 * exception.
23253 */
23254 if (!aborting())
23255 EMSG2(_(e_invexpr2), p);
23256 ret = FAIL;
23257 break;
23258 }
23259
23260 if (!eap->skip)
23261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023262 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263 len = (int)STRLEN(p);
23264 if (ga_grow(&ga, len + 2) == FAIL)
23265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023266 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267 ret = FAIL;
23268 break;
23269 }
23270 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023272 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 ga.ga_len += len;
23274 }
23275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023276 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 arg = skipwhite(arg);
23278 }
23279
23280 if (ret != FAIL && ga.ga_data != NULL)
23281 {
23282 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023285 out_flush();
23286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287 else if (eap->cmdidx == CMD_echoerr)
23288 {
23289 /* We don't want to abort following commands, restore did_emsg. */
23290 save_did_emsg = did_emsg;
23291 EMSG((char_u *)ga.ga_data);
23292 if (!force_abort)
23293 did_emsg = save_did_emsg;
23294 }
23295 else if (eap->cmdidx == CMD_execute)
23296 do_cmdline((char_u *)ga.ga_data,
23297 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23298 }
23299
23300 ga_clear(&ga);
23301
23302 if (eap->skip)
23303 --emsg_skip;
23304
23305 eap->nextcmd = check_nextcmd(arg);
23306}
23307
23308/*
23309 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23310 * "arg" points to the "&" or '+' when called, to "option" when returning.
23311 * Returns NULL when no option name found. Otherwise pointer to the char
23312 * after the option name.
23313 */
23314 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023315find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316{
23317 char_u *p = *arg;
23318
23319 ++p;
23320 if (*p == 'g' && p[1] == ':')
23321 {
23322 *opt_flags = OPT_GLOBAL;
23323 p += 2;
23324 }
23325 else if (*p == 'l' && p[1] == ':')
23326 {
23327 *opt_flags = OPT_LOCAL;
23328 p += 2;
23329 }
23330 else
23331 *opt_flags = 0;
23332
23333 if (!ASCII_ISALPHA(*p))
23334 return NULL;
23335 *arg = p;
23336
23337 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23338 p += 4; /* termcap option */
23339 else
23340 while (ASCII_ISALPHA(*p))
23341 ++p;
23342 return p;
23343}
23344
23345/*
23346 * ":function"
23347 */
23348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023349ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350{
23351 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023352 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353 int j;
23354 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023356 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357 char_u *name = NULL;
23358 char_u *p;
23359 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023360 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 garray_T newargs;
23362 garray_T newlines;
23363 int varargs = FALSE;
23364 int mustend = FALSE;
23365 int flags = 0;
23366 ufunc_T *fp;
23367 int indent;
23368 int nesting;
23369 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023370 dictitem_T *v;
23371 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023372 static int func_nr = 0; /* number for nameless function */
23373 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023374 hashtab_T *ht;
23375 int todo;
23376 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023377 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378
23379 /*
23380 * ":function" without argument: list functions.
23381 */
23382 if (ends_excmd(*eap->arg))
23383 {
23384 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023385 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023386 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023387 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023388 {
23389 if (!HASHITEM_EMPTY(hi))
23390 {
23391 --todo;
23392 fp = HI2UF(hi);
23393 if (!isdigit(*fp->uf_name))
23394 list_func_head(fp, FALSE);
23395 }
23396 }
23397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 eap->nextcmd = check_nextcmd(eap->arg);
23399 return;
23400 }
23401
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023402 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023403 * ":function /pat": list functions matching pattern.
23404 */
23405 if (*eap->arg == '/')
23406 {
23407 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23408 if (!eap->skip)
23409 {
23410 regmatch_T regmatch;
23411
23412 c = *p;
23413 *p = NUL;
23414 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23415 *p = c;
23416 if (regmatch.regprog != NULL)
23417 {
23418 regmatch.rm_ic = p_ic;
23419
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023420 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023421 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23422 {
23423 if (!HASHITEM_EMPTY(hi))
23424 {
23425 --todo;
23426 fp = HI2UF(hi);
23427 if (!isdigit(*fp->uf_name)
23428 && vim_regexec(&regmatch, fp->uf_name, 0))
23429 list_func_head(fp, FALSE);
23430 }
23431 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023432 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023433 }
23434 }
23435 if (*p == '/')
23436 ++p;
23437 eap->nextcmd = check_nextcmd(p);
23438 return;
23439 }
23440
23441 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023442 * Get the function name. There are these situations:
23443 * func normal function name
23444 * "name" == func, "fudi.fd_dict" == NULL
23445 * dict.func new dictionary entry
23446 * "name" == NULL, "fudi.fd_dict" set,
23447 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23448 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023449 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023450 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23451 * dict.func existing dict entry that's not a Funcref
23452 * "name" == NULL, "fudi.fd_dict" set,
23453 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023454 * s:func script-local function name
23455 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023458 name = trans_function_name(&p, eap->skip, 0, &fudi);
23459 paren = (vim_strchr(p, '(') != NULL);
23460 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 {
23462 /*
23463 * Return on an invalid expression in braces, unless the expression
23464 * evaluation has been cancelled due to an aborting error, an
23465 * interrupt, or an exception.
23466 */
23467 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023468 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023469 if (!eap->skip && fudi.fd_newkey != NULL)
23470 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023471 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 else
23475 eap->skip = TRUE;
23476 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023477
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 /* An error in a function call during evaluation of an expression in magic
23479 * braces should not cause the function not to be defined. */
23480 saved_did_emsg = did_emsg;
23481 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482
23483 /*
23484 * ":function func" with only function name: list function.
23485 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023486 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 {
23488 if (!ends_excmd(*skipwhite(p)))
23489 {
23490 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023491 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 }
23493 eap->nextcmd = check_nextcmd(p);
23494 if (eap->nextcmd != NULL)
23495 *p = NUL;
23496 if (!eap->skip && !got_int)
23497 {
23498 fp = find_func(name);
23499 if (fp != NULL)
23500 {
23501 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023502 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023504 if (FUNCLINE(fp, j) == NULL)
23505 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023506 msg_putchar('\n');
23507 msg_outnum((long)(j + 1));
23508 if (j < 9)
23509 msg_putchar(' ');
23510 if (j < 99)
23511 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023512 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 out_flush(); /* show a line at a time */
23514 ui_breakcheck();
23515 }
23516 if (!got_int)
23517 {
23518 msg_putchar('\n');
23519 msg_puts((char_u *)" endfunction");
23520 }
23521 }
23522 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023523 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023525 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526 }
23527
23528 /*
23529 * ":function name(arg1, arg2)" Define function.
23530 */
23531 p = skipwhite(p);
23532 if (*p != '(')
23533 {
23534 if (!eap->skip)
23535 {
23536 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023537 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 }
23539 /* attempt to continue by skipping some text */
23540 if (vim_strchr(p, '(') != NULL)
23541 p = vim_strchr(p, '(');
23542 }
23543 p = skipwhite(p + 1);
23544
23545 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23546 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23547
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023548 if (!eap->skip)
23549 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023550 /* Check the name of the function. Unless it's a dictionary function
23551 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023552 if (name != NULL)
23553 arg = name;
23554 else
23555 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023556 if (arg != NULL && (fudi.fd_di == NULL
23557 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023558 {
23559 if (*arg == K_SPECIAL)
23560 j = 3;
23561 else
23562 j = 0;
23563 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23564 : eval_isnamec(arg[j])))
23565 ++j;
23566 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023567 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023568 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023569 /* Disallow using the g: dict. */
23570 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23571 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023572 }
23573
Bram Moolenaar071d4272004-06-13 20:20:40 +000023574 /*
23575 * Isolate the arguments: "arg1, arg2, ...)"
23576 */
23577 while (*p != ')')
23578 {
23579 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23580 {
23581 varargs = TRUE;
23582 p += 3;
23583 mustend = TRUE;
23584 }
23585 else
23586 {
23587 arg = p;
23588 while (ASCII_ISALNUM(*p) || *p == '_')
23589 ++p;
23590 if (arg == p || isdigit(*arg)
23591 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23592 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23593 {
23594 if (!eap->skip)
23595 EMSG2(_("E125: Illegal argument: %s"), arg);
23596 break;
23597 }
23598 if (ga_grow(&newargs, 1) == FAIL)
23599 goto erret;
23600 c = *p;
23601 *p = NUL;
23602 arg = vim_strsave(arg);
23603 if (arg == NULL)
23604 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023605
23606 /* Check for duplicate argument name. */
23607 for (i = 0; i < newargs.ga_len; ++i)
23608 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23609 {
23610 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023611 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023612 goto erret;
23613 }
23614
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23616 *p = c;
23617 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 if (*p == ',')
23619 ++p;
23620 else
23621 mustend = TRUE;
23622 }
23623 p = skipwhite(p);
23624 if (mustend && *p != ')')
23625 {
23626 if (!eap->skip)
23627 EMSG2(_(e_invarg2), eap->arg);
23628 break;
23629 }
23630 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023631 if (*p != ')')
23632 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 ++p; /* skip the ')' */
23634
Bram Moolenaare9a41262005-01-15 22:18:47 +000023635 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 for (;;)
23637 {
23638 p = skipwhite(p);
23639 if (STRNCMP(p, "range", 5) == 0)
23640 {
23641 flags |= FC_RANGE;
23642 p += 5;
23643 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023644 else if (STRNCMP(p, "dict", 4) == 0)
23645 {
23646 flags |= FC_DICT;
23647 p += 4;
23648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 else if (STRNCMP(p, "abort", 5) == 0)
23650 {
23651 flags |= FC_ABORT;
23652 p += 5;
23653 }
23654 else
23655 break;
23656 }
23657
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023658 /* When there is a line break use what follows for the function body.
23659 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23660 if (*p == '\n')
23661 line_arg = p + 1;
23662 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 EMSG(_(e_trailing));
23664
23665 /*
23666 * Read the body of the function, until ":endfunction" is found.
23667 */
23668 if (KeyTyped)
23669 {
23670 /* Check if the function already exists, don't let the user type the
23671 * whole function before telling him it doesn't work! For a script we
23672 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023673 if (!eap->skip && !eap->forceit)
23674 {
23675 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23676 EMSG(_(e_funcdict));
23677 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023678 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023681 if (!eap->skip && did_emsg)
23682 goto erret;
23683
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 msg_putchar('\n'); /* don't overwrite the function name */
23685 cmdline_row = msg_row;
23686 }
23687
23688 indent = 2;
23689 nesting = 0;
23690 for (;;)
23691 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023692 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023693 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023694 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023695 saved_wait_return = FALSE;
23696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023697 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023698 sourcing_lnum_off = sourcing_lnum;
23699
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023700 if (line_arg != NULL)
23701 {
23702 /* Use eap->arg, split up in parts by line breaks. */
23703 theline = line_arg;
23704 p = vim_strchr(theline, '\n');
23705 if (p == NULL)
23706 line_arg += STRLEN(line_arg);
23707 else
23708 {
23709 *p = NUL;
23710 line_arg = p + 1;
23711 }
23712 }
23713 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714 theline = getcmdline(':', 0L, indent);
23715 else
23716 theline = eap->getline(':', eap->cookie, indent);
23717 if (KeyTyped)
23718 lines_left = Rows - 1;
23719 if (theline == NULL)
23720 {
23721 EMSG(_("E126: Missing :endfunction"));
23722 goto erret;
23723 }
23724
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023725 /* Detect line continuation: sourcing_lnum increased more than one. */
23726 if (sourcing_lnum > sourcing_lnum_off + 1)
23727 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23728 else
23729 sourcing_lnum_off = 0;
23730
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731 if (skip_until != NULL)
23732 {
23733 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23734 * don't check for ":endfunc". */
23735 if (STRCMP(theline, skip_until) == 0)
23736 {
23737 vim_free(skip_until);
23738 skip_until = NULL;
23739 }
23740 }
23741 else
23742 {
23743 /* skip ':' and blanks*/
23744 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23745 ;
23746
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023747 /* Check for "endfunction". */
23748 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023749 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023750 if (line_arg == NULL)
23751 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752 break;
23753 }
23754
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023755 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756 * at "end". */
23757 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23758 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023759 else if (STRNCMP(p, "if", 2) == 0
23760 || STRNCMP(p, "wh", 2) == 0
23761 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 || STRNCMP(p, "try", 3) == 0)
23763 indent += 2;
23764
23765 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023766 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023768 if (*p == '!')
23769 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023771 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23772 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023774 ++nesting;
23775 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023776 }
23777 }
23778
23779 /* Check for ":append" or ":insert". */
23780 p = skip_range(p, NULL);
23781 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23782 || (p[0] == 'i'
23783 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23784 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23785 skip_until = vim_strsave((char_u *)".");
23786
23787 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23788 arg = skipwhite(skiptowhite(p));
23789 if (arg[0] == '<' && arg[1] =='<'
23790 && ((p[0] == 'p' && p[1] == 'y'
23791 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23792 || (p[0] == 'p' && p[1] == 'e'
23793 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23794 || (p[0] == 't' && p[1] == 'c'
23795 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023796 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23797 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23799 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023800 || (p[0] == 'm' && p[1] == 'z'
23801 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802 ))
23803 {
23804 /* ":python <<" continues until a dot, like ":append" */
23805 p = skipwhite(arg + 2);
23806 if (*p == NUL)
23807 skip_until = vim_strsave((char_u *)".");
23808 else
23809 skip_until = vim_strsave(p);
23810 }
23811 }
23812
23813 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023814 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023815 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023816 if (line_arg == NULL)
23817 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023819 }
23820
23821 /* Copy the line to newly allocated memory. get_one_sourceline()
23822 * allocates 250 bytes per line, this saves 80% on average. The cost
23823 * is an extra alloc/free. */
23824 p = vim_strsave(theline);
23825 if (p != NULL)
23826 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023827 if (line_arg == NULL)
23828 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023829 theline = p;
23830 }
23831
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023832 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23833
23834 /* Add NULL lines for continuation lines, so that the line count is
23835 * equal to the index in the growarray. */
23836 while (sourcing_lnum_off-- > 0)
23837 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023838
23839 /* Check for end of eap->arg. */
23840 if (line_arg != NULL && *line_arg == NUL)
23841 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 }
23843
23844 /* Don't define the function when skipping commands or when an error was
23845 * detected. */
23846 if (eap->skip || did_emsg)
23847 goto erret;
23848
23849 /*
23850 * If there are no errors, add the function
23851 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023852 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023853 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023854 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023855 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023856 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023857 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023858 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023859 goto erret;
23860 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023861
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023862 fp = find_func(name);
23863 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023865 if (!eap->forceit)
23866 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023867 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023868 goto erret;
23869 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023870 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023871 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023872 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023873 name);
23874 goto erret;
23875 }
23876 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023877 ga_clear_strings(&(fp->uf_args));
23878 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023879 vim_free(name);
23880 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 }
23883 else
23884 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023885 char numbuf[20];
23886
23887 fp = NULL;
23888 if (fudi.fd_newkey == NULL && !eap->forceit)
23889 {
23890 EMSG(_(e_funcdict));
23891 goto erret;
23892 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023893 if (fudi.fd_di == NULL)
23894 {
23895 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023896 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023897 goto erret;
23898 }
23899 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023900 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023901 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023902
23903 /* Give the function a sequential number. Can only be used with a
23904 * Funcref! */
23905 vim_free(name);
23906 sprintf(numbuf, "%d", ++func_nr);
23907 name = vim_strsave((char_u *)numbuf);
23908 if (name == NULL)
23909 goto erret;
23910 }
23911
23912 if (fp == NULL)
23913 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023914 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023915 {
23916 int slen, plen;
23917 char_u *scriptname;
23918
23919 /* Check that the autoload name matches the script name. */
23920 j = FAIL;
23921 if (sourcing_name != NULL)
23922 {
23923 scriptname = autoload_name(name);
23924 if (scriptname != NULL)
23925 {
23926 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023927 plen = (int)STRLEN(p);
23928 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023929 if (slen > plen && fnamecmp(p,
23930 sourcing_name + slen - plen) == 0)
23931 j = OK;
23932 vim_free(scriptname);
23933 }
23934 }
23935 if (j == FAIL)
23936 {
23937 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23938 goto erret;
23939 }
23940 }
23941
23942 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 if (fp == NULL)
23944 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023945
23946 if (fudi.fd_dict != NULL)
23947 {
23948 if (fudi.fd_di == NULL)
23949 {
23950 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023951 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023952 if (fudi.fd_di == NULL)
23953 {
23954 vim_free(fp);
23955 goto erret;
23956 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023957 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23958 {
23959 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023960 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023961 goto erret;
23962 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023963 }
23964 else
23965 /* overwrite existing dict entry */
23966 clear_tv(&fudi.fd_di->di_tv);
23967 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023968 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023969 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023970 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023971
23972 /* behave like "dict" was used */
23973 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023974 }
23975
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023977 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023978 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23979 {
23980 vim_free(fp);
23981 goto erret;
23982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023984 fp->uf_args = newargs;
23985 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023986#ifdef FEAT_PROFILE
23987 fp->uf_tml_count = NULL;
23988 fp->uf_tml_total = NULL;
23989 fp->uf_tml_self = NULL;
23990 fp->uf_profiling = FALSE;
23991 if (prof_def_func())
23992 func_do_profile(fp);
23993#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023994 fp->uf_varargs = varargs;
23995 fp->uf_flags = flags;
23996 fp->uf_calls = 0;
23997 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023998 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999
24000erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 ga_clear_strings(&newargs);
24002 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024003ret_free:
24004 vim_free(skip_until);
24005 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024007 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024008 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024009}
24010
24011/*
24012 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024013 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024014 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024015 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024016 * TFN_INT: internal function name OK
24017 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024018 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 * Advances "pp" to just after the function name (if no error).
24020 */
24021 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024022trans_function_name(
24023 char_u **pp,
24024 int skip, /* only find the end, don't evaluate */
24025 int flags,
24026 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024028 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024029 char_u *start;
24030 char_u *end;
24031 int lead;
24032 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024034 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024035
24036 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024037 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024038 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024039
24040 /* Check for hard coded <SNR>: already translated function ID (from a user
24041 * command). */
24042 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24043 && (*pp)[2] == (int)KE_SNR)
24044 {
24045 *pp += 3;
24046 len = get_id_len(pp) + 3;
24047 return vim_strnsave(start, len);
24048 }
24049
24050 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24051 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024053 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024055
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024056 /* Note that TFN_ flags use the same values as GLV_ flags. */
24057 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024058 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024059 if (end == start)
24060 {
24061 if (!skip)
24062 EMSG(_("E129: Function name required"));
24063 goto theend;
24064 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024065 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024066 {
24067 /*
24068 * Report an invalid expression in braces, unless the expression
24069 * evaluation has been cancelled due to an aborting error, an
24070 * interrupt, or an exception.
24071 */
24072 if (!aborting())
24073 {
24074 if (end != NULL)
24075 EMSG2(_(e_invarg2), start);
24076 }
24077 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024078 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024079 goto theend;
24080 }
24081
24082 if (lv.ll_tv != NULL)
24083 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024084 if (fdp != NULL)
24085 {
24086 fdp->fd_dict = lv.ll_dict;
24087 fdp->fd_newkey = lv.ll_newkey;
24088 lv.ll_newkey = NULL;
24089 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024090 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024091 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24092 {
24093 name = vim_strsave(lv.ll_tv->vval.v_string);
24094 *pp = end;
24095 }
24096 else
24097 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024098 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24099 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024100 EMSG(_(e_funcref));
24101 else
24102 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024103 name = NULL;
24104 }
24105 goto theend;
24106 }
24107
24108 if (lv.ll_name == NULL)
24109 {
24110 /* Error found, but continue after the function name. */
24111 *pp = end;
24112 goto theend;
24113 }
24114
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024115 /* Check if the name is a Funcref. If so, use the value. */
24116 if (lv.ll_exp_name != NULL)
24117 {
24118 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024119 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024120 if (name == lv.ll_exp_name)
24121 name = NULL;
24122 }
24123 else
24124 {
24125 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024126 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024127 if (name == *pp)
24128 name = NULL;
24129 }
24130 if (name != NULL)
24131 {
24132 name = vim_strsave(name);
24133 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024134 if (STRNCMP(name, "<SNR>", 5) == 0)
24135 {
24136 /* Change "<SNR>" to the byte sequence. */
24137 name[0] = K_SPECIAL;
24138 name[1] = KS_EXTRA;
24139 name[2] = (int)KE_SNR;
24140 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24141 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024142 goto theend;
24143 }
24144
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024145 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024146 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024147 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024148 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24149 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24150 {
24151 /* When there was "s:" already or the name expanded to get a
24152 * leading "s:" then remove it. */
24153 lv.ll_name += 2;
24154 len -= 2;
24155 lead = 2;
24156 }
24157 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024158 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024159 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024160 /* skip over "s:" and "g:" */
24161 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024162 lv.ll_name += 2;
24163 len = (int)(end - lv.ll_name);
24164 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024165
24166 /*
24167 * Copy the function name to allocated memory.
24168 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24169 * Accept <SNR>123_name() outside a script.
24170 */
24171 if (skip)
24172 lead = 0; /* do nothing */
24173 else if (lead > 0)
24174 {
24175 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024176 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24177 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024178 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024179 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024180 if (current_SID <= 0)
24181 {
24182 EMSG(_(e_usingsid));
24183 goto theend;
24184 }
24185 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24186 lead += (int)STRLEN(sid_buf);
24187 }
24188 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024189 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024190 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024191 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024192 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024193 goto theend;
24194 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024195 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024196 {
24197 char_u *cp = vim_strchr(lv.ll_name, ':');
24198
24199 if (cp != NULL && cp < end)
24200 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024201 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024202 goto theend;
24203 }
24204 }
24205
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024206 name = alloc((unsigned)(len + lead + 1));
24207 if (name != NULL)
24208 {
24209 if (lead > 0)
24210 {
24211 name[0] = K_SPECIAL;
24212 name[1] = KS_EXTRA;
24213 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024214 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024215 STRCPY(name + 3, sid_buf);
24216 }
24217 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024218 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024219 }
24220 *pp = end;
24221
24222theend:
24223 clear_lval(&lv);
24224 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225}
24226
24227/*
24228 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24229 * Return 2 if "p" starts with "s:".
24230 * Return 0 otherwise.
24231 */
24232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024233eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024235 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24236 * the standard library function. */
24237 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24238 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 return 5;
24240 if (p[0] == 's' && p[1] == ':')
24241 return 2;
24242 return 0;
24243}
24244
24245/*
24246 * Return TRUE if "p" starts with "<SID>" or "s:".
24247 * Only works if eval_fname_script() returned non-zero for "p"!
24248 */
24249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024250eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251{
24252 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24253}
24254
24255/*
24256 * List the head of the function: "name(arg1, arg2)".
24257 */
24258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024259list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260{
24261 int j;
24262
24263 msg_start();
24264 if (indent)
24265 MSG_PUTS(" ");
24266 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024267 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268 {
24269 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024270 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271 }
24272 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024273 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024274 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024275 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024276 {
24277 if (j)
24278 MSG_PUTS(", ");
24279 msg_puts(FUNCARG(fp, j));
24280 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024281 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 {
24283 if (j)
24284 MSG_PUTS(", ");
24285 MSG_PUTS("...");
24286 }
24287 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024288 if (fp->uf_flags & FC_ABORT)
24289 MSG_PUTS(" abort");
24290 if (fp->uf_flags & FC_RANGE)
24291 MSG_PUTS(" range");
24292 if (fp->uf_flags & FC_DICT)
24293 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024294 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024295 if (p_verbose > 0)
24296 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297}
24298
24299/*
24300 * Find a function by name, return pointer to it in ufuncs.
24301 * Return NULL for unknown function.
24302 */
24303 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024304find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024306 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024308 hi = hash_find(&func_hashtab, name);
24309 if (!HASHITEM_EMPTY(hi))
24310 return HI2UF(hi);
24311 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312}
24313
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024314#if defined(EXITFREE) || defined(PROTO)
24315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024316free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024317{
24318 hashitem_T *hi;
24319
24320 /* Need to start all over every time, because func_free() may change the
24321 * hash table. */
24322 while (func_hashtab.ht_used > 0)
24323 for (hi = func_hashtab.ht_array; ; ++hi)
24324 if (!HASHITEM_EMPTY(hi))
24325 {
24326 func_free(HI2UF(hi));
24327 break;
24328 }
24329}
24330#endif
24331
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024332 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024333translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024334{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024335 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024336 return find_internal_func(name) >= 0;
24337 return find_func(name) != NULL;
24338}
24339
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024340/*
24341 * Return TRUE if a function "name" exists.
24342 */
24343 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024344function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024345{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024346 char_u *nm = name;
24347 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024348 int n = FALSE;
24349
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024350 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24351 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024352 nm = skipwhite(nm);
24353
24354 /* Only accept "funcname", "funcname ", "funcname (..." and
24355 * "funcname(...", not "funcname!...". */
24356 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024357 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024358 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024359 return n;
24360}
24361
Bram Moolenaara1544c02013-05-30 12:35:52 +020024362 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024363get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024364{
24365 char_u *nm = name;
24366 char_u *p;
24367
24368 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24369
24370 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024371 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024372 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024373
Bram Moolenaara1544c02013-05-30 12:35:52 +020024374 vim_free(p);
24375 return NULL;
24376}
24377
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024378/*
24379 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024380 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24381 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024382 */
24383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024384builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024385{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024386 char_u *p;
24387
24388 if (!ASCII_ISLOWER(name[0]))
24389 return FALSE;
24390 p = vim_strchr(name, AUTOLOAD_CHAR);
24391 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024392}
24393
Bram Moolenaar05159a02005-02-26 23:04:13 +000024394#if defined(FEAT_PROFILE) || defined(PROTO)
24395/*
24396 * Start profiling function "fp".
24397 */
24398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024399func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024400{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024401 int len = fp->uf_lines.ga_len;
24402
24403 if (len == 0)
24404 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024405 fp->uf_tm_count = 0;
24406 profile_zero(&fp->uf_tm_self);
24407 profile_zero(&fp->uf_tm_total);
24408 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024409 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024410 if (fp->uf_tml_total == NULL)
24411 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024412 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024413 if (fp->uf_tml_self == NULL)
24414 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024415 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024416 fp->uf_tml_idx = -1;
24417 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24418 || fp->uf_tml_self == NULL)
24419 return; /* out of memory */
24420
24421 fp->uf_profiling = TRUE;
24422}
24423
24424/*
24425 * Dump the profiling results for all functions in file "fd".
24426 */
24427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024428func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024429{
24430 hashitem_T *hi;
24431 int todo;
24432 ufunc_T *fp;
24433 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024434 ufunc_T **sorttab;
24435 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024436
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024437 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024438 if (todo == 0)
24439 return; /* nothing to dump */
24440
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024441 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024442
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24444 {
24445 if (!HASHITEM_EMPTY(hi))
24446 {
24447 --todo;
24448 fp = HI2UF(hi);
24449 if (fp->uf_profiling)
24450 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024451 if (sorttab != NULL)
24452 sorttab[st_len++] = fp;
24453
Bram Moolenaar05159a02005-02-26 23:04:13 +000024454 if (fp->uf_name[0] == K_SPECIAL)
24455 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24456 else
24457 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24458 if (fp->uf_tm_count == 1)
24459 fprintf(fd, "Called 1 time\n");
24460 else
24461 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24462 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24463 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24464 fprintf(fd, "\n");
24465 fprintf(fd, "count total (s) self (s)\n");
24466
24467 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24468 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024469 if (FUNCLINE(fp, i) == NULL)
24470 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024471 prof_func_line(fd, fp->uf_tml_count[i],
24472 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024473 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24474 }
24475 fprintf(fd, "\n");
24476 }
24477 }
24478 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024479
24480 if (sorttab != NULL && st_len > 0)
24481 {
24482 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24483 prof_total_cmp);
24484 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24485 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24486 prof_self_cmp);
24487 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24488 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024489
24490 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024491}
Bram Moolenaar73830342005-02-28 22:48:19 +000024492
24493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024494prof_sort_list(
24495 FILE *fd,
24496 ufunc_T **sorttab,
24497 int st_len,
24498 char *title,
24499 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024500{
24501 int i;
24502 ufunc_T *fp;
24503
24504 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24505 fprintf(fd, "count total (s) self (s) function\n");
24506 for (i = 0; i < 20 && i < st_len; ++i)
24507 {
24508 fp = sorttab[i];
24509 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24510 prefer_self);
24511 if (fp->uf_name[0] == K_SPECIAL)
24512 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24513 else
24514 fprintf(fd, " %s()\n", fp->uf_name);
24515 }
24516 fprintf(fd, "\n");
24517}
24518
24519/*
24520 * Print the count and times for one function or function line.
24521 */
24522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024523prof_func_line(
24524 FILE *fd,
24525 int count,
24526 proftime_T *total,
24527 proftime_T *self,
24528 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024529{
24530 if (count > 0)
24531 {
24532 fprintf(fd, "%5d ", count);
24533 if (prefer_self && profile_equal(total, self))
24534 fprintf(fd, " ");
24535 else
24536 fprintf(fd, "%s ", profile_msg(total));
24537 if (!prefer_self && profile_equal(total, self))
24538 fprintf(fd, " ");
24539 else
24540 fprintf(fd, "%s ", profile_msg(self));
24541 }
24542 else
24543 fprintf(fd, " ");
24544}
24545
24546/*
24547 * Compare function for total time sorting.
24548 */
24549 static int
24550#ifdef __BORLANDC__
24551_RTLENTRYF
24552#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024553prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024554{
24555 ufunc_T *p1, *p2;
24556
24557 p1 = *(ufunc_T **)s1;
24558 p2 = *(ufunc_T **)s2;
24559 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24560}
24561
24562/*
24563 * Compare function for self time sorting.
24564 */
24565 static int
24566#ifdef __BORLANDC__
24567_RTLENTRYF
24568#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024569prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024570{
24571 ufunc_T *p1, *p2;
24572
24573 p1 = *(ufunc_T **)s1;
24574 p2 = *(ufunc_T **)s2;
24575 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24576}
24577
Bram Moolenaar05159a02005-02-26 23:04:13 +000024578#endif
24579
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024580/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024581 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024582 * Return TRUE if a package was loaded.
24583 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024584 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024585script_autoload(
24586 char_u *name,
24587 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024588{
24589 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024590 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024591 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024592 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024593
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024594 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024595 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024596 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024597 return FALSE;
24598
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024599 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024600
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024601 /* Find the name in the list of previously loaded package names. Skip
24602 * "autoload/", it's always the same. */
24603 for (i = 0; i < ga_loaded.ga_len; ++i)
24604 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24605 break;
24606 if (!reload && i < ga_loaded.ga_len)
24607 ret = FALSE; /* was loaded already */
24608 else
24609 {
24610 /* Remember the name if it wasn't loaded already. */
24611 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24612 {
24613 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24614 tofree = NULL;
24615 }
24616
24617 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024618 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024619 ret = TRUE;
24620 }
24621
24622 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024623 return ret;
24624}
24625
24626/*
24627 * Return the autoload script name for a function or variable name.
24628 * Returns NULL when out of memory.
24629 */
24630 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024631autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024632{
24633 char_u *p;
24634 char_u *scriptname;
24635
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024636 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024637 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24638 if (scriptname == NULL)
24639 return FALSE;
24640 STRCPY(scriptname, "autoload/");
24641 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024642 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024643 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024644 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024645 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024646 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024647}
24648
Bram Moolenaar071d4272004-06-13 20:20:40 +000024649#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24650
24651/*
24652 * Function given to ExpandGeneric() to obtain the list of user defined
24653 * function names.
24654 */
24655 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024656get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024658 static long_u done;
24659 static hashitem_T *hi;
24660 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661
24662 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024664 done = 0;
24665 hi = func_hashtab.ht_array;
24666 }
24667 if (done < func_hashtab.ht_used)
24668 {
24669 if (done++ > 0)
24670 ++hi;
24671 while (HASHITEM_EMPTY(hi))
24672 ++hi;
24673 fp = HI2UF(hi);
24674
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024675 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024676 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024677
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024678 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24679 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680
24681 cat_func_name(IObuff, fp);
24682 if (xp->xp_context != EXPAND_USER_FUNC)
24683 {
24684 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024685 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686 STRCAT(IObuff, ")");
24687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024688 return IObuff;
24689 }
24690 return NULL;
24691}
24692
24693#endif /* FEAT_CMDL_COMPL */
24694
24695/*
24696 * Copy the function name of "fp" to buffer "buf".
24697 * "buf" must be able to hold the function name plus three bytes.
24698 * Takes care of script-local function names.
24699 */
24700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024701cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024703 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704 {
24705 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024706 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 }
24708 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024709 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024710}
24711
24712/*
24713 * ":delfunction {name}"
24714 */
24715 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024716ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024717{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024718 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719 char_u *p;
24720 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024721 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024722
24723 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024724 name = trans_function_name(&p, eap->skip, 0, &fudi);
24725 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024726 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024727 {
24728 if (fudi.fd_dict != NULL && !eap->skip)
24729 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024730 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024732 if (!ends_excmd(*skipwhite(p)))
24733 {
24734 vim_free(name);
24735 EMSG(_(e_trailing));
24736 return;
24737 }
24738 eap->nextcmd = check_nextcmd(p);
24739 if (eap->nextcmd != NULL)
24740 *p = NUL;
24741
24742 if (!eap->skip)
24743 fp = find_func(name);
24744 vim_free(name);
24745
24746 if (!eap->skip)
24747 {
24748 if (fp == NULL)
24749 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024750 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 return;
24752 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024753 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024754 {
24755 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24756 return;
24757 }
24758
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024759 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024760 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024761 /* Delete the dict item that refers to the function, it will
24762 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024763 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024764 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024765 else
24766 func_free(fp);
24767 }
24768}
24769
24770/*
24771 * Free a function and remove it from the list of functions.
24772 */
24773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024774func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024775{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024776 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024777
24778 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024779 ga_clear_strings(&(fp->uf_args));
24780 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024781#ifdef FEAT_PROFILE
24782 vim_free(fp->uf_tml_count);
24783 vim_free(fp->uf_tml_total);
24784 vim_free(fp->uf_tml_self);
24785#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024786
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024787 /* remove the function from the function hashtable */
24788 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24789 if (HASHITEM_EMPTY(hi))
24790 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024791 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024792 hash_remove(&func_hashtab, hi);
24793
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024794 vim_free(fp);
24795}
24796
24797/*
24798 * Unreference a Function: decrement the reference count and free it when it
24799 * becomes zero. Only for numbered functions.
24800 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024802func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024803{
24804 ufunc_T *fp;
24805
24806 if (name != NULL && isdigit(*name))
24807 {
24808 fp = find_func(name);
24809 if (fp == NULL)
24810 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024811 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024812 {
24813 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024814 * when "uf_calls" becomes zero. */
24815 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024816 func_free(fp);
24817 }
24818 }
24819}
24820
24821/*
24822 * Count a reference to a Function.
24823 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024825func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024826{
24827 ufunc_T *fp;
24828
24829 if (name != NULL && isdigit(*name))
24830 {
24831 fp = find_func(name);
24832 if (fp == NULL)
24833 EMSG2(_(e_intern2), "func_ref()");
24834 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024835 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024836 }
24837}
24838
24839/*
24840 * Call a user function.
24841 */
24842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024843call_user_func(
24844 ufunc_T *fp, /* pointer to function */
24845 int argcount, /* nr of args */
24846 typval_T *argvars, /* arguments */
24847 typval_T *rettv, /* return value */
24848 linenr_T firstline, /* first line of range */
24849 linenr_T lastline, /* last line of range */
24850 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851{
Bram Moolenaar33570922005-01-25 22:26:29 +000024852 char_u *save_sourcing_name;
24853 linenr_T save_sourcing_lnum;
24854 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024855 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024856 int save_did_emsg;
24857 static int depth = 0;
24858 dictitem_T *v;
24859 int fixvar_idx = 0; /* index in fixvar[] */
24860 int i;
24861 int ai;
24862 char_u numbuf[NUMBUFLEN];
24863 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024864 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024865#ifdef FEAT_PROFILE
24866 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024867 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869
24870 /* If depth of calling is getting too high, don't execute the function */
24871 if (depth >= p_mfd)
24872 {
24873 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024874 rettv->v_type = VAR_NUMBER;
24875 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024876 return;
24877 }
24878 ++depth;
24879
24880 line_breakcheck(); /* check for CTRL-C hit */
24881
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024882 fc = (funccall_T *)alloc(sizeof(funccall_T));
24883 fc->caller = current_funccal;
24884 current_funccal = fc;
24885 fc->func = fp;
24886 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024887 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024888 fc->linenr = 0;
24889 fc->returned = FALSE;
24890 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024892 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24893 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894
Bram Moolenaar33570922005-01-25 22:26:29 +000024895 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024896 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024897 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24898 * each argument variable and saves a lot of time.
24899 */
24900 /*
24901 * Init l: variables.
24902 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024903 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024904 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024905 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024906 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24907 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024908 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024909 name = v->di_key;
24910 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024911 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024912 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024913 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024914 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024915 v->di_tv.vval.v_dict = selfdict;
24916 ++selfdict->dv_refcount;
24917 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024918
Bram Moolenaar33570922005-01-25 22:26:29 +000024919 /*
24920 * Init a: variables.
24921 * Set a:0 to "argcount".
24922 * Set a:000 to a list with room for the "..." arguments.
24923 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024924 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024925 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024926 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024927 /* Use "name" to avoid a warning from some compiler that checks the
24928 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024929 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024930 name = v->di_key;
24931 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024932 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024933 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024934 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024935 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024936 v->di_tv.vval.v_list = &fc->l_varlist;
24937 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24938 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24939 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024940
24941 /*
24942 * Set a:firstline to "firstline" and a:lastline to "lastline".
24943 * Set a:name to named arguments.
24944 * Set a:N to the "..." arguments.
24945 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024946 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024947 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024948 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024949 (varnumber_T)lastline);
24950 for (i = 0; i < argcount; ++i)
24951 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024952 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024953 if (ai < 0)
24954 /* named argument a:name */
24955 name = FUNCARG(fp, i);
24956 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024957 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024958 /* "..." argument a:1, a:2, etc. */
24959 sprintf((char *)numbuf, "%d", ai + 1);
24960 name = numbuf;
24961 }
24962 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24963 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024964 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024965 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24966 }
24967 else
24968 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024969 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24970 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024971 if (v == NULL)
24972 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024973 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024974 }
24975 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024976 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024977
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024978 /* Note: the values are copied directly to avoid alloc/free.
24979 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024980 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024981 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024982
24983 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24984 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024985 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24986 fc->l_listitems[ai].li_tv = argvars[i];
24987 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024988 }
24989 }
24990
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991 /* Don't redraw while executing the function. */
24992 ++RedrawingDisabled;
24993 save_sourcing_name = sourcing_name;
24994 save_sourcing_lnum = sourcing_lnum;
24995 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024996 /* need space for function name + ("function " + 3) or "[number]" */
24997 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24998 + STRLEN(fp->uf_name) + 20;
24999 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000 if (sourcing_name != NULL)
25001 {
25002 if (save_sourcing_name != NULL
25003 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025004 sprintf((char *)sourcing_name, "%s[%d]..",
25005 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006 else
25007 STRCPY(sourcing_name, "function ");
25008 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25009
25010 if (p_verbose >= 12)
25011 {
25012 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025013 verbose_enter_scroll();
25014
Bram Moolenaar555b2802005-05-19 21:08:39 +000025015 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025016 if (p_verbose >= 14)
25017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025019 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025020 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025021 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025022
25023 msg_puts((char_u *)"(");
25024 for (i = 0; i < argcount; ++i)
25025 {
25026 if (i > 0)
25027 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025028 if (argvars[i].v_type == VAR_NUMBER)
25029 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030 else
25031 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025032 /* Do not want errors such as E724 here. */
25033 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025034 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025035 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025036 if (s != NULL)
25037 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025038 if (vim_strsize(s) > MSG_BUF_CLEN)
25039 {
25040 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25041 s = buf;
25042 }
25043 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025044 vim_free(tofree);
25045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046 }
25047 }
25048 msg_puts((char_u *)")");
25049 }
25050 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025051
25052 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025053 --no_wait_return;
25054 }
25055 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025056#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025057 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025058 {
25059 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25060 func_do_profile(fp);
25061 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025062 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025063 {
25064 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025065 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025066 profile_zero(&fp->uf_tm_children);
25067 }
25068 script_prof_save(&wait_start);
25069 }
25070#endif
25071
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025073 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025074 save_did_emsg = did_emsg;
25075 did_emsg = FALSE;
25076
25077 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025078 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25080
25081 --RedrawingDisabled;
25082
25083 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025084 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025086 clear_tv(rettv);
25087 rettv->v_type = VAR_NUMBER;
25088 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 }
25090
Bram Moolenaar05159a02005-02-26 23:04:13 +000025091#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025092 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025093 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025094 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025095 profile_end(&call_start);
25096 profile_sub_wait(&wait_start, &call_start);
25097 profile_add(&fp->uf_tm_total, &call_start);
25098 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025099 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025100 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025101 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25102 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025103 }
25104 }
25105#endif
25106
Bram Moolenaar071d4272004-06-13 20:20:40 +000025107 /* when being verbose, mention the return value */
25108 if (p_verbose >= 12)
25109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025111 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025114 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025115 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025116 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025117 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025119 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025120 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025121 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025122 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025123 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025124
Bram Moolenaar555b2802005-05-19 21:08:39 +000025125 /* The value may be very long. Skip the middle part, so that we
25126 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025127 * truncate it at the end. Don't want errors such as E724 here. */
25128 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025129 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025130 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025131 if (s != NULL)
25132 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025133 if (vim_strsize(s) > MSG_BUF_CLEN)
25134 {
25135 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25136 s = buf;
25137 }
25138 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025139 vim_free(tofree);
25140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025141 }
25142 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025143
25144 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025145 --no_wait_return;
25146 }
25147
25148 vim_free(sourcing_name);
25149 sourcing_name = save_sourcing_name;
25150 sourcing_lnum = save_sourcing_lnum;
25151 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025152#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025153 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025154 script_prof_restore(&wait_start);
25155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156
25157 if (p_verbose >= 12 && sourcing_name != NULL)
25158 {
25159 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025160 verbose_enter_scroll();
25161
Bram Moolenaar555b2802005-05-19 21:08:39 +000025162 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025163 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025164
25165 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025166 --no_wait_return;
25167 }
25168
25169 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025170 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025171 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025172
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025173 /* If the a:000 list and the l: and a: dicts are not referenced we can
25174 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025175 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25176 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25177 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25178 {
25179 free_funccal(fc, FALSE);
25180 }
25181 else
25182 {
25183 hashitem_T *hi;
25184 listitem_T *li;
25185 int todo;
25186
25187 /* "fc" is still in use. This can happen when returning "a:000" or
25188 * assigning "l:" to a global variable.
25189 * Link "fc" in the list for garbage collection later. */
25190 fc->caller = previous_funccal;
25191 previous_funccal = fc;
25192
25193 /* Make a copy of the a: variables, since we didn't do that above. */
25194 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25195 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25196 {
25197 if (!HASHITEM_EMPTY(hi))
25198 {
25199 --todo;
25200 v = HI2DI(hi);
25201 copy_tv(&v->di_tv, &v->di_tv);
25202 }
25203 }
25204
25205 /* Make a copy of the a:000 items, since we didn't do that above. */
25206 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25207 copy_tv(&li->li_tv, &li->li_tv);
25208 }
25209}
25210
25211/*
25212 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025213 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025214 */
25215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025216can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025217{
25218 return (fc->l_varlist.lv_copyID != copyID
25219 && fc->l_vars.dv_copyID != copyID
25220 && fc->l_avars.dv_copyID != copyID);
25221}
25222
25223/*
25224 * Free "fc" and what it contains.
25225 */
25226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025227free_funccal(
25228 funccall_T *fc,
25229 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025230{
25231 listitem_T *li;
25232
25233 /* The a: variables typevals may not have been allocated, only free the
25234 * allocated variables. */
25235 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25236
25237 /* free all l: variables */
25238 vars_clear(&fc->l_vars.dv_hashtab);
25239
25240 /* Free the a:000 variables if they were allocated. */
25241 if (free_val)
25242 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25243 clear_tv(&li->li_tv);
25244
25245 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246}
25247
25248/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025249 * Add a number variable "name" to dict "dp" with value "nr".
25250 */
25251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025252add_nr_var(
25253 dict_T *dp,
25254 dictitem_T *v,
25255 char *name,
25256 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025257{
25258 STRCPY(v->di_key, name);
25259 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25260 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25261 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025262 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025263 v->di_tv.vval.v_number = nr;
25264}
25265
25266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267 * ":return [expr]"
25268 */
25269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025270ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271{
25272 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025273 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 int returning = FALSE;
25275
25276 if (current_funccal == NULL)
25277 {
25278 EMSG(_("E133: :return not inside a function"));
25279 return;
25280 }
25281
25282 if (eap->skip)
25283 ++emsg_skip;
25284
25285 eap->nextcmd = NULL;
25286 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025287 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288 {
25289 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025290 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025292 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 }
25294 /* It's safer to return also on error. */
25295 else if (!eap->skip)
25296 {
25297 /*
25298 * Return unless the expression evaluation has been cancelled due to an
25299 * aborting error, an interrupt, or an exception.
25300 */
25301 if (!aborting())
25302 returning = do_return(eap, FALSE, TRUE, NULL);
25303 }
25304
25305 /* When skipping or the return gets pending, advance to the next command
25306 * in this line (!returning). Otherwise, ignore the rest of the line.
25307 * Following lines will be ignored by get_func_line(). */
25308 if (returning)
25309 eap->nextcmd = NULL;
25310 else if (eap->nextcmd == NULL) /* no argument */
25311 eap->nextcmd = check_nextcmd(arg);
25312
25313 if (eap->skip)
25314 --emsg_skip;
25315}
25316
25317/*
25318 * Return from a function. Possibly makes the return pending. Also called
25319 * for a pending return at the ":endtry" or after returning from an extra
25320 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025321 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025322 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323 * FALSE when the return gets pending.
25324 */
25325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025326do_return(
25327 exarg_T *eap,
25328 int reanimate,
25329 int is_cmd,
25330 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331{
25332 int idx;
25333 struct condstack *cstack = eap->cstack;
25334
25335 if (reanimate)
25336 /* Undo the return. */
25337 current_funccal->returned = FALSE;
25338
25339 /*
25340 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25341 * not in its finally clause (which then is to be executed next) is found.
25342 * In this case, make the ":return" pending for execution at the ":endtry".
25343 * Otherwise, return normally.
25344 */
25345 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25346 if (idx >= 0)
25347 {
25348 cstack->cs_pending[idx] = CSTP_RETURN;
25349
25350 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025351 /* A pending return again gets pending. "rettv" points to an
25352 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025354 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025355 else
25356 {
25357 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025358 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025360 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025362 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 {
25364 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025365 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025366 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367 else
25368 EMSG(_(e_outofmem));
25369 }
25370 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025371 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372
25373 if (reanimate)
25374 {
25375 /* The pending return value could be overwritten by a ":return"
25376 * without argument in a finally clause; reset the default
25377 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025378 current_funccal->rettv->v_type = VAR_NUMBER;
25379 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 }
25381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025382 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383 }
25384 else
25385 {
25386 current_funccal->returned = TRUE;
25387
25388 /* If the return is carried out now, store the return value. For
25389 * a return immediately after reanimation, the value is already
25390 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025391 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025393 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025394 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025396 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397 }
25398 }
25399
25400 return idx < 0;
25401}
25402
25403/*
25404 * Free the variable with a pending return value.
25405 */
25406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025407discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025408{
Bram Moolenaar33570922005-01-25 22:26:29 +000025409 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410}
25411
25412/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025413 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414 * is an allocated string. Used by report_pending() for verbose messages.
25415 */
25416 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025417get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025418{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025419 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025420 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025421 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025422
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025423 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025424 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025425 if (s == NULL)
25426 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025427
25428 STRCPY(IObuff, ":return ");
25429 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25430 if (STRLEN(s) + 8 >= IOSIZE)
25431 STRCPY(IObuff + IOSIZE - 4, "...");
25432 vim_free(tofree);
25433 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434}
25435
25436/*
25437 * Get next function line.
25438 * Called by do_cmdline() to get the next line.
25439 * Returns allocated string, or NULL for end of function.
25440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025442get_func_line(
25443 int c UNUSED,
25444 void *cookie,
25445 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446{
Bram Moolenaar33570922005-01-25 22:26:29 +000025447 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025448 ufunc_T *fp = fcp->func;
25449 char_u *retval;
25450 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451
25452 /* If breakpoints have been added/deleted need to check for it. */
25453 if (fcp->dbg_tick != debug_tick)
25454 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025455 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025456 sourcing_lnum);
25457 fcp->dbg_tick = debug_tick;
25458 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025459#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025460 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025461 func_line_end(cookie);
25462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025463
Bram Moolenaar05159a02005-02-26 23:04:13 +000025464 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025465 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25466 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467 retval = NULL;
25468 else
25469 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025470 /* Skip NULL lines (continuation lines). */
25471 while (fcp->linenr < gap->ga_len
25472 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25473 ++fcp->linenr;
25474 if (fcp->linenr >= gap->ga_len)
25475 retval = NULL;
25476 else
25477 {
25478 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25479 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025480#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025481 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025482 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025483#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485 }
25486
25487 /* Did we encounter a breakpoint? */
25488 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25489 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025490 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025491 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025492 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493 sourcing_lnum);
25494 fcp->dbg_tick = debug_tick;
25495 }
25496
25497 return retval;
25498}
25499
Bram Moolenaar05159a02005-02-26 23:04:13 +000025500#if defined(FEAT_PROFILE) || defined(PROTO)
25501/*
25502 * Called when starting to read a function line.
25503 * "sourcing_lnum" must be correct!
25504 * When skipping lines it may not actually be executed, but we won't find out
25505 * until later and we need to store the time now.
25506 */
25507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025508func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025509{
25510 funccall_T *fcp = (funccall_T *)cookie;
25511 ufunc_T *fp = fcp->func;
25512
25513 if (fp->uf_profiling && sourcing_lnum >= 1
25514 && sourcing_lnum <= fp->uf_lines.ga_len)
25515 {
25516 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025517 /* Skip continuation lines. */
25518 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25519 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025520 fp->uf_tml_execed = FALSE;
25521 profile_start(&fp->uf_tml_start);
25522 profile_zero(&fp->uf_tml_children);
25523 profile_get_wait(&fp->uf_tml_wait);
25524 }
25525}
25526
25527/*
25528 * Called when actually executing a function line.
25529 */
25530 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025531func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025532{
25533 funccall_T *fcp = (funccall_T *)cookie;
25534 ufunc_T *fp = fcp->func;
25535
25536 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25537 fp->uf_tml_execed = TRUE;
25538}
25539
25540/*
25541 * Called when done with a function line.
25542 */
25543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025544func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025545{
25546 funccall_T *fcp = (funccall_T *)cookie;
25547 ufunc_T *fp = fcp->func;
25548
25549 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25550 {
25551 if (fp->uf_tml_execed)
25552 {
25553 ++fp->uf_tml_count[fp->uf_tml_idx];
25554 profile_end(&fp->uf_tml_start);
25555 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025556 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025557 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25558 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025559 }
25560 fp->uf_tml_idx = -1;
25561 }
25562}
25563#endif
25564
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565/*
25566 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025567 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568 */
25569 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025570func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025571{
Bram Moolenaar33570922005-01-25 22:26:29 +000025572 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025573
25574 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25575 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025576 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577 || fcp->returned);
25578}
25579
25580/*
25581 * return TRUE if cookie indicates a function which "abort"s on errors.
25582 */
25583 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025584func_has_abort(
25585 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025587 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025588}
25589
25590#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25591typedef enum
25592{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025593 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25594 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25595 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596} var_flavour_T;
25597
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025598static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599
25600 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025601var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602{
25603 char_u *p = varname;
25604
25605 if (ASCII_ISUPPER(*p))
25606 {
25607 while (*(++p))
25608 if (ASCII_ISLOWER(*p))
25609 return VAR_FLAVOUR_SESSION;
25610 return VAR_FLAVOUR_VIMINFO;
25611 }
25612 else
25613 return VAR_FLAVOUR_DEFAULT;
25614}
25615#endif
25616
25617#if defined(FEAT_VIMINFO) || defined(PROTO)
25618/*
25619 * Restore global vars that start with a capital from the viminfo file
25620 */
25621 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025622read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623{
25624 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025625 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025626 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025627 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025628
25629 if (!writing && (find_viminfo_parameter('!') != NULL))
25630 {
25631 tab = vim_strchr(virp->vir_line + 1, '\t');
25632 if (tab != NULL)
25633 {
25634 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025635 switch (*tab)
25636 {
25637 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025638#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025639 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025640#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025641 case 'D': type = VAR_DICT; break;
25642 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025643 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645
25646 tab = vim_strchr(tab, '\t');
25647 if (tab != NULL)
25648 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025649 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025650 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025651 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025653#ifdef FEAT_FLOAT
25654 else if (type == VAR_FLOAT)
25655 (void)string2float(tab + 1, &tv.vval.v_float);
25656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025658 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025659 if (type == VAR_DICT || type == VAR_LIST)
25660 {
25661 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25662
25663 if (etv == NULL)
25664 /* Failed to parse back the dict or list, use it as a
25665 * string. */
25666 tv.v_type = VAR_STRING;
25667 else
25668 {
25669 vim_free(tv.vval.v_string);
25670 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025671 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025672 }
25673 }
25674
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025675 /* when in a function use global variables */
25676 save_funccal = current_funccal;
25677 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025678 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025679 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025680
25681 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025682 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025683 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25684 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025685 }
25686 }
25687 }
25688
25689 return viminfo_readline(virp);
25690}
25691
25692/*
25693 * Write global vars that start with a capital to the viminfo file
25694 */
25695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025696write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025697{
Bram Moolenaar33570922005-01-25 22:26:29 +000025698 hashitem_T *hi;
25699 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025700 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025701 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025702 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025703 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025704 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025705
25706 if (find_viminfo_parameter('!') == NULL)
25707 return;
25708
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025709 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025710
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025711 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025712 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025713 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025714 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025715 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025716 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025717 this_var = HI2DI(hi);
25718 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025719 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025720 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025721 {
25722 case VAR_STRING: s = "STR"; break;
25723 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025724 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025725 case VAR_DICT: s = "DIC"; break;
25726 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025727 case VAR_SPECIAL: s = "XPL"; break;
25728
25729 case VAR_UNKNOWN:
25730 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025731 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025732 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025733 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025734 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025735 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025736 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025737 if (p != NULL)
25738 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025739 vim_free(tofree);
25740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741 }
25742 }
25743}
25744#endif
25745
25746#if defined(FEAT_SESSION) || defined(PROTO)
25747 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025748store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749{
Bram Moolenaar33570922005-01-25 22:26:29 +000025750 hashitem_T *hi;
25751 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025752 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753 char_u *p, *t;
25754
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025755 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025756 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025757 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025758 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025759 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025760 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025761 this_var = HI2DI(hi);
25762 if ((this_var->di_tv.v_type == VAR_NUMBER
25763 || this_var->di_tv.v_type == VAR_STRING)
25764 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025765 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025766 /* Escape special characters with a backslash. Turn a LF and
25767 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025768 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025769 (char_u *)"\\\"\n\r");
25770 if (p == NULL) /* out of memory */
25771 break;
25772 for (t = p; *t != NUL; ++t)
25773 if (*t == '\n')
25774 *t = 'n';
25775 else if (*t == '\r')
25776 *t = 'r';
25777 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025778 this_var->di_key,
25779 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25780 : ' ',
25781 p,
25782 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25783 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025784 || put_eol(fd) == FAIL)
25785 {
25786 vim_free(p);
25787 return FAIL;
25788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025789 vim_free(p);
25790 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025791#ifdef FEAT_FLOAT
25792 else if (this_var->di_tv.v_type == VAR_FLOAT
25793 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25794 {
25795 float_T f = this_var->di_tv.vval.v_float;
25796 int sign = ' ';
25797
25798 if (f < 0)
25799 {
25800 f = -f;
25801 sign = '-';
25802 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025803 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025804 this_var->di_key, sign, f) < 0)
25805 || put_eol(fd) == FAIL)
25806 return FAIL;
25807 }
25808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809 }
25810 }
25811 return OK;
25812}
25813#endif
25814
Bram Moolenaar661b1822005-07-28 22:36:45 +000025815/*
25816 * Display script name where an item was last set.
25817 * Should only be invoked when 'verbose' is non-zero.
25818 */
25819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025820last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025821{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025822 char_u *p;
25823
Bram Moolenaar661b1822005-07-28 22:36:45 +000025824 if (scriptID != 0)
25825 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025826 p = home_replace_save(NULL, get_scriptname(scriptID));
25827 if (p != NULL)
25828 {
25829 verbose_enter();
25830 MSG_PUTS(_("\n\tLast set from "));
25831 MSG_PUTS(p);
25832 vim_free(p);
25833 verbose_leave();
25834 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025835 }
25836}
25837
Bram Moolenaard812df62008-11-09 12:46:09 +000025838/*
25839 * List v:oldfiles in a nice way.
25840 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025842ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025843{
25844 list_T *l = vimvars[VV_OLDFILES].vv_list;
25845 listitem_T *li;
25846 int nr = 0;
25847
25848 if (l == NULL)
25849 msg((char_u *)_("No old files"));
25850 else
25851 {
25852 msg_start();
25853 msg_scroll = TRUE;
25854 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25855 {
25856 msg_outnum((long)++nr);
25857 MSG_PUTS(": ");
25858 msg_outtrans(get_tv_string(&li->li_tv));
25859 msg_putchar('\n');
25860 out_flush(); /* output one line at a time */
25861 ui_breakcheck();
25862 }
25863 /* Assume "got_int" was set to truncate the listing. */
25864 got_int = FALSE;
25865
25866#ifdef FEAT_BROWSE_CMD
25867 if (cmdmod.browse)
25868 {
25869 quit_more = FALSE;
25870 nr = prompt_for_number(FALSE);
25871 msg_starthere();
25872 if (nr > 0)
25873 {
25874 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25875 (long)nr);
25876
25877 if (p != NULL)
25878 {
25879 p = expand_env_save(p);
25880 eap->arg = p;
25881 eap->cmdidx = CMD_edit;
25882 cmdmod.browse = FALSE;
25883 do_exedit(eap, NULL);
25884 vim_free(p);
25885 }
25886 }
25887 }
25888#endif
25889 }
25890}
25891
Bram Moolenaar53744302015-07-17 17:38:22 +020025892/* reset v:option_new, v:option_old and v:option_type */
25893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025894reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025895{
25896 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25897 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25898 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25899}
25900
25901
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902#endif /* FEAT_EVAL */
25903
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025905#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025906
25907#ifdef WIN3264
25908/*
25909 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25910 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025911static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25912static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25913static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025914
25915/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025916 * Get the short path (8.3) for the filename in "fnamep".
25917 * Only works for a valid file name.
25918 * When the path gets longer "fnamep" is changed and the allocated buffer
25919 * is put in "bufp".
25920 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25921 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922 */
25923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025924get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025925{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025926 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025927 char_u *newbuf;
25928
25929 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025930 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025931 if (l > len - 1)
25932 {
25933 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025934 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025935 newbuf = vim_strnsave(*fnamep, l);
25936 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025937 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025938
25939 vim_free(*bufp);
25940 *fnamep = *bufp = newbuf;
25941
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025942 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025943 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025944 }
25945
25946 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025947 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948}
25949
25950/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025951 * Get the short path (8.3) for the filename in "fname". The converted
25952 * path is returned in "bufp".
25953 *
25954 * Some of the directories specified in "fname" may not exist. This function
25955 * will shorten the existing directories at the beginning of the path and then
25956 * append the remaining non-existing path.
25957 *
25958 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025959 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025960 * bufp - Pointer to an allocated buffer for the filename.
25961 * fnamelen - Length of the filename pointed to by fname
25962 *
25963 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025964 */
25965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025966shortpath_for_invalid_fname(
25967 char_u **fname,
25968 char_u **bufp,
25969 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025971 char_u *short_fname, *save_fname, *pbuf_unused;
25972 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025974 int old_len, len;
25975 int new_len, sfx_len;
25976 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977
25978 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025979 old_len = *fnamelen;
25980 save_fname = vim_strnsave(*fname, old_len);
25981 pbuf_unused = NULL;
25982 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025983
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025984 endp = save_fname + old_len - 1; /* Find the end of the copy */
25985 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025986
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025987 /*
25988 * Try shortening the supplied path till it succeeds by removing one
25989 * directory at a time from the tail of the path.
25990 */
25991 len = 0;
25992 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025994 /* go back one path-separator */
25995 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25996 --endp;
25997 if (endp <= save_fname)
25998 break; /* processed the complete path */
25999
26000 /*
26001 * Replace the path separator with a NUL and try to shorten the
26002 * resulting path.
26003 */
26004 ch = *endp;
26005 *endp = 0;
26006 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026007 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026008 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26009 {
26010 retval = FAIL;
26011 goto theend;
26012 }
26013 *endp = ch; /* preserve the string */
26014
26015 if (len > 0)
26016 break; /* successfully shortened the path */
26017
26018 /* failed to shorten the path. Skip the path separator */
26019 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026020 }
26021
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026022 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026024 /*
26025 * Succeeded in shortening the path. Now concatenate the shortened
26026 * path with the remaining path at the tail.
26027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026028
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026029 /* Compute the length of the new path. */
26030 sfx_len = (int)(save_endp - endp) + 1;
26031 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026032
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026033 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026034 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026035 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026036 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026037 /* There is not enough space in the currently allocated string,
26038 * copy it to a buffer big enough. */
26039 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026040 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026041 {
26042 retval = FAIL;
26043 goto theend;
26044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026045 }
26046 else
26047 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026048 /* Transfer short_fname to the main buffer (it's big enough),
26049 * unless get_short_pathname() did its work in-place. */
26050 *fname = *bufp = save_fname;
26051 if (short_fname != save_fname)
26052 vim_strncpy(save_fname, short_fname, len);
26053 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026054 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026055
26056 /* concat the not-shortened part of the path */
26057 vim_strncpy(*fname + len, endp, sfx_len);
26058 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026059 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026060
26061theend:
26062 vim_free(pbuf_unused);
26063 vim_free(save_fname);
26064
26065 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026066}
26067
26068/*
26069 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026070 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071 */
26072 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026073shortpath_for_partial(
26074 char_u **fnamep,
26075 char_u **bufp,
26076 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077{
26078 int sepcount, len, tflen;
26079 char_u *p;
26080 char_u *pbuf, *tfname;
26081 int hasTilde;
26082
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026083 /* Count up the path separators from the RHS.. so we know which part
26084 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026085 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026086 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026087 if (vim_ispathsep(*p))
26088 ++sepcount;
26089
26090 /* Need full path first (use expand_env() to remove a "~/") */
26091 hasTilde = (**fnamep == '~');
26092 if (hasTilde)
26093 pbuf = tfname = expand_env_save(*fnamep);
26094 else
26095 pbuf = tfname = FullName_save(*fnamep, FALSE);
26096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026097 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026098
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026099 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26100 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026101
26102 if (len == 0)
26103 {
26104 /* Don't have a valid filename, so shorten the rest of the
26105 * path if we can. This CAN give us invalid 8.3 filenames, but
26106 * there's not a lot of point in guessing what it might be.
26107 */
26108 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026109 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26110 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026111 }
26112
26113 /* Count the paths backward to find the beginning of the desired string. */
26114 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026115 {
26116#ifdef FEAT_MBYTE
26117 if (has_mbyte)
26118 p -= mb_head_off(tfname, p);
26119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026120 if (vim_ispathsep(*p))
26121 {
26122 if (sepcount == 0 || (hasTilde && sepcount == 1))
26123 break;
26124 else
26125 sepcount --;
26126 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026128 if (hasTilde)
26129 {
26130 --p;
26131 if (p >= tfname)
26132 *p = '~';
26133 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026134 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026135 }
26136 else
26137 ++p;
26138
26139 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26140 vim_free(*bufp);
26141 *fnamelen = (int)STRLEN(p);
26142 *bufp = pbuf;
26143 *fnamep = p;
26144
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026145 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026146}
26147#endif /* WIN3264 */
26148
26149/*
26150 * Adjust a filename, according to a string of modifiers.
26151 * *fnamep must be NUL terminated when called. When returning, the length is
26152 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026153 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026154 * When there is an error, *fnamep is set to NULL.
26155 */
26156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026157modify_fname(
26158 char_u *src, /* string with modifiers */
26159 int *usedlen, /* characters after src that are used */
26160 char_u **fnamep, /* file name so far */
26161 char_u **bufp, /* buffer for allocated file name or NULL */
26162 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026163{
26164 int valid = 0;
26165 char_u *tail;
26166 char_u *s, *p, *pbuf;
26167 char_u dirname[MAXPATHL];
26168 int c;
26169 int has_fullname = 0;
26170#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026171 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026172 int has_shortname = 0;
26173#endif
26174
26175repeat:
26176 /* ":p" - full path/file_name */
26177 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26178 {
26179 has_fullname = 1;
26180
26181 valid |= VALID_PATH;
26182 *usedlen += 2;
26183
26184 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26185 if ((*fnamep)[0] == '~'
26186#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26187 && ((*fnamep)[1] == '/'
26188# ifdef BACKSLASH_IN_FILENAME
26189 || (*fnamep)[1] == '\\'
26190# endif
26191 || (*fnamep)[1] == NUL)
26192
26193#endif
26194 )
26195 {
26196 *fnamep = expand_env_save(*fnamep);
26197 vim_free(*bufp); /* free any allocated file name */
26198 *bufp = *fnamep;
26199 if (*fnamep == NULL)
26200 return -1;
26201 }
26202
26203 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026204 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026205 {
26206 if (vim_ispathsep(*p)
26207 && p[1] == '.'
26208 && (p[2] == NUL
26209 || vim_ispathsep(p[2])
26210 || (p[2] == '.'
26211 && (p[3] == NUL || vim_ispathsep(p[3])))))
26212 break;
26213 }
26214
26215 /* FullName_save() is slow, don't use it when not needed. */
26216 if (*p != NUL || !vim_isAbsName(*fnamep))
26217 {
26218 *fnamep = FullName_save(*fnamep, *p != NUL);
26219 vim_free(*bufp); /* free any allocated file name */
26220 *bufp = *fnamep;
26221 if (*fnamep == NULL)
26222 return -1;
26223 }
26224
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026225#ifdef WIN3264
26226# if _WIN32_WINNT >= 0x0500
26227 if (vim_strchr(*fnamep, '~') != NULL)
26228 {
26229 /* Expand 8.3 filename to full path. Needed to make sure the same
26230 * file does not have two different names.
26231 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26232 p = alloc(_MAX_PATH + 1);
26233 if (p != NULL)
26234 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026235 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026236 {
26237 vim_free(*bufp);
26238 *bufp = *fnamep = p;
26239 }
26240 else
26241 vim_free(p);
26242 }
26243 }
26244# endif
26245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026246 /* Append a path separator to a directory. */
26247 if (mch_isdir(*fnamep))
26248 {
26249 /* Make room for one or two extra characters. */
26250 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26251 vim_free(*bufp); /* free any allocated file name */
26252 *bufp = *fnamep;
26253 if (*fnamep == NULL)
26254 return -1;
26255 add_pathsep(*fnamep);
26256 }
26257 }
26258
26259 /* ":." - path relative to the current directory */
26260 /* ":~" - path relative to the home directory */
26261 /* ":8" - shortname path - postponed till after */
26262 while (src[*usedlen] == ':'
26263 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26264 {
26265 *usedlen += 2;
26266 if (c == '8')
26267 {
26268#ifdef WIN3264
26269 has_shortname = 1; /* Postpone this. */
26270#endif
26271 continue;
26272 }
26273 pbuf = NULL;
26274 /* Need full path first (use expand_env() to remove a "~/") */
26275 if (!has_fullname)
26276 {
26277 if (c == '.' && **fnamep == '~')
26278 p = pbuf = expand_env_save(*fnamep);
26279 else
26280 p = pbuf = FullName_save(*fnamep, FALSE);
26281 }
26282 else
26283 p = *fnamep;
26284
26285 has_fullname = 0;
26286
26287 if (p != NULL)
26288 {
26289 if (c == '.')
26290 {
26291 mch_dirname(dirname, MAXPATHL);
26292 s = shorten_fname(p, dirname);
26293 if (s != NULL)
26294 {
26295 *fnamep = s;
26296 if (pbuf != NULL)
26297 {
26298 vim_free(*bufp); /* free any allocated file name */
26299 *bufp = pbuf;
26300 pbuf = NULL;
26301 }
26302 }
26303 }
26304 else
26305 {
26306 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26307 /* Only replace it when it starts with '~' */
26308 if (*dirname == '~')
26309 {
26310 s = vim_strsave(dirname);
26311 if (s != NULL)
26312 {
26313 *fnamep = s;
26314 vim_free(*bufp);
26315 *bufp = s;
26316 }
26317 }
26318 }
26319 vim_free(pbuf);
26320 }
26321 }
26322
26323 tail = gettail(*fnamep);
26324 *fnamelen = (int)STRLEN(*fnamep);
26325
26326 /* ":h" - head, remove "/file_name", can be repeated */
26327 /* Don't remove the first "/" or "c:\" */
26328 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26329 {
26330 valid |= VALID_HEAD;
26331 *usedlen += 2;
26332 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026333 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026334 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026335 *fnamelen = (int)(tail - *fnamep);
26336#ifdef VMS
26337 if (*fnamelen > 0)
26338 *fnamelen += 1; /* the path separator is part of the path */
26339#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026340 if (*fnamelen == 0)
26341 {
26342 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26343 p = vim_strsave((char_u *)".");
26344 if (p == NULL)
26345 return -1;
26346 vim_free(*bufp);
26347 *bufp = *fnamep = tail = p;
26348 *fnamelen = 1;
26349 }
26350 else
26351 {
26352 while (tail > s && !after_pathsep(s, tail))
26353 mb_ptr_back(*fnamep, tail);
26354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026355 }
26356
26357 /* ":8" - shortname */
26358 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26359 {
26360 *usedlen += 2;
26361#ifdef WIN3264
26362 has_shortname = 1;
26363#endif
26364 }
26365
26366#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026367 /*
26368 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026369 */
26370 if (has_shortname)
26371 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026372 /* Copy the string if it is shortened by :h and when it wasn't copied
26373 * yet, because we are going to change it in place. Avoids changing
26374 * the buffer name for "%:8". */
26375 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026376 {
26377 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026378 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026379 return -1;
26380 vim_free(*bufp);
26381 *bufp = *fnamep = p;
26382 }
26383
26384 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026385 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026386 if (!has_fullname && !vim_isAbsName(*fnamep))
26387 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026388 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026389 return -1;
26390 }
26391 else
26392 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026393 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026394
Bram Moolenaardc935552011-08-17 15:23:23 +020026395 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026396 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026397 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026398 return -1;
26399
26400 if (l == 0)
26401 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026402 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026403 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026404 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026405 return -1;
26406 }
26407 *fnamelen = l;
26408 }
26409 }
26410#endif /* WIN3264 */
26411
26412 /* ":t" - tail, just the basename */
26413 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26414 {
26415 *usedlen += 2;
26416 *fnamelen -= (int)(tail - *fnamep);
26417 *fnamep = tail;
26418 }
26419
26420 /* ":e" - extension, can be repeated */
26421 /* ":r" - root, without extension, can be repeated */
26422 while (src[*usedlen] == ':'
26423 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26424 {
26425 /* find a '.' in the tail:
26426 * - for second :e: before the current fname
26427 * - otherwise: The last '.'
26428 */
26429 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26430 s = *fnamep - 2;
26431 else
26432 s = *fnamep + *fnamelen - 1;
26433 for ( ; s > tail; --s)
26434 if (s[0] == '.')
26435 break;
26436 if (src[*usedlen + 1] == 'e') /* :e */
26437 {
26438 if (s > tail)
26439 {
26440 *fnamelen += (int)(*fnamep - (s + 1));
26441 *fnamep = s + 1;
26442#ifdef VMS
26443 /* cut version from the extension */
26444 s = *fnamep + *fnamelen - 1;
26445 for ( ; s > *fnamep; --s)
26446 if (s[0] == ';')
26447 break;
26448 if (s > *fnamep)
26449 *fnamelen = s - *fnamep;
26450#endif
26451 }
26452 else if (*fnamep <= tail)
26453 *fnamelen = 0;
26454 }
26455 else /* :r */
26456 {
26457 if (s > tail) /* remove one extension */
26458 *fnamelen = (int)(s - *fnamep);
26459 }
26460 *usedlen += 2;
26461 }
26462
26463 /* ":s?pat?foo?" - substitute */
26464 /* ":gs?pat?foo?" - global substitute */
26465 if (src[*usedlen] == ':'
26466 && (src[*usedlen + 1] == 's'
26467 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26468 {
26469 char_u *str;
26470 char_u *pat;
26471 char_u *sub;
26472 int sep;
26473 char_u *flags;
26474 int didit = FALSE;
26475
26476 flags = (char_u *)"";
26477 s = src + *usedlen + 2;
26478 if (src[*usedlen + 1] == 'g')
26479 {
26480 flags = (char_u *)"g";
26481 ++s;
26482 }
26483
26484 sep = *s++;
26485 if (sep)
26486 {
26487 /* find end of pattern */
26488 p = vim_strchr(s, sep);
26489 if (p != NULL)
26490 {
26491 pat = vim_strnsave(s, (int)(p - s));
26492 if (pat != NULL)
26493 {
26494 s = p + 1;
26495 /* find end of substitution */
26496 p = vim_strchr(s, sep);
26497 if (p != NULL)
26498 {
26499 sub = vim_strnsave(s, (int)(p - s));
26500 str = vim_strnsave(*fnamep, *fnamelen);
26501 if (sub != NULL && str != NULL)
26502 {
26503 *usedlen = (int)(p + 1 - src);
26504 s = do_string_sub(str, pat, sub, flags);
26505 if (s != NULL)
26506 {
26507 *fnamep = s;
26508 *fnamelen = (int)STRLEN(s);
26509 vim_free(*bufp);
26510 *bufp = s;
26511 didit = TRUE;
26512 }
26513 }
26514 vim_free(sub);
26515 vim_free(str);
26516 }
26517 vim_free(pat);
26518 }
26519 }
26520 /* after using ":s", repeat all the modifiers */
26521 if (didit)
26522 goto repeat;
26523 }
26524 }
26525
Bram Moolenaar26df0922014-02-23 23:39:13 +010026526 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26527 {
26528 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26529 if (p == NULL)
26530 return -1;
26531 vim_free(*bufp);
26532 *bufp = *fnamep = p;
26533 *fnamelen = (int)STRLEN(p);
26534 *usedlen += 2;
26535 }
26536
Bram Moolenaar071d4272004-06-13 20:20:40 +000026537 return valid;
26538}
26539
26540/*
26541 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26542 * "flags" can be "g" to do a global substitute.
26543 * Returns an allocated string, NULL for error.
26544 */
26545 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026546do_string_sub(
26547 char_u *str,
26548 char_u *pat,
26549 char_u *sub,
26550 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026551{
26552 int sublen;
26553 regmatch_T regmatch;
26554 int i;
26555 int do_all;
26556 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026557 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026558 garray_T ga;
26559 char_u *ret;
26560 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026561 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562
26563 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26564 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026565 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026566
26567 ga_init2(&ga, 1, 200);
26568
26569 do_all = (flags[0] == 'g');
26570
26571 regmatch.rm_ic = p_ic;
26572 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26573 if (regmatch.regprog != NULL)
26574 {
26575 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026576 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026577 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26578 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026579 /* Skip empty match except for first match. */
26580 if (regmatch.startp[0] == regmatch.endp[0])
26581 {
26582 if (zero_width == regmatch.startp[0])
26583 {
26584 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026585 i = MB_PTR2LEN(tail);
26586 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26587 (size_t)i);
26588 ga.ga_len += i;
26589 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026590 continue;
26591 }
26592 zero_width = regmatch.startp[0];
26593 }
26594
Bram Moolenaar071d4272004-06-13 20:20:40 +000026595 /*
26596 * Get some space for a temporary buffer to do the substitution
26597 * into. It will contain:
26598 * - The text up to where the match is.
26599 * - The substituted text.
26600 * - The text after the match.
26601 */
26602 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026603 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026604 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26605 {
26606 ga_clear(&ga);
26607 break;
26608 }
26609
26610 /* copy the text up to where the match is */
26611 i = (int)(regmatch.startp[0] - tail);
26612 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26613 /* add the substituted text */
26614 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26615 + ga.ga_len + i, TRUE, TRUE, FALSE);
26616 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026617 tail = regmatch.endp[0];
26618 if (*tail == NUL)
26619 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026620 if (!do_all)
26621 break;
26622 }
26623
26624 if (ga.ga_data != NULL)
26625 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26626
Bram Moolenaar473de612013-06-08 18:19:48 +020026627 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026628 }
26629
26630 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26631 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026632 if (p_cpo == empty_option)
26633 p_cpo = save_cpo;
26634 else
26635 /* Darn, evaluating {sub} expression changed the value. */
26636 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026637
26638 return ret;
26639}
26640
26641#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */