blob: bb9a4c5694985fb8e3b66c4f66e67e2407b9da1b [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);
10138 if (opt->jo_ecb_buf == NULL)
10139 {
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 Moolenaar071d4272004-06-13 20:20:40 +000013788#ifdef FEAT_PATH_EXTRA
13789 "path_extra",
13790#endif
13791#ifdef FEAT_PERL
13792#ifndef DYNAMIC_PERL
13793 "perl",
13794#endif
13795#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013796#ifdef FEAT_PERSISTENT_UNDO
13797 "persistent_undo",
13798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799#ifdef FEAT_PYTHON
13800#ifndef DYNAMIC_PYTHON
13801 "python",
13802#endif
13803#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013804#ifdef FEAT_PYTHON3
13805#ifndef DYNAMIC_PYTHON3
13806 "python3",
13807#endif
13808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809#ifdef FEAT_POSTSCRIPT
13810 "postscript",
13811#endif
13812#ifdef FEAT_PRINTER
13813 "printer",
13814#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013815#ifdef FEAT_PROFILE
13816 "profile",
13817#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013818#ifdef FEAT_RELTIME
13819 "reltime",
13820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821#ifdef FEAT_QUICKFIX
13822 "quickfix",
13823#endif
13824#ifdef FEAT_RIGHTLEFT
13825 "rightleft",
13826#endif
13827#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13828 "ruby",
13829#endif
13830#ifdef FEAT_SCROLLBIND
13831 "scrollbind",
13832#endif
13833#ifdef FEAT_CMDL_INFO
13834 "showcmd",
13835 "cmdline_info",
13836#endif
13837#ifdef FEAT_SIGNS
13838 "signs",
13839#endif
13840#ifdef FEAT_SMARTINDENT
13841 "smartindent",
13842#endif
13843#ifdef FEAT_SNIFF
13844 "sniff",
13845#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013846#ifdef STARTUPTIME
13847 "startuptime",
13848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849#ifdef FEAT_STL_OPT
13850 "statusline",
13851#endif
13852#ifdef FEAT_SUN_WORKSHOP
13853 "sun_workshop",
13854#endif
13855#ifdef FEAT_NETBEANS_INTG
13856 "netbeans_intg",
13857#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013858#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013859 "spell",
13860#endif
13861#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 "syntax",
13863#endif
13864#if defined(USE_SYSTEM) || !defined(UNIX)
13865 "system",
13866#endif
13867#ifdef FEAT_TAG_BINS
13868 "tag_binary",
13869#endif
13870#ifdef FEAT_TAG_OLDSTATIC
13871 "tag_old_static",
13872#endif
13873#ifdef FEAT_TAG_ANYWHITE
13874 "tag_any_white",
13875#endif
13876#ifdef FEAT_TCL
13877# ifndef DYNAMIC_TCL
13878 "tcl",
13879# endif
13880#endif
13881#ifdef TERMINFO
13882 "terminfo",
13883#endif
13884#ifdef FEAT_TERMRESPONSE
13885 "termresponse",
13886#endif
13887#ifdef FEAT_TEXTOBJ
13888 "textobjects",
13889#endif
13890#ifdef HAVE_TGETENT
13891 "tgetent",
13892#endif
13893#ifdef FEAT_TITLE
13894 "title",
13895#endif
13896#ifdef FEAT_TOOLBAR
13897 "toolbar",
13898#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013899#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13900 "unnamedplus",
13901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902#ifdef FEAT_USR_CMDS
13903 "user-commands", /* was accidentally included in 5.4 */
13904 "user_commands",
13905#endif
13906#ifdef FEAT_VIMINFO
13907 "viminfo",
13908#endif
13909#ifdef FEAT_VERTSPLIT
13910 "vertsplit",
13911#endif
13912#ifdef FEAT_VIRTUALEDIT
13913 "virtualedit",
13914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916#ifdef FEAT_VISUALEXTRA
13917 "visualextra",
13918#endif
13919#ifdef FEAT_VREPLACE
13920 "vreplace",
13921#endif
13922#ifdef FEAT_WILDIGN
13923 "wildignore",
13924#endif
13925#ifdef FEAT_WILDMENU
13926 "wildmenu",
13927#endif
13928#ifdef FEAT_WINDOWS
13929 "windows",
13930#endif
13931#ifdef FEAT_WAK
13932 "winaltkeys",
13933#endif
13934#ifdef FEAT_WRITEBACKUP
13935 "writebackup",
13936#endif
13937#ifdef FEAT_XIM
13938 "xim",
13939#endif
13940#ifdef FEAT_XFONTSET
13941 "xfontset",
13942#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013943#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013944 "xpm",
13945 "xpm_w32", /* for backward compatibility */
13946#else
13947# if defined(HAVE_XPM)
13948 "xpm",
13949# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951#ifdef USE_XSMP
13952 "xsmp",
13953#endif
13954#ifdef USE_XSMP_INTERACT
13955 "xsmp_interact",
13956#endif
13957#ifdef FEAT_XCLIPBOARD
13958 "xterm_clipboard",
13959#endif
13960#ifdef FEAT_XTERM_SAVE
13961 "xterm_save",
13962#endif
13963#if defined(UNIX) && defined(FEAT_X11)
13964 "X11",
13965#endif
13966 NULL
13967 };
13968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013969 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 for (i = 0; has_list[i] != NULL; ++i)
13971 if (STRICMP(name, has_list[i]) == 0)
13972 {
13973 n = TRUE;
13974 break;
13975 }
13976
13977 if (n == FALSE)
13978 {
13979 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013980 {
13981 if (name[5] == '-'
13982 && STRLEN(name) > 11
13983 && vim_isdigit(name[6])
13984 && vim_isdigit(name[8])
13985 && vim_isdigit(name[10]))
13986 {
13987 int major = atoi((char *)name + 6);
13988 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013989
13990 /* Expect "patch-9.9.01234". */
13991 n = (major < VIM_VERSION_MAJOR
13992 || (major == VIM_VERSION_MAJOR
13993 && (minor < VIM_VERSION_MINOR
13994 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013995 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013996 }
13997 else
13998 n = has_patch(atoi((char *)name + 5));
13999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 else if (STRICMP(name, "vim_starting") == 0)
14001 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014002#ifdef FEAT_MBYTE
14003 else if (STRICMP(name, "multi_byte_encoding") == 0)
14004 n = has_mbyte;
14005#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014006#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14007 else if (STRICMP(name, "balloon_multiline") == 0)
14008 n = multiline_balloon_available();
14009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010#ifdef DYNAMIC_TCL
14011 else if (STRICMP(name, "tcl") == 0)
14012 n = tcl_enabled(FALSE);
14013#endif
14014#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14015 else if (STRICMP(name, "iconv") == 0)
14016 n = iconv_enabled(FALSE);
14017#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014018#ifdef DYNAMIC_LUA
14019 else if (STRICMP(name, "lua") == 0)
14020 n = lua_enabled(FALSE);
14021#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014022#ifdef DYNAMIC_MZSCHEME
14023 else if (STRICMP(name, "mzscheme") == 0)
14024 n = mzscheme_enabled(FALSE);
14025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026#ifdef DYNAMIC_RUBY
14027 else if (STRICMP(name, "ruby") == 0)
14028 n = ruby_enabled(FALSE);
14029#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014030#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031#ifdef DYNAMIC_PYTHON
14032 else if (STRICMP(name, "python") == 0)
14033 n = python_enabled(FALSE);
14034#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014035#endif
14036#ifdef FEAT_PYTHON3
14037#ifdef DYNAMIC_PYTHON3
14038 else if (STRICMP(name, "python3") == 0)
14039 n = python3_enabled(FALSE);
14040#endif
14041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042#ifdef DYNAMIC_PERL
14043 else if (STRICMP(name, "perl") == 0)
14044 n = perl_enabled(FALSE);
14045#endif
14046#ifdef FEAT_GUI
14047 else if (STRICMP(name, "gui_running") == 0)
14048 n = (gui.in_use || gui.starting);
14049# ifdef FEAT_GUI_W32
14050 else if (STRICMP(name, "gui_win32s") == 0)
14051 n = gui_is_win32s();
14052# endif
14053# ifdef FEAT_BROWSE
14054 else if (STRICMP(name, "browse") == 0)
14055 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14056# endif
14057#endif
14058#ifdef FEAT_SYN_HL
14059 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014060 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061#endif
14062#if defined(WIN3264)
14063 else if (STRICMP(name, "win95") == 0)
14064 n = mch_windows95();
14065#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014066#ifdef FEAT_NETBEANS_INTG
14067 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014068 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 }
14071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014072 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073}
14074
14075/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014076 * "has_key()" function
14077 */
14078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014079f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014080{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014081 if (argvars[0].v_type != VAR_DICT)
14082 {
14083 EMSG(_(e_dictreq));
14084 return;
14085 }
14086 if (argvars[0].vval.v_dict == NULL)
14087 return;
14088
14089 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014090 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014091}
14092
14093/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014094 * "haslocaldir()" function
14095 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014097f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014098{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014099 win_T *wp = NULL;
14100
14101 wp = find_tabwin(&argvars[0], &argvars[1]);
14102 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014103}
14104
14105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 * "hasmapto()" function
14107 */
14108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014109f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110{
14111 char_u *name;
14112 char_u *mode;
14113 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014114 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014116 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014117 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 mode = (char_u *)"nvo";
14119 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014121 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014122 if (argvars[2].v_type != VAR_UNKNOWN)
14123 abbr = get_tv_number(&argvars[2]);
14124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125
Bram Moolenaar2c932302006-03-18 21:42:09 +000014126 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014127 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130}
14131
14132/*
14133 * "histadd()" function
14134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014136f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137{
14138#ifdef FEAT_CMDHIST
14139 int histype;
14140 char_u *str;
14141 char_u buf[NUMBUFLEN];
14142#endif
14143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014144 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145 if (check_restricted() || check_secure())
14146 return;
14147#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14149 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 if (histype >= 0)
14151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 if (*str != NUL)
14154 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014155 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 return;
14159 }
14160 }
14161#endif
14162}
14163
14164/*
14165 * "histdel()" function
14166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014168f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169{
14170#ifdef FEAT_CMDHIST
14171 int n;
14172 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14176 if (str == NULL)
14177 n = 0;
14178 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014181 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014184 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185 else
14186 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014188 get_tv_string_buf(&argvars[1], buf));
14189 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190#endif
14191}
14192
14193/*
14194 * "histget()" function
14195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014197f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198{
14199#ifdef FEAT_CMDHIST
14200 int type;
14201 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014202 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14205 if (str == NULL)
14206 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208 {
14209 type = get_histtype(str);
14210 if (argvars[1].v_type == VAR_UNKNOWN)
14211 idx = get_history_idx(type);
14212 else
14213 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14214 /* -1 on type error */
14215 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014218 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014220 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221}
14222
14223/*
14224 * "histnr()" function
14225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014227f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228{
14229 int i;
14230
14231#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014232 char_u *history = get_tv_string_chk(&argvars[0]);
14233
14234 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 if (i >= HIST_CMD && i < HIST_COUNT)
14236 i = get_history_idx(i);
14237 else
14238#endif
14239 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014240 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241}
14242
14243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 * "highlightID(name)" function
14245 */
14246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014247f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014249 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250}
14251
14252/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014253 * "highlight_exists()" function
14254 */
14255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014256f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014257{
14258 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14259}
14260
14261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262 * "hostname()" function
14263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014265f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266{
14267 char_u hostname[256];
14268
14269 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014270 rettv->v_type = VAR_STRING;
14271 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272}
14273
14274/*
14275 * iconv() function
14276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014278f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279{
14280#ifdef FEAT_MBYTE
14281 char_u buf1[NUMBUFLEN];
14282 char_u buf2[NUMBUFLEN];
14283 char_u *from, *to, *str;
14284 vimconv_T vimconv;
14285#endif
14286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014287 rettv->v_type = VAR_STRING;
14288 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289
14290#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014291 str = get_tv_string(&argvars[0]);
14292 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14293 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 vimconv.vc_type = CONV_NONE;
14295 convert_setup(&vimconv, from, to);
14296
14297 /* If the encodings are equal, no conversion needed. */
14298 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014299 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014301 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302
14303 convert_setup(&vimconv, NULL, NULL);
14304 vim_free(from);
14305 vim_free(to);
14306#endif
14307}
14308
14309/*
14310 * "indent()" function
14311 */
14312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014313f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314{
14315 linenr_T lnum;
14316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014319 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014321 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322}
14323
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014324/*
14325 * "index()" function
14326 */
14327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014328f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014329{
Bram Moolenaar33570922005-01-25 22:26:29 +000014330 list_T *l;
14331 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014332 long idx = 0;
14333 int ic = FALSE;
14334
14335 rettv->vval.v_number = -1;
14336 if (argvars[0].v_type != VAR_LIST)
14337 {
14338 EMSG(_(e_listreq));
14339 return;
14340 }
14341 l = argvars[0].vval.v_list;
14342 if (l != NULL)
14343 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014344 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014345 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014347 int error = FALSE;
14348
Bram Moolenaar758711c2005-02-02 23:11:38 +000014349 /* Start at specified item. Use the cached index that list_find()
14350 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014351 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014352 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014353 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 ic = get_tv_number_chk(&argvars[3], &error);
14355 if (error)
14356 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014357 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014358
Bram Moolenaar758711c2005-02-02 23:11:38 +000014359 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014360 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014361 {
14362 rettv->vval.v_number = idx;
14363 break;
14364 }
14365 }
14366}
14367
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368static int inputsecret_flag = 0;
14369
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014370static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014371
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014373 * This function is used by f_input() and f_inputdialog() functions. The third
14374 * argument to f_input() specifies the type of completion to use at the
14375 * prompt. The third argument to f_inputdialog() specifies the value to return
14376 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377 */
14378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014379get_user_input(
14380 typval_T *argvars,
14381 typval_T *rettv,
14382 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014384 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 char_u *p = NULL;
14386 int c;
14387 char_u buf[NUMBUFLEN];
14388 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014390 int xp_type = EXPAND_NOTHING;
14391 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014393 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014394 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395
14396#ifdef NO_CONSOLE_INPUT
14397 /* While starting up, there is no place to enter text. */
14398 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400#endif
14401
14402 cmd_silent = FALSE; /* Want to see the prompt. */
14403 if (prompt != NULL)
14404 {
14405 /* Only the part of the message after the last NL is considered as
14406 * prompt for the command line */
14407 p = vim_strrchr(prompt, '\n');
14408 if (p == NULL)
14409 p = prompt;
14410 else
14411 {
14412 ++p;
14413 c = *p;
14414 *p = NUL;
14415 msg_start();
14416 msg_clr_eos();
14417 msg_puts_attr(prompt, echo_attr);
14418 msg_didout = FALSE;
14419 msg_starthere();
14420 *p = c;
14421 }
14422 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424 if (argvars[1].v_type != VAR_UNKNOWN)
14425 {
14426 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14427 if (defstr != NULL)
14428 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014430 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014431 {
14432 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014433 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014434 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014435
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014436 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014437 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014438
Bram Moolenaar4463f292005-09-25 22:20:24 +000014439 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14440 if (xp_name == NULL)
14441 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014442
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014443 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014444
Bram Moolenaar4463f292005-09-25 22:20:24 +000014445 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14446 &xp_arg) == FAIL)
14447 return;
14448 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014449 }
14450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014452 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014453 int save_ex_normal_busy = ex_normal_busy;
14454 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014455 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014456 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14457 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014458 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014459 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014460 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014461 && argvars[1].v_type != VAR_UNKNOWN
14462 && argvars[2].v_type != VAR_UNKNOWN)
14463 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14464 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014465
14466 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 /* since the user typed this, no need to wait for return */
14469 need_wait_return = FALSE;
14470 msg_didout = FALSE;
14471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 cmd_silent = cmd_silent_save;
14473}
14474
14475/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014476 * "input()" function
14477 * Also handles inputsecret() when inputsecret is set.
14478 */
14479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014480f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014481{
14482 get_user_input(argvars, rettv, FALSE);
14483}
14484
14485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 * "inputdialog()" function
14487 */
14488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014489f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490{
14491#if defined(FEAT_GUI_TEXTDIALOG)
14492 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14493 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14494 {
14495 char_u *message;
14496 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014499 message = get_tv_string_chk(&argvars[0]);
14500 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014501 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014502 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503 else
14504 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014505 if (message != NULL && defstr != NULL
14506 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014507 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014508 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 else
14510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014511 if (message != NULL && defstr != NULL
14512 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014513 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514 rettv->vval.v_string = vim_strsave(
14515 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014519 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 }
14521 else
14522#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014523 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524}
14525
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014526/*
14527 * "inputlist()" function
14528 */
14529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014530f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014531{
14532 listitem_T *li;
14533 int selected;
14534 int mouse_used;
14535
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014536#ifdef NO_CONSOLE_INPUT
14537 /* While starting up, there is no place to enter text. */
14538 if (no_console_input())
14539 return;
14540#endif
14541 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14542 {
14543 EMSG2(_(e_listarg), "inputlist()");
14544 return;
14545 }
14546
14547 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014548 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014549 lines_left = Rows; /* avoid more prompt */
14550 msg_scroll = TRUE;
14551 msg_clr_eos();
14552
14553 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14554 {
14555 msg_puts(get_tv_string(&li->li_tv));
14556 msg_putchar('\n');
14557 }
14558
14559 /* Ask for choice. */
14560 selected = prompt_for_number(&mouse_used);
14561 if (mouse_used)
14562 selected -= lines_left;
14563
14564 rettv->vval.v_number = selected;
14565}
14566
14567
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14569
14570/*
14571 * "inputrestore()" function
14572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014574f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014575{
14576 if (ga_userinput.ga_len > 0)
14577 {
14578 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14580 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014581 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 }
14583 else if (p_verbose > 1)
14584 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014585 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014586 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 }
14588}
14589
14590/*
14591 * "inputsave()" function
14592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014594f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014596 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597 if (ga_grow(&ga_userinput, 1) == OK)
14598 {
14599 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14600 + ga_userinput.ga_len);
14601 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014602 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 }
14604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014605 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606}
14607
14608/*
14609 * "inputsecret()" function
14610 */
14611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014612f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613{
14614 ++cmdline_star;
14615 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014616 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 --cmdline_star;
14618 --inputsecret_flag;
14619}
14620
14621/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014622 * "insert()" function
14623 */
14624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014625f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014626{
14627 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014628 listitem_T *item;
14629 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014631
14632 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014633 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014634 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014635 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014636 {
14637 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014638 before = get_tv_number_chk(&argvars[2], &error);
14639 if (error)
14640 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014641
Bram Moolenaar758711c2005-02-02 23:11:38 +000014642 if (before == l->lv_len)
14643 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014644 else
14645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014646 item = list_find(l, before);
14647 if (item == NULL)
14648 {
14649 EMSGN(_(e_listidx), before);
14650 l = NULL;
14651 }
14652 }
14653 if (l != NULL)
14654 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014655 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014656 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014657 }
14658 }
14659}
14660
14661/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014662 * "invert(expr)" function
14663 */
14664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014665f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014666{
14667 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14668}
14669
14670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671 * "isdirectory()" function
14672 */
14673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014674f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677}
14678
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014679/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014680 * "islocked()" function
14681 */
14682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014683f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014684{
14685 lval_T lv;
14686 char_u *end;
14687 dictitem_T *di;
14688
14689 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014690 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14691 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014692 if (end != NULL && lv.ll_name != NULL)
14693 {
14694 if (*end != NUL)
14695 EMSG(_(e_trailing));
14696 else
14697 {
14698 if (lv.ll_tv == NULL)
14699 {
14700 if (check_changedtick(lv.ll_name))
14701 rettv->vval.v_number = 1; /* always locked */
14702 else
14703 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014704 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014705 if (di != NULL)
14706 {
14707 /* Consider a variable locked when:
14708 * 1. the variable itself is locked
14709 * 2. the value of the variable is locked.
14710 * 3. the List or Dict value is locked.
14711 */
14712 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14713 || tv_islocked(&di->di_tv));
14714 }
14715 }
14716 }
14717 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014718 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014719 else if (lv.ll_newkey != NULL)
14720 EMSG2(_(e_dictkey), lv.ll_newkey);
14721 else if (lv.ll_list != NULL)
14722 /* List item. */
14723 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14724 else
14725 /* Dictionary item. */
14726 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14727 }
14728 }
14729
14730 clear_lval(&lv);
14731}
14732
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014733static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014734
14735/*
14736 * Turn a dict into a list:
14737 * "what" == 0: list of keys
14738 * "what" == 1: list of values
14739 * "what" == 2: list of items
14740 */
14741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014742dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014743{
Bram Moolenaar33570922005-01-25 22:26:29 +000014744 list_T *l2;
14745 dictitem_T *di;
14746 hashitem_T *hi;
14747 listitem_T *li;
14748 listitem_T *li2;
14749 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014750 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014751
Bram Moolenaar8c711452005-01-14 21:53:12 +000014752 if (argvars[0].v_type != VAR_DICT)
14753 {
14754 EMSG(_(e_dictreq));
14755 return;
14756 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014757 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014758 return;
14759
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014760 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014761 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014762
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014763 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014764 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014765 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014766 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014767 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014768 --todo;
14769 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014770
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014771 li = listitem_alloc();
14772 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014773 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014774 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014775
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014776 if (what == 0)
14777 {
14778 /* keys() */
14779 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014780 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014781 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14782 }
14783 else if (what == 1)
14784 {
14785 /* values() */
14786 copy_tv(&di->di_tv, &li->li_tv);
14787 }
14788 else
14789 {
14790 /* items() */
14791 l2 = list_alloc();
14792 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014793 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014794 li->li_tv.vval.v_list = l2;
14795 if (l2 == NULL)
14796 break;
14797 ++l2->lv_refcount;
14798
14799 li2 = listitem_alloc();
14800 if (li2 == NULL)
14801 break;
14802 list_append(l2, li2);
14803 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014804 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014805 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14806
14807 li2 = listitem_alloc();
14808 if (li2 == NULL)
14809 break;
14810 list_append(l2, li2);
14811 copy_tv(&di->di_tv, &li2->li_tv);
14812 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014813 }
14814 }
14815}
14816
14817/*
14818 * "items(dict)" function
14819 */
14820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014821f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014822{
14823 dict_list(argvars, rettv, 2);
14824}
14825
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014826#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014827/*
14828 * Get the job from the argument.
14829 * Returns NULL if the job is invalid.
14830 */
14831 static job_T *
14832get_job_arg(typval_T *tv)
14833{
14834 job_T *job;
14835
14836 if (tv->v_type != VAR_JOB)
14837 {
14838 EMSG2(_(e_invarg2), get_tv_string(tv));
14839 return NULL;
14840 }
14841 job = tv->vval.v_job;
14842
14843 if (job == NULL)
14844 EMSG(_("E916: not a valid job"));
14845 return job;
14846}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014847
14848# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014849/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014850 * "job_getchannel()" function
14851 */
14852 static void
14853f_job_getchannel(typval_T *argvars, typval_T *rettv)
14854{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014855 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014856
Bram Moolenaar65edff82016-02-21 16:40:11 +010014857 if (job != NULL)
14858 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014859 rettv->v_type = VAR_CHANNEL;
14860 rettv->vval.v_channel = job->jv_channel;
14861 if (job->jv_channel != NULL)
14862 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014863 }
14864}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014865# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014866
14867/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014868 * "job_setoptions()" function
14869 */
14870 static void
14871f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14872{
14873 job_T *job = get_job_arg(&argvars[0]);
14874 jobopt_T opt;
14875
14876 if (job == NULL)
14877 return;
14878 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014879 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014880 return;
14881 job_set_options(job, &opt);
14882}
14883
14884/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014885 * "job_start()" function
14886 */
14887 static void
14888f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14889{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014890 job_T *job;
14891 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014892#if defined(UNIX)
14893# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014894 char **argv = NULL;
14895 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014896#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014897 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014898#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014899 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014900
14901 rettv->v_type = VAR_JOB;
14902 job = job_alloc();
14903 rettv->vval.v_job = job;
14904 if (job == NULL)
14905 return;
14906
14907 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014908
14909 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014910 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014911 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014912 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014913 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
14914 + JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014915 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010014916 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014917
Bram Moolenaar835dc632016-02-07 14:27:38 +010014918#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014919 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014920#endif
14921
14922 if (argvars[0].v_type == VAR_STRING)
14923 {
14924 /* Command is a string. */
14925 cmd = argvars[0].vval.v_string;
14926#ifdef USE_ARGV
14927 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14928 return;
14929 argv[argc] = NULL;
14930#endif
14931 }
14932 else if (argvars[0].v_type != VAR_LIST
14933 || argvars[0].vval.v_list == NULL
14934 || argvars[0].vval.v_list->lv_len < 1)
14935 {
14936 EMSG(_(e_invarg));
14937 return;
14938 }
14939 else
14940 {
14941 list_T *l = argvars[0].vval.v_list;
14942 listitem_T *li;
14943 char_u *s;
14944
14945#ifdef USE_ARGV
14946 /* Pass argv[] to mch_call_shell(). */
14947 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14948 if (argv == NULL)
14949 return;
14950#endif
14951 for (li = l->lv_first; li != NULL; li = li->li_next)
14952 {
14953 s = get_tv_string_chk(&li->li_tv);
14954 if (s == NULL)
14955 goto theend;
14956#ifdef USE_ARGV
14957 argv[argc++] = (char *)s;
14958#else
14959 if (li != l->lv_first)
14960 {
14961 s = vim_strsave_shellescape(s, FALSE, TRUE);
14962 if (s == NULL)
14963 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014964 ga_concat(&ga, s);
14965 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014966 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014967 else
14968 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014969 if (li->li_next != NULL)
14970 ga_append(&ga, ' ');
14971#endif
14972 }
14973#ifdef USE_ARGV
14974 argv[argc] = NULL;
14975#else
14976 cmd = ga.ga_data;
14977#endif
14978 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014979
Bram Moolenaar835dc632016-02-07 14:27:38 +010014980#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014981# ifdef FEAT_CHANNEL
14982 if (ch_log_active())
14983 {
14984 garray_T ga;
14985 int i;
14986
14987 ga_init2(&ga, (int)sizeof(char), 200);
14988 for (i = 0; i < argc; ++i)
14989 {
14990 if (i > 0)
14991 ga_concat(&ga, (char_u *)" ");
14992 ga_concat(&ga, (char_u *)argv[i]);
14993 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010014994 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014995 ga_clear(&ga);
14996 }
14997# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014998 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014999#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015000# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015001 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015002# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015003 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015004#endif
15005
15006theend:
15007#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015008 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015009#else
15010 vim_free(ga.ga_data);
15011#endif
15012}
15013
15014/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015015 * Get the status of "job" and invoke the exit callback when needed.
15016 * The returned string is not allocated.
15017 */
15018 static char *
15019job_status(job_T *job)
15020{
15021 char *result;
15022
15023 if (job->jv_status == JOB_ENDED)
15024 /* No need to check, dead is dead. */
15025 result = "dead";
15026 else if (job->jv_status == JOB_FAILED)
15027 result = "fail";
15028 else
15029 {
15030 result = mch_job_status(job);
15031# ifdef FEAT_CHANNEL
15032 if (job->jv_status == JOB_ENDED)
15033 ch_log(job->jv_channel, "Job ended");
15034# endif
15035 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15036 {
15037 typval_T argv[3];
15038 typval_T rettv;
15039 int dummy;
15040
15041 /* invoke the exit callback */
15042 argv[0].v_type = VAR_JOB;
15043 argv[0].vval.v_job = job;
15044 argv[1].v_type = VAR_NUMBER;
15045 argv[1].vval.v_number = job->jv_exitval;
15046 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15047 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15048 clear_tv(&rettv);
15049 }
15050 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15051 {
15052 /* The job already was unreferenced, now that it ended it can be
15053 * freed. Careful: caller must not use "job" after this! */
15054 job_free(job);
15055 }
15056 }
15057 return result;
15058}
15059
15060/*
15061 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15062 */
15063 void
15064job_check_ended()
15065{
15066 static time_t last_check = 0;
15067 time_t now;
15068 job_T *job;
15069 job_T *next;
15070
15071 /* Only do this once in 10 seconds. */
15072 now = time(NULL);
15073 if (last_check + 10 < now)
15074 {
15075 last_check = now;
15076 for (job = first_job; job != NULL; job = next)
15077 {
15078 next = job->jv_next;
15079 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15080 job_status(job); /* may free "job" */
15081 }
15082 }
15083}
15084
15085/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015086 * "job_status()" function
15087 */
15088 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015089f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015090{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015091 job_T *job = get_job_arg(&argvars[0]);
15092 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015093
Bram Moolenaar65edff82016-02-21 16:40:11 +010015094 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015095 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015096 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015097 rettv->v_type = VAR_STRING;
15098 rettv->vval.v_string = vim_strsave((char_u *)result);
15099 }
15100}
15101
15102/*
15103 * "job_stop()" function
15104 */
15105 static void
15106f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15107{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015108 job_T *job = get_job_arg(&argvars[0]);
15109
15110 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015111 {
15112 char_u *arg;
15113
15114 if (argvars[1].v_type == VAR_UNKNOWN)
15115 arg = (char_u *)"";
15116 else
15117 {
15118 arg = get_tv_string_chk(&argvars[1]);
15119 if (arg == NULL)
15120 {
15121 EMSG(_(e_invarg));
15122 return;
15123 }
15124 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015125 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015126 rettv->vval.v_number = 0;
15127 else
15128 rettv->vval.v_number = 1;
15129 }
15130}
15131#endif
15132
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015134 * "join()" function
15135 */
15136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015137f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015138{
15139 garray_T ga;
15140 char_u *sep;
15141
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015142 if (argvars[0].v_type != VAR_LIST)
15143 {
15144 EMSG(_(e_listreq));
15145 return;
15146 }
15147 if (argvars[0].vval.v_list == NULL)
15148 return;
15149 if (argvars[1].v_type == VAR_UNKNOWN)
15150 sep = (char_u *)" ";
15151 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015152 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015153
15154 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015155
15156 if (sep != NULL)
15157 {
15158 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015159 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015160 ga_append(&ga, NUL);
15161 rettv->vval.v_string = (char_u *)ga.ga_data;
15162 }
15163 else
15164 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015165}
15166
15167/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015168 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015169 */
15170 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015171f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015172{
15173 js_read_T reader;
15174
15175 reader.js_buf = get_tv_string(&argvars[0]);
15176 reader.js_fill = NULL;
15177 reader.js_used = 0;
15178 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15179 EMSG(_(e_invarg));
15180}
15181
15182/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015183 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015184 */
15185 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015186f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015187{
15188 rettv->v_type = VAR_STRING;
15189 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15190}
15191
15192/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015193 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015194 */
15195 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015196f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015197{
15198 js_read_T reader;
15199
15200 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015201 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015202 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015203 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015204 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015205}
15206
15207/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015208 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015209 */
15210 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015211f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015212{
15213 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015214 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015215}
15216
15217/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015218 * "keys()" function
15219 */
15220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015221f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015222{
15223 dict_list(argvars, rettv, 0);
15224}
15225
15226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015227 * "last_buffer_nr()" function.
15228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015230f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231{
15232 int n = 0;
15233 buf_T *buf;
15234
15235 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15236 if (n < buf->b_fnum)
15237 n = buf->b_fnum;
15238
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015239 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015240}
15241
15242/*
15243 * "len()" function
15244 */
15245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015246f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015247{
15248 switch (argvars[0].v_type)
15249 {
15250 case VAR_STRING:
15251 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015252 rettv->vval.v_number = (varnumber_T)STRLEN(
15253 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015254 break;
15255 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015256 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015257 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015258 case VAR_DICT:
15259 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15260 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015261 case VAR_UNKNOWN:
15262 case VAR_SPECIAL:
15263 case VAR_FLOAT:
15264 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015265 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015266 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015267 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015268 break;
15269 }
15270}
15271
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015272static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015273
15274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015275libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015276{
15277#ifdef FEAT_LIBCALL
15278 char_u *string_in;
15279 char_u **string_result;
15280 int nr_result;
15281#endif
15282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015283 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015284 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015285 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015286
15287 if (check_restricted() || check_secure())
15288 return;
15289
15290#ifdef FEAT_LIBCALL
15291 /* The first two args must be strings, otherwise its meaningless */
15292 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15293 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015294 string_in = NULL;
15295 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015296 string_in = argvars[2].vval.v_string;
15297 if (type == VAR_NUMBER)
15298 string_result = NULL;
15299 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015300 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015301 if (mch_libcall(argvars[0].vval.v_string,
15302 argvars[1].vval.v_string,
15303 string_in,
15304 argvars[2].vval.v_number,
15305 string_result,
15306 &nr_result) == OK
15307 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015309 }
15310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311}
15312
15313/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015314 * "libcall()" function
15315 */
15316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015317f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015318{
15319 libcall_common(argvars, rettv, VAR_STRING);
15320}
15321
15322/*
15323 * "libcallnr()" function
15324 */
15325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015326f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015327{
15328 libcall_common(argvars, rettv, VAR_NUMBER);
15329}
15330
15331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 * "line(string)" function
15333 */
15334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015335f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015336{
15337 linenr_T lnum = 0;
15338 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015339 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015341 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342 if (fp != NULL)
15343 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015344 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345}
15346
15347/*
15348 * "line2byte(lnum)" function
15349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015351f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352{
15353#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015354 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355#else
15356 linenr_T lnum;
15357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015358 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015360 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015362 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15363 if (rettv->vval.v_number >= 0)
15364 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365#endif
15366}
15367
15368/*
15369 * "lispindent(lnum)" function
15370 */
15371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015372f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373{
15374#ifdef FEAT_LISP
15375 pos_T pos;
15376 linenr_T lnum;
15377
15378 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015379 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15381 {
15382 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015383 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384 curwin->w_cursor = pos;
15385 }
15386 else
15387#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389}
15390
15391/*
15392 * "localtime()" function
15393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015395f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398}
15399
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015400static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401
15402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015403get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404{
15405 char_u *keys;
15406 char_u *which;
15407 char_u buf[NUMBUFLEN];
15408 char_u *keys_buf = NULL;
15409 char_u *rhs;
15410 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015411 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015412 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015413 mapblock_T *mp;
15414 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415
15416 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015417 rettv->v_type = VAR_STRING;
15418 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 if (*keys == NUL)
15422 return;
15423
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015424 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015426 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015427 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015428 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015429 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015430 if (argvars[3].v_type != VAR_UNKNOWN)
15431 get_dict = get_tv_number(&argvars[3]);
15432 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 else
15435 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015436 if (which == NULL)
15437 return;
15438
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 mode = get_map_mode(&which, 0);
15440
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015441 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015442 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015444
15445 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015447 /* Return a string. */
15448 if (rhs != NULL)
15449 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450
Bram Moolenaarbd743252010-10-20 21:23:33 +020015451 }
15452 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15453 {
15454 /* Return a dictionary. */
15455 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15456 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15457 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458
Bram Moolenaarbd743252010-10-20 21:23:33 +020015459 dict_add_nr_str(dict, "lhs", 0L, lhs);
15460 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15461 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15462 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15463 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15464 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15465 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015466 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015467 dict_add_nr_str(dict, "mode", 0L, mapmode);
15468
15469 vim_free(lhs);
15470 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471 }
15472}
15473
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015474#ifdef FEAT_FLOAT
15475/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015476 * "log()" function
15477 */
15478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015479f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015480{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015481 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015482
15483 rettv->v_type = VAR_FLOAT;
15484 if (get_float_arg(argvars, &f) == OK)
15485 rettv->vval.v_float = log(f);
15486 else
15487 rettv->vval.v_float = 0.0;
15488}
15489
15490/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015491 * "log10()" function
15492 */
15493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015494f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015495{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015496 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015497
15498 rettv->v_type = VAR_FLOAT;
15499 if (get_float_arg(argvars, &f) == OK)
15500 rettv->vval.v_float = log10(f);
15501 else
15502 rettv->vval.v_float = 0.0;
15503}
15504#endif
15505
Bram Moolenaar1dced572012-04-05 16:54:08 +020015506#ifdef FEAT_LUA
15507/*
15508 * "luaeval()" function
15509 */
15510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015511f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015512{
15513 char_u *str;
15514 char_u buf[NUMBUFLEN];
15515
15516 str = get_tv_string_buf(&argvars[0], buf);
15517 do_luaeval(str, argvars + 1, rettv);
15518}
15519#endif
15520
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015522 * "map()" function
15523 */
15524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015525f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015526{
15527 filter_map(argvars, rettv, TRUE);
15528}
15529
15530/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015531 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 */
15533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015534f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015536 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537}
15538
15539/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015540 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 */
15542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015543f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015545 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546}
15547
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015548static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549
15550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015551find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015553 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015554 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015555 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556 char_u *pat;
15557 regmatch_T regmatch;
15558 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015559 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 char_u *save_cpo;
15561 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015562 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015563 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015564 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015565 list_T *l = NULL;
15566 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015567 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015568 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569
15570 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15571 save_cpo = p_cpo;
15572 p_cpo = (char_u *)"";
15573
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015574 rettv->vval.v_number = -1;
15575 if (type == 3)
15576 {
15577 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015578 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015579 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015580 }
15581 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583 rettv->v_type = VAR_STRING;
15584 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015587 if (argvars[0].v_type == VAR_LIST)
15588 {
15589 if ((l = argvars[0].vval.v_list) == NULL)
15590 goto theend;
15591 li = l->lv_first;
15592 }
15593 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015594 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015595 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015596 len = (long)STRLEN(str);
15597 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015599 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15600 if (pat == NULL)
15601 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015602
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015603 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015605 int error = FALSE;
15606
15607 start = get_tv_number_chk(&argvars[2], &error);
15608 if (error)
15609 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015610 if (l != NULL)
15611 {
15612 li = list_find(l, start);
15613 if (li == NULL)
15614 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015615 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015616 }
15617 else
15618 {
15619 if (start < 0)
15620 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015621 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015622 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015623 /* When "count" argument is there ignore matches before "start",
15624 * otherwise skip part of the string. Differs when pattern is "^"
15625 * or "\<". */
15626 if (argvars[3].v_type != VAR_UNKNOWN)
15627 startcol = start;
15628 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015629 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015630 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015631 len -= start;
15632 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015633 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015634
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015635 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015636 nth = get_tv_number_chk(&argvars[3], &error);
15637 if (error)
15638 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 }
15640
15641 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15642 if (regmatch.regprog != NULL)
15643 {
15644 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015645
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015646 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015647 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015648 if (l != NULL)
15649 {
15650 if (li == NULL)
15651 {
15652 match = FALSE;
15653 break;
15654 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015655 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015656 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015657 if (str == NULL)
15658 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015659 }
15660
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015661 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015662
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015663 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015664 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015665 if (l == NULL && !match)
15666 break;
15667
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015668 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015669 if (l != NULL)
15670 {
15671 li = li->li_next;
15672 ++idx;
15673 }
15674 else
15675 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015676#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015677 startcol = (colnr_T)(regmatch.startp[0]
15678 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015679#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015680 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015681#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015682 if (startcol > (colnr_T)len
15683 || str + startcol <= regmatch.startp[0])
15684 {
15685 match = FALSE;
15686 break;
15687 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015688 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015689 }
15690
15691 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015693 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015694 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015695 int i;
15696
15697 /* return list with matched string and submatches */
15698 for (i = 0; i < NSUBEXP; ++i)
15699 {
15700 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015701 {
15702 if (list_append_string(rettv->vval.v_list,
15703 (char_u *)"", 0) == FAIL)
15704 break;
15705 }
15706 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015707 regmatch.startp[i],
15708 (int)(regmatch.endp[i] - regmatch.startp[i]))
15709 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015710 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015711 }
15712 }
15713 else if (type == 2)
15714 {
15715 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015716 if (l != NULL)
15717 copy_tv(&li->li_tv, rettv);
15718 else
15719 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015721 }
15722 else if (l != NULL)
15723 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 else
15725 {
15726 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015727 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 (varnumber_T)(regmatch.startp[0] - str);
15729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015732 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 }
15734 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015735 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 }
15737
15738theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015739 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 p_cpo = save_cpo;
15741}
15742
15743/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015744 * "match()" function
15745 */
15746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015747f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015748{
15749 find_some_match(argvars, rettv, 1);
15750}
15751
15752/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015753 * "matchadd()" function
15754 */
15755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015756f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015757{
15758#ifdef FEAT_SEARCH_EXTRA
15759 char_u buf[NUMBUFLEN];
15760 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15761 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15762 int prio = 10; /* default priority */
15763 int id = -1;
15764 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015765 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015766
15767 rettv->vval.v_number = -1;
15768
15769 if (grp == NULL || pat == NULL)
15770 return;
15771 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015772 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015773 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015774 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015775 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015776 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015777 if (argvars[4].v_type != VAR_UNKNOWN)
15778 {
15779 if (argvars[4].v_type != VAR_DICT)
15780 {
15781 EMSG(_(e_dictreq));
15782 return;
15783 }
15784 if (dict_find(argvars[4].vval.v_dict,
15785 (char_u *)"conceal", -1) != NULL)
15786 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15787 (char_u *)"conceal", FALSE);
15788 }
15789 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015790 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015791 if (error == TRUE)
15792 return;
15793 if (id >= 1 && id <= 3)
15794 {
15795 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15796 return;
15797 }
15798
Bram Moolenaar6561d522015-07-21 15:48:27 +020015799 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15800 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015801#endif
15802}
15803
15804/*
15805 * "matchaddpos()" function
15806 */
15807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015808f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015809{
15810#ifdef FEAT_SEARCH_EXTRA
15811 char_u buf[NUMBUFLEN];
15812 char_u *group;
15813 int prio = 10;
15814 int id = -1;
15815 int error = FALSE;
15816 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015817 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015818
15819 rettv->vval.v_number = -1;
15820
15821 group = get_tv_string_buf_chk(&argvars[0], buf);
15822 if (group == NULL)
15823 return;
15824
15825 if (argvars[1].v_type != VAR_LIST)
15826 {
15827 EMSG2(_(e_listarg), "matchaddpos()");
15828 return;
15829 }
15830 l = argvars[1].vval.v_list;
15831 if (l == NULL)
15832 return;
15833
15834 if (argvars[2].v_type != VAR_UNKNOWN)
15835 {
15836 prio = get_tv_number_chk(&argvars[2], &error);
15837 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015838 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015839 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015840 if (argvars[4].v_type != VAR_UNKNOWN)
15841 {
15842 if (argvars[4].v_type != VAR_DICT)
15843 {
15844 EMSG(_(e_dictreq));
15845 return;
15846 }
15847 if (dict_find(argvars[4].vval.v_dict,
15848 (char_u *)"conceal", -1) != NULL)
15849 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15850 (char_u *)"conceal", FALSE);
15851 }
15852 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015853 }
15854 if (error == TRUE)
15855 return;
15856
15857 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15858 if (id == 1 || id == 2)
15859 {
15860 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15861 return;
15862 }
15863
Bram Moolenaar6561d522015-07-21 15:48:27 +020015864 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15865 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015866#endif
15867}
15868
15869/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015870 * "matcharg()" function
15871 */
15872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015873f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015874{
15875 if (rettv_list_alloc(rettv) == OK)
15876 {
15877#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015878 int id = get_tv_number(&argvars[0]);
15879 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015880
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015881 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015882 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015883 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15884 {
15885 list_append_string(rettv->vval.v_list,
15886 syn_id2name(m->hlg_id), -1);
15887 list_append_string(rettv->vval.v_list, m->pattern, -1);
15888 }
15889 else
15890 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015891 list_append_string(rettv->vval.v_list, NULL, -1);
15892 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015893 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015894 }
15895#endif
15896 }
15897}
15898
15899/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015900 * "matchdelete()" function
15901 */
15902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015903f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015904{
15905#ifdef FEAT_SEARCH_EXTRA
15906 rettv->vval.v_number = match_delete(curwin,
15907 (int)get_tv_number(&argvars[0]), TRUE);
15908#endif
15909}
15910
15911/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015912 * "matchend()" function
15913 */
15914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015915f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015916{
15917 find_some_match(argvars, rettv, 0);
15918}
15919
15920/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015921 * "matchlist()" function
15922 */
15923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015924f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015925{
15926 find_some_match(argvars, rettv, 3);
15927}
15928
15929/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015930 * "matchstr()" function
15931 */
15932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015933f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015934{
15935 find_some_match(argvars, rettv, 2);
15936}
15937
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015938static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015939
15940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015941max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015942{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015943 long n = 0;
15944 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015945 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015946
15947 if (argvars[0].v_type == VAR_LIST)
15948 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015949 list_T *l;
15950 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015951
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015952 l = argvars[0].vval.v_list;
15953 if (l != NULL)
15954 {
15955 li = l->lv_first;
15956 if (li != NULL)
15957 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015958 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015959 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015960 {
15961 li = li->li_next;
15962 if (li == NULL)
15963 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015964 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015965 if (domax ? i > n : i < n)
15966 n = i;
15967 }
15968 }
15969 }
15970 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015971 else if (argvars[0].v_type == VAR_DICT)
15972 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015973 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015974 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015975 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015976 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015977
15978 d = argvars[0].vval.v_dict;
15979 if (d != NULL)
15980 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015981 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015982 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015983 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015984 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015985 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015986 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015987 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015988 if (first)
15989 {
15990 n = i;
15991 first = FALSE;
15992 }
15993 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015994 n = i;
15995 }
15996 }
15997 }
15998 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015999 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016000 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016001 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016002}
16003
16004/*
16005 * "max()" function
16006 */
16007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016008f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016009{
16010 max_min(argvars, rettv, TRUE);
16011}
16012
16013/*
16014 * "min()" function
16015 */
16016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016017f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016018{
16019 max_min(argvars, rettv, FALSE);
16020}
16021
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016022static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016023
16024/*
16025 * Create the directory in which "dir" is located, and higher levels when
16026 * needed.
16027 */
16028 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016029mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016030{
16031 char_u *p;
16032 char_u *updir;
16033 int r = FAIL;
16034
16035 /* Get end of directory name in "dir".
16036 * We're done when it's "/" or "c:/". */
16037 p = gettail_sep(dir);
16038 if (p <= get_past_head(dir))
16039 return OK;
16040
16041 /* If the directory exists we're done. Otherwise: create it.*/
16042 updir = vim_strnsave(dir, (int)(p - dir));
16043 if (updir == NULL)
16044 return FAIL;
16045 if (mch_isdir(updir))
16046 r = OK;
16047 else if (mkdir_recurse(updir, prot) == OK)
16048 r = vim_mkdir_emsg(updir, prot);
16049 vim_free(updir);
16050 return r;
16051}
16052
16053#ifdef vim_mkdir
16054/*
16055 * "mkdir()" function
16056 */
16057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016058f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016059{
16060 char_u *dir;
16061 char_u buf[NUMBUFLEN];
16062 int prot = 0755;
16063
16064 rettv->vval.v_number = FAIL;
16065 if (check_restricted() || check_secure())
16066 return;
16067
16068 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016069 if (*dir == NUL)
16070 rettv->vval.v_number = FAIL;
16071 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016072 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016073 if (*gettail(dir) == NUL)
16074 /* remove trailing slashes */
16075 *gettail_sep(dir) = NUL;
16076
16077 if (argvars[1].v_type != VAR_UNKNOWN)
16078 {
16079 if (argvars[2].v_type != VAR_UNKNOWN)
16080 prot = get_tv_number_chk(&argvars[2], NULL);
16081 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16082 mkdir_recurse(dir, prot);
16083 }
16084 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016085 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016086}
16087#endif
16088
Bram Moolenaar0d660222005-01-07 21:51:51 +000016089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 * "mode()" function
16091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016093f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016095 char_u buf[3];
16096
16097 buf[1] = NUL;
16098 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016099
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 if (VIsual_active)
16101 {
16102 if (VIsual_select)
16103 buf[0] = VIsual_mode + 's' - 'v';
16104 else
16105 buf[0] = VIsual_mode;
16106 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016107 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016108 || State == CONFIRM)
16109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016110 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016111 if (State == ASKMORE)
16112 buf[1] = 'm';
16113 else if (State == CONFIRM)
16114 buf[1] = '?';
16115 }
16116 else if (State == EXTERNCMD)
16117 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 else if (State & INSERT)
16119 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016120#ifdef FEAT_VREPLACE
16121 if (State & VREPLACE_FLAG)
16122 {
16123 buf[0] = 'R';
16124 buf[1] = 'v';
16125 }
16126 else
16127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 if (State & REPLACE_FLAG)
16129 buf[0] = 'R';
16130 else
16131 buf[0] = 'i';
16132 }
16133 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016136 if (exmode_active)
16137 buf[1] = 'v';
16138 }
16139 else if (exmode_active)
16140 {
16141 buf[0] = 'c';
16142 buf[1] = 'e';
16143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016147 if (finish_op)
16148 buf[1] = 'o';
16149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016151 /* Clear out the minor mode when the argument is not a non-zero number or
16152 * non-empty string. */
16153 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016154 buf[1] = NUL;
16155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016156 rettv->vval.v_string = vim_strsave(buf);
16157 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158}
16159
Bram Moolenaar429fa852013-04-15 12:27:36 +020016160#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016161/*
16162 * "mzeval()" function
16163 */
16164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016165f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016166{
16167 char_u *str;
16168 char_u buf[NUMBUFLEN];
16169
16170 str = get_tv_string_buf(&argvars[0], buf);
16171 do_mzeval(str, rettv);
16172}
Bram Moolenaar75676462013-01-30 14:55:42 +010016173
16174 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016175mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016176{
16177 typval_T argvars[3];
16178
16179 argvars[0].v_type = VAR_STRING;
16180 argvars[0].vval.v_string = name;
16181 copy_tv(args, &argvars[1]);
16182 argvars[2].v_type = VAR_UNKNOWN;
16183 f_call(argvars, rettv);
16184 clear_tv(&argvars[1]);
16185}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016186#endif
16187
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016189 * "nextnonblank()" function
16190 */
16191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016192f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016193{
16194 linenr_T lnum;
16195
16196 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16197 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016198 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016199 {
16200 lnum = 0;
16201 break;
16202 }
16203 if (*skipwhite(ml_get(lnum)) != NUL)
16204 break;
16205 }
16206 rettv->vval.v_number = lnum;
16207}
16208
16209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 * "nr2char()" function
16211 */
16212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016213f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214{
16215 char_u buf[NUMBUFLEN];
16216
16217#ifdef FEAT_MBYTE
16218 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016219 {
16220 int utf8 = 0;
16221
16222 if (argvars[1].v_type != VAR_UNKNOWN)
16223 utf8 = get_tv_number_chk(&argvars[1], NULL);
16224 if (utf8)
16225 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16226 else
16227 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229 else
16230#endif
16231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016232 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 buf[1] = NUL;
16234 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016235 rettv->v_type = VAR_STRING;
16236 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016237}
16238
16239/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016240 * "or(expr, expr)" function
16241 */
16242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016243f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016244{
16245 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16246 | get_tv_number_chk(&argvars[1], NULL);
16247}
16248
16249/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016250 * "pathshorten()" function
16251 */
16252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016253f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016254{
16255 char_u *p;
16256
16257 rettv->v_type = VAR_STRING;
16258 p = get_tv_string_chk(&argvars[0]);
16259 if (p == NULL)
16260 rettv->vval.v_string = NULL;
16261 else
16262 {
16263 p = vim_strsave(p);
16264 rettv->vval.v_string = p;
16265 if (p != NULL)
16266 shorten_dir(p);
16267 }
16268}
16269
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016270#ifdef FEAT_PERL
16271/*
16272 * "perleval()" function
16273 */
16274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016275f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016276{
16277 char_u *str;
16278 char_u buf[NUMBUFLEN];
16279
16280 str = get_tv_string_buf(&argvars[0], buf);
16281 do_perleval(str, rettv);
16282}
16283#endif
16284
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016285#ifdef FEAT_FLOAT
16286/*
16287 * "pow()" function
16288 */
16289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016290f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016291{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016292 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016293
16294 rettv->v_type = VAR_FLOAT;
16295 if (get_float_arg(argvars, &fx) == OK
16296 && get_float_arg(&argvars[1], &fy) == OK)
16297 rettv->vval.v_float = pow(fx, fy);
16298 else
16299 rettv->vval.v_float = 0.0;
16300}
16301#endif
16302
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016303/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016304 * "prevnonblank()" function
16305 */
16306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016307f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308{
16309 linenr_T lnum;
16310
16311 lnum = get_tv_lnum(argvars);
16312 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16313 lnum = 0;
16314 else
16315 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16316 --lnum;
16317 rettv->vval.v_number = lnum;
16318}
16319
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016320/* This dummy va_list is here because:
16321 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16322 * - locally in the function results in a "used before set" warning
16323 * - using va_start() to initialize it gives "function with fixed args" error */
16324static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016325
Bram Moolenaar8c711452005-01-14 21:53:12 +000016326/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016327 * "printf()" function
16328 */
16329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016330f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016331{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016332 char_u buf[NUMBUFLEN];
16333 int len;
16334 char_u *s;
16335 int saved_did_emsg = did_emsg;
16336 char *fmt;
16337
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016338 rettv->v_type = VAR_STRING;
16339 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016340
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016341 /* Get the required length, allocate the buffer and do it for real. */
16342 did_emsg = FALSE;
16343 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16344 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16345 if (!did_emsg)
16346 {
16347 s = alloc(len + 1);
16348 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016349 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016350 rettv->vval.v_string = s;
16351 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016352 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016353 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016354 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016355}
16356
16357/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016358 * "pumvisible()" function
16359 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016361f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016362{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016363#ifdef FEAT_INS_EXPAND
16364 if (pum_visible())
16365 rettv->vval.v_number = 1;
16366#endif
16367}
16368
Bram Moolenaardb913952012-06-29 12:54:53 +020016369#ifdef FEAT_PYTHON3
16370/*
16371 * "py3eval()" function
16372 */
16373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016374f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016375{
16376 char_u *str;
16377 char_u buf[NUMBUFLEN];
16378
16379 str = get_tv_string_buf(&argvars[0], buf);
16380 do_py3eval(str, rettv);
16381}
16382#endif
16383
16384#ifdef FEAT_PYTHON
16385/*
16386 * "pyeval()" function
16387 */
16388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016389f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016390{
16391 char_u *str;
16392 char_u buf[NUMBUFLEN];
16393
16394 str = get_tv_string_buf(&argvars[0], buf);
16395 do_pyeval(str, rettv);
16396}
16397#endif
16398
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016399/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016400 * "range()" function
16401 */
16402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016403f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016404{
16405 long start;
16406 long end;
16407 long stride = 1;
16408 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016409 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016411 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016412 if (argvars[1].v_type == VAR_UNKNOWN)
16413 {
16414 end = start - 1;
16415 start = 0;
16416 }
16417 else
16418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016419 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016420 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016421 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016422 }
16423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016424 if (error)
16425 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016426 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016427 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016428 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016429 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016430 else
16431 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016432 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016433 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016434 if (list_append_number(rettv->vval.v_list,
16435 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016436 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016437 }
16438}
16439
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016440/*
16441 * "readfile()" function
16442 */
16443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016444f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016445{
16446 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016447 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016448 char_u *fname;
16449 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016450 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16451 int io_size = sizeof(buf);
16452 int readlen; /* size of last fread() */
16453 char_u *prev = NULL; /* previously read bytes, if any */
16454 long prevlen = 0; /* length of data in prev */
16455 long prevsize = 0; /* size of prev buffer */
16456 long maxline = MAXLNUM;
16457 long cnt = 0;
16458 char_u *p; /* position in buf */
16459 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016460
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016461 if (argvars[1].v_type != VAR_UNKNOWN)
16462 {
16463 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16464 binary = TRUE;
16465 if (argvars[2].v_type != VAR_UNKNOWN)
16466 maxline = get_tv_number(&argvars[2]);
16467 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016468
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016469 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016470 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016471
16472 /* Always open the file in binary mode, library functions have a mind of
16473 * their own about CR-LF conversion. */
16474 fname = get_tv_string(&argvars[0]);
16475 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16476 {
16477 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16478 return;
16479 }
16480
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016481 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016482 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016483 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016484
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016485 /* This for loop processes what was read, but is also entered at end
16486 * of file so that either:
16487 * - an incomplete line gets written
16488 * - a "binary" file gets an empty line at the end if it ends in a
16489 * newline. */
16490 for (p = buf, start = buf;
16491 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16492 ++p)
16493 {
16494 if (*p == '\n' || readlen <= 0)
16495 {
16496 listitem_T *li;
16497 char_u *s = NULL;
16498 long_u len = p - start;
16499
16500 /* Finished a line. Remove CRs before NL. */
16501 if (readlen > 0 && !binary)
16502 {
16503 while (len > 0 && start[len - 1] == '\r')
16504 --len;
16505 /* removal may cross back to the "prev" string */
16506 if (len == 0)
16507 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16508 --prevlen;
16509 }
16510 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016511 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016512 else
16513 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016514 /* Change "prev" buffer to be the right size. This way
16515 * the bytes are only copied once, and very long lines are
16516 * allocated only once. */
16517 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016518 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016519 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016520 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016521 prev = NULL; /* the list will own the string */
16522 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016523 }
16524 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016525 if (s == NULL)
16526 {
16527 do_outofmem_msg((long_u) prevlen + len + 1);
16528 failed = TRUE;
16529 break;
16530 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016531
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016532 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016533 {
16534 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016535 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016536 break;
16537 }
16538 li->li_tv.v_type = VAR_STRING;
16539 li->li_tv.v_lock = 0;
16540 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016541 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016542
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016543 start = p + 1; /* step over newline */
16544 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016545 break;
16546 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016547 else if (*p == NUL)
16548 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016549#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016550 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16551 * when finding the BF and check the previous two bytes. */
16552 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016553 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016554 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16555 * + 1, these may be in the "prev" string. */
16556 char_u back1 = p >= buf + 1 ? p[-1]
16557 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16558 char_u back2 = p >= buf + 2 ? p[-2]
16559 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16560 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016561
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016562 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016563 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016564 char_u *dest = p - 2;
16565
16566 /* Usually a BOM is at the beginning of a file, and so at
16567 * the beginning of a line; then we can just step over it.
16568 */
16569 if (start == dest)
16570 start = p + 1;
16571 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016572 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016573 /* have to shuffle buf to close gap */
16574 int adjust_prevlen = 0;
16575
16576 if (dest < buf)
16577 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016578 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016579 dest = buf;
16580 }
16581 if (readlen > p - buf + 1)
16582 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16583 readlen -= 3 - adjust_prevlen;
16584 prevlen -= adjust_prevlen;
16585 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016586 }
16587 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016588 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016589#endif
16590 } /* for */
16591
16592 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16593 break;
16594 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016595 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016596 /* There's part of a line in buf, store it in "prev". */
16597 if (p - start + prevlen >= prevsize)
16598 {
16599 /* need bigger "prev" buffer */
16600 char_u *newprev;
16601
16602 /* A common use case is ordinary text files and "prev" gets a
16603 * fragment of a line, so the first allocation is made
16604 * small, to avoid repeatedly 'allocing' large and
16605 * 'reallocing' small. */
16606 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016607 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016608 else
16609 {
16610 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016611 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016612 prevsize = grow50pc > growmin ? grow50pc : growmin;
16613 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016614 newprev = prev == NULL ? alloc(prevsize)
16615 : vim_realloc(prev, prevsize);
16616 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016617 {
16618 do_outofmem_msg((long_u)prevsize);
16619 failed = TRUE;
16620 break;
16621 }
16622 prev = newprev;
16623 }
16624 /* Add the line part to end of "prev". */
16625 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016626 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016627 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016628 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016629
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016630 /*
16631 * For a negative line count use only the lines at the end of the file,
16632 * free the rest.
16633 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016634 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016635 while (cnt > -maxline)
16636 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016637 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016638 --cnt;
16639 }
16640
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016641 if (failed)
16642 {
16643 list_free(rettv->vval.v_list, TRUE);
16644 /* readfile doc says an empty list is returned on error */
16645 rettv->vval.v_list = list_alloc();
16646 }
16647
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016648 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016649 fclose(fd);
16650}
16651
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016652#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016653static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016654
16655/*
16656 * Convert a List to proftime_T.
16657 * Return FAIL when there is something wrong.
16658 */
16659 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016660list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016661{
16662 long n1, n2;
16663 int error = FALSE;
16664
16665 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16666 || arg->vval.v_list->lv_len != 2)
16667 return FAIL;
16668 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16669 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16670# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016671 tm->HighPart = n1;
16672 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016673# else
16674 tm->tv_sec = n1;
16675 tm->tv_usec = n2;
16676# endif
16677 return error ? FAIL : OK;
16678}
16679#endif /* FEAT_RELTIME */
16680
16681/*
16682 * "reltime()" function
16683 */
16684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016685f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016686{
16687#ifdef FEAT_RELTIME
16688 proftime_T res;
16689 proftime_T start;
16690
16691 if (argvars[0].v_type == VAR_UNKNOWN)
16692 {
16693 /* No arguments: get current time. */
16694 profile_start(&res);
16695 }
16696 else if (argvars[1].v_type == VAR_UNKNOWN)
16697 {
16698 if (list2proftime(&argvars[0], &res) == FAIL)
16699 return;
16700 profile_end(&res);
16701 }
16702 else
16703 {
16704 /* Two arguments: compute the difference. */
16705 if (list2proftime(&argvars[0], &start) == FAIL
16706 || list2proftime(&argvars[1], &res) == FAIL)
16707 return;
16708 profile_sub(&res, &start);
16709 }
16710
16711 if (rettv_list_alloc(rettv) == OK)
16712 {
16713 long n1, n2;
16714
16715# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016716 n1 = res.HighPart;
16717 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016718# else
16719 n1 = res.tv_sec;
16720 n2 = res.tv_usec;
16721# endif
16722 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16723 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16724 }
16725#endif
16726}
16727
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016728#ifdef FEAT_FLOAT
16729/*
16730 * "reltimefloat()" function
16731 */
16732 static void
16733f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16734{
16735# ifdef FEAT_RELTIME
16736 proftime_T tm;
16737# endif
16738
16739 rettv->v_type = VAR_FLOAT;
16740 rettv->vval.v_float = 0;
16741# ifdef FEAT_RELTIME
16742 if (list2proftime(&argvars[0], &tm) == OK)
16743 rettv->vval.v_float = profile_float(&tm);
16744# endif
16745}
16746#endif
16747
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016748/*
16749 * "reltimestr()" function
16750 */
16751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016752f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016753{
16754#ifdef FEAT_RELTIME
16755 proftime_T tm;
16756#endif
16757
16758 rettv->v_type = VAR_STRING;
16759 rettv->vval.v_string = NULL;
16760#ifdef FEAT_RELTIME
16761 if (list2proftime(&argvars[0], &tm) == OK)
16762 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16763#endif
16764}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016765
Bram Moolenaar0d660222005-01-07 21:51:51 +000016766#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016767static void make_connection(void);
16768static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769
16770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016771make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016772{
16773 if (X_DISPLAY == NULL
16774# ifdef FEAT_GUI
16775 && !gui.in_use
16776# endif
16777 )
16778 {
16779 x_force_connect = TRUE;
16780 setup_term_clip();
16781 x_force_connect = FALSE;
16782 }
16783}
16784
16785 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016786check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016787{
16788 make_connection();
16789 if (X_DISPLAY == NULL)
16790 {
16791 EMSG(_("E240: No connection to Vim server"));
16792 return FAIL;
16793 }
16794 return OK;
16795}
16796#endif
16797
16798#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016799static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016800
16801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016802remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016803{
16804 char_u *server_name;
16805 char_u *keys;
16806 char_u *r = NULL;
16807 char_u buf[NUMBUFLEN];
16808# ifdef WIN32
16809 HWND w;
16810# else
16811 Window w;
16812# endif
16813
16814 if (check_restricted() || check_secure())
16815 return;
16816
16817# ifdef FEAT_X11
16818 if (check_connection() == FAIL)
16819 return;
16820# endif
16821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016822 server_name = get_tv_string_chk(&argvars[0]);
16823 if (server_name == NULL)
16824 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016825 keys = get_tv_string_buf(&argvars[1], buf);
16826# ifdef WIN32
16827 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16828# else
16829 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16830 < 0)
16831# endif
16832 {
16833 if (r != NULL)
16834 EMSG(r); /* sending worked but evaluation failed */
16835 else
16836 EMSG2(_("E241: Unable to send to %s"), server_name);
16837 return;
16838 }
16839
16840 rettv->vval.v_string = r;
16841
16842 if (argvars[2].v_type != VAR_UNKNOWN)
16843 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016844 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016845 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016846 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016847
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016848 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016849 v.di_tv.v_type = VAR_STRING;
16850 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016851 idvar = get_tv_string_chk(&argvars[2]);
16852 if (idvar != NULL)
16853 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016854 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016855 }
16856}
16857#endif
16858
16859/*
16860 * "remote_expr()" function
16861 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016863f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016864{
16865 rettv->v_type = VAR_STRING;
16866 rettv->vval.v_string = NULL;
16867#ifdef FEAT_CLIENTSERVER
16868 remote_common(argvars, rettv, TRUE);
16869#endif
16870}
16871
16872/*
16873 * "remote_foreground()" function
16874 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016876f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878#ifdef FEAT_CLIENTSERVER
16879# ifdef WIN32
16880 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016881 {
16882 char_u *server_name = get_tv_string_chk(&argvars[0]);
16883
16884 if (server_name != NULL)
16885 serverForeground(server_name);
16886 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887# else
16888 /* Send a foreground() expression to the server. */
16889 argvars[1].v_type = VAR_STRING;
16890 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16891 argvars[2].v_type = VAR_UNKNOWN;
16892 remote_common(argvars, rettv, TRUE);
16893 vim_free(argvars[1].vval.v_string);
16894# endif
16895#endif
16896}
16897
Bram Moolenaar0d660222005-01-07 21:51:51 +000016898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016899f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016900{
16901#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016902 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016903 char_u *s = NULL;
16904# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016905 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016907 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016908
16909 if (check_restricted() || check_secure())
16910 {
16911 rettv->vval.v_number = -1;
16912 return;
16913 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016914 serverid = get_tv_string_chk(&argvars[0]);
16915 if (serverid == NULL)
16916 {
16917 rettv->vval.v_number = -1;
16918 return; /* type error; errmsg already given */
16919 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016921 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922 if (n == 0)
16923 rettv->vval.v_number = -1;
16924 else
16925 {
16926 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16927 rettv->vval.v_number = (s != NULL);
16928 }
16929# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 if (check_connection() == FAIL)
16931 return;
16932
16933 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016934 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016935# endif
16936
16937 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16938 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016939 char_u *retvar;
16940
Bram Moolenaar33570922005-01-25 22:26:29 +000016941 v.di_tv.v_type = VAR_STRING;
16942 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016943 retvar = get_tv_string_chk(&argvars[1]);
16944 if (retvar != NULL)
16945 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016946 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947 }
16948#else
16949 rettv->vval.v_number = -1;
16950#endif
16951}
16952
Bram Moolenaar0d660222005-01-07 21:51:51 +000016953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016954f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955{
16956 char_u *r = NULL;
16957
16958#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016959 char_u *serverid = get_tv_string_chk(&argvars[0]);
16960
16961 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 {
16963# ifdef WIN32
16964 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016965 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016967 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968 if (n != 0)
16969 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16970 if (r == NULL)
16971# else
16972 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016973 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974# endif
16975 EMSG(_("E277: Unable to read a server reply"));
16976 }
16977#endif
16978 rettv->v_type = VAR_STRING;
16979 rettv->vval.v_string = r;
16980}
16981
16982/*
16983 * "remote_send()" function
16984 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016986f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016987{
16988 rettv->v_type = VAR_STRING;
16989 rettv->vval.v_string = NULL;
16990#ifdef FEAT_CLIENTSERVER
16991 remote_common(argvars, rettv, FALSE);
16992#endif
16993}
16994
16995/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016996 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016997 */
16998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016999f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017000{
Bram Moolenaar33570922005-01-25 22:26:29 +000017001 list_T *l;
17002 listitem_T *item, *item2;
17003 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017004 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017005 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017006 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017007 dict_T *d;
17008 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017009 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017010
Bram Moolenaar8c711452005-01-14 21:53:12 +000017011 if (argvars[0].v_type == VAR_DICT)
17012 {
17013 if (argvars[2].v_type != VAR_UNKNOWN)
17014 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017015 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017016 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 key = get_tv_string_chk(&argvars[1]);
17019 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017020 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 di = dict_find(d, key, -1);
17022 if (di == NULL)
17023 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017024 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17025 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017026 {
17027 *rettv = di->di_tv;
17028 init_tv(&di->di_tv);
17029 dictitem_remove(d, di);
17030 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017031 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017032 }
17033 }
17034 else if (argvars[0].v_type != VAR_LIST)
17035 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017036 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017037 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017038 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017039 int error = FALSE;
17040
17041 idx = get_tv_number_chk(&argvars[1], &error);
17042 if (error)
17043 ; /* type error: do nothing, errmsg already given */
17044 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017045 EMSGN(_(e_listidx), idx);
17046 else
17047 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017048 if (argvars[2].v_type == VAR_UNKNOWN)
17049 {
17050 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017051 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017052 *rettv = item->li_tv;
17053 vim_free(item);
17054 }
17055 else
17056 {
17057 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017058 end = get_tv_number_chk(&argvars[2], &error);
17059 if (error)
17060 ; /* type error: do nothing */
17061 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017062 EMSGN(_(e_listidx), end);
17063 else
17064 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017065 int cnt = 0;
17066
17067 for (li = item; li != NULL; li = li->li_next)
17068 {
17069 ++cnt;
17070 if (li == item2)
17071 break;
17072 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017073 if (li == NULL) /* didn't find "item2" after "item" */
17074 EMSG(_(e_invrange));
17075 else
17076 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017077 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017078 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017079 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017080 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017081 l->lv_first = item;
17082 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017083 item->li_prev = NULL;
17084 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017085 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017086 }
17087 }
17088 }
17089 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017090 }
17091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092}
17093
17094/*
17095 * "rename({from}, {to})" function
17096 */
17097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017098f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017099{
17100 char_u buf[NUMBUFLEN];
17101
17102 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017103 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017105 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17106 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107}
17108
17109/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017110 * "repeat()" function
17111 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017113f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017114{
17115 char_u *p;
17116 int n;
17117 int slen;
17118 int len;
17119 char_u *r;
17120 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017121
17122 n = get_tv_number(&argvars[1]);
17123 if (argvars[0].v_type == VAR_LIST)
17124 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017125 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017126 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017127 if (list_extend(rettv->vval.v_list,
17128 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017129 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017130 }
17131 else
17132 {
17133 p = get_tv_string(&argvars[0]);
17134 rettv->v_type = VAR_STRING;
17135 rettv->vval.v_string = NULL;
17136
17137 slen = (int)STRLEN(p);
17138 len = slen * n;
17139 if (len <= 0)
17140 return;
17141
17142 r = alloc(len + 1);
17143 if (r != NULL)
17144 {
17145 for (i = 0; i < n; i++)
17146 mch_memmove(r + i * slen, p, (size_t)slen);
17147 r[len] = NUL;
17148 }
17149
17150 rettv->vval.v_string = r;
17151 }
17152}
17153
17154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 * "resolve()" function
17156 */
17157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017158f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159{
17160 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017161#ifdef HAVE_READLINK
17162 char_u *buf = NULL;
17163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017165 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166#ifdef FEAT_SHORTCUT
17167 {
17168 char_u *v = NULL;
17169
17170 v = mch_resolve_shortcut(p);
17171 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017172 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017174 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175 }
17176#else
17177# ifdef HAVE_READLINK
17178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 char_u *cpy;
17180 int len;
17181 char_u *remain = NULL;
17182 char_u *q;
17183 int is_relative_to_current = FALSE;
17184 int has_trailing_pathsep = FALSE;
17185 int limit = 100;
17186
17187 p = vim_strsave(p);
17188
17189 if (p[0] == '.' && (vim_ispathsep(p[1])
17190 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17191 is_relative_to_current = TRUE;
17192
17193 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017194 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017197 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199
17200 q = getnextcomp(p);
17201 if (*q != NUL)
17202 {
17203 /* Separate the first path component in "p", and keep the
17204 * remainder (beginning with the path separator). */
17205 remain = vim_strsave(q - 1);
17206 q[-1] = NUL;
17207 }
17208
Bram Moolenaard9462e32011-04-11 21:35:11 +020017209 buf = alloc(MAXPATHL + 1);
17210 if (buf == NULL)
17211 goto fail;
17212
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 for (;;)
17214 {
17215 for (;;)
17216 {
17217 len = readlink((char *)p, (char *)buf, MAXPATHL);
17218 if (len <= 0)
17219 break;
17220 buf[len] = NUL;
17221
17222 if (limit-- == 0)
17223 {
17224 vim_free(p);
17225 vim_free(remain);
17226 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017227 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 goto fail;
17229 }
17230
17231 /* Ensure that the result will have a trailing path separator
17232 * if the argument has one. */
17233 if (remain == NULL && has_trailing_pathsep)
17234 add_pathsep(buf);
17235
17236 /* Separate the first path component in the link value and
17237 * concatenate the remainders. */
17238 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17239 if (*q != NUL)
17240 {
17241 if (remain == NULL)
17242 remain = vim_strsave(q - 1);
17243 else
17244 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017245 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246 if (cpy != NULL)
17247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 vim_free(remain);
17249 remain = cpy;
17250 }
17251 }
17252 q[-1] = NUL;
17253 }
17254
17255 q = gettail(p);
17256 if (q > p && *q == NUL)
17257 {
17258 /* Ignore trailing path separator. */
17259 q[-1] = NUL;
17260 q = gettail(p);
17261 }
17262 if (q > p && !mch_isFullName(buf))
17263 {
17264 /* symlink is relative to directory of argument */
17265 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17266 if (cpy != NULL)
17267 {
17268 STRCPY(cpy, p);
17269 STRCPY(gettail(cpy), buf);
17270 vim_free(p);
17271 p = cpy;
17272 }
17273 }
17274 else
17275 {
17276 vim_free(p);
17277 p = vim_strsave(buf);
17278 }
17279 }
17280
17281 if (remain == NULL)
17282 break;
17283
17284 /* Append the first path component of "remain" to "p". */
17285 q = getnextcomp(remain + 1);
17286 len = q - remain - (*q != NUL);
17287 cpy = vim_strnsave(p, STRLEN(p) + len);
17288 if (cpy != NULL)
17289 {
17290 STRNCAT(cpy, remain, len);
17291 vim_free(p);
17292 p = cpy;
17293 }
17294 /* Shorten "remain". */
17295 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017296 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 else
17298 {
17299 vim_free(remain);
17300 remain = NULL;
17301 }
17302 }
17303
17304 /* If the result is a relative path name, make it explicitly relative to
17305 * the current directory if and only if the argument had this form. */
17306 if (!vim_ispathsep(*p))
17307 {
17308 if (is_relative_to_current
17309 && *p != NUL
17310 && !(p[0] == '.'
17311 && (p[1] == NUL
17312 || vim_ispathsep(p[1])
17313 || (p[1] == '.'
17314 && (p[2] == NUL
17315 || vim_ispathsep(p[2]))))))
17316 {
17317 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017318 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 if (cpy != NULL)
17320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 vim_free(p);
17322 p = cpy;
17323 }
17324 }
17325 else if (!is_relative_to_current)
17326 {
17327 /* Strip leading "./". */
17328 q = p;
17329 while (q[0] == '.' && vim_ispathsep(q[1]))
17330 q += 2;
17331 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017332 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 }
17334 }
17335
17336 /* Ensure that the result will have no trailing path separator
17337 * if the argument had none. But keep "/" or "//". */
17338 if (!has_trailing_pathsep)
17339 {
17340 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017341 if (after_pathsep(p, q))
17342 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343 }
17344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017345 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 }
17347# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349# endif
17350#endif
17351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017352 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353
17354#ifdef HAVE_READLINK
17355fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017356 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017358 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359}
17360
17361/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017362 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 */
17364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017365f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366{
Bram Moolenaar33570922005-01-25 22:26:29 +000017367 list_T *l;
17368 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369
Bram Moolenaar0d660222005-01-07 21:51:51 +000017370 if (argvars[0].v_type != VAR_LIST)
17371 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017372 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017373 && !tv_check_lock(l->lv_lock,
17374 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017375 {
17376 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017377 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017378 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017379 while (li != NULL)
17380 {
17381 ni = li->li_prev;
17382 list_append(l, li);
17383 li = ni;
17384 }
17385 rettv->vval.v_list = l;
17386 rettv->v_type = VAR_LIST;
17387 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017388 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390}
17391
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017392#define SP_NOMOVE 0x01 /* don't move cursor */
17393#define SP_REPEAT 0x02 /* repeat to find outer pair */
17394#define SP_RETCOUNT 0x04 /* return matchcount */
17395#define SP_SETPCMARK 0x08 /* set previous context mark */
17396#define SP_START 0x10 /* accept match at start position */
17397#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17398#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017399#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017400
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017401static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017402
17403/*
17404 * Get flags for a search function.
17405 * Possibly sets "p_ws".
17406 * Returns BACKWARD, FORWARD or zero (for an error).
17407 */
17408 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017409get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017410{
17411 int dir = FORWARD;
17412 char_u *flags;
17413 char_u nbuf[NUMBUFLEN];
17414 int mask;
17415
17416 if (varp->v_type != VAR_UNKNOWN)
17417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017418 flags = get_tv_string_buf_chk(varp, nbuf);
17419 if (flags == NULL)
17420 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017421 while (*flags != NUL)
17422 {
17423 switch (*flags)
17424 {
17425 case 'b': dir = BACKWARD; break;
17426 case 'w': p_ws = TRUE; break;
17427 case 'W': p_ws = FALSE; break;
17428 default: mask = 0;
17429 if (flagsp != NULL)
17430 switch (*flags)
17431 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017432 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017433 case 'e': mask = SP_END; break;
17434 case 'm': mask = SP_RETCOUNT; break;
17435 case 'n': mask = SP_NOMOVE; break;
17436 case 'p': mask = SP_SUBPAT; break;
17437 case 'r': mask = SP_REPEAT; break;
17438 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017439 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017440 }
17441 if (mask == 0)
17442 {
17443 EMSG2(_(e_invarg2), flags);
17444 dir = 0;
17445 }
17446 else
17447 *flagsp |= mask;
17448 }
17449 if (dir == 0)
17450 break;
17451 ++flags;
17452 }
17453 }
17454 return dir;
17455}
17456
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017458 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017460 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017461search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017463 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 char_u *pat;
17465 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017466 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 int save_p_ws = p_ws;
17468 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017469 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017470 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017471 proftime_T tm;
17472#ifdef FEAT_RELTIME
17473 long time_limit = 0;
17474#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017475 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017476 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017478 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017479 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017480 if (dir == 0)
17481 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017482 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017483 if (flags & SP_START)
17484 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017485 if (flags & SP_END)
17486 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017487 if (flags & SP_COLUMN)
17488 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017489
Bram Moolenaar76929292008-01-06 19:07:36 +000017490 /* Optional arguments: line number to stop searching and timeout. */
17491 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017492 {
17493 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17494 if (lnum_stop < 0)
17495 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017496#ifdef FEAT_RELTIME
17497 if (argvars[3].v_type != VAR_UNKNOWN)
17498 {
17499 time_limit = get_tv_number_chk(&argvars[3], NULL);
17500 if (time_limit < 0)
17501 goto theend;
17502 }
17503#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017504 }
17505
Bram Moolenaar76929292008-01-06 19:07:36 +000017506#ifdef FEAT_RELTIME
17507 /* Set the time limit, if there is one. */
17508 profile_setlimit(time_limit, &tm);
17509#endif
17510
Bram Moolenaar231334e2005-07-25 20:46:57 +000017511 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017512 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017513 * Check to make sure only those flags are set.
17514 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17515 * flags cannot be set. Check for that condition also.
17516 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017517 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017518 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017520 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017521 goto theend;
17522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017524 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017525 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017526 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017527 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017529 if (flags & SP_SUBPAT)
17530 retval = subpatnum;
17531 else
17532 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017533 if (flags & SP_SETPCMARK)
17534 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017536 if (match_pos != NULL)
17537 {
17538 /* Store the match cursor position */
17539 match_pos->lnum = pos.lnum;
17540 match_pos->col = pos.col + 1;
17541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542 /* "/$" will put the cursor after the end of the line, may need to
17543 * correct that here */
17544 check_cursor();
17545 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017546
17547 /* If 'n' flag is used: restore cursor position. */
17548 if (flags & SP_NOMOVE)
17549 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017550 else
17551 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017552theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017554
17555 return retval;
17556}
17557
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017558#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017559
17560/*
17561 * round() is not in C90, use ceil() or floor() instead.
17562 */
17563 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017564vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017565{
17566 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17567}
17568
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017569/*
17570 * "round({float})" function
17571 */
17572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017573f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017574{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017575 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017576
17577 rettv->v_type = VAR_FLOAT;
17578 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017579 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017580 else
17581 rettv->vval.v_float = 0.0;
17582}
17583#endif
17584
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017585/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017586 * "screenattr()" function
17587 */
17588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017589f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017590{
17591 int row;
17592 int col;
17593 int c;
17594
17595 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17596 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17597 if (row < 0 || row >= screen_Rows
17598 || col < 0 || col >= screen_Columns)
17599 c = -1;
17600 else
17601 c = ScreenAttrs[LineOffset[row] + col];
17602 rettv->vval.v_number = c;
17603}
17604
17605/*
17606 * "screenchar()" function
17607 */
17608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017609f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017610{
17611 int row;
17612 int col;
17613 int off;
17614 int c;
17615
17616 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17617 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17618 if (row < 0 || row >= screen_Rows
17619 || col < 0 || col >= screen_Columns)
17620 c = -1;
17621 else
17622 {
17623 off = LineOffset[row] + col;
17624#ifdef FEAT_MBYTE
17625 if (enc_utf8 && ScreenLinesUC[off] != 0)
17626 c = ScreenLinesUC[off];
17627 else
17628#endif
17629 c = ScreenLines[off];
17630 }
17631 rettv->vval.v_number = c;
17632}
17633
17634/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017635 * "screencol()" function
17636 *
17637 * First column is 1 to be consistent with virtcol().
17638 */
17639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017640f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017641{
17642 rettv->vval.v_number = screen_screencol() + 1;
17643}
17644
17645/*
17646 * "screenrow()" function
17647 */
17648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017649f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017650{
17651 rettv->vval.v_number = screen_screenrow() + 1;
17652}
17653
17654/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017655 * "search()" function
17656 */
17657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017658f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017659{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017660 int flags = 0;
17661
17662 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663}
17664
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017666 * "searchdecl()" function
17667 */
17668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017669f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017670{
17671 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017672 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017673 int error = FALSE;
17674 char_u *name;
17675
17676 rettv->vval.v_number = 1; /* default: FAIL */
17677
17678 name = get_tv_string_chk(&argvars[0]);
17679 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017680 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017681 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017682 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17683 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17684 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017685 if (!error && name != NULL)
17686 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017687 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017688}
17689
17690/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017691 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017693 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017694searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695{
17696 char_u *spat, *mpat, *epat;
17697 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 int dir;
17700 int flags = 0;
17701 char_u nbuf1[NUMBUFLEN];
17702 char_u nbuf2[NUMBUFLEN];
17703 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017704 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017705 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017706 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017709 spat = get_tv_string_chk(&argvars[0]);
17710 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17711 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17712 if (spat == NULL || mpat == NULL || epat == NULL)
17713 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 /* Handle the optional fourth argument: flags */
17716 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017717 if (dir == 0)
17718 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017719
17720 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017721 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17722 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017723 if ((flags & (SP_END | SP_SUBPAT)) != 0
17724 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017725 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017726 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017727 goto theend;
17728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017730 /* Using 'r' implies 'W', otherwise it doesn't work. */
17731 if (flags & SP_REPEAT)
17732 p_ws = FALSE;
17733
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017734 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017735 if (argvars[3].v_type == VAR_UNKNOWN
17736 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 skip = (char_u *)"";
17738 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017740 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017741 if (argvars[5].v_type != VAR_UNKNOWN)
17742 {
17743 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17744 if (lnum_stop < 0)
17745 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017746#ifdef FEAT_RELTIME
17747 if (argvars[6].v_type != VAR_UNKNOWN)
17748 {
17749 time_limit = get_tv_number_chk(&argvars[6], NULL);
17750 if (time_limit < 0)
17751 goto theend;
17752 }
17753#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017754 }
17755 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017756 if (skip == NULL)
17757 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017759 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017760 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017761
17762theend:
17763 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017764
17765 return retval;
17766}
17767
17768/*
17769 * "searchpair()" function
17770 */
17771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017772f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017773{
17774 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17775}
17776
17777/*
17778 * "searchpairpos()" function
17779 */
17780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017781f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017782{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017783 pos_T match_pos;
17784 int lnum = 0;
17785 int col = 0;
17786
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017787 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017788 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017789
17790 if (searchpair_cmn(argvars, &match_pos) > 0)
17791 {
17792 lnum = match_pos.lnum;
17793 col = match_pos.col;
17794 }
17795
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017796 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17797 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017798}
17799
17800/*
17801 * Search for a start/middle/end thing.
17802 * Used by searchpair(), see its documentation for the details.
17803 * Returns 0 or -1 for no match,
17804 */
17805 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017806do_searchpair(
17807 char_u *spat, /* start pattern */
17808 char_u *mpat, /* middle pattern */
17809 char_u *epat, /* end pattern */
17810 int dir, /* BACKWARD or FORWARD */
17811 char_u *skip, /* skip expression */
17812 int flags, /* SP_SETPCMARK and other SP_ values */
17813 pos_T *match_pos,
17814 linenr_T lnum_stop, /* stop at this line if not zero */
17815 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017816{
17817 char_u *save_cpo;
17818 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17819 long retval = 0;
17820 pos_T pos;
17821 pos_T firstpos;
17822 pos_T foundpos;
17823 pos_T save_cursor;
17824 pos_T save_pos;
17825 int n;
17826 int r;
17827 int nest = 1;
17828 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017829 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017830 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017831
17832 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17833 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017834 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017835
Bram Moolenaar76929292008-01-06 19:07:36 +000017836#ifdef FEAT_RELTIME
17837 /* Set the time limit, if there is one. */
17838 profile_setlimit(time_limit, &tm);
17839#endif
17840
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017841 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17842 * start/middle/end (pat3, for the top pair). */
17843 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17844 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17845 if (pat2 == NULL || pat3 == NULL)
17846 goto theend;
17847 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17848 if (*mpat == NUL)
17849 STRCPY(pat3, pat2);
17850 else
17851 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17852 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017853 if (flags & SP_START)
17854 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017855
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 save_cursor = curwin->w_cursor;
17857 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017858 clearpos(&firstpos);
17859 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 pat = pat3;
17861 for (;;)
17862 {
17863 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017864 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17866 /* didn't find it or found the first match again: FAIL */
17867 break;
17868
17869 if (firstpos.lnum == 0)
17870 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017871 if (equalpos(pos, foundpos))
17872 {
17873 /* Found the same position again. Can happen with a pattern that
17874 * has "\zs" at the end and searching backwards. Advance one
17875 * character and try again. */
17876 if (dir == BACKWARD)
17877 decl(&pos);
17878 else
17879 incl(&pos);
17880 }
17881 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017883 /* clear the start flag to avoid getting stuck here */
17884 options &= ~SEARCH_START;
17885
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 /* If the skip pattern matches, ignore this match. */
17887 if (*skip != NUL)
17888 {
17889 save_pos = curwin->w_cursor;
17890 curwin->w_cursor = pos;
17891 r = eval_to_bool(skip, &err, NULL, FALSE);
17892 curwin->w_cursor = save_pos;
17893 if (err)
17894 {
17895 /* Evaluating {skip} caused an error, break here. */
17896 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017897 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017898 break;
17899 }
17900 if (r)
17901 continue;
17902 }
17903
17904 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17905 {
17906 /* Found end when searching backwards or start when searching
17907 * forward: nested pair. */
17908 ++nest;
17909 pat = pat2; /* nested, don't search for middle */
17910 }
17911 else
17912 {
17913 /* Found end when searching forward or start when searching
17914 * backward: end of (nested) pair; or found middle in outer pair. */
17915 if (--nest == 1)
17916 pat = pat3; /* outer level, search for middle */
17917 }
17918
17919 if (nest == 0)
17920 {
17921 /* Found the match: return matchcount or line number. */
17922 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017923 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017925 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017926 if (flags & SP_SETPCMARK)
17927 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 curwin->w_cursor = pos;
17929 if (!(flags & SP_REPEAT))
17930 break;
17931 nest = 1; /* search for next unmatched */
17932 }
17933 }
17934
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017935 if (match_pos != NULL)
17936 {
17937 /* Store the match cursor position */
17938 match_pos->lnum = curwin->w_cursor.lnum;
17939 match_pos->col = curwin->w_cursor.col + 1;
17940 }
17941
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017943 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944 curwin->w_cursor = save_cursor;
17945
17946theend:
17947 vim_free(pat2);
17948 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017949 if (p_cpo == empty_option)
17950 p_cpo = save_cpo;
17951 else
17952 /* Darn, evaluating the {skip} expression changed the value. */
17953 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017954
17955 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956}
17957
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017958/*
17959 * "searchpos()" function
17960 */
17961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017962f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017963{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017964 pos_T match_pos;
17965 int lnum = 0;
17966 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017967 int n;
17968 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017969
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017970 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017971 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017972
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017973 n = search_cmn(argvars, &match_pos, &flags);
17974 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017975 {
17976 lnum = match_pos.lnum;
17977 col = match_pos.col;
17978 }
17979
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017980 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17981 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017982 if (flags & SP_SUBPAT)
17983 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017984}
17985
Bram Moolenaar0d660222005-01-07 21:51:51 +000017986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017987f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017989#ifdef FEAT_CLIENTSERVER
17990 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017991 char_u *server = get_tv_string_chk(&argvars[0]);
17992 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993
Bram Moolenaar0d660222005-01-07 21:51:51 +000017994 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017995 if (server == NULL || reply == NULL)
17996 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017997 if (check_restricted() || check_secure())
17998 return;
17999# ifdef FEAT_X11
18000 if (check_connection() == FAIL)
18001 return;
18002# endif
18003
18004 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018006 EMSG(_("E258: Unable to send to client"));
18007 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018009 rettv->vval.v_number = 0;
18010#else
18011 rettv->vval.v_number = -1;
18012#endif
18013}
18014
Bram Moolenaar0d660222005-01-07 21:51:51 +000018015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018016f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018017{
18018 char_u *r = NULL;
18019
18020#ifdef FEAT_CLIENTSERVER
18021# ifdef WIN32
18022 r = serverGetVimNames();
18023# else
18024 make_connection();
18025 if (X_DISPLAY != NULL)
18026 r = serverGetVimNames(X_DISPLAY);
18027# endif
18028#endif
18029 rettv->v_type = VAR_STRING;
18030 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031}
18032
18033/*
18034 * "setbufvar()" function
18035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018037f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038{
18039 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018042 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 char_u nbuf[NUMBUFLEN];
18044
18045 if (check_restricted() || check_secure())
18046 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018047 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18048 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018049 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 varp = &argvars[2];
18051
18052 if (buf != NULL && varname != NULL && varp != NULL)
18053 {
18054 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056
18057 if (*varname == '&')
18058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018059 long numval;
18060 char_u *strval;
18061 int error = FALSE;
18062
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018064 numval = get_tv_number_chk(varp, &error);
18065 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018066 if (!error && strval != NULL)
18067 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 }
18069 else
18070 {
18071 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18072 if (bufvarname != NULL)
18073 {
18074 STRCPY(bufvarname, "b:");
18075 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018076 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 vim_free(bufvarname);
18078 }
18079 }
18080
18081 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084}
18085
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018087f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018088{
18089 dict_T *d;
18090 dictitem_T *di;
18091 char_u *csearch;
18092
18093 if (argvars[0].v_type != VAR_DICT)
18094 {
18095 EMSG(_(e_dictreq));
18096 return;
18097 }
18098
18099 if ((d = argvars[0].vval.v_dict) != NULL)
18100 {
18101 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18102 if (csearch != NULL)
18103 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018104#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018105 if (enc_utf8)
18106 {
18107 int pcc[MAX_MCO];
18108 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018109
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018110 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18111 }
18112 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018113#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018114 set_last_csearch(PTR2CHAR(csearch),
18115 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018116 }
18117
18118 di = dict_find(d, (char_u *)"forward", -1);
18119 if (di != NULL)
18120 set_csearch_direction(get_tv_number(&di->di_tv)
18121 ? FORWARD : BACKWARD);
18122
18123 di = dict_find(d, (char_u *)"until", -1);
18124 if (di != NULL)
18125 set_csearch_until(!!get_tv_number(&di->di_tv));
18126 }
18127}
18128
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129/*
18130 * "setcmdpos()" function
18131 */
18132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018133f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018135 int pos = (int)get_tv_number(&argvars[0]) - 1;
18136
18137 if (pos >= 0)
18138 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139}
18140
18141/*
18142 * "setline()" function
18143 */
18144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018145f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146{
18147 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018148 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018149 list_T *l = NULL;
18150 listitem_T *li = NULL;
18151 long added = 0;
18152 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018154 lnum = get_tv_lnum(&argvars[0]);
18155 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018157 l = argvars[1].vval.v_list;
18158 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018160 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018161 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018162
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018163 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018164 for (;;)
18165 {
18166 if (l != NULL)
18167 {
18168 /* list argument, get next string */
18169 if (li == NULL)
18170 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018171 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018172 li = li->li_next;
18173 }
18174
18175 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018176 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018177 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018178
18179 /* When coming here from Insert mode, sync undo, so that this can be
18180 * undone separately from what was previously inserted. */
18181 if (u_sync_once == 2)
18182 {
18183 u_sync_once = 1; /* notify that u_sync() was called */
18184 u_sync(TRUE);
18185 }
18186
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018187 if (lnum <= curbuf->b_ml.ml_line_count)
18188 {
18189 /* existing line, replace it */
18190 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18191 {
18192 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018193 if (lnum == curwin->w_cursor.lnum)
18194 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018195 rettv->vval.v_number = 0; /* OK */
18196 }
18197 }
18198 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18199 {
18200 /* lnum is one past the last line, append the line */
18201 ++added;
18202 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18203 rettv->vval.v_number = 0; /* OK */
18204 }
18205
18206 if (l == NULL) /* only one string argument */
18207 break;
18208 ++lnum;
18209 }
18210
18211 if (added > 0)
18212 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213}
18214
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018215static 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 +000018216
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018218 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018219 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018221set_qf_ll_list(
18222 win_T *wp UNUSED,
18223 typval_T *list_arg UNUSED,
18224 typval_T *action_arg UNUSED,
18225 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018226{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018227#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018228 char_u *act;
18229 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018230#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018231
Bram Moolenaar2641f772005-03-25 21:58:17 +000018232 rettv->vval.v_number = -1;
18233
18234#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018235 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018236 EMSG(_(e_listreq));
18237 else
18238 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018239 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018240
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018241 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018242 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018243 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018244 if (act == NULL)
18245 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018246 if (*act == 'a' || *act == 'r')
18247 action = *act;
18248 }
18249
Bram Moolenaar81484f42012-12-05 15:16:47 +010018250 if (l != NULL && set_errorlist(wp, l, action,
18251 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018252 rettv->vval.v_number = 0;
18253 }
18254#endif
18255}
18256
18257/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018258 * "setloclist()" function
18259 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018261f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018262{
18263 win_T *win;
18264
18265 rettv->vval.v_number = -1;
18266
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018267 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018268 if (win != NULL)
18269 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18270}
18271
18272/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018273 * "setmatches()" function
18274 */
18275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018276f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018277{
18278#ifdef FEAT_SEARCH_EXTRA
18279 list_T *l;
18280 listitem_T *li;
18281 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018282 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018283
18284 rettv->vval.v_number = -1;
18285 if (argvars[0].v_type != VAR_LIST)
18286 {
18287 EMSG(_(e_listreq));
18288 return;
18289 }
18290 if ((l = argvars[0].vval.v_list) != NULL)
18291 {
18292
18293 /* To some extent make sure that we are dealing with a list from
18294 * "getmatches()". */
18295 li = l->lv_first;
18296 while (li != NULL)
18297 {
18298 if (li->li_tv.v_type != VAR_DICT
18299 || (d = li->li_tv.vval.v_dict) == NULL)
18300 {
18301 EMSG(_(e_invarg));
18302 return;
18303 }
18304 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018305 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18306 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018307 && dict_find(d, (char_u *)"priority", -1) != NULL
18308 && dict_find(d, (char_u *)"id", -1) != NULL))
18309 {
18310 EMSG(_(e_invarg));
18311 return;
18312 }
18313 li = li->li_next;
18314 }
18315
18316 clear_matches(curwin);
18317 li = l->lv_first;
18318 while (li != NULL)
18319 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018320 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018321 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018322 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018323 char_u *group;
18324 int priority;
18325 int id;
18326 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018327
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018328 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018329 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18330 {
18331 if (s == NULL)
18332 {
18333 s = list_alloc();
18334 if (s == NULL)
18335 return;
18336 }
18337
18338 /* match from matchaddpos() */
18339 for (i = 1; i < 9; i++)
18340 {
18341 sprintf((char *)buf, (char *)"pos%d", i);
18342 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18343 {
18344 if (di->di_tv.v_type != VAR_LIST)
18345 return;
18346
18347 list_append_tv(s, &di->di_tv);
18348 s->lv_refcount++;
18349 }
18350 else
18351 break;
18352 }
18353 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018354
18355 group = get_dict_string(d, (char_u *)"group", FALSE);
18356 priority = (int)get_dict_number(d, (char_u *)"priority");
18357 id = (int)get_dict_number(d, (char_u *)"id");
18358 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18359 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18360 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018361 if (i == 0)
18362 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018363 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018364 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018365 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018366 }
18367 else
18368 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018369 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018370 list_unref(s);
18371 s = NULL;
18372 }
18373
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018374 li = li->li_next;
18375 }
18376 rettv->vval.v_number = 0;
18377 }
18378#endif
18379}
18380
18381/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018382 * "setpos()" function
18383 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018385f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018386{
18387 pos_T pos;
18388 int fnum;
18389 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018390 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018391
Bram Moolenaar08250432008-02-13 11:42:46 +000018392 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018393 name = get_tv_string_chk(argvars);
18394 if (name != NULL)
18395 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018396 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018397 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018398 if (--pos.col < 0)
18399 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018400 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018401 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018402 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018403 if (fnum == curbuf->b_fnum)
18404 {
18405 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018406 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018407 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018408 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018409 curwin->w_set_curswant = FALSE;
18410 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018411 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018412 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018413 }
18414 else
18415 EMSG(_(e_invarg));
18416 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018417 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18418 {
18419 /* set mark */
18420 if (setmark_pos(name[1], &pos, fnum) == OK)
18421 rettv->vval.v_number = 0;
18422 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018423 else
18424 EMSG(_(e_invarg));
18425 }
18426 }
18427}
18428
18429/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018430 * "setqflist()" function
18431 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018433f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018434{
18435 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18436}
18437
18438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 * "setreg()" function
18440 */
18441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018442f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443{
18444 int regname;
18445 char_u *strregname;
18446 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018447 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 int append;
18449 char_u yank_type;
18450 long block_len;
18451
18452 block_len = -1;
18453 yank_type = MAUTO;
18454 append = FALSE;
18455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018456 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018457 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018459 if (strregname == NULL)
18460 return; /* type error; errmsg already given */
18461 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462 if (regname == 0 || regname == '@')
18463 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018465 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018467 stropt = get_tv_string_chk(&argvars[2]);
18468 if (stropt == NULL)
18469 return; /* type error */
18470 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471 switch (*stropt)
18472 {
18473 case 'a': case 'A': /* append */
18474 append = TRUE;
18475 break;
18476 case 'v': case 'c': /* character-wise selection */
18477 yank_type = MCHAR;
18478 break;
18479 case 'V': case 'l': /* line-wise selection */
18480 yank_type = MLINE;
18481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018482 case 'b': case Ctrl_V: /* block-wise selection */
18483 yank_type = MBLOCK;
18484 if (VIM_ISDIGIT(stropt[1]))
18485 {
18486 ++stropt;
18487 block_len = getdigits(&stropt) - 1;
18488 --stropt;
18489 }
18490 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 }
18492 }
18493
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018494 if (argvars[1].v_type == VAR_LIST)
18495 {
18496 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018497 char_u **allocval;
18498 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018499 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018500 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018501 int len = argvars[1].vval.v_list->lv_len;
18502 listitem_T *li;
18503
Bram Moolenaar7d647822014-04-05 21:28:56 +020018504 /* First half: use for pointers to result lines; second half: use for
18505 * pointers to allocated copies. */
18506 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018507 if (lstval == NULL)
18508 return;
18509 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018510 allocval = lstval + len + 2;
18511 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018512
18513 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18514 li = li->li_next)
18515 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018516 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018517 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018518 goto free_lstval;
18519 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018520 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018521 /* Need to make a copy, next get_tv_string_buf_chk() will
18522 * overwrite the string. */
18523 strval = vim_strsave(buf);
18524 if (strval == NULL)
18525 goto free_lstval;
18526 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018527 }
18528 *curval++ = strval;
18529 }
18530 *curval++ = NULL;
18531
18532 write_reg_contents_lst(regname, lstval, -1,
18533 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018534free_lstval:
18535 while (curallocval > allocval)
18536 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018537 vim_free(lstval);
18538 }
18539 else
18540 {
18541 strval = get_tv_string_chk(&argvars[1]);
18542 if (strval == NULL)
18543 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018544 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018546 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548}
18549
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018550/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018551 * "settabvar()" function
18552 */
18553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018554f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018555{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018556#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018557 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018558 tabpage_T *tp;
18559#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018560 char_u *varname, *tabvarname;
18561 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018562
18563 rettv->vval.v_number = 0;
18564
18565 if (check_restricted() || check_secure())
18566 return;
18567
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018568#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018569 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018570#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018571 varname = get_tv_string_chk(&argvars[1]);
18572 varp = &argvars[2];
18573
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018574 if (varname != NULL && varp != NULL
18575#ifdef FEAT_WINDOWS
18576 && tp != NULL
18577#endif
18578 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018579 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018580#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018581 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018582 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018583#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018584
18585 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18586 if (tabvarname != NULL)
18587 {
18588 STRCPY(tabvarname, "t:");
18589 STRCPY(tabvarname + 2, varname);
18590 set_var(tabvarname, varp, TRUE);
18591 vim_free(tabvarname);
18592 }
18593
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018594#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018595 /* Restore current tabpage */
18596 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018597 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018598#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018599 }
18600}
18601
18602/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018603 * "settabwinvar()" function
18604 */
18605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018606f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018607{
18608 setwinvar(argvars, rettv, 1);
18609}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018610
18611/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018612 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018615f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018617 setwinvar(argvars, rettv, 0);
18618}
18619
18620/*
18621 * "setwinvar()" and "settabwinvar()" functions
18622 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018623
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018625setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018626{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 win_T *win;
18628#ifdef FEAT_WINDOWS
18629 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018630 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018631 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632#endif
18633 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018634 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018636 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637
18638 if (check_restricted() || check_secure())
18639 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018640
18641#ifdef FEAT_WINDOWS
18642 if (off == 1)
18643 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18644 else
18645 tp = curtab;
18646#endif
18647 win = find_win_by_nr(&argvars[off], tp);
18648 varname = get_tv_string_chk(&argvars[off + 1]);
18649 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650
18651 if (win != NULL && varname != NULL && varp != NULL)
18652 {
18653#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018654 need_switch_win = !(tp == curtab && win == curwin);
18655 if (!need_switch_win
18656 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018659 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018661 long numval;
18662 char_u *strval;
18663 int error = FALSE;
18664
18665 ++varname;
18666 numval = get_tv_number_chk(varp, &error);
18667 strval = get_tv_string_buf_chk(varp, nbuf);
18668 if (!error && strval != NULL)
18669 set_option_value(varname, numval, strval, OPT_LOCAL);
18670 }
18671 else
18672 {
18673 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18674 if (winvarname != NULL)
18675 {
18676 STRCPY(winvarname, "w:");
18677 STRCPY(winvarname + 2, varname);
18678 set_var(winvarname, varp, TRUE);
18679 vim_free(winvarname);
18680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 }
18682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018684 if (need_switch_win)
18685 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686#endif
18687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688}
18689
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018690#ifdef FEAT_CRYPT
18691/*
18692 * "sha256({string})" function
18693 */
18694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018695f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018696{
18697 char_u *p;
18698
18699 p = get_tv_string(&argvars[0]);
18700 rettv->vval.v_string = vim_strsave(
18701 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18702 rettv->v_type = VAR_STRING;
18703}
18704#endif /* FEAT_CRYPT */
18705
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018707 * "shellescape({string})" function
18708 */
18709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018710f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018711{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018712 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018713 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018714 rettv->v_type = VAR_STRING;
18715}
18716
18717/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018718 * shiftwidth() function
18719 */
18720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018721f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018722{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018723 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018724}
18725
18726/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018727 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 */
18729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018730f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018732 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733
Bram Moolenaar0d660222005-01-07 21:51:51 +000018734 p = get_tv_string(&argvars[0]);
18735 rettv->vval.v_string = vim_strsave(p);
18736 simplify_filename(rettv->vval.v_string); /* simplify in place */
18737 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738}
18739
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018740#ifdef FEAT_FLOAT
18741/*
18742 * "sin()" function
18743 */
18744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018745f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018746{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018747 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018748
18749 rettv->v_type = VAR_FLOAT;
18750 if (get_float_arg(argvars, &f) == OK)
18751 rettv->vval.v_float = sin(f);
18752 else
18753 rettv->vval.v_float = 0.0;
18754}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018755
18756/*
18757 * "sinh()" function
18758 */
18759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018760f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018761{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018762 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018763
18764 rettv->v_type = VAR_FLOAT;
18765 if (get_float_arg(argvars, &f) == OK)
18766 rettv->vval.v_float = sinh(f);
18767 else
18768 rettv->vval.v_float = 0.0;
18769}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018770#endif
18771
Bram Moolenaar0d660222005-01-07 21:51:51 +000018772static int
18773#ifdef __BORLANDC__
18774 _RTLENTRYF
18775#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018776 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018777static int
18778#ifdef __BORLANDC__
18779 _RTLENTRYF
18780#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018781 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018782
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018783/* struct used in the array that's given to qsort() */
18784typedef struct
18785{
18786 listitem_T *item;
18787 int idx;
18788} sortItem_T;
18789
Bram Moolenaar0d660222005-01-07 21:51:51 +000018790static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018791static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018792static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018793#ifdef FEAT_FLOAT
18794static int item_compare_float;
18795#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018796static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018797static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018798static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018799static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018800static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018801#define ITEM_COMPARE_FAIL 999
18802
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018804 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018806 static int
18807#ifdef __BORLANDC__
18808_RTLENTRYF
18809#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018810item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018812 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018813 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018814 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018815 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018816 int res;
18817 char_u numbuf1[NUMBUFLEN];
18818 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018820 si1 = (sortItem_T *)s1;
18821 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018822 tv1 = &si1->item->li_tv;
18823 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018824
18825 if (item_compare_numbers)
18826 {
18827 long v1 = get_tv_number(tv1);
18828 long v2 = get_tv_number(tv2);
18829
18830 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18831 }
18832
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018833#ifdef FEAT_FLOAT
18834 if (item_compare_float)
18835 {
18836 float_T v1 = get_tv_float(tv1);
18837 float_T v2 = get_tv_float(tv2);
18838
18839 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18840 }
18841#endif
18842
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018843 /* tv2string() puts quotes around a string and allocates memory. Don't do
18844 * that for string variables. Use a single quote when comparing with a
18845 * non-string to do what the docs promise. */
18846 if (tv1->v_type == VAR_STRING)
18847 {
18848 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18849 p1 = (char_u *)"'";
18850 else
18851 p1 = tv1->vval.v_string;
18852 }
18853 else
18854 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18855 if (tv2->v_type == VAR_STRING)
18856 {
18857 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18858 p2 = (char_u *)"'";
18859 else
18860 p2 = tv2->vval.v_string;
18861 }
18862 else
18863 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018864 if (p1 == NULL)
18865 p1 = (char_u *)"";
18866 if (p2 == NULL)
18867 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018868 if (!item_compare_numeric)
18869 {
18870 if (item_compare_ic)
18871 res = STRICMP(p1, p2);
18872 else
18873 res = STRCMP(p1, p2);
18874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018876 {
18877 double n1, n2;
18878 n1 = strtod((char *)p1, (char **)&p1);
18879 n2 = strtod((char *)p2, (char **)&p2);
18880 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18881 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018882
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018883 /* When the result would be zero, compare the item indexes. Makes the
18884 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018885 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018886 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018887
Bram Moolenaar0d660222005-01-07 21:51:51 +000018888 vim_free(tofree1);
18889 vim_free(tofree2);
18890 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891}
18892
18893 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018894#ifdef __BORLANDC__
18895_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018897item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018898{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018899 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018900 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018901 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018902 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018903 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018905 /* shortcut after failure in previous call; compare all items equal */
18906 if (item_compare_func_err)
18907 return 0;
18908
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018909 si1 = (sortItem_T *)s1;
18910 si2 = (sortItem_T *)s2;
18911
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018912 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018913 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018914 copy_tv(&si1->item->li_tv, &argv[0]);
18915 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018916
18917 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018918 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018919 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18920 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018921 clear_tv(&argv[0]);
18922 clear_tv(&argv[1]);
18923
18924 if (res == FAIL)
18925 res = ITEM_COMPARE_FAIL;
18926 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018927 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18928 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018929 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018930 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018931
18932 /* When the result would be zero, compare the pointers themselves. Makes
18933 * the sort stable. */
18934 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018935 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018936
Bram Moolenaar0d660222005-01-07 21:51:51 +000018937 return res;
18938}
18939
18940/*
18941 * "sort({list})" function
18942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018944do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945{
Bram Moolenaar33570922005-01-25 22:26:29 +000018946 list_T *l;
18947 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018948 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018949 long len;
18950 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951
Bram Moolenaar0d660222005-01-07 21:51:51 +000018952 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018953 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 else
18955 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018957 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018958 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18959 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018960 return;
18961 rettv->vval.v_list = l;
18962 rettv->v_type = VAR_LIST;
18963 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964
Bram Moolenaar0d660222005-01-07 21:51:51 +000018965 len = list_len(l);
18966 if (len <= 1)
18967 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968
Bram Moolenaar0d660222005-01-07 21:51:51 +000018969 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018970 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018971 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018972#ifdef FEAT_FLOAT
18973 item_compare_float = FALSE;
18974#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018975 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018976 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018977 if (argvars[1].v_type != VAR_UNKNOWN)
18978 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018979 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018980 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018981 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018982 else
18983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018984 int error = FALSE;
18985
18986 i = get_tv_number_chk(&argvars[1], &error);
18987 if (error)
18988 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018989 if (i == 1)
18990 item_compare_ic = TRUE;
18991 else
18992 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018993 if (item_compare_func != NULL)
18994 {
18995 if (STRCMP(item_compare_func, "n") == 0)
18996 {
18997 item_compare_func = NULL;
18998 item_compare_numeric = TRUE;
18999 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019000 else if (STRCMP(item_compare_func, "N") == 0)
19001 {
19002 item_compare_func = NULL;
19003 item_compare_numbers = TRUE;
19004 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019005#ifdef FEAT_FLOAT
19006 else if (STRCMP(item_compare_func, "f") == 0)
19007 {
19008 item_compare_func = NULL;
19009 item_compare_float = TRUE;
19010 }
19011#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020019012 else if (STRCMP(item_compare_func, "i") == 0)
19013 {
19014 item_compare_func = NULL;
19015 item_compare_ic = TRUE;
19016 }
19017 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019018 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019019
19020 if (argvars[2].v_type != VAR_UNKNOWN)
19021 {
19022 /* optional third argument: {dict} */
19023 if (argvars[2].v_type != VAR_DICT)
19024 {
19025 EMSG(_(e_dictreq));
19026 return;
19027 }
19028 item_compare_selfdict = argvars[2].vval.v_dict;
19029 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031
Bram Moolenaar0d660222005-01-07 21:51:51 +000019032 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019033 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019034 if (ptrs == NULL)
19035 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019036
Bram Moolenaar327aa022014-03-25 18:24:23 +010019037 i = 0;
19038 if (sort)
19039 {
19040 /* sort(): ptrs will be the list to sort */
19041 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019042 {
19043 ptrs[i].item = li;
19044 ptrs[i].idx = i;
19045 ++i;
19046 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019047
19048 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019049 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019050 /* test the compare function */
19051 if (item_compare_func != NULL
19052 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019054 EMSG(_("E702: Sort compare function failed"));
19055 else
19056 {
19057 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019058 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010019059 item_compare_func == NULL ? item_compare : item_compare2);
19060
19061 if (!item_compare_func_err)
19062 {
19063 /* Clear the List and append the items in sorted order. */
19064 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19065 l->lv_len = 0;
19066 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019067 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019068 }
19069 }
19070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019071 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019072 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019073 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019074
19075 /* f_uniq(): ptrs will be a stack of items to remove */
19076 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019077 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019078 item_compare_func_ptr = item_compare_func
19079 ? item_compare2 : item_compare;
19080
19081 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19082 li = li->li_next)
19083 {
19084 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19085 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019086 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019087 if (item_compare_func_err)
19088 {
19089 EMSG(_("E882: Uniq compare function failed"));
19090 break;
19091 }
19092 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019094 if (!item_compare_func_err)
19095 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019096 while (--i >= 0)
19097 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019098 li = ptrs[i].item->li_next;
19099 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019100 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019101 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019102 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019103 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019104 list_fix_watch(l, li);
19105 listitem_free(li);
19106 l->lv_len--;
19107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019108 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019109 }
19110
19111 vim_free(ptrs);
19112 }
19113}
19114
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019115/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019116 * "sort({list})" function
19117 */
19118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019119f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019120{
19121 do_sort_uniq(argvars, rettv, TRUE);
19122}
19123
19124/*
19125 * "uniq({list})" function
19126 */
19127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019128f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019129{
19130 do_sort_uniq(argvars, rettv, FALSE);
19131}
19132
19133/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019134 * "soundfold({word})" function
19135 */
19136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019137f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019138{
19139 char_u *s;
19140
19141 rettv->v_type = VAR_STRING;
19142 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019143#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019144 rettv->vval.v_string = eval_soundfold(s);
19145#else
19146 rettv->vval.v_string = vim_strsave(s);
19147#endif
19148}
19149
19150/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019151 * "spellbadword()" function
19152 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019154f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019155{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019156 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019157 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019158 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019159
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019160 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019161 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019162
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019163#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019164 if (argvars[0].v_type == VAR_UNKNOWN)
19165 {
19166 /* Find the start and length of the badly spelled word. */
19167 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19168 if (len != 0)
19169 word = ml_get_cursor();
19170 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019171 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019172 {
19173 char_u *str = get_tv_string_chk(&argvars[0]);
19174 int capcol = -1;
19175
19176 if (str != NULL)
19177 {
19178 /* Check the argument for spelling. */
19179 while (*str != NUL)
19180 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019181 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019182 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019183 {
19184 word = str;
19185 break;
19186 }
19187 str += len;
19188 }
19189 }
19190 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019191#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019192
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019193 list_append_string(rettv->vval.v_list, word, len);
19194 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019195 attr == HLF_SPB ? "bad" :
19196 attr == HLF_SPR ? "rare" :
19197 attr == HLF_SPL ? "local" :
19198 attr == HLF_SPC ? "caps" :
19199 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019200}
19201
19202/*
19203 * "spellsuggest()" function
19204 */
19205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019206f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019207{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019208#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019209 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019210 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019211 int maxcount;
19212 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019213 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019214 listitem_T *li;
19215 int need_capital = FALSE;
19216#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019217
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019218 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019219 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019220
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019221#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019222 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019223 {
19224 str = get_tv_string(&argvars[0]);
19225 if (argvars[1].v_type != VAR_UNKNOWN)
19226 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019227 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019228 if (maxcount <= 0)
19229 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019230 if (argvars[2].v_type != VAR_UNKNOWN)
19231 {
19232 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19233 if (typeerr)
19234 return;
19235 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019236 }
19237 else
19238 maxcount = 25;
19239
Bram Moolenaar4770d092006-01-12 23:22:24 +000019240 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019241
19242 for (i = 0; i < ga.ga_len; ++i)
19243 {
19244 str = ((char_u **)ga.ga_data)[i];
19245
19246 li = listitem_alloc();
19247 if (li == NULL)
19248 vim_free(str);
19249 else
19250 {
19251 li->li_tv.v_type = VAR_STRING;
19252 li->li_tv.v_lock = 0;
19253 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019254 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019255 }
19256 }
19257 ga_clear(&ga);
19258 }
19259#endif
19260}
19261
Bram Moolenaar0d660222005-01-07 21:51:51 +000019262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019263f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019264{
19265 char_u *str;
19266 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019267 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019268 regmatch_T regmatch;
19269 char_u patbuf[NUMBUFLEN];
19270 char_u *save_cpo;
19271 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019272 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019273 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019274 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019275
19276 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19277 save_cpo = p_cpo;
19278 p_cpo = (char_u *)"";
19279
19280 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019281 if (argvars[1].v_type != VAR_UNKNOWN)
19282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019283 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19284 if (pat == NULL)
19285 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019286 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019287 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019288 }
19289 if (pat == NULL || *pat == NUL)
19290 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019291
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019292 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019294 if (typeerr)
19295 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296
Bram Moolenaar0d660222005-01-07 21:51:51 +000019297 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19298 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019300 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019301 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019302 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019303 if (*str == NUL)
19304 match = FALSE; /* empty item at the end */
19305 else
19306 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019307 if (match)
19308 end = regmatch.startp[0];
19309 else
19310 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019311 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19312 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019313 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019314 if (list_append_string(rettv->vval.v_list, str,
19315 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019316 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019317 }
19318 if (!match)
19319 break;
19320 /* Advance to just after the match. */
19321 if (regmatch.endp[0] > str)
19322 col = 0;
19323 else
19324 {
19325 /* Don't get stuck at the same match. */
19326#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019327 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019328#else
19329 col = 1;
19330#endif
19331 }
19332 str = regmatch.endp[0];
19333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334
Bram Moolenaar473de612013-06-08 18:19:48 +020019335 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337
Bram Moolenaar0d660222005-01-07 21:51:51 +000019338 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339}
19340
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019341#ifdef FEAT_FLOAT
19342/*
19343 * "sqrt()" function
19344 */
19345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019346f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019347{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019348 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019349
19350 rettv->v_type = VAR_FLOAT;
19351 if (get_float_arg(argvars, &f) == OK)
19352 rettv->vval.v_float = sqrt(f);
19353 else
19354 rettv->vval.v_float = 0.0;
19355}
19356
19357/*
19358 * "str2float()" function
19359 */
19360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019361f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019362{
19363 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19364
19365 if (*p == '+')
19366 p = skipwhite(p + 1);
19367 (void)string2float(p, &rettv->vval.v_float);
19368 rettv->v_type = VAR_FLOAT;
19369}
19370#endif
19371
Bram Moolenaar2c932302006-03-18 21:42:09 +000019372/*
19373 * "str2nr()" function
19374 */
19375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019376f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019377{
19378 int base = 10;
19379 char_u *p;
19380 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019381 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019382
19383 if (argvars[1].v_type != VAR_UNKNOWN)
19384 {
19385 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019386 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019387 {
19388 EMSG(_(e_invarg));
19389 return;
19390 }
19391 }
19392
19393 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019394 if (*p == '+')
19395 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019396 switch (base)
19397 {
19398 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19399 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19400 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19401 default: what = 0;
19402 }
19403 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019404 rettv->vval.v_number = n;
19405}
19406
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407#ifdef HAVE_STRFTIME
19408/*
19409 * "strftime({format}[, {time}])" function
19410 */
19411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019412f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413{
19414 char_u result_buf[256];
19415 struct tm *curtime;
19416 time_t seconds;
19417 char_u *p;
19418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019419 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019421 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019422 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423 seconds = time(NULL);
19424 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019425 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 curtime = localtime(&seconds);
19427 /* MSVC returns NULL for an invalid value of seconds. */
19428 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019429 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 else
19431 {
19432# ifdef FEAT_MBYTE
19433 vimconv_T conv;
19434 char_u *enc;
19435
19436 conv.vc_type = CONV_NONE;
19437 enc = enc_locale();
19438 convert_setup(&conv, p_enc, enc);
19439 if (conv.vc_type != CONV_NONE)
19440 p = string_convert(&conv, p, NULL);
19441# endif
19442 if (p != NULL)
19443 (void)strftime((char *)result_buf, sizeof(result_buf),
19444 (char *)p, curtime);
19445 else
19446 result_buf[0] = NUL;
19447
19448# ifdef FEAT_MBYTE
19449 if (conv.vc_type != CONV_NONE)
19450 vim_free(p);
19451 convert_setup(&conv, enc, p_enc);
19452 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019453 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 else
19455# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019456 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457
19458# ifdef FEAT_MBYTE
19459 /* Release conversion descriptors */
19460 convert_setup(&conv, NULL, NULL);
19461 vim_free(enc);
19462# endif
19463 }
19464}
19465#endif
19466
19467/*
19468 * "stridx()" function
19469 */
19470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019471f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472{
19473 char_u buf[NUMBUFLEN];
19474 char_u *needle;
19475 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019476 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019478 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019480 needle = get_tv_string_chk(&argvars[1]);
19481 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019482 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019483 if (needle == NULL || haystack == NULL)
19484 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485
Bram Moolenaar33570922005-01-25 22:26:29 +000019486 if (argvars[2].v_type != VAR_UNKNOWN)
19487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019488 int error = FALSE;
19489
19490 start_idx = get_tv_number_chk(&argvars[2], &error);
19491 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019492 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019493 if (start_idx >= 0)
19494 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019495 }
19496
19497 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19498 if (pos != NULL)
19499 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500}
19501
19502/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019503 * "string()" function
19504 */
19505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019506f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019507{
19508 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019509 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019511 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019512 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019513 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019514 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019515 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516}
19517
19518/*
19519 * "strlen()" function
19520 */
19521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019522f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019524 rettv->vval.v_number = (varnumber_T)(STRLEN(
19525 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526}
19527
19528/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019529 * "strchars()" function
19530 */
19531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019532f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019533{
19534 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019535 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019536#ifdef FEAT_MBYTE
19537 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019538 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019539#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019540
19541 if (argvars[1].v_type != VAR_UNKNOWN)
19542 skipcc = get_tv_number_chk(&argvars[1], NULL);
19543 if (skipcc < 0 || skipcc > 1)
19544 EMSG(_(e_invarg));
19545 else
19546 {
19547#ifdef FEAT_MBYTE
19548 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19549 while (*s != NUL)
19550 {
19551 func_mb_ptr2char_adv(&s);
19552 ++len;
19553 }
19554 rettv->vval.v_number = len;
19555#else
19556 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19557#endif
19558 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019559}
19560
19561/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019562 * "strdisplaywidth()" function
19563 */
19564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019565f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019566{
19567 char_u *s = get_tv_string(&argvars[0]);
19568 int col = 0;
19569
19570 if (argvars[1].v_type != VAR_UNKNOWN)
19571 col = get_tv_number(&argvars[1]);
19572
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019573 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019574}
19575
19576/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019577 * "strwidth()" function
19578 */
19579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019580f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019581{
19582 char_u *s = get_tv_string(&argvars[0]);
19583
19584 rettv->vval.v_number = (varnumber_T)(
19585#ifdef FEAT_MBYTE
19586 mb_string2cells(s, -1)
19587#else
19588 STRLEN(s)
19589#endif
19590 );
19591}
19592
19593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 * "strpart()" function
19595 */
19596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019597f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598{
19599 char_u *p;
19600 int n;
19601 int len;
19602 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019603 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019605 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 slen = (int)STRLEN(p);
19607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019608 n = get_tv_number_chk(&argvars[1], &error);
19609 if (error)
19610 len = 0;
19611 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019612 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 else
19614 len = slen - n; /* default len: all bytes that are available. */
19615
19616 /*
19617 * Only return the overlap between the specified part and the actual
19618 * string.
19619 */
19620 if (n < 0)
19621 {
19622 len += n;
19623 n = 0;
19624 }
19625 else if (n > slen)
19626 n = slen;
19627 if (len < 0)
19628 len = 0;
19629 else if (n + len > slen)
19630 len = slen - n;
19631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019632 rettv->v_type = VAR_STRING;
19633 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634}
19635
19636/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019637 * "strridx()" function
19638 */
19639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019640f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019641{
19642 char_u buf[NUMBUFLEN];
19643 char_u *needle;
19644 char_u *haystack;
19645 char_u *rest;
19646 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019647 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019648
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019649 needle = get_tv_string_chk(&argvars[1]);
19650 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019651
19652 rettv->vval.v_number = -1;
19653 if (needle == NULL || haystack == NULL)
19654 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019655
19656 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019657 if (argvars[2].v_type != VAR_UNKNOWN)
19658 {
19659 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019660 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019661 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019662 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019663 }
19664 else
19665 end_idx = haystack_len;
19666
Bram Moolenaar0d660222005-01-07 21:51:51 +000019667 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019668 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019669 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019670 lastmatch = haystack + end_idx;
19671 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019672 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019673 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019674 for (rest = haystack; *rest != '\0'; ++rest)
19675 {
19676 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019677 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019678 break;
19679 lastmatch = rest;
19680 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019681 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019682
19683 if (lastmatch == NULL)
19684 rettv->vval.v_number = -1;
19685 else
19686 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19687}
19688
19689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690 * "strtrans()" function
19691 */
19692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019693f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019695 rettv->v_type = VAR_STRING;
19696 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697}
19698
19699/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019700 * "submatch()" function
19701 */
19702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019703f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019704{
Bram Moolenaar41571762014-04-02 19:00:58 +020019705 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019706 int no;
19707 int retList = 0;
19708
19709 no = (int)get_tv_number_chk(&argvars[0], &error);
19710 if (error)
19711 return;
19712 error = FALSE;
19713 if (argvars[1].v_type != VAR_UNKNOWN)
19714 retList = get_tv_number_chk(&argvars[1], &error);
19715 if (error)
19716 return;
19717
19718 if (retList == 0)
19719 {
19720 rettv->v_type = VAR_STRING;
19721 rettv->vval.v_string = reg_submatch(no);
19722 }
19723 else
19724 {
19725 rettv->v_type = VAR_LIST;
19726 rettv->vval.v_list = reg_submatch_list(no);
19727 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019728}
19729
19730/*
19731 * "substitute()" function
19732 */
19733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019734f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019735{
19736 char_u patbuf[NUMBUFLEN];
19737 char_u subbuf[NUMBUFLEN];
19738 char_u flagsbuf[NUMBUFLEN];
19739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019740 char_u *str = get_tv_string_chk(&argvars[0]);
19741 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19742 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19743 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19744
Bram Moolenaar0d660222005-01-07 21:51:51 +000019745 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019746 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19747 rettv->vval.v_string = NULL;
19748 else
19749 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019750}
19751
19752/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019753 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019756f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757{
19758 int id = 0;
19759#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019760 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 long col;
19762 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019763 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019765 lnum = get_tv_lnum(argvars); /* -1 on type error */
19766 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19767 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019769 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019770 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019771 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772#endif
19773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019774 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775}
19776
19777/*
19778 * "synIDattr(id, what [, mode])" function
19779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019781f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782{
19783 char_u *p = NULL;
19784#ifdef FEAT_SYN_HL
19785 int id;
19786 char_u *what;
19787 char_u *mode;
19788 char_u modebuf[NUMBUFLEN];
19789 int modec;
19790
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019791 id = get_tv_number(&argvars[0]);
19792 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019793 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019795 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019797 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 modec = 0; /* replace invalid with current */
19799 }
19800 else
19801 {
19802#ifdef FEAT_GUI
19803 if (gui.in_use)
19804 modec = 'g';
19805 else
19806#endif
19807 if (t_colors > 1)
19808 modec = 'c';
19809 else
19810 modec = 't';
19811 }
19812
19813
19814 switch (TOLOWER_ASC(what[0]))
19815 {
19816 case 'b':
19817 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19818 p = highlight_color(id, what, modec);
19819 else /* bold */
19820 p = highlight_has_attr(id, HL_BOLD, modec);
19821 break;
19822
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019823 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824 p = highlight_color(id, what, modec);
19825 break;
19826
19827 case 'i':
19828 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19829 p = highlight_has_attr(id, HL_INVERSE, modec);
19830 else /* italic */
19831 p = highlight_has_attr(id, HL_ITALIC, modec);
19832 break;
19833
19834 case 'n': /* name */
19835 p = get_highlight_name(NULL, id - 1);
19836 break;
19837
19838 case 'r': /* reverse */
19839 p = highlight_has_attr(id, HL_INVERSE, modec);
19840 break;
19841
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019842 case 's':
19843 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19844 p = highlight_color(id, what, modec);
19845 else /* standout */
19846 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 break;
19848
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019849 case 'u':
19850 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19851 /* underline */
19852 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19853 else
19854 /* undercurl */
19855 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 break;
19857 }
19858
19859 if (p != NULL)
19860 p = vim_strsave(p);
19861#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019862 rettv->v_type = VAR_STRING;
19863 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864}
19865
19866/*
19867 * "synIDtrans(id)" function
19868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019870f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871{
19872 int id;
19873
19874#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019875 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876
19877 if (id > 0)
19878 id = syn_get_final_id(id);
19879 else
19880#endif
19881 id = 0;
19882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019883 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884}
19885
19886/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019887 * "synconcealed(lnum, col)" function
19888 */
19889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019890f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019891{
19892#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19893 long lnum;
19894 long col;
19895 int syntax_flags = 0;
19896 int cchar;
19897 int matchid = 0;
19898 char_u str[NUMBUFLEN];
19899#endif
19900
19901 rettv->v_type = VAR_LIST;
19902 rettv->vval.v_list = NULL;
19903
19904#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19905 lnum = get_tv_lnum(argvars); /* -1 on type error */
19906 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19907
19908 vim_memset(str, NUL, sizeof(str));
19909
19910 if (rettv_list_alloc(rettv) != FAIL)
19911 {
19912 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19913 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19914 && curwin->w_p_cole > 0)
19915 {
19916 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19917 syntax_flags = get_syntax_info(&matchid);
19918
19919 /* get the conceal character */
19920 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19921 {
19922 cchar = syn_get_sub_char();
19923 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19924 cchar = lcs_conceal;
19925 if (cchar != NUL)
19926 {
19927# ifdef FEAT_MBYTE
19928 if (has_mbyte)
19929 (*mb_char2bytes)(cchar, str);
19930 else
19931# endif
19932 str[0] = cchar;
19933 }
19934 }
19935 }
19936
19937 list_append_number(rettv->vval.v_list,
19938 (syntax_flags & HL_CONCEAL) != 0);
19939 /* -1 to auto-determine strlen */
19940 list_append_string(rettv->vval.v_list, str, -1);
19941 list_append_number(rettv->vval.v_list, matchid);
19942 }
19943#endif
19944}
19945
19946/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019947 * "synstack(lnum, col)" function
19948 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019950f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019951{
19952#ifdef FEAT_SYN_HL
19953 long lnum;
19954 long col;
19955 int i;
19956 int id;
19957#endif
19958
19959 rettv->v_type = VAR_LIST;
19960 rettv->vval.v_list = NULL;
19961
19962#ifdef FEAT_SYN_HL
19963 lnum = get_tv_lnum(argvars); /* -1 on type error */
19964 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19965
19966 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019967 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019968 && rettv_list_alloc(rettv) != FAIL)
19969 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019970 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019971 for (i = 0; ; ++i)
19972 {
19973 id = syn_get_stack_item(i);
19974 if (id < 0)
19975 break;
19976 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19977 break;
19978 }
19979 }
19980#endif
19981}
19982
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019984get_cmd_output_as_rettv(
19985 typval_T *argvars,
19986 typval_T *rettv,
19987 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019989 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019991 char_u *infile = NULL;
19992 char_u buf[NUMBUFLEN];
19993 int err = FALSE;
19994 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019995 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019996 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019998 rettv->v_type = VAR_STRING;
19999 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020000 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020001 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020002
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020003 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020004 {
20005 /*
20006 * Write the string to a temp file, to be used for input of the shell
20007 * command.
20008 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020009 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020010 {
20011 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020012 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020013 }
20014
20015 fd = mch_fopen((char *)infile, WRITEBIN);
20016 if (fd == NULL)
20017 {
20018 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020019 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020020 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020021 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020022 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020023 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20024 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020025 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020026 else
20027 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020028 size_t len;
20029
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020030 p = get_tv_string_buf_chk(&argvars[1], buf);
20031 if (p == NULL)
20032 {
20033 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020034 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020035 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020036 len = STRLEN(p);
20037 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020038 err = TRUE;
20039 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020040 if (fclose(fd) != 0)
20041 err = TRUE;
20042 if (err)
20043 {
20044 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020045 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020046 }
20047 }
20048
Bram Moolenaar52a72462014-08-29 15:53:52 +020020049 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20050 * echoes typeahead, that messes up the display. */
20051 if (!msg_silent)
20052 flags += SHELL_COOKED;
20053
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020054 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020056 int len;
20057 listitem_T *li;
20058 char_u *s = NULL;
20059 char_u *start;
20060 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020061 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062
Bram Moolenaar52a72462014-08-29 15:53:52 +020020063 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020064 if (res == NULL)
20065 goto errret;
20066
20067 list = list_alloc();
20068 if (list == NULL)
20069 goto errret;
20070
20071 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020073 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020074 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020075 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020076 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020077
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020078 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020079 if (s == NULL)
20080 goto errret;
20081
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020082 for (p = s; start < end; ++p, ++start)
20083 *p = *start == NUL ? NL : *start;
20084 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020085
20086 li = listitem_alloc();
20087 if (li == NULL)
20088 {
20089 vim_free(s);
20090 goto errret;
20091 }
20092 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020093 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020094 li->li_tv.vval.v_string = s;
20095 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020097
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020098 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020099 rettv->v_type = VAR_LIST;
20100 rettv->vval.v_list = list;
20101 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020103 else
20104 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020105 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020106#ifdef USE_CR
20107 /* translate <CR> into <NL> */
20108 if (res != NULL)
20109 {
20110 char_u *s;
20111
20112 for (s = res; *s; ++s)
20113 {
20114 if (*s == CAR)
20115 *s = NL;
20116 }
20117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118#else
20119# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020120 /* translate <CR><NL> into <NL> */
20121 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020123 char_u *s, *d;
20124
20125 d = res;
20126 for (s = res; *s; ++s)
20127 {
20128 if (s[0] == CAR && s[1] == NL)
20129 ++s;
20130 *d++ = *s;
20131 }
20132 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134# endif
20135#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020136 rettv->vval.v_string = res;
20137 res = NULL;
20138 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020139
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020140errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020141 if (infile != NULL)
20142 {
20143 mch_remove(infile);
20144 vim_free(infile);
20145 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020146 if (res != NULL)
20147 vim_free(res);
20148 if (list != NULL)
20149 list_free(list, TRUE);
20150}
20151
20152/*
20153 * "system()" function
20154 */
20155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020156f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020157{
20158 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20159}
20160
20161/*
20162 * "systemlist()" function
20163 */
20164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020165f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020166{
20167 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168}
20169
20170/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020171 * "tabpagebuflist()" function
20172 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020174f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020175{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020176#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020177 tabpage_T *tp;
20178 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020179
20180 if (argvars[0].v_type == VAR_UNKNOWN)
20181 wp = firstwin;
20182 else
20183 {
20184 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20185 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020186 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020187 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020188 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020189 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020190 for (; wp != NULL; wp = wp->w_next)
20191 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020192 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020193 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020194 }
20195#endif
20196}
20197
20198
20199/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020200 * "tabpagenr()" function
20201 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020203f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020204{
20205 int nr = 1;
20206#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020207 char_u *arg;
20208
20209 if (argvars[0].v_type != VAR_UNKNOWN)
20210 {
20211 arg = get_tv_string_chk(&argvars[0]);
20212 nr = 0;
20213 if (arg != NULL)
20214 {
20215 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020216 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020217 else
20218 EMSG2(_(e_invexpr2), arg);
20219 }
20220 }
20221 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020222 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020223#endif
20224 rettv->vval.v_number = nr;
20225}
20226
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020227
20228#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020229static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020230
20231/*
20232 * Common code for tabpagewinnr() and winnr().
20233 */
20234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020235get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020236{
20237 win_T *twin;
20238 int nr = 1;
20239 win_T *wp;
20240 char_u *arg;
20241
20242 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20243 if (argvar->v_type != VAR_UNKNOWN)
20244 {
20245 arg = get_tv_string_chk(argvar);
20246 if (arg == NULL)
20247 nr = 0; /* type error; errmsg already given */
20248 else if (STRCMP(arg, "$") == 0)
20249 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20250 else if (STRCMP(arg, "#") == 0)
20251 {
20252 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20253 if (twin == NULL)
20254 nr = 0;
20255 }
20256 else
20257 {
20258 EMSG2(_(e_invexpr2), arg);
20259 nr = 0;
20260 }
20261 }
20262
20263 if (nr > 0)
20264 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20265 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020266 {
20267 if (wp == NULL)
20268 {
20269 /* didn't find it in this tabpage */
20270 nr = 0;
20271 break;
20272 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020273 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020274 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020275 return nr;
20276}
20277#endif
20278
20279/*
20280 * "tabpagewinnr()" function
20281 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020283f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020284{
20285 int nr = 1;
20286#ifdef FEAT_WINDOWS
20287 tabpage_T *tp;
20288
20289 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20290 if (tp == NULL)
20291 nr = 0;
20292 else
20293 nr = get_winnr(tp, &argvars[1]);
20294#endif
20295 rettv->vval.v_number = nr;
20296}
20297
20298
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020299/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020300 * "tagfiles()" function
20301 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020303f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020304{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020305 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020306 tagname_T tn;
20307 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020308
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020309 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020310 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020311 fname = alloc(MAXPATHL);
20312 if (fname == NULL)
20313 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020314
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020315 for (first = TRUE; ; first = FALSE)
20316 if (get_tagfname(&tn, first, fname) == FAIL
20317 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020318 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020319 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020320 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020321}
20322
20323/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020324 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020325 */
20326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020327f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020328{
20329 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020330
20331 tag_pattern = get_tv_string(&argvars[0]);
20332
20333 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020334 if (*tag_pattern == NUL)
20335 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020336
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020337 if (rettv_list_alloc(rettv) == OK)
20338 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020339}
20340
20341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 * "tempname()" function
20343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020345f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346{
20347 static int x = 'A';
20348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020349 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020350 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351
20352 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20353 * names. Skip 'I' and 'O', they are used for shell redirection. */
20354 do
20355 {
20356 if (x == 'Z')
20357 x = '0';
20358 else if (x == '9')
20359 x = 'A';
20360 else
20361 {
20362#ifdef EBCDIC
20363 if (x == 'I')
20364 x = 'J';
20365 else if (x == 'R')
20366 x = 'S';
20367 else
20368#endif
20369 ++x;
20370 }
20371 } while (x == 'I' || x == 'O');
20372}
20373
20374/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020375 * "test(list)" function: Just checking the walls...
20376 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020378f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020379{
20380 /* Used for unit testing. Change the code below to your liking. */
20381#if 0
20382 listitem_T *li;
20383 list_T *l;
20384 char_u *bad, *good;
20385
20386 if (argvars[0].v_type != VAR_LIST)
20387 return;
20388 l = argvars[0].vval.v_list;
20389 if (l == NULL)
20390 return;
20391 li = l->lv_first;
20392 if (li == NULL)
20393 return;
20394 bad = get_tv_string(&li->li_tv);
20395 li = li->li_next;
20396 if (li == NULL)
20397 return;
20398 good = get_tv_string(&li->li_tv);
20399 rettv->vval.v_number = test_edit_score(bad, good);
20400#endif
20401}
20402
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020403#ifdef FEAT_FLOAT
20404/*
20405 * "tan()" function
20406 */
20407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020408f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020409{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020410 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020411
20412 rettv->v_type = VAR_FLOAT;
20413 if (get_float_arg(argvars, &f) == OK)
20414 rettv->vval.v_float = tan(f);
20415 else
20416 rettv->vval.v_float = 0.0;
20417}
20418
20419/*
20420 * "tanh()" function
20421 */
20422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020423f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020424{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020425 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020426
20427 rettv->v_type = VAR_FLOAT;
20428 if (get_float_arg(argvars, &f) == OK)
20429 rettv->vval.v_float = tanh(f);
20430 else
20431 rettv->vval.v_float = 0.0;
20432}
20433#endif
20434
Bram Moolenaard52d9742005-08-21 22:20:28 +000020435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 * "tolower(string)" function
20437 */
20438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020439f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440{
20441 char_u *p;
20442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020443 p = vim_strsave(get_tv_string(&argvars[0]));
20444 rettv->v_type = VAR_STRING;
20445 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446
20447 if (p != NULL)
20448 while (*p != NUL)
20449 {
20450#ifdef FEAT_MBYTE
20451 int l;
20452
20453 if (enc_utf8)
20454 {
20455 int c, lc;
20456
20457 c = utf_ptr2char(p);
20458 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020459 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 /* TODO: reallocate string when byte count changes. */
20461 if (utf_char2len(lc) == l)
20462 utf_char2bytes(lc, p);
20463 p += l;
20464 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020465 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 p += l; /* skip multi-byte character */
20467 else
20468#endif
20469 {
20470 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20471 ++p;
20472 }
20473 }
20474}
20475
20476/*
20477 * "toupper(string)" function
20478 */
20479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020480f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020482 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020483 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484}
20485
20486/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020487 * "tr(string, fromstr, tostr)" function
20488 */
20489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020490f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020491{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020492 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020493 char_u *fromstr;
20494 char_u *tostr;
20495 char_u *p;
20496#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020497 int inlen;
20498 int fromlen;
20499 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020500 int idx;
20501 char_u *cpstr;
20502 int cplen;
20503 int first = TRUE;
20504#endif
20505 char_u buf[NUMBUFLEN];
20506 char_u buf2[NUMBUFLEN];
20507 garray_T ga;
20508
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020509 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020510 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20511 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020512
20513 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020514 rettv->v_type = VAR_STRING;
20515 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020516 if (fromstr == NULL || tostr == NULL)
20517 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020518 ga_init2(&ga, (int)sizeof(char), 80);
20519
20520#ifdef FEAT_MBYTE
20521 if (!has_mbyte)
20522#endif
20523 /* not multi-byte: fromstr and tostr must be the same length */
20524 if (STRLEN(fromstr) != STRLEN(tostr))
20525 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020526#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020527error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020528#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020529 EMSG2(_(e_invarg2), fromstr);
20530 ga_clear(&ga);
20531 return;
20532 }
20533
20534 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020535 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020536 {
20537#ifdef FEAT_MBYTE
20538 if (has_mbyte)
20539 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020540 inlen = (*mb_ptr2len)(in_str);
20541 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020542 cplen = inlen;
20543 idx = 0;
20544 for (p = fromstr; *p != NUL; p += fromlen)
20545 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020546 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020547 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020548 {
20549 for (p = tostr; *p != NUL; p += tolen)
20550 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020551 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020552 if (idx-- == 0)
20553 {
20554 cplen = tolen;
20555 cpstr = p;
20556 break;
20557 }
20558 }
20559 if (*p == NUL) /* tostr is shorter than fromstr */
20560 goto error;
20561 break;
20562 }
20563 ++idx;
20564 }
20565
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020566 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020567 {
20568 /* Check that fromstr and tostr have the same number of
20569 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020570 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020571 first = FALSE;
20572 for (p = tostr; *p != NUL; p += tolen)
20573 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020574 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020575 --idx;
20576 }
20577 if (idx != 0)
20578 goto error;
20579 }
20580
Bram Moolenaarcde88542015-08-11 19:14:00 +020020581 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020582 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020583 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020584
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020585 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020586 }
20587 else
20588#endif
20589 {
20590 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020591 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020592 if (p != NULL)
20593 ga_append(&ga, tostr[p - fromstr]);
20594 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020595 ga_append(&ga, *in_str);
20596 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020597 }
20598 }
20599
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020600 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020601 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020602 ga_append(&ga, NUL);
20603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020604 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020605}
20606
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020607#ifdef FEAT_FLOAT
20608/*
20609 * "trunc({float})" function
20610 */
20611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020612f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020613{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020614 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020615
20616 rettv->v_type = VAR_FLOAT;
20617 if (get_float_arg(argvars, &f) == OK)
20618 /* trunc() is not in C90, use floor() or ceil() instead. */
20619 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20620 else
20621 rettv->vval.v_float = 0.0;
20622}
20623#endif
20624
Bram Moolenaar8299df92004-07-10 09:47:34 +000020625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 * "type(expr)" function
20627 */
20628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020629f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020631 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020632
20633 switch (argvars[0].v_type)
20634 {
20635 case VAR_NUMBER: n = 0; break;
20636 case VAR_STRING: n = 1; break;
20637 case VAR_FUNC: n = 2; break;
20638 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020639 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020640 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020641 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020642 if (argvars[0].vval.v_number == VVAL_FALSE
20643 || argvars[0].vval.v_number == VVAL_TRUE)
20644 n = 6;
20645 else
20646 n = 7;
20647 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020648 case VAR_JOB: n = 8; break;
20649 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020650 case VAR_UNKNOWN:
20651 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20652 n = -1;
20653 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020654 }
20655 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656}
20657
20658/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020659 * "undofile(name)" function
20660 */
20661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020662f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020663{
20664 rettv->v_type = VAR_STRING;
20665#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020666 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020667 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020668
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020669 if (*fname == NUL)
20670 {
20671 /* If there is no file name there will be no undo file. */
20672 rettv->vval.v_string = NULL;
20673 }
20674 else
20675 {
20676 char_u *ffname = FullName_save(fname, FALSE);
20677
20678 if (ffname != NULL)
20679 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20680 vim_free(ffname);
20681 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020682 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020683#else
20684 rettv->vval.v_string = NULL;
20685#endif
20686}
20687
20688/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020689 * "undotree()" function
20690 */
20691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020692f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020693{
20694 if (rettv_dict_alloc(rettv) == OK)
20695 {
20696 dict_T *dict = rettv->vval.v_dict;
20697 list_T *list;
20698
Bram Moolenaar730cde92010-06-27 05:18:54 +020020699 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020700 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020701 dict_add_nr_str(dict, "save_last",
20702 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020703 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20704 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020705 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020706
20707 list = list_alloc();
20708 if (list != NULL)
20709 {
20710 u_eval_tree(curbuf->b_u_oldhead, list);
20711 dict_add_list(dict, "entries", list);
20712 }
20713 }
20714}
20715
20716/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020717 * "values(dict)" function
20718 */
20719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020720f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020721{
20722 dict_list(argvars, rettv, 1);
20723}
20724
20725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 * "virtcol(string)" function
20727 */
20728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020729f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730{
20731 colnr_T vcol = 0;
20732 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020733 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020735 fp = var2fpos(&argvars[0], FALSE, &fnum);
20736 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20737 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 {
20739 getvvcol(curwin, fp, NULL, NULL, &vcol);
20740 ++vcol;
20741 }
20742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020743 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744}
20745
20746/*
20747 * "visualmode()" function
20748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020750f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752 char_u str[2];
20753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020754 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755 str[0] = curbuf->b_visual_mode_eval;
20756 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020757 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020758
20759 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020760 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762}
20763
20764/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020765 * "wildmenumode()" function
20766 */
20767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020768f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020769{
20770#ifdef FEAT_WILDMENU
20771 if (wild_menu_showing)
20772 rettv->vval.v_number = 1;
20773#endif
20774}
20775
20776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777 * "winbufnr(nr)" function
20778 */
20779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020780f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781{
20782 win_T *wp;
20783
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020784 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020788 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789}
20790
20791/*
20792 * "wincol()" function
20793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020795f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796{
20797 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020798 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799}
20800
20801/*
20802 * "winheight(nr)" function
20803 */
20804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020805f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806{
20807 win_T *wp;
20808
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020809 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020811 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020813 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814}
20815
20816/*
20817 * "winline()" function
20818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020820f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821{
20822 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020823 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824}
20825
20826/*
20827 * "winnr()" function
20828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020830f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831{
20832 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020833
Bram Moolenaar071d4272004-06-13 20:20:40 +000020834#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020835 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020837 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838}
20839
20840/*
20841 * "winrestcmd()" function
20842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020844f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845{
20846#ifdef FEAT_WINDOWS
20847 win_T *wp;
20848 int winnr = 1;
20849 garray_T ga;
20850 char_u buf[50];
20851
20852 ga_init2(&ga, (int)sizeof(char), 70);
20853 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20854 {
20855 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20856 ga_concat(&ga, buf);
20857# ifdef FEAT_VERTSPLIT
20858 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20859 ga_concat(&ga, buf);
20860# endif
20861 ++winnr;
20862 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020863 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020865 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020867 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020869 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870}
20871
20872/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020873 * "winrestview()" function
20874 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020876f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020877{
20878 dict_T *dict;
20879
20880 if (argvars[0].v_type != VAR_DICT
20881 || (dict = argvars[0].vval.v_dict) == NULL)
20882 EMSG(_(e_invarg));
20883 else
20884 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020885 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20886 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20887 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20888 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020889#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020890 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20891 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020892#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020893 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20894 {
20895 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20896 curwin->w_set_curswant = FALSE;
20897 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020898
Bram Moolenaar82c25852014-05-28 16:47:16 +020020899 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20900 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020901#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020902 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20903 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020904#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020905 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20906 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20907 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20908 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020909
20910 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020911 win_new_height(curwin, curwin->w_height);
20912# ifdef FEAT_VERTSPLIT
20913 win_new_width(curwin, W_WIDTH(curwin));
20914# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020915 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020916
Bram Moolenaarb851a962014-10-31 15:45:52 +010020917 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020918 curwin->w_topline = 1;
20919 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20920 curwin->w_topline = curbuf->b_ml.ml_line_count;
20921#ifdef FEAT_DIFF
20922 check_topfill(curwin, TRUE);
20923#endif
20924 }
20925}
20926
20927/*
20928 * "winsaveview()" function
20929 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020931f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020932{
20933 dict_T *dict;
20934
Bram Moolenaara800b422010-06-27 01:15:55 +020020935 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020936 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020937 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020938
20939 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20940 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20941#ifdef FEAT_VIRTUALEDIT
20942 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20943#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020944 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020945 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20946
20947 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20948#ifdef FEAT_DIFF
20949 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20950#endif
20951 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20952 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20953}
20954
20955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 * "winwidth(nr)" function
20957 */
20958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020959f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960{
20961 win_T *wp;
20962
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020963 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966 else
20967#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020968 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020970 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971#endif
20972}
20973
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020975 * "wordcount()" function
20976 */
20977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020978f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020979{
20980 if (rettv_dict_alloc(rettv) == FAIL)
20981 return;
20982 cursor_pos_info(rettv->vval.v_dict);
20983}
20984
20985/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020986 * Write list of strings to file
20987 */
20988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020989write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020990{
20991 listitem_T *li;
20992 int c;
20993 int ret = OK;
20994 char_u *s;
20995
20996 for (li = list->lv_first; li != NULL; li = li->li_next)
20997 {
20998 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20999 {
21000 if (*s == '\n')
21001 c = putc(NUL, fd);
21002 else
21003 c = putc(*s, fd);
21004 if (c == EOF)
21005 {
21006 ret = FAIL;
21007 break;
21008 }
21009 }
21010 if (!binary || li->li_next != NULL)
21011 if (putc('\n', fd) == EOF)
21012 {
21013 ret = FAIL;
21014 break;
21015 }
21016 if (ret == FAIL)
21017 {
21018 EMSG(_(e_write));
21019 break;
21020 }
21021 }
21022 return ret;
21023}
21024
21025/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021026 * "writefile()" function
21027 */
21028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021030{
21031 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021032 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021033 char_u *fname;
21034 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021035 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021036
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021037 if (check_restricted() || check_secure())
21038 return;
21039
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021040 if (argvars[0].v_type != VAR_LIST)
21041 {
21042 EMSG2(_(e_listarg), "writefile()");
21043 return;
21044 }
21045 if (argvars[0].vval.v_list == NULL)
21046 return;
21047
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021048 if (argvars[2].v_type != VAR_UNKNOWN)
21049 {
21050 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21051 binary = TRUE;
21052 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21053 append = TRUE;
21054 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021055
21056 /* Always open the file in binary mode, library functions have a mind of
21057 * their own about CR-LF conversion. */
21058 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021059 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21060 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021061 {
21062 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21063 ret = -1;
21064 }
21065 else
21066 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021067 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21068 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021069 fclose(fd);
21070 }
21071
21072 rettv->vval.v_number = ret;
21073}
21074
21075/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021076 * "xor(expr, expr)" function
21077 */
21078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021079f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021080{
21081 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21082 ^ get_tv_number_chk(&argvars[1], NULL);
21083}
21084
21085
21086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021088 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 */
21090 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021091var2fpos(
21092 typval_T *varp,
21093 int dollar_lnum, /* TRUE when $ is last line */
21094 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021096 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021098 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099
Bram Moolenaara5525202006-03-02 22:52:09 +000021100 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021101 if (varp->v_type == VAR_LIST)
21102 {
21103 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021104 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021105 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021106 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021107
21108 l = varp->vval.v_list;
21109 if (l == NULL)
21110 return NULL;
21111
21112 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021113 pos.lnum = list_find_nr(l, 0L, &error);
21114 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021115 return NULL; /* invalid line number */
21116
21117 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021118 pos.col = list_find_nr(l, 1L, &error);
21119 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021120 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021121 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021122
21123 /* We accept "$" for the column number: last column. */
21124 li = list_find(l, 1L);
21125 if (li != NULL && li->li_tv.v_type == VAR_STRING
21126 && li->li_tv.vval.v_string != NULL
21127 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21128 pos.col = len + 1;
21129
Bram Moolenaara5525202006-03-02 22:52:09 +000021130 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021131 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021132 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021133 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021134
Bram Moolenaara5525202006-03-02 22:52:09 +000021135#ifdef FEAT_VIRTUALEDIT
21136 /* Get the virtual offset. Defaults to zero. */
21137 pos.coladd = list_find_nr(l, 2L, &error);
21138 if (error)
21139 pos.coladd = 0;
21140#endif
21141
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021142 return &pos;
21143 }
21144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021145 name = get_tv_string_chk(varp);
21146 if (name == NULL)
21147 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021148 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021150 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21151 {
21152 if (VIsual_active)
21153 return &VIsual;
21154 return &curwin->w_cursor;
21155 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021156 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021158 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21160 return NULL;
21161 return pp;
21162 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021163
21164#ifdef FEAT_VIRTUALEDIT
21165 pos.coladd = 0;
21166#endif
21167
Bram Moolenaar477933c2007-07-17 14:32:23 +000021168 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021169 {
21170 pos.col = 0;
21171 if (name[1] == '0') /* "w0": first visible line */
21172 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021173 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021174 pos.lnum = curwin->w_topline;
21175 return &pos;
21176 }
21177 else if (name[1] == '$') /* "w$": last visible line */
21178 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021179 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021180 pos.lnum = curwin->w_botline - 1;
21181 return &pos;
21182 }
21183 }
21184 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021186 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 {
21188 pos.lnum = curbuf->b_ml.ml_line_count;
21189 pos.col = 0;
21190 }
21191 else
21192 {
21193 pos.lnum = curwin->w_cursor.lnum;
21194 pos.col = (colnr_T)STRLEN(ml_get_curline());
21195 }
21196 return &pos;
21197 }
21198 return NULL;
21199}
21200
21201/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021202 * Convert list in "arg" into a position and optional file number.
21203 * When "fnump" is NULL there is no file number, only 3 items.
21204 * Note that the column is passed on as-is, the caller may want to decrement
21205 * it to use 1 for the first column.
21206 * Return FAIL when conversion is not possible, doesn't check the position for
21207 * validity.
21208 */
21209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210list2fpos(
21211 typval_T *arg,
21212 pos_T *posp,
21213 int *fnump,
21214 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021215{
21216 list_T *l = arg->vval.v_list;
21217 long i = 0;
21218 long n;
21219
Bram Moolenaar493c1782014-05-28 14:34:46 +020021220 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21221 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021222 if (arg->v_type != VAR_LIST
21223 || l == NULL
21224 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021225 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021226 return FAIL;
21227
21228 if (fnump != NULL)
21229 {
21230 n = list_find_nr(l, i++, NULL); /* fnum */
21231 if (n < 0)
21232 return FAIL;
21233 if (n == 0)
21234 n = curbuf->b_fnum; /* current buffer */
21235 *fnump = n;
21236 }
21237
21238 n = list_find_nr(l, i++, NULL); /* lnum */
21239 if (n < 0)
21240 return FAIL;
21241 posp->lnum = n;
21242
21243 n = list_find_nr(l, i++, NULL); /* col */
21244 if (n < 0)
21245 return FAIL;
21246 posp->col = n;
21247
21248#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021249 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021250 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021251 posp->coladd = 0;
21252 else
21253 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021254#endif
21255
Bram Moolenaar493c1782014-05-28 14:34:46 +020021256 if (curswantp != NULL)
21257 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21258
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021259 return OK;
21260}
21261
21262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 * Get the length of an environment variable name.
21264 * Advance "arg" to the first character after the name.
21265 * Return 0 for error.
21266 */
21267 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021268get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269{
21270 char_u *p;
21271 int len;
21272
21273 for (p = *arg; vim_isIDc(*p); ++p)
21274 ;
21275 if (p == *arg) /* no name found */
21276 return 0;
21277
21278 len = (int)(p - *arg);
21279 *arg = p;
21280 return len;
21281}
21282
21283/*
21284 * Get the length of the name of a function or internal variable.
21285 * "arg" is advanced to the first non-white character after the name.
21286 * Return 0 if something is wrong.
21287 */
21288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021289get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290{
21291 char_u *p;
21292 int len;
21293
21294 /* Find the end of the name. */
21295 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021296 {
21297 if (*p == ':')
21298 {
21299 /* "s:" is start of "s:var", but "n:" is not and can be used in
21300 * slice "[n:]". Also "xx:" is not a namespace. */
21301 len = (int)(p - *arg);
21302 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21303 || len > 1)
21304 break;
21305 }
21306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 if (p == *arg) /* no name found */
21308 return 0;
21309
21310 len = (int)(p - *arg);
21311 *arg = skipwhite(p);
21312
21313 return len;
21314}
21315
21316/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021317 * Get the length of the name of a variable or function.
21318 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021320 * Return -1 if curly braces expansion failed.
21321 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 * If the name contains 'magic' {}'s, expand them and return the
21323 * expanded name in an allocated string via 'alias' - caller must free.
21324 */
21325 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021326get_name_len(
21327 char_u **arg,
21328 char_u **alias,
21329 int evaluate,
21330 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331{
21332 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 char_u *p;
21334 char_u *expr_start;
21335 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336
21337 *alias = NULL; /* default to no alias */
21338
21339 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21340 && (*arg)[2] == (int)KE_SNR)
21341 {
21342 /* hard coded <SNR>, already translated */
21343 *arg += 3;
21344 return get_id_len(arg) + 3;
21345 }
21346 len = eval_fname_script(*arg);
21347 if (len > 0)
21348 {
21349 /* literal "<SID>", "s:" or "<SNR>" */
21350 *arg += len;
21351 }
21352
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021354 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021356 p = find_name_end(*arg, &expr_start, &expr_end,
21357 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 if (expr_start != NULL)
21359 {
21360 char_u *temp_string;
21361
21362 if (!evaluate)
21363 {
21364 len += (int)(p - *arg);
21365 *arg = skipwhite(p);
21366 return len;
21367 }
21368
21369 /*
21370 * Include any <SID> etc in the expanded string:
21371 * Thus the -len here.
21372 */
21373 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21374 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021375 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 *alias = temp_string;
21377 *arg = skipwhite(p);
21378 return (int)STRLEN(temp_string);
21379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380
21381 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021382 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 EMSG2(_(e_invexpr2), *arg);
21384
21385 return len;
21386}
21387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021388/*
21389 * Find the end of a variable or function name, taking care of magic braces.
21390 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21391 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021392 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021393 * Return a pointer to just after the name. Equal to "arg" if there is no
21394 * valid name.
21395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021397find_name_end(
21398 char_u *arg,
21399 char_u **expr_start,
21400 char_u **expr_end,
21401 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021403 int mb_nest = 0;
21404 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021406 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021408 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021410 *expr_start = NULL;
21411 *expr_end = NULL;
21412 }
21413
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021414 /* Quick check for valid starting character. */
21415 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21416 return arg;
21417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021418 for (p = arg; *p != NUL
21419 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021420 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021421 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021422 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021423 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021424 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021425 if (*p == '\'')
21426 {
21427 /* skip over 'string' to avoid counting [ and ] inside it. */
21428 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21429 ;
21430 if (*p == NUL)
21431 break;
21432 }
21433 else if (*p == '"')
21434 {
21435 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21436 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21437 if (*p == '\\' && p[1] != NUL)
21438 ++p;
21439 if (*p == NUL)
21440 break;
21441 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021442 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21443 {
21444 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021445 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021446 len = (int)(p - arg);
21447 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021448 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021449 break;
21450 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021452 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021454 if (*p == '[')
21455 ++br_nest;
21456 else if (*p == ']')
21457 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021460 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021462 if (*p == '{')
21463 {
21464 mb_nest++;
21465 if (expr_start != NULL && *expr_start == NULL)
21466 *expr_start = p;
21467 }
21468 else if (*p == '}')
21469 {
21470 mb_nest--;
21471 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21472 *expr_end = p;
21473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 }
21476
21477 return p;
21478}
21479
21480/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021481 * Expands out the 'magic' {}'s in a variable/function name.
21482 * Note that this can call itself recursively, to deal with
21483 * constructs like foo{bar}{baz}{bam}
21484 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21485 * "in_start" ^
21486 * "expr_start" ^
21487 * "expr_end" ^
21488 * "in_end" ^
21489 *
21490 * Returns a new allocated string, which the caller must free.
21491 * Returns NULL for failure.
21492 */
21493 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021494make_expanded_name(
21495 char_u *in_start,
21496 char_u *expr_start,
21497 char_u *expr_end,
21498 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021499{
21500 char_u c1;
21501 char_u *retval = NULL;
21502 char_u *temp_result;
21503 char_u *nextcmd = NULL;
21504
21505 if (expr_end == NULL || in_end == NULL)
21506 return NULL;
21507 *expr_start = NUL;
21508 *expr_end = NUL;
21509 c1 = *in_end;
21510 *in_end = NUL;
21511
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021512 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021513 if (temp_result != NULL && nextcmd == NULL)
21514 {
21515 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21516 + (in_end - expr_end) + 1));
21517 if (retval != NULL)
21518 {
21519 STRCPY(retval, in_start);
21520 STRCAT(retval, temp_result);
21521 STRCAT(retval, expr_end + 1);
21522 }
21523 }
21524 vim_free(temp_result);
21525
21526 *in_end = c1; /* put char back for error messages */
21527 *expr_start = '{';
21528 *expr_end = '}';
21529
21530 if (retval != NULL)
21531 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021532 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021533 if (expr_start != NULL)
21534 {
21535 /* Further expansion! */
21536 temp_result = make_expanded_name(retval, expr_start,
21537 expr_end, temp_result);
21538 vim_free(retval);
21539 retval = temp_result;
21540 }
21541 }
21542
21543 return retval;
21544}
21545
21546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021548 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549 */
21550 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021551eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021553 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21554}
21555
21556/*
21557 * Return TRUE if character "c" can be used as the first character in a
21558 * variable or function name (excluding '{' and '}').
21559 */
21560 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021561eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021562{
21563 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564}
21565
21566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 * Set number v: variable to "val".
21568 */
21569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021570set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021571{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021572 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573}
21574
21575/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021576 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 */
21578 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021579get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021581 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582}
21583
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021584/*
21585 * Get string v: variable value. Uses a static buffer, can only be used once.
21586 */
21587 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021588get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021589{
21590 return get_tv_string(&vimvars[idx].vv_tv);
21591}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021592
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021594 * Get List v: variable value. Caller must take care of reference count when
21595 * needed.
21596 */
21597 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021598get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021599{
21600 return vimvars[idx].vv_list;
21601}
21602
21603/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021604 * Set v:char to character "c".
21605 */
21606 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021607set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021608{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021609 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021610
21611#ifdef FEAT_MBYTE
21612 if (has_mbyte)
21613 buf[(*mb_char2bytes)(c, buf)] = NUL;
21614 else
21615#endif
21616 {
21617 buf[0] = c;
21618 buf[1] = NUL;
21619 }
21620 set_vim_var_string(VV_CHAR, buf, -1);
21621}
21622
21623/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021624 * Set v:count to "count" and v:count1 to "count1".
21625 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 */
21627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021628set_vcount(
21629 long count,
21630 long count1,
21631 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021633 if (set_prevcount)
21634 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021635 vimvars[VV_COUNT].vv_nr = count;
21636 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637}
21638
21639/*
21640 * Set string v: variable to a copy of "val".
21641 */
21642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021643set_vim_var_string(
21644 int idx,
21645 char_u *val,
21646 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647{
Bram Moolenaara542c682016-01-31 16:28:04 +010021648 clear_tv(&vimvars[idx].vv_di.di_tv);
21649 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021651 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021653 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021655 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656}
21657
21658/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021659 * Set List v: variable to "val".
21660 */
21661 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021662set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021663{
Bram Moolenaara542c682016-01-31 16:28:04 +010021664 clear_tv(&vimvars[idx].vv_di.di_tv);
21665 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021666 vimvars[idx].vv_list = val;
21667 if (val != NULL)
21668 ++val->lv_refcount;
21669}
21670
21671/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021672 * Set Dictionary v: variable to "val".
21673 */
21674 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021675set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021676{
21677 int todo;
21678 hashitem_T *hi;
21679
Bram Moolenaara542c682016-01-31 16:28:04 +010021680 clear_tv(&vimvars[idx].vv_di.di_tv);
21681 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021682 vimvars[idx].vv_dict = val;
21683 if (val != NULL)
21684 {
21685 ++val->dv_refcount;
21686
21687 /* Set readonly */
21688 todo = (int)val->dv_hashtab.ht_used;
21689 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21690 {
21691 if (HASHITEM_EMPTY(hi))
21692 continue;
21693 --todo;
21694 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21695 }
21696 }
21697}
21698
21699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 * Set v:register if needed.
21701 */
21702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021703set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704{
21705 char_u regname;
21706
21707 if (c == 0 || c == ' ')
21708 regname = '"';
21709 else
21710 regname = c;
21711 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021712 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 set_vim_var_string(VV_REG, &regname, 1);
21714}
21715
21716/*
21717 * Get or set v:exception. If "oldval" == NULL, return the current value.
21718 * Otherwise, restore the value to "oldval" and return NULL.
21719 * Must always be called in pairs to save and restore v:exception! Does not
21720 * take care of memory allocations.
21721 */
21722 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021723v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724{
21725 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021726 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727
Bram Moolenaare9a41262005-01-15 22:18:47 +000021728 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 return NULL;
21730}
21731
21732/*
21733 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21734 * Otherwise, restore the value to "oldval" and return NULL.
21735 * Must always be called in pairs to save and restore v:throwpoint! Does not
21736 * take care of memory allocations.
21737 */
21738 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021739v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740{
21741 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021742 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743
Bram Moolenaare9a41262005-01-15 22:18:47 +000021744 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 return NULL;
21746}
21747
21748#if defined(FEAT_AUTOCMD) || defined(PROTO)
21749/*
21750 * Set v:cmdarg.
21751 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21752 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21753 * Must always be called in pairs!
21754 */
21755 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021756set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757{
21758 char_u *oldval;
21759 char_u *newval;
21760 unsigned len;
21761
Bram Moolenaare9a41262005-01-15 22:18:47 +000021762 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021763 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021765 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021766 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021767 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 }
21769
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021770 if (eap->force_bin == FORCE_BIN)
21771 len = 6;
21772 else if (eap->force_bin == FORCE_NOBIN)
21773 len = 8;
21774 else
21775 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021776
21777 if (eap->read_edit)
21778 len += 7;
21779
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021780 if (eap->force_ff != 0)
21781 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21782# ifdef FEAT_MBYTE
21783 if (eap->force_enc != 0)
21784 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021785 if (eap->bad_char != 0)
21786 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021787# endif
21788
21789 newval = alloc(len + 1);
21790 if (newval == NULL)
21791 return NULL;
21792
21793 if (eap->force_bin == FORCE_BIN)
21794 sprintf((char *)newval, " ++bin");
21795 else if (eap->force_bin == FORCE_NOBIN)
21796 sprintf((char *)newval, " ++nobin");
21797 else
21798 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021799
21800 if (eap->read_edit)
21801 STRCAT(newval, " ++edit");
21802
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021803 if (eap->force_ff != 0)
21804 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21805 eap->cmd + eap->force_ff);
21806# ifdef FEAT_MBYTE
21807 if (eap->force_enc != 0)
21808 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21809 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021810 if (eap->bad_char == BAD_KEEP)
21811 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21812 else if (eap->bad_char == BAD_DROP)
21813 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21814 else if (eap->bad_char != 0)
21815 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021816# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021817 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021818 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819}
21820#endif
21821
21822/*
21823 * Get the value of internal variable "name".
21824 * Return OK or FAIL.
21825 */
21826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021827get_var_tv(
21828 char_u *name,
21829 int len, /* length of "name" */
21830 typval_T *rettv, /* NULL when only checking existence */
21831 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21832 int verbose, /* may give error message */
21833 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834{
21835 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021836 typval_T *tv = NULL;
21837 typval_T atv;
21838 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840
21841 /* truncate the name, so that we can use strcmp() */
21842 cc = name[len];
21843 name[len] = NUL;
21844
21845 /*
21846 * Check for "b:changedtick".
21847 */
21848 if (STRCMP(name, "b:changedtick") == 0)
21849 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021850 atv.v_type = VAR_NUMBER;
21851 atv.vval.v_number = curbuf->b_changedtick;
21852 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 }
21854
21855 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 * Check for user-defined variables.
21857 */
21858 else
21859 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021860 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021862 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021864 if (dip != NULL)
21865 *dip = v;
21866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867 }
21868
Bram Moolenaare9a41262005-01-15 22:18:47 +000021869 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021871 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021872 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 ret = FAIL;
21874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021875 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021876 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877
21878 name[len] = cc;
21879
21880 return ret;
21881}
21882
21883/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021884 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21885 * Also handle function call with Funcref variable: func(expr)
21886 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21887 */
21888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021889handle_subscript(
21890 char_u **arg,
21891 typval_T *rettv,
21892 int evaluate, /* do more than finding the end */
21893 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021894{
21895 int ret = OK;
21896 dict_T *selfdict = NULL;
21897 char_u *s;
21898 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021899 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021900
21901 while (ret == OK
21902 && (**arg == '['
21903 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021904 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021905 && !vim_iswhite(*(*arg - 1)))
21906 {
21907 if (**arg == '(')
21908 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021909 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021910 if (evaluate)
21911 {
21912 functv = *rettv;
21913 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021914
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021915 /* Invoke the function. Recursive! */
21916 s = functv.vval.v_string;
21917 }
21918 else
21919 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021920 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021921 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21922 &len, evaluate, selfdict);
21923
21924 /* Clear the funcref afterwards, so that deleting it while
21925 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021926 if (evaluate)
21927 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021928
21929 /* Stop the expression evaluation when immediately aborting on
21930 * error, or when an interrupt occurred or an exception was thrown
21931 * but not caught. */
21932 if (aborting())
21933 {
21934 if (ret == OK)
21935 clear_tv(rettv);
21936 ret = FAIL;
21937 }
21938 dict_unref(selfdict);
21939 selfdict = NULL;
21940 }
21941 else /* **arg == '[' || **arg == '.' */
21942 {
21943 dict_unref(selfdict);
21944 if (rettv->v_type == VAR_DICT)
21945 {
21946 selfdict = rettv->vval.v_dict;
21947 if (selfdict != NULL)
21948 ++selfdict->dv_refcount;
21949 }
21950 else
21951 selfdict = NULL;
21952 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21953 {
21954 clear_tv(rettv);
21955 ret = FAIL;
21956 }
21957 }
21958 }
21959 dict_unref(selfdict);
21960 return ret;
21961}
21962
21963/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021964 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021965 * value).
21966 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021967 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021968alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021969{
Bram Moolenaar33570922005-01-25 22:26:29 +000021970 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021971}
21972
21973/*
21974 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 * The string "s" must have been allocated, it is consumed.
21976 * Return NULL for out of memory, the variable otherwise.
21977 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021978 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021979alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980{
Bram Moolenaar33570922005-01-25 22:26:29 +000021981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021983 rettv = alloc_tv();
21984 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021986 rettv->v_type = VAR_STRING;
21987 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 }
21989 else
21990 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021991 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992}
21993
21994/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021995 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021998free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999{
22000 if (varp != NULL)
22001 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022002 switch (varp->v_type)
22003 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022004 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022005 func_unref(varp->vval.v_string);
22006 /*FALLTHROUGH*/
22007 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022008 vim_free(varp->vval.v_string);
22009 break;
22010 case VAR_LIST:
22011 list_unref(varp->vval.v_list);
22012 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022013 case VAR_DICT:
22014 dict_unref(varp->vval.v_dict);
22015 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022016 case VAR_JOB:
22017#ifdef FEAT_JOB
22018 job_unref(varp->vval.v_job);
22019 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022020#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022021 case VAR_CHANNEL:
22022#ifdef FEAT_CHANNEL
22023 channel_unref(varp->vval.v_channel);
22024 break;
22025#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022026 case VAR_NUMBER:
22027 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022028 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022029 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022030 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 vim_free(varp);
22033 }
22034}
22035
22036/*
22037 * Free the memory for a variable value and set the value to NULL or 0.
22038 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022040clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041{
22042 if (varp != NULL)
22043 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022044 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022046 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022047 func_unref(varp->vval.v_string);
22048 /*FALLTHROUGH*/
22049 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022050 vim_free(varp->vval.v_string);
22051 varp->vval.v_string = NULL;
22052 break;
22053 case VAR_LIST:
22054 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022055 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022056 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022057 case VAR_DICT:
22058 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022059 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022060 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022061 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022062 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022063 varp->vval.v_number = 0;
22064 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022065 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022066#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022067 varp->vval.v_float = 0.0;
22068 break;
22069#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022070 case VAR_JOB:
22071#ifdef FEAT_JOB
22072 job_unref(varp->vval.v_job);
22073 varp->vval.v_job = NULL;
22074#endif
22075 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022076 case VAR_CHANNEL:
22077#ifdef FEAT_CHANNEL
22078 channel_unref(varp->vval.v_channel);
22079 varp->vval.v_channel = NULL;
22080#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022081 case VAR_UNKNOWN:
22082 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022084 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 }
22086}
22087
22088/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022089 * Set the value of a variable to NULL without freeing items.
22090 */
22091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022092init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022093{
22094 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022095 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022096}
22097
22098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 * Get the number value of a variable.
22100 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022101 * For incompatible types, return 0.
22102 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22103 * caller of incompatible types: it sets *denote to TRUE if "denote"
22104 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 */
22106 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022107get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022109 int error = FALSE;
22110
22111 return get_tv_number_chk(varp, &error); /* return 0L on error */
22112}
22113
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022114 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022115get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022116{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022117 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022119 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022121 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022122 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022123 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022124#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022125 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022126 break;
22127#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022128 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022129 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022130 break;
22131 case VAR_STRING:
22132 if (varp->vval.v_string != NULL)
22133 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022134 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022135 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022136 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022137 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022138 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022139 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022140 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022141 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022142 case VAR_SPECIAL:
22143 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22144 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022145 case VAR_JOB:
22146#ifdef FEAT_JOB
22147 EMSG(_("E910: Using a Job as a Number"));
22148 break;
22149#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022150 case VAR_CHANNEL:
22151#ifdef FEAT_CHANNEL
22152 EMSG(_("E913: Using a Channel as a Number"));
22153 break;
22154#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022155 case VAR_UNKNOWN:
22156 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022157 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022159 if (denote == NULL) /* useful for values that must be unsigned */
22160 n = -1;
22161 else
22162 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022163 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164}
22165
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022166#ifdef FEAT_FLOAT
22167 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022168get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022169{
22170 switch (varp->v_type)
22171 {
22172 case VAR_NUMBER:
22173 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022174 case VAR_FLOAT:
22175 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022176 case VAR_FUNC:
22177 EMSG(_("E891: Using a Funcref as a Float"));
22178 break;
22179 case VAR_STRING:
22180 EMSG(_("E892: Using a String as a Float"));
22181 break;
22182 case VAR_LIST:
22183 EMSG(_("E893: Using a List as a Float"));
22184 break;
22185 case VAR_DICT:
22186 EMSG(_("E894: Using a Dictionary as a Float"));
22187 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022188 case VAR_SPECIAL:
22189 EMSG(_("E907: Using a special value as a Float"));
22190 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022191 case VAR_JOB:
22192# ifdef FEAT_JOB
22193 EMSG(_("E911: Using a Job as a Float"));
22194 break;
22195# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022196 case VAR_CHANNEL:
22197# ifdef FEAT_CHANNEL
22198 EMSG(_("E914: Using a Channel as a Float"));
22199 break;
22200# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022201 case VAR_UNKNOWN:
22202 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022203 break;
22204 }
22205 return 0;
22206}
22207#endif
22208
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022210 * Get the lnum from the first argument.
22211 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022212 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 */
22214 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022215get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216{
Bram Moolenaar33570922005-01-25 22:26:29 +000022217 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218 linenr_T lnum;
22219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022220 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 if (lnum == 0) /* no valid number, try using line() */
22222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022223 rettv.v_type = VAR_NUMBER;
22224 f_line(argvars, &rettv);
22225 lnum = rettv.vval.v_number;
22226 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 }
22228 return lnum;
22229}
22230
22231/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022232 * Get the lnum from the first argument.
22233 * Also accepts "$", then "buf" is used.
22234 * Returns 0 on error.
22235 */
22236 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022237get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022238{
22239 if (argvars[0].v_type == VAR_STRING
22240 && argvars[0].vval.v_string != NULL
22241 && argvars[0].vval.v_string[0] == '$'
22242 && buf != NULL)
22243 return buf->b_ml.ml_line_count;
22244 return get_tv_number_chk(&argvars[0], NULL);
22245}
22246
22247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 * Get the string value of a variable.
22249 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022250 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22251 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252 * If the String variable has never been set, return an empty string.
22253 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022254 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22255 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256 */
22257 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022258get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259{
22260 static char_u mybuf[NUMBUFLEN];
22261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022262 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263}
22264
22265 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022266get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022268 char_u *res = get_tv_string_buf_chk(varp, buf);
22269
22270 return res != NULL ? res : (char_u *)"";
22271}
22272
Bram Moolenaar7d647822014-04-05 21:28:56 +020022273/*
22274 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22275 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022276 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022277get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022278{
22279 static char_u mybuf[NUMBUFLEN];
22280
22281 return get_tv_string_buf_chk(varp, mybuf);
22282}
22283
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022284 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022285get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022286{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022287 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022289 case VAR_NUMBER:
22290 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22291 return buf;
22292 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022293 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022294 break;
22295 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022296 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022297 break;
22298 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022299 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022300 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022301 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022302#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022303 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022304 break;
22305#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022306 case VAR_STRING:
22307 if (varp->vval.v_string != NULL)
22308 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022309 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022310 case VAR_SPECIAL:
22311 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22312 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022313 case VAR_JOB:
22314#ifdef FEAT_JOB
22315 {
22316 job_T *job = varp->vval.v_job;
22317 char *status = job->jv_status == JOB_FAILED ? "fail"
22318 : job->jv_status == JOB_ENDED ? "dead"
22319 : "run";
22320# ifdef UNIX
22321 vim_snprintf((char *)buf, NUMBUFLEN,
22322 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022323# elif defined(WIN32)
22324 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022325 "process %ld %s",
22326 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022327 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022328# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022329 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022330 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22331# endif
22332 return buf;
22333 }
22334#endif
22335 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022336 case VAR_CHANNEL:
22337#ifdef FEAT_CHANNEL
22338 {
22339 channel_T *channel = varp->vval.v_channel;
22340 char *status = channel_status(channel);
22341
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022342 if (channel == NULL)
22343 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22344 else
22345 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022346 "channel %d %s", channel->ch_id, status);
22347 return buf;
22348 }
22349#endif
22350 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022351 case VAR_UNKNOWN:
22352 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022355 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356}
22357
22358/*
22359 * Find variable "name" in the list of variables.
22360 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022361 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022362 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022363 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022365 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022366find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022369 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370
Bram Moolenaara7043832005-01-21 11:56:39 +000022371 ht = find_var_ht(name, &varname);
22372 if (htp != NULL)
22373 *htp = ht;
22374 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022376 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377}
22378
22379/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022380 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022381 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022382 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022383 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022384find_var_in_ht(
22385 hashtab_T *ht,
22386 int htname,
22387 char_u *varname,
22388 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022389{
Bram Moolenaar33570922005-01-25 22:26:29 +000022390 hashitem_T *hi;
22391
22392 if (*varname == NUL)
22393 {
22394 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022395 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022396 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022397 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022398 case 'g': return &globvars_var;
22399 case 'v': return &vimvars_var;
22400 case 'b': return &curbuf->b_bufvar;
22401 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022402#ifdef FEAT_WINDOWS
22403 case 't': return &curtab->tp_winvar;
22404#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022405 case 'l': return current_funccal == NULL
22406 ? NULL : &current_funccal->l_vars_var;
22407 case 'a': return current_funccal == NULL
22408 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022409 }
22410 return NULL;
22411 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022412
22413 hi = hash_find(ht, varname);
22414 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022415 {
22416 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022417 * worked find the variable again. Don't auto-load a script if it was
22418 * loaded already, otherwise it would be loaded every time when
22419 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022420 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022421 {
22422 /* Note: script_autoload() may make "hi" invalid. It must either
22423 * be obtained again or not used. */
22424 if (!script_autoload(varname, FALSE) || aborting())
22425 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022426 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022427 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022428 if (HASHITEM_EMPTY(hi))
22429 return NULL;
22430 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022431 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022432}
22433
22434/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022435 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022436 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022437 * Set "varname" to the start of name without ':'.
22438 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022439 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022440find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022442 hashitem_T *hi;
22443
Bram Moolenaar73627d02015-08-11 15:46:09 +020022444 if (name[0] == NUL)
22445 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 if (name[1] != ':')
22447 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022448 /* The name must not start with a colon or #. */
22449 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450 return NULL;
22451 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022452
22453 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022454 hi = hash_find(&compat_hashtab, name);
22455 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022456 return &compat_hashtab;
22457
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022459 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022460 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 }
22462 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022463 if (*name == 'g') /* global variable */
22464 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022465 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22466 */
22467 if (vim_strchr(name + 2, ':') != NULL
22468 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022469 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022471 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022473 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022474#ifdef FEAT_WINDOWS
22475 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022476 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022477#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022478 if (*name == 'v') /* v: variable */
22479 return &vimvarht;
22480 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022481 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022482 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022483 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 if (*name == 's' /* script variable */
22485 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22486 return &SCRIPT_VARS(current_SID);
22487 return NULL;
22488}
22489
22490/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022491 * Get function call environment based on bactrace debug level
22492 */
22493 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022494get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022495{
22496 int i;
22497 funccall_T *funccal;
22498 funccall_T *temp_funccal;
22499
22500 funccal = current_funccal;
22501 if (debug_backtrace_level > 0)
22502 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022503 for (i = 0; i < debug_backtrace_level; i++)
22504 {
22505 temp_funccal = funccal->caller;
22506 if (temp_funccal)
22507 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022508 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022509 /* backtrace level overflow. reset to max */
22510 debug_backtrace_level = i;
22511 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022512 }
22513 return funccal;
22514}
22515
22516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022518 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519 * Returns NULL when it doesn't exist.
22520 */
22521 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022522get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523{
Bram Moolenaar33570922005-01-25 22:26:29 +000022524 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022526 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 if (v == NULL)
22528 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022529 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530}
22531
22532/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022533 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 * sourcing this script and when executing functions defined in the script.
22535 */
22536 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022537new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538{
Bram Moolenaara7043832005-01-21 11:56:39 +000022539 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022540 hashtab_T *ht;
22541 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022542
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22544 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022545 /* Re-allocating ga_data means that an ht_array pointing to
22546 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022547 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022548 for (i = 1; i <= ga_scripts.ga_len; ++i)
22549 {
22550 ht = &SCRIPT_VARS(i);
22551 if (ht->ht_mask == HT_INIT_SIZE - 1)
22552 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022553 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022554 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022555 }
22556
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 while (ga_scripts.ga_len < id)
22558 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022559 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022560 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022561 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 }
22564 }
22565}
22566
22567/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022568 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22569 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 */
22571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022572init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573{
Bram Moolenaar33570922005-01-25 22:26:29 +000022574 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022575 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022576 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022577 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022578 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022579 dict_var->di_tv.vval.v_dict = dict;
22580 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022581 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022582 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22583 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584}
22585
22586/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022587 * Unreference a dictionary initialized by init_var_dict().
22588 */
22589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022590unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022591{
22592 /* Now the dict needs to be freed if no one else is using it, go back to
22593 * normal reference counting. */
22594 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22595 dict_unref(dict);
22596}
22597
22598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022600 * Frees all allocated variables and the value they contain.
22601 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 */
22603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022604vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022605{
22606 vars_clear_ext(ht, TRUE);
22607}
22608
22609/*
22610 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22611 */
22612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022613vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614{
Bram Moolenaara7043832005-01-21 11:56:39 +000022615 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022616 hashitem_T *hi;
22617 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618
Bram Moolenaar33570922005-01-25 22:26:29 +000022619 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022620 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022621 for (hi = ht->ht_array; todo > 0; ++hi)
22622 {
22623 if (!HASHITEM_EMPTY(hi))
22624 {
22625 --todo;
22626
Bram Moolenaar33570922005-01-25 22:26:29 +000022627 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022628 * ht_array might change then. hash_clear() takes care of it
22629 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022630 v = HI2DI(hi);
22631 if (free_val)
22632 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022633 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022634 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022635 }
22636 }
22637 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022638 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639}
22640
Bram Moolenaara7043832005-01-21 11:56:39 +000022641/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022642 * Delete a variable from hashtab "ht" at item "hi".
22643 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022646delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647{
Bram Moolenaar33570922005-01-25 22:26:29 +000022648 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022649
22650 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022651 clear_tv(&di->di_tv);
22652 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022653}
22654
22655/*
22656 * List the value of one internal variable.
22657 */
22658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022659list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022661 char_u *tofree;
22662 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022663 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022664
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022665 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022666 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022667 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022668 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669}
22670
Bram Moolenaar071d4272004-06-13 20:20:40 +000022671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022672list_one_var_a(
22673 char_u *prefix,
22674 char_u *name,
22675 int type,
22676 char_u *string,
22677 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678{
Bram Moolenaar31859182007-08-14 20:41:13 +000022679 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22680 msg_start();
22681 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022682 if (name != NULL) /* "a:" vars don't have a name stored */
22683 msg_puts(name);
22684 msg_putchar(' ');
22685 msg_advance(22);
22686 if (type == VAR_NUMBER)
22687 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022688 else if (type == VAR_FUNC)
22689 msg_putchar('*');
22690 else if (type == VAR_LIST)
22691 {
22692 msg_putchar('[');
22693 if (*string == '[')
22694 ++string;
22695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022696 else if (type == VAR_DICT)
22697 {
22698 msg_putchar('{');
22699 if (*string == '{')
22700 ++string;
22701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 else
22703 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022704
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022706
22707 if (type == VAR_FUNC)
22708 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022709 if (*first)
22710 {
22711 msg_clr_eos();
22712 *first = FALSE;
22713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714}
22715
22716/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022717 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 * If the variable already exists, the value is updated.
22719 * Otherwise the variable is created.
22720 */
22721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022722set_var(
22723 char_u *name,
22724 typval_T *tv,
22725 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726{
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022729 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022731 ht = find_var_ht(name, &varname);
22732 if (ht == NULL || *varname == NUL)
22733 {
22734 EMSG2(_(e_illvar), name);
22735 return;
22736 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022737 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022738
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022739 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22740 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022741
Bram Moolenaar33570922005-01-25 22:26:29 +000022742 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022744 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022745 if (var_check_ro(v->di_flags, name, FALSE)
22746 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022747 return;
22748 if (v->di_tv.v_type != tv->v_type
22749 && !((v->di_tv.v_type == VAR_STRING
22750 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022751 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022752 || tv->v_type == VAR_NUMBER))
22753#ifdef FEAT_FLOAT
22754 && !((v->di_tv.v_type == VAR_NUMBER
22755 || v->di_tv.v_type == VAR_FLOAT)
22756 && (tv->v_type == VAR_NUMBER
22757 || tv->v_type == VAR_FLOAT))
22758#endif
22759 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022760 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022761 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022762 return;
22763 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022764
22765 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022766 * Handle setting internal v: variables separately where needed to
22767 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022768 */
22769 if (ht == &vimvarht)
22770 {
22771 if (v->di_tv.v_type == VAR_STRING)
22772 {
22773 vim_free(v->di_tv.vval.v_string);
22774 if (copy || tv->v_type != VAR_STRING)
22775 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22776 else
22777 {
22778 /* Take over the string to avoid an extra alloc/free. */
22779 v->di_tv.vval.v_string = tv->vval.v_string;
22780 tv->vval.v_string = NULL;
22781 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022782 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022783 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022784 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022785 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022786 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022787 if (STRCMP(varname, "searchforward") == 0)
22788 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022789#ifdef FEAT_SEARCH_EXTRA
22790 else if (STRCMP(varname, "hlsearch") == 0)
22791 {
22792 no_hlsearch = !v->di_tv.vval.v_number;
22793 redraw_all_later(SOME_VALID);
22794 }
22795#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022796 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022797 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022798 else if (v->di_tv.v_type != tv->v_type)
22799 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022800 }
22801
22802 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 }
22804 else /* add a new variable */
22805 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022806 /* Can't add "v:" variable. */
22807 if (ht == &vimvarht)
22808 {
22809 EMSG2(_(e_illvar), name);
22810 return;
22811 }
22812
Bram Moolenaar92124a32005-06-17 22:03:40 +000022813 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022814 if (!valid_varname(varname))
22815 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022816
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022817 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22818 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022819 if (v == NULL)
22820 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022821 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022822 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022824 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 return;
22826 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022827 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022829
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022830 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022831 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022832 else
22833 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022834 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022835 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022836 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838}
22839
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022840/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022841 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022842 * Also give an error message.
22843 */
22844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022845var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022846{
22847 if (flags & DI_FLAGS_RO)
22848 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022849 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022850 return TRUE;
22851 }
22852 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22853 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022854 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022855 return TRUE;
22856 }
22857 return FALSE;
22858}
22859
22860/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022861 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22862 * Also give an error message.
22863 */
22864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022865var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022866{
22867 if (flags & DI_FLAGS_FIX)
22868 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022869 EMSG2(_("E795: Cannot delete variable %s"),
22870 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022871 return TRUE;
22872 }
22873 return FALSE;
22874}
22875
22876/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022877 * Check if a funcref is assigned to a valid variable name.
22878 * Return TRUE and give an error if not.
22879 */
22880 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022881var_check_func_name(
22882 char_u *name, /* points to start of variable name */
22883 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022884{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022885 /* Allow for w: b: s: and t:. */
22886 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022887 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22888 ? name[2] : name[0]))
22889 {
22890 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22891 name);
22892 return TRUE;
22893 }
22894 /* Don't allow hiding a function. When "v" is not NULL we might be
22895 * assigning another function to the same var, the type is checked
22896 * below. */
22897 if (new_var && function_exists(name))
22898 {
22899 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22900 name);
22901 return TRUE;
22902 }
22903 return FALSE;
22904}
22905
22906/*
22907 * Check if a variable name is valid.
22908 * Return FALSE and give an error if not.
22909 */
22910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022911valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022912{
22913 char_u *p;
22914
22915 for (p = varname; *p != NUL; ++p)
22916 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22917 && *p != AUTOLOAD_CHAR)
22918 {
22919 EMSG2(_(e_illvar), varname);
22920 return FALSE;
22921 }
22922 return TRUE;
22923}
22924
22925/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022926 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022927 * Also give an error message, using "name" or _("name") when use_gettext is
22928 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022929 */
22930 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022931tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022932{
22933 if (lock & VAR_LOCKED)
22934 {
22935 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022936 name == NULL ? (char_u *)_("Unknown")
22937 : use_gettext ? (char_u *)_(name)
22938 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022939 return TRUE;
22940 }
22941 if (lock & VAR_FIXED)
22942 {
22943 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022944 name == NULL ? (char_u *)_("Unknown")
22945 : use_gettext ? (char_u *)_(name)
22946 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022947 return TRUE;
22948 }
22949 return FALSE;
22950}
22951
22952/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022953 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022954 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022955 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022956 * It is OK for "from" and "to" to point to the same item. This is used to
22957 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022958 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022960copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022962 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022963 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022964 switch (from->v_type)
22965 {
22966 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022967 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022968 to->vval.v_number = from->vval.v_number;
22969 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022970 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022971#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022972 to->vval.v_float = from->vval.v_float;
22973 break;
22974#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022975 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022976#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022977 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022978 if (to->vval.v_job != NULL)
22979 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022980 break;
22981#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022982 case VAR_CHANNEL:
22983#ifdef FEAT_CHANNEL
22984 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022985 if (to->vval.v_channel != NULL)
22986 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022987 break;
22988#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022989 case VAR_STRING:
22990 case VAR_FUNC:
22991 if (from->vval.v_string == NULL)
22992 to->vval.v_string = NULL;
22993 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022994 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022995 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022996 if (from->v_type == VAR_FUNC)
22997 func_ref(to->vval.v_string);
22998 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022999 break;
23000 case VAR_LIST:
23001 if (from->vval.v_list == NULL)
23002 to->vval.v_list = NULL;
23003 else
23004 {
23005 to->vval.v_list = from->vval.v_list;
23006 ++to->vval.v_list->lv_refcount;
23007 }
23008 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023009 case VAR_DICT:
23010 if (from->vval.v_dict == NULL)
23011 to->vval.v_dict = NULL;
23012 else
23013 {
23014 to->vval.v_dict = from->vval.v_dict;
23015 ++to->vval.v_dict->dv_refcount;
23016 }
23017 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023018 case VAR_UNKNOWN:
23019 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023020 break;
23021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022}
23023
23024/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023025 * Make a copy of an item.
23026 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023027 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23028 * reference to an already copied list/dict can be used.
23029 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023030 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023031 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023032item_copy(
23033 typval_T *from,
23034 typval_T *to,
23035 int deep,
23036 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023037{
23038 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023039 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023040
Bram Moolenaar33570922005-01-25 22:26:29 +000023041 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023042 {
23043 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023044 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023045 }
23046 ++recurse;
23047
23048 switch (from->v_type)
23049 {
23050 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023051 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023052 case VAR_STRING:
23053 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023054 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023055 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023056 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023057 copy_tv(from, to);
23058 break;
23059 case VAR_LIST:
23060 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023061 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023062 if (from->vval.v_list == NULL)
23063 to->vval.v_list = NULL;
23064 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23065 {
23066 /* use the copy made earlier */
23067 to->vval.v_list = from->vval.v_list->lv_copylist;
23068 ++to->vval.v_list->lv_refcount;
23069 }
23070 else
23071 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23072 if (to->vval.v_list == NULL)
23073 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023074 break;
23075 case VAR_DICT:
23076 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023077 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023078 if (from->vval.v_dict == NULL)
23079 to->vval.v_dict = NULL;
23080 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23081 {
23082 /* use the copy made earlier */
23083 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23084 ++to->vval.v_dict->dv_refcount;
23085 }
23086 else
23087 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23088 if (to->vval.v_dict == NULL)
23089 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023090 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023091 case VAR_UNKNOWN:
23092 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023093 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023094 }
23095 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023096 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023097}
23098
23099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 * ":echo expr1 ..." print each argument separated with a space, add a
23101 * newline at the end.
23102 * ":echon expr1 ..." print each argument plain.
23103 */
23104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023105ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106{
23107 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023108 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023109 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 char_u *p;
23111 int needclr = TRUE;
23112 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023113 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114
23115 if (eap->skip)
23116 ++emsg_skip;
23117 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23118 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023119 /* If eval1() causes an error message the text from the command may
23120 * still need to be cleared. E.g., "echo 22,44". */
23121 need_clr_eos = needclr;
23122
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023124 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125 {
23126 /*
23127 * Report the invalid expression unless the expression evaluation
23128 * has been cancelled due to an aborting error, an interrupt, or an
23129 * exception.
23130 */
23131 if (!aborting())
23132 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023133 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 break;
23135 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023136 need_clr_eos = FALSE;
23137
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 if (!eap->skip)
23139 {
23140 if (atstart)
23141 {
23142 atstart = FALSE;
23143 /* Call msg_start() after eval1(), evaluating the expression
23144 * may cause a message to appear. */
23145 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023146 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023147 /* Mark the saved text as finishing the line, so that what
23148 * follows is displayed on a new line when scrolling back
23149 * at the more prompt. */
23150 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 }
23154 else if (eap->cmdidx == CMD_echo)
23155 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023156 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023157 if (p != NULL)
23158 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023159 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023160 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023162 if (*p != TAB && needclr)
23163 {
23164 /* remove any text still there from the command */
23165 msg_clr_eos();
23166 needclr = FALSE;
23167 }
23168 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 }
23170 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023171 {
23172#ifdef FEAT_MBYTE
23173 if (has_mbyte)
23174 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023175 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023176
23177 (void)msg_outtrans_len_attr(p, i, echo_attr);
23178 p += i - 1;
23179 }
23180 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023182 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023185 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023187 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 arg = skipwhite(arg);
23189 }
23190 eap->nextcmd = check_nextcmd(arg);
23191
23192 if (eap->skip)
23193 --emsg_skip;
23194 else
23195 {
23196 /* remove text that may still be there from the command */
23197 if (needclr)
23198 msg_clr_eos();
23199 if (eap->cmdidx == CMD_echo)
23200 msg_end();
23201 }
23202}
23203
23204/*
23205 * ":echohl {name}".
23206 */
23207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023208ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209{
23210 int id;
23211
23212 id = syn_name2id(eap->arg);
23213 if (id == 0)
23214 echo_attr = 0;
23215 else
23216 echo_attr = syn_id2attr(id);
23217}
23218
23219/*
23220 * ":execute expr1 ..." execute the result of an expression.
23221 * ":echomsg expr1 ..." Print a message
23222 * ":echoerr expr1 ..." Print an error
23223 * Each gets spaces around each argument and a newline at the end for
23224 * echo commands
23225 */
23226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023227ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228{
23229 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023230 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 int ret = OK;
23232 char_u *p;
23233 garray_T ga;
23234 int len;
23235 int save_did_emsg;
23236
23237 ga_init2(&ga, 1, 80);
23238
23239 if (eap->skip)
23240 ++emsg_skip;
23241 while (*arg != NUL && *arg != '|' && *arg != '\n')
23242 {
23243 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023244 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 {
23246 /*
23247 * Report the invalid expression unless the expression evaluation
23248 * has been cancelled due to an aborting error, an interrupt, or an
23249 * exception.
23250 */
23251 if (!aborting())
23252 EMSG2(_(e_invexpr2), p);
23253 ret = FAIL;
23254 break;
23255 }
23256
23257 if (!eap->skip)
23258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023259 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 len = (int)STRLEN(p);
23261 if (ga_grow(&ga, len + 2) == FAIL)
23262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023263 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264 ret = FAIL;
23265 break;
23266 }
23267 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270 ga.ga_len += len;
23271 }
23272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023273 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274 arg = skipwhite(arg);
23275 }
23276
23277 if (ret != FAIL && ga.ga_data != NULL)
23278 {
23279 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023282 out_flush();
23283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284 else if (eap->cmdidx == CMD_echoerr)
23285 {
23286 /* We don't want to abort following commands, restore did_emsg. */
23287 save_did_emsg = did_emsg;
23288 EMSG((char_u *)ga.ga_data);
23289 if (!force_abort)
23290 did_emsg = save_did_emsg;
23291 }
23292 else if (eap->cmdidx == CMD_execute)
23293 do_cmdline((char_u *)ga.ga_data,
23294 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23295 }
23296
23297 ga_clear(&ga);
23298
23299 if (eap->skip)
23300 --emsg_skip;
23301
23302 eap->nextcmd = check_nextcmd(arg);
23303}
23304
23305/*
23306 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23307 * "arg" points to the "&" or '+' when called, to "option" when returning.
23308 * Returns NULL when no option name found. Otherwise pointer to the char
23309 * after the option name.
23310 */
23311 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023312find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313{
23314 char_u *p = *arg;
23315
23316 ++p;
23317 if (*p == 'g' && p[1] == ':')
23318 {
23319 *opt_flags = OPT_GLOBAL;
23320 p += 2;
23321 }
23322 else if (*p == 'l' && p[1] == ':')
23323 {
23324 *opt_flags = OPT_LOCAL;
23325 p += 2;
23326 }
23327 else
23328 *opt_flags = 0;
23329
23330 if (!ASCII_ISALPHA(*p))
23331 return NULL;
23332 *arg = p;
23333
23334 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23335 p += 4; /* termcap option */
23336 else
23337 while (ASCII_ISALPHA(*p))
23338 ++p;
23339 return p;
23340}
23341
23342/*
23343 * ":function"
23344 */
23345 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023346ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347{
23348 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023349 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350 int j;
23351 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023353 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 char_u *name = NULL;
23355 char_u *p;
23356 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023357 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 garray_T newargs;
23359 garray_T newlines;
23360 int varargs = FALSE;
23361 int mustend = FALSE;
23362 int flags = 0;
23363 ufunc_T *fp;
23364 int indent;
23365 int nesting;
23366 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023367 dictitem_T *v;
23368 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023369 static int func_nr = 0; /* number for nameless function */
23370 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023371 hashtab_T *ht;
23372 int todo;
23373 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023374 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023375
23376 /*
23377 * ":function" without argument: list functions.
23378 */
23379 if (ends_excmd(*eap->arg))
23380 {
23381 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023382 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023383 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023384 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023385 {
23386 if (!HASHITEM_EMPTY(hi))
23387 {
23388 --todo;
23389 fp = HI2UF(hi);
23390 if (!isdigit(*fp->uf_name))
23391 list_func_head(fp, FALSE);
23392 }
23393 }
23394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 eap->nextcmd = check_nextcmd(eap->arg);
23396 return;
23397 }
23398
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023399 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023400 * ":function /pat": list functions matching pattern.
23401 */
23402 if (*eap->arg == '/')
23403 {
23404 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23405 if (!eap->skip)
23406 {
23407 regmatch_T regmatch;
23408
23409 c = *p;
23410 *p = NUL;
23411 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23412 *p = c;
23413 if (regmatch.regprog != NULL)
23414 {
23415 regmatch.rm_ic = p_ic;
23416
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023417 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023418 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23419 {
23420 if (!HASHITEM_EMPTY(hi))
23421 {
23422 --todo;
23423 fp = HI2UF(hi);
23424 if (!isdigit(*fp->uf_name)
23425 && vim_regexec(&regmatch, fp->uf_name, 0))
23426 list_func_head(fp, FALSE);
23427 }
23428 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023429 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023430 }
23431 }
23432 if (*p == '/')
23433 ++p;
23434 eap->nextcmd = check_nextcmd(p);
23435 return;
23436 }
23437
23438 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023439 * Get the function name. There are these situations:
23440 * func normal function name
23441 * "name" == func, "fudi.fd_dict" == NULL
23442 * dict.func new dictionary entry
23443 * "name" == NULL, "fudi.fd_dict" set,
23444 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23445 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023446 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023447 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23448 * dict.func existing dict entry that's not a Funcref
23449 * "name" == NULL, "fudi.fd_dict" set,
23450 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023451 * s:func script-local function name
23452 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023455 name = trans_function_name(&p, eap->skip, 0, &fudi);
23456 paren = (vim_strchr(p, '(') != NULL);
23457 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 {
23459 /*
23460 * Return on an invalid expression in braces, unless the expression
23461 * evaluation has been cancelled due to an aborting error, an
23462 * interrupt, or an exception.
23463 */
23464 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023465 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023466 if (!eap->skip && fudi.fd_newkey != NULL)
23467 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023468 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 else
23472 eap->skip = TRUE;
23473 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023474
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475 /* An error in a function call during evaluation of an expression in magic
23476 * braces should not cause the function not to be defined. */
23477 saved_did_emsg = did_emsg;
23478 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479
23480 /*
23481 * ":function func" with only function name: list function.
23482 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023483 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484 {
23485 if (!ends_excmd(*skipwhite(p)))
23486 {
23487 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023488 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 }
23490 eap->nextcmd = check_nextcmd(p);
23491 if (eap->nextcmd != NULL)
23492 *p = NUL;
23493 if (!eap->skip && !got_int)
23494 {
23495 fp = find_func(name);
23496 if (fp != NULL)
23497 {
23498 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023499 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023501 if (FUNCLINE(fp, j) == NULL)
23502 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503 msg_putchar('\n');
23504 msg_outnum((long)(j + 1));
23505 if (j < 9)
23506 msg_putchar(' ');
23507 if (j < 99)
23508 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023509 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 out_flush(); /* show a line at a time */
23511 ui_breakcheck();
23512 }
23513 if (!got_int)
23514 {
23515 msg_putchar('\n');
23516 msg_puts((char_u *)" endfunction");
23517 }
23518 }
23519 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023520 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023522 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 }
23524
23525 /*
23526 * ":function name(arg1, arg2)" Define function.
23527 */
23528 p = skipwhite(p);
23529 if (*p != '(')
23530 {
23531 if (!eap->skip)
23532 {
23533 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023534 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 }
23536 /* attempt to continue by skipping some text */
23537 if (vim_strchr(p, '(') != NULL)
23538 p = vim_strchr(p, '(');
23539 }
23540 p = skipwhite(p + 1);
23541
23542 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23543 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23544
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023545 if (!eap->skip)
23546 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023547 /* Check the name of the function. Unless it's a dictionary function
23548 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023549 if (name != NULL)
23550 arg = name;
23551 else
23552 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023553 if (arg != NULL && (fudi.fd_di == NULL
23554 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023555 {
23556 if (*arg == K_SPECIAL)
23557 j = 3;
23558 else
23559 j = 0;
23560 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23561 : eval_isnamec(arg[j])))
23562 ++j;
23563 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023564 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023565 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023566 /* Disallow using the g: dict. */
23567 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23568 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023569 }
23570
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 /*
23572 * Isolate the arguments: "arg1, arg2, ...)"
23573 */
23574 while (*p != ')')
23575 {
23576 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23577 {
23578 varargs = TRUE;
23579 p += 3;
23580 mustend = TRUE;
23581 }
23582 else
23583 {
23584 arg = p;
23585 while (ASCII_ISALNUM(*p) || *p == '_')
23586 ++p;
23587 if (arg == p || isdigit(*arg)
23588 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23589 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23590 {
23591 if (!eap->skip)
23592 EMSG2(_("E125: Illegal argument: %s"), arg);
23593 break;
23594 }
23595 if (ga_grow(&newargs, 1) == FAIL)
23596 goto erret;
23597 c = *p;
23598 *p = NUL;
23599 arg = vim_strsave(arg);
23600 if (arg == NULL)
23601 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023602
23603 /* Check for duplicate argument name. */
23604 for (i = 0; i < newargs.ga_len; ++i)
23605 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23606 {
23607 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023608 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023609 goto erret;
23610 }
23611
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23613 *p = c;
23614 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 if (*p == ',')
23616 ++p;
23617 else
23618 mustend = TRUE;
23619 }
23620 p = skipwhite(p);
23621 if (mustend && *p != ')')
23622 {
23623 if (!eap->skip)
23624 EMSG2(_(e_invarg2), eap->arg);
23625 break;
23626 }
23627 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023628 if (*p != ')')
23629 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 ++p; /* skip the ')' */
23631
Bram Moolenaare9a41262005-01-15 22:18:47 +000023632 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 for (;;)
23634 {
23635 p = skipwhite(p);
23636 if (STRNCMP(p, "range", 5) == 0)
23637 {
23638 flags |= FC_RANGE;
23639 p += 5;
23640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023641 else if (STRNCMP(p, "dict", 4) == 0)
23642 {
23643 flags |= FC_DICT;
23644 p += 4;
23645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 else if (STRNCMP(p, "abort", 5) == 0)
23647 {
23648 flags |= FC_ABORT;
23649 p += 5;
23650 }
23651 else
23652 break;
23653 }
23654
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023655 /* When there is a line break use what follows for the function body.
23656 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23657 if (*p == '\n')
23658 line_arg = p + 1;
23659 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 EMSG(_(e_trailing));
23661
23662 /*
23663 * Read the body of the function, until ":endfunction" is found.
23664 */
23665 if (KeyTyped)
23666 {
23667 /* Check if the function already exists, don't let the user type the
23668 * whole function before telling him it doesn't work! For a script we
23669 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023670 if (!eap->skip && !eap->forceit)
23671 {
23672 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23673 EMSG(_(e_funcdict));
23674 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023675 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023678 if (!eap->skip && did_emsg)
23679 goto erret;
23680
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 msg_putchar('\n'); /* don't overwrite the function name */
23682 cmdline_row = msg_row;
23683 }
23684
23685 indent = 2;
23686 nesting = 0;
23687 for (;;)
23688 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023689 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023690 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023691 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023692 saved_wait_return = FALSE;
23693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023694 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023695 sourcing_lnum_off = sourcing_lnum;
23696
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023697 if (line_arg != NULL)
23698 {
23699 /* Use eap->arg, split up in parts by line breaks. */
23700 theline = line_arg;
23701 p = vim_strchr(theline, '\n');
23702 if (p == NULL)
23703 line_arg += STRLEN(line_arg);
23704 else
23705 {
23706 *p = NUL;
23707 line_arg = p + 1;
23708 }
23709 }
23710 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023711 theline = getcmdline(':', 0L, indent);
23712 else
23713 theline = eap->getline(':', eap->cookie, indent);
23714 if (KeyTyped)
23715 lines_left = Rows - 1;
23716 if (theline == NULL)
23717 {
23718 EMSG(_("E126: Missing :endfunction"));
23719 goto erret;
23720 }
23721
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023722 /* Detect line continuation: sourcing_lnum increased more than one. */
23723 if (sourcing_lnum > sourcing_lnum_off + 1)
23724 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23725 else
23726 sourcing_lnum_off = 0;
23727
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 if (skip_until != NULL)
23729 {
23730 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23731 * don't check for ":endfunc". */
23732 if (STRCMP(theline, skip_until) == 0)
23733 {
23734 vim_free(skip_until);
23735 skip_until = NULL;
23736 }
23737 }
23738 else
23739 {
23740 /* skip ':' and blanks*/
23741 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23742 ;
23743
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023744 /* Check for "endfunction". */
23745 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023747 if (line_arg == NULL)
23748 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023749 break;
23750 }
23751
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023752 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753 * at "end". */
23754 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23755 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023756 else if (STRNCMP(p, "if", 2) == 0
23757 || STRNCMP(p, "wh", 2) == 0
23758 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 || STRNCMP(p, "try", 3) == 0)
23760 indent += 2;
23761
23762 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023763 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023765 if (*p == '!')
23766 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023768 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23769 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023771 ++nesting;
23772 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 }
23774 }
23775
23776 /* Check for ":append" or ":insert". */
23777 p = skip_range(p, NULL);
23778 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23779 || (p[0] == 'i'
23780 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23781 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23782 skip_until = vim_strsave((char_u *)".");
23783
23784 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23785 arg = skipwhite(skiptowhite(p));
23786 if (arg[0] == '<' && arg[1] =='<'
23787 && ((p[0] == 'p' && p[1] == 'y'
23788 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23789 || (p[0] == 'p' && p[1] == 'e'
23790 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23791 || (p[0] == 't' && p[1] == 'c'
23792 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023793 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23794 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23796 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023797 || (p[0] == 'm' && p[1] == 'z'
23798 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799 ))
23800 {
23801 /* ":python <<" continues until a dot, like ":append" */
23802 p = skipwhite(arg + 2);
23803 if (*p == NUL)
23804 skip_until = vim_strsave((char_u *)".");
23805 else
23806 skip_until = vim_strsave(p);
23807 }
23808 }
23809
23810 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023811 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023812 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023813 if (line_arg == NULL)
23814 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023816 }
23817
23818 /* Copy the line to newly allocated memory. get_one_sourceline()
23819 * allocates 250 bytes per line, this saves 80% on average. The cost
23820 * is an extra alloc/free. */
23821 p = vim_strsave(theline);
23822 if (p != NULL)
23823 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023824 if (line_arg == NULL)
23825 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023826 theline = p;
23827 }
23828
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023829 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23830
23831 /* Add NULL lines for continuation lines, so that the line count is
23832 * equal to the index in the growarray. */
23833 while (sourcing_lnum_off-- > 0)
23834 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023835
23836 /* Check for end of eap->arg. */
23837 if (line_arg != NULL && *line_arg == NUL)
23838 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 }
23840
23841 /* Don't define the function when skipping commands or when an error was
23842 * detected. */
23843 if (eap->skip || did_emsg)
23844 goto erret;
23845
23846 /*
23847 * If there are no errors, add the function
23848 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023849 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023850 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023851 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023852 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023853 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023854 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023855 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023856 goto erret;
23857 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023858
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023859 fp = find_func(name);
23860 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023862 if (!eap->forceit)
23863 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023864 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023865 goto erret;
23866 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023867 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023868 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023869 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023870 name);
23871 goto erret;
23872 }
23873 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023874 ga_clear_strings(&(fp->uf_args));
23875 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023876 vim_free(name);
23877 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 }
23880 else
23881 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023882 char numbuf[20];
23883
23884 fp = NULL;
23885 if (fudi.fd_newkey == NULL && !eap->forceit)
23886 {
23887 EMSG(_(e_funcdict));
23888 goto erret;
23889 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023890 if (fudi.fd_di == NULL)
23891 {
23892 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023893 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023894 goto erret;
23895 }
23896 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023897 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023898 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023899
23900 /* Give the function a sequential number. Can only be used with a
23901 * Funcref! */
23902 vim_free(name);
23903 sprintf(numbuf, "%d", ++func_nr);
23904 name = vim_strsave((char_u *)numbuf);
23905 if (name == NULL)
23906 goto erret;
23907 }
23908
23909 if (fp == NULL)
23910 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023911 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023912 {
23913 int slen, plen;
23914 char_u *scriptname;
23915
23916 /* Check that the autoload name matches the script name. */
23917 j = FAIL;
23918 if (sourcing_name != NULL)
23919 {
23920 scriptname = autoload_name(name);
23921 if (scriptname != NULL)
23922 {
23923 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023924 plen = (int)STRLEN(p);
23925 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023926 if (slen > plen && fnamecmp(p,
23927 sourcing_name + slen - plen) == 0)
23928 j = OK;
23929 vim_free(scriptname);
23930 }
23931 }
23932 if (j == FAIL)
23933 {
23934 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23935 goto erret;
23936 }
23937 }
23938
23939 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 if (fp == NULL)
23941 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023942
23943 if (fudi.fd_dict != NULL)
23944 {
23945 if (fudi.fd_di == NULL)
23946 {
23947 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023948 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023949 if (fudi.fd_di == NULL)
23950 {
23951 vim_free(fp);
23952 goto erret;
23953 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023954 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23955 {
23956 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023957 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023958 goto erret;
23959 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023960 }
23961 else
23962 /* overwrite existing dict entry */
23963 clear_tv(&fudi.fd_di->di_tv);
23964 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023965 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023966 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023967 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023968
23969 /* behave like "dict" was used */
23970 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023971 }
23972
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023974 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023975 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23976 {
23977 vim_free(fp);
23978 goto erret;
23979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023981 fp->uf_args = newargs;
23982 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023983#ifdef FEAT_PROFILE
23984 fp->uf_tml_count = NULL;
23985 fp->uf_tml_total = NULL;
23986 fp->uf_tml_self = NULL;
23987 fp->uf_profiling = FALSE;
23988 if (prof_def_func())
23989 func_do_profile(fp);
23990#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023991 fp->uf_varargs = varargs;
23992 fp->uf_flags = flags;
23993 fp->uf_calls = 0;
23994 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023995 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996
23997erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 ga_clear_strings(&newargs);
23999 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024000ret_free:
24001 vim_free(skip_until);
24002 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024005 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006}
24007
24008/*
24009 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024010 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024011 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024012 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024013 * TFN_INT: internal function name OK
24014 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024015 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 * Advances "pp" to just after the function name (if no error).
24017 */
24018 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024019trans_function_name(
24020 char_u **pp,
24021 int skip, /* only find the end, don't evaluate */
24022 int flags,
24023 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024025 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026 char_u *start;
24027 char_u *end;
24028 int lead;
24029 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024031 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024032
24033 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024034 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024035 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024036
24037 /* Check for hard coded <SNR>: already translated function ID (from a user
24038 * command). */
24039 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24040 && (*pp)[2] == (int)KE_SNR)
24041 {
24042 *pp += 3;
24043 len = get_id_len(pp) + 3;
24044 return vim_strnsave(start, len);
24045 }
24046
24047 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24048 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024050 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024052
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024053 /* Note that TFN_ flags use the same values as GLV_ flags. */
24054 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024055 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024056 if (end == start)
24057 {
24058 if (!skip)
24059 EMSG(_("E129: Function name required"));
24060 goto theend;
24061 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024062 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024063 {
24064 /*
24065 * Report an invalid expression in braces, unless the expression
24066 * evaluation has been cancelled due to an aborting error, an
24067 * interrupt, or an exception.
24068 */
24069 if (!aborting())
24070 {
24071 if (end != NULL)
24072 EMSG2(_(e_invarg2), start);
24073 }
24074 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024075 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024076 goto theend;
24077 }
24078
24079 if (lv.ll_tv != NULL)
24080 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024081 if (fdp != NULL)
24082 {
24083 fdp->fd_dict = lv.ll_dict;
24084 fdp->fd_newkey = lv.ll_newkey;
24085 lv.ll_newkey = NULL;
24086 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024087 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024088 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24089 {
24090 name = vim_strsave(lv.ll_tv->vval.v_string);
24091 *pp = end;
24092 }
24093 else
24094 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024095 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24096 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024097 EMSG(_(e_funcref));
24098 else
24099 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024100 name = NULL;
24101 }
24102 goto theend;
24103 }
24104
24105 if (lv.ll_name == NULL)
24106 {
24107 /* Error found, but continue after the function name. */
24108 *pp = end;
24109 goto theend;
24110 }
24111
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024112 /* Check if the name is a Funcref. If so, use the value. */
24113 if (lv.ll_exp_name != NULL)
24114 {
24115 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024116 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024117 if (name == lv.ll_exp_name)
24118 name = NULL;
24119 }
24120 else
24121 {
24122 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024123 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024124 if (name == *pp)
24125 name = NULL;
24126 }
24127 if (name != NULL)
24128 {
24129 name = vim_strsave(name);
24130 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024131 if (STRNCMP(name, "<SNR>", 5) == 0)
24132 {
24133 /* Change "<SNR>" to the byte sequence. */
24134 name[0] = K_SPECIAL;
24135 name[1] = KS_EXTRA;
24136 name[2] = (int)KE_SNR;
24137 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24138 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024139 goto theend;
24140 }
24141
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024142 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024143 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024144 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024145 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24146 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24147 {
24148 /* When there was "s:" already or the name expanded to get a
24149 * leading "s:" then remove it. */
24150 lv.ll_name += 2;
24151 len -= 2;
24152 lead = 2;
24153 }
24154 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024155 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024156 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024157 /* skip over "s:" and "g:" */
24158 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024159 lv.ll_name += 2;
24160 len = (int)(end - lv.ll_name);
24161 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024162
24163 /*
24164 * Copy the function name to allocated memory.
24165 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24166 * Accept <SNR>123_name() outside a script.
24167 */
24168 if (skip)
24169 lead = 0; /* do nothing */
24170 else if (lead > 0)
24171 {
24172 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024173 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24174 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024175 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024176 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024177 if (current_SID <= 0)
24178 {
24179 EMSG(_(e_usingsid));
24180 goto theend;
24181 }
24182 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24183 lead += (int)STRLEN(sid_buf);
24184 }
24185 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024186 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024187 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024188 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024189 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024190 goto theend;
24191 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024192 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024193 {
24194 char_u *cp = vim_strchr(lv.ll_name, ':');
24195
24196 if (cp != NULL && cp < end)
24197 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024198 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024199 goto theend;
24200 }
24201 }
24202
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024203 name = alloc((unsigned)(len + lead + 1));
24204 if (name != NULL)
24205 {
24206 if (lead > 0)
24207 {
24208 name[0] = K_SPECIAL;
24209 name[1] = KS_EXTRA;
24210 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024211 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024212 STRCPY(name + 3, sid_buf);
24213 }
24214 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024215 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024216 }
24217 *pp = end;
24218
24219theend:
24220 clear_lval(&lv);
24221 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222}
24223
24224/*
24225 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24226 * Return 2 if "p" starts with "s:".
24227 * Return 0 otherwise.
24228 */
24229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024230eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024231{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024232 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24233 * the standard library function. */
24234 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24235 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 return 5;
24237 if (p[0] == 's' && p[1] == ':')
24238 return 2;
24239 return 0;
24240}
24241
24242/*
24243 * Return TRUE if "p" starts with "<SID>" or "s:".
24244 * Only works if eval_fname_script() returned non-zero for "p"!
24245 */
24246 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024247eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248{
24249 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24250}
24251
24252/*
24253 * List the head of the function: "name(arg1, arg2)".
24254 */
24255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024256list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257{
24258 int j;
24259
24260 msg_start();
24261 if (indent)
24262 MSG_PUTS(" ");
24263 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024264 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 {
24266 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024267 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268 }
24269 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024270 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024271 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024272 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273 {
24274 if (j)
24275 MSG_PUTS(", ");
24276 msg_puts(FUNCARG(fp, j));
24277 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024278 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279 {
24280 if (j)
24281 MSG_PUTS(", ");
24282 MSG_PUTS("...");
24283 }
24284 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024285 if (fp->uf_flags & FC_ABORT)
24286 MSG_PUTS(" abort");
24287 if (fp->uf_flags & FC_RANGE)
24288 MSG_PUTS(" range");
24289 if (fp->uf_flags & FC_DICT)
24290 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024291 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024292 if (p_verbose > 0)
24293 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294}
24295
24296/*
24297 * Find a function by name, return pointer to it in ufuncs.
24298 * Return NULL for unknown function.
24299 */
24300 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024301find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024302{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024303 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024305 hi = hash_find(&func_hashtab, name);
24306 if (!HASHITEM_EMPTY(hi))
24307 return HI2UF(hi);
24308 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309}
24310
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024311#if defined(EXITFREE) || defined(PROTO)
24312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024313free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024314{
24315 hashitem_T *hi;
24316
24317 /* Need to start all over every time, because func_free() may change the
24318 * hash table. */
24319 while (func_hashtab.ht_used > 0)
24320 for (hi = func_hashtab.ht_array; ; ++hi)
24321 if (!HASHITEM_EMPTY(hi))
24322 {
24323 func_free(HI2UF(hi));
24324 break;
24325 }
24326}
24327#endif
24328
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024330translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024331{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024332 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024333 return find_internal_func(name) >= 0;
24334 return find_func(name) != NULL;
24335}
24336
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024337/*
24338 * Return TRUE if a function "name" exists.
24339 */
24340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024341function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024342{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024343 char_u *nm = name;
24344 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024345 int n = FALSE;
24346
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024347 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24348 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024349 nm = skipwhite(nm);
24350
24351 /* Only accept "funcname", "funcname ", "funcname (..." and
24352 * "funcname(...", not "funcname!...". */
24353 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024354 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024355 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024356 return n;
24357}
24358
Bram Moolenaara1544c02013-05-30 12:35:52 +020024359 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024360get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024361{
24362 char_u *nm = name;
24363 char_u *p;
24364
24365 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24366
24367 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024368 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024369 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024370
Bram Moolenaara1544c02013-05-30 12:35:52 +020024371 vim_free(p);
24372 return NULL;
24373}
24374
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024375/*
24376 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024377 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24378 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024379 */
24380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024381builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024382{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024383 char_u *p;
24384
24385 if (!ASCII_ISLOWER(name[0]))
24386 return FALSE;
24387 p = vim_strchr(name, AUTOLOAD_CHAR);
24388 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024389}
24390
Bram Moolenaar05159a02005-02-26 23:04:13 +000024391#if defined(FEAT_PROFILE) || defined(PROTO)
24392/*
24393 * Start profiling function "fp".
24394 */
24395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024396func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024397{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024398 int len = fp->uf_lines.ga_len;
24399
24400 if (len == 0)
24401 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024402 fp->uf_tm_count = 0;
24403 profile_zero(&fp->uf_tm_self);
24404 profile_zero(&fp->uf_tm_total);
24405 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024406 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024407 if (fp->uf_tml_total == NULL)
24408 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024409 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024410 if (fp->uf_tml_self == NULL)
24411 fp->uf_tml_self = (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 fp->uf_tml_idx = -1;
24414 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24415 || fp->uf_tml_self == NULL)
24416 return; /* out of memory */
24417
24418 fp->uf_profiling = TRUE;
24419}
24420
24421/*
24422 * Dump the profiling results for all functions in file "fd".
24423 */
24424 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024425func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024426{
24427 hashitem_T *hi;
24428 int todo;
24429 ufunc_T *fp;
24430 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024431 ufunc_T **sorttab;
24432 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024433
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024434 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024435 if (todo == 0)
24436 return; /* nothing to dump */
24437
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024438 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024439
Bram Moolenaar05159a02005-02-26 23:04:13 +000024440 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24441 {
24442 if (!HASHITEM_EMPTY(hi))
24443 {
24444 --todo;
24445 fp = HI2UF(hi);
24446 if (fp->uf_profiling)
24447 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024448 if (sorttab != NULL)
24449 sorttab[st_len++] = fp;
24450
Bram Moolenaar05159a02005-02-26 23:04:13 +000024451 if (fp->uf_name[0] == K_SPECIAL)
24452 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24453 else
24454 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24455 if (fp->uf_tm_count == 1)
24456 fprintf(fd, "Called 1 time\n");
24457 else
24458 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24459 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24460 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24461 fprintf(fd, "\n");
24462 fprintf(fd, "count total (s) self (s)\n");
24463
24464 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24465 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024466 if (FUNCLINE(fp, i) == NULL)
24467 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024468 prof_func_line(fd, fp->uf_tml_count[i],
24469 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024470 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24471 }
24472 fprintf(fd, "\n");
24473 }
24474 }
24475 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024476
24477 if (sorttab != NULL && st_len > 0)
24478 {
24479 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24480 prof_total_cmp);
24481 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24482 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24483 prof_self_cmp);
24484 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24485 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024486
24487 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024488}
Bram Moolenaar73830342005-02-28 22:48:19 +000024489
24490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024491prof_sort_list(
24492 FILE *fd,
24493 ufunc_T **sorttab,
24494 int st_len,
24495 char *title,
24496 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024497{
24498 int i;
24499 ufunc_T *fp;
24500
24501 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24502 fprintf(fd, "count total (s) self (s) function\n");
24503 for (i = 0; i < 20 && i < st_len; ++i)
24504 {
24505 fp = sorttab[i];
24506 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24507 prefer_self);
24508 if (fp->uf_name[0] == K_SPECIAL)
24509 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24510 else
24511 fprintf(fd, " %s()\n", fp->uf_name);
24512 }
24513 fprintf(fd, "\n");
24514}
24515
24516/*
24517 * Print the count and times for one function or function line.
24518 */
24519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024520prof_func_line(
24521 FILE *fd,
24522 int count,
24523 proftime_T *total,
24524 proftime_T *self,
24525 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024526{
24527 if (count > 0)
24528 {
24529 fprintf(fd, "%5d ", count);
24530 if (prefer_self && profile_equal(total, self))
24531 fprintf(fd, " ");
24532 else
24533 fprintf(fd, "%s ", profile_msg(total));
24534 if (!prefer_self && profile_equal(total, self))
24535 fprintf(fd, " ");
24536 else
24537 fprintf(fd, "%s ", profile_msg(self));
24538 }
24539 else
24540 fprintf(fd, " ");
24541}
24542
24543/*
24544 * Compare function for total time sorting.
24545 */
24546 static int
24547#ifdef __BORLANDC__
24548_RTLENTRYF
24549#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024550prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024551{
24552 ufunc_T *p1, *p2;
24553
24554 p1 = *(ufunc_T **)s1;
24555 p2 = *(ufunc_T **)s2;
24556 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24557}
24558
24559/*
24560 * Compare function for self time sorting.
24561 */
24562 static int
24563#ifdef __BORLANDC__
24564_RTLENTRYF
24565#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024566prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024567{
24568 ufunc_T *p1, *p2;
24569
24570 p1 = *(ufunc_T **)s1;
24571 p2 = *(ufunc_T **)s2;
24572 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24573}
24574
Bram Moolenaar05159a02005-02-26 23:04:13 +000024575#endif
24576
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024577/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024578 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024579 * Return TRUE if a package was loaded.
24580 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024581 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024582script_autoload(
24583 char_u *name,
24584 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024585{
24586 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024587 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024588 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024589 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024590
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024591 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024592 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024593 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024594 return FALSE;
24595
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024596 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024597
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024598 /* Find the name in the list of previously loaded package names. Skip
24599 * "autoload/", it's always the same. */
24600 for (i = 0; i < ga_loaded.ga_len; ++i)
24601 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24602 break;
24603 if (!reload && i < ga_loaded.ga_len)
24604 ret = FALSE; /* was loaded already */
24605 else
24606 {
24607 /* Remember the name if it wasn't loaded already. */
24608 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24609 {
24610 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24611 tofree = NULL;
24612 }
24613
24614 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024615 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024616 ret = TRUE;
24617 }
24618
24619 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024620 return ret;
24621}
24622
24623/*
24624 * Return the autoload script name for a function or variable name.
24625 * Returns NULL when out of memory.
24626 */
24627 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024628autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024629{
24630 char_u *p;
24631 char_u *scriptname;
24632
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024633 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024634 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24635 if (scriptname == NULL)
24636 return FALSE;
24637 STRCPY(scriptname, "autoload/");
24638 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024639 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024640 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024641 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024642 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024643 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024644}
24645
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24647
24648/*
24649 * Function given to ExpandGeneric() to obtain the list of user defined
24650 * function names.
24651 */
24652 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024653get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024654{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024655 static long_u done;
24656 static hashitem_T *hi;
24657 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658
24659 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024660 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024661 done = 0;
24662 hi = func_hashtab.ht_array;
24663 }
24664 if (done < func_hashtab.ht_used)
24665 {
24666 if (done++ > 0)
24667 ++hi;
24668 while (HASHITEM_EMPTY(hi))
24669 ++hi;
24670 fp = HI2UF(hi);
24671
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024672 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024673 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024674
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024675 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24676 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677
24678 cat_func_name(IObuff, fp);
24679 if (xp->xp_context != EXPAND_USER_FUNC)
24680 {
24681 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024682 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024683 STRCAT(IObuff, ")");
24684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685 return IObuff;
24686 }
24687 return NULL;
24688}
24689
24690#endif /* FEAT_CMDL_COMPL */
24691
24692/*
24693 * Copy the function name of "fp" to buffer "buf".
24694 * "buf" must be able to hold the function name plus three bytes.
24695 * Takes care of script-local function names.
24696 */
24697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024698cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024700 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024701 {
24702 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024703 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704 }
24705 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024706 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707}
24708
24709/*
24710 * ":delfunction {name}"
24711 */
24712 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024713ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024715 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024716 char_u *p;
24717 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024718 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719
24720 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024721 name = trans_function_name(&p, eap->skip, 0, &fudi);
24722 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024724 {
24725 if (fudi.fd_dict != NULL && !eap->skip)
24726 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729 if (!ends_excmd(*skipwhite(p)))
24730 {
24731 vim_free(name);
24732 EMSG(_(e_trailing));
24733 return;
24734 }
24735 eap->nextcmd = check_nextcmd(p);
24736 if (eap->nextcmd != NULL)
24737 *p = NUL;
24738
24739 if (!eap->skip)
24740 fp = find_func(name);
24741 vim_free(name);
24742
24743 if (!eap->skip)
24744 {
24745 if (fp == NULL)
24746 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024747 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024748 return;
24749 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024750 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 {
24752 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24753 return;
24754 }
24755
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024756 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024757 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024758 /* Delete the dict item that refers to the function, it will
24759 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024760 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024761 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024762 else
24763 func_free(fp);
24764 }
24765}
24766
24767/*
24768 * Free a function and remove it from the list of functions.
24769 */
24770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024771func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024772{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024773 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024774
24775 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024776 ga_clear_strings(&(fp->uf_args));
24777 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024778#ifdef FEAT_PROFILE
24779 vim_free(fp->uf_tml_count);
24780 vim_free(fp->uf_tml_total);
24781 vim_free(fp->uf_tml_self);
24782#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024783
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024784 /* remove the function from the function hashtable */
24785 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24786 if (HASHITEM_EMPTY(hi))
24787 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024788 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024789 hash_remove(&func_hashtab, hi);
24790
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024791 vim_free(fp);
24792}
24793
24794/*
24795 * Unreference a Function: decrement the reference count and free it when it
24796 * becomes zero. Only for numbered functions.
24797 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024799func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024800{
24801 ufunc_T *fp;
24802
24803 if (name != NULL && isdigit(*name))
24804 {
24805 fp = find_func(name);
24806 if (fp == NULL)
24807 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024808 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024809 {
24810 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024811 * when "uf_calls" becomes zero. */
24812 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024813 func_free(fp);
24814 }
24815 }
24816}
24817
24818/*
24819 * Count a reference to a Function.
24820 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024822func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024823{
24824 ufunc_T *fp;
24825
24826 if (name != NULL && isdigit(*name))
24827 {
24828 fp = find_func(name);
24829 if (fp == NULL)
24830 EMSG2(_(e_intern2), "func_ref()");
24831 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024832 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024833 }
24834}
24835
24836/*
24837 * Call a user function.
24838 */
24839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024840call_user_func(
24841 ufunc_T *fp, /* pointer to function */
24842 int argcount, /* nr of args */
24843 typval_T *argvars, /* arguments */
24844 typval_T *rettv, /* return value */
24845 linenr_T firstline, /* first line of range */
24846 linenr_T lastline, /* last line of range */
24847 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848{
Bram Moolenaar33570922005-01-25 22:26:29 +000024849 char_u *save_sourcing_name;
24850 linenr_T save_sourcing_lnum;
24851 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024852 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024853 int save_did_emsg;
24854 static int depth = 0;
24855 dictitem_T *v;
24856 int fixvar_idx = 0; /* index in fixvar[] */
24857 int i;
24858 int ai;
24859 char_u numbuf[NUMBUFLEN];
24860 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024861 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024862#ifdef FEAT_PROFILE
24863 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024864 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866
24867 /* If depth of calling is getting too high, don't execute the function */
24868 if (depth >= p_mfd)
24869 {
24870 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024871 rettv->v_type = VAR_NUMBER;
24872 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873 return;
24874 }
24875 ++depth;
24876
24877 line_breakcheck(); /* check for CTRL-C hit */
24878
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024879 fc = (funccall_T *)alloc(sizeof(funccall_T));
24880 fc->caller = current_funccal;
24881 current_funccal = fc;
24882 fc->func = fp;
24883 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024884 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024885 fc->linenr = 0;
24886 fc->returned = FALSE;
24887 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024889 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24890 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891
Bram Moolenaar33570922005-01-25 22:26:29 +000024892 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024893 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024894 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24895 * each argument variable and saves a lot of time.
24896 */
24897 /*
24898 * Init l: variables.
24899 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024900 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024901 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024902 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024903 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24904 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024905 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024906 name = v->di_key;
24907 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024908 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024909 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024910 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024911 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024912 v->di_tv.vval.v_dict = selfdict;
24913 ++selfdict->dv_refcount;
24914 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024915
Bram Moolenaar33570922005-01-25 22:26:29 +000024916 /*
24917 * Init a: variables.
24918 * Set a:0 to "argcount".
24919 * Set a:000 to a list with room for the "..." arguments.
24920 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024921 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024922 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024923 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024924 /* Use "name" to avoid a warning from some compiler that checks the
24925 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024926 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024927 name = v->di_key;
24928 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024929 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024930 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024931 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024932 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024933 v->di_tv.vval.v_list = &fc->l_varlist;
24934 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24935 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24936 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024937
24938 /*
24939 * Set a:firstline to "firstline" and a:lastline to "lastline".
24940 * Set a:name to named arguments.
24941 * Set a:N to the "..." arguments.
24942 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024943 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024944 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024945 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024946 (varnumber_T)lastline);
24947 for (i = 0; i < argcount; ++i)
24948 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024949 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024950 if (ai < 0)
24951 /* named argument a:name */
24952 name = FUNCARG(fp, i);
24953 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024954 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024955 /* "..." argument a:1, a:2, etc. */
24956 sprintf((char *)numbuf, "%d", ai + 1);
24957 name = numbuf;
24958 }
24959 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24960 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024961 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024962 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24963 }
24964 else
24965 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024966 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24967 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024968 if (v == NULL)
24969 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024970 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024971 }
24972 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024973 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024974
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024975 /* Note: the values are copied directly to avoid alloc/free.
24976 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024977 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024978 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024979
24980 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24981 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024982 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24983 fc->l_listitems[ai].li_tv = argvars[i];
24984 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024985 }
24986 }
24987
Bram Moolenaar071d4272004-06-13 20:20:40 +000024988 /* Don't redraw while executing the function. */
24989 ++RedrawingDisabled;
24990 save_sourcing_name = sourcing_name;
24991 save_sourcing_lnum = sourcing_lnum;
24992 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024993 /* need space for function name + ("function " + 3) or "[number]" */
24994 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24995 + STRLEN(fp->uf_name) + 20;
24996 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024997 if (sourcing_name != NULL)
24998 {
24999 if (save_sourcing_name != NULL
25000 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025001 sprintf((char *)sourcing_name, "%s[%d]..",
25002 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025003 else
25004 STRCPY(sourcing_name, "function ");
25005 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25006
25007 if (p_verbose >= 12)
25008 {
25009 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025010 verbose_enter_scroll();
25011
Bram Moolenaar555b2802005-05-19 21:08:39 +000025012 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 if (p_verbose >= 14)
25014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025016 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025017 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025018 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019
25020 msg_puts((char_u *)"(");
25021 for (i = 0; i < argcount; ++i)
25022 {
25023 if (i > 0)
25024 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025025 if (argvars[i].v_type == VAR_NUMBER)
25026 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027 else
25028 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025029 /* Do not want errors such as E724 here. */
25030 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025031 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025032 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025033 if (s != NULL)
25034 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025035 if (vim_strsize(s) > MSG_BUF_CLEN)
25036 {
25037 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25038 s = buf;
25039 }
25040 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025041 vim_free(tofree);
25042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 }
25044 }
25045 msg_puts((char_u *)")");
25046 }
25047 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025048
25049 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050 --no_wait_return;
25051 }
25052 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025053#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025054 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025055 {
25056 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25057 func_do_profile(fp);
25058 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025059 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025060 {
25061 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025062 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025063 profile_zero(&fp->uf_tm_children);
25064 }
25065 script_prof_save(&wait_start);
25066 }
25067#endif
25068
Bram Moolenaar071d4272004-06-13 20:20:40 +000025069 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025070 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071 save_did_emsg = did_emsg;
25072 did_emsg = FALSE;
25073
25074 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025075 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25077
25078 --RedrawingDisabled;
25079
25080 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025081 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025083 clear_tv(rettv);
25084 rettv->v_type = VAR_NUMBER;
25085 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025086 }
25087
Bram Moolenaar05159a02005-02-26 23:04:13 +000025088#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025089 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025090 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025091 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025092 profile_end(&call_start);
25093 profile_sub_wait(&wait_start, &call_start);
25094 profile_add(&fp->uf_tm_total, &call_start);
25095 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025096 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025097 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025098 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25099 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025100 }
25101 }
25102#endif
25103
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104 /* when being verbose, mention the return value */
25105 if (p_verbose >= 12)
25106 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025107 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025108 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025111 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025112 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025113 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025114 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025115 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025117 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025118 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025119 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025120 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025121
Bram Moolenaar555b2802005-05-19 21:08:39 +000025122 /* The value may be very long. Skip the middle part, so that we
25123 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025124 * truncate it at the end. Don't want errors such as E724 here. */
25125 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025126 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025127 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025128 if (s != NULL)
25129 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025130 if (vim_strsize(s) > MSG_BUF_CLEN)
25131 {
25132 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25133 s = buf;
25134 }
25135 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025136 vim_free(tofree);
25137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 }
25139 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025140
25141 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142 --no_wait_return;
25143 }
25144
25145 vim_free(sourcing_name);
25146 sourcing_name = save_sourcing_name;
25147 sourcing_lnum = save_sourcing_lnum;
25148 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025149#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025150 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025151 script_prof_restore(&wait_start);
25152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153
25154 if (p_verbose >= 12 && sourcing_name != NULL)
25155 {
25156 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025157 verbose_enter_scroll();
25158
Bram Moolenaar555b2802005-05-19 21:08:39 +000025159 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025160 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025161
25162 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025163 --no_wait_return;
25164 }
25165
25166 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025167 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025168 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025169
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025170 /* If the a:000 list and the l: and a: dicts are not referenced we can
25171 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025172 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25173 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25174 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25175 {
25176 free_funccal(fc, FALSE);
25177 }
25178 else
25179 {
25180 hashitem_T *hi;
25181 listitem_T *li;
25182 int todo;
25183
25184 /* "fc" is still in use. This can happen when returning "a:000" or
25185 * assigning "l:" to a global variable.
25186 * Link "fc" in the list for garbage collection later. */
25187 fc->caller = previous_funccal;
25188 previous_funccal = fc;
25189
25190 /* Make a copy of the a: variables, since we didn't do that above. */
25191 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25192 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25193 {
25194 if (!HASHITEM_EMPTY(hi))
25195 {
25196 --todo;
25197 v = HI2DI(hi);
25198 copy_tv(&v->di_tv, &v->di_tv);
25199 }
25200 }
25201
25202 /* Make a copy of the a:000 items, since we didn't do that above. */
25203 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25204 copy_tv(&li->li_tv, &li->li_tv);
25205 }
25206}
25207
25208/*
25209 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025210 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025211 */
25212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025213can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025214{
25215 return (fc->l_varlist.lv_copyID != copyID
25216 && fc->l_vars.dv_copyID != copyID
25217 && fc->l_avars.dv_copyID != copyID);
25218}
25219
25220/*
25221 * Free "fc" and what it contains.
25222 */
25223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025224free_funccal(
25225 funccall_T *fc,
25226 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025227{
25228 listitem_T *li;
25229
25230 /* The a: variables typevals may not have been allocated, only free the
25231 * allocated variables. */
25232 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25233
25234 /* free all l: variables */
25235 vars_clear(&fc->l_vars.dv_hashtab);
25236
25237 /* Free the a:000 variables if they were allocated. */
25238 if (free_val)
25239 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25240 clear_tv(&li->li_tv);
25241
25242 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243}
25244
25245/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025246 * Add a number variable "name" to dict "dp" with value "nr".
25247 */
25248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025249add_nr_var(
25250 dict_T *dp,
25251 dictitem_T *v,
25252 char *name,
25253 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025254{
25255 STRCPY(v->di_key, name);
25256 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25257 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25258 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025259 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025260 v->di_tv.vval.v_number = nr;
25261}
25262
25263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025264 * ":return [expr]"
25265 */
25266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025267ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268{
25269 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025270 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 int returning = FALSE;
25272
25273 if (current_funccal == NULL)
25274 {
25275 EMSG(_("E133: :return not inside a function"));
25276 return;
25277 }
25278
25279 if (eap->skip)
25280 ++emsg_skip;
25281
25282 eap->nextcmd = NULL;
25283 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025284 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285 {
25286 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025287 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025289 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290 }
25291 /* It's safer to return also on error. */
25292 else if (!eap->skip)
25293 {
25294 /*
25295 * Return unless the expression evaluation has been cancelled due to an
25296 * aborting error, an interrupt, or an exception.
25297 */
25298 if (!aborting())
25299 returning = do_return(eap, FALSE, TRUE, NULL);
25300 }
25301
25302 /* When skipping or the return gets pending, advance to the next command
25303 * in this line (!returning). Otherwise, ignore the rest of the line.
25304 * Following lines will be ignored by get_func_line(). */
25305 if (returning)
25306 eap->nextcmd = NULL;
25307 else if (eap->nextcmd == NULL) /* no argument */
25308 eap->nextcmd = check_nextcmd(arg);
25309
25310 if (eap->skip)
25311 --emsg_skip;
25312}
25313
25314/*
25315 * Return from a function. Possibly makes the return pending. Also called
25316 * for a pending return at the ":endtry" or after returning from an extra
25317 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025318 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025319 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 * FALSE when the return gets pending.
25321 */
25322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025323do_return(
25324 exarg_T *eap,
25325 int reanimate,
25326 int is_cmd,
25327 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025328{
25329 int idx;
25330 struct condstack *cstack = eap->cstack;
25331
25332 if (reanimate)
25333 /* Undo the return. */
25334 current_funccal->returned = FALSE;
25335
25336 /*
25337 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25338 * not in its finally clause (which then is to be executed next) is found.
25339 * In this case, make the ":return" pending for execution at the ":endtry".
25340 * Otherwise, return normally.
25341 */
25342 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25343 if (idx >= 0)
25344 {
25345 cstack->cs_pending[idx] = CSTP_RETURN;
25346
25347 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025348 /* A pending return again gets pending. "rettv" points to an
25349 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025351 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352 else
25353 {
25354 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025355 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025356 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025357 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025358
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025359 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360 {
25361 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025362 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025363 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364 else
25365 EMSG(_(e_outofmem));
25366 }
25367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025368 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369
25370 if (reanimate)
25371 {
25372 /* The pending return value could be overwritten by a ":return"
25373 * without argument in a finally clause; reset the default
25374 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025375 current_funccal->rettv->v_type = VAR_NUMBER;
25376 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377 }
25378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025379 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 }
25381 else
25382 {
25383 current_funccal->returned = TRUE;
25384
25385 /* If the return is carried out now, store the return value. For
25386 * a return immediately after reanimation, the value is already
25387 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025388 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025390 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025391 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025392 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025393 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394 }
25395 }
25396
25397 return idx < 0;
25398}
25399
25400/*
25401 * Free the variable with a pending return value.
25402 */
25403 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025404discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025405{
Bram Moolenaar33570922005-01-25 22:26:29 +000025406 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025407}
25408
25409/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025410 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025411 * is an allocated string. Used by report_pending() for verbose messages.
25412 */
25413 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025414get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025415{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025416 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025417 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025418 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025420 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025421 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025422 if (s == NULL)
25423 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025424
25425 STRCPY(IObuff, ":return ");
25426 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25427 if (STRLEN(s) + 8 >= IOSIZE)
25428 STRCPY(IObuff + IOSIZE - 4, "...");
25429 vim_free(tofree);
25430 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025431}
25432
25433/*
25434 * Get next function line.
25435 * Called by do_cmdline() to get the next line.
25436 * Returns allocated string, or NULL for end of function.
25437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025438 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025439get_func_line(
25440 int c UNUSED,
25441 void *cookie,
25442 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443{
Bram Moolenaar33570922005-01-25 22:26:29 +000025444 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025445 ufunc_T *fp = fcp->func;
25446 char_u *retval;
25447 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025448
25449 /* If breakpoints have been added/deleted need to check for it. */
25450 if (fcp->dbg_tick != debug_tick)
25451 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025452 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453 sourcing_lnum);
25454 fcp->dbg_tick = debug_tick;
25455 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025456#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025457 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025458 func_line_end(cookie);
25459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025460
Bram Moolenaar05159a02005-02-26 23:04:13 +000025461 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025462 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25463 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464 retval = NULL;
25465 else
25466 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025467 /* Skip NULL lines (continuation lines). */
25468 while (fcp->linenr < gap->ga_len
25469 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25470 ++fcp->linenr;
25471 if (fcp->linenr >= gap->ga_len)
25472 retval = NULL;
25473 else
25474 {
25475 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25476 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025477#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025478 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025479 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025480#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482 }
25483
25484 /* Did we encounter a breakpoint? */
25485 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25486 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025487 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025489 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490 sourcing_lnum);
25491 fcp->dbg_tick = debug_tick;
25492 }
25493
25494 return retval;
25495}
25496
Bram Moolenaar05159a02005-02-26 23:04:13 +000025497#if defined(FEAT_PROFILE) || defined(PROTO)
25498/*
25499 * Called when starting to read a function line.
25500 * "sourcing_lnum" must be correct!
25501 * When skipping lines it may not actually be executed, but we won't find out
25502 * until later and we need to store the time now.
25503 */
25504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025505func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025506{
25507 funccall_T *fcp = (funccall_T *)cookie;
25508 ufunc_T *fp = fcp->func;
25509
25510 if (fp->uf_profiling && sourcing_lnum >= 1
25511 && sourcing_lnum <= fp->uf_lines.ga_len)
25512 {
25513 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025514 /* Skip continuation lines. */
25515 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25516 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025517 fp->uf_tml_execed = FALSE;
25518 profile_start(&fp->uf_tml_start);
25519 profile_zero(&fp->uf_tml_children);
25520 profile_get_wait(&fp->uf_tml_wait);
25521 }
25522}
25523
25524/*
25525 * Called when actually executing a function line.
25526 */
25527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025528func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025529{
25530 funccall_T *fcp = (funccall_T *)cookie;
25531 ufunc_T *fp = fcp->func;
25532
25533 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25534 fp->uf_tml_execed = TRUE;
25535}
25536
25537/*
25538 * Called when done with a function line.
25539 */
25540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025541func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025542{
25543 funccall_T *fcp = (funccall_T *)cookie;
25544 ufunc_T *fp = fcp->func;
25545
25546 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25547 {
25548 if (fp->uf_tml_execed)
25549 {
25550 ++fp->uf_tml_count[fp->uf_tml_idx];
25551 profile_end(&fp->uf_tml_start);
25552 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025553 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025554 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25555 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025556 }
25557 fp->uf_tml_idx = -1;
25558 }
25559}
25560#endif
25561
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562/*
25563 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025564 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 */
25566 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025567func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568{
Bram Moolenaar33570922005-01-25 22:26:29 +000025569 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570
25571 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25572 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025573 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 || fcp->returned);
25575}
25576
25577/*
25578 * return TRUE if cookie indicates a function which "abort"s on errors.
25579 */
25580 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025581func_has_abort(
25582 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025583{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025584 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585}
25586
25587#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25588typedef enum
25589{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025590 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25591 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25592 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025593} var_flavour_T;
25594
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025595static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596
25597 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025598var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599{
25600 char_u *p = varname;
25601
25602 if (ASCII_ISUPPER(*p))
25603 {
25604 while (*(++p))
25605 if (ASCII_ISLOWER(*p))
25606 return VAR_FLAVOUR_SESSION;
25607 return VAR_FLAVOUR_VIMINFO;
25608 }
25609 else
25610 return VAR_FLAVOUR_DEFAULT;
25611}
25612#endif
25613
25614#if defined(FEAT_VIMINFO) || defined(PROTO)
25615/*
25616 * Restore global vars that start with a capital from the viminfo file
25617 */
25618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025619read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620{
25621 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025622 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025623 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025624 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025625
25626 if (!writing && (find_viminfo_parameter('!') != NULL))
25627 {
25628 tab = vim_strchr(virp->vir_line + 1, '\t');
25629 if (tab != NULL)
25630 {
25631 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025632 switch (*tab)
25633 {
25634 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025635#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025636 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025637#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025638 case 'D': type = VAR_DICT; break;
25639 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025640 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025642
25643 tab = vim_strchr(tab, '\t');
25644 if (tab != NULL)
25645 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025646 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025647 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025648 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025649 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025650#ifdef FEAT_FLOAT
25651 else if (type == VAR_FLOAT)
25652 (void)string2float(tab + 1, &tv.vval.v_float);
25653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025655 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025656 if (type == VAR_DICT || type == VAR_LIST)
25657 {
25658 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25659
25660 if (etv == NULL)
25661 /* Failed to parse back the dict or list, use it as a
25662 * string. */
25663 tv.v_type = VAR_STRING;
25664 else
25665 {
25666 vim_free(tv.vval.v_string);
25667 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025668 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025669 }
25670 }
25671
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025672 /* when in a function use global variables */
25673 save_funccal = current_funccal;
25674 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025675 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025676 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025677
25678 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025679 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025680 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25681 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025682 }
25683 }
25684 }
25685
25686 return viminfo_readline(virp);
25687}
25688
25689/*
25690 * Write global vars that start with a capital to the viminfo file
25691 */
25692 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025693write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025694{
Bram Moolenaar33570922005-01-25 22:26:29 +000025695 hashitem_T *hi;
25696 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025697 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025698 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025699 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025700 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025701 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025702
25703 if (find_viminfo_parameter('!') == NULL)
25704 return;
25705
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025706 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025707
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025708 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025709 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025710 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025711 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025713 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025714 this_var = HI2DI(hi);
25715 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025716 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025717 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025718 {
25719 case VAR_STRING: s = "STR"; break;
25720 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025721 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025722 case VAR_DICT: s = "DIC"; break;
25723 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025724 case VAR_SPECIAL: s = "XPL"; break;
25725
25726 case VAR_UNKNOWN:
25727 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025728 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025729 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025730 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025731 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025732 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025733 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025734 if (p != NULL)
25735 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025736 vim_free(tofree);
25737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 }
25739 }
25740}
25741#endif
25742
25743#if defined(FEAT_SESSION) || defined(PROTO)
25744 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025745store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746{
Bram Moolenaar33570922005-01-25 22:26:29 +000025747 hashitem_T *hi;
25748 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025749 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025750 char_u *p, *t;
25751
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025752 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025753 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025755 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025757 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025758 this_var = HI2DI(hi);
25759 if ((this_var->di_tv.v_type == VAR_NUMBER
25760 || this_var->di_tv.v_type == VAR_STRING)
25761 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025762 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025763 /* Escape special characters with a backslash. Turn a LF and
25764 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025765 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025766 (char_u *)"\\\"\n\r");
25767 if (p == NULL) /* out of memory */
25768 break;
25769 for (t = p; *t != NUL; ++t)
25770 if (*t == '\n')
25771 *t = 'n';
25772 else if (*t == '\r')
25773 *t = 'r';
25774 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025775 this_var->di_key,
25776 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25777 : ' ',
25778 p,
25779 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25780 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025781 || put_eol(fd) == FAIL)
25782 {
25783 vim_free(p);
25784 return FAIL;
25785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786 vim_free(p);
25787 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025788#ifdef FEAT_FLOAT
25789 else if (this_var->di_tv.v_type == VAR_FLOAT
25790 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25791 {
25792 float_T f = this_var->di_tv.vval.v_float;
25793 int sign = ' ';
25794
25795 if (f < 0)
25796 {
25797 f = -f;
25798 sign = '-';
25799 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025800 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025801 this_var->di_key, sign, f) < 0)
25802 || put_eol(fd) == FAIL)
25803 return FAIL;
25804 }
25805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 }
25807 }
25808 return OK;
25809}
25810#endif
25811
Bram Moolenaar661b1822005-07-28 22:36:45 +000025812/*
25813 * Display script name where an item was last set.
25814 * Should only be invoked when 'verbose' is non-zero.
25815 */
25816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025817last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025818{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025819 char_u *p;
25820
Bram Moolenaar661b1822005-07-28 22:36:45 +000025821 if (scriptID != 0)
25822 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025823 p = home_replace_save(NULL, get_scriptname(scriptID));
25824 if (p != NULL)
25825 {
25826 verbose_enter();
25827 MSG_PUTS(_("\n\tLast set from "));
25828 MSG_PUTS(p);
25829 vim_free(p);
25830 verbose_leave();
25831 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025832 }
25833}
25834
Bram Moolenaard812df62008-11-09 12:46:09 +000025835/*
25836 * List v:oldfiles in a nice way.
25837 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025839ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025840{
25841 list_T *l = vimvars[VV_OLDFILES].vv_list;
25842 listitem_T *li;
25843 int nr = 0;
25844
25845 if (l == NULL)
25846 msg((char_u *)_("No old files"));
25847 else
25848 {
25849 msg_start();
25850 msg_scroll = TRUE;
25851 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25852 {
25853 msg_outnum((long)++nr);
25854 MSG_PUTS(": ");
25855 msg_outtrans(get_tv_string(&li->li_tv));
25856 msg_putchar('\n');
25857 out_flush(); /* output one line at a time */
25858 ui_breakcheck();
25859 }
25860 /* Assume "got_int" was set to truncate the listing. */
25861 got_int = FALSE;
25862
25863#ifdef FEAT_BROWSE_CMD
25864 if (cmdmod.browse)
25865 {
25866 quit_more = FALSE;
25867 nr = prompt_for_number(FALSE);
25868 msg_starthere();
25869 if (nr > 0)
25870 {
25871 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25872 (long)nr);
25873
25874 if (p != NULL)
25875 {
25876 p = expand_env_save(p);
25877 eap->arg = p;
25878 eap->cmdidx = CMD_edit;
25879 cmdmod.browse = FALSE;
25880 do_exedit(eap, NULL);
25881 vim_free(p);
25882 }
25883 }
25884 }
25885#endif
25886 }
25887}
25888
Bram Moolenaar53744302015-07-17 17:38:22 +020025889/* reset v:option_new, v:option_old and v:option_type */
25890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025891reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025892{
25893 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25894 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25895 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25896}
25897
25898
Bram Moolenaar071d4272004-06-13 20:20:40 +000025899#endif /* FEAT_EVAL */
25900
Bram Moolenaar071d4272004-06-13 20:20:40 +000025901
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025902#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903
25904#ifdef WIN3264
25905/*
25906 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25907 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025908static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25909static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25910static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025911
25912/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025913 * Get the short path (8.3) for the filename in "fnamep".
25914 * Only works for a valid file name.
25915 * When the path gets longer "fnamep" is changed and the allocated buffer
25916 * is put in "bufp".
25917 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25918 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919 */
25920 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025921get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025923 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025924 char_u *newbuf;
25925
25926 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025927 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025928 if (l > len - 1)
25929 {
25930 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025931 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025932 newbuf = vim_strnsave(*fnamep, l);
25933 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025934 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025935
25936 vim_free(*bufp);
25937 *fnamep = *bufp = newbuf;
25938
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025939 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025940 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941 }
25942
25943 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025944 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025945}
25946
25947/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025948 * Get the short path (8.3) for the filename in "fname". The converted
25949 * path is returned in "bufp".
25950 *
25951 * Some of the directories specified in "fname" may not exist. This function
25952 * will shorten the existing directories at the beginning of the path and then
25953 * append the remaining non-existing path.
25954 *
25955 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025956 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025957 * bufp - Pointer to an allocated buffer for the filename.
25958 * fnamelen - Length of the filename pointed to by fname
25959 *
25960 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025961 */
25962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025963shortpath_for_invalid_fname(
25964 char_u **fname,
25965 char_u **bufp,
25966 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025967{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025968 char_u *short_fname, *save_fname, *pbuf_unused;
25969 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025971 int old_len, len;
25972 int new_len, sfx_len;
25973 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025974
25975 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025976 old_len = *fnamelen;
25977 save_fname = vim_strnsave(*fname, old_len);
25978 pbuf_unused = NULL;
25979 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025981 endp = save_fname + old_len - 1; /* Find the end of the copy */
25982 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025983
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025984 /*
25985 * Try shortening the supplied path till it succeeds by removing one
25986 * directory at a time from the tail of the path.
25987 */
25988 len = 0;
25989 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025990 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025991 /* go back one path-separator */
25992 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25993 --endp;
25994 if (endp <= save_fname)
25995 break; /* processed the complete path */
25996
25997 /*
25998 * Replace the path separator with a NUL and try to shorten the
25999 * resulting path.
26000 */
26001 ch = *endp;
26002 *endp = 0;
26003 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026004 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026005 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26006 {
26007 retval = FAIL;
26008 goto theend;
26009 }
26010 *endp = ch; /* preserve the string */
26011
26012 if (len > 0)
26013 break; /* successfully shortened the path */
26014
26015 /* failed to shorten the path. Skip the path separator */
26016 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026017 }
26018
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026019 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026020 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026021 /*
26022 * Succeeded in shortening the path. Now concatenate the shortened
26023 * path with the remaining path at the tail.
26024 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026026 /* Compute the length of the new path. */
26027 sfx_len = (int)(save_endp - endp) + 1;
26028 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026029
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026030 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026031 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026032 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026033 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026034 /* There is not enough space in the currently allocated string,
26035 * copy it to a buffer big enough. */
26036 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026037 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026038 {
26039 retval = FAIL;
26040 goto theend;
26041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026042 }
26043 else
26044 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026045 /* Transfer short_fname to the main buffer (it's big enough),
26046 * unless get_short_pathname() did its work in-place. */
26047 *fname = *bufp = save_fname;
26048 if (short_fname != save_fname)
26049 vim_strncpy(save_fname, short_fname, len);
26050 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026052
26053 /* concat the not-shortened part of the path */
26054 vim_strncpy(*fname + len, endp, sfx_len);
26055 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026056 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026057
26058theend:
26059 vim_free(pbuf_unused);
26060 vim_free(save_fname);
26061
26062 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063}
26064
26065/*
26066 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026067 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068 */
26069 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026070shortpath_for_partial(
26071 char_u **fnamep,
26072 char_u **bufp,
26073 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026074{
26075 int sepcount, len, tflen;
26076 char_u *p;
26077 char_u *pbuf, *tfname;
26078 int hasTilde;
26079
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026080 /* Count up the path separators from the RHS.. so we know which part
26081 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026082 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026083 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026084 if (vim_ispathsep(*p))
26085 ++sepcount;
26086
26087 /* Need full path first (use expand_env() to remove a "~/") */
26088 hasTilde = (**fnamep == '~');
26089 if (hasTilde)
26090 pbuf = tfname = expand_env_save(*fnamep);
26091 else
26092 pbuf = tfname = FullName_save(*fnamep, FALSE);
26093
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026094 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026095
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026096 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26097 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026098
26099 if (len == 0)
26100 {
26101 /* Don't have a valid filename, so shorten the rest of the
26102 * path if we can. This CAN give us invalid 8.3 filenames, but
26103 * there's not a lot of point in guessing what it might be.
26104 */
26105 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026106 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26107 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108 }
26109
26110 /* Count the paths backward to find the beginning of the desired string. */
26111 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026112 {
26113#ifdef FEAT_MBYTE
26114 if (has_mbyte)
26115 p -= mb_head_off(tfname, p);
26116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026117 if (vim_ispathsep(*p))
26118 {
26119 if (sepcount == 0 || (hasTilde && sepcount == 1))
26120 break;
26121 else
26122 sepcount --;
26123 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026125 if (hasTilde)
26126 {
26127 --p;
26128 if (p >= tfname)
26129 *p = '~';
26130 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026131 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026132 }
26133 else
26134 ++p;
26135
26136 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26137 vim_free(*bufp);
26138 *fnamelen = (int)STRLEN(p);
26139 *bufp = pbuf;
26140 *fnamep = p;
26141
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026142 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026143}
26144#endif /* WIN3264 */
26145
26146/*
26147 * Adjust a filename, according to a string of modifiers.
26148 * *fnamep must be NUL terminated when called. When returning, the length is
26149 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026150 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026151 * When there is an error, *fnamep is set to NULL.
26152 */
26153 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026154modify_fname(
26155 char_u *src, /* string with modifiers */
26156 int *usedlen, /* characters after src that are used */
26157 char_u **fnamep, /* file name so far */
26158 char_u **bufp, /* buffer for allocated file name or NULL */
26159 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160{
26161 int valid = 0;
26162 char_u *tail;
26163 char_u *s, *p, *pbuf;
26164 char_u dirname[MAXPATHL];
26165 int c;
26166 int has_fullname = 0;
26167#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026168 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169 int has_shortname = 0;
26170#endif
26171
26172repeat:
26173 /* ":p" - full path/file_name */
26174 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26175 {
26176 has_fullname = 1;
26177
26178 valid |= VALID_PATH;
26179 *usedlen += 2;
26180
26181 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26182 if ((*fnamep)[0] == '~'
26183#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26184 && ((*fnamep)[1] == '/'
26185# ifdef BACKSLASH_IN_FILENAME
26186 || (*fnamep)[1] == '\\'
26187# endif
26188 || (*fnamep)[1] == NUL)
26189
26190#endif
26191 )
26192 {
26193 *fnamep = expand_env_save(*fnamep);
26194 vim_free(*bufp); /* free any allocated file name */
26195 *bufp = *fnamep;
26196 if (*fnamep == NULL)
26197 return -1;
26198 }
26199
26200 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026201 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026202 {
26203 if (vim_ispathsep(*p)
26204 && p[1] == '.'
26205 && (p[2] == NUL
26206 || vim_ispathsep(p[2])
26207 || (p[2] == '.'
26208 && (p[3] == NUL || vim_ispathsep(p[3])))))
26209 break;
26210 }
26211
26212 /* FullName_save() is slow, don't use it when not needed. */
26213 if (*p != NUL || !vim_isAbsName(*fnamep))
26214 {
26215 *fnamep = FullName_save(*fnamep, *p != NUL);
26216 vim_free(*bufp); /* free any allocated file name */
26217 *bufp = *fnamep;
26218 if (*fnamep == NULL)
26219 return -1;
26220 }
26221
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026222#ifdef WIN3264
26223# if _WIN32_WINNT >= 0x0500
26224 if (vim_strchr(*fnamep, '~') != NULL)
26225 {
26226 /* Expand 8.3 filename to full path. Needed to make sure the same
26227 * file does not have two different names.
26228 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26229 p = alloc(_MAX_PATH + 1);
26230 if (p != NULL)
26231 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026232 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026233 {
26234 vim_free(*bufp);
26235 *bufp = *fnamep = p;
26236 }
26237 else
26238 vim_free(p);
26239 }
26240 }
26241# endif
26242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026243 /* Append a path separator to a directory. */
26244 if (mch_isdir(*fnamep))
26245 {
26246 /* Make room for one or two extra characters. */
26247 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26248 vim_free(*bufp); /* free any allocated file name */
26249 *bufp = *fnamep;
26250 if (*fnamep == NULL)
26251 return -1;
26252 add_pathsep(*fnamep);
26253 }
26254 }
26255
26256 /* ":." - path relative to the current directory */
26257 /* ":~" - path relative to the home directory */
26258 /* ":8" - shortname path - postponed till after */
26259 while (src[*usedlen] == ':'
26260 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26261 {
26262 *usedlen += 2;
26263 if (c == '8')
26264 {
26265#ifdef WIN3264
26266 has_shortname = 1; /* Postpone this. */
26267#endif
26268 continue;
26269 }
26270 pbuf = NULL;
26271 /* Need full path first (use expand_env() to remove a "~/") */
26272 if (!has_fullname)
26273 {
26274 if (c == '.' && **fnamep == '~')
26275 p = pbuf = expand_env_save(*fnamep);
26276 else
26277 p = pbuf = FullName_save(*fnamep, FALSE);
26278 }
26279 else
26280 p = *fnamep;
26281
26282 has_fullname = 0;
26283
26284 if (p != NULL)
26285 {
26286 if (c == '.')
26287 {
26288 mch_dirname(dirname, MAXPATHL);
26289 s = shorten_fname(p, dirname);
26290 if (s != NULL)
26291 {
26292 *fnamep = s;
26293 if (pbuf != NULL)
26294 {
26295 vim_free(*bufp); /* free any allocated file name */
26296 *bufp = pbuf;
26297 pbuf = NULL;
26298 }
26299 }
26300 }
26301 else
26302 {
26303 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26304 /* Only replace it when it starts with '~' */
26305 if (*dirname == '~')
26306 {
26307 s = vim_strsave(dirname);
26308 if (s != NULL)
26309 {
26310 *fnamep = s;
26311 vim_free(*bufp);
26312 *bufp = s;
26313 }
26314 }
26315 }
26316 vim_free(pbuf);
26317 }
26318 }
26319
26320 tail = gettail(*fnamep);
26321 *fnamelen = (int)STRLEN(*fnamep);
26322
26323 /* ":h" - head, remove "/file_name", can be repeated */
26324 /* Don't remove the first "/" or "c:\" */
26325 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26326 {
26327 valid |= VALID_HEAD;
26328 *usedlen += 2;
26329 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026330 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026331 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026332 *fnamelen = (int)(tail - *fnamep);
26333#ifdef VMS
26334 if (*fnamelen > 0)
26335 *fnamelen += 1; /* the path separator is part of the path */
26336#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026337 if (*fnamelen == 0)
26338 {
26339 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26340 p = vim_strsave((char_u *)".");
26341 if (p == NULL)
26342 return -1;
26343 vim_free(*bufp);
26344 *bufp = *fnamep = tail = p;
26345 *fnamelen = 1;
26346 }
26347 else
26348 {
26349 while (tail > s && !after_pathsep(s, tail))
26350 mb_ptr_back(*fnamep, tail);
26351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026352 }
26353
26354 /* ":8" - shortname */
26355 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26356 {
26357 *usedlen += 2;
26358#ifdef WIN3264
26359 has_shortname = 1;
26360#endif
26361 }
26362
26363#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026364 /*
26365 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026366 */
26367 if (has_shortname)
26368 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026369 /* Copy the string if it is shortened by :h and when it wasn't copied
26370 * yet, because we are going to change it in place. Avoids changing
26371 * the buffer name for "%:8". */
26372 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026373 {
26374 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026375 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026376 return -1;
26377 vim_free(*bufp);
26378 *bufp = *fnamep = p;
26379 }
26380
26381 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026382 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026383 if (!has_fullname && !vim_isAbsName(*fnamep))
26384 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026385 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026386 return -1;
26387 }
26388 else
26389 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026390 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026391
Bram Moolenaardc935552011-08-17 15:23:23 +020026392 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026393 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026394 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026395 return -1;
26396
26397 if (l == 0)
26398 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026399 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026401 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026402 return -1;
26403 }
26404 *fnamelen = l;
26405 }
26406 }
26407#endif /* WIN3264 */
26408
26409 /* ":t" - tail, just the basename */
26410 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26411 {
26412 *usedlen += 2;
26413 *fnamelen -= (int)(tail - *fnamep);
26414 *fnamep = tail;
26415 }
26416
26417 /* ":e" - extension, can be repeated */
26418 /* ":r" - root, without extension, can be repeated */
26419 while (src[*usedlen] == ':'
26420 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26421 {
26422 /* find a '.' in the tail:
26423 * - for second :e: before the current fname
26424 * - otherwise: The last '.'
26425 */
26426 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26427 s = *fnamep - 2;
26428 else
26429 s = *fnamep + *fnamelen - 1;
26430 for ( ; s > tail; --s)
26431 if (s[0] == '.')
26432 break;
26433 if (src[*usedlen + 1] == 'e') /* :e */
26434 {
26435 if (s > tail)
26436 {
26437 *fnamelen += (int)(*fnamep - (s + 1));
26438 *fnamep = s + 1;
26439#ifdef VMS
26440 /* cut version from the extension */
26441 s = *fnamep + *fnamelen - 1;
26442 for ( ; s > *fnamep; --s)
26443 if (s[0] == ';')
26444 break;
26445 if (s > *fnamep)
26446 *fnamelen = s - *fnamep;
26447#endif
26448 }
26449 else if (*fnamep <= tail)
26450 *fnamelen = 0;
26451 }
26452 else /* :r */
26453 {
26454 if (s > tail) /* remove one extension */
26455 *fnamelen = (int)(s - *fnamep);
26456 }
26457 *usedlen += 2;
26458 }
26459
26460 /* ":s?pat?foo?" - substitute */
26461 /* ":gs?pat?foo?" - global substitute */
26462 if (src[*usedlen] == ':'
26463 && (src[*usedlen + 1] == 's'
26464 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26465 {
26466 char_u *str;
26467 char_u *pat;
26468 char_u *sub;
26469 int sep;
26470 char_u *flags;
26471 int didit = FALSE;
26472
26473 flags = (char_u *)"";
26474 s = src + *usedlen + 2;
26475 if (src[*usedlen + 1] == 'g')
26476 {
26477 flags = (char_u *)"g";
26478 ++s;
26479 }
26480
26481 sep = *s++;
26482 if (sep)
26483 {
26484 /* find end of pattern */
26485 p = vim_strchr(s, sep);
26486 if (p != NULL)
26487 {
26488 pat = vim_strnsave(s, (int)(p - s));
26489 if (pat != NULL)
26490 {
26491 s = p + 1;
26492 /* find end of substitution */
26493 p = vim_strchr(s, sep);
26494 if (p != NULL)
26495 {
26496 sub = vim_strnsave(s, (int)(p - s));
26497 str = vim_strnsave(*fnamep, *fnamelen);
26498 if (sub != NULL && str != NULL)
26499 {
26500 *usedlen = (int)(p + 1 - src);
26501 s = do_string_sub(str, pat, sub, flags);
26502 if (s != NULL)
26503 {
26504 *fnamep = s;
26505 *fnamelen = (int)STRLEN(s);
26506 vim_free(*bufp);
26507 *bufp = s;
26508 didit = TRUE;
26509 }
26510 }
26511 vim_free(sub);
26512 vim_free(str);
26513 }
26514 vim_free(pat);
26515 }
26516 }
26517 /* after using ":s", repeat all the modifiers */
26518 if (didit)
26519 goto repeat;
26520 }
26521 }
26522
Bram Moolenaar26df0922014-02-23 23:39:13 +010026523 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26524 {
26525 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26526 if (p == NULL)
26527 return -1;
26528 vim_free(*bufp);
26529 *bufp = *fnamep = p;
26530 *fnamelen = (int)STRLEN(p);
26531 *usedlen += 2;
26532 }
26533
Bram Moolenaar071d4272004-06-13 20:20:40 +000026534 return valid;
26535}
26536
26537/*
26538 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26539 * "flags" can be "g" to do a global substitute.
26540 * Returns an allocated string, NULL for error.
26541 */
26542 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026543do_string_sub(
26544 char_u *str,
26545 char_u *pat,
26546 char_u *sub,
26547 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026548{
26549 int sublen;
26550 regmatch_T regmatch;
26551 int i;
26552 int do_all;
26553 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026554 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026555 garray_T ga;
26556 char_u *ret;
26557 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026558 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026559
26560 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26561 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026562 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026563
26564 ga_init2(&ga, 1, 200);
26565
26566 do_all = (flags[0] == 'g');
26567
26568 regmatch.rm_ic = p_ic;
26569 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26570 if (regmatch.regprog != NULL)
26571 {
26572 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026573 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026574 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26575 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026576 /* Skip empty match except for first match. */
26577 if (regmatch.startp[0] == regmatch.endp[0])
26578 {
26579 if (zero_width == regmatch.startp[0])
26580 {
26581 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026582 i = MB_PTR2LEN(tail);
26583 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26584 (size_t)i);
26585 ga.ga_len += i;
26586 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026587 continue;
26588 }
26589 zero_width = regmatch.startp[0];
26590 }
26591
Bram Moolenaar071d4272004-06-13 20:20:40 +000026592 /*
26593 * Get some space for a temporary buffer to do the substitution
26594 * into. It will contain:
26595 * - The text up to where the match is.
26596 * - The substituted text.
26597 * - The text after the match.
26598 */
26599 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026600 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026601 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26602 {
26603 ga_clear(&ga);
26604 break;
26605 }
26606
26607 /* copy the text up to where the match is */
26608 i = (int)(regmatch.startp[0] - tail);
26609 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26610 /* add the substituted text */
26611 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26612 + ga.ga_len + i, TRUE, TRUE, FALSE);
26613 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026614 tail = regmatch.endp[0];
26615 if (*tail == NUL)
26616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026617 if (!do_all)
26618 break;
26619 }
26620
26621 if (ga.ga_data != NULL)
26622 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26623
Bram Moolenaar473de612013-06-08 18:19:48 +020026624 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026625 }
26626
26627 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26628 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026629 if (p_cpo == empty_option)
26630 p_cpo = save_cpo;
26631 else
26632 /* Darn, evaluating {sub} expression changed the value. */
26633 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026634
26635 return ret;
26636}
26637
26638#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */