blob: cf908d1d5c444cb2edbb03013fa1f7b5a7074648 [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);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100631#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
632static void f_isnan(typval_T *argvars, typval_T *rettv);
633#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100635#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100636# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100637static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100638# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100639static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100640static void f_job_start(typval_T *argvars, typval_T *rettv);
641static void f_job_stop(typval_T *argvars, typval_T *rettv);
642static void f_job_status(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100645static void f_js_decode(typval_T *argvars, typval_T *rettv);
646static void f_js_encode(typval_T *argvars, typval_T *rettv);
647static void f_json_decode(typval_T *argvars, typval_T *rettv);
648static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_keys(typval_T *argvars, typval_T *rettv);
650static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
651static void f_len(typval_T *argvars, typval_T *rettv);
652static void f_libcall(typval_T *argvars, typval_T *rettv);
653static void f_libcallnr(typval_T *argvars, typval_T *rettv);
654static void f_line(typval_T *argvars, typval_T *rettv);
655static void f_line2byte(typval_T *argvars, typval_T *rettv);
656static void f_lispindent(typval_T *argvars, typval_T *rettv);
657static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_log(typval_T *argvars, typval_T *rettv);
660static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000661#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200662#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100663static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200664#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_map(typval_T *argvars, typval_T *rettv);
666static void f_maparg(typval_T *argvars, typval_T *rettv);
667static void f_mapcheck(typval_T *argvars, typval_T *rettv);
668static void f_match(typval_T *argvars, typval_T *rettv);
669static void f_matchadd(typval_T *argvars, typval_T *rettv);
670static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
671static void f_matcharg(typval_T *argvars, typval_T *rettv);
672static void f_matchdelete(typval_T *argvars, typval_T *rettv);
673static void f_matchend(typval_T *argvars, typval_T *rettv);
674static void f_matchlist(typval_T *argvars, typval_T *rettv);
675static void f_matchstr(typval_T *argvars, typval_T *rettv);
676static void f_max(typval_T *argvars, typval_T *rettv);
677static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000678#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100682#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100684#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
686static void f_nr2char(typval_T *argvars, typval_T *rettv);
687static void f_or(typval_T *argvars, typval_T *rettv);
688static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100689#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100691#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
696static void f_printf(typval_T *argvars, typval_T *rettv);
697static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
701#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200703#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_range(typval_T *argvars, typval_T *rettv);
705static void f_readfile(typval_T *argvars, typval_T *rettv);
706static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100707#ifdef FEAT_FLOAT
708static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_reltimestr(typval_T *argvars, typval_T *rettv);
711static void f_remote_expr(typval_T *argvars, typval_T *rettv);
712static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
713static void f_remote_peek(typval_T *argvars, typval_T *rettv);
714static void f_remote_read(typval_T *argvars, typval_T *rettv);
715static void f_remote_send(typval_T *argvars, typval_T *rettv);
716static void f_remove(typval_T *argvars, typval_T *rettv);
717static void f_rename(typval_T *argvars, typval_T *rettv);
718static void f_repeat(typval_T *argvars, typval_T *rettv);
719static void f_resolve(typval_T *argvars, typval_T *rettv);
720static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000723#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100724static void f_screenattr(typval_T *argvars, typval_T *rettv);
725static void f_screenchar(typval_T *argvars, typval_T *rettv);
726static void f_screencol(typval_T *argvars, typval_T *rettv);
727static void f_screenrow(typval_T *argvars, typval_T *rettv);
728static void f_search(typval_T *argvars, typval_T *rettv);
729static void f_searchdecl(typval_T *argvars, typval_T *rettv);
730static void f_searchpair(typval_T *argvars, typval_T *rettv);
731static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
732static void f_searchpos(typval_T *argvars, typval_T *rettv);
733static void f_server2client(typval_T *argvars, typval_T *rettv);
734static void f_serverlist(typval_T *argvars, typval_T *rettv);
735static void f_setbufvar(typval_T *argvars, typval_T *rettv);
736static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
737static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
738static void f_setline(typval_T *argvars, typval_T *rettv);
739static void f_setloclist(typval_T *argvars, typval_T *rettv);
740static void f_setmatches(typval_T *argvars, typval_T *rettv);
741static void f_setpos(typval_T *argvars, typval_T *rettv);
742static void f_setqflist(typval_T *argvars, typval_T *rettv);
743static void f_setreg(typval_T *argvars, typval_T *rettv);
744static void f_settabvar(typval_T *argvars, typval_T *rettv);
745static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
746static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100747#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100749#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_shellescape(typval_T *argvars, typval_T *rettv);
751static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
752static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sin(typval_T *argvars, typval_T *rettv);
755static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_sort(typval_T *argvars, typval_T *rettv);
758static void f_soundfold(typval_T *argvars, typval_T *rettv);
759static void f_spellbadword(typval_T *argvars, typval_T *rettv);
760static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
761static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sqrt(typval_T *argvars, typval_T *rettv);
764static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_str2nr(typval_T *argvars, typval_T *rettv);
767static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000770#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_stridx(typval_T *argvars, typval_T *rettv);
772static void f_string(typval_T *argvars, typval_T *rettv);
773static void f_strlen(typval_T *argvars, typval_T *rettv);
774static void f_strpart(typval_T *argvars, typval_T *rettv);
775static void f_strridx(typval_T *argvars, typval_T *rettv);
776static void f_strtrans(typval_T *argvars, typval_T *rettv);
777static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
778static void f_strwidth(typval_T *argvars, typval_T *rettv);
779static void f_submatch(typval_T *argvars, typval_T *rettv);
780static void f_substitute(typval_T *argvars, typval_T *rettv);
781static void f_synID(typval_T *argvars, typval_T *rettv);
782static void f_synIDattr(typval_T *argvars, typval_T *rettv);
783static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
784static void f_synstack(typval_T *argvars, typval_T *rettv);
785static void f_synconcealed(typval_T *argvars, typval_T *rettv);
786static void f_system(typval_T *argvars, typval_T *rettv);
787static void f_systemlist(typval_T *argvars, typval_T *rettv);
788static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
789static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
790static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
791static void f_taglist(typval_T *argvars, typval_T *rettv);
792static void f_tagfiles(typval_T *argvars, typval_T *rettv);
793static void f_tempname(typval_T *argvars, typval_T *rettv);
794static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tan(typval_T *argvars, typval_T *rettv);
797static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200798#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100799static void f_tolower(typval_T *argvars, typval_T *rettv);
800static void f_toupper(typval_T *argvars, typval_T *rettv);
801static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000802#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000804#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_type(typval_T *argvars, typval_T *rettv);
806static void f_undofile(typval_T *argvars, typval_T *rettv);
807static void f_undotree(typval_T *argvars, typval_T *rettv);
808static void f_uniq(typval_T *argvars, typval_T *rettv);
809static void f_values(typval_T *argvars, typval_T *rettv);
810static void f_virtcol(typval_T *argvars, typval_T *rettv);
811static void f_visualmode(typval_T *argvars, typval_T *rettv);
812static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
813static void f_winbufnr(typval_T *argvars, typval_T *rettv);
814static void f_wincol(typval_T *argvars, typval_T *rettv);
815static void f_winheight(typval_T *argvars, typval_T *rettv);
816static void f_winline(typval_T *argvars, typval_T *rettv);
817static void f_winnr(typval_T *argvars, typval_T *rettv);
818static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
819static void f_winrestview(typval_T *argvars, typval_T *rettv);
820static void f_winsaveview(typval_T *argvars, typval_T *rettv);
821static void f_winwidth(typval_T *argvars, typval_T *rettv);
822static void f_writefile(typval_T *argvars, typval_T *rettv);
823static void f_wordcount(typval_T *argvars, typval_T *rettv);
824static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000825
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
827static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
828static int get_env_len(char_u **arg);
829static int get_id_len(char_u **arg);
830static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
831static 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 +0000832#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
833#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
834 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
836static int eval_isnamec(int c);
837static int eval_isnamec1(int c);
838static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
839static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100840static typval_T *alloc_string_tv(char_u *string);
841static void init_tv(typval_T *varp);
842static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100843#ifdef FEAT_FLOAT
844static float_T get_tv_float(typval_T *varp);
845#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static linenr_T get_tv_lnum(typval_T *argvars);
847static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
848static char_u *get_tv_string(typval_T *varp);
849static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
850static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
851static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
852static hashtab_T *find_var_ht(char_u *name, char_u **varname);
853static funccall_T *get_funccal(void);
854static void vars_clear_ext(hashtab_T *ht, int free_val);
855static void delete_var(hashtab_T *ht, hashitem_T *hi);
856static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
857static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
858static void set_var(char_u *name, typval_T *varp, int copy);
859static int var_check_ro(int flags, char_u *name, int use_gettext);
860static int var_check_fixed(int flags, char_u *name, int use_gettext);
861static int var_check_func_name(char_u *name, int new_var);
862static int valid_varname(char_u *varname);
863static int tv_check_lock(int lock, char_u *name, int use_gettext);
864static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
865static char_u *find_option_end(char_u **arg, int *opt_flags);
866static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
867static int eval_fname_script(char_u *p);
868static int eval_fname_sid(char_u *p);
869static void list_func_head(ufunc_T *fp, int indent);
870static ufunc_T *find_func(char_u *name);
871static int function_exists(char_u *name);
872static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000873#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static void func_do_profile(ufunc_T *fp);
875static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
876static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000877static int
878# ifdef __BORLANDC__
879 _RTLENTRYF
880# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000882static int
883# ifdef __BORLANDC__
884 _RTLENTRYF
885# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static int script_autoload(char_u *name, int reload);
889static char_u *autoload_name(char_u *name);
890static void cat_func_name(char_u *buf, ufunc_T *fp);
891static void func_free(ufunc_T *fp);
892static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
893static int can_free_funccal(funccall_T *fc, int copyID) ;
894static void free_funccal(funccall_T *fc, int free_val);
895static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
896static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
897static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
898static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
899static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
900static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
901static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
902static int write_list(FILE *fd, list_T *list, int binary);
903static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000904
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200905
906#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907static int compare_func_name(const void *s1, const void *s2);
908static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909#endif
910
Bram Moolenaar33570922005-01-25 22:26:29 +0000911/*
912 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000913 */
914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100915eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000916{
Bram Moolenaar33570922005-01-25 22:26:29 +0000917 int i;
918 struct vimvar *p;
919
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200920 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
921 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200922 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000923 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000924 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000925
926 for (i = 0; i < VV_LEN; ++i)
927 {
928 p = &vimvars[i];
929 STRCPY(p->vv_di.di_key, p->vv_name);
930 if (p->vv_flags & VV_RO)
931 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
932 else if (p->vv_flags & VV_RO_SBX)
933 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
934 else
935 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000936
937 /* add to v: scope dict, unless the value is not always available */
938 if (p->vv_type != VAR_UNKNOWN)
939 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000941 /* add to compat scope dict */
942 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100944 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
945
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000946 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100947 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200948 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100949 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100950
951 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
952 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
953 set_vim_var_nr(VV_NONE, VVAL_NONE);
954 set_vim_var_nr(VV_NULL, VVAL_NULL);
955
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200956 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200957
958#ifdef EBCDIC
959 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100960 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200961 */
962 sortFunctions();
963#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000964}
965
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966#if defined(EXITFREE) || defined(PROTO)
967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100968eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969{
970 int i;
971 struct vimvar *p;
972
973 for (i = 0; i < VV_LEN; ++i)
974 {
975 p = &vimvars[i];
976 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000977 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000978 vim_free(p->vv_str);
979 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000980 }
981 else if (p->vv_di.di_tv.v_type == VAR_LIST)
982 {
983 list_unref(p->vv_list);
984 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000985 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000986 }
987 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000988 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989 hash_clear(&compat_hashtab);
990
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100992# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200993 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100994# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995
996 /* global variables */
997 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000998
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000999 /* autoloaded script names */
1000 ga_clear_strings(&ga_loaded);
1001
Bram Moolenaarcca74132013-09-25 21:00:28 +02001002 /* Script-local variables. First clear all the variables and in a second
1003 * loop free the scriptvar_T, because a variable in one script might hold
1004 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001005 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001007 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001008 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 ga_clear(&ga_scripts);
1010
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001011 /* unreferenced lists and dicts */
1012 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001013
1014 /* functions */
1015 free_all_functions();
1016 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001017}
1018#endif
1019
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 * Return the name of the executed function.
1022 */
1023 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001024func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/*
1030 * Return the address holding the next breakpoint line for a funccall cookie.
1031 */
1032 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001033func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036}
1037
1038/*
1039 * Return the address holding the debug tick for a funccall cookie.
1040 */
1041 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001042func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
Bram Moolenaar33570922005-01-25 22:26:29 +00001044 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045}
1046
1047/*
1048 * Return the nesting level for a funccall cookie.
1049 */
1050 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001051func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar33570922005-01-25 22:26:29 +00001053 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054}
1055
1056/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001057funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001059/* pointer to list of previously used funccal, still around because some
1060 * item in it is still being used. */
1061funccall_T *previous_funccal = NULL;
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063/*
1064 * Return TRUE when a function was ended by a ":return" command.
1065 */
1066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001067current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
1069 return current_funccal->returned;
1070}
1071
1072
1073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * Set an internal variable to a string value. Creates the variable if it does
1075 * not already exist.
1076 */
1077 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001078set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001080 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001081 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 val = vim_strsave(value);
1084 if (val != NULL)
1085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001087 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001089 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001090 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 }
1093}
1094
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097static char_u *redir_endp = NULL;
1098static char_u *redir_varname = NULL;
1099
1100/*
1101 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001102 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 * Returns OK if successfully completed the setup. FAIL otherwise.
1104 */
1105 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001106var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107{
1108 int save_emsg;
1109 int err;
1110 typval_T tv;
1111
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001112 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001113 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 {
1115 EMSG(_(e_invarg));
1116 return FAIL;
1117 }
1118
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001119 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 redir_varname = vim_strsave(name);
1121 if (redir_varname == NULL)
1122 return FAIL;
1123
1124 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1125 if (redir_lval == NULL)
1126 {
1127 var_redir_stop();
1128 return FAIL;
1129 }
1130
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131 /* The output is stored in growarray "redir_ga" until redirection ends. */
1132 ga_init2(&redir_ga, (int)sizeof(char), 500);
1133
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001135 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001136 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1138 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001139 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_endp != NULL && *redir_endp != NUL)
1141 /* Trailing characters are present after the variable name */
1142 EMSG(_(e_trailing));
1143 else
1144 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001145 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 var_redir_stop();
1147 return FAIL;
1148 }
1149
1150 /* check if we can write to the variable: set it to or append an empty
1151 * string */
1152 save_emsg = did_emsg;
1153 did_emsg = FALSE;
1154 tv.v_type = VAR_STRING;
1155 tv.vval.v_string = (char_u *)"";
1156 if (append)
1157 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1158 else
1159 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001160 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001162 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (err)
1164 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 var_redir_stop();
1167 return FAIL;
1168 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169
1170 return OK;
1171}
1172
1173/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001174 * Append "value[value_len]" to the variable set by var_redir_start().
1175 * The actual appending is postponed until redirection ends, because the value
1176 * appended may in fact be the string we write to, changing it may cause freed
1177 * memory to be used:
1178 * :redir => foo
1179 * :let foo
1180 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 */
1182 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001183var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001185 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186
1187 if (redir_lval == NULL)
1188 return;
1189
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001190 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 {
1197 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001199 }
1200 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202}
1203
1204/*
1205 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001206 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 */
1208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001209var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001211 typval_T tv;
1212
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213 if (redir_lval != NULL)
1214 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001215 /* If there was no error: assign the text to the variable. */
1216 if (redir_endp != NULL)
1217 {
1218 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1219 tv.v_type = VAR_STRING;
1220 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001221 /* Call get_lval() again, if it's inside a Dict or List it may
1222 * have changed. */
1223 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001224 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001225 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1226 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1227 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001228 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001229
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001230 /* free the collected output */
1231 vim_free(redir_ga.ga_data);
1232 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001233
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234 vim_free(redir_lval);
1235 redir_lval = NULL;
1236 }
1237 vim_free(redir_varname);
1238 redir_varname = NULL;
1239}
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241# if defined(FEAT_MBYTE) || defined(PROTO)
1242 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001243eval_charconvert(
1244 char_u *enc_from,
1245 char_u *enc_to,
1246 char_u *fname_from,
1247 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 int err = FALSE;
1250
1251 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1252 set_vim_var_string(VV_CC_TO, enc_to, -1);
1253 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1254 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1255 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1256 err = TRUE;
1257 set_vim_var_string(VV_CC_FROM, NULL, -1);
1258 set_vim_var_string(VV_CC_TO, NULL, -1);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1261
1262 if (err)
1263 return FAIL;
1264 return OK;
1265}
1266# endif
1267
1268# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001270eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
1272 int err = FALSE;
1273
1274 set_vim_var_string(VV_FNAME_IN, fname, -1);
1275 set_vim_var_string(VV_CMDARG, args, -1);
1276 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1277 err = TRUE;
1278 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1279 set_vim_var_string(VV_CMDARG, NULL, -1);
1280
1281 if (err)
1282 {
1283 mch_remove(fname);
1284 return FAIL;
1285 }
1286 return OK;
1287}
1288# endif
1289
1290# if defined(FEAT_DIFF) || defined(PROTO)
1291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001292eval_diff(
1293 char_u *origfile,
1294 char_u *newfile,
1295 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int err = FALSE;
1298
1299 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1300 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1301 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1302 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1303 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1304 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1305 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1306}
1307
1308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309eval_patch(
1310 char_u *origfile,
1311 char_u *difffile,
1312 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 int err;
1315
1316 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1317 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1318 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1319 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1320 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1321 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1322 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1323}
1324# endif
1325
1326/*
1327 * Top level evaluation function, returning a boolean.
1328 * Sets "error" to TRUE if there was an error.
1329 * Return TRUE or FALSE.
1330 */
1331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001332eval_to_bool(
1333 char_u *arg,
1334 int *error,
1335 char_u **nextcmd,
1336 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar33570922005-01-25 22:26:29 +00001338 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 int retval = FALSE;
1340
1341 if (skip)
1342 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 else
1346 {
1347 *error = FALSE;
1348 if (!skip)
1349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001350 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 }
1354 if (skip)
1355 --emsg_skip;
1356
1357 return retval;
1358}
1359
1360/*
1361 * Top level evaluation function, returning a string. If "skip" is TRUE,
1362 * only parsing to "nextcmd" is done, without reporting errors. Return
1363 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1364 */
1365 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001366eval_to_string_skip(
1367 char_u *arg,
1368 char_u **nextcmd,
1369 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
Bram Moolenaar33570922005-01-25 22:26:29 +00001371 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 char_u *retval;
1373
1374 if (skip)
1375 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 retval = NULL;
1378 else
1379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 retval = vim_strsave(get_tv_string(&tv));
1381 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 if (skip)
1384 --emsg_skip;
1385
1386 return retval;
1387}
1388
1389/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390 * Skip over an expression at "*pp".
1391 * Return FAIL for an error, OK otherwise.
1392 */
1393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001394skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395{
Bram Moolenaar33570922005-01-25 22:26:29 +00001396 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001397
1398 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001400}
1401
1402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001404 * When "convert" is TRUE convert a List into a sequence of lines and convert
1405 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Return pointer to allocated memory, or NULL for failure.
1407 */
1408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001409eval_to_string(
1410 char_u *arg,
1411 char_u **nextcmd,
1412 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001417#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001418 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001421 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 retval = NULL;
1423 else
1424 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001425 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001426 {
1427 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001428 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001429 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001430 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001431 if (tv.vval.v_list->lv_len > 0)
1432 ga_append(&ga, NL);
1433 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001434 ga_append(&ga, NUL);
1435 retval = (char_u *)ga.ga_data;
1436 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001437#ifdef FEAT_FLOAT
1438 else if (convert && tv.v_type == VAR_FLOAT)
1439 {
1440 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1441 retval = vim_strsave(numbuf);
1442 }
1443#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001444 else
1445 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001446 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
1449 return retval;
1450}
1451
1452/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001453 * Call eval_to_string() without using current local variables and using
1454 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457eval_to_string_safe(
1458 char_u *arg,
1459 char_u **nextcmd,
1460 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 char_u *retval;
1463 void *save_funccalp;
1464
1465 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001466 if (use_sandbox)
1467 ++sandbox;
1468 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001469 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001470 if (use_sandbox)
1471 --sandbox;
1472 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 restore_funccal(save_funccalp);
1474 return retval;
1475}
1476
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477/*
1478 * Top level evaluation function, returning a number.
1479 * Evaluates "expr" silently.
1480 * Returns -1 for an error.
1481 */
1482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
Bram Moolenaar33570922005-01-25 22:26:29 +00001485 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001487 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 ++emsg_off;
1490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 retval = -1;
1493 else
1494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001495 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 --emsg_off;
1499
1500 return retval;
1501}
1502
Bram Moolenaara40058a2005-07-11 22:42:07 +00001503/*
1504 * Prepare v: variable "idx" to be used.
1505 * Save the current typeval in "save_tv".
1506 * When not used yet add the variable to the v: hashtable.
1507 */
1508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001510{
1511 *save_tv = vimvars[idx].vv_tv;
1512 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1513 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1514}
1515
1516/*
1517 * Restore v: variable "idx" to typeval "save_tv".
1518 * When no longer defined, remove the variable from the v: hashtable.
1519 */
1520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001521restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001522{
1523 hashitem_T *hi;
1524
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525 vimvars[idx].vv_tv = *save_tv;
1526 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1527 {
1528 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1529 if (HASHITEM_EMPTY(hi))
1530 EMSG2(_(e_intern2), "restore_vimvar()");
1531 else
1532 hash_remove(&vimvarht, hi);
1533 }
1534}
1535
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001536#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537/*
1538 * Evaluate an expression to a list with suggestions.
1539 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001540 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541 */
1542 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544{
1545 typval_T save_val;
1546 typval_T rettv;
1547 list_T *list = NULL;
1548 char_u *p = skipwhite(expr);
1549
1550 /* Set "v:val" to the bad word. */
1551 prepare_vimvar(VV_VAL, &save_val);
1552 vimvars[VV_VAL].vv_type = VAR_STRING;
1553 vimvars[VV_VAL].vv_str = badword;
1554 if (p_verbose == 0)
1555 ++emsg_off;
1556
1557 if (eval1(&p, &rettv, TRUE) == OK)
1558 {
1559 if (rettv.v_type != VAR_LIST)
1560 clear_tv(&rettv);
1561 else
1562 list = rettv.vval.v_list;
1563 }
1564
1565 if (p_verbose == 0)
1566 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567 restore_vimvar(VV_VAL, &save_val);
1568
1569 return list;
1570}
1571
1572/*
1573 * "list" is supposed to contain two items: a word and a number. Return the
1574 * word in "pp" and the number as the return value.
1575 * Return -1 if anything isn't right.
1576 * Used to get the good word and score from the eval_spell_expr() result.
1577 */
1578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580{
1581 listitem_T *li;
1582
1583 li = list->lv_first;
1584 if (li == NULL)
1585 return -1;
1586 *pp = get_tv_string(&li->li_tv);
1587
1588 li = li->li_next;
1589 if (li == NULL)
1590 return -1;
1591 return get_tv_number(&li->li_tv);
1592}
1593#endif
1594
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001595/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001596 * Top level evaluation function.
1597 * Returns an allocated typval_T with the result.
1598 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001599 */
1600 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602{
1603 typval_T *tv;
1604
1605 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001606 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001607 {
1608 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001609 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610 }
1611
1612 return tv;
1613}
1614
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 * Uses argv[argc] for the function arguments. Only Number and String
1619 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001622 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001623call_vim_function(
1624 char_u *func,
1625 int argc,
1626 char_u **argv,
1627 int safe, /* use the sandbox */
1628 int str_arg_only, /* all arguments are strings */
1629 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 long n;
1633 int len;
1634 int i;
1635 int doesrange;
1636 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001639 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
1643 for (i = 0; i < argc; i++)
1644 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001645 /* Pass a NULL or empty argument as an empty string */
1646 if (argv[i] == NULL || *argv[i] == NUL)
1647 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001648 argvars[i].v_type = VAR_STRING;
1649 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001650 continue;
1651 }
1652
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001653 if (str_arg_only)
1654 len = 0;
1655 else
1656 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001657 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (len != 0 && len == (int)STRLEN(argv[i]))
1659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001660 argvars[i].v_type = VAR_NUMBER;
1661 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 else
1664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001665 argvars[i].v_type = VAR_STRING;
1666 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
1668 }
1669
1670 if (safe)
1671 {
1672 save_funccalp = save_funccal();
1673 ++sandbox;
1674 }
1675
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1677 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (safe)
1681 {
1682 --sandbox;
1683 restore_funccal(save_funccalp);
1684 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 vim_free(argvars);
1686
1687 if (ret == FAIL)
1688 clear_tv(rettv);
1689
1690 return ret;
1691}
1692
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001693/*
1694 * Call vimL function "func" and return the result as a number.
1695 * Returns -1 when calling the function fails.
1696 * Uses argv[argc] for the function arguments.
1697 */
1698 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699call_func_retnr(
1700 char_u *func,
1701 int argc,
1702 char_u **argv,
1703 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001704{
1705 typval_T rettv;
1706 long retval;
1707
1708 /* All arguments are passed as strings, no conversion to number. */
1709 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1710 return -1;
1711
1712 retval = get_tv_number_chk(&rettv, NULL);
1713 clear_tv(&rettv);
1714 return retval;
1715}
1716
1717#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1718 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1719
Bram Moolenaar4f688582007-07-24 12:34:30 +00001720# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001722 * Call vimL function "func" and return the result as a string.
1723 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 * Uses argv[argc] for the function arguments.
1725 */
1726 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001727call_func_retstr(
1728 char_u *func,
1729 int argc,
1730 char_u **argv,
1731 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732{
1733 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001734 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001736 /* All arguments are passed as strings, no conversion to number. */
1737 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 return NULL;
1739
1740 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 return retval;
1743}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001744# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001746/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001747 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001749 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750 */
1751 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752call_func_retlist(
1753 char_u *func,
1754 int argc,
1755 char_u **argv,
1756 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757{
1758 typval_T rettv;
1759
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001760 /* All arguments are passed as strings, no conversion to number. */
1761 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001762 return NULL;
1763
1764 if (rettv.v_type != VAR_LIST)
1765 {
1766 clear_tv(&rettv);
1767 return NULL;
1768 }
1769
1770 return rettv.vval.v_list;
1771}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#endif
1773
1774/*
1775 * Save the current function call pointer, and set it to NULL.
1776 * Used when executing autocommands and for ":source".
1777 */
1778 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001781 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 current_funccal = NULL;
1784 return (void *)fc;
1785}
1786
1787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001790 funccall_T *fc = (funccall_T *)vfc;
1791
1792 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793}
1794
Bram Moolenaar05159a02005-02-26 23:04:13 +00001795#if defined(FEAT_PROFILE) || defined(PROTO)
1796/*
1797 * Prepare profiling for entering a child or something else that is not
1798 * counted for the script/function itself.
1799 * Should always be called in pair with prof_child_exit().
1800 */
1801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802prof_child_enter(
1803 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001804{
1805 funccall_T *fc = current_funccal;
1806
1807 if (fc != NULL && fc->func->uf_profiling)
1808 profile_start(&fc->prof_child);
1809 script_prof_save(tm);
1810}
1811
1812/*
1813 * Take care of time spent in a child.
1814 * Should always be called after prof_child_enter().
1815 */
1816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817prof_child_exit(
1818 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001819{
1820 funccall_T *fc = current_funccal;
1821
1822 if (fc != NULL && fc->func->uf_profiling)
1823 {
1824 profile_end(&fc->prof_child);
1825 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1826 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1827 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1828 }
1829 script_prof_restore(tm);
1830}
1831#endif
1832
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_FOLDING
1835/*
1836 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1837 * it in "*cp". Doesn't give error messages.
1838 */
1839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 int retval;
1844 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001845 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1846 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001849 if (use_sandbox)
1850 ++sandbox;
1851 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 retval = 0;
1855 else
1856 {
1857 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 if (tv.v_type == VAR_NUMBER)
1859 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001860 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 retval = 0;
1862 else
1863 {
1864 /* If the result is a string, check if there is a non-digit before
1865 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (!VIM_ISDIGIT(*s) && *s != '-')
1868 *cp = *s++;
1869 retval = atol((char *)s);
1870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001874 if (use_sandbox)
1875 --sandbox;
1876 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 return retval;
1879}
1880#endif
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 * ":let" list all variable values
1884 * ":let var1 var2" list variable values
1885 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 * ":let var += expr" assignment command.
1887 * ":let var -= expr" assignment command.
1888 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 */
1891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 int var_count = 0;
1899 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001901 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaardb552d602006-03-23 22:59:57 +00001904 argend = skip_var_list(arg, &var_count, &semicolon);
1905 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001907 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1908 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001909 expr = skipwhite(argend);
1910 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1911 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001913 /*
1914 * ":let" without "=": list variables
1915 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 if (*arg == '[')
1917 EMSG(_(e_invarg));
1918 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001920 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001924 list_glob_vars(&first);
1925 list_buf_vars(&first);
1926 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001927#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001929#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001930 list_script_vars(&first);
1931 list_func_vars(&first);
1932 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 eap->nextcmd = check_nextcmd(arg);
1935 }
1936 else
1937 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 op[0] = '=';
1939 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001940 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001942 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1943 op[0] = *expr; /* +=, -= or .= */
1944 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 else
1947 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (eap->skip)
1950 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (eap->skip)
1953 {
1954 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 --emsg_skip;
1957 }
1958 else if (i != FAIL)
1959 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
1964 }
1965}
1966
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967/*
1968 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1969 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970 * When "nextchars" is not NULL it points to a string with characters that
1971 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1972 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 * Returns OK or FAIL;
1974 */
1975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001976ex_let_vars(
1977 char_u *arg_start,
1978 typval_T *tv,
1979 int copy, /* copy values from "tv", don't move */
1980 int semicolon, /* from skip_var_list() */
1981 int var_count, /* from skip_var_list() */
1982 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983{
1984 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001985 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 listitem_T *item;
1988 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989
1990 if (*arg != '[')
1991 {
1992 /*
1993 * ":let var = expr" or ":for var in list"
1994 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 return OK;
1998 }
1999
2000 /*
2001 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2002 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002003 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 {
2005 EMSG(_(e_listreq));
2006 return FAIL;
2007 }
2008
2009 i = list_len(l);
2010 if (semicolon == 0 && var_count < i)
2011 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002012 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 return FAIL;
2014 }
2015 if (var_count - semicolon > i)
2016 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002017 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 return FAIL;
2019 }
2020
2021 item = l->lv_first;
2022 while (*arg != ']')
2023 {
2024 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 item = item->li_next;
2027 if (arg == NULL)
2028 return FAIL;
2029
2030 arg = skipwhite(arg);
2031 if (*arg == ';')
2032 {
2033 /* Put the rest of the list (may be empty) in the var after ';'.
2034 * Create a new list for this. */
2035 l = list_alloc();
2036 if (l == NULL)
2037 return FAIL;
2038 while (item != NULL)
2039 {
2040 list_append_tv(l, &item->li_tv);
2041 item = item->li_next;
2042 }
2043
2044 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002045 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 ltv.vval.v_list = l;
2047 l->lv_refcount = 1;
2048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002049 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2050 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 clear_tv(&ltv);
2052 if (arg == NULL)
2053 return FAIL;
2054 break;
2055 }
2056 else if (*arg != ',' && *arg != ']')
2057 {
2058 EMSG2(_(e_intern2), "ex_let_vars()");
2059 return FAIL;
2060 }
2061 }
2062
2063 return OK;
2064}
2065
2066/*
2067 * Skip over assignable variable "var" or list of variables "[var, var]".
2068 * Used for ":let varvar = expr" and ":for varvar in expr".
2069 * For "[var, var]" increment "*var_count" for each variable.
2070 * for "[var, var; var]" set "semicolon".
2071 * Return NULL for an error.
2072 */
2073 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002074skip_var_list(
2075 char_u *arg,
2076 int *var_count,
2077 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078{
2079 char_u *p, *s;
2080
2081 if (*arg == '[')
2082 {
2083 /* "[var, var]": find the matching ']'. */
2084 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002085 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 {
2087 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2088 s = skip_var_one(p);
2089 if (s == p)
2090 {
2091 EMSG2(_(e_invarg2), p);
2092 return NULL;
2093 }
2094 ++*var_count;
2095
2096 p = skipwhite(s);
2097 if (*p == ']')
2098 break;
2099 else if (*p == ';')
2100 {
2101 if (*semicolon == 1)
2102 {
2103 EMSG(_("Double ; in list of variables"));
2104 return NULL;
2105 }
2106 *semicolon = 1;
2107 }
2108 else if (*p != ',')
2109 {
2110 EMSG2(_(e_invarg2), p);
2111 return NULL;
2112 }
2113 }
2114 return p + 1;
2115 }
2116 else
2117 return skip_var_one(arg);
2118}
2119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002121 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002122 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002125skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002127 if (*arg == '@' && arg[1] != NUL)
2128 return arg + 2;
2129 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2130 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131}
2132
Bram Moolenaara7043832005-01-21 11:56:39 +00002133/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002134 * List variables for hashtab "ht" with prefix "prefix".
2135 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002138list_hashtable_vars(
2139 hashtab_T *ht,
2140 char_u *prefix,
2141 int empty,
2142 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002143{
Bram Moolenaar33570922005-01-25 22:26:29 +00002144 hashitem_T *hi;
2145 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002146 int todo;
2147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002148 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2150 {
2151 if (!HASHITEM_EMPTY(hi))
2152 {
2153 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002154 di = HI2DI(hi);
2155 if (empty || di->di_tv.v_type != VAR_STRING
2156 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158 }
2159 }
2160}
2161
2162/*
2163 * List global variables.
2164 */
2165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002166list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002167{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169}
2170
2171/*
2172 * List buffer variables.
2173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002176{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002177 char_u numbuf[NUMBUFLEN];
2178
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181
2182 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2184 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002185}
2186
2187/*
2188 * List window variables.
2189 */
2190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002191list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002192{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002193 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002195}
2196
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002197#ifdef FEAT_WINDOWS
2198/*
2199 * List tab page variables.
2200 */
2201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002204 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002206}
2207#endif
2208
Bram Moolenaara7043832005-01-21 11:56:39 +00002209/*
2210 * List Vim variables.
2211 */
2212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002213list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216}
2217
2218/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002219 * List script-local variables, if there is a script.
2220 */
2221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002223{
2224 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002225 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2226 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227}
2228
2229/*
2230 * List function variables, if there is a function.
2231 */
2232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002233list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002234{
2235 if (current_funccal != NULL)
2236 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002237 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002238}
2239
2240/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 * List variables in "arg".
2242 */
2243 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002244list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245{
2246 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 char_u *name_start;
2250 char_u *arg_subsc;
2251 char_u *tofree;
2252 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253
2254 while (!ends_excmd(*arg) && !got_int)
2255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002258 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2260 {
2261 emsg_severe = TRUE;
2262 EMSG(_(e_trailing));
2263 break;
2264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 /* get_name_len() takes care of expanding curly braces */
2269 name_start = name = arg;
2270 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2271 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 /* This is mainly to keep test 49 working: when expanding
2274 * curly braces fails overrule the exception error message. */
2275 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 emsg_severe = TRUE;
2278 EMSG2(_(e_invarg2), arg);
2279 break;
2280 }
2281 error = TRUE;
2282 }
2283 else
2284 {
2285 if (tofree != NULL)
2286 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002287 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 else
2290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 /* handle d.key, l[idx], f(expr) */
2292 arg_subsc = arg;
2293 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002301 case 'g': list_glob_vars(first); break;
2302 case 'b': list_buf_vars(first); break;
2303 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002304#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002306#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 case 'v': list_vim_vars(first); break;
2308 case 's': list_script_vars(first); break;
2309 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 default:
2311 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002312 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 }
2314 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 {
2316 char_u numbuf[NUMBUFLEN];
2317 char_u *tf;
2318 int c;
2319 char_u *s;
2320
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002321 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322 c = *arg;
2323 *arg = NUL;
2324 list_one_var_a((char_u *)"",
2325 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002326 tv.v_type,
2327 s == NULL ? (char_u *)"" : s,
2328 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329 *arg = c;
2330 vim_free(tf);
2331 }
2332 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 }
2335 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336
2337 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339
2340 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
2342
2343 return arg;
2344}
2345
2346/*
2347 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2348 * Returns a pointer to the char just after the var name.
2349 * Returns NULL if there is an error.
2350 */
2351 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002352ex_let_one(
2353 char_u *arg, /* points to variable name */
2354 typval_T *tv, /* value to assign to variable */
2355 int copy, /* copy value from "tv" */
2356 char_u *endchars, /* valid chars after variable name or NULL */
2357 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358{
2359 int c1;
2360 char_u *name;
2361 char_u *p;
2362 char_u *arg_end = NULL;
2363 int len;
2364 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366
2367 /*
2368 * ":let $VAR = expr": Set environment variable.
2369 */
2370 if (*arg == '$')
2371 {
2372 /* Find the end of the name. */
2373 ++arg;
2374 name = arg;
2375 len = get_env_len(&arg);
2376 if (len == 0)
2377 EMSG2(_(e_invarg2), name - 1);
2378 else
2379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 if (op != NULL && (*op == '+' || *op == '-'))
2381 EMSG2(_(e_letwrong), op);
2382 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002383 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002385 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 {
2387 c1 = name[len];
2388 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 p = get_tv_string_chk(tv);
2390 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 int mustfree = FALSE;
2393 char_u *s = vim_getenv(name, &mustfree);
2394
2395 if (s != NULL)
2396 {
2397 p = tofree = concat_str(s, p);
2398 if (mustfree)
2399 vim_free(s);
2400 }
2401 }
2402 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002405 if (STRICMP(name, "HOME") == 0)
2406 init_homedir();
2407 else if (didset_vim && STRICMP(name, "VIM") == 0)
2408 didset_vim = FALSE;
2409 else if (didset_vimruntime
2410 && STRICMP(name, "VIMRUNTIME") == 0)
2411 didset_vimruntime = FALSE;
2412 arg_end = arg;
2413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 }
2417 }
2418 }
2419
2420 /*
2421 * ":let &option = expr": Set option value.
2422 * ":let &l:option = expr": Set local option value.
2423 * ":let &g:option = expr": Set global option value.
2424 */
2425 else if (*arg == '&')
2426 {
2427 /* Find the end of the name. */
2428 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429 if (p == NULL || (endchars != NULL
2430 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 EMSG(_(e_letunexp));
2432 else
2433 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 long n;
2435 int opt_type;
2436 long numval;
2437 char_u *stringval = NULL;
2438 char_u *s;
2439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 c1 = *p;
2441 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442
2443 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 s = get_tv_string_chk(tv); /* != NULL if number or string */
2445 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 {
2447 opt_type = get_option_value(arg, &numval,
2448 &stringval, opt_flags);
2449 if ((opt_type == 1 && *op == '.')
2450 || (opt_type == 0 && *op != '.'))
2451 EMSG2(_(e_letwrong), op);
2452 else
2453 {
2454 if (opt_type == 1) /* number */
2455 {
2456 if (*op == '+')
2457 n = numval + n;
2458 else
2459 n = numval - n;
2460 }
2461 else if (opt_type == 0 && stringval != NULL) /* string */
2462 {
2463 s = concat_str(stringval, s);
2464 vim_free(stringval);
2465 stringval = s;
2466 }
2467 }
2468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 if (s != NULL)
2470 {
2471 set_option_value(arg, n, s, opt_flags);
2472 arg_end = p;
2473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
2477 }
2478
2479 /*
2480 * ":let @r = expr": Set register contents.
2481 */
2482 else if (*arg == '@')
2483 {
2484 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 if (op != NULL && (*op == '+' || *op == '-'))
2486 EMSG2(_(e_letwrong), op);
2487 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002488 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002492 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 char_u *s;
2494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 p = get_tv_string_chk(tv);
2496 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002498 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 if (s != NULL)
2500 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002501 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 vim_free(s);
2503 }
2504 }
2505 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002508 arg_end = arg + 1;
2509 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002510 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512 }
2513
2514 /*
2515 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002518 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002520 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002522 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2526 EMSG(_(e_letunexp));
2527 else
2528 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 arg_end = p;
2531 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 }
2535
2536 else
2537 EMSG2(_(e_invarg2), arg);
2538
2539 return arg_end;
2540}
2541
2542/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2544 */
2545 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002546check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547{
2548 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2549 {
2550 EMSG2(_(e_readonlyvar), arg);
2551 return TRUE;
2552 }
2553 return FALSE;
2554}
2555
2556/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 * Get an lval: variable, Dict item or List item that can be assigned a value
2558 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2559 * "name.key", "name.key[expr]" etc.
2560 * Indexing only works if "name" is an existing List or Dictionary.
2561 * "name" points to the start of the name.
2562 * If "rettv" is not NULL it points to the value to be assigned.
2563 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2564 * wrong; must end in space or cmd separator.
2565 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002566 * flags:
2567 * GLV_QUIET: do not give error messages
2568 * GLV_NO_AUTOLOAD: do not use script autoloading
2569 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002571 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 */
2574 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002575get_lval(
2576 char_u *name,
2577 typval_T *rettv,
2578 lval_T *lp,
2579 int unlet,
2580 int skip,
2581 int flags, /* GLV_ values */
2582 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 char_u *expr_start, *expr_end;
2586 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 dictitem_T *v;
2588 typval_T var1;
2589 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002595 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599
2600 if (skip)
2601 {
2602 /* When skipping just find the end of the name. */
2603 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002604 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 }
2606
2607 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002608 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (expr_start != NULL)
2610 {
2611 /* Don't expand the name when we already know there is an error. */
2612 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2613 && *p != '[' && *p != '.')
2614 {
2615 EMSG(_(e_trailing));
2616 return NULL;
2617 }
2618
2619 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2620 if (lp->ll_exp_name == NULL)
2621 {
2622 /* Report an invalid expression in braces, unless the
2623 * expression evaluation has been cancelled due to an
2624 * aborting error, an interrupt, or an exception. */
2625 if (!aborting() && !quiet)
2626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002627 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 EMSG2(_(e_invarg2), name);
2629 return NULL;
2630 }
2631 }
2632 lp->ll_name = lp->ll_exp_name;
2633 }
2634 else
2635 lp->ll_name = name;
2636
2637 /* Without [idx] or .key we are done. */
2638 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2639 return p;
2640
2641 cc = *p;
2642 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002643 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (v == NULL && !quiet)
2645 EMSG2(_(e_undefvar), lp->ll_name);
2646 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 if (v == NULL)
2648 return NULL;
2649
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 /*
2651 * Loop until no more [idx] or .key is following.
2652 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002653 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2657 && !(lp->ll_tv->v_type == VAR_DICT
2658 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!quiet)
2661 EMSG(_("E689: Can only index a List or Dictionary"));
2662 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_("E708: [:] must come last"));
2668 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 len = -1;
2672 if (*p == '.')
2673 {
2674 key = p + 1;
2675 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2676 ;
2677 if (len == 0)
2678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (!quiet)
2680 EMSG(_(e_emptykey));
2681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683 p = key + len;
2684 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 else
2686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (*p == ':')
2690 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 else
2692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 empty1 = FALSE;
2694 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002696 if (get_tv_string_chk(&var1) == NULL)
2697 {
2698 /* not a number or string */
2699 clear_tv(&var1);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
2703
2704 /* Optionally get the second index [ :expr]. */
2705 if (*p == ':')
2706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 if (!empty1)
2712 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (rettv != NULL && (rettv->v_type != VAR_LIST
2716 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (!empty1)
2721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 p = skipwhite(p + 1);
2725 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 else
2728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2731 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (!empty1)
2733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736 if (get_tv_string_chk(&var2) == NULL)
2737 {
2738 /* not a number or string */
2739 if (!empty1)
2740 clear_tv(&var1);
2741 clear_tv(&var2);
2742 return NULL;
2743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (!quiet)
2753 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (!empty1)
2755 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760
2761 /* Skip to past ']'. */
2762 ++p;
2763 }
2764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
2767 if (len == -1)
2768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002770 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (*key == NUL)
2772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (!quiet)
2774 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 }
2778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 lp->ll_list = NULL;
2780 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 /* When assigning to a scope dictionary check that a function and
2784 * variable name is valid (only variable name unless it is l: or
2785 * g: dictionary). Disallow overwriting a builtin function. */
2786 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 int prevval;
2789 int wrong;
2790
2791 if (len != -1)
2792 {
2793 prevval = key[len];
2794 key[len] = NUL;
2795 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002796 else
2797 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002798 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2799 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 || !valid_varname(key);
2802 if (len != -1)
2803 key[len] = prevval;
2804 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002805 return NULL;
2806 }
2807
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* Can't add "v:" variable. */
2811 if (lp->ll_dict == &vimvardict)
2812 {
2813 EMSG2(_(e_illvar), name);
2814 return NULL;
2815 }
2816
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002817 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 if (len == -1)
2823 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 p = NULL;
2834 break;
2835 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002837 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 return NULL;
2839
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 if (len == -1)
2841 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 }
2844 else
2845 {
2846 /*
2847 * Get the number and item for the only or first index of the List.
2848 */
2849 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 else
2852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002853 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 clear_tv(&var1);
2855 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002856 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 lp->ll_list = lp->ll_tv->vval.v_list;
2858 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2859 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002861 if (lp->ll_n1 < 0)
2862 {
2863 lp->ll_n1 = 0;
2864 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2865 }
2866 }
2867 if (lp->ll_li == NULL)
2868 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002871 if (!quiet)
2872 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 }
2875
2876 /*
2877 * May need to find the item or absolute index for the second
2878 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 * When no index given: "lp->ll_empty2" is TRUE.
2880 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002884 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002890 {
2891 if (!quiet)
2892 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002894 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 }
2897
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2899 if (lp->ll_n1 < 0)
2900 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2901 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002902 {
2903 if (!quiet)
2904 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002906 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002907 }
2908
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 }
2912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 return p;
2914}
2915
2916/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 */
2919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921{
2922 vim_free(lp->ll_exp_name);
2923 vim_free(lp->ll_newkey);
2924}
2925
2926/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002927 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 */
2931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932set_var_lval(
2933 lval_T *lp,
2934 char_u *endp,
2935 typval_T *rettv,
2936 int copy,
2937 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938{
2939 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 listitem_T *ri;
2941 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942
2943 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002946 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 cc = *endp;
2948 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 if (op != NULL && *op != '=')
2950 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002954 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002955 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002956 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002958 if ((di == NULL
2959 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2960 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2961 FALSE)))
2962 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 set_var(lp->ll_name, &tv, FALSE);
2964 clear_tv(&tv);
2965 }
2966 }
2967 else
2968 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002972 else if (tv_check_lock(lp->ll_newkey == NULL
2973 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002974 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 else if (lp->ll_range)
2977 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 listitem_T *ll_li = lp->ll_li;
2979 int ll_n1 = lp->ll_n1;
2980
2981 /*
2982 * Check whether any of the list items is locked
2983 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002984 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002985 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002986 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002987 return;
2988 ri = ri->li_next;
2989 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2990 break;
2991 ll_li = ll_li->li_next;
2992 ++ll_n1;
2993 }
2994
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /*
2996 * Assign the List values to the list items.
2997 */
2998 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 if (op != NULL && *op != '=')
3001 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3002 else
3003 {
3004 clear_tv(&lp->ll_li->li_tv);
3005 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 ri = ri->li_next;
3008 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3009 break;
3010 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003013 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 ri = NULL;
3016 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 lp->ll_li = lp->ll_li->li_next;
3020 ++lp->ll_n1;
3021 }
3022 if (ri != NULL)
3023 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 else if (lp->ll_empty2
3025 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003026 : lp->ll_n1 != lp->ll_n2)
3027 EMSG(_("E711: List value has not enough items"));
3028 }
3029 else
3030 {
3031 /*
3032 * Assign to a List or Dictionary item.
3033 */
3034 if (lp->ll_newkey != NULL)
3035 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 if (op != NULL && *op != '=')
3037 {
3038 EMSG2(_(e_letwrong), op);
3039 return;
3040 }
3041
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003043 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 if (di == NULL)
3045 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3047 {
3048 vim_free(di);
3049 return;
3050 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 else if (op != NULL && *op != '=')
3054 {
3055 tv_op(lp->ll_tv, rettv, op);
3056 return;
3057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 /*
3062 * Assign the value to the variable or list item.
3063 */
3064 if (copy)
3065 copy_tv(rettv, lp->ll_tv);
3066 else
3067 {
3068 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003069 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003070 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 }
3072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003073}
3074
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003075/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3077 * Returns OK or FAIL.
3078 */
3079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081{
3082 long n;
3083 char_u numbuf[NUMBUFLEN];
3084 char_u *s;
3085
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003086 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3087 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3088 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 {
3090 switch (tv1->v_type)
3091 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003092 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 case VAR_DICT:
3094 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003095 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003096 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003097 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 break;
3099
3100 case VAR_LIST:
3101 if (*op != '+' || tv2->v_type != VAR_LIST)
3102 break;
3103 /* List += List */
3104 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3105 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3106 return OK;
3107
3108 case VAR_NUMBER:
3109 case VAR_STRING:
3110 if (tv2->v_type == VAR_LIST)
3111 break;
3112 if (*op == '+' || *op == '-')
3113 {
3114 /* nr += nr or nr -= nr*/
3115 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116#ifdef FEAT_FLOAT
3117 if (tv2->v_type == VAR_FLOAT)
3118 {
3119 float_T f = n;
3120
3121 if (*op == '+')
3122 f += tv2->vval.v_float;
3123 else
3124 f -= tv2->vval.v_float;
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_FLOAT;
3127 tv1->vval.v_float = f;
3128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#endif
3131 {
3132 if (*op == '+')
3133 n += get_tv_number(tv2);
3134 else
3135 n -= get_tv_number(tv2);
3136 clear_tv(tv1);
3137 tv1->v_type = VAR_NUMBER;
3138 tv1->vval.v_number = n;
3139 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003140 }
3141 else
3142 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143 if (tv2->v_type == VAR_FLOAT)
3144 break;
3145
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 /* str .= str */
3147 s = get_tv_string(tv1);
3148 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3149 clear_tv(tv1);
3150 tv1->v_type = VAR_STRING;
3151 tv1->vval.v_string = s;
3152 }
3153 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154
3155#ifdef FEAT_FLOAT
3156 case VAR_FLOAT:
3157 {
3158 float_T f;
3159
3160 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3161 && tv2->v_type != VAR_NUMBER
3162 && tv2->v_type != VAR_STRING))
3163 break;
3164 if (tv2->v_type == VAR_FLOAT)
3165 f = tv2->vval.v_float;
3166 else
3167 f = get_tv_number(tv2);
3168 if (*op == '+')
3169 tv1->vval.v_float += f;
3170 else
3171 tv1->vval.v_float -= f;
3172 }
3173 return OK;
3174#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003175 }
3176 }
3177
3178 EMSG2(_(e_letwrong), op);
3179 return FAIL;
3180}
3181
3182/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 * Add a watcher to a list.
3184 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003186list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187{
3188 lw->lw_next = l->lv_watch;
3189 l->lv_watch = lw;
3190}
3191
3192/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003193 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 * No warning when it isn't found...
3195 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003197list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198{
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200
3201 lwp = &l->lv_watch;
3202 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3203 {
3204 if (lw == lwrem)
3205 {
3206 *lwp = lw->lw_next;
3207 break;
3208 }
3209 lwp = &lw->lw_next;
3210 }
3211}
3212
3213/*
3214 * Just before removing an item from a list: advance watchers to the next
3215 * item.
3216 */
3217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219{
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221
3222 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3223 if (lw->lw_item == item)
3224 lw->lw_item = item->li_next;
3225}
3226
3227/*
3228 * Evaluate the expression used in a ":for var in expr" command.
3229 * "arg" points to "var".
3230 * Set "*errp" to TRUE for an error, FALSE otherwise;
3231 * Return a pointer that holds the info. Null when there is an error.
3232 */
3233 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003234eval_for_line(
3235 char_u *arg,
3236 int *errp,
3237 char_u **nextcmdp,
3238 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239{
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 typval_T tv;
3243 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244
3245 *errp = TRUE; /* default: there is an error */
3246
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 if (fi == NULL)
3249 return NULL;
3250
3251 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3252 if (expr == NULL)
3253 return fi;
3254
3255 expr = skipwhite(expr);
3256 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3257 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003258 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 return fi;
3260 }
3261
3262 if (skip)
3263 ++emsg_skip;
3264 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3265 {
3266 *errp = FALSE;
3267 if (!skip)
3268 {
3269 l = tv.vval.v_list;
3270 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003273 clear_tv(&tv);
3274 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 else
3276 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003277 /* No need to increment the refcount, it's already set for the
3278 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 fi->fi_list = l;
3280 list_add_watch(l, &fi->fi_lw);
3281 fi->fi_lw.lw_item = l->lv_first;
3282 }
3283 }
3284 }
3285 if (skip)
3286 --emsg_skip;
3287
3288 return fi;
3289}
3290
3291/*
3292 * Use the first item in a ":for" list. Advance to the next.
3293 * Assign the values to the variable (list). "arg" points to the first one.
3294 * Return TRUE when a valid item was found, FALSE when at end of list or
3295 * something wrong.
3296 */
3297 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003298next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003300 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303
3304 item = fi->fi_lw.lw_item;
3305 if (item == NULL)
3306 result = FALSE;
3307 else
3308 {
3309 fi->fi_lw.lw_item = item->li_next;
3310 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3311 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3312 }
3313 return result;
3314}
3315
3316/*
3317 * Free the structure used to store info used by ":for".
3318 */
3319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321{
Bram Moolenaar33570922005-01-25 22:26:29 +00003322 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003324 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003327 list_unref(fi->fi_list);
3328 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 vim_free(fi);
3330}
3331
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3333
3334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003335set_context_for_expression(
3336 expand_T *xp,
3337 char_u *arg,
3338 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 int got_eq = FALSE;
3341 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 if (cmdidx == CMD_let)
3345 {
3346 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003347 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 {
3349 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003350 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 {
3352 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003353 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003354 if (vim_iswhite(*p))
3355 break;
3356 }
3357 return;
3358 }
3359 }
3360 else
3361 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3362 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 while ((xp->xp_pattern = vim_strpbrk(arg,
3364 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3365 {
3366 c = *xp->xp_pattern;
3367 if (c == '&')
3368 {
3369 c = xp->xp_pattern[1];
3370 if (c == '&')
3371 {
3372 ++xp->xp_pattern;
3373 xp->xp_context = cmdidx != CMD_let || got_eq
3374 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3375 }
3376 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003379 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3380 xp->xp_pattern += 2;
3381
3382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 else if (c == '$')
3385 {
3386 /* environment variable */
3387 xp->xp_context = EXPAND_ENV_VARS;
3388 }
3389 else if (c == '=')
3390 {
3391 got_eq = TRUE;
3392 xp->xp_context = EXPAND_EXPRESSION;
3393 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003394 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 && xp->xp_context == EXPAND_FUNCTIONS
3396 && vim_strchr(xp->xp_pattern, '(') == NULL)
3397 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003398 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 break;
3400 }
3401 else if (cmdidx != CMD_let || got_eq)
3402 {
3403 if (c == '"') /* string */
3404 {
3405 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3406 if (c == '\\' && xp->xp_pattern[1] != NUL)
3407 ++xp->xp_pattern;
3408 xp->xp_context = EXPAND_NOTHING;
3409 }
3410 else if (c == '\'') /* literal string */
3411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003412 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3414 /* skip */ ;
3415 xp->xp_context = EXPAND_NOTHING;
3416 }
3417 else if (c == '|')
3418 {
3419 if (xp->xp_pattern[1] == '|')
3420 {
3421 ++xp->xp_pattern;
3422 xp->xp_context = EXPAND_EXPRESSION;
3423 }
3424 else
3425 xp->xp_context = EXPAND_COMMANDS;
3426 }
3427 else
3428 xp->xp_context = EXPAND_EXPRESSION;
3429 }
3430 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003431 /* Doesn't look like something valid, expand as an expression
3432 * anyway. */
3433 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 arg = xp->xp_pattern;
3435 if (*arg != NUL)
3436 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3437 /* skip */ ;
3438 }
3439 xp->xp_pattern = arg;
3440}
3441
3442#endif /* FEAT_CMDL_COMPL */
3443
3444/*
3445 * ":1,25call func(arg1, arg2)" function call.
3446 */
3447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 char_u *arg = eap->arg;
3451 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003453 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003455 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 linenr_T lnum;
3457 int doesrange;
3458 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003461 if (eap->skip)
3462 {
3463 /* trans_function_name() doesn't work well when skipping, use eval0()
3464 * instead to skip to any following command, e.g. for:
3465 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003466 ++emsg_skip;
3467 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3468 clear_tv(&rettv);
3469 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003470 return;
3471 }
3472
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003473 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003474 if (fudi.fd_newkey != NULL)
3475 {
3476 /* Still need to give an error message for missing key. */
3477 EMSG2(_(e_dictkey), fudi.fd_newkey);
3478 vim_free(fudi.fd_newkey);
3479 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003480 if (tofree == NULL)
3481 return;
3482
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003483 /* Increase refcount on dictionary, it could get deleted when evaluating
3484 * the arguments. */
3485 if (fudi.fd_dict != NULL)
3486 ++fudi.fd_dict->dv_refcount;
3487
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003488 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003489 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003490 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
Bram Moolenaar532c7802005-01-27 14:44:31 +00003492 /* Skip white space to allow ":call func ()". Not good, but required for
3493 * backward compatibility. */
3494 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003495 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497 if (*startarg != '(')
3498 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003499 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 goto end;
3501 }
3502
3503 /*
3504 * When skipping, evaluate the function once, to find the end of the
3505 * arguments.
3506 * When the function takes a range, this is discovered after the first
3507 * call, and the loop is broken.
3508 */
3509 if (eap->skip)
3510 {
3511 ++emsg_skip;
3512 lnum = eap->line2; /* do it once, also with an invalid range */
3513 }
3514 else
3515 lnum = eap->line1;
3516 for ( ; lnum <= eap->line2; ++lnum)
3517 {
3518 if (!eap->skip && eap->addr_count > 0)
3519 {
3520 curwin->w_cursor.lnum = lnum;
3521 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003522#ifdef FEAT_VIRTUALEDIT
3523 curwin->w_cursor.coladd = 0;
3524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003527 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003528 eap->line1, eap->line2, &doesrange,
3529 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
3531 failed = TRUE;
3532 break;
3533 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003534
3535 /* Handle a function returning a Funcref, Dictionary or List. */
3536 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3537 {
3538 failed = TRUE;
3539 break;
3540 }
3541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (doesrange || eap->skip)
3544 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003548 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003549 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (aborting())
3551 break;
3552 }
3553 if (eap->skip)
3554 --emsg_skip;
3555
3556 if (!failed)
3557 {
3558 /* Check for trailing illegal characters and a following command. */
3559 if (!ends_excmd(*arg))
3560 {
3561 emsg_severe = TRUE;
3562 EMSG(_(e_trailing));
3563 }
3564 else
3565 eap->nextcmd = check_nextcmd(arg);
3566 }
3567
3568end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003569 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003570 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
3572
3573/*
3574 * ":unlet[!] var1 ... " command.
3575 */
3576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003577ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 ex_unletlock(eap, eap->arg, 0);
3580}
3581
3582/*
3583 * ":lockvar" and ":unlockvar" commands
3584 */
3585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003586ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 int deep = 2;
3590
3591 if (eap->forceit)
3592 deep = -1;
3593 else if (vim_isdigit(*arg))
3594 {
3595 deep = getdigits(&arg);
3596 arg = skipwhite(arg);
3597 }
3598
3599 ex_unletlock(eap, arg, deep);
3600}
3601
3602/*
3603 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3604 */
3605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003606ex_unletlock(
3607 exarg_T *eap,
3608 char_u *argstart,
3609 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610{
3611 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003614 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 do
3617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003619 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003620 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (lv.ll_name == NULL)
3622 error = TRUE; /* error but continue parsing */
3623 if (name_end == NULL || (!vim_iswhite(*name_end)
3624 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 if (name_end != NULL)
3627 {
3628 emsg_severe = TRUE;
3629 EMSG(_(e_trailing));
3630 }
3631 if (!(eap->skip || error))
3632 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 break;
3634 }
3635
3636 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 {
3638 if (eap->cmdidx == CMD_unlet)
3639 {
3640 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3641 error = TRUE;
3642 }
3643 else
3644 {
3645 if (do_lock_var(&lv, name_end, deep,
3646 eap->cmdidx == CMD_lockvar) == FAIL)
3647 error = TRUE;
3648 }
3649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 if (!eap->skip)
3652 clear_lval(&lv);
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 arg = skipwhite(name_end);
3655 } while (!ends_excmd(*arg));
3656
3657 eap->nextcmd = check_nextcmd(arg);
3658}
3659
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003661do_unlet_var(
3662 lval_T *lp,
3663 char_u *name_end,
3664 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 int ret = OK;
3667 int cc;
3668
3669 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 cc = *name_end;
3672 *name_end = NUL;
3673
3674 /* Normal name or expanded name. */
3675 if (check_changedtick(lp->ll_name))
3676 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003678 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003680 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003681 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003682 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003683 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003684 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 else if (lp->ll_range)
3687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003688 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003690 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003691
3692 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3693 {
3694 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003695 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003696 return FAIL;
3697 ll_li = li;
3698 ++ll_n1;
3699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700
3701 /* Delete a range of List items. */
3702 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3703 {
3704 li = lp->ll_li->li_next;
3705 listitem_remove(lp->ll_list, lp->ll_li);
3706 lp->ll_li = li;
3707 ++lp->ll_n1;
3708 }
3709 }
3710 else
3711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 /* unlet a List item. */
3714 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003717 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 }
3719
3720 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721}
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723/*
3724 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 */
3727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003728do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 hashtab_T *ht;
3731 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003732 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003733 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003734 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 ht = find_var_ht(name, &varname);
3737 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003739 if (ht == &globvarht)
3740 d = &globvardict;
3741 else if (current_funccal != NULL
3742 && ht == &current_funccal->l_vars.dv_hashtab)
3743 d = &current_funccal->l_vars;
3744 else if (ht == &compat_hashtab)
3745 d = &vimvardict;
3746 else
3747 {
3748 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3749 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3750 }
3751 if (d == NULL)
3752 {
3753 EMSG2(_(e_intern2), "do_unlet()");
3754 return FAIL;
3755 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 hi = hash_find(ht, varname);
3757 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003758 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003759 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003760 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003761 || var_check_ro(di->di_flags, name, FALSE)
3762 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003763 return FAIL;
3764
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003765 delete_var(ht, hi);
3766 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003769 if (forceit)
3770 return OK;
3771 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 return FAIL;
3773}
3774
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775/*
3776 * Lock or unlock variable indicated by "lp".
3777 * "deep" is the levels to go (-1 for unlimited);
3778 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3779 */
3780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003781do_lock_var(
3782 lval_T *lp,
3783 char_u *name_end,
3784 int deep,
3785 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003786{
3787 int ret = OK;
3788 int cc;
3789 dictitem_T *di;
3790
3791 if (deep == 0) /* nothing to do */
3792 return OK;
3793
3794 if (lp->ll_tv == NULL)
3795 {
3796 cc = *name_end;
3797 *name_end = NUL;
3798
3799 /* Normal name or expanded name. */
3800 if (check_changedtick(lp->ll_name))
3801 ret = FAIL;
3802 else
3803 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003804 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 if (di == NULL)
3806 ret = FAIL;
3807 else
3808 {
3809 if (lock)
3810 di->di_flags |= DI_FLAGS_LOCK;
3811 else
3812 di->di_flags &= ~DI_FLAGS_LOCK;
3813 item_lock(&di->di_tv, deep, lock);
3814 }
3815 }
3816 *name_end = cc;
3817 }
3818 else if (lp->ll_range)
3819 {
3820 listitem_T *li = lp->ll_li;
3821
3822 /* (un)lock a range of List items. */
3823 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3824 {
3825 item_lock(&li->li_tv, deep, lock);
3826 li = li->li_next;
3827 ++lp->ll_n1;
3828 }
3829 }
3830 else if (lp->ll_list != NULL)
3831 /* (un)lock a List item. */
3832 item_lock(&lp->ll_li->li_tv, deep, lock);
3833 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003834 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003835 item_lock(&lp->ll_di->di_tv, deep, lock);
3836
3837 return ret;
3838}
3839
3840/*
3841 * Lock or unlock an item. "deep" is nr of levels to go.
3842 */
3843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003844item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003845{
3846 static int recurse = 0;
3847 list_T *l;
3848 listitem_T *li;
3849 dict_T *d;
3850 hashitem_T *hi;
3851 int todo;
3852
3853 if (recurse >= DICT_MAXNEST)
3854 {
3855 EMSG(_("E743: variable nested too deep for (un)lock"));
3856 return;
3857 }
3858 if (deep == 0)
3859 return;
3860 ++recurse;
3861
3862 /* lock/unlock the item itself */
3863 if (lock)
3864 tv->v_lock |= VAR_LOCKED;
3865 else
3866 tv->v_lock &= ~VAR_LOCKED;
3867
3868 switch (tv->v_type)
3869 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003870 case VAR_UNKNOWN:
3871 case VAR_NUMBER:
3872 case VAR_STRING:
3873 case VAR_FUNC:
3874 case VAR_FLOAT:
3875 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003876 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003877 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003878 break;
3879
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003880 case VAR_LIST:
3881 if ((l = tv->vval.v_list) != NULL)
3882 {
3883 if (lock)
3884 l->lv_lock |= VAR_LOCKED;
3885 else
3886 l->lv_lock &= ~VAR_LOCKED;
3887 if (deep < 0 || deep > 1)
3888 /* recursive: lock/unlock the items the List contains */
3889 for (li = l->lv_first; li != NULL; li = li->li_next)
3890 item_lock(&li->li_tv, deep - 1, lock);
3891 }
3892 break;
3893 case VAR_DICT:
3894 if ((d = tv->vval.v_dict) != NULL)
3895 {
3896 if (lock)
3897 d->dv_lock |= VAR_LOCKED;
3898 else
3899 d->dv_lock &= ~VAR_LOCKED;
3900 if (deep < 0 || deep > 1)
3901 {
3902 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003903 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003904 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3905 {
3906 if (!HASHITEM_EMPTY(hi))
3907 {
3908 --todo;
3909 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3910 }
3911 }
3912 }
3913 }
3914 }
3915 --recurse;
3916}
3917
Bram Moolenaara40058a2005-07-11 22:42:07 +00003918/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003919 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3920 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003921 */
3922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003923tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003924{
3925 return (tv->v_lock & VAR_LOCKED)
3926 || (tv->v_type == VAR_LIST
3927 && tv->vval.v_list != NULL
3928 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3929 || (tv->v_type == VAR_DICT
3930 && tv->vval.v_dict != NULL
3931 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3932}
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3935/*
3936 * Delete all "menutrans_" variables.
3937 */
3938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003939del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003945 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 {
3948 if (!HASHITEM_EMPTY(hi))
3949 {
3950 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3952 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 }
3954 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956}
3957#endif
3958
3959#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3960
3961/*
3962 * Local string buffer for the next two functions to store a variable name
3963 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3964 * get_user_var_name().
3965 */
3966
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003967static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969static char_u *varnamebuf = NULL;
3970static int varnamebuflen = 0;
3971
3972/*
3973 * Function to concatenate a prefix and a variable name.
3974 */
3975 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003976cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 int len;
3979
3980 len = (int)STRLEN(name) + 3;
3981 if (len > varnamebuflen)
3982 {
3983 vim_free(varnamebuf);
3984 len += 10; /* some additional space */
3985 varnamebuf = alloc(len);
3986 if (varnamebuf == NULL)
3987 {
3988 varnamebuflen = 0;
3989 return NULL;
3990 }
3991 varnamebuflen = len;
3992 }
3993 *varnamebuf = prefix;
3994 varnamebuf[1] = ':';
3995 STRCPY(varnamebuf + 2, name);
3996 return varnamebuf;
3997}
3998
3999/*
4000 * Function given to ExpandGeneric() to obtain the list of user defined
4001 * (global/buffer/window/built-in) variable names.
4002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004004get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004006 static long_u gdone;
4007 static long_u bdone;
4008 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004009#ifdef FEAT_WINDOWS
4010 static long_u tdone;
4011#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004012 static int vidx;
4013 static hashitem_T *hi;
4014 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019#ifdef FEAT_WINDOWS
4020 tdone = 0;
4021#endif
4022 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004023
4024 /* Global variables */
4025 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004029 else
4030 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 while (HASHITEM_EMPTY(hi))
4032 ++hi;
4033 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4034 return cat_prefix_varname('g', hi->hi_key);
4035 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004037
4038 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004039 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004044 else
4045 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 while (HASHITEM_EMPTY(hi))
4047 ++hi;
4048 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return (char_u *)"b:changedtick";
4054 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004055
4056 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004057 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004060 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 else
4063 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004064 while (HASHITEM_EMPTY(hi))
4065 ++hi;
4066 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004068
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069#ifdef FEAT_WINDOWS
4070 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004071 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072 if (tdone < ht->ht_used)
4073 {
4074 if (tdone++ == 0)
4075 hi = ht->ht_array;
4076 else
4077 ++hi;
4078 while (HASHITEM_EMPTY(hi))
4079 ++hi;
4080 return cat_prefix_varname('t', hi->hi_key);
4081 }
4082#endif
4083
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 /* v: variables */
4085 if (vidx < VV_LEN)
4086 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 vim_free(varnamebuf);
4089 varnamebuf = NULL;
4090 varnamebuflen = 0;
4091 return NULL;
4092}
4093
4094#endif /* FEAT_CMDL_COMPL */
4095
4096/*
4097 * types for expressions.
4098 */
4099typedef enum
4100{
4101 TYPE_UNKNOWN = 0
4102 , TYPE_EQUAL /* == */
4103 , TYPE_NEQUAL /* != */
4104 , TYPE_GREATER /* > */
4105 , TYPE_GEQUAL /* >= */
4106 , TYPE_SMALLER /* < */
4107 , TYPE_SEQUAL /* <= */
4108 , TYPE_MATCH /* =~ */
4109 , TYPE_NOMATCH /* !~ */
4110} exptype_T;
4111
4112/*
4113 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4116 */
4117
4118/*
4119 * Handle zero level expression.
4120 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004122 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 * Return OK or FAIL.
4124 */
4125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004126eval0(
4127 char_u *arg,
4128 typval_T *rettv,
4129 char_u **nextcmd,
4130 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131{
4132 int ret;
4133 char_u *p;
4134
4135 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (ret == FAIL || !ends_excmd(*p))
4138 {
4139 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 /*
4142 * Report the invalid expression unless the expression evaluation has
4143 * been cancelled due to an aborting error, an interrupt, or an
4144 * exception.
4145 */
4146 if (!aborting())
4147 EMSG2(_(e_invexpr2), arg);
4148 ret = FAIL;
4149 }
4150 if (nextcmd != NULL)
4151 *nextcmd = check_nextcmd(p);
4152
4153 return ret;
4154}
4155
4156/*
4157 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004158 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 *
4160 * "arg" must point to the first non-white of the expression.
4161 * "arg" is advanced to the next non-white after the recognized expression.
4162 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004163 * Note: "rettv.v_lock" is not set.
4164 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * Return OK or FAIL.
4166 */
4167 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004168eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
4170 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 if ((*arg)[0] == '?')
4180 {
4181 result = FALSE;
4182 if (evaluate)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 int error = FALSE;
4185
4186 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (error)
4190 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192
4193 /*
4194 * Get the second variable.
4195 */
4196 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 return FAIL;
4199
4200 /*
4201 * Check for the ":".
4202 */
4203 if ((*arg)[0] != ':')
4204 {
4205 EMSG(_("E109: Missing ':' after '?'"));
4206 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return FAIL;
4209 }
4210
4211 /*
4212 * Get the third variable.
4213 */
4214 *arg = skipwhite(*arg + 1);
4215 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4216 {
4217 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return FAIL;
4220 }
4221 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224
4225 return OK;
4226}
4227
4228/*
4229 * Handle first level expression:
4230 * expr2 || expr2 || expr2 logical OR
4231 *
4232 * "arg" must point to the first non-white of the expression.
4233 * "arg" is advanced to the next non-white after the recognized expression.
4234 *
4235 * Return OK or FAIL.
4236 */
4237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004238eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
Bram Moolenaar33570922005-01-25 22:26:29 +00004240 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 long result;
4242 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 /*
4246 * Get the first variable.
4247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 return FAIL;
4250
4251 /*
4252 * Repeat until there is no following "||".
4253 */
4254 first = TRUE;
4255 result = FALSE;
4256 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4257 {
4258 if (evaluate && first)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (error)
4264 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 first = FALSE;
4266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 2);
4272 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4273 return FAIL;
4274
4275 /*
4276 * Compute the result.
4277 */
4278 if (evaluate && !result)
4279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (error)
4284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 if (evaluate)
4287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 rettv->v_type = VAR_NUMBER;
4289 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
4292
4293 return OK;
4294}
4295
4296/*
4297 * Handle second level expression:
4298 * expr3 && expr3 && expr3 logical AND
4299 *
4300 * "arg" must point to the first non-white of the expression.
4301 * "arg" is advanced to the next non-white after the recognized expression.
4302 *
4303 * Return OK or FAIL.
4304 */
4305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004306eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
Bram Moolenaar33570922005-01-25 22:26:29 +00004308 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 long result;
4310 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 /*
4314 * Get the first variable.
4315 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 return FAIL;
4318
4319 /*
4320 * Repeat until there is no following "&&".
4321 */
4322 first = TRUE;
4323 result = TRUE;
4324 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4325 {
4326 if (evaluate && first)
4327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004328 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 if (error)
4332 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 first = FALSE;
4334 }
4335
4336 /*
4337 * Get the second variable.
4338 */
4339 *arg = skipwhite(*arg + 2);
4340 if (eval4(arg, &var2, evaluate && result) == FAIL)
4341 return FAIL;
4342
4343 /*
4344 * Compute the result.
4345 */
4346 if (evaluate && result)
4347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (error)
4352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 if (evaluate)
4355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 rettv->v_type = VAR_NUMBER;
4357 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 }
4360
4361 return OK;
4362}
4363
4364/*
4365 * Handle third level expression:
4366 * var1 == var2
4367 * var1 =~ var2
4368 * var1 != var2
4369 * var1 !~ var2
4370 * var1 > var2
4371 * var1 >= var2
4372 * var1 < var2
4373 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 * var1 is var2
4375 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 *
4377 * "arg" must point to the first non-white of the expression.
4378 * "arg" is advanced to the next non-white after the recognized expression.
4379 *
4380 * Return OK or FAIL.
4381 */
4382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004383eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
Bram Moolenaar33570922005-01-25 22:26:29 +00004385 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 char_u *p;
4387 int i;
4388 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 int len = 2;
4391 long n1, n2;
4392 char_u *s1, *s2;
4393 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4394 regmatch_T regmatch;
4395 int ic;
4396 char_u *save_cpo;
4397
4398 /*
4399 * Get the first variable.
4400 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004401 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 return FAIL;
4403
4404 p = *arg;
4405 switch (p[0])
4406 {
4407 case '=': if (p[1] == '=')
4408 type = TYPE_EQUAL;
4409 else if (p[1] == '~')
4410 type = TYPE_MATCH;
4411 break;
4412 case '!': if (p[1] == '=')
4413 type = TYPE_NEQUAL;
4414 else if (p[1] == '~')
4415 type = TYPE_NOMATCH;
4416 break;
4417 case '>': if (p[1] != '=')
4418 {
4419 type = TYPE_GREATER;
4420 len = 1;
4421 }
4422 else
4423 type = TYPE_GEQUAL;
4424 break;
4425 case '<': if (p[1] != '=')
4426 {
4427 type = TYPE_SMALLER;
4428 len = 1;
4429 }
4430 else
4431 type = TYPE_SEQUAL;
4432 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 case 'i': if (p[1] == 's')
4434 {
4435 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4436 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004437 i = p[len];
4438 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 {
4440 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4441 type_is = TRUE;
4442 }
4443 }
4444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446
4447 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004448 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 */
4450 if (type != TYPE_UNKNOWN)
4451 {
4452 /* extra question mark appended: ignore case */
4453 if (p[len] == '?')
4454 {
4455 ic = TRUE;
4456 ++len;
4457 }
4458 /* extra '#' appended: match case */
4459 else if (p[len] == '#')
4460 {
4461 ic = FALSE;
4462 ++len;
4463 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004464 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 else
4466 ic = p_ic;
4467
4468 /*
4469 * Get the second variable.
4470 */
4471 *arg = skipwhite(p + len);
4472 if (eval5(arg, &var2, evaluate) == FAIL)
4473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 return FAIL;
4476 }
4477
4478 if (evaluate)
4479 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004480 if (type_is && rettv->v_type != var2.v_type)
4481 {
4482 /* For "is" a different type always means FALSE, for "notis"
4483 * it means TRUE. */
4484 n1 = (type == TYPE_NEQUAL);
4485 }
4486 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4487 {
4488 if (type_is)
4489 {
4490 n1 = (rettv->v_type == var2.v_type
4491 && rettv->vval.v_list == var2.vval.v_list);
4492 if (type == TYPE_NEQUAL)
4493 n1 = !n1;
4494 }
4495 else if (rettv->v_type != var2.v_type
4496 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4497 {
4498 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004499 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004500 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004501 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 clear_tv(rettv);
4503 clear_tv(&var2);
4504 return FAIL;
4505 }
4506 else
4507 {
4508 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004509 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4510 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004511 if (type == TYPE_NEQUAL)
4512 n1 = !n1;
4513 }
4514 }
4515
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004516 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4517 {
4518 if (type_is)
4519 {
4520 n1 = (rettv->v_type == var2.v_type
4521 && rettv->vval.v_dict == var2.vval.v_dict);
4522 if (type == TYPE_NEQUAL)
4523 n1 = !n1;
4524 }
4525 else if (rettv->v_type != var2.v_type
4526 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4527 {
4528 if (rettv->v_type != var2.v_type)
4529 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4530 else
4531 EMSG(_("E736: Invalid operation for Dictionary"));
4532 clear_tv(rettv);
4533 clear_tv(&var2);
4534 return FAIL;
4535 }
4536 else
4537 {
4538 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004539 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4540 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004541 if (type == TYPE_NEQUAL)
4542 n1 = !n1;
4543 }
4544 }
4545
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004546 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4547 {
4548 if (rettv->v_type != var2.v_type
4549 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4550 {
4551 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004552 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004554 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 clear_tv(rettv);
4556 clear_tv(&var2);
4557 return FAIL;
4558 }
4559 else
4560 {
4561 /* Compare two Funcrefs for being equal or unequal. */
4562 if (rettv->vval.v_string == NULL
4563 || var2.vval.v_string == NULL)
4564 n1 = FALSE;
4565 else
4566 n1 = STRCMP(rettv->vval.v_string,
4567 var2.vval.v_string) == 0;
4568 if (type == TYPE_NEQUAL)
4569 n1 = !n1;
4570 }
4571 }
4572
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004573#ifdef FEAT_FLOAT
4574 /*
4575 * If one of the two variables is a float, compare as a float.
4576 * When using "=~" or "!~", always compare as string.
4577 */
4578 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4579 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4580 {
4581 float_T f1, f2;
4582
4583 if (rettv->v_type == VAR_FLOAT)
4584 f1 = rettv->vval.v_float;
4585 else
4586 f1 = get_tv_number(rettv);
4587 if (var2.v_type == VAR_FLOAT)
4588 f2 = var2.vval.v_float;
4589 else
4590 f2 = get_tv_number(&var2);
4591 n1 = FALSE;
4592 switch (type)
4593 {
4594 case TYPE_EQUAL: n1 = (f1 == f2); break;
4595 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4596 case TYPE_GREATER: n1 = (f1 > f2); break;
4597 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4598 case TYPE_SMALLER: n1 = (f1 < f2); break;
4599 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4600 case TYPE_UNKNOWN:
4601 case TYPE_MATCH:
4602 case TYPE_NOMATCH: break; /* avoid gcc warning */
4603 }
4604 }
4605#endif
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 /*
4608 * If one of the two variables is a number, compare as a number.
4609 * When using "=~" or "!~", always compare as string.
4610 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004611 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 n1 = get_tv_number(rettv);
4615 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 switch (type)
4617 {
4618 case TYPE_EQUAL: n1 = (n1 == n2); break;
4619 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4620 case TYPE_GREATER: n1 = (n1 > n2); break;
4621 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4622 case TYPE_SMALLER: n1 = (n1 < n2); break;
4623 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4624 case TYPE_UNKNOWN:
4625 case TYPE_MATCH:
4626 case TYPE_NOMATCH: break; /* avoid gcc warning */
4627 }
4628 }
4629 else
4630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 s1 = get_tv_string_buf(rettv, buf1);
4632 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4634 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4635 else
4636 i = 0;
4637 n1 = FALSE;
4638 switch (type)
4639 {
4640 case TYPE_EQUAL: n1 = (i == 0); break;
4641 case TYPE_NEQUAL: n1 = (i != 0); break;
4642 case TYPE_GREATER: n1 = (i > 0); break;
4643 case TYPE_GEQUAL: n1 = (i >= 0); break;
4644 case TYPE_SMALLER: n1 = (i < 0); break;
4645 case TYPE_SEQUAL: n1 = (i <= 0); break;
4646
4647 case TYPE_MATCH:
4648 case TYPE_NOMATCH:
4649 /* avoid 'l' flag in 'cpoptions' */
4650 save_cpo = p_cpo;
4651 p_cpo = (char_u *)"";
4652 regmatch.regprog = vim_regcomp(s2,
4653 RE_MAGIC + RE_STRING);
4654 regmatch.rm_ic = ic;
4655 if (regmatch.regprog != NULL)
4656 {
4657 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004658 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (type == TYPE_NOMATCH)
4660 n1 = !n1;
4661 }
4662 p_cpo = save_cpo;
4663 break;
4664
4665 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4666 }
4667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
4669 clear_tv(&var2);
4670 rettv->v_type = VAR_NUMBER;
4671 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 }
4674
4675 return OK;
4676}
4677
4678/*
4679 * Handle fourth level expression:
4680 * + number addition
4681 * - number subtraction
4682 * . string concatenation
4683 *
4684 * "arg" must point to the first non-white of the expression.
4685 * "arg" is advanced to the next non-white after the recognized expression.
4686 *
4687 * Return OK or FAIL.
4688 */
4689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004690eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
Bram Moolenaar33570922005-01-25 22:26:29 +00004692 typval_T var2;
4693 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 int op;
4695 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696#ifdef FEAT_FLOAT
4697 float_T f1 = 0, f2 = 0;
4698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 char_u *s1, *s2;
4700 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4701 char_u *p;
4702
4703 /*
4704 * Get the first variable.
4705 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return FAIL;
4708
4709 /*
4710 * Repeat computing, until no '+', '-' or '.' is following.
4711 */
4712 for (;;)
4713 {
4714 op = **arg;
4715 if (op != '+' && op != '-' && op != '.')
4716 break;
4717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 if ((op != '+' || rettv->v_type != VAR_LIST)
4719#ifdef FEAT_FLOAT
4720 && (op == '.' || rettv->v_type != VAR_FLOAT)
4721#endif
4722 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 {
4724 /* For "list + ...", an illegal use of the first operand as
4725 * a number cannot be determined before evaluating the 2nd
4726 * operand: if this is also a list, all is ok.
4727 * For "something . ...", "something - ..." or "non-list + ...",
4728 * we know that the first operand needs to be a string or number
4729 * without evaluating the 2nd operand. So check before to avoid
4730 * side effects after an error. */
4731 if (evaluate && get_tv_string_chk(rettv) == NULL)
4732 {
4733 clear_tv(rettv);
4734 return FAIL;
4735 }
4736 }
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /*
4739 * Get the second variable.
4740 */
4741 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return FAIL;
4746 }
4747
4748 if (evaluate)
4749 {
4750 /*
4751 * Compute the result.
4752 */
4753 if (op == '.')
4754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4756 s2 = get_tv_string_buf_chk(&var2, buf2);
4757 if (s2 == NULL) /* type error ? */
4758 {
4759 clear_tv(rettv);
4760 clear_tv(&var2);
4761 return FAIL;
4762 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004763 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004768 else if (op == '+' && rettv->v_type == VAR_LIST
4769 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004770 {
4771 /* concatenate Lists */
4772 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4773 &var3) == FAIL)
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
4779 clear_tv(rettv);
4780 *rettv = var3;
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 else
4783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 int error = FALSE;
4785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786#ifdef FEAT_FLOAT
4787 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 f1 = rettv->vval.v_float;
4790 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 else
4793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 n1 = get_tv_number_chk(rettv, &error);
4796 if (error)
4797 {
4798 /* This can only happen for "list + non-list". For
4799 * "non-list + ..." or "something - ...", we returned
4800 * before evaluating the 2nd operand. */
4801 clear_tv(rettv);
4802 return FAIL;
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 f1 = n1;
4807#endif
4808 }
4809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 {
4812 f2 = var2.vval.v_float;
4813 n2 = 0;
4814 }
4815 else
4816#endif
4817 {
4818 n2 = get_tv_number_chk(&var2, &error);
4819 if (error)
4820 {
4821 clear_tv(rettv);
4822 clear_tv(&var2);
4823 return FAIL;
4824 }
4825#ifdef FEAT_FLOAT
4826 if (rettv->v_type == VAR_FLOAT)
4827 f2 = n2;
4828#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831
4832#ifdef FEAT_FLOAT
4833 /* If there is a float on either side the result is a float. */
4834 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4835 {
4836 if (op == '+')
4837 f1 = f1 + f2;
4838 else
4839 f1 = f1 - f2;
4840 rettv->v_type = VAR_FLOAT;
4841 rettv->vval.v_float = f1;
4842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#endif
4845 {
4846 if (op == '+')
4847 n1 = n1 + n2;
4848 else
4849 n1 = n1 - n2;
4850 rettv->v_type = VAR_NUMBER;
4851 rettv->vval.v_number = n1;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857 return OK;
4858}
4859
4860/*
4861 * Handle fifth level expression:
4862 * * number multiplication
4863 * / number division
4864 * % number modulo
4865 *
4866 * "arg" must point to the first non-white of the expression.
4867 * "arg" is advanced to the next non-white after the recognized expression.
4868 *
4869 * Return OK or FAIL.
4870 */
4871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872eval6(
4873 char_u **arg,
4874 typval_T *rettv,
4875 int evaluate,
4876 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 int op;
4880 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881#ifdef FEAT_FLOAT
4882 int use_float = FALSE;
4883 float_T f1 = 0, f2;
4884#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Get the first variable.
4889 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004890 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return FAIL;
4892
4893 /*
4894 * Repeat computing, until no '*', '/' or '%' is following.
4895 */
4896 for (;;)
4897 {
4898 op = **arg;
4899 if (op != '*' && op != '/' && op != '%')
4900 break;
4901
4902 if (evaluate)
4903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904#ifdef FEAT_FLOAT
4905 if (rettv->v_type == VAR_FLOAT)
4906 {
4907 f1 = rettv->vval.v_float;
4908 use_float = TRUE;
4909 n1 = 0;
4910 }
4911 else
4912#endif
4913 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 if (error)
4916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
4919 n1 = 0;
4920
4921 /*
4922 * Get the second variable.
4923 */
4924 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004925 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return FAIL;
4927
4928 if (evaluate)
4929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#ifdef FEAT_FLOAT
4931 if (var2.v_type == VAR_FLOAT)
4932 {
4933 if (!use_float)
4934 {
4935 f1 = n1;
4936 use_float = TRUE;
4937 }
4938 f2 = var2.vval.v_float;
4939 n2 = 0;
4940 }
4941 else
4942#endif
4943 {
4944 n2 = get_tv_number_chk(&var2, &error);
4945 clear_tv(&var2);
4946 if (error)
4947 return FAIL;
4948#ifdef FEAT_FLOAT
4949 if (use_float)
4950 f2 = n2;
4951#endif
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 /*
4955 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958#ifdef FEAT_FLOAT
4959 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 if (op == '*')
4962 f1 = f1 * f2;
4963 else if (op == '/')
4964 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004965# ifdef VMS
4966 /* VMS crashes on divide by zero, work around it */
4967 if (f2 == 0.0)
4968 {
4969 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004970 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004972 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004974 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975 }
4976 else
4977 f1 = f1 / f2;
4978# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 /* We rely on the floating point library to handle divide
4980 * by zero to result in "inf" and not a crash. */
4981 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004986 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 return FAIL;
4988 }
4989 rettv->v_type = VAR_FLOAT;
4990 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 if (op == '*')
4996 n1 = n1 * n2;
4997 else if (op == '/')
4998 {
4999 if (n2 == 0) /* give an error message? */
5000 {
5001 if (n1 == 0)
5002 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5003 else if (n1 < 0)
5004 n1 = -0x7fffffffL;
5005 else
5006 n1 = 0x7fffffffL;
5007 }
5008 else
5009 n1 = n1 / n2;
5010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013 if (n2 == 0) /* give an error message? */
5014 n1 = 0;
5015 else
5016 n1 = n1 % n2;
5017 }
5018 rettv->v_type = VAR_NUMBER;
5019 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 }
5023
5024 return OK;
5025}
5026
5027/*
5028 * Handle sixth level expression:
5029 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005030 * "string" string constant
5031 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * &option-name option value
5033 * @r register contents
5034 * identifier variable value
5035 * function() function call
5036 * $VAR environment variable
5037 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005038 * [expr, expr] List
5039 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * Also handle:
5042 * ! in front logical NOT
5043 * - in front unary minus
5044 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * trailing [] subscript in String or List
5046 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 *
5048 * "arg" must point to the first non-white of the expression.
5049 * "arg" is advanced to the next non-white after the recognized expression.
5050 *
5051 * Return OK or FAIL.
5052 */
5053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005054eval7(
5055 char_u **arg,
5056 typval_T *rettv,
5057 int evaluate,
5058 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 long n;
5061 int len;
5062 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *start_leader, *end_leader;
5064 int ret = OK;
5065 char_u *alias;
5066
5067 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005069 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 /*
5074 * Skip '!' and '-' characters. They are handled later.
5075 */
5076 start_leader = *arg;
5077 while (**arg == '!' || **arg == '-' || **arg == '+')
5078 *arg = skipwhite(*arg + 1);
5079 end_leader = *arg;
5080
5081 switch (**arg)
5082 {
5083 /*
5084 * Number constant.
5085 */
5086 case '0':
5087 case '1':
5088 case '2':
5089 case '3':
5090 case '4':
5091 case '5':
5092 case '6':
5093 case '7':
5094 case '8':
5095 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 {
5097#ifdef FEAT_FLOAT
5098 char_u *p = skipdigits(*arg + 1);
5099 int get_float = FALSE;
5100
5101 /* We accept a float when the format matches
5102 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005103 * strict to avoid backwards compatibility problems.
5104 * Don't look for a float after the "." operator, so that
5105 * ":let vers = 1.2.3" doesn't fail. */
5106 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 get_float = TRUE;
5109 p = skipdigits(p + 2);
5110 if (*p == 'e' || *p == 'E')
5111 {
5112 ++p;
5113 if (*p == '-' || *p == '+')
5114 ++p;
5115 if (!vim_isdigit(*p))
5116 get_float = FALSE;
5117 else
5118 p = skipdigits(p + 1);
5119 }
5120 if (ASCII_ISALPHA(*p) || *p == '.')
5121 get_float = FALSE;
5122 }
5123 if (get_float)
5124 {
5125 float_T f;
5126
5127 *arg += string2float(*arg, &f);
5128 if (evaluate)
5129 {
5130 rettv->v_type = VAR_FLOAT;
5131 rettv->vval.v_float = f;
5132 }
5133 }
5134 else
5135#endif
5136 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005137 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 *arg += len;
5139 if (evaluate)
5140 {
5141 rettv->v_type = VAR_NUMBER;
5142 rettv->vval.v_number = n;
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
5149 * String constant: "string".
5150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153
5154 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 break;
5159
5160 /*
5161 * List: [expr, expr]
5162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165
5166 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 * Dictionary: {key: val, key: val}
5168 */
5169 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5170 break;
5171
5172 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
5179 * Environment variable: $VAR.
5180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Register contents: @r.
5186 */
5187 case '@': ++*arg;
5188 if (evaluate)
5189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005191 rettv->vval.v_string = get_reg_contents(**arg,
5192 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 if (**arg != NUL)
5195 ++*arg;
5196 break;
5197
5198 /*
5199 * nested expression: (expression).
5200 */
5201 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (**arg == ')')
5204 ++*arg;
5205 else if (ret == OK)
5206 {
5207 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 ret = FAIL;
5210 }
5211 break;
5212
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 if (ret == NOTDONE)
5218 {
5219 /*
5220 * Must be a variable or function name.
5221 * Can also be a curly-braces kind of name: {expr}.
5222 */
5223 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (alias != NULL)
5226 s = alias;
5227
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 ret = FAIL;
5230 else
5231 {
5232 if (**arg == '(') /* recursive! */
5233 {
5234 /* If "s" is the name of a variable of type VAR_FUNC
5235 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005236 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237
5238 /* Invoke the function. */
5239 ret = get_func_tv(s, len, rettv, arg,
5240 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005241 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005242
5243 /* If evaluate is FALSE rettv->v_type was not set in
5244 * get_func_tv, but it's needed in handle_subscript() to parse
5245 * what follows. So set it here. */
5246 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5247 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005248 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005249 rettv->v_type = VAR_FUNC;
5250 }
5251
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /* Stop the expression evaluation when immediately
5253 * aborting on error, or when an interrupt occurred or
5254 * an exception was thrown but not caught. */
5255 if (aborting())
5256 {
5257 if (ret == OK)
5258 clear_tv(rettv);
5259 ret = FAIL;
5260 }
5261 }
5262 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005263 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005264 else
5265 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005267 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 }
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 *arg = skipwhite(*arg);
5271
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5273 * expr(expr). */
5274 if (ret == OK)
5275 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /*
5278 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5279 */
5280 if (ret == OK && evaluate && end_leader > start_leader)
5281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005283 int val = 0;
5284#ifdef FEAT_FLOAT
5285 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 if (rettv->v_type == VAR_FLOAT)
5288 f = rettv->vval.v_float;
5289 else
5290#endif
5291 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 clear_tv(rettv);
5295 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 else
5298 {
5299 while (end_leader > start_leader)
5300 {
5301 --end_leader;
5302 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 {
5304#ifdef FEAT_FLOAT
5305 if (rettv->v_type == VAR_FLOAT)
5306 f = !f;
5307 else
5308#endif
5309 val = !val;
5310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 {
5313#ifdef FEAT_FLOAT
5314 if (rettv->v_type == VAR_FLOAT)
5315 f = -f;
5316 else
5317#endif
5318 val = -val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321#ifdef FEAT_FLOAT
5322 if (rettv->v_type == VAR_FLOAT)
5323 {
5324 clear_tv(rettv);
5325 rettv->vval.v_float = f;
5326 }
5327 else
5328#endif
5329 {
5330 clear_tv(rettv);
5331 rettv->v_type = VAR_NUMBER;
5332 rettv->vval.v_number = val;
5333 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336
5337 return ret;
5338}
5339
5340/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005341 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5342 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5344 */
5345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005346eval_index(
5347 char_u **arg,
5348 typval_T *rettv,
5349 int evaluate,
5350 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351{
5352 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 long len = -1;
5356 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359
Bram Moolenaara03f2332016-02-06 18:09:59 +01005360 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 case VAR_FUNC:
5363 if (verbose)
5364 EMSG(_("E695: Cannot index a Funcref"));
5365 return FAIL;
5366 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005367#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 if (verbose)
5369 EMSG(_(e_float_as_string));
5370 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005371#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005372 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005373 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005374 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005375 if (verbose)
5376 EMSG(_("E909: Cannot index a special variable"));
5377 return FAIL;
5378 case VAR_UNKNOWN:
5379 if (evaluate)
5380 return FAIL;
5381 /* FALLTHROUGH */
5382
5383 case VAR_STRING:
5384 case VAR_NUMBER:
5385 case VAR_LIST:
5386 case VAR_DICT:
5387 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005388 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005390 init_tv(&var1);
5391 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 /*
5395 * dict.name
5396 */
5397 key = *arg + 1;
5398 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5399 ;
5400 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 /*
5407 * something[idx]
5408 *
5409 * Get the (first) variable from inside the [].
5410 */
5411 *arg = skipwhite(*arg + 1);
5412 if (**arg == ':')
5413 empty1 = TRUE;
5414 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5415 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005416 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5417 {
5418 /* not a number or string */
5419 clear_tv(&var1);
5420 return FAIL;
5421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422
5423 /*
5424 * Get the second variable from inside the [:].
5425 */
5426 if (**arg == ':')
5427 {
5428 range = TRUE;
5429 *arg = skipwhite(*arg + 1);
5430 if (**arg == ']')
5431 empty2 = TRUE;
5432 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005434 if (!empty1)
5435 clear_tv(&var1);
5436 return FAIL;
5437 }
5438 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5439 {
5440 /* not a number or string */
5441 if (!empty1)
5442 clear_tv(&var1);
5443 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 return FAIL;
5445 }
5446 }
5447
5448 /* Check for the ']'. */
5449 if (**arg != ']')
5450 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005451 if (verbose)
5452 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 clear_tv(&var1);
5454 if (range)
5455 clear_tv(&var2);
5456 return FAIL;
5457 }
5458 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460
5461 if (evaluate)
5462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 n1 = 0;
5464 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 n1 = get_tv_number(&var1);
5467 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 if (range)
5470 {
5471 if (empty2)
5472 n2 = -1;
5473 else
5474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 n2 = get_tv_number(&var2);
5476 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 }
5478 }
5479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005482 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005483 case VAR_FUNC:
5484 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005485 case VAR_SPECIAL:
5486 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005487 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005488 break; /* not evaluating, skipping over subscript */
5489
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 case VAR_NUMBER:
5491 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 len = (long)STRLEN(s);
5494 if (range)
5495 {
5496 /* The resulting variable is a substring. If the indexes
5497 * are out of range the result is empty. */
5498 if (n1 < 0)
5499 {
5500 n1 = len + n1;
5501 if (n1 < 0)
5502 n1 = 0;
5503 }
5504 if (n2 < 0)
5505 n2 = len + n2;
5506 else if (n2 >= len)
5507 n2 = len;
5508 if (n1 >= len || n2 < 0 || n1 > n2)
5509 s = NULL;
5510 else
5511 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5512 }
5513 else
5514 {
5515 /* The resulting variable is a string of a single
5516 * character. If the index is too big or negative the
5517 * result is empty. */
5518 if (n1 >= len || n1 < 0)
5519 s = NULL;
5520 else
5521 s = vim_strnsave(s + n1, 1);
5522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 clear_tv(rettv);
5524 rettv->v_type = VAR_STRING;
5525 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 break;
5527
5528 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 if (n1 < 0)
5531 n1 = len + n1;
5532 if (!empty1 && (n1 < 0 || n1 >= len))
5533 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005534 /* For a range we allow invalid values and return an empty
5535 * list. A list index out of range is an error. */
5536 if (!range)
5537 {
5538 if (verbose)
5539 EMSGN(_(e_listidx), n1);
5540 return FAIL;
5541 }
5542 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 }
5544 if (range)
5545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005546 list_T *l;
5547 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548
5549 if (n2 < 0)
5550 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005551 else if (n2 >= len)
5552 n2 = len - 1;
5553 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005554 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 l = list_alloc();
5556 if (l == NULL)
5557 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 n1 <= n2; ++n1)
5560 {
5561 if (list_append_tv(l, &item->li_tv) == FAIL)
5562 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005563 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 return FAIL;
5565 }
5566 item = item->li_next;
5567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 clear_tv(rettv);
5569 rettv->v_type = VAR_LIST;
5570 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005571 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 }
5573 else
5574 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005575 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 clear_tv(rettv);
5577 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 }
5579 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580
5581 case VAR_DICT:
5582 if (range)
5583 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 if (verbose)
5585 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 if (len == -1)
5587 clear_tv(&var1);
5588 return FAIL;
5589 }
5590 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592
5593 if (len == -1)
5594 {
5595 key = get_tv_string(&var1);
5596 if (*key == NUL)
5597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005598 if (verbose)
5599 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 clear_tv(&var1);
5601 return FAIL;
5602 }
5603 }
5604
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005605 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005607 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005608 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609 if (len == -1)
5610 clear_tv(&var1);
5611 if (item == NULL)
5612 return FAIL;
5613
5614 copy_tv(&item->di_tv, &var1);
5615 clear_tv(rettv);
5616 *rettv = var1;
5617 }
5618 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 }
5620 }
5621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 return OK;
5623}
5624
5625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 * Get an option value.
5627 * "arg" points to the '&' or '+' before the option name.
5628 * "arg" is advanced to character after the option name.
5629 * Return OK or FAIL.
5630 */
5631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005632get_option_tv(
5633 char_u **arg,
5634 typval_T *rettv, /* when NULL, only check if option exists */
5635 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 char_u *option_end;
5638 long numval;
5639 char_u *stringval;
5640 int opt_type;
5641 int c;
5642 int working = (**arg == '+'); /* has("+option") */
5643 int ret = OK;
5644 int opt_flags;
5645
5646 /*
5647 * Isolate the option name and find its value.
5648 */
5649 option_end = find_option_end(arg, &opt_flags);
5650 if (option_end == NULL)
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 EMSG2(_("E112: Option name missing: %s"), *arg);
5654 return FAIL;
5655 }
5656
5657 if (!evaluate)
5658 {
5659 *arg = option_end;
5660 return OK;
5661 }
5662
5663 c = *option_end;
5664 *option_end = NUL;
5665 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 if (opt_type == -3) /* invalid name */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 EMSG2(_("E113: Unknown option: %s"), *arg);
5672 ret = FAIL;
5673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
5676 if (opt_type == -2) /* hidden string option */
5677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 rettv->v_type = VAR_STRING;
5679 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 else if (opt_type == -1) /* hidden number option */
5682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 rettv->v_type = VAR_NUMBER;
5684 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 else if (opt_type == 1) /* number option */
5687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 rettv->v_type = VAR_NUMBER;
5689 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 else /* string option */
5692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 rettv->v_type = VAR_STRING;
5694 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696 }
5697 else if (working && (opt_type == -2 || opt_type == -1))
5698 ret = FAIL;
5699
5700 *option_end = c; /* put back for error messages */
5701 *arg = option_end;
5702
5703 return ret;
5704}
5705
5706/*
5707 * Allocate a variable for a string constant.
5708 * Return OK or FAIL.
5709 */
5710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005711get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 char_u *p;
5714 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 int extra = 0;
5716
5717 /*
5718 * Find the end of the string, skipping backslashed characters.
5719 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005720 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 if (*p == '\\' && p[1] != NUL)
5723 {
5724 ++p;
5725 /* A "\<x>" form occupies at least 4 characters, and produces up
5726 * to 6 characters: reserve space for 2 extra */
5727 if (*p == '<')
5728 extra += 2;
5729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731
5732 if (*p != '"')
5733 {
5734 EMSG2(_("E114: Missing quote: %s"), *arg);
5735 return FAIL;
5736 }
5737
5738 /* If only parsing, set *arg and return here */
5739 if (!evaluate)
5740 {
5741 *arg = p + 1;
5742 return OK;
5743 }
5744
5745 /*
5746 * Copy the string into allocated memory, handling backslashed
5747 * characters.
5748 */
5749 name = alloc((unsigned)(p - *arg + extra));
5750 if (name == NULL)
5751 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 rettv->v_type = VAR_STRING;
5753 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
5757 if (*p == '\\')
5758 {
5759 switch (*++p)
5760 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 case 'b': *name++ = BS; ++p; break;
5762 case 'e': *name++ = ESC; ++p; break;
5763 case 'f': *name++ = FF; ++p; break;
5764 case 'n': *name++ = NL; ++p; break;
5765 case 'r': *name++ = CAR; ++p; break;
5766 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 case 'X': /* hex: "\x1", "\x12" */
5769 case 'x':
5770 case 'u': /* Unicode: "\u0023" */
5771 case 'U':
5772 if (vim_isxdigit(p[1]))
5773 {
5774 int n, nr;
5775 int c = toupper(*p);
5776
5777 if (c == 'X')
5778 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005779 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005781 else
5782 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 nr = 0;
5784 while (--n >= 0 && vim_isxdigit(p[1]))
5785 {
5786 ++p;
5787 nr = (nr << 4) + hex2nr(*p);
5788 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790#ifdef FEAT_MBYTE
5791 /* For "\u" store the number according to
5792 * 'encoding'. */
5793 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 else
5796#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 break;
5800
5801 /* octal: "\1", "\12", "\123" */
5802 case '0':
5803 case '1':
5804 case '2':
5805 case '3':
5806 case '4':
5807 case '5':
5808 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 case '7': *name = *p++ - '0';
5810 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 *name = (*name << 3) + *p++ - '0';
5813 if (*p >= '0' && *p <= '7')
5814 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 break;
5818
5819 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 if (extra != 0)
5822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 break;
5825 }
5826 /* FALLTHROUGH */
5827
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 break;
5830 }
5831 }
5832 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 *arg = p + 1;
5838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 return OK;
5840}
5841
5842/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 * Return OK or FAIL.
5845 */
5846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005847get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
5849 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 int reduce = 0;
5852
5853 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 */
5856 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 break;
5862 ++reduce;
5863 ++p;
5864 }
5865 }
5866
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 return FAIL;
5871 }
5872
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 if (!evaluate)
5875 {
5876 *arg = p + 1;
5877 return OK;
5878 }
5879
5880 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 */
5883 str = alloc((unsigned)((p - *arg) - reduce));
5884 if (str == NULL)
5885 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 rettv->v_type = VAR_STRING;
5887 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 break;
5895 ++p;
5896 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 *arg = p + 1;
5901
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 return OK;
5903}
5904
5905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 * Allocate a variable for a List and fill it from "*arg".
5907 * Return OK or FAIL.
5908 */
5909 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005910get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911{
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 list_T *l = NULL;
5913 typval_T tv;
5914 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915
5916 if (evaluate)
5917 {
5918 l = list_alloc();
5919 if (l == NULL)
5920 return FAIL;
5921 }
5922
5923 *arg = skipwhite(*arg + 1);
5924 while (**arg != ']' && **arg != NUL)
5925 {
5926 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5927 goto failret;
5928 if (evaluate)
5929 {
5930 item = listitem_alloc();
5931 if (item != NULL)
5932 {
5933 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005934 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 list_append(l, item);
5936 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005937 else
5938 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 }
5940
5941 if (**arg == ']')
5942 break;
5943 if (**arg != ',')
5944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 goto failret;
5947 }
5948 *arg = skipwhite(*arg + 1);
5949 }
5950
5951 if (**arg != ']')
5952 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954failret:
5955 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005956 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 return FAIL;
5958 }
5959
5960 *arg = skipwhite(*arg + 1);
5961 if (evaluate)
5962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005963 rettv->v_type = VAR_LIST;
5964 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 ++l->lv_refcount;
5966 }
5967
5968 return OK;
5969}
5970
5971/*
5972 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005973 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005975 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005976list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005978 list_T *l;
5979
5980 l = (list_T *)alloc_clear(sizeof(list_T));
5981 if (l != NULL)
5982 {
5983 /* Prepend the list to the list of lists for garbage collection. */
5984 if (first_list != NULL)
5985 first_list->lv_used_prev = l;
5986 l->lv_used_prev = NULL;
5987 l->lv_used_next = first_list;
5988 first_list = l;
5989 }
5990 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991}
5992
5993/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005994 * Allocate an empty list for a return value.
5995 * Returns OK or FAIL.
5996 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005998rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005999{
6000 list_T *l = list_alloc();
6001
6002 if (l == NULL)
6003 return FAIL;
6004
6005 rettv->vval.v_list = l;
6006 rettv->v_type = VAR_LIST;
6007 ++l->lv_refcount;
6008 return OK;
6009}
6010
6011/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 * Unreference a list: decrement the reference count and free it when it
6013 * becomes zero.
6014 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006015 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006016list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006018 if (l != NULL && --l->lv_refcount <= 0)
6019 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020}
6021
6022/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006023 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 * Ignores the reference count.
6025 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006027list_free(
6028 list_T *l,
6029 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030{
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006033 /* Remove the list from the list of lists for garbage collection. */
6034 if (l->lv_used_prev == NULL)
6035 first_list = l->lv_used_next;
6036 else
6037 l->lv_used_prev->lv_used_next = l->lv_used_next;
6038 if (l->lv_used_next != NULL)
6039 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6040
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006043 /* Remove the item before deleting it. */
6044 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006045 if (recurse || (item->li_tv.v_type != VAR_LIST
6046 && item->li_tv.v_type != VAR_DICT))
6047 clear_tv(&item->li_tv);
6048 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 }
6050 vim_free(l);
6051}
6052
6053/*
6054 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006055 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006057 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006058listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059{
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061}
6062
6063/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006064 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006067listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006069 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 vim_free(item);
6071}
6072
6073/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006074 * Remove a list item from a List and free it. Also clears the value.
6075 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006077listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006079 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006080 listitem_free(item);
6081}
6082
6083/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 * Get the number of items in a list.
6085 */
6086 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006087list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089 if (l == NULL)
6090 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006091 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092}
6093
6094/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095 * Return TRUE when two lists have exactly the same values.
6096 */
6097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006098list_equal(
6099 list_T *l1,
6100 list_T *l2,
6101 int ic, /* ignore case for strings */
6102 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103{
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006106 if (l1 == NULL || l2 == NULL)
6107 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006108 if (l1 == l2)
6109 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006110 if (list_len(l1) != list_len(l2))
6111 return FALSE;
6112
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113 for (item1 = l1->lv_first, item2 = l2->lv_first;
6114 item1 != NULL && item2 != NULL;
6115 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006116 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 return FALSE;
6118 return item1 == NULL && item2 == NULL;
6119}
6120
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006121/*
6122 * Return the dictitem that an entry in a hashtable points to.
6123 */
6124 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006126{
6127 return HI2DI(hi);
6128}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006129
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131 * Return TRUE when two dictionaries have exactly the same key/values.
6132 */
6133 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006134dict_equal(
6135 dict_T *d1,
6136 dict_T *d2,
6137 int ic, /* ignore case for strings */
6138 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139{
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 hashitem_T *hi;
6141 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006142 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006144 if (d1 == NULL || d2 == NULL)
6145 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006146 if (d1 == d2)
6147 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148 if (dict_len(d1) != dict_len(d2))
6149 return FALSE;
6150
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006151 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006153 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 if (!HASHITEM_EMPTY(hi))
6155 {
6156 item2 = dict_find(d2, hi->hi_key, -1);
6157 if (item2 == NULL)
6158 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006159 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 return FALSE;
6161 --todo;
6162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163 }
6164 return TRUE;
6165}
6166
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006167static int tv_equal_recurse_limit;
6168
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006171 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006172 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173 */
6174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006175tv_equal(
6176 typval_T *tv1,
6177 typval_T *tv2,
6178 int ic, /* ignore case */
6179 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180{
6181 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006182 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006184 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006186 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006187 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006189 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 * recursiveness to a limit. We guess they are equal then.
6191 * A fixed limit has the problem of still taking an awful long time.
6192 * Reduce the limit every time running into it. That should work fine for
6193 * deeply linked structures that are not recursively linked and catch
6194 * recursiveness quickly. */
6195 if (!recursive)
6196 tv_equal_recurse_limit = 1000;
6197 if (recursive_cnt >= tv_equal_recurse_limit)
6198 {
6199 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006200 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202
6203 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006204 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006205 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 ++recursive_cnt;
6207 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6208 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006209 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210
6211 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006212 ++recursive_cnt;
6213 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6214 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006215 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216
6217 case VAR_FUNC:
6218 return (tv1->vval.v_string != NULL
6219 && tv2->vval.v_string != NULL
6220 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6221
6222 case VAR_NUMBER:
6223 return tv1->vval.v_number == tv2->vval.v_number;
6224
6225 case VAR_STRING:
6226 s1 = get_tv_string_buf(tv1, buf1);
6227 s2 = get_tv_string_buf(tv2, buf2);
6228 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006229
6230 case VAR_SPECIAL:
6231 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006232
6233 case VAR_FLOAT:
6234#ifdef FEAT_FLOAT
6235 return tv1->vval.v_float == tv2->vval.v_float;
6236#endif
6237 case VAR_JOB:
6238#ifdef FEAT_JOB
6239 return tv1->vval.v_job == tv2->vval.v_job;
6240#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006241 case VAR_CHANNEL:
6242#ifdef FEAT_CHANNEL
6243 return tv1->vval.v_channel == tv2->vval.v_channel;
6244#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006245 case VAR_UNKNOWN:
6246 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006247 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006248
Bram Moolenaara03f2332016-02-06 18:09:59 +01006249 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6250 * does not equal anything, not even itself. */
6251 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006252}
6253
6254/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006255 * Locate item with index "n" in list "l" and return it.
6256 * A negative index is counted from the end; -1 is the last item.
6257 * Returns NULL when "n" is out of range.
6258 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006260list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261{
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 long idx;
6264
6265 if (l == NULL)
6266 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267
6268 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 n = l->lv_len + n;
6271
6272 /* Check for index out of range. */
6273 if (n < 0 || n >= l->lv_len)
6274 return NULL;
6275
6276 /* When there is a cached index may start search from there. */
6277 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_idx / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else if (n > (l->lv_idx + l->lv_len) / 2)
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
6291 else
6292 {
6293 /* closest to the cached index */
6294 item = l->lv_idx_item;
6295 idx = l->lv_idx;
6296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 }
6298 else
6299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 if (n < l->lv_len / 2)
6301 {
6302 /* closest to the start of the list */
6303 item = l->lv_first;
6304 idx = 0;
6305 }
6306 else
6307 {
6308 /* closest to the end of the list */
6309 item = l->lv_last;
6310 idx = l->lv_len - 1;
6311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313
6314 while (n > idx)
6315 {
6316 /* search forward */
6317 item = item->li_next;
6318 ++idx;
6319 }
6320 while (n < idx)
6321 {
6322 /* search backward */
6323 item = item->li_prev;
6324 --idx;
6325 }
6326
6327 /* cache the used index */
6328 l->lv_idx = idx;
6329 l->lv_idx_item = item;
6330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 return item;
6332}
6333
6334/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006335 * Get list item "l[idx]" as a number.
6336 */
6337 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006338list_find_nr(
6339 list_T *l,
6340 long idx,
6341 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006342{
6343 listitem_T *li;
6344
6345 li = list_find(l, idx);
6346 if (li == NULL)
6347 {
6348 if (errorp != NULL)
6349 *errorp = TRUE;
6350 return -1L;
6351 }
6352 return get_tv_number_chk(&li->li_tv, errorp);
6353}
6354
6355/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006356 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6357 */
6358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006359list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006360{
6361 listitem_T *li;
6362
6363 li = list_find(l, idx - 1);
6364 if (li == NULL)
6365 {
6366 EMSGN(_(e_listidx), idx);
6367 return NULL;
6368 }
6369 return get_tv_string(&li->li_tv);
6370}
6371
6372/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006373 * Locate "item" list "l" and return its index.
6374 * Returns -1 when "item" is not in the list.
6375 */
6376 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006377list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378{
6379 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006381
6382 if (l == NULL)
6383 return -1;
6384 idx = 0;
6385 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6386 ++idx;
6387 if (li == NULL)
6388 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006389 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006390}
6391
6392/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 * Append item "item" to the end of list "l".
6394 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006396list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397{
6398 if (l->lv_last == NULL)
6399 {
6400 /* empty list */
6401 l->lv_first = item;
6402 l->lv_last = item;
6403 item->li_prev = NULL;
6404 }
6405 else
6406 {
6407 l->lv_last->li_next = item;
6408 item->li_prev = l->lv_last;
6409 l->lv_last = item;
6410 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006411 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 item->li_next = NULL;
6413}
6414
6415/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006419 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006420list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423
Bram Moolenaar05159a02005-02-26 23:04:13 +00006424 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006426 copy_tv(tv, &li->li_tv);
6427 list_append(l, li);
6428 return OK;
6429}
6430
6431/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006432 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006433 * Return FAIL when out of memory.
6434 */
6435 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006436list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006437{
6438 listitem_T *li = listitem_alloc();
6439
6440 if (li == NULL)
6441 return FAIL;
6442 li->li_tv.v_type = VAR_DICT;
6443 li->li_tv.v_lock = 0;
6444 li->li_tv.vval.v_dict = dict;
6445 list_append(list, li);
6446 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 return OK;
6448}
6449
6450/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006452 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006453 * Returns FAIL when out of memory.
6454 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006457{
6458 listitem_T *li = listitem_alloc();
6459
6460 if (li == NULL)
6461 return FAIL;
6462 list_append(l, li);
6463 li->li_tv.v_type = VAR_STRING;
6464 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006465 if (str == NULL)
6466 li->li_tv.vval.v_string = NULL;
6467 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006468 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006469 return FAIL;
6470 return OK;
6471}
6472
6473/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006474 * Append "n" to list "l".
6475 * Returns FAIL when out of memory.
6476 */
6477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006479{
6480 listitem_T *li;
6481
6482 li = listitem_alloc();
6483 if (li == NULL)
6484 return FAIL;
6485 li->li_tv.v_type = VAR_NUMBER;
6486 li->li_tv.v_lock = 0;
6487 li->li_tv.vval.v_number = n;
6488 list_append(l, li);
6489 return OK;
6490}
6491
6492/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494 * If "item" is NULL append at the end.
6495 * Return FAIL when out of memory.
6496 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006498list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499{
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501
6502 if (ni == NULL)
6503 return FAIL;
6504 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006505 list_insert(l, ni, item);
6506 return OK;
6507}
6508
6509 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006511{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 if (item == NULL)
6513 /* Append new item at end of list. */
6514 list_append(l, ni);
6515 else
6516 {
6517 /* Insert new item before existing item. */
6518 ni->li_prev = item->li_prev;
6519 ni->li_next = item;
6520 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 ++l->lv_idx;
6524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 l->lv_idx_item = NULL;
6529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533}
6534
6535/*
6536 * Extend "l1" with "l2".
6537 * If "bef" is NULL append at the end, otherwise insert before this item.
6538 * Returns FAIL when out of memory.
6539 */
6540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006541list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542{
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006544 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006546 /* We also quit the loop when we have inserted the original item count of
6547 * the list, avoid a hang when we extend a list with itself. */
6548 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6550 return FAIL;
6551 return OK;
6552}
6553
6554/*
6555 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6556 * Return FAIL when out of memory.
6557 */
6558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560{
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006563 if (l1 == NULL || l2 == NULL)
6564 return FAIL;
6565
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 if (l == NULL)
6569 return FAIL;
6570 tv->v_type = VAR_LIST;
6571 tv->vval.v_list = l;
6572
6573 /* append all items from the second list */
6574 return list_extend(l, l2, NULL);
6575}
6576
6577/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 * Returns NULL when out of memory.
6582 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006584list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585{
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 list_T *copy;
6587 listitem_T *item;
6588 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589
6590 if (orig == NULL)
6591 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592
6593 copy = list_alloc();
6594 if (copy != NULL)
6595 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 if (copyID != 0)
6597 {
6598 /* Do this before adding the items, because one of the items may
6599 * refer back to this list. */
6600 orig->lv_copyID = copyID;
6601 orig->lv_copylist = copy;
6602 }
6603 for (item = orig->lv_first; item != NULL && !got_int;
6604 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 {
6606 ni = listitem_alloc();
6607 if (ni == NULL)
6608 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 {
6611 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6612 {
6613 vim_free(ni);
6614 break;
6615 }
6616 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006618 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 list_append(copy, ni);
6620 }
6621 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006622 if (item != NULL)
6623 {
6624 list_unref(copy);
6625 copy = NULL;
6626 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 }
6628
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 return copy;
6630}
6631
6632/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006634 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006635 * This used to be called list_remove, but that conflicts with a Sun header
6636 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006639vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006640{
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006643 /* notify watchers */
6644 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006646 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 list_fix_watch(l, ip);
6648 if (ip == item2)
6649 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651
6652 if (item2->li_next == NULL)
6653 l->lv_last = item->li_prev;
6654 else
6655 item2->li_next->li_prev = item->li_prev;
6656 if (item->li_prev == NULL)
6657 l->lv_first = item2->li_next;
6658 else
6659 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006660 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661}
6662
6663/*
6664 * Return an allocated string with the string representation of a list.
6665 * May return NULL.
6666 */
6667 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006668list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669{
6670 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671
6672 if (tv->vval.v_list == NULL)
6673 return NULL;
6674 ga_init2(&ga, (int)sizeof(char), 80);
6675 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006676 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006677 {
6678 vim_free(ga.ga_data);
6679 return NULL;
6680 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681 ga_append(&ga, ']');
6682 ga_append(&ga, NUL);
6683 return (char_u *)ga.ga_data;
6684}
6685
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006686typedef struct join_S {
6687 char_u *s;
6688 char_u *tofree;
6689} join_T;
6690
6691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006692list_join_inner(
6693 garray_T *gap, /* to store the result in */
6694 list_T *l,
6695 char_u *sep,
6696 int echo_style,
6697 int copyID,
6698 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006699{
6700 int i;
6701 join_T *p;
6702 int len;
6703 int sumlen = 0;
6704 int first = TRUE;
6705 char_u *tofree;
6706 char_u numbuf[NUMBUFLEN];
6707 listitem_T *item;
6708 char_u *s;
6709
6710 /* Stringify each item in the list. */
6711 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6712 {
6713 if (echo_style)
6714 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6715 else
6716 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6717 if (s == NULL)
6718 return FAIL;
6719
6720 len = (int)STRLEN(s);
6721 sumlen += len;
6722
Bram Moolenaarcde88542015-08-11 19:14:00 +02006723 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6725 if (tofree != NULL || s != numbuf)
6726 {
6727 p->s = s;
6728 p->tofree = tofree;
6729 }
6730 else
6731 {
6732 p->s = vim_strnsave(s, len);
6733 p->tofree = p->s;
6734 }
6735
6736 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006737 if (did_echo_string_emsg) /* recursion error, bail out */
6738 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006739 }
6740
6741 /* Allocate result buffer with its total size, avoid re-allocation and
6742 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6743 if (join_gap->ga_len >= 2)
6744 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6745 if (ga_grow(gap, sumlen + 2) == FAIL)
6746 return FAIL;
6747
6748 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6749 {
6750 if (first)
6751 first = FALSE;
6752 else
6753 ga_concat(gap, sep);
6754 p = ((join_T *)join_gap->ga_data) + i;
6755
6756 if (p->s != NULL)
6757 ga_concat(gap, p->s);
6758 line_breakcheck();
6759 }
6760
6761 return OK;
6762}
6763
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006764/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006765 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006766 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006768 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006770list_join(
6771 garray_T *gap,
6772 list_T *l,
6773 char_u *sep,
6774 int echo_style,
6775 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006777 garray_T join_ga;
6778 int retval;
6779 join_T *p;
6780 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781
Bram Moolenaard39a7512015-04-16 22:51:22 +02006782 if (l->lv_len < 1)
6783 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006784 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6785 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6786
6787 /* Dispose each item in join_ga. */
6788 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006790 p = (join_T *)join_ga.ga_data;
6791 for (i = 0; i < join_ga.ga_len; ++i)
6792 {
6793 vim_free(p->tofree);
6794 ++p;
6795 }
6796 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798
6799 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800}
6801
6802/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006803 * Return the next (unique) copy ID.
6804 * Used for serializing nested structures.
6805 */
6806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006807get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006808{
6809 current_copyID += COPYID_INC;
6810 return current_copyID;
6811}
6812
6813/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006814 * Garbage collection for lists and dictionaries.
6815 *
6816 * We use reference counts to be able to free most items right away when they
6817 * are no longer used. But for composite items it's possible that it becomes
6818 * unused while the reference count is > 0: When there is a recursive
6819 * reference. Example:
6820 * :let l = [1, 2, 3]
6821 * :let d = {9: l}
6822 * :let l[1] = d
6823 *
6824 * Since this is quite unusual we handle this with garbage collection: every
6825 * once in a while find out which lists and dicts are not referenced from any
6826 * variable.
6827 *
6828 * Here is a good reference text about garbage collection (refers to Python
6829 * but it applies to all reference-counting mechanisms):
6830 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832
6833/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 * Do garbage collection for lists and dicts.
6835 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006838garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006840 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006841 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 buf_T *buf;
6843 win_T *wp;
6844 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006845 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006846 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006848#ifdef FEAT_WINDOWS
6849 tabpage_T *tp;
6850#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006852 /* Only do this once. */
6853 want_garbage_collect = FALSE;
6854 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006855 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006856
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 /* We advance by two because we add one for items referenced through
6858 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006859 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 /*
6862 * 1. Go through all accessible variables and mark all lists and dicts
6863 * with copyID.
6864 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865
6866 /* Don't free variables in the previous_funccal list unless they are only
6867 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006868 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6870 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006871 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6872 NULL);
6873 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6874 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 }
6876
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 /* script-local variables */
6878 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006880
6881 /* buffer-local variables */
6882 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6884 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885
6886 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006887 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6889 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006890#ifdef FEAT_AUTOCMD
6891 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006894#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006896#ifdef FEAT_WINDOWS
6897 /* tabpage-local variables */
6898 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6900 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006901#endif
6902
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905
6906 /* function-local variables */
6907 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6908 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6910 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 }
6912
Bram Moolenaard812df62008-11-09 12:46:09 +00006913 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006915
Bram Moolenaar1dced572012-04-05 16:54:08 +02006916#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006918#endif
6919
Bram Moolenaardb913952012-06-29 12:54:53 +02006920#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006922#endif
6923
6924#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006926#endif
6927
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006928#ifdef FEAT_CHANNEL
6929 abort = abort || set_ref_in_channel(copyID);
6930#endif
6931
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 /*
6935 * 2. Free lists and dictionaries that are not referenced.
6936 */
6937 did_free = free_unref_items(copyID);
6938
6939 /*
6940 * 3. Check if any funccal can be freed now.
6941 */
6942 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 if (can_free_funccal(*pfc, copyID))
6945 {
6946 fc = *pfc;
6947 *pfc = fc->caller;
6948 free_funccal(fc, TRUE);
6949 did_free = TRUE;
6950 did_free_funccal = TRUE;
6951 }
6952 else
6953 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 if (did_free_funccal)
6956 /* When a funccal was freed some more items might be garbage
6957 * collected, so run again. */
6958 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006960 else if (p_verbose > 0)
6961 {
6962 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6963 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964
6965 return did_free;
6966}
6967
6968/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006969 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970 */
6971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006974 dict_T *dd, *dd_next;
6975 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 int did_free = FALSE;
6977
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 */
6981 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006982 {
6983 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006986 /* Free the Dictionary and ordinary items it contains, but don't
6987 * recurse into Lists and Dictionaries, they will be in the list
6988 * of dicts or list of lists. */
6989 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006992 dd = dd_next;
6993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994
6995 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 * Go through the list of lists and free items without the copyID.
6997 * But don't free a list that has a watcher (used in a for loop), these
6998 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 */
7000 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007001 {
7002 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007003 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7004 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007006 /* Free the List and ordinary items it contains, but don't recurse
7007 * into Lists and Dictionaries, they will be in the list of dicts
7008 * or list of lists. */
7009 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007012 ll = ll_next;
7013 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007014
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 return did_free;
7016}
7017
7018/*
7019 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 * "list_stack" is used to add lists to be marked. Can be NULL.
7021 *
7022 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007025set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026{
7027 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 hashtab_T *cur_ht;
7031 ht_stack_T *ht_stack = NULL;
7032 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 cur_ht = ht;
7035 for (;;)
7036 {
7037 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 /* Mark each item in the hashtab. If the item contains a hashtab
7040 * it is added to ht_stack, if it contains a list it is added to
7041 * list_stack. */
7042 todo = (int)cur_ht->ht_used;
7043 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7044 if (!HASHITEM_EMPTY(hi))
7045 {
7046 --todo;
7047 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7048 &ht_stack, list_stack);
7049 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051
7052 if (ht_stack == NULL)
7053 break;
7054
7055 /* take an item from the stack */
7056 cur_ht = ht_stack->ht;
7057 tempitem = ht_stack;
7058 ht_stack = ht_stack->prev;
7059 free(tempitem);
7060 }
7061
7062 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063}
7064
7065/*
7066 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7068 *
7069 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007072set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 listitem_T *li;
7075 int abort = FALSE;
7076 list_T *cur_l;
7077 list_stack_T *list_stack = NULL;
7078 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 cur_l = l;
7081 for (;;)
7082 {
7083 if (!abort)
7084 /* Mark each item in the list. If the item contains a hashtab
7085 * it is added to ht_stack, if it contains a list it is added to
7086 * list_stack. */
7087 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7088 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7089 ht_stack, &list_stack);
7090 if (list_stack == NULL)
7091 break;
7092
7093 /* take an item from the stack */
7094 cur_l = list_stack->list;
7095 tempitem = list_stack;
7096 list_stack = list_stack->prev;
7097 free(tempitem);
7098 }
7099
7100 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101}
7102
7103/*
7104 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 * "list_stack" is used to add lists to be marked. Can be NULL.
7106 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7107 *
7108 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111set_ref_in_item(
7112 typval_T *tv,
7113 int copyID,
7114 ht_stack_T **ht_stack,
7115 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116{
7117 dict_T *dd;
7118 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007119 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007120
Bram Moolenaara03f2332016-02-06 18:09:59 +01007121 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007122 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007123 dd = tv->vval.v_dict;
7124 if (dd != NULL && dd->dv_copyID != copyID)
7125 {
7126 /* Didn't see this dict yet. */
7127 dd->dv_copyID = copyID;
7128 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007129 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007130 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7131 }
7132 else
7133 {
7134 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7135 if (newitem == NULL)
7136 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 else
7138 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007139 newitem->ht = &dd->dv_hashtab;
7140 newitem->prev = *ht_stack;
7141 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007142 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007143 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007144 }
7145 }
7146 else if (tv->v_type == VAR_LIST)
7147 {
7148 ll = tv->vval.v_list;
7149 if (ll != NULL && ll->lv_copyID != copyID)
7150 {
7151 /* Didn't see this list yet. */
7152 ll->lv_copyID = copyID;
7153 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007155 abort = set_ref_in_list(ll, copyID, ht_stack);
7156 }
7157 else
7158 {
7159 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007160 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007161 if (newitem == NULL)
7162 abort = TRUE;
7163 else
7164 {
7165 newitem->list = ll;
7166 newitem->prev = *list_stack;
7167 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007170 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007171 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173}
7174
7175/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176 * Allocate an empty header for a dictionary.
7177 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007178 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007179dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180{
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007185 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007186 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187 if (first_dict != NULL)
7188 first_dict->dv_used_prev = d;
7189 d->dv_used_next = first_dict;
7190 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007191 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007192
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007194 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007195 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007196 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007198 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200}
7201
7202/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007203 * Allocate an empty dict for a return value.
7204 * Returns OK or FAIL.
7205 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007206 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007207rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007208{
7209 dict_T *d = dict_alloc();
7210
7211 if (d == NULL)
7212 return FAIL;
7213
7214 rettv->vval.v_dict = d;
7215 rettv->v_type = VAR_DICT;
7216 ++d->dv_refcount;
7217 return OK;
7218}
7219
7220
7221/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 * Unreference a Dictionary: decrement the reference count and free it when it
7223 * becomes zero.
7224 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007226dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007228 if (d != NULL && --d->dv_refcount <= 0)
7229 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230}
7231
7232/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007233 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234 * Ignores the reference count.
7235 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007237dict_free(
7238 dict_T *d,
7239 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007241 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007243 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007245 /* Remove the dict from the list of dicts for garbage collection. */
7246 if (d->dv_used_prev == NULL)
7247 first_dict = d->dv_used_next;
7248 else
7249 d->dv_used_prev->dv_used_next = d->dv_used_next;
7250 if (d->dv_used_next != NULL)
7251 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7252
7253 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007254 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007255 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007258 if (!HASHITEM_EMPTY(hi))
7259 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007260 /* Remove the item before deleting it, just in case there is
7261 * something recursive causing trouble. */
7262 di = HI2DI(hi);
7263 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007264 if (recurse || (di->di_tv.v_type != VAR_LIST
7265 && di->di_tv.v_type != VAR_DICT))
7266 clear_tv(&di->di_tv);
7267 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007268 --todo;
7269 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007271 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272 vim_free(d);
7273}
7274
7275/*
7276 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007277 * The "key" is copied to the new item.
7278 * Note that the value of the item "di_tv" still needs to be initialized!
7279 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007281 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007282dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283{
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007286 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007290 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007291 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007292 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293}
7294
7295/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296 * Make a copy of a Dictionary item.
7297 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007299dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300{
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7304 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 if (di != NULL)
7306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007308 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309 copy_tv(&org->di_tv, &di->di_tv);
7310 }
7311 return di;
7312}
7313
7314/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 * Remove item "item" from Dictionary "dict" and free it.
7316 */
7317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007318dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319{
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (HASHITEM_EMPTY(hi))
7324 EMSG2(_(e_intern2), "dictitem_remove()");
7325 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 dictitem_free(item);
7328}
7329
7330/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 * Free a dict item. Also clears the value.
7332 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007334dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007337 if (item->di_flags & DI_FLAGS_ALLOC)
7338 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339}
7340
7341/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7343 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007344 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 * Returns NULL when out of memory.
7346 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007348dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349{
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 dict_T *copy;
7351 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007354
7355 if (orig == NULL)
7356 return NULL;
7357
7358 copy = dict_alloc();
7359 if (copy != NULL)
7360 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 if (copyID != 0)
7362 {
7363 orig->dv_copyID = copyID;
7364 orig->dv_copydict = copy;
7365 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007366 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 --todo;
7372
7373 di = dictitem_alloc(hi->hi_key);
7374 if (di == NULL)
7375 break;
7376 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007377 {
7378 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7379 copyID) == FAIL)
7380 {
7381 vim_free(di);
7382 break;
7383 }
7384 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007385 else
7386 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7387 if (dict_add(copy, di) == FAIL)
7388 {
7389 dictitem_free(di);
7390 break;
7391 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007394
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396 if (todo > 0)
7397 {
7398 dict_unref(copy);
7399 copy = NULL;
7400 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 }
7402
7403 return copy;
7404}
7405
7406/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007408 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412{
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414}
7415
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007417 * Add a number or string entry to dictionary "d".
7418 * When "str" is NULL use number "nr", otherwise use "str".
7419 * Returns FAIL when out of memory and when key already exists.
7420 */
7421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007422dict_add_nr_str(
7423 dict_T *d,
7424 char *key,
7425 long nr,
7426 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007427{
7428 dictitem_T *item;
7429
7430 item = dictitem_alloc((char_u *)key);
7431 if (item == NULL)
7432 return FAIL;
7433 item->di_tv.v_lock = 0;
7434 if (str == NULL)
7435 {
7436 item->di_tv.v_type = VAR_NUMBER;
7437 item->di_tv.vval.v_number = nr;
7438 }
7439 else
7440 {
7441 item->di_tv.v_type = VAR_STRING;
7442 item->di_tv.vval.v_string = vim_strsave(str);
7443 }
7444 if (dict_add(d, item) == FAIL)
7445 {
7446 dictitem_free(item);
7447 return FAIL;
7448 }
7449 return OK;
7450}
7451
7452/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007453 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007454 * Returns FAIL when out of memory and when key already exists.
7455 */
7456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007457dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007458{
7459 dictitem_T *item;
7460
7461 item = dictitem_alloc((char_u *)key);
7462 if (item == NULL)
7463 return FAIL;
7464 item->di_tv.v_lock = 0;
7465 item->di_tv.v_type = VAR_LIST;
7466 item->di_tv.vval.v_list = list;
7467 if (dict_add(d, item) == FAIL)
7468 {
7469 dictitem_free(item);
7470 return FAIL;
7471 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007472 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007473 return OK;
7474}
7475
7476/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 * Get the number of items in a Dictionary.
7478 */
7479 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007480dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007481{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482 if (d == NULL)
7483 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007484 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007485}
7486
7487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 * Find item "key[len]" in Dictionary "d".
7489 * If "len" is negative use strlen(key).
7490 * Returns NULL when not found.
7491 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007492 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007493dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495#define AKEYLEN 200
7496 char_u buf[AKEYLEN];
7497 char_u *akey;
7498 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007501 if (len < 0)
7502 akey = key;
7503 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 tofree = akey = vim_strnsave(key, len);
7506 if (akey == NULL)
7507 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007508 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 else
7510 {
7511 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007512 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 akey = buf;
7514 }
7515
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 vim_free(tofree);
7518 if (HASHITEM_EMPTY(hi))
7519 return NULL;
7520 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521}
7522
7523/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007524 * Get a string item from a dictionary.
7525 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007526 * Returns NULL if the entry doesn't exist or out of memory.
7527 */
7528 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007529get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007530{
7531 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007532 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007533
7534 di = dict_find(d, key, -1);
7535 if (di == NULL)
7536 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007537 s = get_tv_string(&di->di_tv);
7538 if (save && s != NULL)
7539 s = vim_strsave(s);
7540 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541}
7542
7543/*
7544 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007545 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007546 */
7547 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007549{
7550 dictitem_T *di;
7551
7552 di = dict_find(d, key, -1);
7553 if (di == NULL)
7554 return 0;
7555 return get_tv_number(&di->di_tv);
7556}
7557
7558/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 * Return an allocated string with the string representation of a Dictionary.
7560 * May return NULL.
7561 */
7562 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007563dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564{
7565 garray_T ga;
7566 int first = TRUE;
7567 char_u *tofree;
7568 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 return NULL;
7576 ga_init2(&ga, (int)sizeof(char), 80);
7577 ga_append(&ga, '{');
7578
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007579 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007580 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584 --todo;
7585
7586 if (first)
7587 first = FALSE;
7588 else
7589 ga_concat(&ga, (char_u *)", ");
7590
7591 tofree = string_quote(hi->hi_key, FALSE);
7592 if (tofree != NULL)
7593 {
7594 ga_concat(&ga, tofree);
7595 vim_free(tofree);
7596 }
7597 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 if (s != NULL)
7600 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007602 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007603 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007604 line_breakcheck();
7605
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007608 if (todo > 0)
7609 {
7610 vim_free(ga.ga_data);
7611 return NULL;
7612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613
7614 ga_append(&ga, '}');
7615 ga_append(&ga, NUL);
7616 return (char_u *)ga.ga_data;
7617}
7618
7619/*
7620 * Allocate a variable for a Dictionary and fill it from "*arg".
7621 * Return OK or FAIL. Returns NOTDONE for {expr}.
7622 */
7623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007624get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625{
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 dict_T *d = NULL;
7627 typval_T tvkey;
7628 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007629 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007632 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633
7634 /*
7635 * First check if it's not a curly-braces thing: {expr}.
7636 * Must do this without evaluating, otherwise a function may be called
7637 * twice. Unfortunately this means we need to call eval1() twice for the
7638 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 if (*start != '}')
7642 {
7643 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7644 return FAIL;
7645 if (*start == '}')
7646 return NOTDONE;
7647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648
7649 if (evaluate)
7650 {
7651 d = dict_alloc();
7652 if (d == NULL)
7653 return FAIL;
7654 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 tvkey.v_type = VAR_UNKNOWN;
7656 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657
7658 *arg = skipwhite(*arg + 1);
7659 while (**arg != '}' && **arg != NUL)
7660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 goto failret;
7663 if (**arg != ':')
7664 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007665 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007666 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 goto failret;
7668 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007669 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007671 key = get_tv_string_buf_chk(&tvkey, buf);
7672 if (key == NULL || *key == NUL)
7673 {
7674 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7675 if (key != NULL)
7676 EMSG(_(e_emptykey));
7677 clear_tv(&tvkey);
7678 goto failret;
7679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681
7682 *arg = skipwhite(*arg + 1);
7683 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7684 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007685 if (evaluate)
7686 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 goto failret;
7688 }
7689 if (evaluate)
7690 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 if (item != NULL)
7693 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007694 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 clear_tv(&tv);
7697 goto failret;
7698 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 item = dictitem_alloc(key);
7700 clear_tv(&tvkey);
7701 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007704 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007705 if (dict_add(d, item) == FAIL)
7706 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707 }
7708 }
7709
7710 if (**arg == '}')
7711 break;
7712 if (**arg != ',')
7713 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007714 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 goto failret;
7716 }
7717 *arg = skipwhite(*arg + 1);
7718 }
7719
7720 if (**arg != '}')
7721 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007722 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723failret:
7724 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007725 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 return FAIL;
7727 }
7728
7729 *arg = skipwhite(*arg + 1);
7730 if (evaluate)
7731 {
7732 rettv->v_type = VAR_DICT;
7733 rettv->vval.v_dict = d;
7734 ++d->dv_refcount;
7735 }
7736
7737 return OK;
7738}
7739
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007740#if defined(FEAT_CHANNEL) || defined(PROTO)
7741/*
7742 * Decrement the reference count on "channel" and free it when it goes down to
7743 * zero.
7744 * Returns TRUE when the channel was freed.
7745 */
7746 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007747channel_unref(channel_T *channel)
7748{
7749 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007750 {
Bram Moolenaar77073442016-02-13 23:23:53 +01007751 channel_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007752 return TRUE;
7753 }
7754 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007755}
7756#endif
7757
Bram Moolenaar65edff82016-02-21 16:40:11 +01007758#if defined(FEAT_JOB) || defined(PROTO)
7759static job_T *first_job = NULL;
7760
Bram Moolenaar835dc632016-02-07 14:27:38 +01007761 static void
7762job_free(job_T *job)
7763{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007764# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007765 if (job->jv_channel != NULL)
7766 {
7767 /* The channel doesn't count as a references for the job, we need to
7768 * NULL the reference when the job is freed. */
7769 job->jv_channel->ch_job = NULL;
7770 channel_unref(job->jv_channel);
7771 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007772# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007773 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007774
7775 if (job->jv_next != NULL)
7776 job->jv_next->jv_prev = job->jv_prev;
7777 if (job->jv_prev == NULL)
7778 first_job = job->jv_next;
7779 else
7780 job->jv_prev->jv_next = job->jv_next;
7781
7782 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007783 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007784 vim_free(job);
7785}
7786
7787 static void
7788job_unref(job_T *job)
7789{
7790 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007791 {
7792 /* Do not free the job when it has not ended yet and there is a
7793 * "stoponexit" flag or an exit callback. */
7794 if (job->jv_status != JOB_STARTED
7795 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
7796 job_free(job);
7797 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007798}
7799
7800/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007801 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007802 */
7803 static job_T *
7804job_alloc(void)
7805{
7806 job_T *job;
7807
7808 job = (job_T *)alloc_clear(sizeof(job_T));
7809 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007810 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007811 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007812 job->jv_stoponexit = vim_strsave((char_u *)"term");
7813
7814 if (first_job != NULL)
7815 {
7816 first_job->jv_prev = job;
7817 job->jv_next = first_job;
7818 }
7819 first_job = job;
7820 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007821 return job;
7822}
7823
Bram Moolenaar65edff82016-02-21 16:40:11 +01007824 static void
7825job_set_options(job_T *job, jobopt_T *opt)
7826{
7827 if (opt->jo_set & JO_STOPONEXIT)
7828 {
7829 vim_free(job->jv_stoponexit);
7830 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7831 job->jv_stoponexit = NULL;
7832 else
7833 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7834 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007835 if (opt->jo_set & JO_EXIT_CB)
7836 {
7837 vim_free(job->jv_exit_cb);
7838 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7839 job->jv_exit_cb = NULL;
7840 else
7841 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7842 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007843}
7844
7845/*
7846 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7847 */
7848 void
7849job_stop_on_exit()
7850{
7851 job_T *job;
7852
7853 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007854 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007855 mch_stop_job(job, job->jv_stoponexit);
7856}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007857#endif
7858
Bram Moolenaar17a13432016-01-24 14:22:10 +01007859 static char *
7860get_var_special_name(int nr)
7861{
7862 switch (nr)
7863 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007864 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007865 case VVAL_TRUE: return "v:true";
7866 case VVAL_NONE: return "v:none";
7867 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007868 }
7869 EMSG2(_(e_intern2), "get_var_special_name()");
7870 return "42";
7871}
7872
Bram Moolenaar8c711452005-01-14 21:53:12 +00007873/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007874 * Return a string with the string representation of a variable.
7875 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007876 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007877 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007878 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007879 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007880 */
7881 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007882echo_string(
7883 typval_T *tv,
7884 char_u **tofree,
7885 char_u *numbuf,
7886 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007887{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888 static int recurse = 0;
7889 char_u *r = NULL;
7890
Bram Moolenaar33570922005-01-25 22:26:29 +00007891 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007892 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007893 if (!did_echo_string_emsg)
7894 {
7895 /* Only give this message once for a recursive call to avoid
7896 * flooding the user with errors. And stop iterating over lists
7897 * and dicts. */
7898 did_echo_string_emsg = TRUE;
7899 EMSG(_("E724: variable nested too deep for displaying"));
7900 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007901 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007902 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007903 }
7904 ++recurse;
7905
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007906 switch (tv->v_type)
7907 {
7908 case VAR_FUNC:
7909 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007910 r = tv->vval.v_string;
7911 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007912
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007914 if (tv->vval.v_list == NULL)
7915 {
7916 *tofree = NULL;
7917 r = NULL;
7918 }
7919 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7920 {
7921 *tofree = NULL;
7922 r = (char_u *)"[...]";
7923 }
7924 else
7925 {
7926 tv->vval.v_list->lv_copyID = copyID;
7927 *tofree = list2string(tv, copyID);
7928 r = *tofree;
7929 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007930 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007931
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007933 if (tv->vval.v_dict == NULL)
7934 {
7935 *tofree = NULL;
7936 r = NULL;
7937 }
7938 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7939 {
7940 *tofree = NULL;
7941 r = (char_u *)"{...}";
7942 }
7943 else
7944 {
7945 tv->vval.v_dict->dv_copyID = copyID;
7946 *tofree = dict2string(tv, copyID);
7947 r = *tofree;
7948 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007949 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007950
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007951 case VAR_STRING:
7952 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007953 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007954 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007955 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007956 *tofree = NULL;
7957 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007958 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007959
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007960 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007961#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007962 *tofree = NULL;
7963 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7964 r = numbuf;
7965 break;
7966#endif
7967
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007968 case VAR_SPECIAL:
7969 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007970 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007971 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007973
Bram Moolenaar8502c702014-06-17 12:51:16 +02007974 if (--recurse == 0)
7975 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007976 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007977}
7978
7979/*
7980 * Return a string with the string representation of a variable.
7981 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7982 * "numbuf" is used for a number.
7983 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007984 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007985 */
7986 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007987tv2string(
7988 typval_T *tv,
7989 char_u **tofree,
7990 char_u *numbuf,
7991 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007992{
7993 switch (tv->v_type)
7994 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007995 case VAR_FUNC:
7996 *tofree = string_quote(tv->vval.v_string, TRUE);
7997 return *tofree;
7998 case VAR_STRING:
7999 *tofree = string_quote(tv->vval.v_string, FALSE);
8000 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008001#ifdef FEAT_FLOAT
8002 case VAR_FLOAT:
8003 *tofree = NULL;
8004 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8005 return numbuf;
8006#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008007 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008008 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008009 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008010 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008011 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008012 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008013 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008015 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008016 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008017}
8018
8019/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 * Return string "str" in ' quotes, doubling ' characters.
8021 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008022 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008023 */
8024 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008025string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008026{
Bram Moolenaar33570922005-01-25 22:26:29 +00008027 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008028 char_u *p, *r, *s;
8029
Bram Moolenaar33570922005-01-25 22:26:29 +00008030 len = (function ? 13 : 3);
8031 if (str != NULL)
8032 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008033 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 for (p = str; *p != NUL; mb_ptr_adv(p))
8035 if (*p == '\'')
8036 ++len;
8037 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008038 s = r = alloc(len);
8039 if (r != NULL)
8040 {
8041 if (function)
8042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008043 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008044 r += 10;
8045 }
8046 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008047 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 if (str != NULL)
8049 for (p = str; *p != NUL; )
8050 {
8051 if (*p == '\'')
8052 *r++ = '\'';
8053 MB_COPY_CHAR(p, r);
8054 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008055 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008056 if (function)
8057 *r++ = ')';
8058 *r++ = NUL;
8059 }
8060 return s;
8061}
8062
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008063#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008064/*
8065 * Convert the string "text" to a floating point number.
8066 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8067 * this always uses a decimal point.
8068 * Returns the length of the text that was consumed.
8069 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008070 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008071string2float(
8072 char_u *text,
8073 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008074{
8075 char *s = (char *)text;
8076 float_T f;
8077
8078 f = strtod(s, &s);
8079 *value = f;
8080 return (int)((char_u *)s - text);
8081}
8082#endif
8083
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 * Get the value of an environment variable.
8086 * "arg" is pointing to the '$'. It is advanced to after the name.
8087 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008088 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 */
8090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008091get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092{
8093 char_u *string = NULL;
8094 int len;
8095 int cc;
8096 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008097 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098
8099 ++*arg;
8100 name = *arg;
8101 len = get_env_len(arg);
8102 if (evaluate)
8103 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008104 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008105 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008106
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008107 cc = name[len];
8108 name[len] = NUL;
8109 /* first try vim_getenv(), fast for normal environment vars */
8110 string = vim_getenv(name, &mustfree);
8111 if (string != NULL && *string != NUL)
8112 {
8113 if (!mustfree)
8114 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008116 else
8117 {
8118 if (mustfree)
8119 vim_free(string);
8120
8121 /* next try expanding things like $VIM and ${HOME} */
8122 string = expand_env_save(name - 1);
8123 if (string != NULL && *string == '$')
8124 {
8125 vim_free(string);
8126 string = NULL;
8127 }
8128 }
8129 name[len] = cc;
8130
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008131 rettv->v_type = VAR_STRING;
8132 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 }
8134
8135 return OK;
8136}
8137
8138/*
8139 * Array with names and number of arguments of all internal functions
8140 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8141 */
8142static struct fst
8143{
8144 char *f_name; /* function name */
8145 char f_min_argc; /* minimal number of arguments */
8146 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008147 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008148 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149} functions[] =
8150{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008151#ifdef FEAT_FLOAT
8152 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008153 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008154#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008155 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008156 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008157 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"append", 2, 2, f_append},
8159 {"argc", 0, 0, f_argc},
8160 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008161 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008162 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008163#ifdef FEAT_FLOAT
8164 {"asin", 1, 1, f_asin}, /* WJMc */
8165#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008166 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008167 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008168 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008169 {"assert_false", 1, 2, f_assert_false},
8170 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008171#ifdef FEAT_FLOAT
8172 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008173 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008176 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"bufexists", 1, 1, f_bufexists},
8178 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8179 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8180 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8181 {"buflisted", 1, 1, f_buflisted},
8182 {"bufloaded", 1, 1, f_bufloaded},
8183 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008184 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"bufwinnr", 1, 1, f_bufwinnr},
8186 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008187 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008188 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008189 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008190#ifdef FEAT_FLOAT
8191 {"ceil", 1, 1, f_ceil},
8192#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008193#ifdef FEAT_CHANNEL
8194 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008195# ifdef FEAT_JOB
8196 {"ch_getjob", 1, 1, f_ch_getjob},
8197# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008198 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008199 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008200 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008201 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008202 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008203 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8204 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008205 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008206 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008207#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008208 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008209 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008211 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008213#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008214 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008215 {"complete_add", 1, 1, f_complete_add},
8216 {"complete_check", 0, 0, f_complete_check},
8217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008219 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008220#ifdef FEAT_FLOAT
8221 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008222 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008223#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008224 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008226 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008227 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008228 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008230 {"diff_filler", 1, 1, f_diff_filler},
8231 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008232 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008233 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008235 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"eventhandler", 0, 0, f_eventhandler},
8237 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008238 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008240#ifdef FEAT_FLOAT
8241 {"exp", 1, 1, f_exp},
8242#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008243 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008244 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008245 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8247 {"filereadable", 1, 1, f_filereadable},
8248 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008249 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008250 {"finddir", 1, 3, f_finddir},
8251 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008252#ifdef FEAT_FLOAT
8253 {"float2nr", 1, 1, f_float2nr},
8254 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008255 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008256#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008257 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {"fnamemodify", 2, 2, f_fnamemodify},
8259 {"foldclosed", 1, 1, f_foldclosed},
8260 {"foldclosedend", 1, 1, f_foldclosedend},
8261 {"foldlevel", 1, 1, f_foldlevel},
8262 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008263 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008265 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008266 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008267 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008268 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008269 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 {"getchar", 0, 1, f_getchar},
8271 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008272 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"getcmdline", 0, 0, f_getcmdline},
8274 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008275 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008276 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008277 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008278 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008279 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008280 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"getfsize", 1, 1, f_getfsize},
8282 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008283 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008284 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008285 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008286 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008287 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008288 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008289 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008290 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008292 {"gettabvar", 2, 3, f_gettabvar},
8293 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"getwinposx", 0, 0, f_getwinposx},
8295 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008296 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008297 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008298 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008299 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008301 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008302 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008303 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8305 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8306 {"histadd", 2, 2, f_histadd},
8307 {"histdel", 1, 2, f_histdel},
8308 {"histget", 1, 2, f_histget},
8309 {"histnr", 1, 1, f_histnr},
8310 {"hlID", 1, 1, f_hlID},
8311 {"hlexists", 1, 1, f_hlexists},
8312 {"hostname", 0, 0, f_hostname},
8313 {"iconv", 3, 3, f_iconv},
8314 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008315 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008316 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008318 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {"inputrestore", 0, 0, f_inputrestore},
8320 {"inputsave", 0, 0, f_inputsave},
8321 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008322 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008323 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008325 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008326#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8327 {"isnan", 1, 1, f_isnan},
8328#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008329 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008330#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008331# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008332 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008333# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008334 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008335 {"job_start", 1, 2, f_job_start},
8336 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008337 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008338#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008339 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008340 {"js_decode", 1, 1, f_js_decode},
8341 {"js_encode", 1, 1, f_js_encode},
8342 {"json_decode", 1, 1, f_json_decode},
8343 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008344 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008346 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 {"libcall", 3, 3, f_libcall},
8348 {"libcallnr", 3, 3, f_libcallnr},
8349 {"line", 1, 1, f_line},
8350 {"line2byte", 1, 1, f_line2byte},
8351 {"lispindent", 1, 1, f_lispindent},
8352 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008353#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008354 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008355 {"log10", 1, 1, f_log10},
8356#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008357#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008358 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008359#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008360 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008361 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008362 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008363 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008364 {"matchadd", 2, 5, f_matchadd},
8365 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008366 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008367 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008368 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008369 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008370 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008371 {"max", 1, 1, f_max},
8372 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008373#ifdef vim_mkdir
8374 {"mkdir", 1, 3, f_mkdir},
8375#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008376 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008377#ifdef FEAT_MZSCHEME
8378 {"mzeval", 1, 1, f_mzeval},
8379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008381 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008382 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008383 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008384#ifdef FEAT_PERL
8385 {"perleval", 1, 1, f_perleval},
8386#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008387#ifdef FEAT_FLOAT
8388 {"pow", 2, 2, f_pow},
8389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008391 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008392 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008393#ifdef FEAT_PYTHON3
8394 {"py3eval", 1, 1, f_py3eval},
8395#endif
8396#ifdef FEAT_PYTHON
8397 {"pyeval", 1, 1, f_pyeval},
8398#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008399 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008400 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008401 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008402 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008403 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 {"remote_expr", 2, 3, f_remote_expr},
8405 {"remote_foreground", 1, 1, f_remote_foreground},
8406 {"remote_peek", 1, 2, f_remote_peek},
8407 {"remote_read", 1, 1, f_remote_read},
8408 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008409 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008411 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008413 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008414#ifdef FEAT_FLOAT
8415 {"round", 1, 1, f_round},
8416#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008417 {"screenattr", 2, 2, f_screenattr},
8418 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008419 {"screencol", 0, 0, f_screencol},
8420 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008421 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008422 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008423 {"searchpair", 3, 7, f_searchpair},
8424 {"searchpairpos", 3, 7, f_searchpairpos},
8425 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {"server2client", 2, 2, f_server2client},
8427 {"serverlist", 0, 0, f_serverlist},
8428 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008429 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"setcmdpos", 1, 1, f_setcmdpos},
8431 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008432 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008433 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008434 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008435 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008437 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008438 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008440#ifdef FEAT_CRYPT
8441 {"sha256", 1, 1, f_sha256},
8442#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008443 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008444 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008446#ifdef FEAT_FLOAT
8447 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008448 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008449#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008450 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008451 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008452 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008453 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008454 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008455#ifdef FEAT_FLOAT
8456 {"sqrt", 1, 1, f_sqrt},
8457 {"str2float", 1, 1, f_str2float},
8458#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008459 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008460 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008461 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462#ifdef HAVE_STRFTIME
8463 {"strftime", 1, 2, f_strftime},
8464#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008465 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008466 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {"strlen", 1, 1, f_strlen},
8468 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008469 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008471 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008472 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {"substitute", 4, 4, f_substitute},
8474 {"synID", 3, 3, f_synID},
8475 {"synIDattr", 2, 3, f_synIDattr},
8476 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008477 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008478 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008479 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008480 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008481 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008482 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008483 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008484 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008485 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008486#ifdef FEAT_FLOAT
8487 {"tan", 1, 1, f_tan},
8488 {"tanh", 1, 1, f_tanh},
8489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008491 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {"tolower", 1, 1, f_tolower},
8493 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008494 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008495#ifdef FEAT_FLOAT
8496 {"trunc", 1, 1, f_trunc},
8497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008499 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008500 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008501 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008502 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {"virtcol", 1, 1, f_virtcol},
8504 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008505 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"winbufnr", 1, 1, f_winbufnr},
8507 {"wincol", 0, 0, f_wincol},
8508 {"winheight", 1, 1, f_winheight},
8509 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008510 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008512 {"winrestview", 1, 1, f_winrestview},
8513 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008515 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008516 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008517 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518};
8519
8520#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8521
8522/*
8523 * Function given to ExpandGeneric() to obtain the list of internal
8524 * or user defined function names.
8525 */
8526 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008527get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528{
8529 static int intidx = -1;
8530 char_u *name;
8531
8532 if (idx == 0)
8533 intidx = -1;
8534 if (intidx < 0)
8535 {
8536 name = get_user_func_name(xp, idx);
8537 if (name != NULL)
8538 return name;
8539 }
8540 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8541 {
8542 STRCPY(IObuff, functions[intidx].f_name);
8543 STRCAT(IObuff, "(");
8544 if (functions[intidx].f_max_argc == 0)
8545 STRCAT(IObuff, ")");
8546 return IObuff;
8547 }
8548
8549 return NULL;
8550}
8551
8552/*
8553 * Function given to ExpandGeneric() to obtain the list of internal or
8554 * user defined variable or function names.
8555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008557get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
8559 static int intidx = -1;
8560 char_u *name;
8561
8562 if (idx == 0)
8563 intidx = -1;
8564 if (intidx < 0)
8565 {
8566 name = get_function_name(xp, idx);
8567 if (name != NULL)
8568 return name;
8569 }
8570 return get_user_var_name(xp, ++intidx);
8571}
8572
8573#endif /* FEAT_CMDL_COMPL */
8574
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008575#if defined(EBCDIC) || defined(PROTO)
8576/*
8577 * Compare struct fst by function name.
8578 */
8579 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008580compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008581{
8582 struct fst *p1 = (struct fst *)s1;
8583 struct fst *p2 = (struct fst *)s2;
8584
8585 return STRCMP(p1->f_name, p2->f_name);
8586}
8587
8588/*
8589 * Sort the function table by function name.
8590 * The sorting of the table above is ASCII dependant.
8591 * On machines using EBCDIC we have to sort it.
8592 */
8593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008594sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008595{
8596 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8597
8598 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8599}
8600#endif
8601
8602
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603/*
8604 * Find internal function in table above.
8605 * Return index, or -1 if not found
8606 */
8607 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008608find_internal_func(
8609 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610{
8611 int first = 0;
8612 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8613 int cmp;
8614 int x;
8615
8616 /*
8617 * Find the function name in the table. Binary search.
8618 */
8619 while (first <= last)
8620 {
8621 x = first + ((unsigned)(last - first) >> 1);
8622 cmp = STRCMP(name, functions[x].f_name);
8623 if (cmp < 0)
8624 last = x - 1;
8625 else if (cmp > 0)
8626 first = x + 1;
8627 else
8628 return x;
8629 }
8630 return -1;
8631}
8632
8633/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008634 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8635 * name it contains, otherwise return "name".
8636 */
8637 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008638deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008639{
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008641 int cc;
8642
8643 cc = name[*lenp];
8644 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008645 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008646 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008647 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008648 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008649 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 {
8651 *lenp = 0;
8652 return (char_u *)""; /* just in case */
8653 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008654 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656 }
8657
8658 return name;
8659}
8660
8661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 * Allocate a variable for the result of a function.
8663 * Return OK or FAIL.
8664 */
8665 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008666get_func_tv(
8667 char_u *name, /* name of the function */
8668 int len, /* length of "name" */
8669 typval_T *rettv,
8670 char_u **arg, /* argument, pointing to the '(' */
8671 linenr_T firstline, /* first line of range */
8672 linenr_T lastline, /* last line of range */
8673 int *doesrange, /* return: function handled range */
8674 int evaluate,
8675 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 char_u *argp;
8678 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008679 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 int argcount = 0; /* number of arguments found */
8681
8682 /*
8683 * Get the arguments.
8684 */
8685 argp = *arg;
8686 while (argcount < MAX_FUNC_ARGS)
8687 {
8688 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8689 if (*argp == ')' || *argp == ',' || *argp == NUL)
8690 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8692 {
8693 ret = FAIL;
8694 break;
8695 }
8696 ++argcount;
8697 if (*argp != ',')
8698 break;
8699 }
8700 if (*argp == ')')
8701 ++argp;
8702 else
8703 ret = FAIL;
8704
8705 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008707 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008709 {
8710 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008711 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008712 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008713 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715
8716 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008717 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718
8719 *arg = skipwhite(argp);
8720 return ret;
8721}
8722
8723
8724/*
8725 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008726 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008727 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008730call_func(
8731 char_u *funcname, /* name of the function */
8732 int len, /* length of "name" */
8733 typval_T *rettv, /* return value goes here */
8734 int argcount, /* number of "argvars" */
8735 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008736 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008737 linenr_T firstline, /* first line of range */
8738 linenr_T lastline, /* last line of range */
8739 int *doesrange, /* return: function handled range */
8740 int evaluate,
8741 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742{
8743 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744#define ERROR_UNKNOWN 0
8745#define ERROR_TOOMANY 1
8746#define ERROR_TOOFEW 2
8747#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008748#define ERROR_DICT 4
8749#define ERROR_NONE 5
8750#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 int error = ERROR_NONE;
8752 int i;
8753 int llen;
8754 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755#define FLEN_FIXED 40
8756 char_u fname_buf[FLEN_FIXED + 1];
8757 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008758 char_u *name;
8759
8760 /* Make a copy of the name, if it comes from a funcref variable it could
8761 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008762 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008763 if (name == NULL)
8764 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765
8766 /*
8767 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8768 * Change <SNR>123_name() to K_SNR 123_name().
8769 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 llen = eval_fname_script(name);
8772 if (llen > 0)
8773 {
8774 fname_buf[0] = K_SPECIAL;
8775 fname_buf[1] = KS_EXTRA;
8776 fname_buf[2] = (int)KE_SNR;
8777 i = 3;
8778 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8779 {
8780 if (current_SID <= 0)
8781 error = ERROR_SCRIPT;
8782 else
8783 {
8784 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8785 i = (int)STRLEN(fname_buf);
8786 }
8787 }
8788 if (i + STRLEN(name + llen) < FLEN_FIXED)
8789 {
8790 STRCPY(fname_buf + i, name + llen);
8791 fname = fname_buf;
8792 }
8793 else
8794 {
8795 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8796 if (fname == NULL)
8797 error = ERROR_OTHER;
8798 else
8799 {
8800 mch_memmove(fname, fname_buf, (size_t)i);
8801 STRCPY(fname + i, name + llen);
8802 }
8803 }
8804 }
8805 else
8806 fname = name;
8807
8808 *doesrange = FALSE;
8809
8810
8811 /* execute the function if no errors detected and executing */
8812 if (evaluate && error == ERROR_NONE)
8813 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008814 char_u *rfname = fname;
8815
8816 /* Ignore "g:" before a function name. */
8817 if (fname[0] == 'g' && fname[1] == ':')
8818 rfname = fname + 2;
8819
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008820 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8821 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 error = ERROR_UNKNOWN;
8823
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008824 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 {
8826 /*
8827 * User defined function.
8828 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008829 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008830
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008832 /* Trigger FuncUndefined event, may load the function. */
8833 if (fp == NULL
8834 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008835 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008836 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008838 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008839 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 }
8841#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008842 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008843 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008844 {
8845 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008846 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008847 }
8848
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 if (fp != NULL)
8850 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008851 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008853 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008855 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008857 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008858 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 else
8860 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008861 int did_save_redo = FALSE;
8862
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 /*
8864 * Call the user function.
8865 * Save and restore search patterns, script variables and
8866 * redo buffer.
8867 */
8868 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008869#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008870 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008871#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008872 {
8873 saveRedobuff();
8874 did_save_redo = TRUE;
8875 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008876 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008877 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008878 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008879 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8880 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8881 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008882 /* Function was unreferenced while being used, free it
8883 * now. */
8884 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008885 if (did_save_redo)
8886 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 restore_search_patterns();
8888 error = ERROR_NONE;
8889 }
8890 }
8891 }
8892 else
8893 {
8894 /*
8895 * Find the function name in the table, call its implementation.
8896 */
8897 i = find_internal_func(fname);
8898 if (i >= 0)
8899 {
8900 if (argcount < functions[i].f_min_argc)
8901 error = ERROR_TOOFEW;
8902 else if (argcount > functions[i].f_max_argc)
8903 error = ERROR_TOOMANY;
8904 else
8905 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008906 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008907 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 error = ERROR_NONE;
8909 }
8910 }
8911 }
8912 /*
8913 * The function call (or "FuncUndefined" autocommand sequence) might
8914 * have been aborted by an error, an interrupt, or an explicitly thrown
8915 * exception that has not been caught so far. This situation can be
8916 * tested for by calling aborting(). For an error in an internal
8917 * function or for the "E132" error in call_user_func(), however, the
8918 * throw point at which the "force_abort" flag (temporarily reset by
8919 * emsg()) is normally updated has not been reached yet. We need to
8920 * update that flag first to make aborting() reliable.
8921 */
8922 update_force_abort();
8923 }
8924 if (error == ERROR_NONE)
8925 ret = OK;
8926
8927 /*
8928 * Report an error unless the argument evaluation or function call has been
8929 * cancelled due to an aborting error, an interrupt, or an exception.
8930 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008931 if (!aborting())
8932 {
8933 switch (error)
8934 {
8935 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008936 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008937 break;
8938 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008939 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008940 break;
8941 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008942 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008943 name);
8944 break;
8945 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008946 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008947 name);
8948 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008949 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008950 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008951 name);
8952 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008953 }
8954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 if (fname != name && fname != fname_buf)
8957 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008958 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
8960 return ret;
8961}
8962
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008963/*
8964 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008965 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008966 */
8967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008968emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008969{
8970 char_u *p;
8971
8972 if (*name == K_SPECIAL)
8973 p = concat_str((char_u *)"<SNR>", name + 3);
8974 else
8975 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008976 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008977 if (p != name)
8978 vim_free(p);
8979}
8980
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008981/*
8982 * Return TRUE for a non-zero Number and a non-empty String.
8983 */
8984 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008985non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008986{
8987 return ((argvars[0].v_type == VAR_NUMBER
8988 && argvars[0].vval.v_number != 0)
8989 || (argvars[0].v_type == VAR_STRING
8990 && argvars[0].vval.v_string != NULL
8991 && *argvars[0].vval.v_string != NUL));
8992}
8993
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994/*********************************************
8995 * Implementation of the built-in functions
8996 */
8997
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008998#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008999static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009000
9001/*
9002 * Get the float value of "argvars[0]" into "f".
9003 * Returns FAIL when the argument is not a Number or Float.
9004 */
9005 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009006get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009007{
9008 if (argvars[0].v_type == VAR_FLOAT)
9009 {
9010 *f = argvars[0].vval.v_float;
9011 return OK;
9012 }
9013 if (argvars[0].v_type == VAR_NUMBER)
9014 {
9015 *f = (float_T)argvars[0].vval.v_number;
9016 return OK;
9017 }
9018 EMSG(_("E808: Number or Float required"));
9019 return FAIL;
9020}
9021
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009022/*
9023 * "abs(expr)" function
9024 */
9025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009026f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009027{
9028 if (argvars[0].v_type == VAR_FLOAT)
9029 {
9030 rettv->v_type = VAR_FLOAT;
9031 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9032 }
9033 else
9034 {
9035 varnumber_T n;
9036 int error = FALSE;
9037
9038 n = get_tv_number_chk(&argvars[0], &error);
9039 if (error)
9040 rettv->vval.v_number = -1;
9041 else if (n > 0)
9042 rettv->vval.v_number = n;
9043 else
9044 rettv->vval.v_number = -n;
9045 }
9046}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009047
9048/*
9049 * "acos()" function
9050 */
9051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009052f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009053{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009054 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009055
9056 rettv->v_type = VAR_FLOAT;
9057 if (get_float_arg(argvars, &f) == OK)
9058 rettv->vval.v_float = acos(f);
9059 else
9060 rettv->vval.v_float = 0.0;
9061}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009062#endif
9063
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009065 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 */
9067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009068f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069{
Bram Moolenaar33570922005-01-25 22:26:29 +00009070 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009073 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009075 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009076 && !tv_check_lock(l->lv_lock,
9077 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009078 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009080 }
9081 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009082 EMSG(_(e_listreq));
9083}
9084
9085/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009086 * "alloc_fail(id, countdown, repeat)" function
9087 */
9088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009089f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009090{
9091 if (argvars[0].v_type != VAR_NUMBER
9092 || argvars[0].vval.v_number <= 0
9093 || argvars[1].v_type != VAR_NUMBER
9094 || argvars[1].vval.v_number < 0
9095 || argvars[2].v_type != VAR_NUMBER)
9096 EMSG(_(e_invarg));
9097 else
9098 {
9099 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009100 if (alloc_fail_id >= aid_last)
9101 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009102 alloc_fail_countdown = argvars[1].vval.v_number;
9103 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009104 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009105 }
9106}
9107
9108/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009109 * "and(expr, expr)" function
9110 */
9111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009112f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009113{
9114 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9115 & get_tv_number_chk(&argvars[1], NULL);
9116}
9117
9118/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009119 * "append(lnum, string/list)" function
9120 */
9121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009122f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009123{
9124 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009125 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009126 list_T *l = NULL;
9127 listitem_T *li = NULL;
9128 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009129 long added = 0;
9130
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009131 /* When coming here from Insert mode, sync undo, so that this can be
9132 * undone separately from what was previously inserted. */
9133 if (u_sync_once == 2)
9134 {
9135 u_sync_once = 1; /* notify that u_sync() was called */
9136 u_sync(TRUE);
9137 }
9138
Bram Moolenaar0d660222005-01-07 21:51:51 +00009139 lnum = get_tv_lnum(argvars);
9140 if (lnum >= 0
9141 && lnum <= curbuf->b_ml.ml_line_count
9142 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009143 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009144 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009145 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009146 l = argvars[1].vval.v_list;
9147 if (l == NULL)
9148 return;
9149 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009150 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009151 for (;;)
9152 {
9153 if (l == NULL)
9154 tv = &argvars[1]; /* append a string */
9155 else if (li == NULL)
9156 break; /* end of list */
9157 else
9158 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009159 line = get_tv_string_chk(tv);
9160 if (line == NULL) /* type error */
9161 {
9162 rettv->vval.v_number = 1; /* Failed */
9163 break;
9164 }
9165 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009166 ++added;
9167 if (l == NULL)
9168 break;
9169 li = li->li_next;
9170 }
9171
9172 appended_lines_mark(lnum, added);
9173 if (curwin->w_cursor.lnum > lnum)
9174 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009176 else
9177 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178}
9179
9180/*
9181 * "argc()" function
9182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009184f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009186 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187}
9188
9189/*
9190 * "argidx()" function
9191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009193f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196}
9197
9198/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009199 * "arglistid()" function
9200 */
9201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009202f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009203{
9204 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009205
9206 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009207 wp = find_tabwin(&argvars[0], &argvars[1]);
9208 if (wp != NULL)
9209 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009210}
9211
9212/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 * "argv(nr)" function
9214 */
9215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009216f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217{
9218 int idx;
9219
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009220 if (argvars[0].v_type != VAR_UNKNOWN)
9221 {
9222 idx = get_tv_number_chk(&argvars[0], NULL);
9223 if (idx >= 0 && idx < ARGCOUNT)
9224 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9225 else
9226 rettv->vval.v_string = NULL;
9227 rettv->v_type = VAR_STRING;
9228 }
9229 else if (rettv_list_alloc(rettv) == OK)
9230 for (idx = 0; idx < ARGCOUNT; ++idx)
9231 list_append_string(rettv->vval.v_list,
9232 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233}
9234
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009235static void prepare_assert_error(garray_T*gap);
9236static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9237static void assert_error(garray_T *gap);
9238static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009239
9240/*
9241 * Prepare "gap" for an assert error and add the sourcing position.
9242 */
9243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009244prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009245{
9246 char buf[NUMBUFLEN];
9247
9248 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009249 if (sourcing_name != NULL)
9250 {
9251 ga_concat(gap, sourcing_name);
9252 if (sourcing_lnum > 0)
9253 ga_concat(gap, (char_u *)" ");
9254 }
9255 if (sourcing_lnum > 0)
9256 {
9257 sprintf(buf, "line %ld", (long)sourcing_lnum);
9258 ga_concat(gap, (char_u *)buf);
9259 }
9260 if (sourcing_name != NULL || sourcing_lnum > 0)
9261 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009262}
9263
9264/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009265 * Append "str" to "gap", escaping unprintable characters.
9266 * Changes NL to \n, CR to \r, etc.
9267 */
9268 static void
9269ga_concat_esc(garray_T *gap, char_u *str)
9270{
9271 char_u *p;
9272 char_u buf[NUMBUFLEN];
9273
9274 for (p = str; *p != NUL; ++p)
9275 switch (*p)
9276 {
9277 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9278 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9279 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9280 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9281 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9282 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9283 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9284 default:
9285 if (*p < ' ')
9286 {
9287 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9288 ga_concat(gap, buf);
9289 }
9290 else
9291 ga_append(gap, *p);
9292 break;
9293 }
9294}
9295
9296/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009297 * Fill "gap" with information about an assert error.
9298 */
9299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009300fill_assert_error(
9301 garray_T *gap,
9302 typval_T *opt_msg_tv,
9303 char_u *exp_str,
9304 typval_T *exp_tv,
9305 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009306{
9307 char_u numbuf[NUMBUFLEN];
9308 char_u *tofree;
9309
9310 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9311 {
9312 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9313 vim_free(tofree);
9314 }
9315 else
9316 {
9317 ga_concat(gap, (char_u *)"Expected ");
9318 if (exp_str == NULL)
9319 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009320 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009321 vim_free(tofree);
9322 }
9323 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009324 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009325 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009326 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009327 vim_free(tofree);
9328 }
9329}
Bram Moolenaar43345542015-11-29 17:35:35 +01009330
9331/*
9332 * Add an assert error to v:errors.
9333 */
9334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009335assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009336{
9337 struct vimvar *vp = &vimvars[VV_ERRORS];
9338
9339 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9340 /* Make sure v:errors is a list. */
9341 set_vim_var_list(VV_ERRORS, list_alloc());
9342 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9343}
9344
Bram Moolenaar43345542015-11-29 17:35:35 +01009345/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009346 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009347 */
9348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009349f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009350{
9351 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009352
9353 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9354 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009355 prepare_assert_error(&ga);
9356 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9357 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009358 ga_clear(&ga);
9359 }
9360}
9361
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009362/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009363 * "assert_exception(string[, msg])" function
9364 */
9365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009366f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009367{
9368 garray_T ga;
9369 char *error;
9370
9371 error = (char *)get_tv_string_chk(&argvars[0]);
9372 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9373 {
9374 prepare_assert_error(&ga);
9375 ga_concat(&ga, (char_u *)"v:exception is not set");
9376 assert_error(&ga);
9377 ga_clear(&ga);
9378 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009379 else if (error != NULL
9380 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009381 {
9382 prepare_assert_error(&ga);
9383 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9384 &vimvars[VV_EXCEPTION].vv_tv);
9385 assert_error(&ga);
9386 ga_clear(&ga);
9387 }
9388}
9389
9390/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009391 * "assert_fails(cmd [, error])" function
9392 */
9393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009394f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009395{
9396 char_u *cmd = get_tv_string_chk(&argvars[0]);
9397 garray_T ga;
9398
9399 called_emsg = FALSE;
9400 suppress_errthrow = TRUE;
9401 emsg_silent = TRUE;
9402 do_cmdline_cmd(cmd);
9403 if (!called_emsg)
9404 {
9405 prepare_assert_error(&ga);
9406 ga_concat(&ga, (char_u *)"command did not fail: ");
9407 ga_concat(&ga, cmd);
9408 assert_error(&ga);
9409 ga_clear(&ga);
9410 }
9411 else if (argvars[1].v_type != VAR_UNKNOWN)
9412 {
9413 char_u buf[NUMBUFLEN];
9414 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9415
9416 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9417 {
9418 prepare_assert_error(&ga);
9419 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9420 &vimvars[VV_ERRMSG].vv_tv);
9421 assert_error(&ga);
9422 ga_clear(&ga);
9423 }
9424 }
9425
9426 called_emsg = FALSE;
9427 suppress_errthrow = FALSE;
9428 emsg_silent = FALSE;
9429 emsg_on_display = FALSE;
9430 set_vim_var_string(VV_ERRMSG, NULL, 0);
9431}
9432
9433/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009434 * Common for assert_true() and assert_false().
9435 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009437assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009438{
9439 int error = FALSE;
9440 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009441
Bram Moolenaar37127922016-02-06 20:29:28 +01009442 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009443 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009444 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009445 if (argvars[0].v_type != VAR_NUMBER
9446 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9447 || error)
9448 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009449 prepare_assert_error(&ga);
9450 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009451 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009452 NULL, &argvars[0]);
9453 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009454 ga_clear(&ga);
9455 }
9456}
9457
9458/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009459 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009460 */
9461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009462f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009463{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009464 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009465}
9466
9467/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009468 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009469 */
9470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009471f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009472{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009473 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009474}
9475
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009476#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009477/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009478 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009479 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009481f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009482{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009483 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009484
9485 rettv->v_type = VAR_FLOAT;
9486 if (get_float_arg(argvars, &f) == OK)
9487 rettv->vval.v_float = asin(f);
9488 else
9489 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009490}
9491
9492/*
9493 * "atan()" function
9494 */
9495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009496f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009497{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009498 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009499
9500 rettv->v_type = VAR_FLOAT;
9501 if (get_float_arg(argvars, &f) == OK)
9502 rettv->vval.v_float = atan(f);
9503 else
9504 rettv->vval.v_float = 0.0;
9505}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009506
9507/*
9508 * "atan2()" function
9509 */
9510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009511f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009512{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009513 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009514
9515 rettv->v_type = VAR_FLOAT;
9516 if (get_float_arg(argvars, &fx) == OK
9517 && get_float_arg(&argvars[1], &fy) == OK)
9518 rettv->vval.v_float = atan2(fx, fy);
9519 else
9520 rettv->vval.v_float = 0.0;
9521}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009522#endif
9523
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524/*
9525 * "browse(save, title, initdir, default)" function
9526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009528f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
9530#ifdef FEAT_BROWSE
9531 int save;
9532 char_u *title;
9533 char_u *initdir;
9534 char_u *defname;
9535 char_u buf[NUMBUFLEN];
9536 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009537 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009539 save = get_tv_number_chk(&argvars[0], &error);
9540 title = get_tv_string_chk(&argvars[1]);
9541 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9542 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009544 if (error || title == NULL || initdir == NULL || defname == NULL)
9545 rettv->vval.v_string = NULL;
9546 else
9547 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009548 do_browse(save ? BROWSE_SAVE : 0,
9549 title, defname, NULL, initdir, NULL, curbuf);
9550#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009552#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009554}
9555
9556/*
9557 * "browsedir(title, initdir)" function
9558 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009560f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009561{
9562#ifdef FEAT_BROWSE
9563 char_u *title;
9564 char_u *initdir;
9565 char_u buf[NUMBUFLEN];
9566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 title = get_tv_string_chk(&argvars[0]);
9568 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009570 if (title == NULL || initdir == NULL)
9571 rettv->vval.v_string = NULL;
9572 else
9573 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579}
9580
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009581static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009582
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583/*
9584 * Find a buffer by number or exact name.
9585 */
9586 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009587find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
9589 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009591 if (avar->v_type == VAR_NUMBER)
9592 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009593 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009595 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009596 if (buf == NULL)
9597 {
9598 /* No full path name match, try a match with a URL or a "nofile"
9599 * buffer, these don't use the full path. */
9600 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9601 if (buf->b_fname != NULL
9602 && (path_with_url(buf->b_fname)
9603#ifdef FEAT_QUICKFIX
9604 || bt_nofile(buf)
9605#endif
9606 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009607 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009608 break;
9609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 }
9611 return buf;
9612}
9613
9614/*
9615 * "bufexists(expr)" function
9616 */
9617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009618f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009620 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621}
9622
9623/*
9624 * "buflisted(expr)" function
9625 */
9626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009627f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
9629 buf_T *buf;
9630
9631 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009632 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633}
9634
9635/*
9636 * "bufloaded(expr)" function
9637 */
9638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009639f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640{
9641 buf_T *buf;
9642
9643 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009644 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645}
9646
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009647static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009648
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649/*
9650 * Get buffer by number or pattern.
9651 */
9652 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009653get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 int save_magic;
9657 char_u *save_cpo;
9658 buf_T *buf;
9659
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 if (tv->v_type == VAR_NUMBER)
9661 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009662 if (tv->v_type != VAR_STRING)
9663 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (name == NULL || *name == NUL)
9665 return curbuf;
9666 if (name[0] == '$' && name[1] == NUL)
9667 return lastbuf;
9668
9669 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9670 save_magic = p_magic;
9671 p_magic = TRUE;
9672 save_cpo = p_cpo;
9673 p_cpo = (char_u *)"";
9674
9675 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009676 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677
9678 p_magic = save_magic;
9679 p_cpo = save_cpo;
9680
9681 /* If not found, try expanding the name, like done for bufexists(). */
9682 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684
9685 return buf;
9686}
9687
9688/*
9689 * "bufname(expr)" function
9690 */
9691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009692f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
9694 buf_T *buf;
9695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009696 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009698 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 --emsg_off;
9705}
9706
9707/*
9708 * "bufnr(expr)" function
9709 */
9710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009711f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712{
9713 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009714 int error = FALSE;
9715 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009717 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009719 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009720 --emsg_off;
9721
9722 /* If the buffer isn't found and the second argument is not zero create a
9723 * new buffer. */
9724 if (buf == NULL
9725 && argvars[1].v_type != VAR_UNKNOWN
9726 && get_tv_number_chk(&argvars[1], &error) != 0
9727 && !error
9728 && (name = get_tv_string_chk(&argvars[0])) != NULL
9729 && !error)
9730 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9731
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009735 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736}
9737
9738/*
9739 * "bufwinnr(nr)" function
9740 */
9741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009742f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743{
9744#ifdef FEAT_WINDOWS
9745 win_T *wp;
9746 int winnr = 0;
9747#endif
9748 buf_T *buf;
9749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009750 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009752 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753#ifdef FEAT_WINDOWS
9754 for (wp = firstwin; wp; wp = wp->w_next)
9755 {
9756 ++winnr;
9757 if (wp->w_buffer == buf)
9758 break;
9759 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763#endif
9764 --emsg_off;
9765}
9766
9767/*
9768 * "byte2line(byte)" function
9769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009771f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772{
9773#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775#else
9776 long boff = 0;
9777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009778 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 (linenr_T)0, &boff);
9784#endif
9785}
9786
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009788byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009789{
9790#ifdef FEAT_MBYTE
9791 char_u *t;
9792#endif
9793 char_u *str;
9794 long idx;
9795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009796 str = get_tv_string_chk(&argvars[0]);
9797 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009799 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009800 return;
9801
9802#ifdef FEAT_MBYTE
9803 t = str;
9804 for ( ; idx > 0; idx--)
9805 {
9806 if (*t == NUL) /* EOL reached */
9807 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009808 if (enc_utf8 && comp)
9809 t += utf_ptr2len(t);
9810 else
9811 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009812 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009813 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009814#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009815 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009817#endif
9818}
9819
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009820/*
9821 * "byteidx()" function
9822 */
9823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009824f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009825{
9826 byteidx(argvars, rettv, FALSE);
9827}
9828
9829/*
9830 * "byteidxcomp()" function
9831 */
9832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009833f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009834{
9835 byteidx(argvars, rettv, TRUE);
9836}
9837
Bram Moolenaardb913952012-06-29 12:54:53 +02009838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009839func_call(
9840 char_u *name,
9841 typval_T *args,
9842 dict_T *selfdict,
9843 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009844{
9845 listitem_T *item;
9846 typval_T argv[MAX_FUNC_ARGS + 1];
9847 int argc = 0;
9848 int dummy;
9849 int r = 0;
9850
9851 for (item = args->vval.v_list->lv_first; item != NULL;
9852 item = item->li_next)
9853 {
9854 if (argc == MAX_FUNC_ARGS)
9855 {
9856 EMSG(_("E699: Too many arguments"));
9857 break;
9858 }
9859 /* Make a copy of each argument. This is needed to be able to set
9860 * v_lock to VAR_FIXED in the copy without changing the original list.
9861 */
9862 copy_tv(&item->li_tv, &argv[argc++]);
9863 }
9864
9865 if (item == NULL)
9866 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9867 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9868 &dummy, TRUE, selfdict);
9869
9870 /* Free the arguments. */
9871 while (argc > 0)
9872 clear_tv(&argv[--argc]);
9873
9874 return r;
9875}
9876
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009877/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009878 * "call(func, arglist)" function
9879 */
9880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009881f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009882{
9883 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009884 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009885
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009886 if (argvars[1].v_type != VAR_LIST)
9887 {
9888 EMSG(_(e_listreq));
9889 return;
9890 }
9891 if (argvars[1].vval.v_list == NULL)
9892 return;
9893
9894 if (argvars[0].v_type == VAR_FUNC)
9895 func = argvars[0].vval.v_string;
9896 else
9897 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898 if (*func == NUL)
9899 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009900
Bram Moolenaare9a41262005-01-15 22:18:47 +00009901 if (argvars[2].v_type != VAR_UNKNOWN)
9902 {
9903 if (argvars[2].v_type != VAR_DICT)
9904 {
9905 EMSG(_(e_dictreq));
9906 return;
9907 }
9908 selfdict = argvars[2].vval.v_dict;
9909 }
9910
Bram Moolenaardb913952012-06-29 12:54:53 +02009911 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009912}
9913
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009914#ifdef FEAT_FLOAT
9915/*
9916 * "ceil({float})" function
9917 */
9918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009919f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009920{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009921 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009922
9923 rettv->v_type = VAR_FLOAT;
9924 if (get_float_arg(argvars, &f) == OK)
9925 rettv->vval.v_float = ceil(f);
9926 else
9927 rettv->vval.v_float = 0.0;
9928}
9929#endif
9930
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009931#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009932/*
9933 * Get a callback from "arg". It can be a Funcref or a function name.
9934 * When "arg" is zero return an empty string.
9935 * Return NULL for an invalid argument.
9936 */
9937 static char_u *
9938get_callback(typval_T *arg)
9939{
9940 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9941 return arg->vval.v_string;
9942 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9943 return (char_u *)"";
9944 EMSG(_("E999: Invalid callback argument"));
9945 return NULL;
9946}
9947
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009948 static int
9949handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9950{
9951 char_u *val = get_tv_string(item);
9952
9953 opt->jo_set |= jo;
9954 if (STRCMP(val, "nl") == 0)
9955 *modep = MODE_NL;
9956 else if (STRCMP(val, "raw") == 0)
9957 *modep = MODE_RAW;
9958 else if (STRCMP(val, "js") == 0)
9959 *modep = MODE_JS;
9960 else if (STRCMP(val, "json") == 0)
9961 *modep = MODE_JSON;
9962 else
9963 {
9964 EMSG2(_(e_invarg2), val);
9965 return FAIL;
9966 }
9967 return OK;
9968}
9969
9970 static void
9971clear_job_options(jobopt_T *opt)
9972{
9973 vim_memset(opt, 0, sizeof(jobopt_T));
9974}
9975
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009976/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009977 * Get the option entries from the dict in "tv", parse them and put the result
9978 * in "opt".
9979 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009980 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009981 */
9982 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009983get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009984{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009985 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01009986 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009987 dict_T *dict;
9988 int todo;
9989 hashitem_T *hi;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009990
Bram Moolenaar8600ace2016-02-19 23:31:40 +01009991 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009992 if (tv->v_type == VAR_UNKNOWN)
9993 return OK;
9994 if (tv->v_type != VAR_DICT)
9995 {
9996 EMSG(_(e_invarg));
9997 return FAIL;
9998 }
9999 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010000 if (dict == NULL)
10001 return OK;
10002
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010003 todo = (int)dict->dv_hashtab.ht_used;
10004 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10005 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010006 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010007 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010008
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010009 if (STRCMP(hi->hi_key, "mode") == 0)
10010 {
10011 if (!(supported & JO_MODE))
10012 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010013 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010014 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010015 }
10016 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10017 {
10018 if (!(supported & JO_IN_MODE))
10019 break;
10020 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10021 == FAIL)
10022 return FAIL;
10023 }
10024 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10025 {
10026 if (!(supported & JO_OUT_MODE))
10027 break;
10028 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10029 == FAIL)
10030 return FAIL;
10031 }
10032 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10033 {
10034 if (!(supported & JO_ERR_MODE))
10035 break;
10036 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10037 == FAIL)
10038 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010039 }
10040 else if (STRCMP(hi->hi_key, "callback") == 0)
10041 {
10042 if (!(supported & JO_CALLBACK))
10043 break;
10044 opt->jo_set |= JO_CALLBACK;
10045 opt->jo_callback = get_callback(item);
10046 if (opt->jo_callback == NULL)
10047 {
10048 EMSG2(_(e_invarg2), "callback");
10049 return FAIL;
10050 }
10051 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010052 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10053 {
10054 if (!(supported & JO_OUT_CALLBACK))
10055 break;
10056 opt->jo_set |= JO_OUT_CALLBACK;
10057 opt->jo_out_cb = get_callback(item);
10058 if (opt->jo_out_cb == NULL)
10059 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010060 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010061 return FAIL;
10062 }
10063 }
10064 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10065 {
10066 if (!(supported & JO_ERR_CALLBACK))
10067 break;
10068 opt->jo_set |= JO_ERR_CALLBACK;
10069 opt->jo_err_cb = get_callback(item);
10070 if (opt->jo_err_cb == NULL)
10071 {
10072 EMSG2(_(e_invarg2), "err-cb");
10073 return FAIL;
10074 }
10075 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010076 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10077 {
10078 if (!(supported & JO_CLOSE_CALLBACK))
10079 break;
10080 opt->jo_set |= JO_CLOSE_CALLBACK;
10081 opt->jo_close_cb = get_callback(item);
10082 if (opt->jo_close_cb == NULL)
10083 {
10084 EMSG2(_(e_invarg2), "close-cb");
10085 return FAIL;
10086 }
10087 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010088 else if (STRCMP(hi->hi_key, "waittime") == 0)
10089 {
10090 if (!(supported & JO_WAITTIME))
10091 break;
10092 opt->jo_set |= JO_WAITTIME;
10093 opt->jo_waittime = get_tv_number(item);
10094 }
10095 else if (STRCMP(hi->hi_key, "timeout") == 0)
10096 {
10097 if (!(supported & JO_TIMEOUT))
10098 break;
10099 opt->jo_set |= JO_TIMEOUT;
10100 opt->jo_timeout = get_tv_number(item);
10101 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010102 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10103 {
10104 if (!(supported & JO_OUT_TIMEOUT))
10105 break;
10106 opt->jo_set |= JO_OUT_TIMEOUT;
10107 opt->jo_out_timeout = get_tv_number(item);
10108 }
10109 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10110 {
10111 if (!(supported & JO_ERR_TIMEOUT))
10112 break;
10113 opt->jo_set |= JO_ERR_TIMEOUT;
10114 opt->jo_err_timeout = get_tv_number(item);
10115 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010116 else if (STRCMP(hi->hi_key, "part") == 0)
10117 {
10118 if (!(supported & JO_PART))
10119 break;
10120 opt->jo_set |= JO_PART;
10121 val = get_tv_string(item);
10122 if (STRCMP(val, "err") == 0)
10123 opt->jo_part = PART_ERR;
10124 else
10125 {
10126 EMSG2(_(e_invarg2), val);
10127 return FAIL;
10128 }
10129 }
10130 else if (STRCMP(hi->hi_key, "id") == 0)
10131 {
10132 if (!(supported & JO_ID))
10133 break;
10134 opt->jo_set |= JO_ID;
10135 opt->jo_id = get_tv_number(item);
10136 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010137 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10138 {
10139 if (!(supported & JO_STOPONEXIT))
10140 break;
10141 opt->jo_set |= JO_STOPONEXIT;
10142 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10143 opt->jo_soe_buf);
10144 if (opt->jo_stoponexit == NULL)
10145 {
10146 EMSG2(_(e_invarg2), "stoponexit");
10147 return FAIL;
10148 }
10149 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010150 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10151 {
10152 if (!(supported & JO_EXIT_CB))
10153 break;
10154 opt->jo_set |= JO_EXIT_CB;
10155 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010156 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010157 {
10158 EMSG2(_(e_invarg2), "exit-cb");
10159 return FAIL;
10160 }
10161 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010162 else
10163 break;
10164 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010165 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010166 if (todo > 0)
10167 {
10168 EMSG2(_(e_invarg2), hi->hi_key);
10169 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010170 }
10171
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010172 return OK;
10173}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010174#endif
10175
10176#ifdef FEAT_CHANNEL
10177/*
10178 * Get the channel from the argument.
10179 * Returns NULL if the handle is invalid.
10180 */
10181 static channel_T *
10182get_channel_arg(typval_T *tv)
10183{
10184 channel_T *channel;
10185
10186 if (tv->v_type != VAR_CHANNEL)
10187 {
10188 EMSG2(_(e_invarg2), get_tv_string(tv));
10189 return NULL;
10190 }
10191 channel = tv->vval.v_channel;
10192
10193 if (channel == NULL || !channel_is_open(channel))
10194 {
10195 EMSG(_("E906: not an open channel"));
10196 return NULL;
10197 }
10198 return channel;
10199}
10200
10201/*
10202 * "ch_close()" function
10203 */
10204 static void
10205f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10206{
10207 channel_T *channel = get_channel_arg(&argvars[0]);
10208
10209 if (channel != NULL)
10210 channel_close(channel);
10211}
10212
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010213# ifdef FEAT_JOB
10214/*
10215 * "ch_getjob()" function
10216 */
10217 static void
10218f_ch_getjob(typval_T *argvars, typval_T *rettv)
10219{
10220 channel_T *channel = get_channel_arg(&argvars[0]);
10221
10222 if (channel != NULL)
10223 {
10224 rettv->v_type = VAR_JOB;
10225 rettv->vval.v_job = channel->ch_job;
10226 if (channel->ch_job != NULL)
10227 ++channel->ch_job->jv_refcount;
10228 }
10229}
10230# endif
10231
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010232/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010233 * "ch_log()" function
10234 */
10235 static void
10236f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10237{
10238 char_u *msg = get_tv_string(&argvars[0]);
10239 channel_T *channel = NULL;
10240
10241 if (argvars[1].v_type != VAR_UNKNOWN)
10242 channel = get_channel_arg(&argvars[1]);
10243
10244 ch_log(channel, (char *)msg);
10245}
10246
10247/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010248 * "ch_logfile()" function
10249 */
10250 static void
10251f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10252{
10253 char_u *fname;
10254 char_u *opt = (char_u *)"";
10255 char_u buf[NUMBUFLEN];
10256 FILE *file = NULL;
10257
10258 fname = get_tv_string(&argvars[0]);
10259 if (argvars[1].v_type == VAR_STRING)
10260 opt = get_tv_string_buf(&argvars[1], buf);
10261 if (*fname != NUL)
10262 {
10263 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10264 if (file == NULL)
10265 {
10266 EMSG2(_(e_notopen), fname);
10267 return;
10268 }
10269 }
10270 ch_logfile(file);
10271}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010272
10273/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010274 * "ch_open()" function
10275 */
10276 static void
10277f_ch_open(typval_T *argvars, typval_T *rettv)
10278{
10279 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010280 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010281 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010282 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010283 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010284 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010285
10286 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010287 rettv->v_type = VAR_CHANNEL;
10288 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010289
10290 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010291 if (argvars[1].v_type != VAR_UNKNOWN
10292 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010293 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010294 EMSG(_(e_invarg));
10295 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010296 }
10297
10298 /* parse address */
10299 p = vim_strchr(address, ':');
10300 if (p == NULL)
10301 {
10302 EMSG2(_(e_invarg2), address);
10303 return;
10304 }
10305 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010306 port = strtol((char *)p, &rest, 10);
10307 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010308 {
10309 p[-1] = ':';
10310 EMSG2(_(e_invarg2), address);
10311 return;
10312 }
10313
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010314 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010315 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010316 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010317 opt.jo_timeout = 2000;
10318 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010319 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010320 return;
10321 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010322 {
10323 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010324 return;
10325 }
10326
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010327 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010328 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010329 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010330 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010331 opt.jo_set = JO_ALL;
10332 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010333 }
10334}
10335
10336/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010337 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010338 */
10339 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010340common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010341{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010342 channel_T *channel;
10343 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010344 jobopt_T opt;
10345 int mode;
10346 int timeout;
10347 int id = -1;
10348 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010349
10350 /* return an empty string by default */
10351 rettv->v_type = VAR_STRING;
10352 rettv->vval.v_string = NULL;
10353
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010354 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010355 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10356 == FAIL)
10357 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010358
Bram Moolenaar77073442016-02-13 23:23:53 +010010359 channel = get_channel_arg(&argvars[0]);
10360 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010361 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010362 if (opt.jo_set & JO_PART)
10363 part = opt.jo_part;
10364 else
10365 part = channel_part_read(channel);
10366 mode = channel_get_mode(channel, part);
10367 timeout = channel_get_timeout(channel, part);
10368 if (opt.jo_set & JO_TIMEOUT)
10369 timeout = opt.jo_timeout;
10370
10371 if (raw || mode == MODE_RAW || mode == MODE_NL)
10372 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10373 else
10374 {
10375 if (opt.jo_set & JO_ID)
10376 id = opt.jo_id;
10377 channel_read_json_block(channel, part, timeout, id, &listtv);
10378 if (listtv != NULL)
10379 *rettv = *listtv;
10380 else
10381 {
10382 rettv->v_type = VAR_SPECIAL;
10383 rettv->vval.v_number = VVAL_NONE;
10384 }
10385 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010386 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010387}
10388
10389/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010390 * "ch_read()" function
10391 */
10392 static void
10393f_ch_read(typval_T *argvars, typval_T *rettv)
10394{
10395 common_channel_read(argvars, rettv, FALSE);
10396}
10397
10398/*
10399 * "ch_readraw()" function
10400 */
10401 static void
10402f_ch_readraw(typval_T *argvars, typval_T *rettv)
10403{
10404 common_channel_read(argvars, rettv, TRUE);
10405}
10406
10407/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010408 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010409 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010410 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010411 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010412 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010413 static channel_T *
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010414send_common(typval_T *argvars, char_u *text, int id, char *fun, int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010415{
Bram Moolenaar77073442016-02-13 23:23:53 +010010416 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010417 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010418 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010419
Bram Moolenaar77073442016-02-13 23:23:53 +010010420 channel = get_channel_arg(&argvars[0]);
10421 if (channel == NULL)
10422 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010423 part_send = channel_part_send(channel);
10424 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010425
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010426 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010427 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10428 return NULL;
10429
Bram Moolenaara07fec92016-02-05 21:04:08 +010010430 /* Set the callback. An empty callback means no callback and not reading
10431 * the response. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010432 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010433 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010434
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010435 if (channel_send(channel, part_send, text, fun) == OK
10436 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010437 return channel;
10438 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010439}
10440
10441/*
10442 * "ch_sendexpr()" function
10443 */
10444 static void
10445f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10446{
10447 char_u *text;
10448 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010449 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010450 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010451 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010452 int part_send;
10453 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010454 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010455
10456 /* return an empty string by default */
10457 rettv->v_type = VAR_STRING;
10458 rettv->vval.v_string = NULL;
10459
Bram Moolenaar77073442016-02-13 23:23:53 +010010460 channel = get_channel_arg(&argvars[0]);
10461 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010462 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010463 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010464
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010465 ch_mode = channel_get_mode(channel, part_send);
10466 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010467 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010468 EMSG(_("E912: cannot use ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010469 return;
10470 }
10471
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010472 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010473 text = json_encode_nr_expr(id, &argvars[1],
10474 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010475 if (text == NULL)
10476 return;
10477
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010478 channel = send_common(argvars, text, id, "sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010479 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010480 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010481 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010482 /* TODO: timeout from options */
10483 timeout = channel_get_timeout(channel, part_read);
10484 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10485 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010486 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010487 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010488
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010489 /* Move the item from the list and then change the type to
10490 * avoid the value being freed. */
10491 *rettv = list->lv_last->li_tv;
10492 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010493 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010494 }
10495 }
10496}
10497
10498/*
10499 * "ch_sendraw()" function
10500 */
10501 static void
10502f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10503{
10504 char_u buf[NUMBUFLEN];
10505 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010506 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010507 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010508 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010509
10510 /* return an empty string by default */
10511 rettv->v_type = VAR_STRING;
10512 rettv->vval.v_string = NULL;
10513
10514 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010515 channel = send_common(argvars, text, 0, "sendraw", &part_read);
Bram Moolenaar77073442016-02-13 23:23:53 +010010516 if (channel != NULL)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010517 {
10518 /* TODO: timeout from options */
10519 timeout = channel_get_timeout(channel, part_read);
10520 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10521 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010522}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010523
10524/*
10525 * "ch_setoptions()" function
10526 */
10527 static void
10528f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10529{
10530 channel_T *channel;
10531 jobopt_T opt;
10532
10533 channel = get_channel_arg(&argvars[0]);
10534 if (channel == NULL)
10535 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010536 clear_job_options(&opt);
10537 if (get_job_options(&argvars[1], &opt,
10538 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010539 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010540 channel_set_options(channel, &opt);
10541}
10542
10543/*
10544 * "ch_status()" function
10545 */
10546 static void
10547f_ch_status(typval_T *argvars, typval_T *rettv)
10548{
10549 /* return an empty string by default */
10550 rettv->v_type = VAR_STRING;
10551
10552 if (argvars[0].v_type != VAR_CHANNEL)
10553 {
10554 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10555 rettv->vval.v_string = NULL;
10556 }
10557 else
10558 rettv->vval.v_string = vim_strsave(
10559 (char_u *)channel_status(argvars[0].vval.v_channel));
10560}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010561#endif
10562
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010563/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010564 * "changenr()" function
10565 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010567f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010568{
10569 rettv->vval.v_number = curbuf->b_u_seq_cur;
10570}
10571
10572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 * "char2nr(string)" function
10574 */
10575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010576f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577{
10578#ifdef FEAT_MBYTE
10579 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010580 {
10581 int utf8 = 0;
10582
10583 if (argvars[1].v_type != VAR_UNKNOWN)
10584 utf8 = get_tv_number_chk(&argvars[1], NULL);
10585
10586 if (utf8)
10587 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10588 else
10589 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 else
10592#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594}
10595
10596/*
10597 * "cindent(lnum)" function
10598 */
10599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010600f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601{
10602#ifdef FEAT_CINDENT
10603 pos_T pos;
10604 linenr_T lnum;
10605
10606 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010607 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10609 {
10610 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010611 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 curwin->w_cursor = pos;
10613 }
10614 else
10615#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010617}
10618
10619/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010620 * "clearmatches()" function
10621 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010623f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010624{
10625#ifdef FEAT_SEARCH_EXTRA
10626 clear_matches(curwin);
10627#endif
10628}
10629
10630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 * "col(string)" function
10632 */
10633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010634f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635{
10636 colnr_T col = 0;
10637 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010638 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010640 fp = var2fpos(&argvars[0], FALSE, &fnum);
10641 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 {
10643 if (fp->col == MAXCOL)
10644 {
10645 /* '> can be MAXCOL, get the length of the line then */
10646 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010647 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 else
10649 col = MAXCOL;
10650 }
10651 else
10652 {
10653 col = fp->col + 1;
10654#ifdef FEAT_VIRTUALEDIT
10655 /* col(".") when the cursor is on the NUL at the end of the line
10656 * because of "coladd" can be seen as an extra column. */
10657 if (virtual_active() && fp == &curwin->w_cursor)
10658 {
10659 char_u *p = ml_get_cursor();
10660
10661 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10662 curwin->w_virtcol - curwin->w_cursor.coladd))
10663 {
10664# ifdef FEAT_MBYTE
10665 int l;
10666
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010667 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 col += l;
10669# else
10670 if (*p != NUL && p[1] == NUL)
10671 ++col;
10672# endif
10673 }
10674 }
10675#endif
10676 }
10677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010678 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679}
10680
Bram Moolenaar572cb562005-08-05 21:35:02 +000010681#if defined(FEAT_INS_EXPAND)
10682/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010683 * "complete()" function
10684 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010686f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010687{
10688 int startcol;
10689
10690 if ((State & INSERT) == 0)
10691 {
10692 EMSG(_("E785: complete() can only be used in Insert mode"));
10693 return;
10694 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010695
10696 /* Check for undo allowed here, because if something was already inserted
10697 * the line was already saved for undo and this check isn't done. */
10698 if (!undo_allowed())
10699 return;
10700
Bram Moolenaarade00832006-03-10 21:46:58 +000010701 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10702 {
10703 EMSG(_(e_invarg));
10704 return;
10705 }
10706
10707 startcol = get_tv_number_chk(&argvars[0], NULL);
10708 if (startcol <= 0)
10709 return;
10710
10711 set_completion(startcol - 1, argvars[1].vval.v_list);
10712}
10713
10714/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010715 * "complete_add()" function
10716 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010718f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010719{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010720 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010721}
10722
10723/*
10724 * "complete_check()" function
10725 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010727f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010728{
10729 int saved = RedrawingDisabled;
10730
10731 RedrawingDisabled = 0;
10732 ins_compl_check_keys(0);
10733 rettv->vval.v_number = compl_interrupted;
10734 RedrawingDisabled = saved;
10735}
10736#endif
10737
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738/*
10739 * "confirm(message, buttons[, default [, type]])" function
10740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010742f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743{
10744#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10745 char_u *message;
10746 char_u *buttons = NULL;
10747 char_u buf[NUMBUFLEN];
10748 char_u buf2[NUMBUFLEN];
10749 int def = 1;
10750 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010751 char_u *typestr;
10752 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 message = get_tv_string_chk(&argvars[0]);
10755 if (message == NULL)
10756 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010757 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10760 if (buttons == NULL)
10761 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010762 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010764 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010765 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010767 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10768 if (typestr == NULL)
10769 error = TRUE;
10770 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010772 switch (TOUPPER_ASC(*typestr))
10773 {
10774 case 'E': type = VIM_ERROR; break;
10775 case 'Q': type = VIM_QUESTION; break;
10776 case 'I': type = VIM_INFO; break;
10777 case 'W': type = VIM_WARNING; break;
10778 case 'G': type = VIM_GENERIC; break;
10779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 }
10781 }
10782 }
10783 }
10784
10785 if (buttons == NULL || *buttons == NUL)
10786 buttons = (char_u *)_("&Ok");
10787
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010788 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010789 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010790 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791#endif
10792}
10793
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010794/*
10795 * "copy()" function
10796 */
10797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010798f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010799{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010800 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010801}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010803#ifdef FEAT_FLOAT
10804/*
10805 * "cos()" function
10806 */
10807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010808f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010809{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010810 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010811
10812 rettv->v_type = VAR_FLOAT;
10813 if (get_float_arg(argvars, &f) == OK)
10814 rettv->vval.v_float = cos(f);
10815 else
10816 rettv->vval.v_float = 0.0;
10817}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010818
10819/*
10820 * "cosh()" function
10821 */
10822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010823f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010824{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010825 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010826
10827 rettv->v_type = VAR_FLOAT;
10828 if (get_float_arg(argvars, &f) == OK)
10829 rettv->vval.v_float = cosh(f);
10830 else
10831 rettv->vval.v_float = 0.0;
10832}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010833#endif
10834
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010836 * "count()" function
10837 */
10838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010839f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010840{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010841 long n = 0;
10842 int ic = FALSE;
10843
Bram Moolenaare9a41262005-01-15 22:18:47 +000010844 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010845 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010846 listitem_T *li;
10847 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010848 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010849
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850 if ((l = argvars[0].vval.v_list) != NULL)
10851 {
10852 li = l->lv_first;
10853 if (argvars[2].v_type != VAR_UNKNOWN)
10854 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010855 int error = FALSE;
10856
10857 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858 if (argvars[3].v_type != VAR_UNKNOWN)
10859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010860 idx = get_tv_number_chk(&argvars[3], &error);
10861 if (!error)
10862 {
10863 li = list_find(l, idx);
10864 if (li == NULL)
10865 EMSGN(_(e_listidx), idx);
10866 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010867 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 if (error)
10869 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010870 }
10871
10872 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010873 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010874 ++n;
10875 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010876 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010877 else if (argvars[0].v_type == VAR_DICT)
10878 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010879 int todo;
10880 dict_T *d;
10881 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010882
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010883 if ((d = argvars[0].vval.v_dict) != NULL)
10884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010885 int error = FALSE;
10886
Bram Moolenaare9a41262005-01-15 22:18:47 +000010887 if (argvars[2].v_type != VAR_UNKNOWN)
10888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010889 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010890 if (argvars[3].v_type != VAR_UNKNOWN)
10891 EMSG(_(e_invarg));
10892 }
10893
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010894 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010895 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010896 {
10897 if (!HASHITEM_EMPTY(hi))
10898 {
10899 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010900 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010901 ++n;
10902 }
10903 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010904 }
10905 }
10906 else
10907 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010908 rettv->vval.v_number = n;
10909}
10910
10911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10913 *
10914 * Checks the existence of a cscope connection.
10915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010917f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918{
10919#ifdef FEAT_CSCOPE
10920 int num = 0;
10921 char_u *dbpath = NULL;
10922 char_u *prepend = NULL;
10923 char_u buf[NUMBUFLEN];
10924
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010925 if (argvars[0].v_type != VAR_UNKNOWN
10926 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010928 num = (int)get_tv_number(&argvars[0]);
10929 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010930 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010931 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 }
10933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935#endif
10936}
10937
10938/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010939 * "cursor(lnum, col)" function, or
10940 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010942 * Moves the cursor to the specified line and column.
10943 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010946f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947{
10948 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010949#ifdef FEAT_VIRTUALEDIT
10950 long coladd = 0;
10951#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010952 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010954 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010955 if (argvars[1].v_type == VAR_UNKNOWN)
10956 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010957 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010958 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010959
Bram Moolenaar493c1782014-05-28 14:34:46 +020010960 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010961 {
10962 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010963 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010964 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010965 line = pos.lnum;
10966 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010967#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010968 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010969#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010970 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010971 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010972 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010973 set_curswant = FALSE;
10974 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010975 }
10976 else
10977 {
10978 line = get_tv_lnum(argvars);
10979 col = get_tv_number_chk(&argvars[1], NULL);
10980#ifdef FEAT_VIRTUALEDIT
10981 if (argvars[2].v_type != VAR_UNKNOWN)
10982 coladd = get_tv_number_chk(&argvars[2], NULL);
10983#endif
10984 }
10985 if (line < 0 || col < 0
10986#ifdef FEAT_VIRTUALEDIT
10987 || coladd < 0
10988#endif
10989 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010990 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 if (line > 0)
10992 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 if (col > 0)
10994 curwin->w_cursor.col = col - 1;
10995#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010996 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997#endif
10998
10999 /* Make sure the cursor is in a valid position. */
11000 check_cursor();
11001#ifdef FEAT_MBYTE
11002 /* Correct cursor for multi-byte character. */
11003 if (has_mbyte)
11004 mb_adjust_cursor();
11005#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011006
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011007 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011008 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009}
11010
11011/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011012 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 */
11014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011015f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011017 int noref = 0;
11018
11019 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011021 if (noref < 0 || noref > 1)
11022 EMSG(_(e_invarg));
11023 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011024 {
11025 current_copyID += COPYID_INC;
11026 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028}
11029
11030/*
11031 * "delete()" function
11032 */
11033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011034f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011036 char_u nbuf[NUMBUFLEN];
11037 char_u *name;
11038 char_u *flags;
11039
11040 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011042 return;
11043
11044 name = get_tv_string(&argvars[0]);
11045 if (name == NULL || *name == NUL)
11046 {
11047 EMSG(_(e_invarg));
11048 return;
11049 }
11050
11051 if (argvars[1].v_type != VAR_UNKNOWN)
11052 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011054 flags = (char_u *)"";
11055
11056 if (*flags == NUL)
11057 /* delete a file */
11058 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11059 else if (STRCMP(flags, "d") == 0)
11060 /* delete an empty directory */
11061 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11062 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011063 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011064 rettv->vval.v_number = delete_recursive(name);
11065 else
11066 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067}
11068
11069/*
11070 * "did_filetype()" function
11071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011073f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074{
11075#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011076 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077#endif
11078}
11079
11080/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011081 * "diff_filler()" function
11082 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011084f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011085{
11086#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011087 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011088#endif
11089}
11090
11091/*
11092 * "diff_hlID()" function
11093 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011095f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011096{
11097#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011099 static linenr_T prev_lnum = 0;
11100 static int changedtick = 0;
11101 static int fnum = 0;
11102 static int change_start = 0;
11103 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011104 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011105 int filler_lines;
11106 int col;
11107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011108 if (lnum < 0) /* ignore type error in {lnum} arg */
11109 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011110 if (lnum != prev_lnum
11111 || changedtick != curbuf->b_changedtick
11112 || fnum != curbuf->b_fnum)
11113 {
11114 /* New line, buffer, change: need to get the values. */
11115 filler_lines = diff_check(curwin, lnum);
11116 if (filler_lines < 0)
11117 {
11118 if (filler_lines == -1)
11119 {
11120 change_start = MAXCOL;
11121 change_end = -1;
11122 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11123 hlID = HLF_ADD; /* added line */
11124 else
11125 hlID = HLF_CHD; /* changed line */
11126 }
11127 else
11128 hlID = HLF_ADD; /* added line */
11129 }
11130 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011131 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011132 prev_lnum = lnum;
11133 changedtick = curbuf->b_changedtick;
11134 fnum = curbuf->b_fnum;
11135 }
11136
11137 if (hlID == HLF_CHD || hlID == HLF_TXD)
11138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011140 if (col >= change_start && col <= change_end)
11141 hlID = HLF_TXD; /* changed text */
11142 else
11143 hlID = HLF_CHD; /* changed line */
11144 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011145 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011146#endif
11147}
11148
11149/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011150 * "disable_char_avail_for_testing({expr})" function
11151 */
11152 static void
11153f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11154{
11155 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11156}
11157
11158/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011159 * "empty({expr})" function
11160 */
11161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011162f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011163{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011164 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011165
11166 switch (argvars[0].v_type)
11167 {
11168 case VAR_STRING:
11169 case VAR_FUNC:
11170 n = argvars[0].vval.v_string == NULL
11171 || *argvars[0].vval.v_string == NUL;
11172 break;
11173 case VAR_NUMBER:
11174 n = argvars[0].vval.v_number == 0;
11175 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011176 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011177#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011178 n = argvars[0].vval.v_float == 0.0;
11179 break;
11180#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011181 case VAR_LIST:
11182 n = argvars[0].vval.v_list == NULL
11183 || argvars[0].vval.v_list->lv_first == NULL;
11184 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011185 case VAR_DICT:
11186 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011187 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011188 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011189 case VAR_SPECIAL:
11190 n = argvars[0].vval.v_number != VVAL_TRUE;
11191 break;
11192
Bram Moolenaar835dc632016-02-07 14:27:38 +010011193 case VAR_JOB:
11194#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011195 n = argvars[0].vval.v_job == NULL
11196 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11197 break;
11198#endif
11199 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011200#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011201 n = argvars[0].vval.v_channel == NULL
11202 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011203 break;
11204#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011205 case VAR_UNKNOWN:
11206 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11207 n = TRUE;
11208 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011209 }
11210
11211 rettv->vval.v_number = n;
11212}
11213
11214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 * "escape({string}, {chars})" function
11216 */
11217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011218f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219{
11220 char_u buf[NUMBUFLEN];
11221
Bram Moolenaar758711c2005-02-02 23:11:38 +000011222 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11223 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011224 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225}
11226
11227/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011228 * "eval()" function
11229 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011231f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011232{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011233 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 s = get_tv_string_chk(&argvars[0]);
11236 if (s != NULL)
11237 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011238
Bram Moolenaar615b9972015-01-14 17:15:05 +010011239 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011240 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11241 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011242 if (p != NULL && !aborting())
11243 EMSG2(_(e_invexpr2), p);
11244 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011245 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011246 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011247 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011248 else if (*s != NUL)
11249 EMSG(_(e_trailing));
11250}
11251
11252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 * "eventhandler()" function
11254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011256f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011259}
11260
11261/*
11262 * "executable()" function
11263 */
11264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011265f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011267 char_u *name = get_tv_string(&argvars[0]);
11268
11269 /* Check in $PATH and also check directly if there is a directory name. */
11270 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11271 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011272}
11273
11274/*
11275 * "exepath()" function
11276 */
11277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011278f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011279{
11280 char_u *p = NULL;
11281
Bram Moolenaarb5971142015-03-21 17:32:19 +010011282 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011283 rettv->v_type = VAR_STRING;
11284 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285}
11286
11287/*
11288 * "exists()" function
11289 */
11290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011291f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292{
11293 char_u *p;
11294 char_u *name;
11295 int n = FALSE;
11296 int len = 0;
11297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 if (*p == '$') /* environment variable */
11300 {
11301 /* first try "normal" environment variables (fast) */
11302 if (mch_getenv(p + 1) != NULL)
11303 n = TRUE;
11304 else
11305 {
11306 /* try expanding things like $VIM and ${HOME} */
11307 p = expand_env_save(p);
11308 if (p != NULL && *p != '$')
11309 n = TRUE;
11310 vim_free(p);
11311 }
11312 }
11313 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011316 if (*skipwhite(p) != NUL)
11317 n = FALSE; /* trailing garbage */
11318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 else if (*p == '*') /* internal or user defined function */
11320 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011321 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322 }
11323 else if (*p == ':')
11324 {
11325 n = cmd_exists(p + 1);
11326 }
11327 else if (*p == '#')
11328 {
11329#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011330 if (p[1] == '#')
11331 n = autocmd_supported(p + 2);
11332 else
11333 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334#endif
11335 }
11336 else /* internal variable */
11337 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011338 char_u *tofree;
11339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011341 /* get_name_len() takes care of expanding curly braces */
11342 name = p;
11343 len = get_name_len(&p, &tofree, TRUE, FALSE);
11344 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011346 if (tofree != NULL)
11347 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011348 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011349 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011351 /* handle d.key, l[idx], f(expr) */
11352 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11353 if (n)
11354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355 }
11356 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011357 if (*p != NUL)
11358 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011360 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 }
11362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364}
11365
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011366#ifdef FEAT_FLOAT
11367/*
11368 * "exp()" function
11369 */
11370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011371f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011372{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011373 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011374
11375 rettv->v_type = VAR_FLOAT;
11376 if (get_float_arg(argvars, &f) == OK)
11377 rettv->vval.v_float = exp(f);
11378 else
11379 rettv->vval.v_float = 0.0;
11380}
11381#endif
11382
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383/*
11384 * "expand()" function
11385 */
11386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011387f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388{
11389 char_u *s;
11390 int len;
11391 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011392 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011394 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011395 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011398 if (argvars[1].v_type != VAR_UNKNOWN
11399 && argvars[2].v_type != VAR_UNKNOWN
11400 && get_tv_number_chk(&argvars[2], &error)
11401 && !error)
11402 {
11403 rettv->v_type = VAR_LIST;
11404 rettv->vval.v_list = NULL;
11405 }
11406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 if (*s == '%' || *s == '#' || *s == '<')
11409 {
11410 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011411 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011413 if (rettv->v_type == VAR_LIST)
11414 {
11415 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11416 list_append_string(rettv->vval.v_list, result, -1);
11417 else
11418 vim_free(result);
11419 }
11420 else
11421 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 }
11423 else
11424 {
11425 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011426 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011427 if (argvars[1].v_type != VAR_UNKNOWN
11428 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011429 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 if (!error)
11431 {
11432 ExpandInit(&xpc);
11433 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011434 if (p_wic)
11435 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011436 if (rettv->v_type == VAR_STRING)
11437 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11438 options, WILD_ALL);
11439 else if (rettv_list_alloc(rettv) != FAIL)
11440 {
11441 int i;
11442
11443 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11444 for (i = 0; i < xpc.xp_numfiles; i++)
11445 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11446 ExpandCleanup(&xpc);
11447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 }
11449 else
11450 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 }
11452}
11453
11454/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011455 * Go over all entries in "d2" and add them to "d1".
11456 * When "action" is "error" then a duplicate key is an error.
11457 * When "action" is "force" then a duplicate key is overwritten.
11458 * Otherwise duplicate keys are ignored ("action" is "keep").
11459 */
11460 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011461dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011462{
11463 dictitem_T *di1;
11464 hashitem_T *hi2;
11465 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011466 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011467
11468 todo = (int)d2->dv_hashtab.ht_used;
11469 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11470 {
11471 if (!HASHITEM_EMPTY(hi2))
11472 {
11473 --todo;
11474 di1 = dict_find(d1, hi2->hi_key, -1);
11475 if (d1->dv_scope != 0)
11476 {
11477 /* Disallow replacing a builtin function in l: and g:.
11478 * Check the key to be valid when adding to any
11479 * scope. */
11480 if (d1->dv_scope == VAR_DEF_SCOPE
11481 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11482 && var_check_func_name(hi2->hi_key,
11483 di1 == NULL))
11484 break;
11485 if (!valid_varname(hi2->hi_key))
11486 break;
11487 }
11488 if (di1 == NULL)
11489 {
11490 di1 = dictitem_copy(HI2DI(hi2));
11491 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11492 dictitem_free(di1);
11493 }
11494 else if (*action == 'e')
11495 {
11496 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11497 break;
11498 }
11499 else if (*action == 'f' && HI2DI(hi2) != di1)
11500 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011501 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11502 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011503 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011504 clear_tv(&di1->di_tv);
11505 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11506 }
11507 }
11508 }
11509}
11510
11511/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011512 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011514 */
11515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011516f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011517{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011518 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011519
Bram Moolenaare9a41262005-01-15 22:18:47 +000011520 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011521 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011522 list_T *l1, *l2;
11523 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011524 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011525 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011526
Bram Moolenaare9a41262005-01-15 22:18:47 +000011527 l1 = argvars[0].vval.v_list;
11528 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011529 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011530 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011531 {
11532 if (argvars[2].v_type != VAR_UNKNOWN)
11533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011534 before = get_tv_number_chk(&argvars[2], &error);
11535 if (error)
11536 return; /* type error; errmsg already given */
11537
Bram Moolenaar758711c2005-02-02 23:11:38 +000011538 if (before == l1->lv_len)
11539 item = NULL;
11540 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011541 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011542 item = list_find(l1, before);
11543 if (item == NULL)
11544 {
11545 EMSGN(_(e_listidx), before);
11546 return;
11547 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548 }
11549 }
11550 else
11551 item = NULL;
11552 list_extend(l1, l2, item);
11553
Bram Moolenaare9a41262005-01-15 22:18:47 +000011554 copy_tv(&argvars[0], rettv);
11555 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011557 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11558 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011559 dict_T *d1, *d2;
11560 char_u *action;
11561 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011562
11563 d1 = argvars[0].vval.v_dict;
11564 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011565 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011566 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011567 {
11568 /* Check the third argument. */
11569 if (argvars[2].v_type != VAR_UNKNOWN)
11570 {
11571 static char *(av[]) = {"keep", "force", "error"};
11572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 action = get_tv_string_chk(&argvars[2]);
11574 if (action == NULL)
11575 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011576 for (i = 0; i < 3; ++i)
11577 if (STRCMP(action, av[i]) == 0)
11578 break;
11579 if (i == 3)
11580 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011581 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011582 return;
11583 }
11584 }
11585 else
11586 action = (char_u *)"force";
11587
Bram Moolenaara9922d62013-05-30 13:01:18 +020011588 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011589
Bram Moolenaare9a41262005-01-15 22:18:47 +000011590 copy_tv(&argvars[0], rettv);
11591 }
11592 }
11593 else
11594 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011595}
11596
11597/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011598 * "feedkeys()" function
11599 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011601f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011602{
11603 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011604 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011605 char_u *keys, *flags;
11606 char_u nbuf[NUMBUFLEN];
11607 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011608 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011609 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011610
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011611 /* This is not allowed in the sandbox. If the commands would still be
11612 * executed in the sandbox it would be OK, but it probably happens later,
11613 * when "sandbox" is no longer set. */
11614 if (check_secure())
11615 return;
11616
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011617 keys = get_tv_string(&argvars[0]);
11618 if (*keys != NUL)
11619 {
11620 if (argvars[1].v_type != VAR_UNKNOWN)
11621 {
11622 flags = get_tv_string_buf(&argvars[1], nbuf);
11623 for ( ; *flags != NUL; ++flags)
11624 {
11625 switch (*flags)
11626 {
11627 case 'n': remap = FALSE; break;
11628 case 'm': remap = TRUE; break;
11629 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011630 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011631 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011632 }
11633 }
11634 }
11635
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011636 /* Need to escape K_SPECIAL and CSI before putting the string in the
11637 * typeahead buffer. */
11638 keys_esc = vim_strsave_escape_csi(keys);
11639 if (keys_esc != NULL)
11640 {
11641 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011642 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011643 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011644 if (vgetc_busy)
11645 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011646 if (execute)
11647 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011648 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011649 }
11650}
11651
11652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 * "filereadable()" function
11654 */
11655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011656f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011658 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 char_u *p;
11660 int n;
11661
Bram Moolenaarc236c162008-07-13 17:41:49 +000011662#ifndef O_NONBLOCK
11663# define O_NONBLOCK 0
11664#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011666 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11667 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668 {
11669 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011670 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 }
11672 else
11673 n = FALSE;
11674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011675 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676}
11677
11678/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011679 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 * rights to write into.
11681 */
11682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011683f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011685 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686}
11687
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011688 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011689findfilendir(
11690 typval_T *argvars UNUSED,
11691 typval_T *rettv,
11692 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011693{
11694#ifdef FEAT_SEARCHPATH
11695 char_u *fname;
11696 char_u *fresult = NULL;
11697 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11698 char_u *p;
11699 char_u pathbuf[NUMBUFLEN];
11700 int count = 1;
11701 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011702 int error = FALSE;
11703#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011704
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011705 rettv->vval.v_string = NULL;
11706 rettv->v_type = VAR_STRING;
11707
11708#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011710
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011711 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011713 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11714 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011715 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716 else
11717 {
11718 if (*p != NUL)
11719 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011722 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011723 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011724 }
11725
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011726 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11727 error = TRUE;
11728
11729 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011730 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011731 do
11732 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011733 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011734 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011735 fresult = find_file_in_path_option(first ? fname : NULL,
11736 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011737 0, first, path,
11738 find_what,
11739 curbuf->b_ffname,
11740 find_what == FINDFILE_DIR
11741 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011742 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011743
11744 if (fresult != NULL && rettv->v_type == VAR_LIST)
11745 list_append_string(rettv->vval.v_list, fresult, -1);
11746
11747 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011748 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011749
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011750 if (rettv->v_type == VAR_STRING)
11751 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011752#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011753}
11754
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011755static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11756static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011757
11758/*
11759 * Implementation of map() and filter().
11760 */
11761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011762filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011763{
11764 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011765 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011766 listitem_T *li, *nli;
11767 list_T *l = NULL;
11768 dictitem_T *di;
11769 hashtab_T *ht;
11770 hashitem_T *hi;
11771 dict_T *d = NULL;
11772 typval_T save_val;
11773 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011774 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011775 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011776 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011777 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011778 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011779 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011780 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011781
Bram Moolenaare9a41262005-01-15 22:18:47 +000011782 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011783 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011784 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011785 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011786 return;
11787 }
11788 else if (argvars[0].v_type == VAR_DICT)
11789 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011790 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011791 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011792 return;
11793 }
11794 else
11795 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011796 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011797 return;
11798 }
11799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011800 expr = get_tv_string_buf_chk(&argvars[1], buf);
11801 /* On type errors, the preceding call has already displayed an error
11802 * message. Avoid a misleading error message for an empty string that
11803 * was not passed as argument. */
11804 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011806 prepare_vimvar(VV_VAL, &save_val);
11807 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011808
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011809 /* We reset "did_emsg" to be able to detect whether an error
11810 * occurred during evaluation of the expression. */
11811 save_did_emsg = did_emsg;
11812 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011813
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011814 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011817 vimvars[VV_KEY].vv_type = VAR_STRING;
11818
11819 ht = &d->dv_hashtab;
11820 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011821 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824 if (!HASHITEM_EMPTY(hi))
11825 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011826 int r;
11827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 --todo;
11829 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011830 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011831 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11832 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011833 break;
11834 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011835 r = filter_map_one(&di->di_tv, expr, map, &rem);
11836 clear_tv(&vimvars[VV_KEY].vv_tv);
11837 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011838 break;
11839 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011840 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011841 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11842 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011843 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011844 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011845 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011846 }
11847 }
11848 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011849 }
11850 else
11851 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011852 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011854 for (li = l->lv_first; li != NULL; li = nli)
11855 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011856 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011857 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011858 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011859 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011860 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011861 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011862 break;
11863 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011864 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011865 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011866 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011867 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011868
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011869 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011870 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011871
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011872 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011873 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011874
11875 copy_tv(&argvars[0], rettv);
11876}
11877
11878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011879filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011880{
Bram Moolenaar33570922005-01-25 22:26:29 +000011881 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011882 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011883 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011884
Bram Moolenaar33570922005-01-25 22:26:29 +000011885 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011886 s = expr;
11887 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011888 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011889 if (*s != NUL) /* check for trailing chars after expr */
11890 {
11891 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011892 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011893 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011894 }
11895 if (map)
11896 {
11897 /* map(): replace the list item value */
11898 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011899 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011900 *tv = rettv;
11901 }
11902 else
11903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011904 int error = FALSE;
11905
Bram Moolenaare9a41262005-01-15 22:18:47 +000011906 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011908 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011909 /* On type error, nothing has been removed; return FAIL to stop the
11910 * loop. The error message was given by get_tv_number_chk(). */
11911 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011912 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011913 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011914 retval = OK;
11915theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011916 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011917 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011918}
11919
11920/*
11921 * "filter()" function
11922 */
11923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011924f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011925{
11926 filter_map(argvars, rettv, FALSE);
11927}
11928
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011929/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011930 * "finddir({fname}[, {path}[, {count}]])" function
11931 */
11932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011933f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011934{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011935 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011936}
11937
11938/*
11939 * "findfile({fname}[, {path}[, {count}]])" function
11940 */
11941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011942f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011943{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011944 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011945}
11946
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011947#ifdef FEAT_FLOAT
11948/*
11949 * "float2nr({float})" function
11950 */
11951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011952f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011953{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011954 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011955
11956 if (get_float_arg(argvars, &f) == OK)
11957 {
11958 if (f < -0x7fffffff)
11959 rettv->vval.v_number = -0x7fffffff;
11960 else if (f > 0x7fffffff)
11961 rettv->vval.v_number = 0x7fffffff;
11962 else
11963 rettv->vval.v_number = (varnumber_T)f;
11964 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011965}
11966
11967/*
11968 * "floor({float})" function
11969 */
11970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011971f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011972{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011973 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011974
11975 rettv->v_type = VAR_FLOAT;
11976 if (get_float_arg(argvars, &f) == OK)
11977 rettv->vval.v_float = floor(f);
11978 else
11979 rettv->vval.v_float = 0.0;
11980}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011981
11982/*
11983 * "fmod()" function
11984 */
11985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011986f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011987{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011988 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011989
11990 rettv->v_type = VAR_FLOAT;
11991 if (get_float_arg(argvars, &fx) == OK
11992 && get_float_arg(&argvars[1], &fy) == OK)
11993 rettv->vval.v_float = fmod(fx, fy);
11994 else
11995 rettv->vval.v_float = 0.0;
11996}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011997#endif
11998
Bram Moolenaar0d660222005-01-07 21:51:51 +000011999/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012000 * "fnameescape({string})" function
12001 */
12002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012003f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012004{
12005 rettv->vval.v_string = vim_strsave_fnameescape(
12006 get_tv_string(&argvars[0]), FALSE);
12007 rettv->v_type = VAR_STRING;
12008}
12009
12010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011 * "fnamemodify({fname}, {mods})" function
12012 */
12013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012014f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012015{
12016 char_u *fname;
12017 char_u *mods;
12018 int usedlen = 0;
12019 int len;
12020 char_u *fbuf = NULL;
12021 char_u buf[NUMBUFLEN];
12022
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012023 fname = get_tv_string_chk(&argvars[0]);
12024 mods = get_tv_string_buf_chk(&argvars[1], buf);
12025 if (fname == NULL || mods == NULL)
12026 fname = NULL;
12027 else
12028 {
12029 len = (int)STRLEN(fname);
12030 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038 vim_free(fbuf);
12039}
12040
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012041static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042
12043/*
12044 * "foldclosed()" function
12045 */
12046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012047foldclosed_both(
12048 typval_T *argvars UNUSED,
12049 typval_T *rettv,
12050 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
12052#ifdef FEAT_FOLDING
12053 linenr_T lnum;
12054 linenr_T first, last;
12055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12058 {
12059 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12060 {
12061 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012062 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012064 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065 return;
12066 }
12067 }
12068#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012069 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070}
12071
12072/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073 * "foldclosed()" function
12074 */
12075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012076f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012077{
12078 foldclosed_both(argvars, rettv, FALSE);
12079}
12080
12081/*
12082 * "foldclosedend()" function
12083 */
12084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012085f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012086{
12087 foldclosed_both(argvars, rettv, TRUE);
12088}
12089
12090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 * "foldlevel()" function
12092 */
12093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012094f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095{
12096#ifdef FEAT_FOLDING
12097 linenr_T lnum;
12098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103}
12104
12105/*
12106 * "foldtext()" function
12107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012109f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110{
12111#ifdef FEAT_FOLDING
12112 linenr_T lnum;
12113 char_u *s;
12114 char_u *r;
12115 int len;
12116 char *txt;
12117#endif
12118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119 rettv->v_type = VAR_STRING;
12120 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012122 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12123 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12124 <= curbuf->b_ml.ml_line_count
12125 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 {
12127 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012128 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12129 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 {
12131 if (!linewhite(lnum))
12132 break;
12133 ++lnum;
12134 }
12135
12136 /* Find interesting text in this line. */
12137 s = skipwhite(ml_get(lnum));
12138 /* skip C comment-start */
12139 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012142 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012143 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012144 {
12145 s = skipwhite(ml_get(lnum + 1));
12146 if (*s == '*')
12147 s = skipwhite(s + 1);
12148 }
12149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 txt = _("+-%s%3ld lines: ");
12151 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012152 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 + 20 /* for %3ld */
12154 + STRLEN(s))); /* concatenated */
12155 if (r != NULL)
12156 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012157 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12158 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12159 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 len = (int)STRLEN(r);
12161 STRCAT(r, s);
12162 /* remove 'foldmarker' and 'commentstring' */
12163 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012164 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 }
12166 }
12167#endif
12168}
12169
12170/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012171 * "foldtextresult(lnum)" function
12172 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012174f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012175{
12176#ifdef FEAT_FOLDING
12177 linenr_T lnum;
12178 char_u *text;
12179 char_u buf[51];
12180 foldinfo_T foldinfo;
12181 int fold_count;
12182#endif
12183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 rettv->v_type = VAR_STRING;
12185 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012186#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012187 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012188 /* treat illegal types and illegal string values for {lnum} the same */
12189 if (lnum < 0)
12190 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012191 fold_count = foldedCount(curwin, lnum, &foldinfo);
12192 if (fold_count > 0)
12193 {
12194 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12195 &foldinfo, buf);
12196 if (text == buf)
12197 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012198 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012199 }
12200#endif
12201}
12202
12203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 * "foreground()" function
12205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012207f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209#ifdef FEAT_GUI
12210 if (gui.in_use)
12211 gui_mch_set_foreground();
12212#else
12213# ifdef WIN32
12214 win32_set_foreground();
12215# endif
12216#endif
12217}
12218
12219/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012220 * "function()" function
12221 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012223f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012224{
12225 char_u *s;
12226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012228 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012229 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012230 /* Don't check an autoload name for existence here. */
12231 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012232 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012233 else
12234 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012235 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012236 {
12237 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012238 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012239
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012240 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12241 * also be called from another script. Using trans_function_name()
12242 * would also work, but some plugins depend on the name being
12243 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012244 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012245 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012246 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012247 if (rettv->vval.v_string != NULL)
12248 {
12249 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012250 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012251 }
12252 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012253 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012254 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012256 }
12257}
12258
12259/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012260 * "garbagecollect()" function
12261 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012263f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012264{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012265 /* This is postponed until we are back at the toplevel, because we may be
12266 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12267 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012268
12269 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12270 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012271}
12272
12273/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012274 * "get()" function
12275 */
12276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012277f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012278{
Bram Moolenaar33570922005-01-25 22:26:29 +000012279 listitem_T *li;
12280 list_T *l;
12281 dictitem_T *di;
12282 dict_T *d;
12283 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012284
Bram Moolenaare9a41262005-01-15 22:18:47 +000012285 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012286 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012287 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012289 int error = FALSE;
12290
12291 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12292 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012293 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012294 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012295 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012296 else if (argvars[0].v_type == VAR_DICT)
12297 {
12298 if ((d = argvars[0].vval.v_dict) != NULL)
12299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012300 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012301 if (di != NULL)
12302 tv = &di->di_tv;
12303 }
12304 }
12305 else
12306 EMSG2(_(e_listdictarg), "get()");
12307
12308 if (tv == NULL)
12309 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012310 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012311 copy_tv(&argvars[2], rettv);
12312 }
12313 else
12314 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012315}
12316
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012317static 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 +000012318
12319/*
12320 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012321 * Return a range (from start to end) of lines in rettv from the specified
12322 * buffer.
12323 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012324 */
12325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012326get_buffer_lines(
12327 buf_T *buf,
12328 linenr_T start,
12329 linenr_T end,
12330 int retlist,
12331 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012332{
12333 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012334
Bram Moolenaar959a1432013-12-14 12:17:38 +010012335 rettv->v_type = VAR_STRING;
12336 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012337 if (retlist && rettv_list_alloc(rettv) == FAIL)
12338 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012339
12340 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12341 return;
12342
12343 if (!retlist)
12344 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012345 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12346 p = ml_get_buf(buf, start, FALSE);
12347 else
12348 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012349 rettv->vval.v_string = vim_strsave(p);
12350 }
12351 else
12352 {
12353 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012354 return;
12355
12356 if (start < 1)
12357 start = 1;
12358 if (end > buf->b_ml.ml_line_count)
12359 end = buf->b_ml.ml_line_count;
12360 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012361 if (list_append_string(rettv->vval.v_list,
12362 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012363 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012364 }
12365}
12366
12367/*
12368 * "getbufline()" function
12369 */
12370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012371f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012372{
12373 linenr_T lnum;
12374 linenr_T end;
12375 buf_T *buf;
12376
12377 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12378 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012379 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012380 --emsg_off;
12381
Bram Moolenaar661b1822005-07-28 22:36:45 +000012382 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012383 if (argvars[2].v_type == VAR_UNKNOWN)
12384 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012385 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012386 end = get_tv_lnum_buf(&argvars[2], buf);
12387
Bram Moolenaar342337a2005-07-21 21:11:17 +000012388 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012389}
12390
Bram Moolenaar0d660222005-01-07 21:51:51 +000012391/*
12392 * "getbufvar()" function
12393 */
12394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012395f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012396{
12397 buf_T *buf;
12398 buf_T *save_curbuf;
12399 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012400 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012401 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012402
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012403 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12404 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012405 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012406 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012407
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012408 rettv->v_type = VAR_STRING;
12409 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012410
12411 if (buf != NULL && varname != NULL)
12412 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012413 /* set curbuf to be our buf, temporarily */
12414 save_curbuf = curbuf;
12415 curbuf = buf;
12416
Bram Moolenaar0d660222005-01-07 21:51:51 +000012417 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012418 {
12419 if (get_option_tv(&varname, rettv, TRUE) == OK)
12420 done = TRUE;
12421 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012422 else if (STRCMP(varname, "changedtick") == 0)
12423 {
12424 rettv->v_type = VAR_NUMBER;
12425 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012426 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012427 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012428 else
12429 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012430 /* Look up the variable. */
12431 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12432 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12433 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012434 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012435 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012436 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012437 done = TRUE;
12438 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012439 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012440
12441 /* restore previous notion of curbuf */
12442 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012443 }
12444
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012445 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12446 /* use the default value */
12447 copy_tv(&argvars[2], rettv);
12448
Bram Moolenaar0d660222005-01-07 21:51:51 +000012449 --emsg_off;
12450}
12451
12452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 * "getchar()" function
12454 */
12455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012456f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457{
12458 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012459 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012461 /* Position the cursor. Needed after a message that ends in a space. */
12462 windgoto(msg_row, msg_col);
12463
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464 ++no_mapping;
12465 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012466 for (;;)
12467 {
12468 if (argvars[0].v_type == VAR_UNKNOWN)
12469 /* getchar(): blocking wait. */
12470 n = safe_vgetc();
12471 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12472 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012473 n = vpeekc_any();
12474 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012475 /* illegal argument or getchar(0) and no char avail: return zero */
12476 n = 0;
12477 else
12478 /* getchar(0) and char avail: return char */
12479 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012480
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012481 if (n == K_IGNORE)
12482 continue;
12483 break;
12484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485 --no_mapping;
12486 --allow_keys;
12487
Bram Moolenaar219b8702006-11-01 14:32:36 +000012488 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12489 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12490 vimvars[VV_MOUSE_COL].vv_nr = 0;
12491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012492 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493 if (IS_SPECIAL(n) || mod_mask != 0)
12494 {
12495 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12496 int i = 0;
12497
12498 /* Turn a special key into three bytes, plus modifier. */
12499 if (mod_mask != 0)
12500 {
12501 temp[i++] = K_SPECIAL;
12502 temp[i++] = KS_MODIFIER;
12503 temp[i++] = mod_mask;
12504 }
12505 if (IS_SPECIAL(n))
12506 {
12507 temp[i++] = K_SPECIAL;
12508 temp[i++] = K_SECOND(n);
12509 temp[i++] = K_THIRD(n);
12510 }
12511#ifdef FEAT_MBYTE
12512 else if (has_mbyte)
12513 i += (*mb_char2bytes)(n, temp + i);
12514#endif
12515 else
12516 temp[i++] = n;
12517 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012518 rettv->v_type = VAR_STRING;
12519 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012520
12521#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012522 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012523 {
12524 int row = mouse_row;
12525 int col = mouse_col;
12526 win_T *win;
12527 linenr_T lnum;
12528# ifdef FEAT_WINDOWS
12529 win_T *wp;
12530# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012531 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012532
12533 if (row >= 0 && col >= 0)
12534 {
12535 /* Find the window at the mouse coordinates and compute the
12536 * text position. */
12537 win = mouse_find_win(&row, &col);
12538 (void)mouse_comp_pos(win, &row, &col, &lnum);
12539# ifdef FEAT_WINDOWS
12540 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012541 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012542# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012543 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012544 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12545 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12546 }
12547 }
12548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549 }
12550}
12551
12552/*
12553 * "getcharmod()" function
12554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012556f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559}
12560
12561/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012562 * "getcharsearch()" function
12563 */
12564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012565f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012566{
12567 if (rettv_dict_alloc(rettv) != FAIL)
12568 {
12569 dict_T *dict = rettv->vval.v_dict;
12570
12571 dict_add_nr_str(dict, "char", 0L, last_csearch());
12572 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12573 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12574 }
12575}
12576
12577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578 * "getcmdline()" function
12579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012581f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012583 rettv->v_type = VAR_STRING;
12584 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585}
12586
12587/*
12588 * "getcmdpos()" function
12589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012591f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012593 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594}
12595
12596/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012597 * "getcmdtype()" function
12598 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012600f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012601{
12602 rettv->v_type = VAR_STRING;
12603 rettv->vval.v_string = alloc(2);
12604 if (rettv->vval.v_string != NULL)
12605 {
12606 rettv->vval.v_string[0] = get_cmdline_type();
12607 rettv->vval.v_string[1] = NUL;
12608 }
12609}
12610
12611/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012612 * "getcmdwintype()" function
12613 */
12614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012615f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012616{
12617 rettv->v_type = VAR_STRING;
12618 rettv->vval.v_string = NULL;
12619#ifdef FEAT_CMDWIN
12620 rettv->vval.v_string = alloc(2);
12621 if (rettv->vval.v_string != NULL)
12622 {
12623 rettv->vval.v_string[0] = cmdwin_type;
12624 rettv->vval.v_string[1] = NUL;
12625 }
12626#endif
12627}
12628
12629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630 * "getcwd()" function
12631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012633f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012635 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012636 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012639 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012640
12641 wp = find_tabwin(&argvars[0], &argvars[1]);
12642 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012644 if (wp->w_localdir != NULL)
12645 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12646 else if(globaldir != NULL)
12647 rettv->vval.v_string = vim_strsave(globaldir);
12648 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012649 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012650 cwd = alloc(MAXPATHL);
12651 if (cwd != NULL)
12652 {
12653 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12654 rettv->vval.v_string = vim_strsave(cwd);
12655 vim_free(cwd);
12656 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012657 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012658#ifdef BACKSLASH_IN_FILENAME
12659 if (rettv->vval.v_string != NULL)
12660 slash_adjust(rettv->vval.v_string);
12661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 }
12663}
12664
12665/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012666 * "getfontname()" function
12667 */
12668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012669f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012670{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012671 rettv->v_type = VAR_STRING;
12672 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012673#ifdef FEAT_GUI
12674 if (gui.in_use)
12675 {
12676 GuiFont font;
12677 char_u *name = NULL;
12678
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012679 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012680 {
12681 /* Get the "Normal" font. Either the name saved by
12682 * hl_set_font_name() or from the font ID. */
12683 font = gui.norm_font;
12684 name = hl_get_font_name();
12685 }
12686 else
12687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012689 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12690 return;
12691 font = gui_mch_get_font(name, FALSE);
12692 if (font == NOFONT)
12693 return; /* Invalid font name, return empty string. */
12694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012696 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012697 gui_mch_free_font(font);
12698 }
12699#endif
12700}
12701
12702/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012703 * "getfperm({fname})" function
12704 */
12705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012706f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012707{
12708 char_u *fname;
12709 struct stat st;
12710 char_u *perm = NULL;
12711 char_u flags[] = "rwx";
12712 int i;
12713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012715
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012716 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012717 if (mch_stat((char *)fname, &st) >= 0)
12718 {
12719 perm = vim_strsave((char_u *)"---------");
12720 if (perm != NULL)
12721 {
12722 for (i = 0; i < 9; i++)
12723 {
12724 if (st.st_mode & (1 << (8 - i)))
12725 perm[i] = flags[i % 3];
12726 }
12727 }
12728 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012729 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012730}
12731
12732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 * "getfsize({fname})" function
12734 */
12735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012736f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737{
12738 char_u *fname;
12739 struct stat st;
12740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744
12745 if (mch_stat((char *)fname, &st) >= 0)
12746 {
12747 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012751 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012752
12753 /* non-perfect check for overflow */
12754 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12755 rettv->vval.v_number = -2;
12756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 }
12758 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760}
12761
12762/*
12763 * "getftime({fname})" function
12764 */
12765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012766f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767{
12768 char_u *fname;
12769 struct stat st;
12770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772
12773 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777}
12778
12779/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012780 * "getftype({fname})" function
12781 */
12782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012783f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012784{
12785 char_u *fname;
12786 struct stat st;
12787 char_u *type = NULL;
12788 char *t;
12789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012793 if (mch_lstat((char *)fname, &st) >= 0)
12794 {
12795#ifdef S_ISREG
12796 if (S_ISREG(st.st_mode))
12797 t = "file";
12798 else if (S_ISDIR(st.st_mode))
12799 t = "dir";
12800# ifdef S_ISLNK
12801 else if (S_ISLNK(st.st_mode))
12802 t = "link";
12803# endif
12804# ifdef S_ISBLK
12805 else if (S_ISBLK(st.st_mode))
12806 t = "bdev";
12807# endif
12808# ifdef S_ISCHR
12809 else if (S_ISCHR(st.st_mode))
12810 t = "cdev";
12811# endif
12812# ifdef S_ISFIFO
12813 else if (S_ISFIFO(st.st_mode))
12814 t = "fifo";
12815# endif
12816# ifdef S_ISSOCK
12817 else if (S_ISSOCK(st.st_mode))
12818 t = "fifo";
12819# endif
12820 else
12821 t = "other";
12822#else
12823# ifdef S_IFMT
12824 switch (st.st_mode & S_IFMT)
12825 {
12826 case S_IFREG: t = "file"; break;
12827 case S_IFDIR: t = "dir"; break;
12828# ifdef S_IFLNK
12829 case S_IFLNK: t = "link"; break;
12830# endif
12831# ifdef S_IFBLK
12832 case S_IFBLK: t = "bdev"; break;
12833# endif
12834# ifdef S_IFCHR
12835 case S_IFCHR: t = "cdev"; break;
12836# endif
12837# ifdef S_IFIFO
12838 case S_IFIFO: t = "fifo"; break;
12839# endif
12840# ifdef S_IFSOCK
12841 case S_IFSOCK: t = "socket"; break;
12842# endif
12843 default: t = "other";
12844 }
12845# else
12846 if (mch_isdir(fname))
12847 t = "dir";
12848 else
12849 t = "file";
12850# endif
12851#endif
12852 type = vim_strsave((char_u *)t);
12853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012855}
12856
12857/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012858 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012859 */
12860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012861f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012862{
12863 linenr_T lnum;
12864 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012865 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012866
12867 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012868 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012869 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012870 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012871 retlist = FALSE;
12872 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012873 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012874 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012875 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012876 retlist = TRUE;
12877 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012878
Bram Moolenaar342337a2005-07-21 21:11:17 +000012879 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012880}
12881
12882/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012883 * "getmatches()" function
12884 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012886f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012887{
12888#ifdef FEAT_SEARCH_EXTRA
12889 dict_T *dict;
12890 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012891 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012892
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012893 if (rettv_list_alloc(rettv) == OK)
12894 {
12895 while (cur != NULL)
12896 {
12897 dict = dict_alloc();
12898 if (dict == NULL)
12899 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012900 if (cur->match.regprog == NULL)
12901 {
12902 /* match added with matchaddpos() */
12903 for (i = 0; i < MAXPOSMATCH; ++i)
12904 {
12905 llpos_T *llpos;
12906 char buf[6];
12907 list_T *l;
12908
12909 llpos = &cur->pos.pos[i];
12910 if (llpos->lnum == 0)
12911 break;
12912 l = list_alloc();
12913 if (l == NULL)
12914 break;
12915 list_append_number(l, (varnumber_T)llpos->lnum);
12916 if (llpos->col > 0)
12917 {
12918 list_append_number(l, (varnumber_T)llpos->col);
12919 list_append_number(l, (varnumber_T)llpos->len);
12920 }
12921 sprintf(buf, "pos%d", i + 1);
12922 dict_add_list(dict, buf, l);
12923 }
12924 }
12925 else
12926 {
12927 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12928 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012929 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012930 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12931 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012932# ifdef FEAT_CONCEAL
12933 if (cur->conceal_char)
12934 {
12935 char_u buf[MB_MAXBYTES + 1];
12936
12937 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12938 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12939 }
12940# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012941 list_append_dict(rettv->vval.v_list, dict);
12942 cur = cur->next;
12943 }
12944 }
12945#endif
12946}
12947
12948/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012949 * "getpid()" function
12950 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012952f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012953{
12954 rettv->vval.v_number = mch_get_pid();
12955}
12956
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012957static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012958
12959/*
12960 * "getcurpos()" function
12961 */
12962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012963f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012964{
12965 getpos_both(argvars, rettv, TRUE);
12966}
12967
Bram Moolenaar18081e32008-02-20 19:11:07 +000012968/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012969 * "getpos(string)" function
12970 */
12971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012972f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012973{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012974 getpos_both(argvars, rettv, FALSE);
12975}
12976
12977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012978getpos_both(
12979 typval_T *argvars,
12980 typval_T *rettv,
12981 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012982{
Bram Moolenaara5525202006-03-02 22:52:09 +000012983 pos_T *fp;
12984 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012985 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012986
12987 if (rettv_list_alloc(rettv) == OK)
12988 {
12989 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012990 if (getcurpos)
12991 fp = &curwin->w_cursor;
12992 else
12993 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012994 if (fnum != -1)
12995 list_append_number(l, (varnumber_T)fnum);
12996 else
12997 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012998 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12999 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013000 list_append_number(l, (fp != NULL)
13001 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013002 : (varnumber_T)0);
13003 list_append_number(l,
13004#ifdef FEAT_VIRTUALEDIT
13005 (fp != NULL) ? (varnumber_T)fp->coladd :
13006#endif
13007 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013008 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013009 {
13010 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013011 list_append_number(l, curwin->w_curswant == MAXCOL ?
13012 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013013 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013014 }
13015 else
13016 rettv->vval.v_number = FALSE;
13017}
13018
13019/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013020 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013021 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013023f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013024{
13025#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013026 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013027#endif
13028
Bram Moolenaar2641f772005-03-25 21:58:17 +000013029#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013030 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013031 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013032 wp = NULL;
13033 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13034 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013035 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013036 if (wp == NULL)
13037 return;
13038 }
13039
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013040 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013041 }
13042#endif
13043}
13044
13045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046 * "getreg()" function
13047 */
13048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013049f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050{
13051 char_u *strregname;
13052 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013053 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013054 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013055 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013057 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013059 strregname = get_tv_string_chk(&argvars[0]);
13060 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013061 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013063 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013064 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13065 return_list = get_tv_number_chk(&argvars[2], &error);
13066 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013069 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013070
13071 if (error)
13072 return;
13073
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 regname = (strregname == NULL ? '"' : *strregname);
13075 if (regname == 0)
13076 regname = '"';
13077
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013078 if (return_list)
13079 {
13080 rettv->v_type = VAR_LIST;
13081 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13082 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013083 if (rettv->vval.v_list != NULL)
13084 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013085 }
13086 else
13087 {
13088 rettv->v_type = VAR_STRING;
13089 rettv->vval.v_string = get_reg_contents(regname,
13090 arg2 ? GREG_EXPR_SRC : 0);
13091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092}
13093
13094/*
13095 * "getregtype()" function
13096 */
13097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013098f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099{
13100 char_u *strregname;
13101 int regname;
13102 char_u buf[NUMBUFLEN + 2];
13103 long reglen = 0;
13104
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013105 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013106 {
13107 strregname = get_tv_string_chk(&argvars[0]);
13108 if (strregname == NULL) /* type error; errmsg already given */
13109 {
13110 rettv->v_type = VAR_STRING;
13111 rettv->vval.v_string = NULL;
13112 return;
13113 }
13114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 else
13116 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013117 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118
13119 regname = (strregname == NULL ? '"' : *strregname);
13120 if (regname == 0)
13121 regname = '"';
13122
13123 buf[0] = NUL;
13124 buf[1] = NUL;
13125 switch (get_reg_type(regname, &reglen))
13126 {
13127 case MLINE: buf[0] = 'V'; break;
13128 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129 case MBLOCK:
13130 buf[0] = Ctrl_V;
13131 sprintf((char *)buf + 1, "%ld", reglen + 1);
13132 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013134 rettv->v_type = VAR_STRING;
13135 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136}
13137
13138/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013139 * "gettabvar()" function
13140 */
13141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013142f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013143{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013144 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013145 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013146 dictitem_T *v;
13147 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013148 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013149
13150 rettv->v_type = VAR_STRING;
13151 rettv->vval.v_string = NULL;
13152
13153 varname = get_tv_string_chk(&argvars[1]);
13154 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13155 if (tp != NULL && varname != NULL)
13156 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013157 /* Set tp to be our tabpage, temporarily. Also set the window to the
13158 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013159 if (switch_win(&oldcurwin, &oldtabpage,
13160 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013161 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013162 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013163 /* look up the variable */
13164 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13165 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13166 if (v != NULL)
13167 {
13168 copy_tv(&v->di_tv, rettv);
13169 done = TRUE;
13170 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013171 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013172
13173 /* restore previous notion of curwin */
13174 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013175 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013176
13177 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013178 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013179}
13180
13181/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013182 * "gettabwinvar()" function
13183 */
13184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013185f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013186{
13187 getwinvar(argvars, rettv, 1);
13188}
13189
13190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191 * "getwinposx()" function
13192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013194f_getwinposx(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 = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 }
13205#endif
13206}
13207
13208/*
13209 * "getwinposy()" function
13210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013212f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013214 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215#ifdef FEAT_GUI
13216 if (gui.in_use)
13217 {
13218 int x, y;
13219
13220 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013221 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 }
13223#endif
13224}
13225
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013226/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013227 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013228 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013229 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013230find_win_by_nr(
13231 typval_T *vp,
13232 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013233{
13234#ifdef FEAT_WINDOWS
13235 win_T *wp;
13236#endif
13237 int nr;
13238
13239 nr = get_tv_number_chk(vp, NULL);
13240
13241#ifdef FEAT_WINDOWS
13242 if (nr < 0)
13243 return NULL;
13244 if (nr == 0)
13245 return curwin;
13246
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013247 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13248 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013249 if (--nr <= 0)
13250 break;
13251 return wp;
13252#else
13253 if (nr == 0 || nr == 1)
13254 return curwin;
13255 return NULL;
13256#endif
13257}
13258
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013260 * Find window specified by "wvp" in tabpage "tvp".
13261 */
13262 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013263find_tabwin(
13264 typval_T *wvp, /* VAR_UNKNOWN for current window */
13265 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013266{
13267 win_T *wp = NULL;
13268 tabpage_T *tp = NULL;
13269 long n;
13270
13271 if (wvp->v_type != VAR_UNKNOWN)
13272 {
13273 if (tvp->v_type != VAR_UNKNOWN)
13274 {
13275 n = get_tv_number(tvp);
13276 if (n >= 0)
13277 tp = find_tabpage(n);
13278 }
13279 else
13280 tp = curtab;
13281
13282 if (tp != NULL)
13283 wp = find_win_by_nr(wvp, tp);
13284 }
13285 else
13286 wp = curwin;
13287
13288 return wp;
13289}
13290
13291/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292 * "getwinvar()" function
13293 */
13294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013295f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013297 getwinvar(argvars, rettv, 0);
13298}
13299
13300/*
13301 * getwinvar() and gettabwinvar()
13302 */
13303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013304getwinvar(
13305 typval_T *argvars,
13306 typval_T *rettv,
13307 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013308{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013309 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013311 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013312 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013313 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013314#ifdef FEAT_WINDOWS
13315 win_T *oldcurwin;
13316 tabpage_T *oldtabpage;
13317 int need_switch_win;
13318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013320#ifdef FEAT_WINDOWS
13321 if (off == 1)
13322 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13323 else
13324 tp = curtab;
13325#endif
13326 win = find_win_by_nr(&argvars[off], tp);
13327 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013328 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013330 rettv->v_type = VAR_STRING;
13331 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332
13333 if (win != NULL && varname != NULL)
13334 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013335#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013336 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013337 * otherwise the window is not valid. Only do this when needed,
13338 * autocommands get blocked. */
13339 need_switch_win = !(tp == curtab && win == curwin);
13340 if (!need_switch_win
13341 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13342#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013343 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013344 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013345 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013346 if (get_option_tv(&varname, rettv, 1) == OK)
13347 done = TRUE;
13348 }
13349 else
13350 {
13351 /* Look up the variable. */
13352 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13353 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13354 varname, FALSE);
13355 if (v != NULL)
13356 {
13357 copy_tv(&v->di_tv, rettv);
13358 done = TRUE;
13359 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013361 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013362
Bram Moolenaarba117c22015-09-29 16:53:22 +020013363#ifdef FEAT_WINDOWS
13364 if (need_switch_win)
13365 /* restore previous notion of curwin */
13366 restore_win(oldcurwin, oldtabpage, TRUE);
13367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368 }
13369
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013370 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13371 /* use the default return value */
13372 copy_tv(&argvars[off + 2], rettv);
13373
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 --emsg_off;
13375}
13376
13377/*
13378 * "glob()" function
13379 */
13380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013381f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013383 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013385 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013387 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013388 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013389 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013390 if (argvars[1].v_type != VAR_UNKNOWN)
13391 {
13392 if (get_tv_number_chk(&argvars[1], &error))
13393 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013394 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013395 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013396 if (get_tv_number_chk(&argvars[2], &error))
13397 {
13398 rettv->v_type = VAR_LIST;
13399 rettv->vval.v_list = NULL;
13400 }
13401 if (argvars[3].v_type != VAR_UNKNOWN
13402 && get_tv_number_chk(&argvars[3], &error))
13403 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013404 }
13405 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013406 if (!error)
13407 {
13408 ExpandInit(&xpc);
13409 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013410 if (p_wic)
13411 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013412 if (rettv->v_type == VAR_STRING)
13413 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013414 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013415 else if (rettv_list_alloc(rettv) != FAIL)
13416 {
13417 int i;
13418
13419 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13420 NULL, options, WILD_ALL_KEEP);
13421 for (i = 0; i < xpc.xp_numfiles; i++)
13422 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13423
13424 ExpandCleanup(&xpc);
13425 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013426 }
13427 else
13428 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429}
13430
13431/*
13432 * "globpath()" function
13433 */
13434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013435f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013437 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013439 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013440 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013441 garray_T ga;
13442 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013444 /* When the optional second argument is non-zero, don't remove matches
13445 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013446 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013447 if (argvars[2].v_type != VAR_UNKNOWN)
13448 {
13449 if (get_tv_number_chk(&argvars[2], &error))
13450 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013451 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013452 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013453 if (get_tv_number_chk(&argvars[3], &error))
13454 {
13455 rettv->v_type = VAR_LIST;
13456 rettv->vval.v_list = NULL;
13457 }
13458 if (argvars[4].v_type != VAR_UNKNOWN
13459 && get_tv_number_chk(&argvars[4], &error))
13460 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013461 }
13462 }
13463 if (file != NULL && !error)
13464 {
13465 ga_init2(&ga, (int)sizeof(char_u *), 10);
13466 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13467 if (rettv->v_type == VAR_STRING)
13468 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13469 else if (rettv_list_alloc(rettv) != FAIL)
13470 for (i = 0; i < ga.ga_len; ++i)
13471 list_append_string(rettv->vval.v_list,
13472 ((char_u **)(ga.ga_data))[i], -1);
13473 ga_clear_strings(&ga);
13474 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013475 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013476 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477}
13478
13479/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013480 * "glob2regpat()" function
13481 */
13482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013483f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013484{
13485 char_u *pat = get_tv_string_chk(&argvars[0]);
13486
13487 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013488 rettv->vval.v_string = (pat == NULL)
13489 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013490}
13491
13492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493 * "has()" function
13494 */
13495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013496f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497{
13498 int i;
13499 char_u *name;
13500 int n = FALSE;
13501 static char *(has_list[]) =
13502 {
13503#ifdef AMIGA
13504 "amiga",
13505# ifdef FEAT_ARP
13506 "arp",
13507# endif
13508#endif
13509#ifdef __BEOS__
13510 "beos",
13511#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013512#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 "mac",
13514#endif
13515#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013516 "macunix", /* built with 'darwin' enabled */
13517#endif
13518#if defined(__APPLE__) && __APPLE__ == 1
13519 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521#ifdef __QNX__
13522 "qnx",
13523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524#ifdef UNIX
13525 "unix",
13526#endif
13527#ifdef VMS
13528 "vms",
13529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530#ifdef WIN32
13531 "win32",
13532#endif
13533#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13534 "win32unix",
13535#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013536#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 "win64",
13538#endif
13539#ifdef EBCDIC
13540 "ebcdic",
13541#endif
13542#ifndef CASE_INSENSITIVE_FILENAME
13543 "fname_case",
13544#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013545#ifdef HAVE_ACL
13546 "acl",
13547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548#ifdef FEAT_ARABIC
13549 "arabic",
13550#endif
13551#ifdef FEAT_AUTOCMD
13552 "autocmd",
13553#endif
13554#ifdef FEAT_BEVAL
13555 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013556# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13557 "balloon_multiline",
13558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559#endif
13560#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13561 "builtin_terms",
13562# ifdef ALL_BUILTIN_TCAPS
13563 "all_builtin_terms",
13564# endif
13565#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013566#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13567 || defined(FEAT_GUI_W32) \
13568 || defined(FEAT_GUI_MOTIF))
13569 "browsefilter",
13570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571#ifdef FEAT_BYTEOFF
13572 "byte_offset",
13573#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013574#ifdef FEAT_CHANNEL
13575 "channel",
13576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577#ifdef FEAT_CINDENT
13578 "cindent",
13579#endif
13580#ifdef FEAT_CLIENTSERVER
13581 "clientserver",
13582#endif
13583#ifdef FEAT_CLIPBOARD
13584 "clipboard",
13585#endif
13586#ifdef FEAT_CMDL_COMPL
13587 "cmdline_compl",
13588#endif
13589#ifdef FEAT_CMDHIST
13590 "cmdline_hist",
13591#endif
13592#ifdef FEAT_COMMENTS
13593 "comments",
13594#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013595#ifdef FEAT_CONCEAL
13596 "conceal",
13597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598#ifdef FEAT_CRYPT
13599 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013600 "crypt-blowfish",
13601 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602#endif
13603#ifdef FEAT_CSCOPE
13604 "cscope",
13605#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013606#ifdef FEAT_CURSORBIND
13607 "cursorbind",
13608#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013609#ifdef CURSOR_SHAPE
13610 "cursorshape",
13611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612#ifdef DEBUG
13613 "debug",
13614#endif
13615#ifdef FEAT_CON_DIALOG
13616 "dialog_con",
13617#endif
13618#ifdef FEAT_GUI_DIALOG
13619 "dialog_gui",
13620#endif
13621#ifdef FEAT_DIFF
13622 "diff",
13623#endif
13624#ifdef FEAT_DIGRAPHS
13625 "digraphs",
13626#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013627#ifdef FEAT_DIRECTX
13628 "directx",
13629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630#ifdef FEAT_DND
13631 "dnd",
13632#endif
13633#ifdef FEAT_EMACS_TAGS
13634 "emacs_tags",
13635#endif
13636 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013637 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638#ifdef FEAT_SEARCH_EXTRA
13639 "extra_search",
13640#endif
13641#ifdef FEAT_FKMAP
13642 "farsi",
13643#endif
13644#ifdef FEAT_SEARCHPATH
13645 "file_in_path",
13646#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013647#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013648 "filterpipe",
13649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650#ifdef FEAT_FIND_ID
13651 "find_in_path",
13652#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013653#ifdef FEAT_FLOAT
13654 "float",
13655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656#ifdef FEAT_FOLDING
13657 "folding",
13658#endif
13659#ifdef FEAT_FOOTER
13660 "footer",
13661#endif
13662#if !defined(USE_SYSTEM) && defined(UNIX)
13663 "fork",
13664#endif
13665#ifdef FEAT_GETTEXT
13666 "gettext",
13667#endif
13668#ifdef FEAT_GUI
13669 "gui",
13670#endif
13671#ifdef FEAT_GUI_ATHENA
13672# ifdef FEAT_GUI_NEXTAW
13673 "gui_neXtaw",
13674# else
13675 "gui_athena",
13676# endif
13677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678#ifdef FEAT_GUI_GTK
13679 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013680# ifdef USE_GTK3
13681 "gui_gtk3",
13682# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013686#ifdef FEAT_GUI_GNOME
13687 "gui_gnome",
13688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689#ifdef FEAT_GUI_MAC
13690 "gui_mac",
13691#endif
13692#ifdef FEAT_GUI_MOTIF
13693 "gui_motif",
13694#endif
13695#ifdef FEAT_GUI_PHOTON
13696 "gui_photon",
13697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698#ifdef FEAT_GUI_W32
13699 "gui_win32",
13700#endif
13701#ifdef FEAT_HANGULIN
13702 "hangul_input",
13703#endif
13704#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13705 "iconv",
13706#endif
13707#ifdef FEAT_INS_EXPAND
13708 "insert_expand",
13709#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013710#ifdef FEAT_JOB
13711 "job",
13712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713#ifdef FEAT_JUMPLIST
13714 "jumplist",
13715#endif
13716#ifdef FEAT_KEYMAP
13717 "keymap",
13718#endif
13719#ifdef FEAT_LANGMAP
13720 "langmap",
13721#endif
13722#ifdef FEAT_LIBCALL
13723 "libcall",
13724#endif
13725#ifdef FEAT_LINEBREAK
13726 "linebreak",
13727#endif
13728#ifdef FEAT_LISP
13729 "lispindent",
13730#endif
13731#ifdef FEAT_LISTCMDS
13732 "listcmds",
13733#endif
13734#ifdef FEAT_LOCALMAP
13735 "localmap",
13736#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013737#ifdef FEAT_LUA
13738# ifndef DYNAMIC_LUA
13739 "lua",
13740# endif
13741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742#ifdef FEAT_MENU
13743 "menu",
13744#endif
13745#ifdef FEAT_SESSION
13746 "mksession",
13747#endif
13748#ifdef FEAT_MODIFY_FNAME
13749 "modify_fname",
13750#endif
13751#ifdef FEAT_MOUSE
13752 "mouse",
13753#endif
13754#ifdef FEAT_MOUSESHAPE
13755 "mouseshape",
13756#endif
13757#if defined(UNIX) || defined(VMS)
13758# ifdef FEAT_MOUSE_DEC
13759 "mouse_dec",
13760# endif
13761# ifdef FEAT_MOUSE_GPM
13762 "mouse_gpm",
13763# endif
13764# ifdef FEAT_MOUSE_JSB
13765 "mouse_jsbterm",
13766# endif
13767# ifdef FEAT_MOUSE_NET
13768 "mouse_netterm",
13769# endif
13770# ifdef FEAT_MOUSE_PTERM
13771 "mouse_pterm",
13772# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013773# ifdef FEAT_MOUSE_SGR
13774 "mouse_sgr",
13775# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013776# ifdef FEAT_SYSMOUSE
13777 "mouse_sysmouse",
13778# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013779# ifdef FEAT_MOUSE_URXVT
13780 "mouse_urxvt",
13781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782# ifdef FEAT_MOUSE_XTERM
13783 "mouse_xterm",
13784# endif
13785#endif
13786#ifdef FEAT_MBYTE
13787 "multi_byte",
13788#endif
13789#ifdef FEAT_MBYTE_IME
13790 "multi_byte_ime",
13791#endif
13792#ifdef FEAT_MULTI_LANG
13793 "multi_lang",
13794#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013795#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013796#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013797 "mzscheme",
13798#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013800#ifdef FEAT_OLE
13801 "ole",
13802#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013803 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804#ifdef FEAT_PATH_EXTRA
13805 "path_extra",
13806#endif
13807#ifdef FEAT_PERL
13808#ifndef DYNAMIC_PERL
13809 "perl",
13810#endif
13811#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013812#ifdef FEAT_PERSISTENT_UNDO
13813 "persistent_undo",
13814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815#ifdef FEAT_PYTHON
13816#ifndef DYNAMIC_PYTHON
13817 "python",
13818#endif
13819#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013820#ifdef FEAT_PYTHON3
13821#ifndef DYNAMIC_PYTHON3
13822 "python3",
13823#endif
13824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825#ifdef FEAT_POSTSCRIPT
13826 "postscript",
13827#endif
13828#ifdef FEAT_PRINTER
13829 "printer",
13830#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013831#ifdef FEAT_PROFILE
13832 "profile",
13833#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013834#ifdef FEAT_RELTIME
13835 "reltime",
13836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837#ifdef FEAT_QUICKFIX
13838 "quickfix",
13839#endif
13840#ifdef FEAT_RIGHTLEFT
13841 "rightleft",
13842#endif
13843#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13844 "ruby",
13845#endif
13846#ifdef FEAT_SCROLLBIND
13847 "scrollbind",
13848#endif
13849#ifdef FEAT_CMDL_INFO
13850 "showcmd",
13851 "cmdline_info",
13852#endif
13853#ifdef FEAT_SIGNS
13854 "signs",
13855#endif
13856#ifdef FEAT_SMARTINDENT
13857 "smartindent",
13858#endif
13859#ifdef FEAT_SNIFF
13860 "sniff",
13861#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013862#ifdef STARTUPTIME
13863 "startuptime",
13864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865#ifdef FEAT_STL_OPT
13866 "statusline",
13867#endif
13868#ifdef FEAT_SUN_WORKSHOP
13869 "sun_workshop",
13870#endif
13871#ifdef FEAT_NETBEANS_INTG
13872 "netbeans_intg",
13873#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013874#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013875 "spell",
13876#endif
13877#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 "syntax",
13879#endif
13880#if defined(USE_SYSTEM) || !defined(UNIX)
13881 "system",
13882#endif
13883#ifdef FEAT_TAG_BINS
13884 "tag_binary",
13885#endif
13886#ifdef FEAT_TAG_OLDSTATIC
13887 "tag_old_static",
13888#endif
13889#ifdef FEAT_TAG_ANYWHITE
13890 "tag_any_white",
13891#endif
13892#ifdef FEAT_TCL
13893# ifndef DYNAMIC_TCL
13894 "tcl",
13895# endif
13896#endif
13897#ifdef TERMINFO
13898 "terminfo",
13899#endif
13900#ifdef FEAT_TERMRESPONSE
13901 "termresponse",
13902#endif
13903#ifdef FEAT_TEXTOBJ
13904 "textobjects",
13905#endif
13906#ifdef HAVE_TGETENT
13907 "tgetent",
13908#endif
13909#ifdef FEAT_TITLE
13910 "title",
13911#endif
13912#ifdef FEAT_TOOLBAR
13913 "toolbar",
13914#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013915#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13916 "unnamedplus",
13917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918#ifdef FEAT_USR_CMDS
13919 "user-commands", /* was accidentally included in 5.4 */
13920 "user_commands",
13921#endif
13922#ifdef FEAT_VIMINFO
13923 "viminfo",
13924#endif
13925#ifdef FEAT_VERTSPLIT
13926 "vertsplit",
13927#endif
13928#ifdef FEAT_VIRTUALEDIT
13929 "virtualedit",
13930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932#ifdef FEAT_VISUALEXTRA
13933 "visualextra",
13934#endif
13935#ifdef FEAT_VREPLACE
13936 "vreplace",
13937#endif
13938#ifdef FEAT_WILDIGN
13939 "wildignore",
13940#endif
13941#ifdef FEAT_WILDMENU
13942 "wildmenu",
13943#endif
13944#ifdef FEAT_WINDOWS
13945 "windows",
13946#endif
13947#ifdef FEAT_WAK
13948 "winaltkeys",
13949#endif
13950#ifdef FEAT_WRITEBACKUP
13951 "writebackup",
13952#endif
13953#ifdef FEAT_XIM
13954 "xim",
13955#endif
13956#ifdef FEAT_XFONTSET
13957 "xfontset",
13958#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013959#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013960 "xpm",
13961 "xpm_w32", /* for backward compatibility */
13962#else
13963# if defined(HAVE_XPM)
13964 "xpm",
13965# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967#ifdef USE_XSMP
13968 "xsmp",
13969#endif
13970#ifdef USE_XSMP_INTERACT
13971 "xsmp_interact",
13972#endif
13973#ifdef FEAT_XCLIPBOARD
13974 "xterm_clipboard",
13975#endif
13976#ifdef FEAT_XTERM_SAVE
13977 "xterm_save",
13978#endif
13979#if defined(UNIX) && defined(FEAT_X11)
13980 "X11",
13981#endif
13982 NULL
13983 };
13984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 for (i = 0; has_list[i] != NULL; ++i)
13987 if (STRICMP(name, has_list[i]) == 0)
13988 {
13989 n = TRUE;
13990 break;
13991 }
13992
13993 if (n == FALSE)
13994 {
13995 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013996 {
13997 if (name[5] == '-'
13998 && STRLEN(name) > 11
13999 && vim_isdigit(name[6])
14000 && vim_isdigit(name[8])
14001 && vim_isdigit(name[10]))
14002 {
14003 int major = atoi((char *)name + 6);
14004 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014005
14006 /* Expect "patch-9.9.01234". */
14007 n = (major < VIM_VERSION_MAJOR
14008 || (major == VIM_VERSION_MAJOR
14009 && (minor < VIM_VERSION_MINOR
14010 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014011 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014012 }
14013 else
14014 n = has_patch(atoi((char *)name + 5));
14015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 else if (STRICMP(name, "vim_starting") == 0)
14017 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014018#ifdef FEAT_MBYTE
14019 else if (STRICMP(name, "multi_byte_encoding") == 0)
14020 n = has_mbyte;
14021#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014022#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14023 else if (STRICMP(name, "balloon_multiline") == 0)
14024 n = multiline_balloon_available();
14025#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026#ifdef DYNAMIC_TCL
14027 else if (STRICMP(name, "tcl") == 0)
14028 n = tcl_enabled(FALSE);
14029#endif
14030#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14031 else if (STRICMP(name, "iconv") == 0)
14032 n = iconv_enabled(FALSE);
14033#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014034#ifdef DYNAMIC_LUA
14035 else if (STRICMP(name, "lua") == 0)
14036 n = lua_enabled(FALSE);
14037#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014038#ifdef DYNAMIC_MZSCHEME
14039 else if (STRICMP(name, "mzscheme") == 0)
14040 n = mzscheme_enabled(FALSE);
14041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042#ifdef DYNAMIC_RUBY
14043 else if (STRICMP(name, "ruby") == 0)
14044 n = ruby_enabled(FALSE);
14045#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014046#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047#ifdef DYNAMIC_PYTHON
14048 else if (STRICMP(name, "python") == 0)
14049 n = python_enabled(FALSE);
14050#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014051#endif
14052#ifdef FEAT_PYTHON3
14053#ifdef DYNAMIC_PYTHON3
14054 else if (STRICMP(name, "python3") == 0)
14055 n = python3_enabled(FALSE);
14056#endif
14057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058#ifdef DYNAMIC_PERL
14059 else if (STRICMP(name, "perl") == 0)
14060 n = perl_enabled(FALSE);
14061#endif
14062#ifdef FEAT_GUI
14063 else if (STRICMP(name, "gui_running") == 0)
14064 n = (gui.in_use || gui.starting);
14065# ifdef FEAT_GUI_W32
14066 else if (STRICMP(name, "gui_win32s") == 0)
14067 n = gui_is_win32s();
14068# endif
14069# ifdef FEAT_BROWSE
14070 else if (STRICMP(name, "browse") == 0)
14071 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14072# endif
14073#endif
14074#ifdef FEAT_SYN_HL
14075 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014076 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077#endif
14078#if defined(WIN3264)
14079 else if (STRICMP(name, "win95") == 0)
14080 n = mch_windows95();
14081#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014082#ifdef FEAT_NETBEANS_INTG
14083 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014084 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 }
14087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014088 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089}
14090
14091/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014092 * "has_key()" function
14093 */
14094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014095f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014096{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014097 if (argvars[0].v_type != VAR_DICT)
14098 {
14099 EMSG(_(e_dictreq));
14100 return;
14101 }
14102 if (argvars[0].vval.v_dict == NULL)
14103 return;
14104
14105 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014106 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014107}
14108
14109/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014110 * "haslocaldir()" function
14111 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014113f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014114{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014115 win_T *wp = NULL;
14116
14117 wp = find_tabwin(&argvars[0], &argvars[1]);
14118 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014119}
14120
14121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122 * "hasmapto()" function
14123 */
14124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014125f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126{
14127 char_u *name;
14128 char_u *mode;
14129 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014130 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014133 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 mode = (char_u *)"nvo";
14135 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014138 if (argvars[2].v_type != VAR_UNKNOWN)
14139 abbr = get_tv_number(&argvars[2]);
14140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141
Bram Moolenaar2c932302006-03-18 21:42:09 +000014142 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014143 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014145 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146}
14147
14148/*
14149 * "histadd()" function
14150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014152f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153{
14154#ifdef FEAT_CMDHIST
14155 int histype;
14156 char_u *str;
14157 char_u buf[NUMBUFLEN];
14158#endif
14159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161 if (check_restricted() || check_secure())
14162 return;
14163#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14165 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014166 if (histype >= 0)
14167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014168 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 if (*str != NUL)
14170 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014171 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014173 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 return;
14175 }
14176 }
14177#endif
14178}
14179
14180/*
14181 * "histdel()" function
14182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014184f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185{
14186#ifdef FEAT_CMDHIST
14187 int n;
14188 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14192 if (str == NULL)
14193 n = 0;
14194 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014197 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014200 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014201 else
14202 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014203 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014204 get_tv_string_buf(&argvars[1], buf));
14205 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206#endif
14207}
14208
14209/*
14210 * "histget()" function
14211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014213f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214{
14215#ifdef FEAT_CMDHIST
14216 int type;
14217 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014218 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014220 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14221 if (str == NULL)
14222 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 {
14225 type = get_histtype(str);
14226 if (argvars[1].v_type == VAR_UNKNOWN)
14227 idx = get_history_idx(type);
14228 else
14229 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14230 /* -1 on type error */
14231 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014234 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014236 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237}
14238
14239/*
14240 * "histnr()" function
14241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014243f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244{
14245 int i;
14246
14247#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 char_u *history = get_tv_string_chk(&argvars[0]);
14249
14250 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251 if (i >= HIST_CMD && i < HIST_COUNT)
14252 i = get_history_idx(i);
14253 else
14254#endif
14255 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257}
14258
14259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 * "highlightID(name)" function
14261 */
14262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014263f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014265 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266}
14267
14268/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014269 * "highlight_exists()" function
14270 */
14271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014272f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014273{
14274 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14275}
14276
14277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 * "hostname()" function
14279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014281f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282{
14283 char_u hostname[256];
14284
14285 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 rettv->v_type = VAR_STRING;
14287 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288}
14289
14290/*
14291 * iconv() function
14292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014294f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295{
14296#ifdef FEAT_MBYTE
14297 char_u buf1[NUMBUFLEN];
14298 char_u buf2[NUMBUFLEN];
14299 char_u *from, *to, *str;
14300 vimconv_T vimconv;
14301#endif
14302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014303 rettv->v_type = VAR_STRING;
14304 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305
14306#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307 str = get_tv_string(&argvars[0]);
14308 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14309 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 vimconv.vc_type = CONV_NONE;
14311 convert_setup(&vimconv, from, to);
14312
14313 /* If the encodings are equal, no conversion needed. */
14314 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014315 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318
14319 convert_setup(&vimconv, NULL, NULL);
14320 vim_free(from);
14321 vim_free(to);
14322#endif
14323}
14324
14325/*
14326 * "indent()" function
14327 */
14328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014329f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330{
14331 linenr_T lnum;
14332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014333 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014335 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014337 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338}
14339
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014340/*
14341 * "index()" function
14342 */
14343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014344f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014345{
Bram Moolenaar33570922005-01-25 22:26:29 +000014346 list_T *l;
14347 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014348 long idx = 0;
14349 int ic = FALSE;
14350
14351 rettv->vval.v_number = -1;
14352 if (argvars[0].v_type != VAR_LIST)
14353 {
14354 EMSG(_(e_listreq));
14355 return;
14356 }
14357 l = argvars[0].vval.v_list;
14358 if (l != NULL)
14359 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014360 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014361 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 int error = FALSE;
14364
Bram Moolenaar758711c2005-02-02 23:11:38 +000014365 /* Start at specified item. Use the cached index that list_find()
14366 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014368 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014369 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370 ic = get_tv_number_chk(&argvars[3], &error);
14371 if (error)
14372 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014373 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014374
Bram Moolenaar758711c2005-02-02 23:11:38 +000014375 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014376 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014377 {
14378 rettv->vval.v_number = idx;
14379 break;
14380 }
14381 }
14382}
14383
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384static int inputsecret_flag = 0;
14385
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014386static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014387
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014389 * This function is used by f_input() and f_inputdialog() functions. The third
14390 * argument to f_input() specifies the type of completion to use at the
14391 * prompt. The third argument to f_inputdialog() specifies the value to return
14392 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 */
14394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014395get_user_input(
14396 typval_T *argvars,
14397 typval_T *rettv,
14398 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014400 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 char_u *p = NULL;
14402 int c;
14403 char_u buf[NUMBUFLEN];
14404 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014405 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014406 int xp_type = EXPAND_NOTHING;
14407 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014409 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014410 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411
14412#ifdef NO_CONSOLE_INPUT
14413 /* While starting up, there is no place to enter text. */
14414 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416#endif
14417
14418 cmd_silent = FALSE; /* Want to see the prompt. */
14419 if (prompt != NULL)
14420 {
14421 /* Only the part of the message after the last NL is considered as
14422 * prompt for the command line */
14423 p = vim_strrchr(prompt, '\n');
14424 if (p == NULL)
14425 p = prompt;
14426 else
14427 {
14428 ++p;
14429 c = *p;
14430 *p = NUL;
14431 msg_start();
14432 msg_clr_eos();
14433 msg_puts_attr(prompt, echo_attr);
14434 msg_didout = FALSE;
14435 msg_starthere();
14436 *p = c;
14437 }
14438 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014440 if (argvars[1].v_type != VAR_UNKNOWN)
14441 {
14442 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14443 if (defstr != NULL)
14444 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014446 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014447 {
14448 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014449 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014450 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014451
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014452 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014453 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014454
Bram Moolenaar4463f292005-09-25 22:20:24 +000014455 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14456 if (xp_name == NULL)
14457 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014458
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014459 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014460
Bram Moolenaar4463f292005-09-25 22:20:24 +000014461 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14462 &xp_arg) == FAIL)
14463 return;
14464 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014465 }
14466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014467 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014468 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014469 int save_ex_normal_busy = ex_normal_busy;
14470 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014471 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014472 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14473 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014474 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014475 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014476 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014477 && argvars[1].v_type != VAR_UNKNOWN
14478 && argvars[2].v_type != VAR_UNKNOWN)
14479 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14480 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014481
14482 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014484 /* since the user typed this, no need to wait for return */
14485 need_wait_return = FALSE;
14486 msg_didout = FALSE;
14487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 cmd_silent = cmd_silent_save;
14489}
14490
14491/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014492 * "input()" function
14493 * Also handles inputsecret() when inputsecret is set.
14494 */
14495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014496f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014497{
14498 get_user_input(argvars, rettv, FALSE);
14499}
14500
14501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 * "inputdialog()" function
14503 */
14504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014505f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506{
14507#if defined(FEAT_GUI_TEXTDIALOG)
14508 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14509 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14510 {
14511 char_u *message;
14512 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014513 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 message = get_tv_string_chk(&argvars[0]);
14516 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014517 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014518 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 else
14520 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 if (message != NULL && defstr != NULL
14522 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014523 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 else
14526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014527 if (message != NULL && defstr != NULL
14528 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014529 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530 rettv->vval.v_string = vim_strsave(
14531 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 }
14537 else
14538#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014539 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540}
14541
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014542/*
14543 * "inputlist()" function
14544 */
14545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014546f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014547{
14548 listitem_T *li;
14549 int selected;
14550 int mouse_used;
14551
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014552#ifdef NO_CONSOLE_INPUT
14553 /* While starting up, there is no place to enter text. */
14554 if (no_console_input())
14555 return;
14556#endif
14557 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14558 {
14559 EMSG2(_(e_listarg), "inputlist()");
14560 return;
14561 }
14562
14563 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014564 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014565 lines_left = Rows; /* avoid more prompt */
14566 msg_scroll = TRUE;
14567 msg_clr_eos();
14568
14569 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14570 {
14571 msg_puts(get_tv_string(&li->li_tv));
14572 msg_putchar('\n');
14573 }
14574
14575 /* Ask for choice. */
14576 selected = prompt_for_number(&mouse_used);
14577 if (mouse_used)
14578 selected -= lines_left;
14579
14580 rettv->vval.v_number = selected;
14581}
14582
14583
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14585
14586/*
14587 * "inputrestore()" function
14588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014590f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591{
14592 if (ga_userinput.ga_len > 0)
14593 {
14594 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14596 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014597 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 }
14599 else if (p_verbose > 1)
14600 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014601 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014602 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 }
14604}
14605
14606/*
14607 * "inputsave()" function
14608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014610f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014612 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 if (ga_grow(&ga_userinput, 1) == OK)
14614 {
14615 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14616 + ga_userinput.ga_len);
14617 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014618 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 }
14620 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014621 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622}
14623
14624/*
14625 * "inputsecret()" function
14626 */
14627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014628f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629{
14630 ++cmdline_star;
14631 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014632 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 --cmdline_star;
14634 --inputsecret_flag;
14635}
14636
14637/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014638 * "insert()" function
14639 */
14640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014641f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014642{
14643 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014644 listitem_T *item;
14645 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014646 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014647
14648 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014650 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014651 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014652 {
14653 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 before = get_tv_number_chk(&argvars[2], &error);
14655 if (error)
14656 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014657
Bram Moolenaar758711c2005-02-02 23:11:38 +000014658 if (before == l->lv_len)
14659 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014660 else
14661 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014662 item = list_find(l, before);
14663 if (item == NULL)
14664 {
14665 EMSGN(_(e_listidx), before);
14666 l = NULL;
14667 }
14668 }
14669 if (l != NULL)
14670 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014671 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014672 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014673 }
14674 }
14675}
14676
14677/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014678 * "invert(expr)" function
14679 */
14680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014681f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014682{
14683 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14684}
14685
14686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687 * "isdirectory()" function
14688 */
14689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014690f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014692 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693}
14694
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014695/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014696 * "islocked()" function
14697 */
14698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014699f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014700{
14701 lval_T lv;
14702 char_u *end;
14703 dictitem_T *di;
14704
14705 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014706 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14707 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014708 if (end != NULL && lv.ll_name != NULL)
14709 {
14710 if (*end != NUL)
14711 EMSG(_(e_trailing));
14712 else
14713 {
14714 if (lv.ll_tv == NULL)
14715 {
14716 if (check_changedtick(lv.ll_name))
14717 rettv->vval.v_number = 1; /* always locked */
14718 else
14719 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014720 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014721 if (di != NULL)
14722 {
14723 /* Consider a variable locked when:
14724 * 1. the variable itself is locked
14725 * 2. the value of the variable is locked.
14726 * 3. the List or Dict value is locked.
14727 */
14728 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14729 || tv_islocked(&di->di_tv));
14730 }
14731 }
14732 }
14733 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014734 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014735 else if (lv.ll_newkey != NULL)
14736 EMSG2(_(e_dictkey), lv.ll_newkey);
14737 else if (lv.ll_list != NULL)
14738 /* List item. */
14739 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14740 else
14741 /* Dictionary item. */
14742 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14743 }
14744 }
14745
14746 clear_lval(&lv);
14747}
14748
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014749#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14750/*
14751 * "isnan()" function
14752 */
14753 static void
14754f_isnan(typval_T *argvars, typval_T *rettv)
14755{
14756 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14757 && isnan(argvars[0].vval.v_float);
14758}
14759#endif
14760
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014761static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014762
14763/*
14764 * Turn a dict into a list:
14765 * "what" == 0: list of keys
14766 * "what" == 1: list of values
14767 * "what" == 2: list of items
14768 */
14769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014770dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014771{
Bram Moolenaar33570922005-01-25 22:26:29 +000014772 list_T *l2;
14773 dictitem_T *di;
14774 hashitem_T *hi;
14775 listitem_T *li;
14776 listitem_T *li2;
14777 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014778 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014779
Bram Moolenaar8c711452005-01-14 21:53:12 +000014780 if (argvars[0].v_type != VAR_DICT)
14781 {
14782 EMSG(_(e_dictreq));
14783 return;
14784 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014785 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014786 return;
14787
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014788 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014789 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014790
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014791 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014792 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014793 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014794 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014795 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014796 --todo;
14797 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014798
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014799 li = listitem_alloc();
14800 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014801 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014802 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014803
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014804 if (what == 0)
14805 {
14806 /* keys() */
14807 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014808 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014809 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14810 }
14811 else if (what == 1)
14812 {
14813 /* values() */
14814 copy_tv(&di->di_tv, &li->li_tv);
14815 }
14816 else
14817 {
14818 /* items() */
14819 l2 = list_alloc();
14820 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014821 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014822 li->li_tv.vval.v_list = l2;
14823 if (l2 == NULL)
14824 break;
14825 ++l2->lv_refcount;
14826
14827 li2 = listitem_alloc();
14828 if (li2 == NULL)
14829 break;
14830 list_append(l2, li2);
14831 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014832 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014833 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14834
14835 li2 = listitem_alloc();
14836 if (li2 == NULL)
14837 break;
14838 list_append(l2, li2);
14839 copy_tv(&di->di_tv, &li2->li_tv);
14840 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014841 }
14842 }
14843}
14844
14845/*
14846 * "items(dict)" function
14847 */
14848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014849f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014850{
14851 dict_list(argvars, rettv, 2);
14852}
14853
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014854#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014855/*
14856 * Get the job from the argument.
14857 * Returns NULL if the job is invalid.
14858 */
14859 static job_T *
14860get_job_arg(typval_T *tv)
14861{
14862 job_T *job;
14863
14864 if (tv->v_type != VAR_JOB)
14865 {
14866 EMSG2(_(e_invarg2), get_tv_string(tv));
14867 return NULL;
14868 }
14869 job = tv->vval.v_job;
14870
14871 if (job == NULL)
14872 EMSG(_("E916: not a valid job"));
14873 return job;
14874}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014875
14876# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014877/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014878 * "job_getchannel()" function
14879 */
14880 static void
14881f_job_getchannel(typval_T *argvars, typval_T *rettv)
14882{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014883 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014884
Bram Moolenaar65edff82016-02-21 16:40:11 +010014885 if (job != NULL)
14886 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014887 rettv->v_type = VAR_CHANNEL;
14888 rettv->vval.v_channel = job->jv_channel;
14889 if (job->jv_channel != NULL)
14890 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014891 }
14892}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014893# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014894
14895/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014896 * "job_setoptions()" function
14897 */
14898 static void
14899f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14900{
14901 job_T *job = get_job_arg(&argvars[0]);
14902 jobopt_T opt;
14903
14904 if (job == NULL)
14905 return;
14906 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014907 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014908 return;
14909 job_set_options(job, &opt);
14910}
14911
14912/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014913 * "job_start()" function
14914 */
14915 static void
14916f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14917{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014918 job_T *job;
14919 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014920#if defined(UNIX)
14921# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014922 char **argv = NULL;
14923 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014924#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014925 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014926#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014927 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014928
14929 rettv->v_type = VAR_JOB;
14930 job = job_alloc();
14931 rettv->vval.v_job = job;
14932 if (job == NULL)
14933 return;
14934
14935 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014936
14937 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014938 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014939 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010014940 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014941 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
14942 + JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014943 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010014944 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014945
Bram Moolenaar835dc632016-02-07 14:27:38 +010014946#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014947 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014948#endif
14949
14950 if (argvars[0].v_type == VAR_STRING)
14951 {
14952 /* Command is a string. */
14953 cmd = argvars[0].vval.v_string;
14954#ifdef USE_ARGV
14955 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14956 return;
14957 argv[argc] = NULL;
14958#endif
14959 }
14960 else if (argvars[0].v_type != VAR_LIST
14961 || argvars[0].vval.v_list == NULL
14962 || argvars[0].vval.v_list->lv_len < 1)
14963 {
14964 EMSG(_(e_invarg));
14965 return;
14966 }
14967 else
14968 {
14969 list_T *l = argvars[0].vval.v_list;
14970 listitem_T *li;
14971 char_u *s;
14972
14973#ifdef USE_ARGV
14974 /* Pass argv[] to mch_call_shell(). */
14975 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14976 if (argv == NULL)
14977 return;
14978#endif
14979 for (li = l->lv_first; li != NULL; li = li->li_next)
14980 {
14981 s = get_tv_string_chk(&li->li_tv);
14982 if (s == NULL)
14983 goto theend;
14984#ifdef USE_ARGV
14985 argv[argc++] = (char *)s;
14986#else
14987 if (li != l->lv_first)
14988 {
14989 s = vim_strsave_shellescape(s, FALSE, TRUE);
14990 if (s == NULL)
14991 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014992 ga_concat(&ga, s);
14993 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014994 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014995 else
14996 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014997 if (li->li_next != NULL)
14998 ga_append(&ga, ' ');
14999#endif
15000 }
15001#ifdef USE_ARGV
15002 argv[argc] = NULL;
15003#else
15004 cmd = ga.ga_data;
15005#endif
15006 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015007
Bram Moolenaar835dc632016-02-07 14:27:38 +010015008#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015009# ifdef FEAT_CHANNEL
15010 if (ch_log_active())
15011 {
15012 garray_T ga;
15013 int i;
15014
15015 ga_init2(&ga, (int)sizeof(char), 200);
15016 for (i = 0; i < argc; ++i)
15017 {
15018 if (i > 0)
15019 ga_concat(&ga, (char_u *)" ");
15020 ga_concat(&ga, (char_u *)argv[i]);
15021 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015022 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015023 ga_clear(&ga);
15024 }
15025# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015026 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015027#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015028# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015029 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015030# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015031 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015032#endif
15033
15034theend:
15035#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015036 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015037#else
15038 vim_free(ga.ga_data);
15039#endif
15040}
15041
15042/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015043 * Get the status of "job" and invoke the exit callback when needed.
15044 * The returned string is not allocated.
15045 */
15046 static char *
15047job_status(job_T *job)
15048{
15049 char *result;
15050
15051 if (job->jv_status == JOB_ENDED)
15052 /* No need to check, dead is dead. */
15053 result = "dead";
15054 else if (job->jv_status == JOB_FAILED)
15055 result = "fail";
15056 else
15057 {
15058 result = mch_job_status(job);
15059# ifdef FEAT_CHANNEL
15060 if (job->jv_status == JOB_ENDED)
15061 ch_log(job->jv_channel, "Job ended");
15062# endif
15063 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15064 {
15065 typval_T argv[3];
15066 typval_T rettv;
15067 int dummy;
15068
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015069 /* invoke the exit callback; make sure the refcount is > 0 */
15070 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015071 argv[0].v_type = VAR_JOB;
15072 argv[0].vval.v_job = job;
15073 argv[1].v_type = VAR_NUMBER;
15074 argv[1].vval.v_number = job->jv_exitval;
15075 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15076 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15077 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015078 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015079 }
15080 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15081 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015082 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015083 * freed. Careful: caller must not use "job" after this! */
15084 job_free(job);
15085 }
15086 }
15087 return result;
15088}
15089
15090/*
15091 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15092 */
15093 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015094job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015095{
15096 static time_t last_check = 0;
15097 time_t now;
15098 job_T *job;
15099 job_T *next;
15100
15101 /* Only do this once in 10 seconds. */
15102 now = time(NULL);
15103 if (last_check + 10 < now)
15104 {
15105 last_check = now;
15106 for (job = first_job; job != NULL; job = next)
15107 {
15108 next = job->jv_next;
15109 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15110 job_status(job); /* may free "job" */
15111 }
15112 }
15113}
15114
15115/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015116 * "job_status()" function
15117 */
15118 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015119f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015120{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015121 job_T *job = get_job_arg(&argvars[0]);
15122 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015123
Bram Moolenaar65edff82016-02-21 16:40:11 +010015124 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015125 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015126 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015127 rettv->v_type = VAR_STRING;
15128 rettv->vval.v_string = vim_strsave((char_u *)result);
15129 }
15130}
15131
15132/*
15133 * "job_stop()" function
15134 */
15135 static void
15136f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15137{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015138 job_T *job = get_job_arg(&argvars[0]);
15139
15140 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015141 {
15142 char_u *arg;
15143
15144 if (argvars[1].v_type == VAR_UNKNOWN)
15145 arg = (char_u *)"";
15146 else
15147 {
15148 arg = get_tv_string_chk(&argvars[1]);
15149 if (arg == NULL)
15150 {
15151 EMSG(_(e_invarg));
15152 return;
15153 }
15154 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015155 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015156 rettv->vval.v_number = 0;
15157 else
15158 rettv->vval.v_number = 1;
15159 }
15160}
15161#endif
15162
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015164 * "join()" function
15165 */
15166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015167f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015168{
15169 garray_T ga;
15170 char_u *sep;
15171
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015172 if (argvars[0].v_type != VAR_LIST)
15173 {
15174 EMSG(_(e_listreq));
15175 return;
15176 }
15177 if (argvars[0].vval.v_list == NULL)
15178 return;
15179 if (argvars[1].v_type == VAR_UNKNOWN)
15180 sep = (char_u *)" ";
15181 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015182 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015183
15184 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015185
15186 if (sep != NULL)
15187 {
15188 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015189 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015190 ga_append(&ga, NUL);
15191 rettv->vval.v_string = (char_u *)ga.ga_data;
15192 }
15193 else
15194 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015195}
15196
15197/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015198 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015199 */
15200 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015201f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015202{
15203 js_read_T reader;
15204
15205 reader.js_buf = get_tv_string(&argvars[0]);
15206 reader.js_fill = NULL;
15207 reader.js_used = 0;
15208 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15209 EMSG(_(e_invarg));
15210}
15211
15212/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015213 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015214 */
15215 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015216f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015217{
15218 rettv->v_type = VAR_STRING;
15219 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15220}
15221
15222/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015223 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015224 */
15225 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015226f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015227{
15228 js_read_T reader;
15229
15230 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015231 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015232 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015233 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015234 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015235}
15236
15237/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015238 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015239 */
15240 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015241f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015242{
15243 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015244 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015245}
15246
15247/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015248 * "keys()" function
15249 */
15250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015251f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015252{
15253 dict_list(argvars, rettv, 0);
15254}
15255
15256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015257 * "last_buffer_nr()" function.
15258 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015260f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015261{
15262 int n = 0;
15263 buf_T *buf;
15264
15265 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15266 if (n < buf->b_fnum)
15267 n = buf->b_fnum;
15268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015269 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015270}
15271
15272/*
15273 * "len()" function
15274 */
15275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015276f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015277{
15278 switch (argvars[0].v_type)
15279 {
15280 case VAR_STRING:
15281 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015282 rettv->vval.v_number = (varnumber_T)STRLEN(
15283 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015284 break;
15285 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015286 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015287 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015288 case VAR_DICT:
15289 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15290 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015291 case VAR_UNKNOWN:
15292 case VAR_SPECIAL:
15293 case VAR_FLOAT:
15294 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015295 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015296 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015297 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015298 break;
15299 }
15300}
15301
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015302static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015303
15304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015305libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015306{
15307#ifdef FEAT_LIBCALL
15308 char_u *string_in;
15309 char_u **string_result;
15310 int nr_result;
15311#endif
15312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015313 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015314 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015315 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015316
15317 if (check_restricted() || check_secure())
15318 return;
15319
15320#ifdef FEAT_LIBCALL
15321 /* The first two args must be strings, otherwise its meaningless */
15322 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15323 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015324 string_in = NULL;
15325 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015326 string_in = argvars[2].vval.v_string;
15327 if (type == VAR_NUMBER)
15328 string_result = NULL;
15329 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015330 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015331 if (mch_libcall(argvars[0].vval.v_string,
15332 argvars[1].vval.v_string,
15333 string_in,
15334 argvars[2].vval.v_number,
15335 string_result,
15336 &nr_result) == OK
15337 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015338 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015339 }
15340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341}
15342
15343/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015344 * "libcall()" function
15345 */
15346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015347f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015348{
15349 libcall_common(argvars, rettv, VAR_STRING);
15350}
15351
15352/*
15353 * "libcallnr()" function
15354 */
15355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015356f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015357{
15358 libcall_common(argvars, rettv, VAR_NUMBER);
15359}
15360
15361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 * "line(string)" function
15363 */
15364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015365f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366{
15367 linenr_T lnum = 0;
15368 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015369 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015371 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372 if (fp != NULL)
15373 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015374 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375}
15376
15377/*
15378 * "line2byte(lnum)" function
15379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015381f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382{
15383#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015384 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385#else
15386 linenr_T lnum;
15387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015388 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015392 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15393 if (rettv->vval.v_number >= 0)
15394 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395#endif
15396}
15397
15398/*
15399 * "lispindent(lnum)" function
15400 */
15401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015402f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403{
15404#ifdef FEAT_LISP
15405 pos_T pos;
15406 linenr_T lnum;
15407
15408 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15411 {
15412 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 curwin->w_cursor = pos;
15415 }
15416 else
15417#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015418 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419}
15420
15421/*
15422 * "localtime()" function
15423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015425f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015427 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428}
15429
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015430static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431
15432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015433get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434{
15435 char_u *keys;
15436 char_u *which;
15437 char_u buf[NUMBUFLEN];
15438 char_u *keys_buf = NULL;
15439 char_u *rhs;
15440 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015441 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015442 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015443 mapblock_T *mp;
15444 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445
15446 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 rettv->v_type = VAR_STRING;
15448 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 if (*keys == NUL)
15452 return;
15453
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015454 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015456 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015457 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015458 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015459 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015460 if (argvars[3].v_type != VAR_UNKNOWN)
15461 get_dict = get_tv_number(&argvars[3]);
15462 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 else
15465 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015466 if (which == NULL)
15467 return;
15468
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469 mode = get_map_mode(&which, 0);
15470
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015471 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015472 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015474
15475 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015477 /* Return a string. */
15478 if (rhs != NULL)
15479 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480
Bram Moolenaarbd743252010-10-20 21:23:33 +020015481 }
15482 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15483 {
15484 /* Return a dictionary. */
15485 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15486 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15487 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488
Bram Moolenaarbd743252010-10-20 21:23:33 +020015489 dict_add_nr_str(dict, "lhs", 0L, lhs);
15490 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15491 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15492 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15493 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15494 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15495 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015496 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015497 dict_add_nr_str(dict, "mode", 0L, mapmode);
15498
15499 vim_free(lhs);
15500 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501 }
15502}
15503
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015504#ifdef FEAT_FLOAT
15505/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015506 * "log()" function
15507 */
15508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015509f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015510{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015511 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015512
15513 rettv->v_type = VAR_FLOAT;
15514 if (get_float_arg(argvars, &f) == OK)
15515 rettv->vval.v_float = log(f);
15516 else
15517 rettv->vval.v_float = 0.0;
15518}
15519
15520/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015521 * "log10()" function
15522 */
15523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015524f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015525{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015526 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015527
15528 rettv->v_type = VAR_FLOAT;
15529 if (get_float_arg(argvars, &f) == OK)
15530 rettv->vval.v_float = log10(f);
15531 else
15532 rettv->vval.v_float = 0.0;
15533}
15534#endif
15535
Bram Moolenaar1dced572012-04-05 16:54:08 +020015536#ifdef FEAT_LUA
15537/*
15538 * "luaeval()" function
15539 */
15540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015541f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015542{
15543 char_u *str;
15544 char_u buf[NUMBUFLEN];
15545
15546 str = get_tv_string_buf(&argvars[0], buf);
15547 do_luaeval(str, argvars + 1, rettv);
15548}
15549#endif
15550
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015552 * "map()" function
15553 */
15554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015555f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015556{
15557 filter_map(argvars, rettv, TRUE);
15558}
15559
15560/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015561 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 */
15563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015564f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015566 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015567}
15568
15569/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015570 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 */
15572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015573f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015575 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576}
15577
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015578static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579
15580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015581find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015583 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015584 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015585 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 char_u *pat;
15587 regmatch_T regmatch;
15588 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015589 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 char_u *save_cpo;
15591 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015592 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015593 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015594 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015595 list_T *l = NULL;
15596 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015597 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015598 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599
15600 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15601 save_cpo = p_cpo;
15602 p_cpo = (char_u *)"";
15603
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015604 rettv->vval.v_number = -1;
15605 if (type == 3)
15606 {
15607 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015608 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015609 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015610 }
15611 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015613 rettv->v_type = VAR_STRING;
15614 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015617 if (argvars[0].v_type == VAR_LIST)
15618 {
15619 if ((l = argvars[0].vval.v_list) == NULL)
15620 goto theend;
15621 li = l->lv_first;
15622 }
15623 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015624 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015625 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015626 len = (long)STRLEN(str);
15627 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015628
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015629 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15630 if (pat == NULL)
15631 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015632
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015633 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015635 int error = FALSE;
15636
15637 start = get_tv_number_chk(&argvars[2], &error);
15638 if (error)
15639 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015640 if (l != NULL)
15641 {
15642 li = list_find(l, start);
15643 if (li == NULL)
15644 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015645 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015646 }
15647 else
15648 {
15649 if (start < 0)
15650 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015651 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015652 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015653 /* When "count" argument is there ignore matches before "start",
15654 * otherwise skip part of the string. Differs when pattern is "^"
15655 * or "\<". */
15656 if (argvars[3].v_type != VAR_UNKNOWN)
15657 startcol = start;
15658 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015659 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015660 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015661 len -= start;
15662 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015663 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015664
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015665 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015666 nth = get_tv_number_chk(&argvars[3], &error);
15667 if (error)
15668 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 }
15670
15671 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15672 if (regmatch.regprog != NULL)
15673 {
15674 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015675
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015676 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015677 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015678 if (l != NULL)
15679 {
15680 if (li == NULL)
15681 {
15682 match = FALSE;
15683 break;
15684 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015685 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015686 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015687 if (str == NULL)
15688 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015689 }
15690
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015691 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015692
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015693 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015694 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015695 if (l == NULL && !match)
15696 break;
15697
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015698 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015699 if (l != NULL)
15700 {
15701 li = li->li_next;
15702 ++idx;
15703 }
15704 else
15705 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015706#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015707 startcol = (colnr_T)(regmatch.startp[0]
15708 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015709#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015710 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015711#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015712 if (startcol > (colnr_T)len
15713 || str + startcol <= regmatch.startp[0])
15714 {
15715 match = FALSE;
15716 break;
15717 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015718 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015719 }
15720
15721 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015723 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015724 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015725 int i;
15726
15727 /* return list with matched string and submatches */
15728 for (i = 0; i < NSUBEXP; ++i)
15729 {
15730 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015731 {
15732 if (list_append_string(rettv->vval.v_list,
15733 (char_u *)"", 0) == FAIL)
15734 break;
15735 }
15736 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015737 regmatch.startp[i],
15738 (int)(regmatch.endp[i] - regmatch.startp[i]))
15739 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015740 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015741 }
15742 }
15743 else if (type == 2)
15744 {
15745 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015746 if (l != NULL)
15747 copy_tv(&li->li_tv, rettv);
15748 else
15749 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015751 }
15752 else if (l != NULL)
15753 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 else
15755 {
15756 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015757 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 (varnumber_T)(regmatch.startp[0] - str);
15759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015760 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015762 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 }
15764 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015765 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766 }
15767
15768theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015769 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 p_cpo = save_cpo;
15771}
15772
15773/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015774 * "match()" function
15775 */
15776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015777f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015778{
15779 find_some_match(argvars, rettv, 1);
15780}
15781
15782/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015783 * "matchadd()" function
15784 */
15785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015786f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015787{
15788#ifdef FEAT_SEARCH_EXTRA
15789 char_u buf[NUMBUFLEN];
15790 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15791 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15792 int prio = 10; /* default priority */
15793 int id = -1;
15794 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015795 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015796
15797 rettv->vval.v_number = -1;
15798
15799 if (grp == NULL || pat == NULL)
15800 return;
15801 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015802 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015803 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015804 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015805 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015806 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015807 if (argvars[4].v_type != VAR_UNKNOWN)
15808 {
15809 if (argvars[4].v_type != VAR_DICT)
15810 {
15811 EMSG(_(e_dictreq));
15812 return;
15813 }
15814 if (dict_find(argvars[4].vval.v_dict,
15815 (char_u *)"conceal", -1) != NULL)
15816 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15817 (char_u *)"conceal", FALSE);
15818 }
15819 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015820 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015821 if (error == TRUE)
15822 return;
15823 if (id >= 1 && id <= 3)
15824 {
15825 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15826 return;
15827 }
15828
Bram Moolenaar6561d522015-07-21 15:48:27 +020015829 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15830 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015831#endif
15832}
15833
15834/*
15835 * "matchaddpos()" function
15836 */
15837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015838f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015839{
15840#ifdef FEAT_SEARCH_EXTRA
15841 char_u buf[NUMBUFLEN];
15842 char_u *group;
15843 int prio = 10;
15844 int id = -1;
15845 int error = FALSE;
15846 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015847 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015848
15849 rettv->vval.v_number = -1;
15850
15851 group = get_tv_string_buf_chk(&argvars[0], buf);
15852 if (group == NULL)
15853 return;
15854
15855 if (argvars[1].v_type != VAR_LIST)
15856 {
15857 EMSG2(_(e_listarg), "matchaddpos()");
15858 return;
15859 }
15860 l = argvars[1].vval.v_list;
15861 if (l == NULL)
15862 return;
15863
15864 if (argvars[2].v_type != VAR_UNKNOWN)
15865 {
15866 prio = get_tv_number_chk(&argvars[2], &error);
15867 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015868 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015869 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015870 if (argvars[4].v_type != VAR_UNKNOWN)
15871 {
15872 if (argvars[4].v_type != VAR_DICT)
15873 {
15874 EMSG(_(e_dictreq));
15875 return;
15876 }
15877 if (dict_find(argvars[4].vval.v_dict,
15878 (char_u *)"conceal", -1) != NULL)
15879 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15880 (char_u *)"conceal", FALSE);
15881 }
15882 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015883 }
15884 if (error == TRUE)
15885 return;
15886
15887 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15888 if (id == 1 || id == 2)
15889 {
15890 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15891 return;
15892 }
15893
Bram Moolenaar6561d522015-07-21 15:48:27 +020015894 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15895 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015896#endif
15897}
15898
15899/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015900 * "matcharg()" function
15901 */
15902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015903f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015904{
15905 if (rettv_list_alloc(rettv) == OK)
15906 {
15907#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015908 int id = get_tv_number(&argvars[0]);
15909 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015910
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015911 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015912 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015913 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15914 {
15915 list_append_string(rettv->vval.v_list,
15916 syn_id2name(m->hlg_id), -1);
15917 list_append_string(rettv->vval.v_list, m->pattern, -1);
15918 }
15919 else
15920 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015921 list_append_string(rettv->vval.v_list, NULL, -1);
15922 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015923 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015924 }
15925#endif
15926 }
15927}
15928
15929/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015930 * "matchdelete()" function
15931 */
15932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015933f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015934{
15935#ifdef FEAT_SEARCH_EXTRA
15936 rettv->vval.v_number = match_delete(curwin,
15937 (int)get_tv_number(&argvars[0]), TRUE);
15938#endif
15939}
15940
15941/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015942 * "matchend()" function
15943 */
15944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015945f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015946{
15947 find_some_match(argvars, rettv, 0);
15948}
15949
15950/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015951 * "matchlist()" function
15952 */
15953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015954f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015955{
15956 find_some_match(argvars, rettv, 3);
15957}
15958
15959/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015960 * "matchstr()" function
15961 */
15962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015963f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015964{
15965 find_some_match(argvars, rettv, 2);
15966}
15967
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015968static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015969
15970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015971max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015972{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015973 long n = 0;
15974 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015975 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015976
15977 if (argvars[0].v_type == VAR_LIST)
15978 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015979 list_T *l;
15980 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015981
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015982 l = argvars[0].vval.v_list;
15983 if (l != NULL)
15984 {
15985 li = l->lv_first;
15986 if (li != NULL)
15987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015988 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015989 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015990 {
15991 li = li->li_next;
15992 if (li == NULL)
15993 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015994 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015995 if (domax ? i > n : i < n)
15996 n = i;
15997 }
15998 }
15999 }
16000 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016001 else if (argvars[0].v_type == VAR_DICT)
16002 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016003 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016004 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016005 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016006 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016007
16008 d = argvars[0].vval.v_dict;
16009 if (d != NULL)
16010 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016011 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016012 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016013 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016014 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016015 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016016 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016017 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016018 if (first)
16019 {
16020 n = i;
16021 first = FALSE;
16022 }
16023 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016024 n = i;
16025 }
16026 }
16027 }
16028 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016029 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016030 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016031 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016032}
16033
16034/*
16035 * "max()" function
16036 */
16037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016038f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016039{
16040 max_min(argvars, rettv, TRUE);
16041}
16042
16043/*
16044 * "min()" function
16045 */
16046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016047f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016048{
16049 max_min(argvars, rettv, FALSE);
16050}
16051
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016052static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016053
16054/*
16055 * Create the directory in which "dir" is located, and higher levels when
16056 * needed.
16057 */
16058 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016059mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016060{
16061 char_u *p;
16062 char_u *updir;
16063 int r = FAIL;
16064
16065 /* Get end of directory name in "dir".
16066 * We're done when it's "/" or "c:/". */
16067 p = gettail_sep(dir);
16068 if (p <= get_past_head(dir))
16069 return OK;
16070
16071 /* If the directory exists we're done. Otherwise: create it.*/
16072 updir = vim_strnsave(dir, (int)(p - dir));
16073 if (updir == NULL)
16074 return FAIL;
16075 if (mch_isdir(updir))
16076 r = OK;
16077 else if (mkdir_recurse(updir, prot) == OK)
16078 r = vim_mkdir_emsg(updir, prot);
16079 vim_free(updir);
16080 return r;
16081}
16082
16083#ifdef vim_mkdir
16084/*
16085 * "mkdir()" function
16086 */
16087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016088f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016089{
16090 char_u *dir;
16091 char_u buf[NUMBUFLEN];
16092 int prot = 0755;
16093
16094 rettv->vval.v_number = FAIL;
16095 if (check_restricted() || check_secure())
16096 return;
16097
16098 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016099 if (*dir == NUL)
16100 rettv->vval.v_number = FAIL;
16101 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016102 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016103 if (*gettail(dir) == NUL)
16104 /* remove trailing slashes */
16105 *gettail_sep(dir) = NUL;
16106
16107 if (argvars[1].v_type != VAR_UNKNOWN)
16108 {
16109 if (argvars[2].v_type != VAR_UNKNOWN)
16110 prot = get_tv_number_chk(&argvars[2], NULL);
16111 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16112 mkdir_recurse(dir, prot);
16113 }
16114 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016115 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016116}
16117#endif
16118
Bram Moolenaar0d660222005-01-07 21:51:51 +000016119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 * "mode()" function
16121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016123f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016125 char_u buf[3];
16126
16127 buf[1] = NUL;
16128 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 if (VIsual_active)
16131 {
16132 if (VIsual_select)
16133 buf[0] = VIsual_mode + 's' - 'v';
16134 else
16135 buf[0] = VIsual_mode;
16136 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016137 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016138 || State == CONFIRM)
16139 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016141 if (State == ASKMORE)
16142 buf[1] = 'm';
16143 else if (State == CONFIRM)
16144 buf[1] = '?';
16145 }
16146 else if (State == EXTERNCMD)
16147 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 else if (State & INSERT)
16149 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016150#ifdef FEAT_VREPLACE
16151 if (State & VREPLACE_FLAG)
16152 {
16153 buf[0] = 'R';
16154 buf[1] = 'v';
16155 }
16156 else
16157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 if (State & REPLACE_FLAG)
16159 buf[0] = 'R';
16160 else
16161 buf[0] = 'i';
16162 }
16163 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016164 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016166 if (exmode_active)
16167 buf[1] = 'v';
16168 }
16169 else if (exmode_active)
16170 {
16171 buf[0] = 'c';
16172 buf[1] = 'e';
16173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016177 if (finish_op)
16178 buf[1] = 'o';
16179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016181 /* Clear out the minor mode when the argument is not a non-zero number or
16182 * non-empty string. */
16183 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016184 buf[1] = NUL;
16185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016186 rettv->vval.v_string = vim_strsave(buf);
16187 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188}
16189
Bram Moolenaar429fa852013-04-15 12:27:36 +020016190#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016191/*
16192 * "mzeval()" function
16193 */
16194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016195f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016196{
16197 char_u *str;
16198 char_u buf[NUMBUFLEN];
16199
16200 str = get_tv_string_buf(&argvars[0], buf);
16201 do_mzeval(str, rettv);
16202}
Bram Moolenaar75676462013-01-30 14:55:42 +010016203
16204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016205mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016206{
16207 typval_T argvars[3];
16208
16209 argvars[0].v_type = VAR_STRING;
16210 argvars[0].vval.v_string = name;
16211 copy_tv(args, &argvars[1]);
16212 argvars[2].v_type = VAR_UNKNOWN;
16213 f_call(argvars, rettv);
16214 clear_tv(&argvars[1]);
16215}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016216#endif
16217
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219 * "nextnonblank()" function
16220 */
16221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016222f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016223{
16224 linenr_T lnum;
16225
16226 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016228 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016229 {
16230 lnum = 0;
16231 break;
16232 }
16233 if (*skipwhite(ml_get(lnum)) != NUL)
16234 break;
16235 }
16236 rettv->vval.v_number = lnum;
16237}
16238
16239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 * "nr2char()" function
16241 */
16242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016243f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244{
16245 char_u buf[NUMBUFLEN];
16246
16247#ifdef FEAT_MBYTE
16248 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016249 {
16250 int utf8 = 0;
16251
16252 if (argvars[1].v_type != VAR_UNKNOWN)
16253 utf8 = get_tv_number_chk(&argvars[1], NULL);
16254 if (utf8)
16255 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16256 else
16257 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 else
16260#endif
16261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016262 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 buf[1] = NUL;
16264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016265 rettv->v_type = VAR_STRING;
16266 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016267}
16268
16269/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016270 * "or(expr, expr)" function
16271 */
16272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016273f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016274{
16275 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16276 | get_tv_number_chk(&argvars[1], NULL);
16277}
16278
16279/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016280 * "pathshorten()" function
16281 */
16282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016283f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016284{
16285 char_u *p;
16286
16287 rettv->v_type = VAR_STRING;
16288 p = get_tv_string_chk(&argvars[0]);
16289 if (p == NULL)
16290 rettv->vval.v_string = NULL;
16291 else
16292 {
16293 p = vim_strsave(p);
16294 rettv->vval.v_string = p;
16295 if (p != NULL)
16296 shorten_dir(p);
16297 }
16298}
16299
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016300#ifdef FEAT_PERL
16301/*
16302 * "perleval()" function
16303 */
16304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016305f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016306{
16307 char_u *str;
16308 char_u buf[NUMBUFLEN];
16309
16310 str = get_tv_string_buf(&argvars[0], buf);
16311 do_perleval(str, rettv);
16312}
16313#endif
16314
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016315#ifdef FEAT_FLOAT
16316/*
16317 * "pow()" function
16318 */
16319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016320f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016321{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016322 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016323
16324 rettv->v_type = VAR_FLOAT;
16325 if (get_float_arg(argvars, &fx) == OK
16326 && get_float_arg(&argvars[1], &fy) == OK)
16327 rettv->vval.v_float = pow(fx, fy);
16328 else
16329 rettv->vval.v_float = 0.0;
16330}
16331#endif
16332
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016333/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016334 * "prevnonblank()" function
16335 */
16336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016337f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016338{
16339 linenr_T lnum;
16340
16341 lnum = get_tv_lnum(argvars);
16342 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16343 lnum = 0;
16344 else
16345 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16346 --lnum;
16347 rettv->vval.v_number = lnum;
16348}
16349
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016350/* This dummy va_list is here because:
16351 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16352 * - locally in the function results in a "used before set" warning
16353 * - using va_start() to initialize it gives "function with fixed args" error */
16354static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016355
Bram Moolenaar8c711452005-01-14 21:53:12 +000016356/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016357 * "printf()" function
16358 */
16359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016360f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016361{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016362 char_u buf[NUMBUFLEN];
16363 int len;
16364 char_u *s;
16365 int saved_did_emsg = did_emsg;
16366 char *fmt;
16367
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016368 rettv->v_type = VAR_STRING;
16369 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016370
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016371 /* Get the required length, allocate the buffer and do it for real. */
16372 did_emsg = FALSE;
16373 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16374 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16375 if (!did_emsg)
16376 {
16377 s = alloc(len + 1);
16378 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016379 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016380 rettv->vval.v_string = s;
16381 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016382 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016383 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016384 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016385}
16386
16387/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016388 * "pumvisible()" function
16389 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016391f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016392{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016393#ifdef FEAT_INS_EXPAND
16394 if (pum_visible())
16395 rettv->vval.v_number = 1;
16396#endif
16397}
16398
Bram Moolenaardb913952012-06-29 12:54:53 +020016399#ifdef FEAT_PYTHON3
16400/*
16401 * "py3eval()" function
16402 */
16403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016404f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016405{
16406 char_u *str;
16407 char_u buf[NUMBUFLEN];
16408
16409 str = get_tv_string_buf(&argvars[0], buf);
16410 do_py3eval(str, rettv);
16411}
16412#endif
16413
16414#ifdef FEAT_PYTHON
16415/*
16416 * "pyeval()" function
16417 */
16418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016419f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016420{
16421 char_u *str;
16422 char_u buf[NUMBUFLEN];
16423
16424 str = get_tv_string_buf(&argvars[0], buf);
16425 do_pyeval(str, rettv);
16426}
16427#endif
16428
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016429/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016430 * "range()" function
16431 */
16432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016433f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016434{
16435 long start;
16436 long end;
16437 long stride = 1;
16438 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016439 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016442 if (argvars[1].v_type == VAR_UNKNOWN)
16443 {
16444 end = start - 1;
16445 start = 0;
16446 }
16447 else
16448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016449 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016450 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016451 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016452 }
16453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016454 if (error)
16455 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016456 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016457 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016458 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016459 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016460 else
16461 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016462 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016463 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016464 if (list_append_number(rettv->vval.v_list,
16465 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016466 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016467 }
16468}
16469
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016470/*
16471 * "readfile()" function
16472 */
16473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016474f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016475{
16476 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016477 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016478 char_u *fname;
16479 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016480 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16481 int io_size = sizeof(buf);
16482 int readlen; /* size of last fread() */
16483 char_u *prev = NULL; /* previously read bytes, if any */
16484 long prevlen = 0; /* length of data in prev */
16485 long prevsize = 0; /* size of prev buffer */
16486 long maxline = MAXLNUM;
16487 long cnt = 0;
16488 char_u *p; /* position in buf */
16489 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016490
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016491 if (argvars[1].v_type != VAR_UNKNOWN)
16492 {
16493 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16494 binary = TRUE;
16495 if (argvars[2].v_type != VAR_UNKNOWN)
16496 maxline = get_tv_number(&argvars[2]);
16497 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016498
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016499 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016500 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016501
16502 /* Always open the file in binary mode, library functions have a mind of
16503 * their own about CR-LF conversion. */
16504 fname = get_tv_string(&argvars[0]);
16505 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16506 {
16507 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16508 return;
16509 }
16510
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016511 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016512 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016513 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016514
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016515 /* This for loop processes what was read, but is also entered at end
16516 * of file so that either:
16517 * - an incomplete line gets written
16518 * - a "binary" file gets an empty line at the end if it ends in a
16519 * newline. */
16520 for (p = buf, start = buf;
16521 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16522 ++p)
16523 {
16524 if (*p == '\n' || readlen <= 0)
16525 {
16526 listitem_T *li;
16527 char_u *s = NULL;
16528 long_u len = p - start;
16529
16530 /* Finished a line. Remove CRs before NL. */
16531 if (readlen > 0 && !binary)
16532 {
16533 while (len > 0 && start[len - 1] == '\r')
16534 --len;
16535 /* removal may cross back to the "prev" string */
16536 if (len == 0)
16537 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16538 --prevlen;
16539 }
16540 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016541 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016542 else
16543 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016544 /* Change "prev" buffer to be the right size. This way
16545 * the bytes are only copied once, and very long lines are
16546 * allocated only once. */
16547 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016548 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016549 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016550 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016551 prev = NULL; /* the list will own the string */
16552 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016553 }
16554 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016555 if (s == NULL)
16556 {
16557 do_outofmem_msg((long_u) prevlen + len + 1);
16558 failed = TRUE;
16559 break;
16560 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016561
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016562 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016563 {
16564 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016565 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016566 break;
16567 }
16568 li->li_tv.v_type = VAR_STRING;
16569 li->li_tv.v_lock = 0;
16570 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016571 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016572
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016573 start = p + 1; /* step over newline */
16574 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016575 break;
16576 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016577 else if (*p == NUL)
16578 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016579#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016580 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16581 * when finding the BF and check the previous two bytes. */
16582 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016583 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016584 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16585 * + 1, these may be in the "prev" string. */
16586 char_u back1 = p >= buf + 1 ? p[-1]
16587 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16588 char_u back2 = p >= buf + 2 ? p[-2]
16589 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16590 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016591
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016592 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016593 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016594 char_u *dest = p - 2;
16595
16596 /* Usually a BOM is at the beginning of a file, and so at
16597 * the beginning of a line; then we can just step over it.
16598 */
16599 if (start == dest)
16600 start = p + 1;
16601 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016602 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016603 /* have to shuffle buf to close gap */
16604 int adjust_prevlen = 0;
16605
16606 if (dest < buf)
16607 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016608 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016609 dest = buf;
16610 }
16611 if (readlen > p - buf + 1)
16612 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16613 readlen -= 3 - adjust_prevlen;
16614 prevlen -= adjust_prevlen;
16615 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016616 }
16617 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016618 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016619#endif
16620 } /* for */
16621
16622 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16623 break;
16624 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016625 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016626 /* There's part of a line in buf, store it in "prev". */
16627 if (p - start + prevlen >= prevsize)
16628 {
16629 /* need bigger "prev" buffer */
16630 char_u *newprev;
16631
16632 /* A common use case is ordinary text files and "prev" gets a
16633 * fragment of a line, so the first allocation is made
16634 * small, to avoid repeatedly 'allocing' large and
16635 * 'reallocing' small. */
16636 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016637 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016638 else
16639 {
16640 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016641 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016642 prevsize = grow50pc > growmin ? grow50pc : growmin;
16643 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016644 newprev = prev == NULL ? alloc(prevsize)
16645 : vim_realloc(prev, prevsize);
16646 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016647 {
16648 do_outofmem_msg((long_u)prevsize);
16649 failed = TRUE;
16650 break;
16651 }
16652 prev = newprev;
16653 }
16654 /* Add the line part to end of "prev". */
16655 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016656 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016657 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016658 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016659
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016660 /*
16661 * For a negative line count use only the lines at the end of the file,
16662 * free the rest.
16663 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016664 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016665 while (cnt > -maxline)
16666 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016667 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016668 --cnt;
16669 }
16670
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016671 if (failed)
16672 {
16673 list_free(rettv->vval.v_list, TRUE);
16674 /* readfile doc says an empty list is returned on error */
16675 rettv->vval.v_list = list_alloc();
16676 }
16677
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016678 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016679 fclose(fd);
16680}
16681
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016682#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016683static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016684
16685/*
16686 * Convert a List to proftime_T.
16687 * Return FAIL when there is something wrong.
16688 */
16689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016690list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016691{
16692 long n1, n2;
16693 int error = FALSE;
16694
16695 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16696 || arg->vval.v_list->lv_len != 2)
16697 return FAIL;
16698 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16699 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16700# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016701 tm->HighPart = n1;
16702 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016703# else
16704 tm->tv_sec = n1;
16705 tm->tv_usec = n2;
16706# endif
16707 return error ? FAIL : OK;
16708}
16709#endif /* FEAT_RELTIME */
16710
16711/*
16712 * "reltime()" function
16713 */
16714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016715f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016716{
16717#ifdef FEAT_RELTIME
16718 proftime_T res;
16719 proftime_T start;
16720
16721 if (argvars[0].v_type == VAR_UNKNOWN)
16722 {
16723 /* No arguments: get current time. */
16724 profile_start(&res);
16725 }
16726 else if (argvars[1].v_type == VAR_UNKNOWN)
16727 {
16728 if (list2proftime(&argvars[0], &res) == FAIL)
16729 return;
16730 profile_end(&res);
16731 }
16732 else
16733 {
16734 /* Two arguments: compute the difference. */
16735 if (list2proftime(&argvars[0], &start) == FAIL
16736 || list2proftime(&argvars[1], &res) == FAIL)
16737 return;
16738 profile_sub(&res, &start);
16739 }
16740
16741 if (rettv_list_alloc(rettv) == OK)
16742 {
16743 long n1, n2;
16744
16745# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016746 n1 = res.HighPart;
16747 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016748# else
16749 n1 = res.tv_sec;
16750 n2 = res.tv_usec;
16751# endif
16752 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16753 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16754 }
16755#endif
16756}
16757
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016758#ifdef FEAT_FLOAT
16759/*
16760 * "reltimefloat()" function
16761 */
16762 static void
16763f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16764{
16765# ifdef FEAT_RELTIME
16766 proftime_T tm;
16767# endif
16768
16769 rettv->v_type = VAR_FLOAT;
16770 rettv->vval.v_float = 0;
16771# ifdef FEAT_RELTIME
16772 if (list2proftime(&argvars[0], &tm) == OK)
16773 rettv->vval.v_float = profile_float(&tm);
16774# endif
16775}
16776#endif
16777
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016778/*
16779 * "reltimestr()" function
16780 */
16781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016782f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016783{
16784#ifdef FEAT_RELTIME
16785 proftime_T tm;
16786#endif
16787
16788 rettv->v_type = VAR_STRING;
16789 rettv->vval.v_string = NULL;
16790#ifdef FEAT_RELTIME
16791 if (list2proftime(&argvars[0], &tm) == OK)
16792 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16793#endif
16794}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016795
Bram Moolenaar0d660222005-01-07 21:51:51 +000016796#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016797static void make_connection(void);
16798static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016799
16800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016801make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016802{
16803 if (X_DISPLAY == NULL
16804# ifdef FEAT_GUI
16805 && !gui.in_use
16806# endif
16807 )
16808 {
16809 x_force_connect = TRUE;
16810 setup_term_clip();
16811 x_force_connect = FALSE;
16812 }
16813}
16814
16815 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016816check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016817{
16818 make_connection();
16819 if (X_DISPLAY == NULL)
16820 {
16821 EMSG(_("E240: No connection to Vim server"));
16822 return FAIL;
16823 }
16824 return OK;
16825}
16826#endif
16827
16828#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016829static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016830
16831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016832remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016833{
16834 char_u *server_name;
16835 char_u *keys;
16836 char_u *r = NULL;
16837 char_u buf[NUMBUFLEN];
16838# ifdef WIN32
16839 HWND w;
16840# else
16841 Window w;
16842# endif
16843
16844 if (check_restricted() || check_secure())
16845 return;
16846
16847# ifdef FEAT_X11
16848 if (check_connection() == FAIL)
16849 return;
16850# endif
16851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016852 server_name = get_tv_string_chk(&argvars[0]);
16853 if (server_name == NULL)
16854 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016855 keys = get_tv_string_buf(&argvars[1], buf);
16856# ifdef WIN32
16857 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16858# else
16859 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16860 < 0)
16861# endif
16862 {
16863 if (r != NULL)
16864 EMSG(r); /* sending worked but evaluation failed */
16865 else
16866 EMSG2(_("E241: Unable to send to %s"), server_name);
16867 return;
16868 }
16869
16870 rettv->vval.v_string = r;
16871
16872 if (argvars[2].v_type != VAR_UNKNOWN)
16873 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016874 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016875 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016876 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016878 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016879 v.di_tv.v_type = VAR_STRING;
16880 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016881 idvar = get_tv_string_chk(&argvars[2]);
16882 if (idvar != NULL)
16883 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016884 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016885 }
16886}
16887#endif
16888
16889/*
16890 * "remote_expr()" function
16891 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016893f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016894{
16895 rettv->v_type = VAR_STRING;
16896 rettv->vval.v_string = NULL;
16897#ifdef FEAT_CLIENTSERVER
16898 remote_common(argvars, rettv, TRUE);
16899#endif
16900}
16901
16902/*
16903 * "remote_foreground()" function
16904 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016906f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016908#ifdef FEAT_CLIENTSERVER
16909# ifdef WIN32
16910 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016911 {
16912 char_u *server_name = get_tv_string_chk(&argvars[0]);
16913
16914 if (server_name != NULL)
16915 serverForeground(server_name);
16916 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016917# else
16918 /* Send a foreground() expression to the server. */
16919 argvars[1].v_type = VAR_STRING;
16920 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16921 argvars[2].v_type = VAR_UNKNOWN;
16922 remote_common(argvars, rettv, TRUE);
16923 vim_free(argvars[1].vval.v_string);
16924# endif
16925#endif
16926}
16927
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016929f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930{
16931#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016932 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 char_u *s = NULL;
16934# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016935 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016937 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016938
16939 if (check_restricted() || check_secure())
16940 {
16941 rettv->vval.v_number = -1;
16942 return;
16943 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016944 serverid = get_tv_string_chk(&argvars[0]);
16945 if (serverid == NULL)
16946 {
16947 rettv->vval.v_number = -1;
16948 return; /* type error; errmsg already given */
16949 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016951 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 if (n == 0)
16953 rettv->vval.v_number = -1;
16954 else
16955 {
16956 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16957 rettv->vval.v_number = (s != NULL);
16958 }
16959# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960 if (check_connection() == FAIL)
16961 return;
16962
16963 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016964 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965# endif
16966
16967 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16968 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016969 char_u *retvar;
16970
Bram Moolenaar33570922005-01-25 22:26:29 +000016971 v.di_tv.v_type = VAR_STRING;
16972 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016973 retvar = get_tv_string_chk(&argvars[1]);
16974 if (retvar != NULL)
16975 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016976 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977 }
16978#else
16979 rettv->vval.v_number = -1;
16980#endif
16981}
16982
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016984f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985{
16986 char_u *r = NULL;
16987
16988#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016989 char_u *serverid = get_tv_string_chk(&argvars[0]);
16990
16991 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 {
16993# ifdef WIN32
16994 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016995 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016996
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016997 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 if (n != 0)
16999 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17000 if (r == NULL)
17001# else
17002 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017003 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004# endif
17005 EMSG(_("E277: Unable to read a server reply"));
17006 }
17007#endif
17008 rettv->v_type = VAR_STRING;
17009 rettv->vval.v_string = r;
17010}
17011
17012/*
17013 * "remote_send()" function
17014 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017016f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017{
17018 rettv->v_type = VAR_STRING;
17019 rettv->vval.v_string = NULL;
17020#ifdef FEAT_CLIENTSERVER
17021 remote_common(argvars, rettv, FALSE);
17022#endif
17023}
17024
17025/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017026 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017027 */
17028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017029f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017030{
Bram Moolenaar33570922005-01-25 22:26:29 +000017031 list_T *l;
17032 listitem_T *item, *item2;
17033 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017034 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017035 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017036 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017037 dict_T *d;
17038 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017039 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017040
Bram Moolenaar8c711452005-01-14 21:53:12 +000017041 if (argvars[0].v_type == VAR_DICT)
17042 {
17043 if (argvars[2].v_type != VAR_UNKNOWN)
17044 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017045 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017046 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017048 key = get_tv_string_chk(&argvars[1]);
17049 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017051 di = dict_find(d, key, -1);
17052 if (di == NULL)
17053 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017054 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17055 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017056 {
17057 *rettv = di->di_tv;
17058 init_tv(&di->di_tv);
17059 dictitem_remove(d, di);
17060 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017062 }
17063 }
17064 else if (argvars[0].v_type != VAR_LIST)
17065 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017066 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017067 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017069 int error = FALSE;
17070
17071 idx = get_tv_number_chk(&argvars[1], &error);
17072 if (error)
17073 ; /* type error: do nothing, errmsg already given */
17074 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017075 EMSGN(_(e_listidx), idx);
17076 else
17077 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017078 if (argvars[2].v_type == VAR_UNKNOWN)
17079 {
17080 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017081 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017082 *rettv = item->li_tv;
17083 vim_free(item);
17084 }
17085 else
17086 {
17087 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017088 end = get_tv_number_chk(&argvars[2], &error);
17089 if (error)
17090 ; /* type error: do nothing */
17091 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017092 EMSGN(_(e_listidx), end);
17093 else
17094 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017095 int cnt = 0;
17096
17097 for (li = item; li != NULL; li = li->li_next)
17098 {
17099 ++cnt;
17100 if (li == item2)
17101 break;
17102 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017103 if (li == NULL) /* didn't find "item2" after "item" */
17104 EMSG(_(e_invrange));
17105 else
17106 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017107 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017108 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017109 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017110 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017111 l->lv_first = item;
17112 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017113 item->li_prev = NULL;
17114 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017115 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017116 }
17117 }
17118 }
17119 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017120 }
17121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122}
17123
17124/*
17125 * "rename({from}, {to})" function
17126 */
17127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017128f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129{
17130 char_u buf[NUMBUFLEN];
17131
17132 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017135 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17136 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137}
17138
17139/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017140 * "repeat()" function
17141 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017143f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017144{
17145 char_u *p;
17146 int n;
17147 int slen;
17148 int len;
17149 char_u *r;
17150 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017151
17152 n = get_tv_number(&argvars[1]);
17153 if (argvars[0].v_type == VAR_LIST)
17154 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017155 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017156 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017157 if (list_extend(rettv->vval.v_list,
17158 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017159 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017160 }
17161 else
17162 {
17163 p = get_tv_string(&argvars[0]);
17164 rettv->v_type = VAR_STRING;
17165 rettv->vval.v_string = NULL;
17166
17167 slen = (int)STRLEN(p);
17168 len = slen * n;
17169 if (len <= 0)
17170 return;
17171
17172 r = alloc(len + 1);
17173 if (r != NULL)
17174 {
17175 for (i = 0; i < n; i++)
17176 mch_memmove(r + i * slen, p, (size_t)slen);
17177 r[len] = NUL;
17178 }
17179
17180 rettv->vval.v_string = r;
17181 }
17182}
17183
17184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 * "resolve()" function
17186 */
17187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017188f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189{
17190 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017191#ifdef HAVE_READLINK
17192 char_u *buf = NULL;
17193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017195 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196#ifdef FEAT_SHORTCUT
17197 {
17198 char_u *v = NULL;
17199
17200 v = mch_resolve_shortcut(p);
17201 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017202 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017204 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205 }
17206#else
17207# ifdef HAVE_READLINK
17208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 char_u *cpy;
17210 int len;
17211 char_u *remain = NULL;
17212 char_u *q;
17213 int is_relative_to_current = FALSE;
17214 int has_trailing_pathsep = FALSE;
17215 int limit = 100;
17216
17217 p = vim_strsave(p);
17218
17219 if (p[0] == '.' && (vim_ispathsep(p[1])
17220 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17221 is_relative_to_current = TRUE;
17222
17223 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017224 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017226 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017227 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229
17230 q = getnextcomp(p);
17231 if (*q != NUL)
17232 {
17233 /* Separate the first path component in "p", and keep the
17234 * remainder (beginning with the path separator). */
17235 remain = vim_strsave(q - 1);
17236 q[-1] = NUL;
17237 }
17238
Bram Moolenaard9462e32011-04-11 21:35:11 +020017239 buf = alloc(MAXPATHL + 1);
17240 if (buf == NULL)
17241 goto fail;
17242
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 for (;;)
17244 {
17245 for (;;)
17246 {
17247 len = readlink((char *)p, (char *)buf, MAXPATHL);
17248 if (len <= 0)
17249 break;
17250 buf[len] = NUL;
17251
17252 if (limit-- == 0)
17253 {
17254 vim_free(p);
17255 vim_free(remain);
17256 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017257 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 goto fail;
17259 }
17260
17261 /* Ensure that the result will have a trailing path separator
17262 * if the argument has one. */
17263 if (remain == NULL && has_trailing_pathsep)
17264 add_pathsep(buf);
17265
17266 /* Separate the first path component in the link value and
17267 * concatenate the remainders. */
17268 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17269 if (*q != NUL)
17270 {
17271 if (remain == NULL)
17272 remain = vim_strsave(q - 1);
17273 else
17274 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017275 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 if (cpy != NULL)
17277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017278 vim_free(remain);
17279 remain = cpy;
17280 }
17281 }
17282 q[-1] = NUL;
17283 }
17284
17285 q = gettail(p);
17286 if (q > p && *q == NUL)
17287 {
17288 /* Ignore trailing path separator. */
17289 q[-1] = NUL;
17290 q = gettail(p);
17291 }
17292 if (q > p && !mch_isFullName(buf))
17293 {
17294 /* symlink is relative to directory of argument */
17295 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17296 if (cpy != NULL)
17297 {
17298 STRCPY(cpy, p);
17299 STRCPY(gettail(cpy), buf);
17300 vim_free(p);
17301 p = cpy;
17302 }
17303 }
17304 else
17305 {
17306 vim_free(p);
17307 p = vim_strsave(buf);
17308 }
17309 }
17310
17311 if (remain == NULL)
17312 break;
17313
17314 /* Append the first path component of "remain" to "p". */
17315 q = getnextcomp(remain + 1);
17316 len = q - remain - (*q != NUL);
17317 cpy = vim_strnsave(p, STRLEN(p) + len);
17318 if (cpy != NULL)
17319 {
17320 STRNCAT(cpy, remain, len);
17321 vim_free(p);
17322 p = cpy;
17323 }
17324 /* Shorten "remain". */
17325 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017326 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327 else
17328 {
17329 vim_free(remain);
17330 remain = NULL;
17331 }
17332 }
17333
17334 /* If the result is a relative path name, make it explicitly relative to
17335 * the current directory if and only if the argument had this form. */
17336 if (!vim_ispathsep(*p))
17337 {
17338 if (is_relative_to_current
17339 && *p != NUL
17340 && !(p[0] == '.'
17341 && (p[1] == NUL
17342 || vim_ispathsep(p[1])
17343 || (p[1] == '.'
17344 && (p[2] == NUL
17345 || vim_ispathsep(p[2]))))))
17346 {
17347 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017348 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 if (cpy != NULL)
17350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 vim_free(p);
17352 p = cpy;
17353 }
17354 }
17355 else if (!is_relative_to_current)
17356 {
17357 /* Strip leading "./". */
17358 q = p;
17359 while (q[0] == '.' && vim_ispathsep(q[1]))
17360 q += 2;
17361 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017362 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 }
17364 }
17365
17366 /* Ensure that the result will have no trailing path separator
17367 * if the argument had none. But keep "/" or "//". */
17368 if (!has_trailing_pathsep)
17369 {
17370 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017371 if (after_pathsep(p, q))
17372 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 }
17374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017375 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 }
17377# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017378 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379# endif
17380#endif
17381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017382 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383
17384#ifdef HAVE_READLINK
17385fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017386 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389}
17390
17391/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017392 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 */
17394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017395f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396{
Bram Moolenaar33570922005-01-25 22:26:29 +000017397 list_T *l;
17398 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399
Bram Moolenaar0d660222005-01-07 21:51:51 +000017400 if (argvars[0].v_type != VAR_LIST)
17401 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017402 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017403 && !tv_check_lock(l->lv_lock,
17404 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017405 {
17406 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017407 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017408 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017409 while (li != NULL)
17410 {
17411 ni = li->li_prev;
17412 list_append(l, li);
17413 li = ni;
17414 }
17415 rettv->vval.v_list = l;
17416 rettv->v_type = VAR_LIST;
17417 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017418 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420}
17421
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017422#define SP_NOMOVE 0x01 /* don't move cursor */
17423#define SP_REPEAT 0x02 /* repeat to find outer pair */
17424#define SP_RETCOUNT 0x04 /* return matchcount */
17425#define SP_SETPCMARK 0x08 /* set previous context mark */
17426#define SP_START 0x10 /* accept match at start position */
17427#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17428#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017429#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017430
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017431static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017432
17433/*
17434 * Get flags for a search function.
17435 * Possibly sets "p_ws".
17436 * Returns BACKWARD, FORWARD or zero (for an error).
17437 */
17438 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017439get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017440{
17441 int dir = FORWARD;
17442 char_u *flags;
17443 char_u nbuf[NUMBUFLEN];
17444 int mask;
17445
17446 if (varp->v_type != VAR_UNKNOWN)
17447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017448 flags = get_tv_string_buf_chk(varp, nbuf);
17449 if (flags == NULL)
17450 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017451 while (*flags != NUL)
17452 {
17453 switch (*flags)
17454 {
17455 case 'b': dir = BACKWARD; break;
17456 case 'w': p_ws = TRUE; break;
17457 case 'W': p_ws = FALSE; break;
17458 default: mask = 0;
17459 if (flagsp != NULL)
17460 switch (*flags)
17461 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017462 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017463 case 'e': mask = SP_END; break;
17464 case 'm': mask = SP_RETCOUNT; break;
17465 case 'n': mask = SP_NOMOVE; break;
17466 case 'p': mask = SP_SUBPAT; break;
17467 case 'r': mask = SP_REPEAT; break;
17468 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017469 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017470 }
17471 if (mask == 0)
17472 {
17473 EMSG2(_(e_invarg2), flags);
17474 dir = 0;
17475 }
17476 else
17477 *flagsp |= mask;
17478 }
17479 if (dir == 0)
17480 break;
17481 ++flags;
17482 }
17483 }
17484 return dir;
17485}
17486
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017488 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017490 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017491search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017493 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 char_u *pat;
17495 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017496 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 int save_p_ws = p_ws;
17498 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017499 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017500 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017501 proftime_T tm;
17502#ifdef FEAT_RELTIME
17503 long time_limit = 0;
17504#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017505 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017506 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017508 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017509 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017510 if (dir == 0)
17511 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017512 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017513 if (flags & SP_START)
17514 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017515 if (flags & SP_END)
17516 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017517 if (flags & SP_COLUMN)
17518 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017519
Bram Moolenaar76929292008-01-06 19:07:36 +000017520 /* Optional arguments: line number to stop searching and timeout. */
17521 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017522 {
17523 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17524 if (lnum_stop < 0)
17525 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017526#ifdef FEAT_RELTIME
17527 if (argvars[3].v_type != VAR_UNKNOWN)
17528 {
17529 time_limit = get_tv_number_chk(&argvars[3], NULL);
17530 if (time_limit < 0)
17531 goto theend;
17532 }
17533#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017534 }
17535
Bram Moolenaar76929292008-01-06 19:07:36 +000017536#ifdef FEAT_RELTIME
17537 /* Set the time limit, if there is one. */
17538 profile_setlimit(time_limit, &tm);
17539#endif
17540
Bram Moolenaar231334e2005-07-25 20:46:57 +000017541 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017542 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017543 * Check to make sure only those flags are set.
17544 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17545 * flags cannot be set. Check for that condition also.
17546 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017547 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017548 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017550 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017551 goto theend;
17552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017554 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017555 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017556 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017557 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017559 if (flags & SP_SUBPAT)
17560 retval = subpatnum;
17561 else
17562 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017563 if (flags & SP_SETPCMARK)
17564 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017566 if (match_pos != NULL)
17567 {
17568 /* Store the match cursor position */
17569 match_pos->lnum = pos.lnum;
17570 match_pos->col = pos.col + 1;
17571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 /* "/$" will put the cursor after the end of the line, may need to
17573 * correct that here */
17574 check_cursor();
17575 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017576
17577 /* If 'n' flag is used: restore cursor position. */
17578 if (flags & SP_NOMOVE)
17579 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017580 else
17581 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017582theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017584
17585 return retval;
17586}
17587
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017588#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017589
17590/*
17591 * round() is not in C90, use ceil() or floor() instead.
17592 */
17593 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017594vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017595{
17596 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17597}
17598
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017599/*
17600 * "round({float})" function
17601 */
17602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017603f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017604{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017605 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017606
17607 rettv->v_type = VAR_FLOAT;
17608 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017609 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017610 else
17611 rettv->vval.v_float = 0.0;
17612}
17613#endif
17614
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017615/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017616 * "screenattr()" function
17617 */
17618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017619f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017620{
17621 int row;
17622 int col;
17623 int c;
17624
17625 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17626 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17627 if (row < 0 || row >= screen_Rows
17628 || col < 0 || col >= screen_Columns)
17629 c = -1;
17630 else
17631 c = ScreenAttrs[LineOffset[row] + col];
17632 rettv->vval.v_number = c;
17633}
17634
17635/*
17636 * "screenchar()" function
17637 */
17638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017639f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017640{
17641 int row;
17642 int col;
17643 int off;
17644 int c;
17645
17646 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17647 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17648 if (row < 0 || row >= screen_Rows
17649 || col < 0 || col >= screen_Columns)
17650 c = -1;
17651 else
17652 {
17653 off = LineOffset[row] + col;
17654#ifdef FEAT_MBYTE
17655 if (enc_utf8 && ScreenLinesUC[off] != 0)
17656 c = ScreenLinesUC[off];
17657 else
17658#endif
17659 c = ScreenLines[off];
17660 }
17661 rettv->vval.v_number = c;
17662}
17663
17664/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017665 * "screencol()" function
17666 *
17667 * First column is 1 to be consistent with virtcol().
17668 */
17669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017670f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017671{
17672 rettv->vval.v_number = screen_screencol() + 1;
17673}
17674
17675/*
17676 * "screenrow()" function
17677 */
17678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017679f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017680{
17681 rettv->vval.v_number = screen_screenrow() + 1;
17682}
17683
17684/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017685 * "search()" function
17686 */
17687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017688f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017689{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017690 int flags = 0;
17691
17692 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693}
17694
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017696 * "searchdecl()" function
17697 */
17698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017699f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017700{
17701 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017702 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017703 int error = FALSE;
17704 char_u *name;
17705
17706 rettv->vval.v_number = 1; /* default: FAIL */
17707
17708 name = get_tv_string_chk(&argvars[0]);
17709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017710 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017711 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017712 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17713 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17714 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017715 if (!error && name != NULL)
17716 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017717 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017718}
17719
17720/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017721 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017723 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017724searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725{
17726 char_u *spat, *mpat, *epat;
17727 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 int dir;
17730 int flags = 0;
17731 char_u nbuf1[NUMBUFLEN];
17732 char_u nbuf2[NUMBUFLEN];
17733 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017734 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017735 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017736 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017739 spat = get_tv_string_chk(&argvars[0]);
17740 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17741 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17742 if (spat == NULL || mpat == NULL || epat == NULL)
17743 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745 /* Handle the optional fourth argument: flags */
17746 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017747 if (dir == 0)
17748 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017749
17750 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017751 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17752 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017753 if ((flags & (SP_END | SP_SUBPAT)) != 0
17754 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017755 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017756 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017757 goto theend;
17758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017760 /* Using 'r' implies 'W', otherwise it doesn't work. */
17761 if (flags & SP_REPEAT)
17762 p_ws = FALSE;
17763
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017764 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017765 if (argvars[3].v_type == VAR_UNKNOWN
17766 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767 skip = (char_u *)"";
17768 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017770 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017771 if (argvars[5].v_type != VAR_UNKNOWN)
17772 {
17773 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17774 if (lnum_stop < 0)
17775 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017776#ifdef FEAT_RELTIME
17777 if (argvars[6].v_type != VAR_UNKNOWN)
17778 {
17779 time_limit = get_tv_number_chk(&argvars[6], NULL);
17780 if (time_limit < 0)
17781 goto theend;
17782 }
17783#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017784 }
17785 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017786 if (skip == NULL)
17787 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017789 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017790 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017791
17792theend:
17793 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017794
17795 return retval;
17796}
17797
17798/*
17799 * "searchpair()" function
17800 */
17801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017802f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017803{
17804 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17805}
17806
17807/*
17808 * "searchpairpos()" function
17809 */
17810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017811f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017812{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017813 pos_T match_pos;
17814 int lnum = 0;
17815 int col = 0;
17816
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017817 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017818 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017819
17820 if (searchpair_cmn(argvars, &match_pos) > 0)
17821 {
17822 lnum = match_pos.lnum;
17823 col = match_pos.col;
17824 }
17825
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017826 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17827 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017828}
17829
17830/*
17831 * Search for a start/middle/end thing.
17832 * Used by searchpair(), see its documentation for the details.
17833 * Returns 0 or -1 for no match,
17834 */
17835 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017836do_searchpair(
17837 char_u *spat, /* start pattern */
17838 char_u *mpat, /* middle pattern */
17839 char_u *epat, /* end pattern */
17840 int dir, /* BACKWARD or FORWARD */
17841 char_u *skip, /* skip expression */
17842 int flags, /* SP_SETPCMARK and other SP_ values */
17843 pos_T *match_pos,
17844 linenr_T lnum_stop, /* stop at this line if not zero */
17845 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017846{
17847 char_u *save_cpo;
17848 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17849 long retval = 0;
17850 pos_T pos;
17851 pos_T firstpos;
17852 pos_T foundpos;
17853 pos_T save_cursor;
17854 pos_T save_pos;
17855 int n;
17856 int r;
17857 int nest = 1;
17858 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017859 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017860 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017861
17862 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17863 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017864 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017865
Bram Moolenaar76929292008-01-06 19:07:36 +000017866#ifdef FEAT_RELTIME
17867 /* Set the time limit, if there is one. */
17868 profile_setlimit(time_limit, &tm);
17869#endif
17870
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017871 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17872 * start/middle/end (pat3, for the top pair). */
17873 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17874 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17875 if (pat2 == NULL || pat3 == NULL)
17876 goto theend;
17877 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17878 if (*mpat == NUL)
17879 STRCPY(pat3, pat2);
17880 else
17881 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17882 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017883 if (flags & SP_START)
17884 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017885
Bram Moolenaar071d4272004-06-13 20:20:40 +000017886 save_cursor = curwin->w_cursor;
17887 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017888 clearpos(&firstpos);
17889 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 pat = pat3;
17891 for (;;)
17892 {
17893 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017894 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17896 /* didn't find it or found the first match again: FAIL */
17897 break;
17898
17899 if (firstpos.lnum == 0)
17900 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017901 if (equalpos(pos, foundpos))
17902 {
17903 /* Found the same position again. Can happen with a pattern that
17904 * has "\zs" at the end and searching backwards. Advance one
17905 * character and try again. */
17906 if (dir == BACKWARD)
17907 decl(&pos);
17908 else
17909 incl(&pos);
17910 }
17911 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017913 /* clear the start flag to avoid getting stuck here */
17914 options &= ~SEARCH_START;
17915
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 /* If the skip pattern matches, ignore this match. */
17917 if (*skip != NUL)
17918 {
17919 save_pos = curwin->w_cursor;
17920 curwin->w_cursor = pos;
17921 r = eval_to_bool(skip, &err, NULL, FALSE);
17922 curwin->w_cursor = save_pos;
17923 if (err)
17924 {
17925 /* Evaluating {skip} caused an error, break here. */
17926 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017927 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 break;
17929 }
17930 if (r)
17931 continue;
17932 }
17933
17934 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17935 {
17936 /* Found end when searching backwards or start when searching
17937 * forward: nested pair. */
17938 ++nest;
17939 pat = pat2; /* nested, don't search for middle */
17940 }
17941 else
17942 {
17943 /* Found end when searching forward or start when searching
17944 * backward: end of (nested) pair; or found middle in outer pair. */
17945 if (--nest == 1)
17946 pat = pat3; /* outer level, search for middle */
17947 }
17948
17949 if (nest == 0)
17950 {
17951 /* Found the match: return matchcount or line number. */
17952 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017953 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017955 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017956 if (flags & SP_SETPCMARK)
17957 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958 curwin->w_cursor = pos;
17959 if (!(flags & SP_REPEAT))
17960 break;
17961 nest = 1; /* search for next unmatched */
17962 }
17963 }
17964
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017965 if (match_pos != NULL)
17966 {
17967 /* Store the match cursor position */
17968 match_pos->lnum = curwin->w_cursor.lnum;
17969 match_pos->col = curwin->w_cursor.col + 1;
17970 }
17971
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017973 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 curwin->w_cursor = save_cursor;
17975
17976theend:
17977 vim_free(pat2);
17978 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017979 if (p_cpo == empty_option)
17980 p_cpo = save_cpo;
17981 else
17982 /* Darn, evaluating the {skip} expression changed the value. */
17983 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017984
17985 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986}
17987
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017988/*
17989 * "searchpos()" function
17990 */
17991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017992f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017993{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017994 pos_T match_pos;
17995 int lnum = 0;
17996 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017997 int n;
17998 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017999
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018000 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018001 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018002
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018003 n = search_cmn(argvars, &match_pos, &flags);
18004 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018005 {
18006 lnum = match_pos.lnum;
18007 col = match_pos.col;
18008 }
18009
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018010 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18011 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018012 if (flags & SP_SUBPAT)
18013 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018014}
18015
Bram Moolenaar0d660222005-01-07 21:51:51 +000018016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018017f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018019#ifdef FEAT_CLIENTSERVER
18020 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018021 char_u *server = get_tv_string_chk(&argvars[0]);
18022 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023
Bram Moolenaar0d660222005-01-07 21:51:51 +000018024 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018025 if (server == NULL || reply == NULL)
18026 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018027 if (check_restricted() || check_secure())
18028 return;
18029# ifdef FEAT_X11
18030 if (check_connection() == FAIL)
18031 return;
18032# endif
18033
18034 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018036 EMSG(_("E258: Unable to send to client"));
18037 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018039 rettv->vval.v_number = 0;
18040#else
18041 rettv->vval.v_number = -1;
18042#endif
18043}
18044
Bram Moolenaar0d660222005-01-07 21:51:51 +000018045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018046f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018047{
18048 char_u *r = NULL;
18049
18050#ifdef FEAT_CLIENTSERVER
18051# ifdef WIN32
18052 r = serverGetVimNames();
18053# else
18054 make_connection();
18055 if (X_DISPLAY != NULL)
18056 r = serverGetVimNames(X_DISPLAY);
18057# endif
18058#endif
18059 rettv->v_type = VAR_STRING;
18060 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061}
18062
18063/*
18064 * "setbufvar()" function
18065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018067f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068{
18069 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018070 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018072 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 char_u nbuf[NUMBUFLEN];
18074
18075 if (check_restricted() || check_secure())
18076 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018077 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18078 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018079 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 varp = &argvars[2];
18081
18082 if (buf != NULL && varname != NULL && varp != NULL)
18083 {
18084 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086
18087 if (*varname == '&')
18088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018089 long numval;
18090 char_u *strval;
18091 int error = FALSE;
18092
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018094 numval = get_tv_number_chk(varp, &error);
18095 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018096 if (!error && strval != NULL)
18097 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 }
18099 else
18100 {
18101 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18102 if (bufvarname != NULL)
18103 {
18104 STRCPY(bufvarname, "b:");
18105 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018106 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 vim_free(bufvarname);
18108 }
18109 }
18110
18111 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018114}
18115
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018117f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018118{
18119 dict_T *d;
18120 dictitem_T *di;
18121 char_u *csearch;
18122
18123 if (argvars[0].v_type != VAR_DICT)
18124 {
18125 EMSG(_(e_dictreq));
18126 return;
18127 }
18128
18129 if ((d = argvars[0].vval.v_dict) != NULL)
18130 {
18131 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18132 if (csearch != NULL)
18133 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018134#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018135 if (enc_utf8)
18136 {
18137 int pcc[MAX_MCO];
18138 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018139
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018140 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18141 }
18142 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018143#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018144 set_last_csearch(PTR2CHAR(csearch),
18145 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018146 }
18147
18148 di = dict_find(d, (char_u *)"forward", -1);
18149 if (di != NULL)
18150 set_csearch_direction(get_tv_number(&di->di_tv)
18151 ? FORWARD : BACKWARD);
18152
18153 di = dict_find(d, (char_u *)"until", -1);
18154 if (di != NULL)
18155 set_csearch_until(!!get_tv_number(&di->di_tv));
18156 }
18157}
18158
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159/*
18160 * "setcmdpos()" function
18161 */
18162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018163f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018165 int pos = (int)get_tv_number(&argvars[0]) - 1;
18166
18167 if (pos >= 0)
18168 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169}
18170
18171/*
18172 * "setline()" function
18173 */
18174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018175f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176{
18177 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018178 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018179 list_T *l = NULL;
18180 listitem_T *li = NULL;
18181 long added = 0;
18182 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018184 lnum = get_tv_lnum(&argvars[0]);
18185 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018187 l = argvars[1].vval.v_list;
18188 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018189 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018190 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018191 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018192
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018193 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018194 for (;;)
18195 {
18196 if (l != NULL)
18197 {
18198 /* list argument, get next string */
18199 if (li == NULL)
18200 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018201 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018202 li = li->li_next;
18203 }
18204
18205 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018206 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018207 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018208
18209 /* When coming here from Insert mode, sync undo, so that this can be
18210 * undone separately from what was previously inserted. */
18211 if (u_sync_once == 2)
18212 {
18213 u_sync_once = 1; /* notify that u_sync() was called */
18214 u_sync(TRUE);
18215 }
18216
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018217 if (lnum <= curbuf->b_ml.ml_line_count)
18218 {
18219 /* existing line, replace it */
18220 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18221 {
18222 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018223 if (lnum == curwin->w_cursor.lnum)
18224 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018225 rettv->vval.v_number = 0; /* OK */
18226 }
18227 }
18228 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18229 {
18230 /* lnum is one past the last line, append the line */
18231 ++added;
18232 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18233 rettv->vval.v_number = 0; /* OK */
18234 }
18235
18236 if (l == NULL) /* only one string argument */
18237 break;
18238 ++lnum;
18239 }
18240
18241 if (added > 0)
18242 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243}
18244
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018245static 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 +000018246
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018248 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018249 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018251set_qf_ll_list(
18252 win_T *wp UNUSED,
18253 typval_T *list_arg UNUSED,
18254 typval_T *action_arg UNUSED,
18255 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018256{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018257#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018258 char_u *act;
18259 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018260#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018261
Bram Moolenaar2641f772005-03-25 21:58:17 +000018262 rettv->vval.v_number = -1;
18263
18264#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018265 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018266 EMSG(_(e_listreq));
18267 else
18268 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018269 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018270
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018271 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018272 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018273 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018274 if (act == NULL)
18275 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018276 if (*act == 'a' || *act == 'r')
18277 action = *act;
18278 }
18279
Bram Moolenaar81484f42012-12-05 15:16:47 +010018280 if (l != NULL && set_errorlist(wp, l, action,
18281 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018282 rettv->vval.v_number = 0;
18283 }
18284#endif
18285}
18286
18287/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018288 * "setloclist()" function
18289 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018291f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018292{
18293 win_T *win;
18294
18295 rettv->vval.v_number = -1;
18296
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018297 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018298 if (win != NULL)
18299 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18300}
18301
18302/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018303 * "setmatches()" function
18304 */
18305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018306f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018307{
18308#ifdef FEAT_SEARCH_EXTRA
18309 list_T *l;
18310 listitem_T *li;
18311 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018312 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018313
18314 rettv->vval.v_number = -1;
18315 if (argvars[0].v_type != VAR_LIST)
18316 {
18317 EMSG(_(e_listreq));
18318 return;
18319 }
18320 if ((l = argvars[0].vval.v_list) != NULL)
18321 {
18322
18323 /* To some extent make sure that we are dealing with a list from
18324 * "getmatches()". */
18325 li = l->lv_first;
18326 while (li != NULL)
18327 {
18328 if (li->li_tv.v_type != VAR_DICT
18329 || (d = li->li_tv.vval.v_dict) == NULL)
18330 {
18331 EMSG(_(e_invarg));
18332 return;
18333 }
18334 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018335 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18336 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018337 && dict_find(d, (char_u *)"priority", -1) != NULL
18338 && dict_find(d, (char_u *)"id", -1) != NULL))
18339 {
18340 EMSG(_(e_invarg));
18341 return;
18342 }
18343 li = li->li_next;
18344 }
18345
18346 clear_matches(curwin);
18347 li = l->lv_first;
18348 while (li != NULL)
18349 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018350 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018351 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018352 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018353 char_u *group;
18354 int priority;
18355 int id;
18356 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018357
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018358 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018359 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18360 {
18361 if (s == NULL)
18362 {
18363 s = list_alloc();
18364 if (s == NULL)
18365 return;
18366 }
18367
18368 /* match from matchaddpos() */
18369 for (i = 1; i < 9; i++)
18370 {
18371 sprintf((char *)buf, (char *)"pos%d", i);
18372 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18373 {
18374 if (di->di_tv.v_type != VAR_LIST)
18375 return;
18376
18377 list_append_tv(s, &di->di_tv);
18378 s->lv_refcount++;
18379 }
18380 else
18381 break;
18382 }
18383 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018384
18385 group = get_dict_string(d, (char_u *)"group", FALSE);
18386 priority = (int)get_dict_number(d, (char_u *)"priority");
18387 id = (int)get_dict_number(d, (char_u *)"id");
18388 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18389 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18390 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018391 if (i == 0)
18392 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018393 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018394 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018395 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018396 }
18397 else
18398 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018399 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018400 list_unref(s);
18401 s = NULL;
18402 }
18403
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018404 li = li->li_next;
18405 }
18406 rettv->vval.v_number = 0;
18407 }
18408#endif
18409}
18410
18411/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018412 * "setpos()" function
18413 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018415f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018416{
18417 pos_T pos;
18418 int fnum;
18419 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018420 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018421
Bram Moolenaar08250432008-02-13 11:42:46 +000018422 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018423 name = get_tv_string_chk(argvars);
18424 if (name != NULL)
18425 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018426 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018427 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018428 if (--pos.col < 0)
18429 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018430 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018431 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018432 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018433 if (fnum == curbuf->b_fnum)
18434 {
18435 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018436 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018437 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018438 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018439 curwin->w_set_curswant = FALSE;
18440 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018441 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018442 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018443 }
18444 else
18445 EMSG(_(e_invarg));
18446 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018447 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18448 {
18449 /* set mark */
18450 if (setmark_pos(name[1], &pos, fnum) == OK)
18451 rettv->vval.v_number = 0;
18452 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018453 else
18454 EMSG(_(e_invarg));
18455 }
18456 }
18457}
18458
18459/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018460 * "setqflist()" function
18461 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018463f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018464{
18465 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18466}
18467
18468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 * "setreg()" function
18470 */
18471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018472f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473{
18474 int regname;
18475 char_u *strregname;
18476 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018477 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 int append;
18479 char_u yank_type;
18480 long block_len;
18481
18482 block_len = -1;
18483 yank_type = MAUTO;
18484 append = FALSE;
18485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018486 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018487 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018489 if (strregname == NULL)
18490 return; /* type error; errmsg already given */
18491 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492 if (regname == 0 || regname == '@')
18493 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018495 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018497 stropt = get_tv_string_chk(&argvars[2]);
18498 if (stropt == NULL)
18499 return; /* type error */
18500 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 switch (*stropt)
18502 {
18503 case 'a': case 'A': /* append */
18504 append = TRUE;
18505 break;
18506 case 'v': case 'c': /* character-wise selection */
18507 yank_type = MCHAR;
18508 break;
18509 case 'V': case 'l': /* line-wise selection */
18510 yank_type = MLINE;
18511 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 case 'b': case Ctrl_V: /* block-wise selection */
18513 yank_type = MBLOCK;
18514 if (VIM_ISDIGIT(stropt[1]))
18515 {
18516 ++stropt;
18517 block_len = getdigits(&stropt) - 1;
18518 --stropt;
18519 }
18520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 }
18522 }
18523
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018524 if (argvars[1].v_type == VAR_LIST)
18525 {
18526 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018527 char_u **allocval;
18528 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018529 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018530 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018531 int len = argvars[1].vval.v_list->lv_len;
18532 listitem_T *li;
18533
Bram Moolenaar7d647822014-04-05 21:28:56 +020018534 /* First half: use for pointers to result lines; second half: use for
18535 * pointers to allocated copies. */
18536 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018537 if (lstval == NULL)
18538 return;
18539 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018540 allocval = lstval + len + 2;
18541 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018542
18543 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18544 li = li->li_next)
18545 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018546 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018547 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018548 goto free_lstval;
18549 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018550 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018551 /* Need to make a copy, next get_tv_string_buf_chk() will
18552 * overwrite the string. */
18553 strval = vim_strsave(buf);
18554 if (strval == NULL)
18555 goto free_lstval;
18556 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018557 }
18558 *curval++ = strval;
18559 }
18560 *curval++ = NULL;
18561
18562 write_reg_contents_lst(regname, lstval, -1,
18563 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018564free_lstval:
18565 while (curallocval > allocval)
18566 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018567 vim_free(lstval);
18568 }
18569 else
18570 {
18571 strval = get_tv_string_chk(&argvars[1]);
18572 if (strval == NULL)
18573 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018574 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018577 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578}
18579
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018580/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018581 * "settabvar()" function
18582 */
18583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018584f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018585{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018586#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018587 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018588 tabpage_T *tp;
18589#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018590 char_u *varname, *tabvarname;
18591 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018592
18593 rettv->vval.v_number = 0;
18594
18595 if (check_restricted() || check_secure())
18596 return;
18597
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018598#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018599 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018600#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018601 varname = get_tv_string_chk(&argvars[1]);
18602 varp = &argvars[2];
18603
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018604 if (varname != NULL && varp != NULL
18605#ifdef FEAT_WINDOWS
18606 && tp != NULL
18607#endif
18608 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018609 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018610#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018611 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018612 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018613#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018614
18615 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18616 if (tabvarname != NULL)
18617 {
18618 STRCPY(tabvarname, "t:");
18619 STRCPY(tabvarname + 2, varname);
18620 set_var(tabvarname, varp, TRUE);
18621 vim_free(tabvarname);
18622 }
18623
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018624#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018625 /* Restore current tabpage */
18626 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018627 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018628#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018629 }
18630}
18631
18632/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018633 * "settabwinvar()" function
18634 */
18635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018636f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018637{
18638 setwinvar(argvars, rettv, 1);
18639}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640
18641/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018642 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018645f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018647 setwinvar(argvars, rettv, 0);
18648}
18649
18650/*
18651 * "setwinvar()" and "settabwinvar()" functions
18652 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018653
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018655setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018656{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 win_T *win;
18658#ifdef FEAT_WINDOWS
18659 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018660 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018661 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018662#endif
18663 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018664 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018666 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667
18668 if (check_restricted() || check_secure())
18669 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018670
18671#ifdef FEAT_WINDOWS
18672 if (off == 1)
18673 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18674 else
18675 tp = curtab;
18676#endif
18677 win = find_win_by_nr(&argvars[off], tp);
18678 varname = get_tv_string_chk(&argvars[off + 1]);
18679 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680
18681 if (win != NULL && varname != NULL && varp != NULL)
18682 {
18683#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018684 need_switch_win = !(tp == curtab && win == curwin);
18685 if (!need_switch_win
18686 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018689 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018691 long numval;
18692 char_u *strval;
18693 int error = FALSE;
18694
18695 ++varname;
18696 numval = get_tv_number_chk(varp, &error);
18697 strval = get_tv_string_buf_chk(varp, nbuf);
18698 if (!error && strval != NULL)
18699 set_option_value(varname, numval, strval, OPT_LOCAL);
18700 }
18701 else
18702 {
18703 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18704 if (winvarname != NULL)
18705 {
18706 STRCPY(winvarname, "w:");
18707 STRCPY(winvarname + 2, varname);
18708 set_var(winvarname, varp, TRUE);
18709 vim_free(winvarname);
18710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711 }
18712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018714 if (need_switch_win)
18715 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716#endif
18717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718}
18719
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018720#ifdef FEAT_CRYPT
18721/*
18722 * "sha256({string})" function
18723 */
18724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018725f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018726{
18727 char_u *p;
18728
18729 p = get_tv_string(&argvars[0]);
18730 rettv->vval.v_string = vim_strsave(
18731 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18732 rettv->v_type = VAR_STRING;
18733}
18734#endif /* FEAT_CRYPT */
18735
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018737 * "shellescape({string})" function
18738 */
18739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018740f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018741{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018742 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018743 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018744 rettv->v_type = VAR_STRING;
18745}
18746
18747/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018748 * shiftwidth() function
18749 */
18750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018751f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018752{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018753 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018754}
18755
18756/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018757 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758 */
18759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018760f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018762 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763
Bram Moolenaar0d660222005-01-07 21:51:51 +000018764 p = get_tv_string(&argvars[0]);
18765 rettv->vval.v_string = vim_strsave(p);
18766 simplify_filename(rettv->vval.v_string); /* simplify in place */
18767 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768}
18769
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018770#ifdef FEAT_FLOAT
18771/*
18772 * "sin()" function
18773 */
18774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018775f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018776{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018777 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018778
18779 rettv->v_type = VAR_FLOAT;
18780 if (get_float_arg(argvars, &f) == OK)
18781 rettv->vval.v_float = sin(f);
18782 else
18783 rettv->vval.v_float = 0.0;
18784}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018785
18786/*
18787 * "sinh()" function
18788 */
18789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018790f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018791{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018792 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018793
18794 rettv->v_type = VAR_FLOAT;
18795 if (get_float_arg(argvars, &f) == OK)
18796 rettv->vval.v_float = sinh(f);
18797 else
18798 rettv->vval.v_float = 0.0;
18799}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018800#endif
18801
Bram Moolenaar0d660222005-01-07 21:51:51 +000018802static int
18803#ifdef __BORLANDC__
18804 _RTLENTRYF
18805#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018806 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018807static int
18808#ifdef __BORLANDC__
18809 _RTLENTRYF
18810#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018811 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018812
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018813/* struct used in the array that's given to qsort() */
18814typedef struct
18815{
18816 listitem_T *item;
18817 int idx;
18818} sortItem_T;
18819
Bram Moolenaar0b962472016-02-22 22:51:33 +010018820/* struct storing information about current sort */
18821typedef struct
18822{
18823 int item_compare_ic;
18824 int item_compare_numeric;
18825 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018826#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018827 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018828#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018829 char_u *item_compare_func;
18830 dict_T *item_compare_selfdict;
18831 int item_compare_func_err;
18832 int item_compare_keep_zero;
18833} sortinfo_T;
18834static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018835static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018836#define ITEM_COMPARE_FAIL 999
18837
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018839 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018841 static int
18842#ifdef __BORLANDC__
18843_RTLENTRYF
18844#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018845item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018847 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018848 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018849 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018850 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018851 int res;
18852 char_u numbuf1[NUMBUFLEN];
18853 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018855 si1 = (sortItem_T *)s1;
18856 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018857 tv1 = &si1->item->li_tv;
18858 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018859
Bram Moolenaar0b962472016-02-22 22:51:33 +010018860 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018861 {
18862 long v1 = get_tv_number(tv1);
18863 long v2 = get_tv_number(tv2);
18864
18865 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18866 }
18867
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018868#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018869 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018870 {
18871 float_T v1 = get_tv_float(tv1);
18872 float_T v2 = get_tv_float(tv2);
18873
18874 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18875 }
18876#endif
18877
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018878 /* tv2string() puts quotes around a string and allocates memory. Don't do
18879 * that for string variables. Use a single quote when comparing with a
18880 * non-string to do what the docs promise. */
18881 if (tv1->v_type == VAR_STRING)
18882 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018883 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018884 p1 = (char_u *)"'";
18885 else
18886 p1 = tv1->vval.v_string;
18887 }
18888 else
18889 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18890 if (tv2->v_type == VAR_STRING)
18891 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018892 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018893 p2 = (char_u *)"'";
18894 else
18895 p2 = tv2->vval.v_string;
18896 }
18897 else
18898 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018899 if (p1 == NULL)
18900 p1 = (char_u *)"";
18901 if (p2 == NULL)
18902 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018903 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018904 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018905 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018906 res = STRICMP(p1, p2);
18907 else
18908 res = STRCMP(p1, p2);
18909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018911 {
18912 double n1, n2;
18913 n1 = strtod((char *)p1, (char **)&p1);
18914 n2 = strtod((char *)p2, (char **)&p2);
18915 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18916 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018917
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018918 /* When the result would be zero, compare the item indexes. Makes the
18919 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018920 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018921 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018922
Bram Moolenaar0d660222005-01-07 21:51:51 +000018923 vim_free(tofree1);
18924 vim_free(tofree2);
18925 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926}
18927
18928 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929#ifdef __BORLANDC__
18930_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018932item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018933{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018934 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018935 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018936 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018937 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018938 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018940 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018941 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018942 return 0;
18943
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018944 si1 = (sortItem_T *)s1;
18945 si2 = (sortItem_T *)s2;
18946
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018947 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018948 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018949 copy_tv(&si1->item->li_tv, &argv[0]);
18950 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018951
18952 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018953 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010018954 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018955 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010018956 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018957 clear_tv(&argv[0]);
18958 clear_tv(&argv[1]);
18959
18960 if (res == FAIL)
18961 res = ITEM_COMPARE_FAIL;
18962 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018963 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18964 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018965 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018966 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018967
18968 /* When the result would be zero, compare the pointers themselves. Makes
18969 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018970 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018971 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018972
Bram Moolenaar0d660222005-01-07 21:51:51 +000018973 return res;
18974}
18975
18976/*
18977 * "sort({list})" function
18978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018980do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981{
Bram Moolenaar33570922005-01-25 22:26:29 +000018982 list_T *l;
18983 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018984 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018985 sortinfo_T *old_sortinfo;
18986 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018987 long len;
18988 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989
Bram Moolenaar0b962472016-02-22 22:51:33 +010018990 /* Pointer to current info struct used in compare function. Save and
18991 * restore the current one for nested calls. */
18992 old_sortinfo = sortinfo;
18993 sortinfo = &info;
18994
Bram Moolenaar0d660222005-01-07 21:51:51 +000018995 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018996 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 else
18998 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019000 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019001 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19002 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019003 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019004 rettv->vval.v_list = l;
19005 rettv->v_type = VAR_LIST;
19006 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007
Bram Moolenaar0d660222005-01-07 21:51:51 +000019008 len = list_len(l);
19009 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019010 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011
Bram Moolenaar0b962472016-02-22 22:51:33 +010019012 info.item_compare_ic = FALSE;
19013 info.item_compare_numeric = FALSE;
19014 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019015#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019016 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019017#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019018 info.item_compare_func = NULL;
19019 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019020 if (argvars[1].v_type != VAR_UNKNOWN)
19021 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019022 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019023 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019024 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019025 else
19026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019027 int error = FALSE;
19028
19029 i = get_tv_number_chk(&argvars[1], &error);
19030 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019031 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019032 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019033 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019034 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019035 info.item_compare_func = get_tv_string(&argvars[1]);
19036 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019037 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019038 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019039 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019040 info.item_compare_func = NULL;
19041 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019042 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019043 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019044 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019045 info.item_compare_func = NULL;
19046 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019047 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019048#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019049 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019050 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019051 info.item_compare_func = NULL;
19052 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019053 }
19054#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019055 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019056 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019057 info.item_compare_func = NULL;
19058 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019059 }
19060 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019061 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019062
19063 if (argvars[2].v_type != VAR_UNKNOWN)
19064 {
19065 /* optional third argument: {dict} */
19066 if (argvars[2].v_type != VAR_DICT)
19067 {
19068 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019069 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019070 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019071 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019072 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074
Bram Moolenaar0d660222005-01-07 21:51:51 +000019075 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019076 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019078 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019079
Bram Moolenaar327aa022014-03-25 18:24:23 +010019080 i = 0;
19081 if (sort)
19082 {
19083 /* sort(): ptrs will be the list to sort */
19084 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019085 {
19086 ptrs[i].item = li;
19087 ptrs[i].idx = i;
19088 ++i;
19089 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019090
Bram Moolenaar0b962472016-02-22 22:51:33 +010019091 info.item_compare_func_err = FALSE;
19092 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019093 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019094 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019095 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019096 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019097 EMSG(_("E702: Sort compare function failed"));
19098 else
19099 {
19100 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019101 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019102 info.item_compare_func == NULL
19103 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019104
Bram Moolenaar0b962472016-02-22 22:51:33 +010019105 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019106 {
19107 /* Clear the List and append the items in sorted order. */
19108 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19109 l->lv_len = 0;
19110 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019111 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019112 }
19113 }
19114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019116 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019117 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019118
19119 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019120 info.item_compare_func_err = FALSE;
19121 info.item_compare_keep_zero = TRUE;
19122 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019123 ? item_compare2 : item_compare;
19124
19125 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19126 li = li->li_next)
19127 {
19128 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19129 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019130 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019131 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019132 {
19133 EMSG(_("E882: Uniq compare function failed"));
19134 break;
19135 }
19136 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019137
Bram Moolenaar0b962472016-02-22 22:51:33 +010019138 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019139 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019140 while (--i >= 0)
19141 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019142 li = ptrs[i].item->li_next;
19143 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019144 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019145 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019146 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019147 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019148 list_fix_watch(l, li);
19149 listitem_free(li);
19150 l->lv_len--;
19151 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019152 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019153 }
19154
19155 vim_free(ptrs);
19156 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019157theend:
19158 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019159}
19160
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019161/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019162 * "sort({list})" function
19163 */
19164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019165f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019166{
19167 do_sort_uniq(argvars, rettv, TRUE);
19168}
19169
19170/*
19171 * "uniq({list})" function
19172 */
19173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019174f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019175{
19176 do_sort_uniq(argvars, rettv, FALSE);
19177}
19178
19179/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019180 * "soundfold({word})" function
19181 */
19182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019183f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019184{
19185 char_u *s;
19186
19187 rettv->v_type = VAR_STRING;
19188 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019189#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019190 rettv->vval.v_string = eval_soundfold(s);
19191#else
19192 rettv->vval.v_string = vim_strsave(s);
19193#endif
19194}
19195
19196/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019197 * "spellbadword()" function
19198 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019200f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019201{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019202 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019203 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019204 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019205
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019206 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019207 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019208
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019209#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019210 if (argvars[0].v_type == VAR_UNKNOWN)
19211 {
19212 /* Find the start and length of the badly spelled word. */
19213 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19214 if (len != 0)
19215 word = ml_get_cursor();
19216 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019217 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019218 {
19219 char_u *str = get_tv_string_chk(&argvars[0]);
19220 int capcol = -1;
19221
19222 if (str != NULL)
19223 {
19224 /* Check the argument for spelling. */
19225 while (*str != NUL)
19226 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019227 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019228 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019229 {
19230 word = str;
19231 break;
19232 }
19233 str += len;
19234 }
19235 }
19236 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019237#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019238
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019239 list_append_string(rettv->vval.v_list, word, len);
19240 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019241 attr == HLF_SPB ? "bad" :
19242 attr == HLF_SPR ? "rare" :
19243 attr == HLF_SPL ? "local" :
19244 attr == HLF_SPC ? "caps" :
19245 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019246}
19247
19248/*
19249 * "spellsuggest()" function
19250 */
19251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019252f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019253{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019254#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019255 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019256 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019257 int maxcount;
19258 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019259 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019260 listitem_T *li;
19261 int need_capital = FALSE;
19262#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019263
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019264 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019265 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019266
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019267#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019268 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019269 {
19270 str = get_tv_string(&argvars[0]);
19271 if (argvars[1].v_type != VAR_UNKNOWN)
19272 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019273 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019274 if (maxcount <= 0)
19275 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019276 if (argvars[2].v_type != VAR_UNKNOWN)
19277 {
19278 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19279 if (typeerr)
19280 return;
19281 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019282 }
19283 else
19284 maxcount = 25;
19285
Bram Moolenaar4770d092006-01-12 23:22:24 +000019286 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019287
19288 for (i = 0; i < ga.ga_len; ++i)
19289 {
19290 str = ((char_u **)ga.ga_data)[i];
19291
19292 li = listitem_alloc();
19293 if (li == NULL)
19294 vim_free(str);
19295 else
19296 {
19297 li->li_tv.v_type = VAR_STRING;
19298 li->li_tv.v_lock = 0;
19299 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019300 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019301 }
19302 }
19303 ga_clear(&ga);
19304 }
19305#endif
19306}
19307
Bram Moolenaar0d660222005-01-07 21:51:51 +000019308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019309f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019310{
19311 char_u *str;
19312 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019313 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019314 regmatch_T regmatch;
19315 char_u patbuf[NUMBUFLEN];
19316 char_u *save_cpo;
19317 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019318 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019319 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019320 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019321
19322 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19323 save_cpo = p_cpo;
19324 p_cpo = (char_u *)"";
19325
19326 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019327 if (argvars[1].v_type != VAR_UNKNOWN)
19328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019329 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19330 if (pat == NULL)
19331 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019332 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019333 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019334 }
19335 if (pat == NULL || *pat == NUL)
19336 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019337
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019338 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019340 if (typeerr)
19341 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342
Bram Moolenaar0d660222005-01-07 21:51:51 +000019343 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19344 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019346 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019347 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019348 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019349 if (*str == NUL)
19350 match = FALSE; /* empty item at the end */
19351 else
19352 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019353 if (match)
19354 end = regmatch.startp[0];
19355 else
19356 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019357 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19358 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019359 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019360 if (list_append_string(rettv->vval.v_list, str,
19361 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019362 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019363 }
19364 if (!match)
19365 break;
19366 /* Advance to just after the match. */
19367 if (regmatch.endp[0] > str)
19368 col = 0;
19369 else
19370 {
19371 /* Don't get stuck at the same match. */
19372#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019373 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019374#else
19375 col = 1;
19376#endif
19377 }
19378 str = regmatch.endp[0];
19379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380
Bram Moolenaar473de612013-06-08 18:19:48 +020019381 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383
Bram Moolenaar0d660222005-01-07 21:51:51 +000019384 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385}
19386
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019387#ifdef FEAT_FLOAT
19388/*
19389 * "sqrt()" function
19390 */
19391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019392f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019393{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019394 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019395
19396 rettv->v_type = VAR_FLOAT;
19397 if (get_float_arg(argvars, &f) == OK)
19398 rettv->vval.v_float = sqrt(f);
19399 else
19400 rettv->vval.v_float = 0.0;
19401}
19402
19403/*
19404 * "str2float()" function
19405 */
19406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019407f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019408{
19409 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19410
19411 if (*p == '+')
19412 p = skipwhite(p + 1);
19413 (void)string2float(p, &rettv->vval.v_float);
19414 rettv->v_type = VAR_FLOAT;
19415}
19416#endif
19417
Bram Moolenaar2c932302006-03-18 21:42:09 +000019418/*
19419 * "str2nr()" function
19420 */
19421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019422f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019423{
19424 int base = 10;
19425 char_u *p;
19426 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019427 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019428
19429 if (argvars[1].v_type != VAR_UNKNOWN)
19430 {
19431 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019432 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019433 {
19434 EMSG(_(e_invarg));
19435 return;
19436 }
19437 }
19438
19439 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019440 if (*p == '+')
19441 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019442 switch (base)
19443 {
19444 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19445 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19446 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19447 default: what = 0;
19448 }
19449 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019450 rettv->vval.v_number = n;
19451}
19452
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453#ifdef HAVE_STRFTIME
19454/*
19455 * "strftime({format}[, {time}])" function
19456 */
19457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019458f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459{
19460 char_u result_buf[256];
19461 struct tm *curtime;
19462 time_t seconds;
19463 char_u *p;
19464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019465 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019467 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019468 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 seconds = time(NULL);
19470 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019471 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 curtime = localtime(&seconds);
19473 /* MSVC returns NULL for an invalid value of seconds. */
19474 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019475 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 else
19477 {
19478# ifdef FEAT_MBYTE
19479 vimconv_T conv;
19480 char_u *enc;
19481
19482 conv.vc_type = CONV_NONE;
19483 enc = enc_locale();
19484 convert_setup(&conv, p_enc, enc);
19485 if (conv.vc_type != CONV_NONE)
19486 p = string_convert(&conv, p, NULL);
19487# endif
19488 if (p != NULL)
19489 (void)strftime((char *)result_buf, sizeof(result_buf),
19490 (char *)p, curtime);
19491 else
19492 result_buf[0] = NUL;
19493
19494# ifdef FEAT_MBYTE
19495 if (conv.vc_type != CONV_NONE)
19496 vim_free(p);
19497 convert_setup(&conv, enc, p_enc);
19498 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019499 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500 else
19501# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019502 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503
19504# ifdef FEAT_MBYTE
19505 /* Release conversion descriptors */
19506 convert_setup(&conv, NULL, NULL);
19507 vim_free(enc);
19508# endif
19509 }
19510}
19511#endif
19512
19513/*
19514 * "stridx()" function
19515 */
19516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019517f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019518{
19519 char_u buf[NUMBUFLEN];
19520 char_u *needle;
19521 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019522 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019524 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019526 needle = get_tv_string_chk(&argvars[1]);
19527 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019528 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019529 if (needle == NULL || haystack == NULL)
19530 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531
Bram Moolenaar33570922005-01-25 22:26:29 +000019532 if (argvars[2].v_type != VAR_UNKNOWN)
19533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019534 int error = FALSE;
19535
19536 start_idx = get_tv_number_chk(&argvars[2], &error);
19537 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019538 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019539 if (start_idx >= 0)
19540 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019541 }
19542
19543 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19544 if (pos != NULL)
19545 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546}
19547
19548/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019549 * "string()" function
19550 */
19551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019552f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019553{
19554 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019555 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019557 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019558 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019559 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019560 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019561 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019562}
19563
19564/*
19565 * "strlen()" function
19566 */
19567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019568f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019570 rettv->vval.v_number = (varnumber_T)(STRLEN(
19571 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572}
19573
19574/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019575 * "strchars()" function
19576 */
19577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019578f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019579{
19580 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019581 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019582#ifdef FEAT_MBYTE
19583 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019584 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019585#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019586
19587 if (argvars[1].v_type != VAR_UNKNOWN)
19588 skipcc = get_tv_number_chk(&argvars[1], NULL);
19589 if (skipcc < 0 || skipcc > 1)
19590 EMSG(_(e_invarg));
19591 else
19592 {
19593#ifdef FEAT_MBYTE
19594 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19595 while (*s != NUL)
19596 {
19597 func_mb_ptr2char_adv(&s);
19598 ++len;
19599 }
19600 rettv->vval.v_number = len;
19601#else
19602 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19603#endif
19604 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019605}
19606
19607/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019608 * "strdisplaywidth()" function
19609 */
19610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019611f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019612{
19613 char_u *s = get_tv_string(&argvars[0]);
19614 int col = 0;
19615
19616 if (argvars[1].v_type != VAR_UNKNOWN)
19617 col = get_tv_number(&argvars[1]);
19618
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019619 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019620}
19621
19622/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019623 * "strwidth()" function
19624 */
19625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019626f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019627{
19628 char_u *s = get_tv_string(&argvars[0]);
19629
19630 rettv->vval.v_number = (varnumber_T)(
19631#ifdef FEAT_MBYTE
19632 mb_string2cells(s, -1)
19633#else
19634 STRLEN(s)
19635#endif
19636 );
19637}
19638
19639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 * "strpart()" function
19641 */
19642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019643f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644{
19645 char_u *p;
19646 int n;
19647 int len;
19648 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019649 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019651 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 slen = (int)STRLEN(p);
19653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019654 n = get_tv_number_chk(&argvars[1], &error);
19655 if (error)
19656 len = 0;
19657 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019658 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 else
19660 len = slen - n; /* default len: all bytes that are available. */
19661
19662 /*
19663 * Only return the overlap between the specified part and the actual
19664 * string.
19665 */
19666 if (n < 0)
19667 {
19668 len += n;
19669 n = 0;
19670 }
19671 else if (n > slen)
19672 n = slen;
19673 if (len < 0)
19674 len = 0;
19675 else if (n + len > slen)
19676 len = slen - n;
19677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019678 rettv->v_type = VAR_STRING;
19679 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680}
19681
19682/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019683 * "strridx()" function
19684 */
19685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019686f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019687{
19688 char_u buf[NUMBUFLEN];
19689 char_u *needle;
19690 char_u *haystack;
19691 char_u *rest;
19692 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019693 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019695 needle = get_tv_string_chk(&argvars[1]);
19696 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019697
19698 rettv->vval.v_number = -1;
19699 if (needle == NULL || haystack == NULL)
19700 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019701
19702 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019703 if (argvars[2].v_type != VAR_UNKNOWN)
19704 {
19705 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019706 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019707 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019708 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019709 }
19710 else
19711 end_idx = haystack_len;
19712
Bram Moolenaar0d660222005-01-07 21:51:51 +000019713 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019714 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019715 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019716 lastmatch = haystack + end_idx;
19717 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019718 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019719 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019720 for (rest = haystack; *rest != '\0'; ++rest)
19721 {
19722 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019723 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019724 break;
19725 lastmatch = rest;
19726 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019727 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019728
19729 if (lastmatch == NULL)
19730 rettv->vval.v_number = -1;
19731 else
19732 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19733}
19734
19735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 * "strtrans()" function
19737 */
19738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019739f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019741 rettv->v_type = VAR_STRING;
19742 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743}
19744
19745/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019746 * "submatch()" function
19747 */
19748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019749f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019750{
Bram Moolenaar41571762014-04-02 19:00:58 +020019751 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019752 int no;
19753 int retList = 0;
19754
19755 no = (int)get_tv_number_chk(&argvars[0], &error);
19756 if (error)
19757 return;
19758 error = FALSE;
19759 if (argvars[1].v_type != VAR_UNKNOWN)
19760 retList = get_tv_number_chk(&argvars[1], &error);
19761 if (error)
19762 return;
19763
19764 if (retList == 0)
19765 {
19766 rettv->v_type = VAR_STRING;
19767 rettv->vval.v_string = reg_submatch(no);
19768 }
19769 else
19770 {
19771 rettv->v_type = VAR_LIST;
19772 rettv->vval.v_list = reg_submatch_list(no);
19773 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019774}
19775
19776/*
19777 * "substitute()" function
19778 */
19779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019780f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019781{
19782 char_u patbuf[NUMBUFLEN];
19783 char_u subbuf[NUMBUFLEN];
19784 char_u flagsbuf[NUMBUFLEN];
19785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019786 char_u *str = get_tv_string_chk(&argvars[0]);
19787 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19788 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19789 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19790
Bram Moolenaar0d660222005-01-07 21:51:51 +000019791 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019792 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19793 rettv->vval.v_string = NULL;
19794 else
19795 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019796}
19797
19798/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019799 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019802f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803{
19804 int id = 0;
19805#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019806 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 long col;
19808 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019809 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019811 lnum = get_tv_lnum(argvars); /* -1 on type error */
19812 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19813 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019815 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019816 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019817 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818#endif
19819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019820 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821}
19822
19823/*
19824 * "synIDattr(id, what [, mode])" function
19825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019827f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828{
19829 char_u *p = NULL;
19830#ifdef FEAT_SYN_HL
19831 int id;
19832 char_u *what;
19833 char_u *mode;
19834 char_u modebuf[NUMBUFLEN];
19835 int modec;
19836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019837 id = get_tv_number(&argvars[0]);
19838 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019839 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019841 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019843 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844 modec = 0; /* replace invalid with current */
19845 }
19846 else
19847 {
19848#ifdef FEAT_GUI
19849 if (gui.in_use)
19850 modec = 'g';
19851 else
19852#endif
19853 if (t_colors > 1)
19854 modec = 'c';
19855 else
19856 modec = 't';
19857 }
19858
19859
19860 switch (TOLOWER_ASC(what[0]))
19861 {
19862 case 'b':
19863 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19864 p = highlight_color(id, what, modec);
19865 else /* bold */
19866 p = highlight_has_attr(id, HL_BOLD, modec);
19867 break;
19868
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019869 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870 p = highlight_color(id, what, modec);
19871 break;
19872
19873 case 'i':
19874 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19875 p = highlight_has_attr(id, HL_INVERSE, modec);
19876 else /* italic */
19877 p = highlight_has_attr(id, HL_ITALIC, modec);
19878 break;
19879
19880 case 'n': /* name */
19881 p = get_highlight_name(NULL, id - 1);
19882 break;
19883
19884 case 'r': /* reverse */
19885 p = highlight_has_attr(id, HL_INVERSE, modec);
19886 break;
19887
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019888 case 's':
19889 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19890 p = highlight_color(id, what, modec);
19891 else /* standout */
19892 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 break;
19894
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019895 case 'u':
19896 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19897 /* underline */
19898 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19899 else
19900 /* undercurl */
19901 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902 break;
19903 }
19904
19905 if (p != NULL)
19906 p = vim_strsave(p);
19907#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019908 rettv->v_type = VAR_STRING;
19909 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910}
19911
19912/*
19913 * "synIDtrans(id)" function
19914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019916f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917{
19918 int id;
19919
19920#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019921 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922
19923 if (id > 0)
19924 id = syn_get_final_id(id);
19925 else
19926#endif
19927 id = 0;
19928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019929 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930}
19931
19932/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019933 * "synconcealed(lnum, col)" function
19934 */
19935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019936f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019937{
19938#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19939 long lnum;
19940 long col;
19941 int syntax_flags = 0;
19942 int cchar;
19943 int matchid = 0;
19944 char_u str[NUMBUFLEN];
19945#endif
19946
19947 rettv->v_type = VAR_LIST;
19948 rettv->vval.v_list = NULL;
19949
19950#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19951 lnum = get_tv_lnum(argvars); /* -1 on type error */
19952 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19953
19954 vim_memset(str, NUL, sizeof(str));
19955
19956 if (rettv_list_alloc(rettv) != FAIL)
19957 {
19958 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19959 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19960 && curwin->w_p_cole > 0)
19961 {
19962 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19963 syntax_flags = get_syntax_info(&matchid);
19964
19965 /* get the conceal character */
19966 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19967 {
19968 cchar = syn_get_sub_char();
19969 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19970 cchar = lcs_conceal;
19971 if (cchar != NUL)
19972 {
19973# ifdef FEAT_MBYTE
19974 if (has_mbyte)
19975 (*mb_char2bytes)(cchar, str);
19976 else
19977# endif
19978 str[0] = cchar;
19979 }
19980 }
19981 }
19982
19983 list_append_number(rettv->vval.v_list,
19984 (syntax_flags & HL_CONCEAL) != 0);
19985 /* -1 to auto-determine strlen */
19986 list_append_string(rettv->vval.v_list, str, -1);
19987 list_append_number(rettv->vval.v_list, matchid);
19988 }
19989#endif
19990}
19991
19992/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019993 * "synstack(lnum, col)" function
19994 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019996f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019997{
19998#ifdef FEAT_SYN_HL
19999 long lnum;
20000 long col;
20001 int i;
20002 int id;
20003#endif
20004
20005 rettv->v_type = VAR_LIST;
20006 rettv->vval.v_list = NULL;
20007
20008#ifdef FEAT_SYN_HL
20009 lnum = get_tv_lnum(argvars); /* -1 on type error */
20010 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20011
20012 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020013 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020014 && rettv_list_alloc(rettv) != FAIL)
20015 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020016 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020017 for (i = 0; ; ++i)
20018 {
20019 id = syn_get_stack_item(i);
20020 if (id < 0)
20021 break;
20022 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20023 break;
20024 }
20025 }
20026#endif
20027}
20028
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020030get_cmd_output_as_rettv(
20031 typval_T *argvars,
20032 typval_T *rettv,
20033 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020035 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020037 char_u *infile = NULL;
20038 char_u buf[NUMBUFLEN];
20039 int err = FALSE;
20040 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020041 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020042 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020044 rettv->v_type = VAR_STRING;
20045 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020046 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020047 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020048
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020049 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020050 {
20051 /*
20052 * Write the string to a temp file, to be used for input of the shell
20053 * command.
20054 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020055 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020056 {
20057 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020058 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020059 }
20060
20061 fd = mch_fopen((char *)infile, WRITEBIN);
20062 if (fd == NULL)
20063 {
20064 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020065 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020066 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020067 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020068 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020069 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20070 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020071 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020072 else
20073 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020074 size_t len;
20075
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020076 p = get_tv_string_buf_chk(&argvars[1], buf);
20077 if (p == NULL)
20078 {
20079 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020080 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020081 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020082 len = STRLEN(p);
20083 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020084 err = TRUE;
20085 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020086 if (fclose(fd) != 0)
20087 err = TRUE;
20088 if (err)
20089 {
20090 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020091 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020092 }
20093 }
20094
Bram Moolenaar52a72462014-08-29 15:53:52 +020020095 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20096 * echoes typeahead, that messes up the display. */
20097 if (!msg_silent)
20098 flags += SHELL_COOKED;
20099
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020100 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020102 int len;
20103 listitem_T *li;
20104 char_u *s = NULL;
20105 char_u *start;
20106 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020107 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108
Bram Moolenaar52a72462014-08-29 15:53:52 +020020109 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020110 if (res == NULL)
20111 goto errret;
20112
20113 list = list_alloc();
20114 if (list == NULL)
20115 goto errret;
20116
20117 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020119 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020120 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020121 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020122 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020123
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020124 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020125 if (s == NULL)
20126 goto errret;
20127
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020128 for (p = s; start < end; ++p, ++start)
20129 *p = *start == NUL ? NL : *start;
20130 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020131
20132 li = listitem_alloc();
20133 if (li == NULL)
20134 {
20135 vim_free(s);
20136 goto errret;
20137 }
20138 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020139 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020140 li->li_tv.vval.v_string = s;
20141 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020143
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020144 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020145 rettv->v_type = VAR_LIST;
20146 rettv->vval.v_list = list;
20147 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020149 else
20150 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020151 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020152#ifdef USE_CR
20153 /* translate <CR> into <NL> */
20154 if (res != NULL)
20155 {
20156 char_u *s;
20157
20158 for (s = res; *s; ++s)
20159 {
20160 if (*s == CAR)
20161 *s = NL;
20162 }
20163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164#else
20165# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020166 /* translate <CR><NL> into <NL> */
20167 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020169 char_u *s, *d;
20170
20171 d = res;
20172 for (s = res; *s; ++s)
20173 {
20174 if (s[0] == CAR && s[1] == NL)
20175 ++s;
20176 *d++ = *s;
20177 }
20178 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180# endif
20181#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020182 rettv->vval.v_string = res;
20183 res = NULL;
20184 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020185
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020186errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020187 if (infile != NULL)
20188 {
20189 mch_remove(infile);
20190 vim_free(infile);
20191 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020192 if (res != NULL)
20193 vim_free(res);
20194 if (list != NULL)
20195 list_free(list, TRUE);
20196}
20197
20198/*
20199 * "system()" function
20200 */
20201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020202f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020203{
20204 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20205}
20206
20207/*
20208 * "systemlist()" function
20209 */
20210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020211f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020212{
20213 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214}
20215
20216/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020217 * "tabpagebuflist()" function
20218 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020220f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020221{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020222#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020223 tabpage_T *tp;
20224 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020225
20226 if (argvars[0].v_type == VAR_UNKNOWN)
20227 wp = firstwin;
20228 else
20229 {
20230 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20231 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020232 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020233 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020234 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020235 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020236 for (; wp != NULL; wp = wp->w_next)
20237 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020238 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020239 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020240 }
20241#endif
20242}
20243
20244
20245/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020246 * "tabpagenr()" function
20247 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020249f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020250{
20251 int nr = 1;
20252#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020253 char_u *arg;
20254
20255 if (argvars[0].v_type != VAR_UNKNOWN)
20256 {
20257 arg = get_tv_string_chk(&argvars[0]);
20258 nr = 0;
20259 if (arg != NULL)
20260 {
20261 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020262 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020263 else
20264 EMSG2(_(e_invexpr2), arg);
20265 }
20266 }
20267 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020268 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020269#endif
20270 rettv->vval.v_number = nr;
20271}
20272
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020273
20274#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020275static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020276
20277/*
20278 * Common code for tabpagewinnr() and winnr().
20279 */
20280 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020281get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020282{
20283 win_T *twin;
20284 int nr = 1;
20285 win_T *wp;
20286 char_u *arg;
20287
20288 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20289 if (argvar->v_type != VAR_UNKNOWN)
20290 {
20291 arg = get_tv_string_chk(argvar);
20292 if (arg == NULL)
20293 nr = 0; /* type error; errmsg already given */
20294 else if (STRCMP(arg, "$") == 0)
20295 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20296 else if (STRCMP(arg, "#") == 0)
20297 {
20298 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20299 if (twin == NULL)
20300 nr = 0;
20301 }
20302 else
20303 {
20304 EMSG2(_(e_invexpr2), arg);
20305 nr = 0;
20306 }
20307 }
20308
20309 if (nr > 0)
20310 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20311 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020312 {
20313 if (wp == NULL)
20314 {
20315 /* didn't find it in this tabpage */
20316 nr = 0;
20317 break;
20318 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020319 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020320 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020321 return nr;
20322}
20323#endif
20324
20325/*
20326 * "tabpagewinnr()" function
20327 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020329f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020330{
20331 int nr = 1;
20332#ifdef FEAT_WINDOWS
20333 tabpage_T *tp;
20334
20335 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20336 if (tp == NULL)
20337 nr = 0;
20338 else
20339 nr = get_winnr(tp, &argvars[1]);
20340#endif
20341 rettv->vval.v_number = nr;
20342}
20343
20344
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020345/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020346 * "tagfiles()" function
20347 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020349f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020350{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020351 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020352 tagname_T tn;
20353 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020354
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020355 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020356 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020357 fname = alloc(MAXPATHL);
20358 if (fname == NULL)
20359 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020360
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020361 for (first = TRUE; ; first = FALSE)
20362 if (get_tagfname(&tn, first, fname) == FAIL
20363 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020364 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020365 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020366 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020367}
20368
20369/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020370 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020371 */
20372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020373f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020374{
20375 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020376
20377 tag_pattern = get_tv_string(&argvars[0]);
20378
20379 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020380 if (*tag_pattern == NUL)
20381 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020382
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020383 if (rettv_list_alloc(rettv) == OK)
20384 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020385}
20386
20387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 * "tempname()" function
20389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020391f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392{
20393 static int x = 'A';
20394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020395 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020396 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397
20398 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20399 * names. Skip 'I' and 'O', they are used for shell redirection. */
20400 do
20401 {
20402 if (x == 'Z')
20403 x = '0';
20404 else if (x == '9')
20405 x = 'A';
20406 else
20407 {
20408#ifdef EBCDIC
20409 if (x == 'I')
20410 x = 'J';
20411 else if (x == 'R')
20412 x = 'S';
20413 else
20414#endif
20415 ++x;
20416 }
20417 } while (x == 'I' || x == 'O');
20418}
20419
20420/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020421 * "test(list)" function: Just checking the walls...
20422 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020424f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020425{
20426 /* Used for unit testing. Change the code below to your liking. */
20427#if 0
20428 listitem_T *li;
20429 list_T *l;
20430 char_u *bad, *good;
20431
20432 if (argvars[0].v_type != VAR_LIST)
20433 return;
20434 l = argvars[0].vval.v_list;
20435 if (l == NULL)
20436 return;
20437 li = l->lv_first;
20438 if (li == NULL)
20439 return;
20440 bad = get_tv_string(&li->li_tv);
20441 li = li->li_next;
20442 if (li == NULL)
20443 return;
20444 good = get_tv_string(&li->li_tv);
20445 rettv->vval.v_number = test_edit_score(bad, good);
20446#endif
20447}
20448
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020449#ifdef FEAT_FLOAT
20450/*
20451 * "tan()" function
20452 */
20453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020454f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020455{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020456 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020457
20458 rettv->v_type = VAR_FLOAT;
20459 if (get_float_arg(argvars, &f) == OK)
20460 rettv->vval.v_float = tan(f);
20461 else
20462 rettv->vval.v_float = 0.0;
20463}
20464
20465/*
20466 * "tanh()" function
20467 */
20468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020469f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020470{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020471 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020472
20473 rettv->v_type = VAR_FLOAT;
20474 if (get_float_arg(argvars, &f) == OK)
20475 rettv->vval.v_float = tanh(f);
20476 else
20477 rettv->vval.v_float = 0.0;
20478}
20479#endif
20480
Bram Moolenaard52d9742005-08-21 22:20:28 +000020481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482 * "tolower(string)" function
20483 */
20484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020485f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486{
20487 char_u *p;
20488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020489 p = vim_strsave(get_tv_string(&argvars[0]));
20490 rettv->v_type = VAR_STRING;
20491 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492
20493 if (p != NULL)
20494 while (*p != NUL)
20495 {
20496#ifdef FEAT_MBYTE
20497 int l;
20498
20499 if (enc_utf8)
20500 {
20501 int c, lc;
20502
20503 c = utf_ptr2char(p);
20504 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020505 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506 /* TODO: reallocate string when byte count changes. */
20507 if (utf_char2len(lc) == l)
20508 utf_char2bytes(lc, p);
20509 p += l;
20510 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020511 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 p += l; /* skip multi-byte character */
20513 else
20514#endif
20515 {
20516 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20517 ++p;
20518 }
20519 }
20520}
20521
20522/*
20523 * "toupper(string)" function
20524 */
20525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020526f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020528 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020529 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530}
20531
20532/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020533 * "tr(string, fromstr, tostr)" function
20534 */
20535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020536f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020537{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020538 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020539 char_u *fromstr;
20540 char_u *tostr;
20541 char_u *p;
20542#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020543 int inlen;
20544 int fromlen;
20545 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020546 int idx;
20547 char_u *cpstr;
20548 int cplen;
20549 int first = TRUE;
20550#endif
20551 char_u buf[NUMBUFLEN];
20552 char_u buf2[NUMBUFLEN];
20553 garray_T ga;
20554
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020555 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020556 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20557 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020558
20559 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020560 rettv->v_type = VAR_STRING;
20561 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020562 if (fromstr == NULL || tostr == NULL)
20563 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020564 ga_init2(&ga, (int)sizeof(char), 80);
20565
20566#ifdef FEAT_MBYTE
20567 if (!has_mbyte)
20568#endif
20569 /* not multi-byte: fromstr and tostr must be the same length */
20570 if (STRLEN(fromstr) != STRLEN(tostr))
20571 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020572#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020573error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020574#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020575 EMSG2(_(e_invarg2), fromstr);
20576 ga_clear(&ga);
20577 return;
20578 }
20579
20580 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020581 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020582 {
20583#ifdef FEAT_MBYTE
20584 if (has_mbyte)
20585 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020586 inlen = (*mb_ptr2len)(in_str);
20587 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020588 cplen = inlen;
20589 idx = 0;
20590 for (p = fromstr; *p != NUL; p += fromlen)
20591 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020592 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020593 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020594 {
20595 for (p = tostr; *p != NUL; p += tolen)
20596 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020597 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020598 if (idx-- == 0)
20599 {
20600 cplen = tolen;
20601 cpstr = p;
20602 break;
20603 }
20604 }
20605 if (*p == NUL) /* tostr is shorter than fromstr */
20606 goto error;
20607 break;
20608 }
20609 ++idx;
20610 }
20611
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020612 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020613 {
20614 /* Check that fromstr and tostr have the same number of
20615 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020616 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020617 first = FALSE;
20618 for (p = tostr; *p != NUL; p += tolen)
20619 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020620 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020621 --idx;
20622 }
20623 if (idx != 0)
20624 goto error;
20625 }
20626
Bram Moolenaarcde88542015-08-11 19:14:00 +020020627 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020628 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020629 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020630
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020631 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020632 }
20633 else
20634#endif
20635 {
20636 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020637 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020638 if (p != NULL)
20639 ga_append(&ga, tostr[p - fromstr]);
20640 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020641 ga_append(&ga, *in_str);
20642 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020643 }
20644 }
20645
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020646 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020647 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020648 ga_append(&ga, NUL);
20649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020650 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020651}
20652
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020653#ifdef FEAT_FLOAT
20654/*
20655 * "trunc({float})" function
20656 */
20657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020658f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020659{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020660 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020661
20662 rettv->v_type = VAR_FLOAT;
20663 if (get_float_arg(argvars, &f) == OK)
20664 /* trunc() is not in C90, use floor() or ceil() instead. */
20665 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20666 else
20667 rettv->vval.v_float = 0.0;
20668}
20669#endif
20670
Bram Moolenaar8299df92004-07-10 09:47:34 +000020671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 * "type(expr)" function
20673 */
20674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020675f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020677 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020678
20679 switch (argvars[0].v_type)
20680 {
20681 case VAR_NUMBER: n = 0; break;
20682 case VAR_STRING: n = 1; break;
20683 case VAR_FUNC: n = 2; break;
20684 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020685 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020686 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020687 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020688 if (argvars[0].vval.v_number == VVAL_FALSE
20689 || argvars[0].vval.v_number == VVAL_TRUE)
20690 n = 6;
20691 else
20692 n = 7;
20693 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020694 case VAR_JOB: n = 8; break;
20695 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020696 case VAR_UNKNOWN:
20697 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20698 n = -1;
20699 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020700 }
20701 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702}
20703
20704/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020705 * "undofile(name)" function
20706 */
20707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020708f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020709{
20710 rettv->v_type = VAR_STRING;
20711#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020712 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020713 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020714
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020715 if (*fname == NUL)
20716 {
20717 /* If there is no file name there will be no undo file. */
20718 rettv->vval.v_string = NULL;
20719 }
20720 else
20721 {
20722 char_u *ffname = FullName_save(fname, FALSE);
20723
20724 if (ffname != NULL)
20725 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20726 vim_free(ffname);
20727 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020728 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020729#else
20730 rettv->vval.v_string = NULL;
20731#endif
20732}
20733
20734/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020735 * "undotree()" function
20736 */
20737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020738f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020739{
20740 if (rettv_dict_alloc(rettv) == OK)
20741 {
20742 dict_T *dict = rettv->vval.v_dict;
20743 list_T *list;
20744
Bram Moolenaar730cde92010-06-27 05:18:54 +020020745 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020746 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020747 dict_add_nr_str(dict, "save_last",
20748 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020749 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20750 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020751 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020752
20753 list = list_alloc();
20754 if (list != NULL)
20755 {
20756 u_eval_tree(curbuf->b_u_oldhead, list);
20757 dict_add_list(dict, "entries", list);
20758 }
20759 }
20760}
20761
20762/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020763 * "values(dict)" function
20764 */
20765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020766f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020767{
20768 dict_list(argvars, rettv, 1);
20769}
20770
20771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772 * "virtcol(string)" function
20773 */
20774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020775f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776{
20777 colnr_T vcol = 0;
20778 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020779 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020781 fp = var2fpos(&argvars[0], FALSE, &fnum);
20782 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20783 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784 {
20785 getvvcol(curwin, fp, NULL, NULL, &vcol);
20786 ++vcol;
20787 }
20788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020789 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790}
20791
20792/*
20793 * "visualmode()" function
20794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020796f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798 char_u str[2];
20799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020800 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 str[0] = curbuf->b_visual_mode_eval;
20802 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020803 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804
20805 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020806 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020808}
20809
20810/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020811 * "wildmenumode()" function
20812 */
20813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020814f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020815{
20816#ifdef FEAT_WILDMENU
20817 if (wild_menu_showing)
20818 rettv->vval.v_number = 1;
20819#endif
20820}
20821
20822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 * "winbufnr(nr)" function
20824 */
20825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020826f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827{
20828 win_T *wp;
20829
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020830 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020832 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020834 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835}
20836
20837/*
20838 * "wincol()" function
20839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020841f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842{
20843 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020844 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020845}
20846
20847/*
20848 * "winheight(nr)" function
20849 */
20850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020851f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852{
20853 win_T *wp;
20854
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020855 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020857 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020859 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860}
20861
20862/*
20863 * "winline()" function
20864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020866f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020867{
20868 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020869 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870}
20871
20872/*
20873 * "winnr()" function
20874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020876f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877{
20878 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020879
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020881 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020883 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884}
20885
20886/*
20887 * "winrestcmd()" function
20888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020890f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020891{
20892#ifdef FEAT_WINDOWS
20893 win_T *wp;
20894 int winnr = 1;
20895 garray_T ga;
20896 char_u buf[50];
20897
20898 ga_init2(&ga, (int)sizeof(char), 70);
20899 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20900 {
20901 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20902 ga_concat(&ga, buf);
20903# ifdef FEAT_VERTSPLIT
20904 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20905 ga_concat(&ga, buf);
20906# endif
20907 ++winnr;
20908 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020909 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020911 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020913 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020915 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916}
20917
20918/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020919 * "winrestview()" function
20920 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020922f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020923{
20924 dict_T *dict;
20925
20926 if (argvars[0].v_type != VAR_DICT
20927 || (dict = argvars[0].vval.v_dict) == NULL)
20928 EMSG(_(e_invarg));
20929 else
20930 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020931 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20932 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20933 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20934 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020935#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020936 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20937 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020938#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020939 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20940 {
20941 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20942 curwin->w_set_curswant = FALSE;
20943 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020944
Bram Moolenaar82c25852014-05-28 16:47:16 +020020945 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20946 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020947#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020948 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20949 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020950#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020951 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20952 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20953 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20954 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020955
20956 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020957 win_new_height(curwin, curwin->w_height);
20958# ifdef FEAT_VERTSPLIT
20959 win_new_width(curwin, W_WIDTH(curwin));
20960# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020961 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020962
Bram Moolenaarb851a962014-10-31 15:45:52 +010020963 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020964 curwin->w_topline = 1;
20965 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20966 curwin->w_topline = curbuf->b_ml.ml_line_count;
20967#ifdef FEAT_DIFF
20968 check_topfill(curwin, TRUE);
20969#endif
20970 }
20971}
20972
20973/*
20974 * "winsaveview()" function
20975 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020977f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020978{
20979 dict_T *dict;
20980
Bram Moolenaara800b422010-06-27 01:15:55 +020020981 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020982 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020983 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020984
20985 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20986 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20987#ifdef FEAT_VIRTUALEDIT
20988 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20989#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020990 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020991 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20992
20993 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20994#ifdef FEAT_DIFF
20995 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20996#endif
20997 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20998 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20999}
21000
21001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021002 * "winwidth(nr)" function
21003 */
21004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021005f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006{
21007 win_T *wp;
21008
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021009 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021010 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021011 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 else
21013#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021014 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021016 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017#endif
21018}
21019
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021021 * "wordcount()" function
21022 */
21023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021024f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021025{
21026 if (rettv_dict_alloc(rettv) == FAIL)
21027 return;
21028 cursor_pos_info(rettv->vval.v_dict);
21029}
21030
21031/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021032 * Write list of strings to file
21033 */
21034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021035write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021036{
21037 listitem_T *li;
21038 int c;
21039 int ret = OK;
21040 char_u *s;
21041
21042 for (li = list->lv_first; li != NULL; li = li->li_next)
21043 {
21044 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21045 {
21046 if (*s == '\n')
21047 c = putc(NUL, fd);
21048 else
21049 c = putc(*s, fd);
21050 if (c == EOF)
21051 {
21052 ret = FAIL;
21053 break;
21054 }
21055 }
21056 if (!binary || li->li_next != NULL)
21057 if (putc('\n', fd) == EOF)
21058 {
21059 ret = FAIL;
21060 break;
21061 }
21062 if (ret == FAIL)
21063 {
21064 EMSG(_(e_write));
21065 break;
21066 }
21067 }
21068 return ret;
21069}
21070
21071/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021072 * "writefile()" function
21073 */
21074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021075f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021076{
21077 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021078 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021079 char_u *fname;
21080 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021081 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021082
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021083 if (check_restricted() || check_secure())
21084 return;
21085
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021086 if (argvars[0].v_type != VAR_LIST)
21087 {
21088 EMSG2(_(e_listarg), "writefile()");
21089 return;
21090 }
21091 if (argvars[0].vval.v_list == NULL)
21092 return;
21093
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021094 if (argvars[2].v_type != VAR_UNKNOWN)
21095 {
21096 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21097 binary = TRUE;
21098 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21099 append = TRUE;
21100 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021101
21102 /* Always open the file in binary mode, library functions have a mind of
21103 * their own about CR-LF conversion. */
21104 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021105 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21106 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021107 {
21108 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21109 ret = -1;
21110 }
21111 else
21112 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021113 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21114 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021115 fclose(fd);
21116 }
21117
21118 rettv->vval.v_number = ret;
21119}
21120
21121/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021122 * "xor(expr, expr)" function
21123 */
21124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021125f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021126{
21127 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21128 ^ get_tv_number_chk(&argvars[1], NULL);
21129}
21130
21131
21132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021134 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 */
21136 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021137var2fpos(
21138 typval_T *varp,
21139 int dollar_lnum, /* TRUE when $ is last line */
21140 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021142 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021144 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145
Bram Moolenaara5525202006-03-02 22:52:09 +000021146 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021147 if (varp->v_type == VAR_LIST)
21148 {
21149 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021150 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021151 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021152 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021153
21154 l = varp->vval.v_list;
21155 if (l == NULL)
21156 return NULL;
21157
21158 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021159 pos.lnum = list_find_nr(l, 0L, &error);
21160 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021161 return NULL; /* invalid line number */
21162
21163 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021164 pos.col = list_find_nr(l, 1L, &error);
21165 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021166 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021167 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021168
21169 /* We accept "$" for the column number: last column. */
21170 li = list_find(l, 1L);
21171 if (li != NULL && li->li_tv.v_type == VAR_STRING
21172 && li->li_tv.vval.v_string != NULL
21173 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21174 pos.col = len + 1;
21175
Bram Moolenaara5525202006-03-02 22:52:09 +000021176 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021177 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021178 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021179 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021180
Bram Moolenaara5525202006-03-02 22:52:09 +000021181#ifdef FEAT_VIRTUALEDIT
21182 /* Get the virtual offset. Defaults to zero. */
21183 pos.coladd = list_find_nr(l, 2L, &error);
21184 if (error)
21185 pos.coladd = 0;
21186#endif
21187
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021188 return &pos;
21189 }
21190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021191 name = get_tv_string_chk(varp);
21192 if (name == NULL)
21193 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021194 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021196 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21197 {
21198 if (VIsual_active)
21199 return &VIsual;
21200 return &curwin->w_cursor;
21201 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021202 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021204 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21206 return NULL;
21207 return pp;
21208 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021209
21210#ifdef FEAT_VIRTUALEDIT
21211 pos.coladd = 0;
21212#endif
21213
Bram Moolenaar477933c2007-07-17 14:32:23 +000021214 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021215 {
21216 pos.col = 0;
21217 if (name[1] == '0') /* "w0": first visible line */
21218 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021219 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021220 pos.lnum = curwin->w_topline;
21221 return &pos;
21222 }
21223 else if (name[1] == '$') /* "w$": last visible line */
21224 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021225 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021226 pos.lnum = curwin->w_botline - 1;
21227 return &pos;
21228 }
21229 }
21230 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021232 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 {
21234 pos.lnum = curbuf->b_ml.ml_line_count;
21235 pos.col = 0;
21236 }
21237 else
21238 {
21239 pos.lnum = curwin->w_cursor.lnum;
21240 pos.col = (colnr_T)STRLEN(ml_get_curline());
21241 }
21242 return &pos;
21243 }
21244 return NULL;
21245}
21246
21247/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021248 * Convert list in "arg" into a position and optional file number.
21249 * When "fnump" is NULL there is no file number, only 3 items.
21250 * Note that the column is passed on as-is, the caller may want to decrement
21251 * it to use 1 for the first column.
21252 * Return FAIL when conversion is not possible, doesn't check the position for
21253 * validity.
21254 */
21255 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021256list2fpos(
21257 typval_T *arg,
21258 pos_T *posp,
21259 int *fnump,
21260 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021261{
21262 list_T *l = arg->vval.v_list;
21263 long i = 0;
21264 long n;
21265
Bram Moolenaar493c1782014-05-28 14:34:46 +020021266 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21267 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021268 if (arg->v_type != VAR_LIST
21269 || l == NULL
21270 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021271 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021272 return FAIL;
21273
21274 if (fnump != NULL)
21275 {
21276 n = list_find_nr(l, i++, NULL); /* fnum */
21277 if (n < 0)
21278 return FAIL;
21279 if (n == 0)
21280 n = curbuf->b_fnum; /* current buffer */
21281 *fnump = n;
21282 }
21283
21284 n = list_find_nr(l, i++, NULL); /* lnum */
21285 if (n < 0)
21286 return FAIL;
21287 posp->lnum = n;
21288
21289 n = list_find_nr(l, i++, NULL); /* col */
21290 if (n < 0)
21291 return FAIL;
21292 posp->col = n;
21293
21294#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021295 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021296 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021297 posp->coladd = 0;
21298 else
21299 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021300#endif
21301
Bram Moolenaar493c1782014-05-28 14:34:46 +020021302 if (curswantp != NULL)
21303 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21304
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021305 return OK;
21306}
21307
21308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 * Get the length of an environment variable name.
21310 * Advance "arg" to the first character after the name.
21311 * Return 0 for error.
21312 */
21313 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021314get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315{
21316 char_u *p;
21317 int len;
21318
21319 for (p = *arg; vim_isIDc(*p); ++p)
21320 ;
21321 if (p == *arg) /* no name found */
21322 return 0;
21323
21324 len = (int)(p - *arg);
21325 *arg = p;
21326 return len;
21327}
21328
21329/*
21330 * Get the length of the name of a function or internal variable.
21331 * "arg" is advanced to the first non-white character after the name.
21332 * Return 0 if something is wrong.
21333 */
21334 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021335get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336{
21337 char_u *p;
21338 int len;
21339
21340 /* Find the end of the name. */
21341 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021342 {
21343 if (*p == ':')
21344 {
21345 /* "s:" is start of "s:var", but "n:" is not and can be used in
21346 * slice "[n:]". Also "xx:" is not a namespace. */
21347 len = (int)(p - *arg);
21348 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21349 || len > 1)
21350 break;
21351 }
21352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 if (p == *arg) /* no name found */
21354 return 0;
21355
21356 len = (int)(p - *arg);
21357 *arg = skipwhite(p);
21358
21359 return len;
21360}
21361
21362/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021363 * Get the length of the name of a variable or function.
21364 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021366 * Return -1 if curly braces expansion failed.
21367 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 * If the name contains 'magic' {}'s, expand them and return the
21369 * expanded name in an allocated string via 'alias' - caller must free.
21370 */
21371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021372get_name_len(
21373 char_u **arg,
21374 char_u **alias,
21375 int evaluate,
21376 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377{
21378 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 char_u *p;
21380 char_u *expr_start;
21381 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382
21383 *alias = NULL; /* default to no alias */
21384
21385 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21386 && (*arg)[2] == (int)KE_SNR)
21387 {
21388 /* hard coded <SNR>, already translated */
21389 *arg += 3;
21390 return get_id_len(arg) + 3;
21391 }
21392 len = eval_fname_script(*arg);
21393 if (len > 0)
21394 {
21395 /* literal "<SID>", "s:" or "<SNR>" */
21396 *arg += len;
21397 }
21398
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021400 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021402 p = find_name_end(*arg, &expr_start, &expr_end,
21403 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 if (expr_start != NULL)
21405 {
21406 char_u *temp_string;
21407
21408 if (!evaluate)
21409 {
21410 len += (int)(p - *arg);
21411 *arg = skipwhite(p);
21412 return len;
21413 }
21414
21415 /*
21416 * Include any <SID> etc in the expanded string:
21417 * Thus the -len here.
21418 */
21419 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21420 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021421 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 *alias = temp_string;
21423 *arg = skipwhite(p);
21424 return (int)STRLEN(temp_string);
21425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426
21427 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021428 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429 EMSG2(_(e_invexpr2), *arg);
21430
21431 return len;
21432}
21433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434/*
21435 * Find the end of a variable or function name, taking care of magic braces.
21436 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21437 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021438 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021439 * Return a pointer to just after the name. Equal to "arg" if there is no
21440 * valid name.
21441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021443find_name_end(
21444 char_u *arg,
21445 char_u **expr_start,
21446 char_u **expr_end,
21447 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021449 int mb_nest = 0;
21450 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021452 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021454 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021456 *expr_start = NULL;
21457 *expr_end = NULL;
21458 }
21459
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021460 /* Quick check for valid starting character. */
21461 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21462 return arg;
21463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021464 for (p = arg; *p != NUL
21465 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021466 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021467 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021468 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021469 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021470 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021471 if (*p == '\'')
21472 {
21473 /* skip over 'string' to avoid counting [ and ] inside it. */
21474 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21475 ;
21476 if (*p == NUL)
21477 break;
21478 }
21479 else if (*p == '"')
21480 {
21481 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21482 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21483 if (*p == '\\' && p[1] != NUL)
21484 ++p;
21485 if (*p == NUL)
21486 break;
21487 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021488 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21489 {
21490 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021491 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021492 len = (int)(p - arg);
21493 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021494 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021495 break;
21496 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021498 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021500 if (*p == '[')
21501 ++br_nest;
21502 else if (*p == ']')
21503 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021506 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021508 if (*p == '{')
21509 {
21510 mb_nest++;
21511 if (expr_start != NULL && *expr_start == NULL)
21512 *expr_start = p;
21513 }
21514 else if (*p == '}')
21515 {
21516 mb_nest--;
21517 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21518 *expr_end = p;
21519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521 }
21522
21523 return p;
21524}
21525
21526/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021527 * Expands out the 'magic' {}'s in a variable/function name.
21528 * Note that this can call itself recursively, to deal with
21529 * constructs like foo{bar}{baz}{bam}
21530 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21531 * "in_start" ^
21532 * "expr_start" ^
21533 * "expr_end" ^
21534 * "in_end" ^
21535 *
21536 * Returns a new allocated string, which the caller must free.
21537 * Returns NULL for failure.
21538 */
21539 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021540make_expanded_name(
21541 char_u *in_start,
21542 char_u *expr_start,
21543 char_u *expr_end,
21544 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021545{
21546 char_u c1;
21547 char_u *retval = NULL;
21548 char_u *temp_result;
21549 char_u *nextcmd = NULL;
21550
21551 if (expr_end == NULL || in_end == NULL)
21552 return NULL;
21553 *expr_start = NUL;
21554 *expr_end = NUL;
21555 c1 = *in_end;
21556 *in_end = NUL;
21557
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021558 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021559 if (temp_result != NULL && nextcmd == NULL)
21560 {
21561 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21562 + (in_end - expr_end) + 1));
21563 if (retval != NULL)
21564 {
21565 STRCPY(retval, in_start);
21566 STRCAT(retval, temp_result);
21567 STRCAT(retval, expr_end + 1);
21568 }
21569 }
21570 vim_free(temp_result);
21571
21572 *in_end = c1; /* put char back for error messages */
21573 *expr_start = '{';
21574 *expr_end = '}';
21575
21576 if (retval != NULL)
21577 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021578 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021579 if (expr_start != NULL)
21580 {
21581 /* Further expansion! */
21582 temp_result = make_expanded_name(retval, expr_start,
21583 expr_end, temp_result);
21584 vim_free(retval);
21585 retval = temp_result;
21586 }
21587 }
21588
21589 return retval;
21590}
21591
21592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021594 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 */
21596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021597eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021599 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21600}
21601
21602/*
21603 * Return TRUE if character "c" can be used as the first character in a
21604 * variable or function name (excluding '{' and '}').
21605 */
21606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021607eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021608{
21609 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610}
21611
21612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 * Set number v: variable to "val".
21614 */
21615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021616set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021618 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619}
21620
21621/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021622 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 */
21624 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021625get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021627 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628}
21629
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021630/*
21631 * Get string v: variable value. Uses a static buffer, can only be used once.
21632 */
21633 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021634get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021635{
21636 return get_tv_string(&vimvars[idx].vv_tv);
21637}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021638
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021640 * Get List v: variable value. Caller must take care of reference count when
21641 * needed.
21642 */
21643 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021644get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021645{
21646 return vimvars[idx].vv_list;
21647}
21648
21649/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021650 * Set v:char to character "c".
21651 */
21652 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021653set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021654{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021655 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021656
21657#ifdef FEAT_MBYTE
21658 if (has_mbyte)
21659 buf[(*mb_char2bytes)(c, buf)] = NUL;
21660 else
21661#endif
21662 {
21663 buf[0] = c;
21664 buf[1] = NUL;
21665 }
21666 set_vim_var_string(VV_CHAR, buf, -1);
21667}
21668
21669/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021670 * Set v:count to "count" and v:count1 to "count1".
21671 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 */
21673 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021674set_vcount(
21675 long count,
21676 long count1,
21677 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021679 if (set_prevcount)
21680 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021681 vimvars[VV_COUNT].vv_nr = count;
21682 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683}
21684
21685/*
21686 * Set string v: variable to a copy of "val".
21687 */
21688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021689set_vim_var_string(
21690 int idx,
21691 char_u *val,
21692 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693{
Bram Moolenaara542c682016-01-31 16:28:04 +010021694 clear_tv(&vimvars[idx].vv_di.di_tv);
21695 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021697 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021699 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021701 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702}
21703
21704/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021705 * Set List v: variable to "val".
21706 */
21707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021708set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021709{
Bram Moolenaara542c682016-01-31 16:28:04 +010021710 clear_tv(&vimvars[idx].vv_di.di_tv);
21711 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021712 vimvars[idx].vv_list = val;
21713 if (val != NULL)
21714 ++val->lv_refcount;
21715}
21716
21717/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021718 * Set Dictionary v: variable to "val".
21719 */
21720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021721set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021722{
21723 int todo;
21724 hashitem_T *hi;
21725
Bram Moolenaara542c682016-01-31 16:28:04 +010021726 clear_tv(&vimvars[idx].vv_di.di_tv);
21727 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021728 vimvars[idx].vv_dict = val;
21729 if (val != NULL)
21730 {
21731 ++val->dv_refcount;
21732
21733 /* Set readonly */
21734 todo = (int)val->dv_hashtab.ht_used;
21735 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21736 {
21737 if (HASHITEM_EMPTY(hi))
21738 continue;
21739 --todo;
21740 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21741 }
21742 }
21743}
21744
21745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 * Set v:register if needed.
21747 */
21748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021749set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750{
21751 char_u regname;
21752
21753 if (c == 0 || c == ' ')
21754 regname = '"';
21755 else
21756 regname = c;
21757 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021758 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 set_vim_var_string(VV_REG, &regname, 1);
21760}
21761
21762/*
21763 * Get or set v:exception. If "oldval" == NULL, return the current value.
21764 * Otherwise, restore the value to "oldval" and return NULL.
21765 * Must always be called in pairs to save and restore v:exception! Does not
21766 * take care of memory allocations.
21767 */
21768 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021769v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770{
21771 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021772 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773
Bram Moolenaare9a41262005-01-15 22:18:47 +000021774 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 return NULL;
21776}
21777
21778/*
21779 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21780 * Otherwise, restore the value to "oldval" and return NULL.
21781 * Must always be called in pairs to save and restore v:throwpoint! Does not
21782 * take care of memory allocations.
21783 */
21784 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021785v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786{
21787 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021788 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789
Bram Moolenaare9a41262005-01-15 22:18:47 +000021790 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 return NULL;
21792}
21793
21794#if defined(FEAT_AUTOCMD) || defined(PROTO)
21795/*
21796 * Set v:cmdarg.
21797 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21798 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21799 * Must always be called in pairs!
21800 */
21801 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021802set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803{
21804 char_u *oldval;
21805 char_u *newval;
21806 unsigned len;
21807
Bram Moolenaare9a41262005-01-15 22:18:47 +000021808 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021809 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021811 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021812 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021813 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814 }
21815
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021816 if (eap->force_bin == FORCE_BIN)
21817 len = 6;
21818 else if (eap->force_bin == FORCE_NOBIN)
21819 len = 8;
21820 else
21821 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021822
21823 if (eap->read_edit)
21824 len += 7;
21825
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021826 if (eap->force_ff != 0)
21827 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21828# ifdef FEAT_MBYTE
21829 if (eap->force_enc != 0)
21830 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021831 if (eap->bad_char != 0)
21832 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021833# endif
21834
21835 newval = alloc(len + 1);
21836 if (newval == NULL)
21837 return NULL;
21838
21839 if (eap->force_bin == FORCE_BIN)
21840 sprintf((char *)newval, " ++bin");
21841 else if (eap->force_bin == FORCE_NOBIN)
21842 sprintf((char *)newval, " ++nobin");
21843 else
21844 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021845
21846 if (eap->read_edit)
21847 STRCAT(newval, " ++edit");
21848
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021849 if (eap->force_ff != 0)
21850 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21851 eap->cmd + eap->force_ff);
21852# ifdef FEAT_MBYTE
21853 if (eap->force_enc != 0)
21854 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21855 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021856 if (eap->bad_char == BAD_KEEP)
21857 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21858 else if (eap->bad_char == BAD_DROP)
21859 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21860 else if (eap->bad_char != 0)
21861 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021862# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021863 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021864 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865}
21866#endif
21867
21868/*
21869 * Get the value of internal variable "name".
21870 * Return OK or FAIL.
21871 */
21872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021873get_var_tv(
21874 char_u *name,
21875 int len, /* length of "name" */
21876 typval_T *rettv, /* NULL when only checking existence */
21877 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21878 int verbose, /* may give error message */
21879 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880{
21881 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 typval_T *tv = NULL;
21883 typval_T atv;
21884 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021886
21887 /* truncate the name, so that we can use strcmp() */
21888 cc = name[len];
21889 name[len] = NUL;
21890
21891 /*
21892 * Check for "b:changedtick".
21893 */
21894 if (STRCMP(name, "b:changedtick") == 0)
21895 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021896 atv.v_type = VAR_NUMBER;
21897 atv.vval.v_number = curbuf->b_changedtick;
21898 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 }
21900
21901 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 * Check for user-defined variables.
21903 */
21904 else
21905 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021906 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021908 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021909 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021910 if (dip != NULL)
21911 *dip = v;
21912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 }
21914
Bram Moolenaare9a41262005-01-15 22:18:47 +000021915 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021917 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021918 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 ret = FAIL;
21920 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021921 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021922 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923
21924 name[len] = cc;
21925
21926 return ret;
21927}
21928
21929/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021930 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21931 * Also handle function call with Funcref variable: func(expr)
21932 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21933 */
21934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021935handle_subscript(
21936 char_u **arg,
21937 typval_T *rettv,
21938 int evaluate, /* do more than finding the end */
21939 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021940{
21941 int ret = OK;
21942 dict_T *selfdict = NULL;
21943 char_u *s;
21944 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021945 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021946
21947 while (ret == OK
21948 && (**arg == '['
21949 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021950 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021951 && !vim_iswhite(*(*arg - 1)))
21952 {
21953 if (**arg == '(')
21954 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021955 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021956 if (evaluate)
21957 {
21958 functv = *rettv;
21959 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021960
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021961 /* Invoke the function. Recursive! */
21962 s = functv.vval.v_string;
21963 }
21964 else
21965 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021966 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021967 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21968 &len, evaluate, selfdict);
21969
21970 /* Clear the funcref afterwards, so that deleting it while
21971 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021972 if (evaluate)
21973 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021974
21975 /* Stop the expression evaluation when immediately aborting on
21976 * error, or when an interrupt occurred or an exception was thrown
21977 * but not caught. */
21978 if (aborting())
21979 {
21980 if (ret == OK)
21981 clear_tv(rettv);
21982 ret = FAIL;
21983 }
21984 dict_unref(selfdict);
21985 selfdict = NULL;
21986 }
21987 else /* **arg == '[' || **arg == '.' */
21988 {
21989 dict_unref(selfdict);
21990 if (rettv->v_type == VAR_DICT)
21991 {
21992 selfdict = rettv->vval.v_dict;
21993 if (selfdict != NULL)
21994 ++selfdict->dv_refcount;
21995 }
21996 else
21997 selfdict = NULL;
21998 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21999 {
22000 clear_tv(rettv);
22001 ret = FAIL;
22002 }
22003 }
22004 }
22005 dict_unref(selfdict);
22006 return ret;
22007}
22008
22009/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022010 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022011 * value).
22012 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022013 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022014alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022015{
Bram Moolenaar33570922005-01-25 22:26:29 +000022016 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022017}
22018
22019/*
22020 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 * The string "s" must have been allocated, it is consumed.
22022 * Return NULL for out of memory, the variable otherwise.
22023 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022024 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022025alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022026{
Bram Moolenaar33570922005-01-25 22:26:29 +000022027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022029 rettv = alloc_tv();
22030 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022032 rettv->v_type = VAR_STRING;
22033 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034 }
22035 else
22036 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022037 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038}
22039
22040/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022041 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022043 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022044free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045{
22046 if (varp != NULL)
22047 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022048 switch (varp->v_type)
22049 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022050 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022051 func_unref(varp->vval.v_string);
22052 /*FALLTHROUGH*/
22053 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022054 vim_free(varp->vval.v_string);
22055 break;
22056 case VAR_LIST:
22057 list_unref(varp->vval.v_list);
22058 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022059 case VAR_DICT:
22060 dict_unref(varp->vval.v_dict);
22061 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022062 case VAR_JOB:
22063#ifdef FEAT_JOB
22064 job_unref(varp->vval.v_job);
22065 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022066#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022067 case VAR_CHANNEL:
22068#ifdef FEAT_CHANNEL
22069 channel_unref(varp->vval.v_channel);
22070 break;
22071#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022072 case VAR_NUMBER:
22073 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022074 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022075 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022076 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078 vim_free(varp);
22079 }
22080}
22081
22082/*
22083 * Free the memory for a variable value and set the value to NULL or 0.
22084 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022086clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087{
22088 if (varp != NULL)
22089 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022090 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022092 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022093 func_unref(varp->vval.v_string);
22094 /*FALLTHROUGH*/
22095 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022096 vim_free(varp->vval.v_string);
22097 varp->vval.v_string = NULL;
22098 break;
22099 case VAR_LIST:
22100 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022101 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022102 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022103 case VAR_DICT:
22104 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022105 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022106 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022107 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022108 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022109 varp->vval.v_number = 0;
22110 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022111 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022112#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022113 varp->vval.v_float = 0.0;
22114 break;
22115#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022116 case VAR_JOB:
22117#ifdef FEAT_JOB
22118 job_unref(varp->vval.v_job);
22119 varp->vval.v_job = NULL;
22120#endif
22121 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022122 case VAR_CHANNEL:
22123#ifdef FEAT_CHANNEL
22124 channel_unref(varp->vval.v_channel);
22125 varp->vval.v_channel = NULL;
22126#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022127 case VAR_UNKNOWN:
22128 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022130 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131 }
22132}
22133
22134/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022135 * Set the value of a variable to NULL without freeing items.
22136 */
22137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022138init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022139{
22140 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022141 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022142}
22143
22144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 * Get the number value of a variable.
22146 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022147 * For incompatible types, return 0.
22148 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22149 * caller of incompatible types: it sets *denote to TRUE if "denote"
22150 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 */
22152 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022153get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022155 int error = FALSE;
22156
22157 return get_tv_number_chk(varp, &error); /* return 0L on error */
22158}
22159
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022160 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022161get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022162{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022163 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022165 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022167 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022168 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022169 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022170#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022171 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022172 break;
22173#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022174 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022175 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022176 break;
22177 case VAR_STRING:
22178 if (varp->vval.v_string != NULL)
22179 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022180 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022181 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022182 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022183 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022184 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022185 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022186 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022187 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022188 case VAR_SPECIAL:
22189 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22190 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022191 case VAR_JOB:
22192#ifdef FEAT_JOB
22193 EMSG(_("E910: Using a Job as a Number"));
22194 break;
22195#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022196 case VAR_CHANNEL:
22197#ifdef FEAT_CHANNEL
22198 EMSG(_("E913: Using a Channel as a Number"));
22199 break;
22200#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022201 case VAR_UNKNOWN:
22202 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022203 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022205 if (denote == NULL) /* useful for values that must be unsigned */
22206 n = -1;
22207 else
22208 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022209 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210}
22211
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022212#ifdef FEAT_FLOAT
22213 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022214get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022215{
22216 switch (varp->v_type)
22217 {
22218 case VAR_NUMBER:
22219 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022220 case VAR_FLOAT:
22221 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022222 case VAR_FUNC:
22223 EMSG(_("E891: Using a Funcref as a Float"));
22224 break;
22225 case VAR_STRING:
22226 EMSG(_("E892: Using a String as a Float"));
22227 break;
22228 case VAR_LIST:
22229 EMSG(_("E893: Using a List as a Float"));
22230 break;
22231 case VAR_DICT:
22232 EMSG(_("E894: Using a Dictionary as a Float"));
22233 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022234 case VAR_SPECIAL:
22235 EMSG(_("E907: Using a special value as a Float"));
22236 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022237 case VAR_JOB:
22238# ifdef FEAT_JOB
22239 EMSG(_("E911: Using a Job as a Float"));
22240 break;
22241# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022242 case VAR_CHANNEL:
22243# ifdef FEAT_CHANNEL
22244 EMSG(_("E914: Using a Channel as a Float"));
22245 break;
22246# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022247 case VAR_UNKNOWN:
22248 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022249 break;
22250 }
22251 return 0;
22252}
22253#endif
22254
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022256 * Get the lnum from the first argument.
22257 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022258 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 */
22260 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022261get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262{
Bram Moolenaar33570922005-01-25 22:26:29 +000022263 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 linenr_T lnum;
22265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022266 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 if (lnum == 0) /* no valid number, try using line() */
22268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022269 rettv.v_type = VAR_NUMBER;
22270 f_line(argvars, &rettv);
22271 lnum = rettv.vval.v_number;
22272 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273 }
22274 return lnum;
22275}
22276
22277/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022278 * Get the lnum from the first argument.
22279 * Also accepts "$", then "buf" is used.
22280 * Returns 0 on error.
22281 */
22282 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022283get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022284{
22285 if (argvars[0].v_type == VAR_STRING
22286 && argvars[0].vval.v_string != NULL
22287 && argvars[0].vval.v_string[0] == '$'
22288 && buf != NULL)
22289 return buf->b_ml.ml_line_count;
22290 return get_tv_number_chk(&argvars[0], NULL);
22291}
22292
22293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 * Get the string value of a variable.
22295 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022296 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22297 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 * If the String variable has never been set, return an empty string.
22299 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022300 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22301 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302 */
22303 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022304get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305{
22306 static char_u mybuf[NUMBUFLEN];
22307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022308 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309}
22310
22311 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022312get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022314 char_u *res = get_tv_string_buf_chk(varp, buf);
22315
22316 return res != NULL ? res : (char_u *)"";
22317}
22318
Bram Moolenaar7d647822014-04-05 21:28:56 +020022319/*
22320 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22321 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022322 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022323get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022324{
22325 static char_u mybuf[NUMBUFLEN];
22326
22327 return get_tv_string_buf_chk(varp, mybuf);
22328}
22329
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022330 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022331get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022332{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022333 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022335 case VAR_NUMBER:
22336 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22337 return buf;
22338 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022339 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022340 break;
22341 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022342 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022343 break;
22344 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022345 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022346 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022347 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022348#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022349 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022350 break;
22351#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022352 case VAR_STRING:
22353 if (varp->vval.v_string != NULL)
22354 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022355 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022356 case VAR_SPECIAL:
22357 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22358 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022359 case VAR_JOB:
22360#ifdef FEAT_JOB
22361 {
22362 job_T *job = varp->vval.v_job;
22363 char *status = job->jv_status == JOB_FAILED ? "fail"
22364 : job->jv_status == JOB_ENDED ? "dead"
22365 : "run";
22366# ifdef UNIX
22367 vim_snprintf((char *)buf, NUMBUFLEN,
22368 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022369# elif defined(WIN32)
22370 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022371 "process %ld %s",
22372 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022373 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022374# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022375 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022376 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22377# endif
22378 return buf;
22379 }
22380#endif
22381 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022382 case VAR_CHANNEL:
22383#ifdef FEAT_CHANNEL
22384 {
22385 channel_T *channel = varp->vval.v_channel;
22386 char *status = channel_status(channel);
22387
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022388 if (channel == NULL)
22389 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22390 else
22391 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022392 "channel %d %s", channel->ch_id, status);
22393 return buf;
22394 }
22395#endif
22396 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022397 case VAR_UNKNOWN:
22398 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022399 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022400 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022401 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402}
22403
22404/*
22405 * Find variable "name" in the list of variables.
22406 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022407 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022408 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022409 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022411 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022412find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022415 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416
Bram Moolenaara7043832005-01-21 11:56:39 +000022417 ht = find_var_ht(name, &varname);
22418 if (htp != NULL)
22419 *htp = ht;
22420 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022422 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423}
22424
22425/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022426 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022427 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022429 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022430find_var_in_ht(
22431 hashtab_T *ht,
22432 int htname,
22433 char_u *varname,
22434 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022435{
Bram Moolenaar33570922005-01-25 22:26:29 +000022436 hashitem_T *hi;
22437
22438 if (*varname == NUL)
22439 {
22440 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022441 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022442 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022443 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 case 'g': return &globvars_var;
22445 case 'v': return &vimvars_var;
22446 case 'b': return &curbuf->b_bufvar;
22447 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022448#ifdef FEAT_WINDOWS
22449 case 't': return &curtab->tp_winvar;
22450#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022451 case 'l': return current_funccal == NULL
22452 ? NULL : &current_funccal->l_vars_var;
22453 case 'a': return current_funccal == NULL
22454 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022455 }
22456 return NULL;
22457 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022458
22459 hi = hash_find(ht, varname);
22460 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022461 {
22462 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022463 * worked find the variable again. Don't auto-load a script if it was
22464 * loaded already, otherwise it would be loaded every time when
22465 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022466 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022467 {
22468 /* Note: script_autoload() may make "hi" invalid. It must either
22469 * be obtained again or not used. */
22470 if (!script_autoload(varname, FALSE) || aborting())
22471 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022472 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022473 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022474 if (HASHITEM_EMPTY(hi))
22475 return NULL;
22476 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022477 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022478}
22479
22480/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022481 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022482 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022483 * Set "varname" to the start of name without ':'.
22484 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022485 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022486find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022488 hashitem_T *hi;
22489
Bram Moolenaar73627d02015-08-11 15:46:09 +020022490 if (name[0] == NUL)
22491 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 if (name[1] != ':')
22493 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022494 /* The name must not start with a colon or #. */
22495 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 return NULL;
22497 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022498
22499 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022500 hi = hash_find(&compat_hashtab, name);
22501 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022502 return &compat_hashtab;
22503
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022505 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022506 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 }
22508 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022509 if (*name == 'g') /* global variable */
22510 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022511 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22512 */
22513 if (vim_strchr(name + 2, ':') != NULL
22514 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022515 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022517 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022519 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022520#ifdef FEAT_WINDOWS
22521 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022522 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022523#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022524 if (*name == 'v') /* v: variable */
22525 return &vimvarht;
22526 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022527 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022528 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022529 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 if (*name == 's' /* script variable */
22531 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22532 return &SCRIPT_VARS(current_SID);
22533 return NULL;
22534}
22535
22536/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022537 * Get function call environment based on bactrace debug level
22538 */
22539 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022540get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022541{
22542 int i;
22543 funccall_T *funccal;
22544 funccall_T *temp_funccal;
22545
22546 funccal = current_funccal;
22547 if (debug_backtrace_level > 0)
22548 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022549 for (i = 0; i < debug_backtrace_level; i++)
22550 {
22551 temp_funccal = funccal->caller;
22552 if (temp_funccal)
22553 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022554 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022555 /* backtrace level overflow. reset to max */
22556 debug_backtrace_level = i;
22557 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022558 }
22559 return funccal;
22560}
22561
22562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022564 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 * Returns NULL when it doesn't exist.
22566 */
22567 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022568get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022569{
Bram Moolenaar33570922005-01-25 22:26:29 +000022570 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022572 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 if (v == NULL)
22574 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576}
22577
22578/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022579 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 * sourcing this script and when executing functions defined in the script.
22581 */
22582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022583new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584{
Bram Moolenaara7043832005-01-21 11:56:39 +000022585 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022586 hashtab_T *ht;
22587 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022588
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22590 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022591 /* Re-allocating ga_data means that an ht_array pointing to
22592 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022593 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022594 for (i = 1; i <= ga_scripts.ga_len; ++i)
22595 {
22596 ht = &SCRIPT_VARS(i);
22597 if (ht->ht_mask == HT_INIT_SIZE - 1)
22598 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022599 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022600 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022601 }
22602
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603 while (ga_scripts.ga_len < id)
22604 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022605 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022606 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022607 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022608 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 }
22610 }
22611}
22612
22613/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022614 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22615 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 */
22617 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022618init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619{
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022621 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022622 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022623 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022624 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022625 dict_var->di_tv.vval.v_dict = dict;
22626 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022627 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022628 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22629 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630}
22631
22632/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022633 * Unreference a dictionary initialized by init_var_dict().
22634 */
22635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022636unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022637{
22638 /* Now the dict needs to be freed if no one else is using it, go back to
22639 * normal reference counting. */
22640 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22641 dict_unref(dict);
22642}
22643
22644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022646 * Frees all allocated variables and the value they contain.
22647 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648 */
22649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022650vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022651{
22652 vars_clear_ext(ht, TRUE);
22653}
22654
22655/*
22656 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22657 */
22658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022659vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660{
Bram Moolenaara7043832005-01-21 11:56:39 +000022661 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022662 hashitem_T *hi;
22663 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664
Bram Moolenaar33570922005-01-25 22:26:29 +000022665 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022666 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022667 for (hi = ht->ht_array; todo > 0; ++hi)
22668 {
22669 if (!HASHITEM_EMPTY(hi))
22670 {
22671 --todo;
22672
Bram Moolenaar33570922005-01-25 22:26:29 +000022673 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022674 * ht_array might change then. hash_clear() takes care of it
22675 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022676 v = HI2DI(hi);
22677 if (free_val)
22678 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022679 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022680 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022681 }
22682 }
22683 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022684 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685}
22686
Bram Moolenaara7043832005-01-21 11:56:39 +000022687/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022688 * Delete a variable from hashtab "ht" at item "hi".
22689 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022692delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693{
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022695
22696 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022697 clear_tv(&di->di_tv);
22698 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699}
22700
22701/*
22702 * List the value of one internal variable.
22703 */
22704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022705list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022707 char_u *tofree;
22708 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022709 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022710
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022711 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022712 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022713 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022714 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715}
22716
Bram Moolenaar071d4272004-06-13 20:20:40 +000022717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022718list_one_var_a(
22719 char_u *prefix,
22720 char_u *name,
22721 int type,
22722 char_u *string,
22723 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724{
Bram Moolenaar31859182007-08-14 20:41:13 +000022725 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22726 msg_start();
22727 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 if (name != NULL) /* "a:" vars don't have a name stored */
22729 msg_puts(name);
22730 msg_putchar(' ');
22731 msg_advance(22);
22732 if (type == VAR_NUMBER)
22733 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022734 else if (type == VAR_FUNC)
22735 msg_putchar('*');
22736 else if (type == VAR_LIST)
22737 {
22738 msg_putchar('[');
22739 if (*string == '[')
22740 ++string;
22741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022742 else if (type == VAR_DICT)
22743 {
22744 msg_putchar('{');
22745 if (*string == '{')
22746 ++string;
22747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 else
22749 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022750
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022752
22753 if (type == VAR_FUNC)
22754 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022755 if (*first)
22756 {
22757 msg_clr_eos();
22758 *first = FALSE;
22759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760}
22761
22762/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022763 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764 * If the variable already exists, the value is updated.
22765 * Otherwise the variable is created.
22766 */
22767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022768set_var(
22769 char_u *name,
22770 typval_T *tv,
22771 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772{
Bram Moolenaar33570922005-01-25 22:26:29 +000022773 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022777 ht = find_var_ht(name, &varname);
22778 if (ht == NULL || *varname == NUL)
22779 {
22780 EMSG2(_(e_illvar), name);
22781 return;
22782 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022783 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022784
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022785 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22786 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022787
Bram Moolenaar33570922005-01-25 22:26:29 +000022788 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022790 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022791 if (var_check_ro(v->di_flags, name, FALSE)
22792 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022793 return;
22794 if (v->di_tv.v_type != tv->v_type
22795 && !((v->di_tv.v_type == VAR_STRING
22796 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022797 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022798 || tv->v_type == VAR_NUMBER))
22799#ifdef FEAT_FLOAT
22800 && !((v->di_tv.v_type == VAR_NUMBER
22801 || v->di_tv.v_type == VAR_FLOAT)
22802 && (tv->v_type == VAR_NUMBER
22803 || tv->v_type == VAR_FLOAT))
22804#endif
22805 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022806 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022807 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022808 return;
22809 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022810
22811 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022812 * Handle setting internal v: variables separately where needed to
22813 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022814 */
22815 if (ht == &vimvarht)
22816 {
22817 if (v->di_tv.v_type == VAR_STRING)
22818 {
22819 vim_free(v->di_tv.vval.v_string);
22820 if (copy || tv->v_type != VAR_STRING)
22821 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22822 else
22823 {
22824 /* Take over the string to avoid an extra alloc/free. */
22825 v->di_tv.vval.v_string = tv->vval.v_string;
22826 tv->vval.v_string = NULL;
22827 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022828 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022829 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022830 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022831 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022832 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022833 if (STRCMP(varname, "searchforward") == 0)
22834 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022835#ifdef FEAT_SEARCH_EXTRA
22836 else if (STRCMP(varname, "hlsearch") == 0)
22837 {
22838 no_hlsearch = !v->di_tv.vval.v_number;
22839 redraw_all_later(SOME_VALID);
22840 }
22841#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022842 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022843 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022844 else if (v->di_tv.v_type != tv->v_type)
22845 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022846 }
22847
22848 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 }
22850 else /* add a new variable */
22851 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022852 /* Can't add "v:" variable. */
22853 if (ht == &vimvarht)
22854 {
22855 EMSG2(_(e_illvar), name);
22856 return;
22857 }
22858
Bram Moolenaar92124a32005-06-17 22:03:40 +000022859 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022860 if (!valid_varname(varname))
22861 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022862
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022863 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22864 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022865 if (v == NULL)
22866 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022867 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022868 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022870 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 return;
22872 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022873 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022875
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022876 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022877 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022878 else
22879 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022880 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022881 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022882 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884}
22885
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022886/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022887 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022888 * Also give an error message.
22889 */
22890 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022891var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022892{
22893 if (flags & DI_FLAGS_RO)
22894 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022895 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022896 return TRUE;
22897 }
22898 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22899 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022900 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022901 return TRUE;
22902 }
22903 return FALSE;
22904}
22905
22906/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022907 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22908 * Also give an error message.
22909 */
22910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022911var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022912{
22913 if (flags & DI_FLAGS_FIX)
22914 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022915 EMSG2(_("E795: Cannot delete variable %s"),
22916 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022917 return TRUE;
22918 }
22919 return FALSE;
22920}
22921
22922/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022923 * Check if a funcref is assigned to a valid variable name.
22924 * Return TRUE and give an error if not.
22925 */
22926 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022927var_check_func_name(
22928 char_u *name, /* points to start of variable name */
22929 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022930{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022931 /* Allow for w: b: s: and t:. */
22932 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022933 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22934 ? name[2] : name[0]))
22935 {
22936 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22937 name);
22938 return TRUE;
22939 }
22940 /* Don't allow hiding a function. When "v" is not NULL we might be
22941 * assigning another function to the same var, the type is checked
22942 * below. */
22943 if (new_var && function_exists(name))
22944 {
22945 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22946 name);
22947 return TRUE;
22948 }
22949 return FALSE;
22950}
22951
22952/*
22953 * Check if a variable name is valid.
22954 * Return FALSE and give an error if not.
22955 */
22956 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022957valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022958{
22959 char_u *p;
22960
22961 for (p = varname; *p != NUL; ++p)
22962 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22963 && *p != AUTOLOAD_CHAR)
22964 {
22965 EMSG2(_(e_illvar), varname);
22966 return FALSE;
22967 }
22968 return TRUE;
22969}
22970
22971/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022972 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022973 * Also give an error message, using "name" or _("name") when use_gettext is
22974 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022975 */
22976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022977tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022978{
22979 if (lock & VAR_LOCKED)
22980 {
22981 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022982 name == NULL ? (char_u *)_("Unknown")
22983 : use_gettext ? (char_u *)_(name)
22984 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022985 return TRUE;
22986 }
22987 if (lock & VAR_FIXED)
22988 {
22989 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022990 name == NULL ? (char_u *)_("Unknown")
22991 : use_gettext ? (char_u *)_(name)
22992 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022993 return TRUE;
22994 }
22995 return FALSE;
22996}
22997
22998/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022999 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023000 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023001 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023002 * It is OK for "from" and "to" to point to the same item. This is used to
23003 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023004 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023006copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023008 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023009 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023010 switch (from->v_type)
23011 {
23012 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023013 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023014 to->vval.v_number = from->vval.v_number;
23015 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023016 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023017#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023018 to->vval.v_float = from->vval.v_float;
23019 break;
23020#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023021 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023022#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023023 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023024 if (to->vval.v_job != NULL)
23025 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023026 break;
23027#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023028 case VAR_CHANNEL:
23029#ifdef FEAT_CHANNEL
23030 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023031 if (to->vval.v_channel != NULL)
23032 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023033 break;
23034#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023035 case VAR_STRING:
23036 case VAR_FUNC:
23037 if (from->vval.v_string == NULL)
23038 to->vval.v_string = NULL;
23039 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023040 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023041 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023042 if (from->v_type == VAR_FUNC)
23043 func_ref(to->vval.v_string);
23044 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023045 break;
23046 case VAR_LIST:
23047 if (from->vval.v_list == NULL)
23048 to->vval.v_list = NULL;
23049 else
23050 {
23051 to->vval.v_list = from->vval.v_list;
23052 ++to->vval.v_list->lv_refcount;
23053 }
23054 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023055 case VAR_DICT:
23056 if (from->vval.v_dict == NULL)
23057 to->vval.v_dict = NULL;
23058 else
23059 {
23060 to->vval.v_dict = from->vval.v_dict;
23061 ++to->vval.v_dict->dv_refcount;
23062 }
23063 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023064 case VAR_UNKNOWN:
23065 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023066 break;
23067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068}
23069
23070/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023071 * Make a copy of an item.
23072 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023073 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23074 * reference to an already copied list/dict can be used.
23075 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023076 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023078item_copy(
23079 typval_T *from,
23080 typval_T *to,
23081 int deep,
23082 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023083{
23084 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023085 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023086
Bram Moolenaar33570922005-01-25 22:26:29 +000023087 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023088 {
23089 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023090 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023091 }
23092 ++recurse;
23093
23094 switch (from->v_type)
23095 {
23096 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023097 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023098 case VAR_STRING:
23099 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023100 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023101 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023102 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023103 copy_tv(from, to);
23104 break;
23105 case VAR_LIST:
23106 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023107 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023108 if (from->vval.v_list == NULL)
23109 to->vval.v_list = NULL;
23110 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23111 {
23112 /* use the copy made earlier */
23113 to->vval.v_list = from->vval.v_list->lv_copylist;
23114 ++to->vval.v_list->lv_refcount;
23115 }
23116 else
23117 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23118 if (to->vval.v_list == NULL)
23119 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023120 break;
23121 case VAR_DICT:
23122 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023123 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023124 if (from->vval.v_dict == NULL)
23125 to->vval.v_dict = NULL;
23126 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23127 {
23128 /* use the copy made earlier */
23129 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23130 ++to->vval.v_dict->dv_refcount;
23131 }
23132 else
23133 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23134 if (to->vval.v_dict == NULL)
23135 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023136 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023137 case VAR_UNKNOWN:
23138 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023139 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023140 }
23141 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023142 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023143}
23144
23145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 * ":echo expr1 ..." print each argument separated with a space, add a
23147 * newline at the end.
23148 * ":echon expr1 ..." print each argument plain.
23149 */
23150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023151ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152{
23153 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023154 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023155 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 char_u *p;
23157 int needclr = TRUE;
23158 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023159 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160
23161 if (eap->skip)
23162 ++emsg_skip;
23163 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23164 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023165 /* If eval1() causes an error message the text from the command may
23166 * still need to be cleared. E.g., "echo 22,44". */
23167 need_clr_eos = needclr;
23168
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023170 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 {
23172 /*
23173 * Report the invalid expression unless the expression evaluation
23174 * has been cancelled due to an aborting error, an interrupt, or an
23175 * exception.
23176 */
23177 if (!aborting())
23178 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023179 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 break;
23181 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023182 need_clr_eos = FALSE;
23183
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 if (!eap->skip)
23185 {
23186 if (atstart)
23187 {
23188 atstart = FALSE;
23189 /* Call msg_start() after eval1(), evaluating the expression
23190 * may cause a message to appear. */
23191 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023192 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023193 /* Mark the saved text as finishing the line, so that what
23194 * follows is displayed on a new line when scrolling back
23195 * at the more prompt. */
23196 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 }
23200 else if (eap->cmdidx == CMD_echo)
23201 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023202 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023203 if (p != NULL)
23204 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023206 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023208 if (*p != TAB && needclr)
23209 {
23210 /* remove any text still there from the command */
23211 msg_clr_eos();
23212 needclr = FALSE;
23213 }
23214 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 }
23216 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023217 {
23218#ifdef FEAT_MBYTE
23219 if (has_mbyte)
23220 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023221 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023222
23223 (void)msg_outtrans_len_attr(p, i, echo_attr);
23224 p += i - 1;
23225 }
23226 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023228 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023231 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023233 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234 arg = skipwhite(arg);
23235 }
23236 eap->nextcmd = check_nextcmd(arg);
23237
23238 if (eap->skip)
23239 --emsg_skip;
23240 else
23241 {
23242 /* remove text that may still be there from the command */
23243 if (needclr)
23244 msg_clr_eos();
23245 if (eap->cmdidx == CMD_echo)
23246 msg_end();
23247 }
23248}
23249
23250/*
23251 * ":echohl {name}".
23252 */
23253 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023254ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023255{
23256 int id;
23257
23258 id = syn_name2id(eap->arg);
23259 if (id == 0)
23260 echo_attr = 0;
23261 else
23262 echo_attr = syn_id2attr(id);
23263}
23264
23265/*
23266 * ":execute expr1 ..." execute the result of an expression.
23267 * ":echomsg expr1 ..." Print a message
23268 * ":echoerr expr1 ..." Print an error
23269 * Each gets spaces around each argument and a newline at the end for
23270 * echo commands
23271 */
23272 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023273ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023274{
23275 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023276 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 int ret = OK;
23278 char_u *p;
23279 garray_T ga;
23280 int len;
23281 int save_did_emsg;
23282
23283 ga_init2(&ga, 1, 80);
23284
23285 if (eap->skip)
23286 ++emsg_skip;
23287 while (*arg != NUL && *arg != '|' && *arg != '\n')
23288 {
23289 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023290 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 {
23292 /*
23293 * Report the invalid expression unless the expression evaluation
23294 * has been cancelled due to an aborting error, an interrupt, or an
23295 * exception.
23296 */
23297 if (!aborting())
23298 EMSG2(_(e_invexpr2), p);
23299 ret = FAIL;
23300 break;
23301 }
23302
23303 if (!eap->skip)
23304 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023305 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306 len = (int)STRLEN(p);
23307 if (ga_grow(&ga, len + 2) == FAIL)
23308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023309 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 ret = FAIL;
23311 break;
23312 }
23313 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023314 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 ga.ga_len += len;
23317 }
23318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023319 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023320 arg = skipwhite(arg);
23321 }
23322
23323 if (ret != FAIL && ga.ga_data != NULL)
23324 {
23325 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023328 out_flush();
23329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 else if (eap->cmdidx == CMD_echoerr)
23331 {
23332 /* We don't want to abort following commands, restore did_emsg. */
23333 save_did_emsg = did_emsg;
23334 EMSG((char_u *)ga.ga_data);
23335 if (!force_abort)
23336 did_emsg = save_did_emsg;
23337 }
23338 else if (eap->cmdidx == CMD_execute)
23339 do_cmdline((char_u *)ga.ga_data,
23340 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23341 }
23342
23343 ga_clear(&ga);
23344
23345 if (eap->skip)
23346 --emsg_skip;
23347
23348 eap->nextcmd = check_nextcmd(arg);
23349}
23350
23351/*
23352 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23353 * "arg" points to the "&" or '+' when called, to "option" when returning.
23354 * Returns NULL when no option name found. Otherwise pointer to the char
23355 * after the option name.
23356 */
23357 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023358find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023359{
23360 char_u *p = *arg;
23361
23362 ++p;
23363 if (*p == 'g' && p[1] == ':')
23364 {
23365 *opt_flags = OPT_GLOBAL;
23366 p += 2;
23367 }
23368 else if (*p == 'l' && p[1] == ':')
23369 {
23370 *opt_flags = OPT_LOCAL;
23371 p += 2;
23372 }
23373 else
23374 *opt_flags = 0;
23375
23376 if (!ASCII_ISALPHA(*p))
23377 return NULL;
23378 *arg = p;
23379
23380 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23381 p += 4; /* termcap option */
23382 else
23383 while (ASCII_ISALPHA(*p))
23384 ++p;
23385 return p;
23386}
23387
23388/*
23389 * ":function"
23390 */
23391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023392ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393{
23394 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023395 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 int j;
23397 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023399 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400 char_u *name = NULL;
23401 char_u *p;
23402 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023403 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404 garray_T newargs;
23405 garray_T newlines;
23406 int varargs = FALSE;
23407 int mustend = FALSE;
23408 int flags = 0;
23409 ufunc_T *fp;
23410 int indent;
23411 int nesting;
23412 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023413 dictitem_T *v;
23414 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023415 static int func_nr = 0; /* number for nameless function */
23416 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023417 hashtab_T *ht;
23418 int todo;
23419 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023420 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421
23422 /*
23423 * ":function" without argument: list functions.
23424 */
23425 if (ends_excmd(*eap->arg))
23426 {
23427 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023428 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023429 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023430 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023431 {
23432 if (!HASHITEM_EMPTY(hi))
23433 {
23434 --todo;
23435 fp = HI2UF(hi);
23436 if (!isdigit(*fp->uf_name))
23437 list_func_head(fp, FALSE);
23438 }
23439 }
23440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 eap->nextcmd = check_nextcmd(eap->arg);
23442 return;
23443 }
23444
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023445 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023446 * ":function /pat": list functions matching pattern.
23447 */
23448 if (*eap->arg == '/')
23449 {
23450 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23451 if (!eap->skip)
23452 {
23453 regmatch_T regmatch;
23454
23455 c = *p;
23456 *p = NUL;
23457 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23458 *p = c;
23459 if (regmatch.regprog != NULL)
23460 {
23461 regmatch.rm_ic = p_ic;
23462
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023463 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023464 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23465 {
23466 if (!HASHITEM_EMPTY(hi))
23467 {
23468 --todo;
23469 fp = HI2UF(hi);
23470 if (!isdigit(*fp->uf_name)
23471 && vim_regexec(&regmatch, fp->uf_name, 0))
23472 list_func_head(fp, FALSE);
23473 }
23474 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023475 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023476 }
23477 }
23478 if (*p == '/')
23479 ++p;
23480 eap->nextcmd = check_nextcmd(p);
23481 return;
23482 }
23483
23484 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023485 * Get the function name. There are these situations:
23486 * func normal function name
23487 * "name" == func, "fudi.fd_dict" == NULL
23488 * dict.func new dictionary entry
23489 * "name" == NULL, "fudi.fd_dict" set,
23490 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23491 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023492 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023493 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23494 * dict.func existing dict entry that's not a Funcref
23495 * "name" == NULL, "fudi.fd_dict" set,
23496 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023497 * s:func script-local function name
23498 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023501 name = trans_function_name(&p, eap->skip, 0, &fudi);
23502 paren = (vim_strchr(p, '(') != NULL);
23503 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023504 {
23505 /*
23506 * Return on an invalid expression in braces, unless the expression
23507 * evaluation has been cancelled due to an aborting error, an
23508 * interrupt, or an exception.
23509 */
23510 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023512 if (!eap->skip && fudi.fd_newkey != NULL)
23513 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023514 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023515 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 else
23518 eap->skip = TRUE;
23519 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023520
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 /* An error in a function call during evaluation of an expression in magic
23522 * braces should not cause the function not to be defined. */
23523 saved_did_emsg = did_emsg;
23524 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525
23526 /*
23527 * ":function func" with only function name: list function.
23528 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023529 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 {
23531 if (!ends_excmd(*skipwhite(p)))
23532 {
23533 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023534 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 }
23536 eap->nextcmd = check_nextcmd(p);
23537 if (eap->nextcmd != NULL)
23538 *p = NUL;
23539 if (!eap->skip && !got_int)
23540 {
23541 fp = find_func(name);
23542 if (fp != NULL)
23543 {
23544 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023545 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023547 if (FUNCLINE(fp, j) == NULL)
23548 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 msg_putchar('\n');
23550 msg_outnum((long)(j + 1));
23551 if (j < 9)
23552 msg_putchar(' ');
23553 if (j < 99)
23554 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023555 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556 out_flush(); /* show a line at a time */
23557 ui_breakcheck();
23558 }
23559 if (!got_int)
23560 {
23561 msg_putchar('\n');
23562 msg_puts((char_u *)" endfunction");
23563 }
23564 }
23565 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023566 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023568 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 }
23570
23571 /*
23572 * ":function name(arg1, arg2)" Define function.
23573 */
23574 p = skipwhite(p);
23575 if (*p != '(')
23576 {
23577 if (!eap->skip)
23578 {
23579 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023580 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 }
23582 /* attempt to continue by skipping some text */
23583 if (vim_strchr(p, '(') != NULL)
23584 p = vim_strchr(p, '(');
23585 }
23586 p = skipwhite(p + 1);
23587
23588 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23589 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23590
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023591 if (!eap->skip)
23592 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023593 /* Check the name of the function. Unless it's a dictionary function
23594 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023595 if (name != NULL)
23596 arg = name;
23597 else
23598 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023599 if (arg != NULL && (fudi.fd_di == NULL
23600 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023601 {
23602 if (*arg == K_SPECIAL)
23603 j = 3;
23604 else
23605 j = 0;
23606 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23607 : eval_isnamec(arg[j])))
23608 ++j;
23609 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023610 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023611 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023612 /* Disallow using the g: dict. */
23613 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23614 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023615 }
23616
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 /*
23618 * Isolate the arguments: "arg1, arg2, ...)"
23619 */
23620 while (*p != ')')
23621 {
23622 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23623 {
23624 varargs = TRUE;
23625 p += 3;
23626 mustend = TRUE;
23627 }
23628 else
23629 {
23630 arg = p;
23631 while (ASCII_ISALNUM(*p) || *p == '_')
23632 ++p;
23633 if (arg == p || isdigit(*arg)
23634 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23635 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23636 {
23637 if (!eap->skip)
23638 EMSG2(_("E125: Illegal argument: %s"), arg);
23639 break;
23640 }
23641 if (ga_grow(&newargs, 1) == FAIL)
23642 goto erret;
23643 c = *p;
23644 *p = NUL;
23645 arg = vim_strsave(arg);
23646 if (arg == NULL)
23647 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023648
23649 /* Check for duplicate argument name. */
23650 for (i = 0; i < newargs.ga_len; ++i)
23651 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23652 {
23653 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023654 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023655 goto erret;
23656 }
23657
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23659 *p = c;
23660 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661 if (*p == ',')
23662 ++p;
23663 else
23664 mustend = TRUE;
23665 }
23666 p = skipwhite(p);
23667 if (mustend && *p != ')')
23668 {
23669 if (!eap->skip)
23670 EMSG2(_(e_invarg2), eap->arg);
23671 break;
23672 }
23673 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023674 if (*p != ')')
23675 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 ++p; /* skip the ')' */
23677
Bram Moolenaare9a41262005-01-15 22:18:47 +000023678 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 for (;;)
23680 {
23681 p = skipwhite(p);
23682 if (STRNCMP(p, "range", 5) == 0)
23683 {
23684 flags |= FC_RANGE;
23685 p += 5;
23686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023687 else if (STRNCMP(p, "dict", 4) == 0)
23688 {
23689 flags |= FC_DICT;
23690 p += 4;
23691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692 else if (STRNCMP(p, "abort", 5) == 0)
23693 {
23694 flags |= FC_ABORT;
23695 p += 5;
23696 }
23697 else
23698 break;
23699 }
23700
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023701 /* When there is a line break use what follows for the function body.
23702 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23703 if (*p == '\n')
23704 line_arg = p + 1;
23705 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023706 EMSG(_(e_trailing));
23707
23708 /*
23709 * Read the body of the function, until ":endfunction" is found.
23710 */
23711 if (KeyTyped)
23712 {
23713 /* Check if the function already exists, don't let the user type the
23714 * whole function before telling him it doesn't work! For a script we
23715 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023716 if (!eap->skip && !eap->forceit)
23717 {
23718 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23719 EMSG(_(e_funcdict));
23720 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023721 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023724 if (!eap->skip && did_emsg)
23725 goto erret;
23726
Bram Moolenaar071d4272004-06-13 20:20:40 +000023727 msg_putchar('\n'); /* don't overwrite the function name */
23728 cmdline_row = msg_row;
23729 }
23730
23731 indent = 2;
23732 nesting = 0;
23733 for (;;)
23734 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023735 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023736 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023737 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023738 saved_wait_return = FALSE;
23739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023741 sourcing_lnum_off = sourcing_lnum;
23742
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023743 if (line_arg != NULL)
23744 {
23745 /* Use eap->arg, split up in parts by line breaks. */
23746 theline = line_arg;
23747 p = vim_strchr(theline, '\n');
23748 if (p == NULL)
23749 line_arg += STRLEN(line_arg);
23750 else
23751 {
23752 *p = NUL;
23753 line_arg = p + 1;
23754 }
23755 }
23756 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 theline = getcmdline(':', 0L, indent);
23758 else
23759 theline = eap->getline(':', eap->cookie, indent);
23760 if (KeyTyped)
23761 lines_left = Rows - 1;
23762 if (theline == NULL)
23763 {
23764 EMSG(_("E126: Missing :endfunction"));
23765 goto erret;
23766 }
23767
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023768 /* Detect line continuation: sourcing_lnum increased more than one. */
23769 if (sourcing_lnum > sourcing_lnum_off + 1)
23770 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23771 else
23772 sourcing_lnum_off = 0;
23773
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774 if (skip_until != NULL)
23775 {
23776 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23777 * don't check for ":endfunc". */
23778 if (STRCMP(theline, skip_until) == 0)
23779 {
23780 vim_free(skip_until);
23781 skip_until = NULL;
23782 }
23783 }
23784 else
23785 {
23786 /* skip ':' and blanks*/
23787 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23788 ;
23789
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023790 /* Check for "endfunction". */
23791 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023793 if (line_arg == NULL)
23794 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 break;
23796 }
23797
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023798 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799 * at "end". */
23800 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23801 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023802 else if (STRNCMP(p, "if", 2) == 0
23803 || STRNCMP(p, "wh", 2) == 0
23804 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 || STRNCMP(p, "try", 3) == 0)
23806 indent += 2;
23807
23808 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023809 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023811 if (*p == '!')
23812 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023814 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23815 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023817 ++nesting;
23818 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 }
23820 }
23821
23822 /* Check for ":append" or ":insert". */
23823 p = skip_range(p, NULL);
23824 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23825 || (p[0] == 'i'
23826 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23827 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23828 skip_until = vim_strsave((char_u *)".");
23829
23830 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23831 arg = skipwhite(skiptowhite(p));
23832 if (arg[0] == '<' && arg[1] =='<'
23833 && ((p[0] == 'p' && p[1] == 'y'
23834 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23835 || (p[0] == 'p' && p[1] == 'e'
23836 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23837 || (p[0] == 't' && p[1] == 'c'
23838 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023839 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23840 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23842 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023843 || (p[0] == 'm' && p[1] == 'z'
23844 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 ))
23846 {
23847 /* ":python <<" continues until a dot, like ":append" */
23848 p = skipwhite(arg + 2);
23849 if (*p == NUL)
23850 skip_until = vim_strsave((char_u *)".");
23851 else
23852 skip_until = vim_strsave(p);
23853 }
23854 }
23855
23856 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023857 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023858 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023859 if (line_arg == NULL)
23860 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023862 }
23863
23864 /* Copy the line to newly allocated memory. get_one_sourceline()
23865 * allocates 250 bytes per line, this saves 80% on average. The cost
23866 * is an extra alloc/free. */
23867 p = vim_strsave(theline);
23868 if (p != NULL)
23869 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023870 if (line_arg == NULL)
23871 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023872 theline = p;
23873 }
23874
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023875 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23876
23877 /* Add NULL lines for continuation lines, so that the line count is
23878 * equal to the index in the growarray. */
23879 while (sourcing_lnum_off-- > 0)
23880 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023881
23882 /* Check for end of eap->arg. */
23883 if (line_arg != NULL && *line_arg == NUL)
23884 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885 }
23886
23887 /* Don't define the function when skipping commands or when an error was
23888 * detected. */
23889 if (eap->skip || did_emsg)
23890 goto erret;
23891
23892 /*
23893 * If there are no errors, add the function
23894 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023895 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023896 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023897 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023898 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023899 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023900 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023901 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023902 goto erret;
23903 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023904
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023905 fp = find_func(name);
23906 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023908 if (!eap->forceit)
23909 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023910 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023911 goto erret;
23912 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023913 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023914 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023915 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023916 name);
23917 goto erret;
23918 }
23919 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023920 ga_clear_strings(&(fp->uf_args));
23921 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023922 vim_free(name);
23923 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 }
23926 else
23927 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023928 char numbuf[20];
23929
23930 fp = NULL;
23931 if (fudi.fd_newkey == NULL && !eap->forceit)
23932 {
23933 EMSG(_(e_funcdict));
23934 goto erret;
23935 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023936 if (fudi.fd_di == NULL)
23937 {
23938 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023939 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023940 goto erret;
23941 }
23942 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023943 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023944 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023945
23946 /* Give the function a sequential number. Can only be used with a
23947 * Funcref! */
23948 vim_free(name);
23949 sprintf(numbuf, "%d", ++func_nr);
23950 name = vim_strsave((char_u *)numbuf);
23951 if (name == NULL)
23952 goto erret;
23953 }
23954
23955 if (fp == NULL)
23956 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023957 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023958 {
23959 int slen, plen;
23960 char_u *scriptname;
23961
23962 /* Check that the autoload name matches the script name. */
23963 j = FAIL;
23964 if (sourcing_name != NULL)
23965 {
23966 scriptname = autoload_name(name);
23967 if (scriptname != NULL)
23968 {
23969 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023970 plen = (int)STRLEN(p);
23971 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023972 if (slen > plen && fnamecmp(p,
23973 sourcing_name + slen - plen) == 0)
23974 j = OK;
23975 vim_free(scriptname);
23976 }
23977 }
23978 if (j == FAIL)
23979 {
23980 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23981 goto erret;
23982 }
23983 }
23984
23985 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986 if (fp == NULL)
23987 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023988
23989 if (fudi.fd_dict != NULL)
23990 {
23991 if (fudi.fd_di == NULL)
23992 {
23993 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023994 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023995 if (fudi.fd_di == NULL)
23996 {
23997 vim_free(fp);
23998 goto erret;
23999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024000 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24001 {
24002 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024003 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024004 goto erret;
24005 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024006 }
24007 else
24008 /* overwrite existing dict entry */
24009 clear_tv(&fudi.fd_di->di_tv);
24010 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024011 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024012 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024013 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024014
24015 /* behave like "dict" was used */
24016 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024017 }
24018
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024020 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024021 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24022 {
24023 vim_free(fp);
24024 goto erret;
24025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024027 fp->uf_args = newargs;
24028 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024029#ifdef FEAT_PROFILE
24030 fp->uf_tml_count = NULL;
24031 fp->uf_tml_total = NULL;
24032 fp->uf_tml_self = NULL;
24033 fp->uf_profiling = FALSE;
24034 if (prof_def_func())
24035 func_do_profile(fp);
24036#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024037 fp->uf_varargs = varargs;
24038 fp->uf_flags = flags;
24039 fp->uf_calls = 0;
24040 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024041 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024042
24043erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 ga_clear_strings(&newargs);
24045 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024046ret_free:
24047 vim_free(skip_until);
24048 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024051 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024052}
24053
24054/*
24055 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024056 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024058 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024059 * TFN_INT: internal function name OK
24060 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024061 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062 * Advances "pp" to just after the function name (if no error).
24063 */
24064 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024065trans_function_name(
24066 char_u **pp,
24067 int skip, /* only find the end, don't evaluate */
24068 int flags,
24069 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024071 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072 char_u *start;
24073 char_u *end;
24074 int lead;
24075 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024077 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024078
24079 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024080 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024082
24083 /* Check for hard coded <SNR>: already translated function ID (from a user
24084 * command). */
24085 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24086 && (*pp)[2] == (int)KE_SNR)
24087 {
24088 *pp += 3;
24089 len = get_id_len(pp) + 3;
24090 return vim_strnsave(start, len);
24091 }
24092
24093 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24094 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024096 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024098
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024099 /* Note that TFN_ flags use the same values as GLV_ flags. */
24100 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024101 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024102 if (end == start)
24103 {
24104 if (!skip)
24105 EMSG(_("E129: Function name required"));
24106 goto theend;
24107 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024108 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024109 {
24110 /*
24111 * Report an invalid expression in braces, unless the expression
24112 * evaluation has been cancelled due to an aborting error, an
24113 * interrupt, or an exception.
24114 */
24115 if (!aborting())
24116 {
24117 if (end != NULL)
24118 EMSG2(_(e_invarg2), start);
24119 }
24120 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024121 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024122 goto theend;
24123 }
24124
24125 if (lv.ll_tv != NULL)
24126 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024127 if (fdp != NULL)
24128 {
24129 fdp->fd_dict = lv.ll_dict;
24130 fdp->fd_newkey = lv.ll_newkey;
24131 lv.ll_newkey = NULL;
24132 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024133 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024134 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24135 {
24136 name = vim_strsave(lv.ll_tv->vval.v_string);
24137 *pp = end;
24138 }
24139 else
24140 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024141 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24142 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024143 EMSG(_(e_funcref));
24144 else
24145 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024146 name = NULL;
24147 }
24148 goto theend;
24149 }
24150
24151 if (lv.ll_name == NULL)
24152 {
24153 /* Error found, but continue after the function name. */
24154 *pp = end;
24155 goto theend;
24156 }
24157
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024158 /* Check if the name is a Funcref. If so, use the value. */
24159 if (lv.ll_exp_name != NULL)
24160 {
24161 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024162 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024163 if (name == lv.ll_exp_name)
24164 name = NULL;
24165 }
24166 else
24167 {
24168 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024169 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024170 if (name == *pp)
24171 name = NULL;
24172 }
24173 if (name != NULL)
24174 {
24175 name = vim_strsave(name);
24176 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024177 if (STRNCMP(name, "<SNR>", 5) == 0)
24178 {
24179 /* Change "<SNR>" to the byte sequence. */
24180 name[0] = K_SPECIAL;
24181 name[1] = KS_EXTRA;
24182 name[2] = (int)KE_SNR;
24183 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24184 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024185 goto theend;
24186 }
24187
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024188 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024189 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024190 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024191 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24192 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24193 {
24194 /* When there was "s:" already or the name expanded to get a
24195 * leading "s:" then remove it. */
24196 lv.ll_name += 2;
24197 len -= 2;
24198 lead = 2;
24199 }
24200 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024201 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024202 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024203 /* skip over "s:" and "g:" */
24204 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024205 lv.ll_name += 2;
24206 len = (int)(end - lv.ll_name);
24207 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024208
24209 /*
24210 * Copy the function name to allocated memory.
24211 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24212 * Accept <SNR>123_name() outside a script.
24213 */
24214 if (skip)
24215 lead = 0; /* do nothing */
24216 else if (lead > 0)
24217 {
24218 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024219 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24220 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024221 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024222 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024223 if (current_SID <= 0)
24224 {
24225 EMSG(_(e_usingsid));
24226 goto theend;
24227 }
24228 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24229 lead += (int)STRLEN(sid_buf);
24230 }
24231 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024232 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024233 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024234 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024235 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024236 goto theend;
24237 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024238 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024239 {
24240 char_u *cp = vim_strchr(lv.ll_name, ':');
24241
24242 if (cp != NULL && cp < end)
24243 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024244 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024245 goto theend;
24246 }
24247 }
24248
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024249 name = alloc((unsigned)(len + lead + 1));
24250 if (name != NULL)
24251 {
24252 if (lead > 0)
24253 {
24254 name[0] = K_SPECIAL;
24255 name[1] = KS_EXTRA;
24256 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024257 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024258 STRCPY(name + 3, sid_buf);
24259 }
24260 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024261 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024262 }
24263 *pp = end;
24264
24265theend:
24266 clear_lval(&lv);
24267 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268}
24269
24270/*
24271 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24272 * Return 2 if "p" starts with "s:".
24273 * Return 0 otherwise.
24274 */
24275 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024276eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024278 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24279 * the standard library function. */
24280 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24281 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282 return 5;
24283 if (p[0] == 's' && p[1] == ':')
24284 return 2;
24285 return 0;
24286}
24287
24288/*
24289 * Return TRUE if "p" starts with "<SID>" or "s:".
24290 * Only works if eval_fname_script() returned non-zero for "p"!
24291 */
24292 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024293eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294{
24295 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24296}
24297
24298/*
24299 * List the head of the function: "name(arg1, arg2)".
24300 */
24301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024302list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303{
24304 int j;
24305
24306 msg_start();
24307 if (indent)
24308 MSG_PUTS(" ");
24309 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024310 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311 {
24312 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024313 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024314 }
24315 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024316 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024318 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319 {
24320 if (j)
24321 MSG_PUTS(", ");
24322 msg_puts(FUNCARG(fp, j));
24323 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024324 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024325 {
24326 if (j)
24327 MSG_PUTS(", ");
24328 MSG_PUTS("...");
24329 }
24330 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024331 if (fp->uf_flags & FC_ABORT)
24332 MSG_PUTS(" abort");
24333 if (fp->uf_flags & FC_RANGE)
24334 MSG_PUTS(" range");
24335 if (fp->uf_flags & FC_DICT)
24336 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024337 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024338 if (p_verbose > 0)
24339 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340}
24341
24342/*
24343 * Find a function by name, return pointer to it in ufuncs.
24344 * Return NULL for unknown function.
24345 */
24346 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024347find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024349 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024351 hi = hash_find(&func_hashtab, name);
24352 if (!HASHITEM_EMPTY(hi))
24353 return HI2UF(hi);
24354 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355}
24356
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024357#if defined(EXITFREE) || defined(PROTO)
24358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024359free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024360{
24361 hashitem_T *hi;
24362
24363 /* Need to start all over every time, because func_free() may change the
24364 * hash table. */
24365 while (func_hashtab.ht_used > 0)
24366 for (hi = func_hashtab.ht_array; ; ++hi)
24367 if (!HASHITEM_EMPTY(hi))
24368 {
24369 func_free(HI2UF(hi));
24370 break;
24371 }
24372}
24373#endif
24374
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024375 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024376translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024377{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024378 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024379 return find_internal_func(name) >= 0;
24380 return find_func(name) != NULL;
24381}
24382
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024383/*
24384 * Return TRUE if a function "name" exists.
24385 */
24386 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024387function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024388{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024389 char_u *nm = name;
24390 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024391 int n = FALSE;
24392
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024393 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24394 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024395 nm = skipwhite(nm);
24396
24397 /* Only accept "funcname", "funcname ", "funcname (..." and
24398 * "funcname(...", not "funcname!...". */
24399 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024400 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024401 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024402 return n;
24403}
24404
Bram Moolenaara1544c02013-05-30 12:35:52 +020024405 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024406get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024407{
24408 char_u *nm = name;
24409 char_u *p;
24410
24411 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24412
24413 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024414 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024415 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024416
Bram Moolenaara1544c02013-05-30 12:35:52 +020024417 vim_free(p);
24418 return NULL;
24419}
24420
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024421/*
24422 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024423 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24424 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024425 */
24426 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024427builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024428{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024429 char_u *p;
24430
24431 if (!ASCII_ISLOWER(name[0]))
24432 return FALSE;
24433 p = vim_strchr(name, AUTOLOAD_CHAR);
24434 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024435}
24436
Bram Moolenaar05159a02005-02-26 23:04:13 +000024437#if defined(FEAT_PROFILE) || defined(PROTO)
24438/*
24439 * Start profiling function "fp".
24440 */
24441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024442func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024443{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024444 int len = fp->uf_lines.ga_len;
24445
24446 if (len == 0)
24447 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024448 fp->uf_tm_count = 0;
24449 profile_zero(&fp->uf_tm_self);
24450 profile_zero(&fp->uf_tm_total);
24451 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024452 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024453 if (fp->uf_tml_total == NULL)
24454 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024455 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024456 if (fp->uf_tml_self == NULL)
24457 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024458 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024459 fp->uf_tml_idx = -1;
24460 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24461 || fp->uf_tml_self == NULL)
24462 return; /* out of memory */
24463
24464 fp->uf_profiling = TRUE;
24465}
24466
24467/*
24468 * Dump the profiling results for all functions in file "fd".
24469 */
24470 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024471func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024472{
24473 hashitem_T *hi;
24474 int todo;
24475 ufunc_T *fp;
24476 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024477 ufunc_T **sorttab;
24478 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024479
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024480 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024481 if (todo == 0)
24482 return; /* nothing to dump */
24483
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024484 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024485
Bram Moolenaar05159a02005-02-26 23:04:13 +000024486 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24487 {
24488 if (!HASHITEM_EMPTY(hi))
24489 {
24490 --todo;
24491 fp = HI2UF(hi);
24492 if (fp->uf_profiling)
24493 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024494 if (sorttab != NULL)
24495 sorttab[st_len++] = fp;
24496
Bram Moolenaar05159a02005-02-26 23:04:13 +000024497 if (fp->uf_name[0] == K_SPECIAL)
24498 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24499 else
24500 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24501 if (fp->uf_tm_count == 1)
24502 fprintf(fd, "Called 1 time\n");
24503 else
24504 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24505 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24506 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24507 fprintf(fd, "\n");
24508 fprintf(fd, "count total (s) self (s)\n");
24509
24510 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24511 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024512 if (FUNCLINE(fp, i) == NULL)
24513 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024514 prof_func_line(fd, fp->uf_tml_count[i],
24515 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024516 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24517 }
24518 fprintf(fd, "\n");
24519 }
24520 }
24521 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024522
24523 if (sorttab != NULL && st_len > 0)
24524 {
24525 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24526 prof_total_cmp);
24527 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24528 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24529 prof_self_cmp);
24530 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24531 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024532
24533 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024534}
Bram Moolenaar73830342005-02-28 22:48:19 +000024535
24536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024537prof_sort_list(
24538 FILE *fd,
24539 ufunc_T **sorttab,
24540 int st_len,
24541 char *title,
24542 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024543{
24544 int i;
24545 ufunc_T *fp;
24546
24547 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24548 fprintf(fd, "count total (s) self (s) function\n");
24549 for (i = 0; i < 20 && i < st_len; ++i)
24550 {
24551 fp = sorttab[i];
24552 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24553 prefer_self);
24554 if (fp->uf_name[0] == K_SPECIAL)
24555 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24556 else
24557 fprintf(fd, " %s()\n", fp->uf_name);
24558 }
24559 fprintf(fd, "\n");
24560}
24561
24562/*
24563 * Print the count and times for one function or function line.
24564 */
24565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024566prof_func_line(
24567 FILE *fd,
24568 int count,
24569 proftime_T *total,
24570 proftime_T *self,
24571 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024572{
24573 if (count > 0)
24574 {
24575 fprintf(fd, "%5d ", count);
24576 if (prefer_self && profile_equal(total, self))
24577 fprintf(fd, " ");
24578 else
24579 fprintf(fd, "%s ", profile_msg(total));
24580 if (!prefer_self && profile_equal(total, self))
24581 fprintf(fd, " ");
24582 else
24583 fprintf(fd, "%s ", profile_msg(self));
24584 }
24585 else
24586 fprintf(fd, " ");
24587}
24588
24589/*
24590 * Compare function for total time sorting.
24591 */
24592 static int
24593#ifdef __BORLANDC__
24594_RTLENTRYF
24595#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024596prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024597{
24598 ufunc_T *p1, *p2;
24599
24600 p1 = *(ufunc_T **)s1;
24601 p2 = *(ufunc_T **)s2;
24602 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24603}
24604
24605/*
24606 * Compare function for self time sorting.
24607 */
24608 static int
24609#ifdef __BORLANDC__
24610_RTLENTRYF
24611#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024612prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024613{
24614 ufunc_T *p1, *p2;
24615
24616 p1 = *(ufunc_T **)s1;
24617 p2 = *(ufunc_T **)s2;
24618 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24619}
24620
Bram Moolenaar05159a02005-02-26 23:04:13 +000024621#endif
24622
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024623/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024624 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024625 * Return TRUE if a package was loaded.
24626 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024628script_autoload(
24629 char_u *name,
24630 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024631{
24632 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024633 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024634 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024635 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024636
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024637 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024638 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024639 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024640 return FALSE;
24641
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024642 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024643
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024644 /* Find the name in the list of previously loaded package names. Skip
24645 * "autoload/", it's always the same. */
24646 for (i = 0; i < ga_loaded.ga_len; ++i)
24647 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24648 break;
24649 if (!reload && i < ga_loaded.ga_len)
24650 ret = FALSE; /* was loaded already */
24651 else
24652 {
24653 /* Remember the name if it wasn't loaded already. */
24654 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24655 {
24656 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24657 tofree = NULL;
24658 }
24659
24660 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024661 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024662 ret = TRUE;
24663 }
24664
24665 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024666 return ret;
24667}
24668
24669/*
24670 * Return the autoload script name for a function or variable name.
24671 * Returns NULL when out of memory.
24672 */
24673 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024674autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024675{
24676 char_u *p;
24677 char_u *scriptname;
24678
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024679 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024680 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24681 if (scriptname == NULL)
24682 return FALSE;
24683 STRCPY(scriptname, "autoload/");
24684 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024685 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024686 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024687 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024688 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024689 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024690}
24691
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24693
24694/*
24695 * Function given to ExpandGeneric() to obtain the list of user defined
24696 * function names.
24697 */
24698 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024699get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024701 static long_u done;
24702 static hashitem_T *hi;
24703 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704
24705 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024707 done = 0;
24708 hi = func_hashtab.ht_array;
24709 }
24710 if (done < func_hashtab.ht_used)
24711 {
24712 if (done++ > 0)
24713 ++hi;
24714 while (HASHITEM_EMPTY(hi))
24715 ++hi;
24716 fp = HI2UF(hi);
24717
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024718 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024719 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024720
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024721 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24722 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723
24724 cat_func_name(IObuff, fp);
24725 if (xp->xp_context != EXPAND_USER_FUNC)
24726 {
24727 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024728 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729 STRCAT(IObuff, ")");
24730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024731 return IObuff;
24732 }
24733 return NULL;
24734}
24735
24736#endif /* FEAT_CMDL_COMPL */
24737
24738/*
24739 * Copy the function name of "fp" to buffer "buf".
24740 * "buf" must be able to hold the function name plus three bytes.
24741 * Takes care of script-local function names.
24742 */
24743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024744cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024746 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024747 {
24748 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024749 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024750 }
24751 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024752 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753}
24754
24755/*
24756 * ":delfunction {name}"
24757 */
24758 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024759ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024760{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024761 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762 char_u *p;
24763 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024764 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765
24766 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024767 name = trans_function_name(&p, eap->skip, 0, &fudi);
24768 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024770 {
24771 if (fudi.fd_dict != NULL && !eap->skip)
24772 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024773 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775 if (!ends_excmd(*skipwhite(p)))
24776 {
24777 vim_free(name);
24778 EMSG(_(e_trailing));
24779 return;
24780 }
24781 eap->nextcmd = check_nextcmd(p);
24782 if (eap->nextcmd != NULL)
24783 *p = NUL;
24784
24785 if (!eap->skip)
24786 fp = find_func(name);
24787 vim_free(name);
24788
24789 if (!eap->skip)
24790 {
24791 if (fp == NULL)
24792 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024793 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794 return;
24795 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024796 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024797 {
24798 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24799 return;
24800 }
24801
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024802 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024804 /* Delete the dict item that refers to the function, it will
24805 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024806 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024808 else
24809 func_free(fp);
24810 }
24811}
24812
24813/*
24814 * Free a function and remove it from the list of functions.
24815 */
24816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024817func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024818{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024819 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024820
24821 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024822 ga_clear_strings(&(fp->uf_args));
24823 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024824#ifdef FEAT_PROFILE
24825 vim_free(fp->uf_tml_count);
24826 vim_free(fp->uf_tml_total);
24827 vim_free(fp->uf_tml_self);
24828#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024829
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024830 /* remove the function from the function hashtable */
24831 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24832 if (HASHITEM_EMPTY(hi))
24833 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024834 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024835 hash_remove(&func_hashtab, hi);
24836
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024837 vim_free(fp);
24838}
24839
24840/*
24841 * Unreference a Function: decrement the reference count and free it when it
24842 * becomes zero. Only for numbered functions.
24843 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024844 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024845func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024846{
24847 ufunc_T *fp;
24848
24849 if (name != NULL && isdigit(*name))
24850 {
24851 fp = find_func(name);
24852 if (fp == NULL)
24853 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024854 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024855 {
24856 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024857 * when "uf_calls" becomes zero. */
24858 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024859 func_free(fp);
24860 }
24861 }
24862}
24863
24864/*
24865 * Count a reference to a Function.
24866 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024868func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024869{
24870 ufunc_T *fp;
24871
24872 if (name != NULL && isdigit(*name))
24873 {
24874 fp = find_func(name);
24875 if (fp == NULL)
24876 EMSG2(_(e_intern2), "func_ref()");
24877 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024878 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024879 }
24880}
24881
24882/*
24883 * Call a user function.
24884 */
24885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024886call_user_func(
24887 ufunc_T *fp, /* pointer to function */
24888 int argcount, /* nr of args */
24889 typval_T *argvars, /* arguments */
24890 typval_T *rettv, /* return value */
24891 linenr_T firstline, /* first line of range */
24892 linenr_T lastline, /* last line of range */
24893 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894{
Bram Moolenaar33570922005-01-25 22:26:29 +000024895 char_u *save_sourcing_name;
24896 linenr_T save_sourcing_lnum;
24897 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024898 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024899 int save_did_emsg;
24900 static int depth = 0;
24901 dictitem_T *v;
24902 int fixvar_idx = 0; /* index in fixvar[] */
24903 int i;
24904 int ai;
24905 char_u numbuf[NUMBUFLEN];
24906 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024907 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024908#ifdef FEAT_PROFILE
24909 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024910 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912
24913 /* If depth of calling is getting too high, don't execute the function */
24914 if (depth >= p_mfd)
24915 {
24916 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024917 rettv->v_type = VAR_NUMBER;
24918 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919 return;
24920 }
24921 ++depth;
24922
24923 line_breakcheck(); /* check for CTRL-C hit */
24924
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024925 fc = (funccall_T *)alloc(sizeof(funccall_T));
24926 fc->caller = current_funccal;
24927 current_funccal = fc;
24928 fc->func = fp;
24929 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024930 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024931 fc->linenr = 0;
24932 fc->returned = FALSE;
24933 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024935 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24936 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937
Bram Moolenaar33570922005-01-25 22:26:29 +000024938 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024939 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024940 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24941 * each argument variable and saves a lot of time.
24942 */
24943 /*
24944 * Init l: variables.
24945 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024946 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024947 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024948 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024949 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24950 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024951 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024952 name = v->di_key;
24953 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024954 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024955 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024956 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024957 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024958 v->di_tv.vval.v_dict = selfdict;
24959 ++selfdict->dv_refcount;
24960 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024961
Bram Moolenaar33570922005-01-25 22:26:29 +000024962 /*
24963 * Init a: variables.
24964 * Set a:0 to "argcount".
24965 * Set a:000 to a list with room for the "..." arguments.
24966 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024967 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024968 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024969 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024970 /* Use "name" to avoid a warning from some compiler that checks the
24971 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024972 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024973 name = v->di_key;
24974 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024975 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024976 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024977 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024978 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024979 v->di_tv.vval.v_list = &fc->l_varlist;
24980 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24981 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24982 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024983
24984 /*
24985 * Set a:firstline to "firstline" and a:lastline to "lastline".
24986 * Set a:name to named arguments.
24987 * Set a:N to the "..." arguments.
24988 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024989 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024990 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024991 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024992 (varnumber_T)lastline);
24993 for (i = 0; i < argcount; ++i)
24994 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024995 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024996 if (ai < 0)
24997 /* named argument a:name */
24998 name = FUNCARG(fp, i);
24999 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025000 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025001 /* "..." argument a:1, a:2, etc. */
25002 sprintf((char *)numbuf, "%d", ai + 1);
25003 name = numbuf;
25004 }
25005 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25006 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025007 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025008 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25009 }
25010 else
25011 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025012 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25013 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025014 if (v == NULL)
25015 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025016 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025017 }
25018 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025019 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025020
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025021 /* Note: the values are copied directly to avoid alloc/free.
25022 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025023 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025024 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025025
25026 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25027 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025028 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25029 fc->l_listitems[ai].li_tv = argvars[i];
25030 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025031 }
25032 }
25033
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034 /* Don't redraw while executing the function. */
25035 ++RedrawingDisabled;
25036 save_sourcing_name = sourcing_name;
25037 save_sourcing_lnum = sourcing_lnum;
25038 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025039 /* need space for function name + ("function " + 3) or "[number]" */
25040 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25041 + STRLEN(fp->uf_name) + 20;
25042 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025043 if (sourcing_name != NULL)
25044 {
25045 if (save_sourcing_name != NULL
25046 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025047 sprintf((char *)sourcing_name, "%s[%d]..",
25048 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049 else
25050 STRCPY(sourcing_name, "function ");
25051 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25052
25053 if (p_verbose >= 12)
25054 {
25055 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025056 verbose_enter_scroll();
25057
Bram Moolenaar555b2802005-05-19 21:08:39 +000025058 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059 if (p_verbose >= 14)
25060 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025062 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025063 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025064 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065
25066 msg_puts((char_u *)"(");
25067 for (i = 0; i < argcount; ++i)
25068 {
25069 if (i > 0)
25070 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025071 if (argvars[i].v_type == VAR_NUMBER)
25072 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025073 else
25074 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025075 /* Do not want errors such as E724 here. */
25076 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025077 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025078 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025079 if (s != NULL)
25080 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025081 if (vim_strsize(s) > MSG_BUF_CLEN)
25082 {
25083 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25084 s = buf;
25085 }
25086 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025087 vim_free(tofree);
25088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089 }
25090 }
25091 msg_puts((char_u *)")");
25092 }
25093 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025094
25095 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025096 --no_wait_return;
25097 }
25098 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025099#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025100 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025101 {
25102 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25103 func_do_profile(fp);
25104 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025105 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025106 {
25107 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025108 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025109 profile_zero(&fp->uf_tm_children);
25110 }
25111 script_prof_save(&wait_start);
25112 }
25113#endif
25114
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025116 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117 save_did_emsg = did_emsg;
25118 did_emsg = FALSE;
25119
25120 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025121 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025122 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25123
25124 --RedrawingDisabled;
25125
25126 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025127 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025129 clear_tv(rettv);
25130 rettv->v_type = VAR_NUMBER;
25131 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025132 }
25133
Bram Moolenaar05159a02005-02-26 23:04:13 +000025134#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025135 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025136 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025137 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025138 profile_end(&call_start);
25139 profile_sub_wait(&wait_start, &call_start);
25140 profile_add(&fp->uf_tm_total, &call_start);
25141 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025142 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025143 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025144 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25145 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025146 }
25147 }
25148#endif
25149
Bram Moolenaar071d4272004-06-13 20:20:40 +000025150 /* when being verbose, mention the return value */
25151 if (p_verbose >= 12)
25152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025154 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025157 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025158 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025159 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025160 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025162 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025163 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025164 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025165 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025166 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025167
Bram Moolenaar555b2802005-05-19 21:08:39 +000025168 /* The value may be very long. Skip the middle part, so that we
25169 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025170 * truncate it at the end. Don't want errors such as E724 here. */
25171 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025172 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025173 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025174 if (s != NULL)
25175 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025176 if (vim_strsize(s) > MSG_BUF_CLEN)
25177 {
25178 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25179 s = buf;
25180 }
25181 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025182 vim_free(tofree);
25183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025184 }
25185 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025186
25187 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025188 --no_wait_return;
25189 }
25190
25191 vim_free(sourcing_name);
25192 sourcing_name = save_sourcing_name;
25193 sourcing_lnum = save_sourcing_lnum;
25194 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025195#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025196 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025197 script_prof_restore(&wait_start);
25198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199
25200 if (p_verbose >= 12 && sourcing_name != NULL)
25201 {
25202 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025203 verbose_enter_scroll();
25204
Bram Moolenaar555b2802005-05-19 21:08:39 +000025205 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025207
25208 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 --no_wait_return;
25210 }
25211
25212 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025213 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025215
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025216 /* If the a:000 list and the l: and a: dicts are not referenced we can
25217 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025218 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25219 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25220 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25221 {
25222 free_funccal(fc, FALSE);
25223 }
25224 else
25225 {
25226 hashitem_T *hi;
25227 listitem_T *li;
25228 int todo;
25229
25230 /* "fc" is still in use. This can happen when returning "a:000" or
25231 * assigning "l:" to a global variable.
25232 * Link "fc" in the list for garbage collection later. */
25233 fc->caller = previous_funccal;
25234 previous_funccal = fc;
25235
25236 /* Make a copy of the a: variables, since we didn't do that above. */
25237 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25238 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25239 {
25240 if (!HASHITEM_EMPTY(hi))
25241 {
25242 --todo;
25243 v = HI2DI(hi);
25244 copy_tv(&v->di_tv, &v->di_tv);
25245 }
25246 }
25247
25248 /* Make a copy of the a:000 items, since we didn't do that above. */
25249 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25250 copy_tv(&li->li_tv, &li->li_tv);
25251 }
25252}
25253
25254/*
25255 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025256 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025257 */
25258 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025259can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025260{
25261 return (fc->l_varlist.lv_copyID != copyID
25262 && fc->l_vars.dv_copyID != copyID
25263 && fc->l_avars.dv_copyID != copyID);
25264}
25265
25266/*
25267 * Free "fc" and what it contains.
25268 */
25269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025270free_funccal(
25271 funccall_T *fc,
25272 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025273{
25274 listitem_T *li;
25275
25276 /* The a: variables typevals may not have been allocated, only free the
25277 * allocated variables. */
25278 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25279
25280 /* free all l: variables */
25281 vars_clear(&fc->l_vars.dv_hashtab);
25282
25283 /* Free the a:000 variables if they were allocated. */
25284 if (free_val)
25285 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25286 clear_tv(&li->li_tv);
25287
25288 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289}
25290
25291/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025292 * Add a number variable "name" to dict "dp" with value "nr".
25293 */
25294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025295add_nr_var(
25296 dict_T *dp,
25297 dictitem_T *v,
25298 char *name,
25299 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025300{
25301 STRCPY(v->di_key, name);
25302 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25303 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25304 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025305 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025306 v->di_tv.vval.v_number = nr;
25307}
25308
25309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310 * ":return [expr]"
25311 */
25312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025313ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314{
25315 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025316 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317 int returning = FALSE;
25318
25319 if (current_funccal == NULL)
25320 {
25321 EMSG(_("E133: :return not inside a function"));
25322 return;
25323 }
25324
25325 if (eap->skip)
25326 ++emsg_skip;
25327
25328 eap->nextcmd = NULL;
25329 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025330 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 {
25332 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025333 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025335 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336 }
25337 /* It's safer to return also on error. */
25338 else if (!eap->skip)
25339 {
25340 /*
25341 * Return unless the expression evaluation has been cancelled due to an
25342 * aborting error, an interrupt, or an exception.
25343 */
25344 if (!aborting())
25345 returning = do_return(eap, FALSE, TRUE, NULL);
25346 }
25347
25348 /* When skipping or the return gets pending, advance to the next command
25349 * in this line (!returning). Otherwise, ignore the rest of the line.
25350 * Following lines will be ignored by get_func_line(). */
25351 if (returning)
25352 eap->nextcmd = NULL;
25353 else if (eap->nextcmd == NULL) /* no argument */
25354 eap->nextcmd = check_nextcmd(arg);
25355
25356 if (eap->skip)
25357 --emsg_skip;
25358}
25359
25360/*
25361 * Return from a function. Possibly makes the return pending. Also called
25362 * for a pending return at the ":endtry" or after returning from an extra
25363 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025364 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025365 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366 * FALSE when the return gets pending.
25367 */
25368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025369do_return(
25370 exarg_T *eap,
25371 int reanimate,
25372 int is_cmd,
25373 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374{
25375 int idx;
25376 struct condstack *cstack = eap->cstack;
25377
25378 if (reanimate)
25379 /* Undo the return. */
25380 current_funccal->returned = FALSE;
25381
25382 /*
25383 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25384 * not in its finally clause (which then is to be executed next) is found.
25385 * In this case, make the ":return" pending for execution at the ":endtry".
25386 * Otherwise, return normally.
25387 */
25388 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25389 if (idx >= 0)
25390 {
25391 cstack->cs_pending[idx] = CSTP_RETURN;
25392
25393 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025394 /* A pending return again gets pending. "rettv" points to an
25395 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025396 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025397 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398 else
25399 {
25400 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025401 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025402 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025403 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025405 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025406 {
25407 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025408 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025409 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410 else
25411 EMSG(_(e_outofmem));
25412 }
25413 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025414 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025415
25416 if (reanimate)
25417 {
25418 /* The pending return value could be overwritten by a ":return"
25419 * without argument in a finally clause; reset the default
25420 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025421 current_funccal->rettv->v_type = VAR_NUMBER;
25422 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025423 }
25424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025425 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426 }
25427 else
25428 {
25429 current_funccal->returned = TRUE;
25430
25431 /* If the return is carried out now, store the return value. For
25432 * a return immediately after reanimation, the value is already
25433 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025434 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025436 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025437 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025438 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025439 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025440 }
25441 }
25442
25443 return idx < 0;
25444}
25445
25446/*
25447 * Free the variable with a pending return value.
25448 */
25449 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025450discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451{
Bram Moolenaar33570922005-01-25 22:26:29 +000025452 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453}
25454
25455/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025456 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457 * is an allocated string. Used by report_pending() for verbose messages.
25458 */
25459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025460get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025462 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025463 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025464 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025466 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025467 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025468 if (s == NULL)
25469 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025470
25471 STRCPY(IObuff, ":return ");
25472 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25473 if (STRLEN(s) + 8 >= IOSIZE)
25474 STRCPY(IObuff + IOSIZE - 4, "...");
25475 vim_free(tofree);
25476 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477}
25478
25479/*
25480 * Get next function line.
25481 * Called by do_cmdline() to get the next line.
25482 * Returns allocated string, or NULL for end of function.
25483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025485get_func_line(
25486 int c UNUSED,
25487 void *cookie,
25488 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025489{
Bram Moolenaar33570922005-01-25 22:26:29 +000025490 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025491 ufunc_T *fp = fcp->func;
25492 char_u *retval;
25493 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494
25495 /* If breakpoints have been added/deleted need to check for it. */
25496 if (fcp->dbg_tick != debug_tick)
25497 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025498 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499 sourcing_lnum);
25500 fcp->dbg_tick = debug_tick;
25501 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025502#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025503 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025504 func_line_end(cookie);
25505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506
Bram Moolenaar05159a02005-02-26 23:04:13 +000025507 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025508 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25509 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025510 retval = NULL;
25511 else
25512 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025513 /* Skip NULL lines (continuation lines). */
25514 while (fcp->linenr < gap->ga_len
25515 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25516 ++fcp->linenr;
25517 if (fcp->linenr >= gap->ga_len)
25518 retval = NULL;
25519 else
25520 {
25521 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25522 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025523#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025524 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025525 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025526#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025528 }
25529
25530 /* Did we encounter a breakpoint? */
25531 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25532 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025533 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025535 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536 sourcing_lnum);
25537 fcp->dbg_tick = debug_tick;
25538 }
25539
25540 return retval;
25541}
25542
Bram Moolenaar05159a02005-02-26 23:04:13 +000025543#if defined(FEAT_PROFILE) || defined(PROTO)
25544/*
25545 * Called when starting to read a function line.
25546 * "sourcing_lnum" must be correct!
25547 * When skipping lines it may not actually be executed, but we won't find out
25548 * until later and we need to store the time now.
25549 */
25550 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025551func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025552{
25553 funccall_T *fcp = (funccall_T *)cookie;
25554 ufunc_T *fp = fcp->func;
25555
25556 if (fp->uf_profiling && sourcing_lnum >= 1
25557 && sourcing_lnum <= fp->uf_lines.ga_len)
25558 {
25559 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025560 /* Skip continuation lines. */
25561 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25562 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025563 fp->uf_tml_execed = FALSE;
25564 profile_start(&fp->uf_tml_start);
25565 profile_zero(&fp->uf_tml_children);
25566 profile_get_wait(&fp->uf_tml_wait);
25567 }
25568}
25569
25570/*
25571 * Called when actually executing a function line.
25572 */
25573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025574func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025575{
25576 funccall_T *fcp = (funccall_T *)cookie;
25577 ufunc_T *fp = fcp->func;
25578
25579 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25580 fp->uf_tml_execed = TRUE;
25581}
25582
25583/*
25584 * Called when done with a function line.
25585 */
25586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025587func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025588{
25589 funccall_T *fcp = (funccall_T *)cookie;
25590 ufunc_T *fp = fcp->func;
25591
25592 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25593 {
25594 if (fp->uf_tml_execed)
25595 {
25596 ++fp->uf_tml_count[fp->uf_tml_idx];
25597 profile_end(&fp->uf_tml_start);
25598 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025599 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025600 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25601 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025602 }
25603 fp->uf_tml_idx = -1;
25604 }
25605}
25606#endif
25607
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608/*
25609 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025610 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611 */
25612 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025613func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614{
Bram Moolenaar33570922005-01-25 22:26:29 +000025615 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616
25617 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25618 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025619 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620 || fcp->returned);
25621}
25622
25623/*
25624 * return TRUE if cookie indicates a function which "abort"s on errors.
25625 */
25626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025627func_has_abort(
25628 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025629{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025630 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025631}
25632
25633#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25634typedef enum
25635{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025636 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25637 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25638 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025639} var_flavour_T;
25640
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025641static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025642
25643 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025644var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645{
25646 char_u *p = varname;
25647
25648 if (ASCII_ISUPPER(*p))
25649 {
25650 while (*(++p))
25651 if (ASCII_ISLOWER(*p))
25652 return VAR_FLAVOUR_SESSION;
25653 return VAR_FLAVOUR_VIMINFO;
25654 }
25655 else
25656 return VAR_FLAVOUR_DEFAULT;
25657}
25658#endif
25659
25660#if defined(FEAT_VIMINFO) || defined(PROTO)
25661/*
25662 * Restore global vars that start with a capital from the viminfo file
25663 */
25664 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025665read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666{
25667 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025668 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025669 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025670 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671
25672 if (!writing && (find_viminfo_parameter('!') != NULL))
25673 {
25674 tab = vim_strchr(virp->vir_line + 1, '\t');
25675 if (tab != NULL)
25676 {
25677 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025678 switch (*tab)
25679 {
25680 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025681#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025682 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025683#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025684 case 'D': type = VAR_DICT; break;
25685 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025686 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025688
25689 tab = vim_strchr(tab, '\t');
25690 if (tab != NULL)
25691 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025692 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025693 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025694 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025695 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025696#ifdef FEAT_FLOAT
25697 else if (type == VAR_FLOAT)
25698 (void)string2float(tab + 1, &tv.vval.v_float);
25699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025700 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025701 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025702 if (type == VAR_DICT || type == VAR_LIST)
25703 {
25704 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25705
25706 if (etv == NULL)
25707 /* Failed to parse back the dict or list, use it as a
25708 * string. */
25709 tv.v_type = VAR_STRING;
25710 else
25711 {
25712 vim_free(tv.vval.v_string);
25713 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025714 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025715 }
25716 }
25717
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025718 /* when in a function use global variables */
25719 save_funccal = current_funccal;
25720 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025721 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025722 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025723
25724 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025725 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025726 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25727 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728 }
25729 }
25730 }
25731
25732 return viminfo_readline(virp);
25733}
25734
25735/*
25736 * Write global vars that start with a capital to the viminfo file
25737 */
25738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025739write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740{
Bram Moolenaar33570922005-01-25 22:26:29 +000025741 hashitem_T *hi;
25742 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025743 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025744 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025745 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025746 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025747 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025748
25749 if (find_viminfo_parameter('!') == NULL)
25750 return;
25751
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025752 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025753
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025754 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025755 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025757 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025759 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025760 this_var = HI2DI(hi);
25761 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025762 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025763 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025764 {
25765 case VAR_STRING: s = "STR"; break;
25766 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025767 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025768 case VAR_DICT: s = "DIC"; break;
25769 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025770 case VAR_SPECIAL: s = "XPL"; break;
25771
25772 case VAR_UNKNOWN:
25773 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025774 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025775 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025776 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025777 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025778 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025779 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025780 if (p != NULL)
25781 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025782 vim_free(tofree);
25783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784 }
25785 }
25786}
25787#endif
25788
25789#if defined(FEAT_SESSION) || defined(PROTO)
25790 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025791store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025792{
Bram Moolenaar33570922005-01-25 22:26:29 +000025793 hashitem_T *hi;
25794 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025795 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796 char_u *p, *t;
25797
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025798 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025799 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025800 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025801 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025803 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025804 this_var = HI2DI(hi);
25805 if ((this_var->di_tv.v_type == VAR_NUMBER
25806 || this_var->di_tv.v_type == VAR_STRING)
25807 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025808 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025809 /* Escape special characters with a backslash. Turn a LF and
25810 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025811 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025812 (char_u *)"\\\"\n\r");
25813 if (p == NULL) /* out of memory */
25814 break;
25815 for (t = p; *t != NUL; ++t)
25816 if (*t == '\n')
25817 *t = 'n';
25818 else if (*t == '\r')
25819 *t = 'r';
25820 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025821 this_var->di_key,
25822 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25823 : ' ',
25824 p,
25825 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25826 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025827 || put_eol(fd) == FAIL)
25828 {
25829 vim_free(p);
25830 return FAIL;
25831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025832 vim_free(p);
25833 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025834#ifdef FEAT_FLOAT
25835 else if (this_var->di_tv.v_type == VAR_FLOAT
25836 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25837 {
25838 float_T f = this_var->di_tv.vval.v_float;
25839 int sign = ' ';
25840
25841 if (f < 0)
25842 {
25843 f = -f;
25844 sign = '-';
25845 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025846 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025847 this_var->di_key, sign, f) < 0)
25848 || put_eol(fd) == FAIL)
25849 return FAIL;
25850 }
25851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852 }
25853 }
25854 return OK;
25855}
25856#endif
25857
Bram Moolenaar661b1822005-07-28 22:36:45 +000025858/*
25859 * Display script name where an item was last set.
25860 * Should only be invoked when 'verbose' is non-zero.
25861 */
25862 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025863last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025864{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025865 char_u *p;
25866
Bram Moolenaar661b1822005-07-28 22:36:45 +000025867 if (scriptID != 0)
25868 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025869 p = home_replace_save(NULL, get_scriptname(scriptID));
25870 if (p != NULL)
25871 {
25872 verbose_enter();
25873 MSG_PUTS(_("\n\tLast set from "));
25874 MSG_PUTS(p);
25875 vim_free(p);
25876 verbose_leave();
25877 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025878 }
25879}
25880
Bram Moolenaard812df62008-11-09 12:46:09 +000025881/*
25882 * List v:oldfiles in a nice way.
25883 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025885ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025886{
25887 list_T *l = vimvars[VV_OLDFILES].vv_list;
25888 listitem_T *li;
25889 int nr = 0;
25890
25891 if (l == NULL)
25892 msg((char_u *)_("No old files"));
25893 else
25894 {
25895 msg_start();
25896 msg_scroll = TRUE;
25897 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25898 {
25899 msg_outnum((long)++nr);
25900 MSG_PUTS(": ");
25901 msg_outtrans(get_tv_string(&li->li_tv));
25902 msg_putchar('\n');
25903 out_flush(); /* output one line at a time */
25904 ui_breakcheck();
25905 }
25906 /* Assume "got_int" was set to truncate the listing. */
25907 got_int = FALSE;
25908
25909#ifdef FEAT_BROWSE_CMD
25910 if (cmdmod.browse)
25911 {
25912 quit_more = FALSE;
25913 nr = prompt_for_number(FALSE);
25914 msg_starthere();
25915 if (nr > 0)
25916 {
25917 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25918 (long)nr);
25919
25920 if (p != NULL)
25921 {
25922 p = expand_env_save(p);
25923 eap->arg = p;
25924 eap->cmdidx = CMD_edit;
25925 cmdmod.browse = FALSE;
25926 do_exedit(eap, NULL);
25927 vim_free(p);
25928 }
25929 }
25930 }
25931#endif
25932 }
25933}
25934
Bram Moolenaar53744302015-07-17 17:38:22 +020025935/* reset v:option_new, v:option_old and v:option_type */
25936 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025937reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025938{
25939 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25940 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25941 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25942}
25943
25944
Bram Moolenaar071d4272004-06-13 20:20:40 +000025945#endif /* FEAT_EVAL */
25946
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025948#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025949
25950#ifdef WIN3264
25951/*
25952 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25953 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025954static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25955static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25956static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025957
25958/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025959 * Get the short path (8.3) for the filename in "fnamep".
25960 * Only works for a valid file name.
25961 * When the path gets longer "fnamep" is changed and the allocated buffer
25962 * is put in "bufp".
25963 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25964 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025965 */
25966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025967get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025969 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970 char_u *newbuf;
25971
25972 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025973 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025974 if (l > len - 1)
25975 {
25976 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025977 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025978 newbuf = vim_strnsave(*fnamep, l);
25979 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025980 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025981
25982 vim_free(*bufp);
25983 *fnamep = *bufp = newbuf;
25984
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025985 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025986 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987 }
25988
25989 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025990 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025991}
25992
25993/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025994 * Get the short path (8.3) for the filename in "fname". The converted
25995 * path is returned in "bufp".
25996 *
25997 * Some of the directories specified in "fname" may not exist. This function
25998 * will shorten the existing directories at the beginning of the path and then
25999 * append the remaining non-existing path.
26000 *
26001 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026002 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026003 * bufp - Pointer to an allocated buffer for the filename.
26004 * fnamelen - Length of the filename pointed to by fname
26005 *
26006 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007 */
26008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026009shortpath_for_invalid_fname(
26010 char_u **fname,
26011 char_u **bufp,
26012 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026014 char_u *short_fname, *save_fname, *pbuf_unused;
26015 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026016 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026017 int old_len, len;
26018 int new_len, sfx_len;
26019 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026020
26021 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026022 old_len = *fnamelen;
26023 save_fname = vim_strnsave(*fname, old_len);
26024 pbuf_unused = NULL;
26025 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026027 endp = save_fname + old_len - 1; /* Find the end of the copy */
26028 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026029
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026030 /*
26031 * Try shortening the supplied path till it succeeds by removing one
26032 * directory at a time from the tail of the path.
26033 */
26034 len = 0;
26035 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026036 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026037 /* go back one path-separator */
26038 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26039 --endp;
26040 if (endp <= save_fname)
26041 break; /* processed the complete path */
26042
26043 /*
26044 * Replace the path separator with a NUL and try to shorten the
26045 * resulting path.
26046 */
26047 ch = *endp;
26048 *endp = 0;
26049 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026050 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026051 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26052 {
26053 retval = FAIL;
26054 goto theend;
26055 }
26056 *endp = ch; /* preserve the string */
26057
26058 if (len > 0)
26059 break; /* successfully shortened the path */
26060
26061 /* failed to shorten the path. Skip the path separator */
26062 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063 }
26064
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026065 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026066 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026067 /*
26068 * Succeeded in shortening the path. Now concatenate the shortened
26069 * path with the remaining path at the tail.
26070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026072 /* Compute the length of the new path. */
26073 sfx_len = (int)(save_endp - endp) + 1;
26074 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026075
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026076 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026078 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026079 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026080 /* There is not enough space in the currently allocated string,
26081 * copy it to a buffer big enough. */
26082 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026083 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026084 {
26085 retval = FAIL;
26086 goto theend;
26087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026088 }
26089 else
26090 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026091 /* Transfer short_fname to the main buffer (it's big enough),
26092 * unless get_short_pathname() did its work in-place. */
26093 *fname = *bufp = save_fname;
26094 if (short_fname != save_fname)
26095 vim_strncpy(save_fname, short_fname, len);
26096 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026097 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026098
26099 /* concat the not-shortened part of the path */
26100 vim_strncpy(*fname + len, endp, sfx_len);
26101 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026103
26104theend:
26105 vim_free(pbuf_unused);
26106 vim_free(save_fname);
26107
26108 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026109}
26110
26111/*
26112 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026113 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026114 */
26115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026116shortpath_for_partial(
26117 char_u **fnamep,
26118 char_u **bufp,
26119 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026120{
26121 int sepcount, len, tflen;
26122 char_u *p;
26123 char_u *pbuf, *tfname;
26124 int hasTilde;
26125
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026126 /* Count up the path separators from the RHS.. so we know which part
26127 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026128 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026129 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026130 if (vim_ispathsep(*p))
26131 ++sepcount;
26132
26133 /* Need full path first (use expand_env() to remove a "~/") */
26134 hasTilde = (**fnamep == '~');
26135 if (hasTilde)
26136 pbuf = tfname = expand_env_save(*fnamep);
26137 else
26138 pbuf = tfname = FullName_save(*fnamep, FALSE);
26139
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026140 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026142 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26143 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144
26145 if (len == 0)
26146 {
26147 /* Don't have a valid filename, so shorten the rest of the
26148 * path if we can. This CAN give us invalid 8.3 filenames, but
26149 * there's not a lot of point in guessing what it might be.
26150 */
26151 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026152 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26153 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026154 }
26155
26156 /* Count the paths backward to find the beginning of the desired string. */
26157 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026158 {
26159#ifdef FEAT_MBYTE
26160 if (has_mbyte)
26161 p -= mb_head_off(tfname, p);
26162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026163 if (vim_ispathsep(*p))
26164 {
26165 if (sepcount == 0 || (hasTilde && sepcount == 1))
26166 break;
26167 else
26168 sepcount --;
26169 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171 if (hasTilde)
26172 {
26173 --p;
26174 if (p >= tfname)
26175 *p = '~';
26176 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026177 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026178 }
26179 else
26180 ++p;
26181
26182 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26183 vim_free(*bufp);
26184 *fnamelen = (int)STRLEN(p);
26185 *bufp = pbuf;
26186 *fnamep = p;
26187
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026188 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026189}
26190#endif /* WIN3264 */
26191
26192/*
26193 * Adjust a filename, according to a string of modifiers.
26194 * *fnamep must be NUL terminated when called. When returning, the length is
26195 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026196 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026197 * When there is an error, *fnamep is set to NULL.
26198 */
26199 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026200modify_fname(
26201 char_u *src, /* string with modifiers */
26202 int *usedlen, /* characters after src that are used */
26203 char_u **fnamep, /* file name so far */
26204 char_u **bufp, /* buffer for allocated file name or NULL */
26205 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026206{
26207 int valid = 0;
26208 char_u *tail;
26209 char_u *s, *p, *pbuf;
26210 char_u dirname[MAXPATHL];
26211 int c;
26212 int has_fullname = 0;
26213#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026214 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026215 int has_shortname = 0;
26216#endif
26217
26218repeat:
26219 /* ":p" - full path/file_name */
26220 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26221 {
26222 has_fullname = 1;
26223
26224 valid |= VALID_PATH;
26225 *usedlen += 2;
26226
26227 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26228 if ((*fnamep)[0] == '~'
26229#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26230 && ((*fnamep)[1] == '/'
26231# ifdef BACKSLASH_IN_FILENAME
26232 || (*fnamep)[1] == '\\'
26233# endif
26234 || (*fnamep)[1] == NUL)
26235
26236#endif
26237 )
26238 {
26239 *fnamep = expand_env_save(*fnamep);
26240 vim_free(*bufp); /* free any allocated file name */
26241 *bufp = *fnamep;
26242 if (*fnamep == NULL)
26243 return -1;
26244 }
26245
26246 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026247 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026248 {
26249 if (vim_ispathsep(*p)
26250 && p[1] == '.'
26251 && (p[2] == NUL
26252 || vim_ispathsep(p[2])
26253 || (p[2] == '.'
26254 && (p[3] == NUL || vim_ispathsep(p[3])))))
26255 break;
26256 }
26257
26258 /* FullName_save() is slow, don't use it when not needed. */
26259 if (*p != NUL || !vim_isAbsName(*fnamep))
26260 {
26261 *fnamep = FullName_save(*fnamep, *p != NUL);
26262 vim_free(*bufp); /* free any allocated file name */
26263 *bufp = *fnamep;
26264 if (*fnamep == NULL)
26265 return -1;
26266 }
26267
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026268#ifdef WIN3264
26269# if _WIN32_WINNT >= 0x0500
26270 if (vim_strchr(*fnamep, '~') != NULL)
26271 {
26272 /* Expand 8.3 filename to full path. Needed to make sure the same
26273 * file does not have two different names.
26274 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26275 p = alloc(_MAX_PATH + 1);
26276 if (p != NULL)
26277 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026278 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026279 {
26280 vim_free(*bufp);
26281 *bufp = *fnamep = p;
26282 }
26283 else
26284 vim_free(p);
26285 }
26286 }
26287# endif
26288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026289 /* Append a path separator to a directory. */
26290 if (mch_isdir(*fnamep))
26291 {
26292 /* Make room for one or two extra characters. */
26293 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26294 vim_free(*bufp); /* free any allocated file name */
26295 *bufp = *fnamep;
26296 if (*fnamep == NULL)
26297 return -1;
26298 add_pathsep(*fnamep);
26299 }
26300 }
26301
26302 /* ":." - path relative to the current directory */
26303 /* ":~" - path relative to the home directory */
26304 /* ":8" - shortname path - postponed till after */
26305 while (src[*usedlen] == ':'
26306 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26307 {
26308 *usedlen += 2;
26309 if (c == '8')
26310 {
26311#ifdef WIN3264
26312 has_shortname = 1; /* Postpone this. */
26313#endif
26314 continue;
26315 }
26316 pbuf = NULL;
26317 /* Need full path first (use expand_env() to remove a "~/") */
26318 if (!has_fullname)
26319 {
26320 if (c == '.' && **fnamep == '~')
26321 p = pbuf = expand_env_save(*fnamep);
26322 else
26323 p = pbuf = FullName_save(*fnamep, FALSE);
26324 }
26325 else
26326 p = *fnamep;
26327
26328 has_fullname = 0;
26329
26330 if (p != NULL)
26331 {
26332 if (c == '.')
26333 {
26334 mch_dirname(dirname, MAXPATHL);
26335 s = shorten_fname(p, dirname);
26336 if (s != NULL)
26337 {
26338 *fnamep = s;
26339 if (pbuf != NULL)
26340 {
26341 vim_free(*bufp); /* free any allocated file name */
26342 *bufp = pbuf;
26343 pbuf = NULL;
26344 }
26345 }
26346 }
26347 else
26348 {
26349 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26350 /* Only replace it when it starts with '~' */
26351 if (*dirname == '~')
26352 {
26353 s = vim_strsave(dirname);
26354 if (s != NULL)
26355 {
26356 *fnamep = s;
26357 vim_free(*bufp);
26358 *bufp = s;
26359 }
26360 }
26361 }
26362 vim_free(pbuf);
26363 }
26364 }
26365
26366 tail = gettail(*fnamep);
26367 *fnamelen = (int)STRLEN(*fnamep);
26368
26369 /* ":h" - head, remove "/file_name", can be repeated */
26370 /* Don't remove the first "/" or "c:\" */
26371 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26372 {
26373 valid |= VALID_HEAD;
26374 *usedlen += 2;
26375 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026376 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026377 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026378 *fnamelen = (int)(tail - *fnamep);
26379#ifdef VMS
26380 if (*fnamelen > 0)
26381 *fnamelen += 1; /* the path separator is part of the path */
26382#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026383 if (*fnamelen == 0)
26384 {
26385 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26386 p = vim_strsave((char_u *)".");
26387 if (p == NULL)
26388 return -1;
26389 vim_free(*bufp);
26390 *bufp = *fnamep = tail = p;
26391 *fnamelen = 1;
26392 }
26393 else
26394 {
26395 while (tail > s && !after_pathsep(s, tail))
26396 mb_ptr_back(*fnamep, tail);
26397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026398 }
26399
26400 /* ":8" - shortname */
26401 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26402 {
26403 *usedlen += 2;
26404#ifdef WIN3264
26405 has_shortname = 1;
26406#endif
26407 }
26408
26409#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026410 /*
26411 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026412 */
26413 if (has_shortname)
26414 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026415 /* Copy the string if it is shortened by :h and when it wasn't copied
26416 * yet, because we are going to change it in place. Avoids changing
26417 * the buffer name for "%:8". */
26418 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026419 {
26420 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026421 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026422 return -1;
26423 vim_free(*bufp);
26424 *bufp = *fnamep = p;
26425 }
26426
26427 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026428 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026429 if (!has_fullname && !vim_isAbsName(*fnamep))
26430 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026431 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026432 return -1;
26433 }
26434 else
26435 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026436 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026437
Bram Moolenaardc935552011-08-17 15:23:23 +020026438 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026439 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026440 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026441 return -1;
26442
26443 if (l == 0)
26444 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026445 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026447 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026448 return -1;
26449 }
26450 *fnamelen = l;
26451 }
26452 }
26453#endif /* WIN3264 */
26454
26455 /* ":t" - tail, just the basename */
26456 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26457 {
26458 *usedlen += 2;
26459 *fnamelen -= (int)(tail - *fnamep);
26460 *fnamep = tail;
26461 }
26462
26463 /* ":e" - extension, can be repeated */
26464 /* ":r" - root, without extension, can be repeated */
26465 while (src[*usedlen] == ':'
26466 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26467 {
26468 /* find a '.' in the tail:
26469 * - for second :e: before the current fname
26470 * - otherwise: The last '.'
26471 */
26472 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26473 s = *fnamep - 2;
26474 else
26475 s = *fnamep + *fnamelen - 1;
26476 for ( ; s > tail; --s)
26477 if (s[0] == '.')
26478 break;
26479 if (src[*usedlen + 1] == 'e') /* :e */
26480 {
26481 if (s > tail)
26482 {
26483 *fnamelen += (int)(*fnamep - (s + 1));
26484 *fnamep = s + 1;
26485#ifdef VMS
26486 /* cut version from the extension */
26487 s = *fnamep + *fnamelen - 1;
26488 for ( ; s > *fnamep; --s)
26489 if (s[0] == ';')
26490 break;
26491 if (s > *fnamep)
26492 *fnamelen = s - *fnamep;
26493#endif
26494 }
26495 else if (*fnamep <= tail)
26496 *fnamelen = 0;
26497 }
26498 else /* :r */
26499 {
26500 if (s > tail) /* remove one extension */
26501 *fnamelen = (int)(s - *fnamep);
26502 }
26503 *usedlen += 2;
26504 }
26505
26506 /* ":s?pat?foo?" - substitute */
26507 /* ":gs?pat?foo?" - global substitute */
26508 if (src[*usedlen] == ':'
26509 && (src[*usedlen + 1] == 's'
26510 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26511 {
26512 char_u *str;
26513 char_u *pat;
26514 char_u *sub;
26515 int sep;
26516 char_u *flags;
26517 int didit = FALSE;
26518
26519 flags = (char_u *)"";
26520 s = src + *usedlen + 2;
26521 if (src[*usedlen + 1] == 'g')
26522 {
26523 flags = (char_u *)"g";
26524 ++s;
26525 }
26526
26527 sep = *s++;
26528 if (sep)
26529 {
26530 /* find end of pattern */
26531 p = vim_strchr(s, sep);
26532 if (p != NULL)
26533 {
26534 pat = vim_strnsave(s, (int)(p - s));
26535 if (pat != NULL)
26536 {
26537 s = p + 1;
26538 /* find end of substitution */
26539 p = vim_strchr(s, sep);
26540 if (p != NULL)
26541 {
26542 sub = vim_strnsave(s, (int)(p - s));
26543 str = vim_strnsave(*fnamep, *fnamelen);
26544 if (sub != NULL && str != NULL)
26545 {
26546 *usedlen = (int)(p + 1 - src);
26547 s = do_string_sub(str, pat, sub, flags);
26548 if (s != NULL)
26549 {
26550 *fnamep = s;
26551 *fnamelen = (int)STRLEN(s);
26552 vim_free(*bufp);
26553 *bufp = s;
26554 didit = TRUE;
26555 }
26556 }
26557 vim_free(sub);
26558 vim_free(str);
26559 }
26560 vim_free(pat);
26561 }
26562 }
26563 /* after using ":s", repeat all the modifiers */
26564 if (didit)
26565 goto repeat;
26566 }
26567 }
26568
Bram Moolenaar26df0922014-02-23 23:39:13 +010026569 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26570 {
26571 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26572 if (p == NULL)
26573 return -1;
26574 vim_free(*bufp);
26575 *bufp = *fnamep = p;
26576 *fnamelen = (int)STRLEN(p);
26577 *usedlen += 2;
26578 }
26579
Bram Moolenaar071d4272004-06-13 20:20:40 +000026580 return valid;
26581}
26582
26583/*
26584 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26585 * "flags" can be "g" to do a global substitute.
26586 * Returns an allocated string, NULL for error.
26587 */
26588 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026589do_string_sub(
26590 char_u *str,
26591 char_u *pat,
26592 char_u *sub,
26593 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026594{
26595 int sublen;
26596 regmatch_T regmatch;
26597 int i;
26598 int do_all;
26599 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026600 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026601 garray_T ga;
26602 char_u *ret;
26603 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026604 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026605
26606 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26607 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026608 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026609
26610 ga_init2(&ga, 1, 200);
26611
26612 do_all = (flags[0] == 'g');
26613
26614 regmatch.rm_ic = p_ic;
26615 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26616 if (regmatch.regprog != NULL)
26617 {
26618 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026619 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026620 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26621 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026622 /* Skip empty match except for first match. */
26623 if (regmatch.startp[0] == regmatch.endp[0])
26624 {
26625 if (zero_width == regmatch.startp[0])
26626 {
26627 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026628 i = MB_PTR2LEN(tail);
26629 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26630 (size_t)i);
26631 ga.ga_len += i;
26632 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026633 continue;
26634 }
26635 zero_width = regmatch.startp[0];
26636 }
26637
Bram Moolenaar071d4272004-06-13 20:20:40 +000026638 /*
26639 * Get some space for a temporary buffer to do the substitution
26640 * into. It will contain:
26641 * - The text up to where the match is.
26642 * - The substituted text.
26643 * - The text after the match.
26644 */
26645 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026646 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026647 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26648 {
26649 ga_clear(&ga);
26650 break;
26651 }
26652
26653 /* copy the text up to where the match is */
26654 i = (int)(regmatch.startp[0] - tail);
26655 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26656 /* add the substituted text */
26657 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26658 + ga.ga_len + i, TRUE, TRUE, FALSE);
26659 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026660 tail = regmatch.endp[0];
26661 if (*tail == NUL)
26662 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026663 if (!do_all)
26664 break;
26665 }
26666
26667 if (ga.ga_data != NULL)
26668 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26669
Bram Moolenaar473de612013-06-08 18:19:48 +020026670 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026671 }
26672
26673 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26674 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026675 if (p_cpo == empty_option)
26676 p_cpo = save_cpo;
26677 else
26678 /* Darn, evaluating {sub} expression changed the value. */
26679 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026680
26681 return ret;
26682}
26683
26684#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */