blob: 2a0f92a102a7b12e2f35395e3d80908caae3ff80 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000096static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000097static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000098static char *e_undefvar = N_("E121: Undefined variable: %s");
99static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000100static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000101static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_listreq = N_("E714: List required");
103static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200368 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200369 {VV_NAME("option_new", VAR_STRING), VV_RO},
370 {VV_NAME("option_old", VAR_STRING), VV_RO},
371 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100372 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 {VV_NAME("false", VAR_SPECIAL), VV_RO},
374 {VV_NAME("true", VAR_SPECIAL), VV_RO},
375 {VV_NAME("null", VAR_SPECIAL), VV_RO},
376 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000377};
378
379/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000380#define vv_type vv_di.di_tv.v_type
381#define vv_nr vv_di.di_tv.vval.v_number
382#define vv_float vv_di.di_tv.vval.v_float
383#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000384#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200385#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000387
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200388static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000389#define vimvarht vimvardict.dv_hashtab
390
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100391static void prepare_vimvar(int idx, typval_T *save_tv);
392static void restore_vimvar(int idx, typval_T *save_tv);
393static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
394static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
395static char_u *skip_var_one(char_u *arg);
396static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
397static void list_glob_vars(int *first);
398static void list_buf_vars(int *first);
399static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000400#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100401static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000402#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100403static void list_vim_vars(int *first);
404static void list_script_vars(int *first);
405static void list_func_vars(int *first);
406static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
407static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
408static int check_changedtick(char_u *arg);
409static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
410static void clear_lval(lval_T *lp);
411static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
412static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
413static void list_fix_watch(list_T *l, listitem_T *item);
414static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
415static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
416static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
417static void item_lock(typval_T *tv, int deep, int lock);
418static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000419
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100420static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
421static int eval1(char_u **arg, typval_T *rettv, int evaluate);
422static int eval2(char_u **arg, typval_T *rettv, int evaluate);
423static int eval3(char_u **arg, typval_T *rettv, int evaluate);
424static int eval4(char_u **arg, typval_T *rettv, int evaluate);
425static int eval5(char_u **arg, typval_T *rettv, int evaluate);
426static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
427static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000428
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100429static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
430static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
433static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
434static long list_len(list_T *l);
435static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
436static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
437static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
438static long list_find_nr(list_T *l, long idx, int *errorp);
439static long list_idx_of_item(list_T *l, listitem_T *item);
440static int list_append_number(list_T *l, varnumber_T n);
441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100454#ifdef FEAT_JOB
455static void job_free(job_T *job);
456#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
458static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
462static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
463static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
483static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100485static void f_asin(typval_T *argvars, typval_T *rettv);
486static void f_atan(typval_T *argvars, typval_T *rettv);
487static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_browse(typval_T *argvars, typval_T *rettv);
490static void f_browsedir(typval_T *argvars, typval_T *rettv);
491static void f_bufexists(typval_T *argvars, typval_T *rettv);
492static void f_buflisted(typval_T *argvars, typval_T *rettv);
493static void f_bufloaded(typval_T *argvars, typval_T *rettv);
494static void f_bufname(typval_T *argvars, typval_T *rettv);
495static void f_bufnr(typval_T *argvars, typval_T *rettv);
496static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
497static void f_byte2line(typval_T *argvars, typval_T *rettv);
498static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
499static void f_byteidx(typval_T *argvars, typval_T *rettv);
500static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
501static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100503static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100505#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100506static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram 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);
510static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100511static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
512static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100513static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100514static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100516static void f_changenr(typval_T *argvars, typval_T *rettv);
517static void f_char2nr(typval_T *argvars, typval_T *rettv);
518static void f_cindent(typval_T *argvars, typval_T *rettv);
519static void f_clearmatches(typval_T *argvars, typval_T *rettv);
520static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000521#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100522static void f_complete(typval_T *argvars, typval_T *rettv);
523static void f_complete_add(typval_T *argvars, typval_T *rettv);
524static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000525#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100526static void f_confirm(typval_T *argvars, typval_T *rettv);
527static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100529static void f_cos(typval_T *argvars, typval_T *rettv);
530static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000531#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100532static void f_count(typval_T *argvars, typval_T *rettv);
533static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
534static void f_cursor(typval_T *argsvars, typval_T *rettv);
535static void f_deepcopy(typval_T *argvars, typval_T *rettv);
536static void f_delete(typval_T *argvars, typval_T *rettv);
537static void f_did_filetype(typval_T *argvars, typval_T *rettv);
538static void f_diff_filler(typval_T *argvars, typval_T *rettv);
539static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100540static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_empty(typval_T *argvars, typval_T *rettv);
542static void f_escape(typval_T *argvars, typval_T *rettv);
543static void f_eval(typval_T *argvars, typval_T *rettv);
544static void f_eventhandler(typval_T *argvars, typval_T *rettv);
545static void f_executable(typval_T *argvars, typval_T *rettv);
546static void f_exepath(typval_T *argvars, typval_T *rettv);
547static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200550#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_expand(typval_T *argvars, typval_T *rettv);
552static void f_extend(typval_T *argvars, typval_T *rettv);
553static void f_feedkeys(typval_T *argvars, typval_T *rettv);
554static void f_filereadable(typval_T *argvars, typval_T *rettv);
555static void f_filewritable(typval_T *argvars, typval_T *rettv);
556static void f_filter(typval_T *argvars, typval_T *rettv);
557static void f_finddir(typval_T *argvars, typval_T *rettv);
558static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000559#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_float2nr(typval_T *argvars, typval_T *rettv);
561static void f_floor(typval_T *argvars, typval_T *rettv);
562static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000563#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_fnameescape(typval_T *argvars, typval_T *rettv);
565static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
566static void f_foldclosed(typval_T *argvars, typval_T *rettv);
567static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
568static void f_foldlevel(typval_T *argvars, typval_T *rettv);
569static void f_foldtext(typval_T *argvars, typval_T *rettv);
570static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
571static void f_foreground(typval_T *argvars, typval_T *rettv);
572static void f_function(typval_T *argvars, typval_T *rettv);
573static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
574static void f_get(typval_T *argvars, typval_T *rettv);
575static void f_getbufline(typval_T *argvars, typval_T *rettv);
576static void f_getbufvar(typval_T *argvars, typval_T *rettv);
577static void f_getchar(typval_T *argvars, typval_T *rettv);
578static void f_getcharmod(typval_T *argvars, typval_T *rettv);
579static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
580static void f_getcmdline(typval_T *argvars, typval_T *rettv);
581static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
582static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
583static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
584static void f_getcwd(typval_T *argvars, typval_T *rettv);
585static void f_getfontname(typval_T *argvars, typval_T *rettv);
586static void f_getfperm(typval_T *argvars, typval_T *rettv);
587static void f_getfsize(typval_T *argvars, typval_T *rettv);
588static void f_getftime(typval_T *argvars, typval_T *rettv);
589static void f_getftype(typval_T *argvars, typval_T *rettv);
590static void f_getline(typval_T *argvars, typval_T *rettv);
591static void f_getmatches(typval_T *argvars, typval_T *rettv);
592static void f_getpid(typval_T *argvars, typval_T *rettv);
593static void f_getcurpos(typval_T *argvars, typval_T *rettv);
594static void f_getpos(typval_T *argvars, typval_T *rettv);
595static void f_getqflist(typval_T *argvars, typval_T *rettv);
596static void f_getreg(typval_T *argvars, typval_T *rettv);
597static void f_getregtype(typval_T *argvars, typval_T *rettv);
598static void f_gettabvar(typval_T *argvars, typval_T *rettv);
599static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
600static void f_getwinposx(typval_T *argvars, typval_T *rettv);
601static void f_getwinposy(typval_T *argvars, typval_T *rettv);
602static void f_getwinvar(typval_T *argvars, typval_T *rettv);
603static void f_glob(typval_T *argvars, typval_T *rettv);
604static void f_globpath(typval_T *argvars, typval_T *rettv);
605static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
606static void f_has(typval_T *argvars, typval_T *rettv);
607static void f_has_key(typval_T *argvars, typval_T *rettv);
608static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
609static void f_hasmapto(typval_T *argvars, typval_T *rettv);
610static void f_histadd(typval_T *argvars, typval_T *rettv);
611static void f_histdel(typval_T *argvars, typval_T *rettv);
612static void f_histget(typval_T *argvars, typval_T *rettv);
613static void f_histnr(typval_T *argvars, typval_T *rettv);
614static void f_hlID(typval_T *argvars, typval_T *rettv);
615static void f_hlexists(typval_T *argvars, typval_T *rettv);
616static void f_hostname(typval_T *argvars, typval_T *rettv);
617static void f_iconv(typval_T *argvars, typval_T *rettv);
618static void f_indent(typval_T *argvars, typval_T *rettv);
619static void f_index(typval_T *argvars, typval_T *rettv);
620static void f_input(typval_T *argvars, typval_T *rettv);
621static void f_inputdialog(typval_T *argvars, typval_T *rettv);
622static void f_inputlist(typval_T *argvars, typval_T *rettv);
623static void f_inputrestore(typval_T *argvars, typval_T *rettv);
624static void f_inputsave(typval_T *argvars, typval_T *rettv);
625static void f_inputsecret(typval_T *argvars, typval_T *rettv);
626static void f_insert(typval_T *argvars, typval_T *rettv);
627static void f_invert(typval_T *argvars, typval_T *rettv);
628static void f_isdirectory(typval_T *argvars, typval_T *rettv);
629static void f_islocked(typval_T *argvars, typval_T *rettv);
630static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100631#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100632# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100633static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100634# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +0100635static void f_job_start(typval_T *argvars, typval_T *rettv);
636static void f_job_stop(typval_T *argvars, typval_T *rettv);
637static void f_job_status(typval_T *argvars, typval_T *rettv);
638#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100639static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100640static void f_js_decode(typval_T *argvars, typval_T *rettv);
641static void f_js_encode(typval_T *argvars, typval_T *rettv);
642static void f_json_decode(typval_T *argvars, typval_T *rettv);
643static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_keys(typval_T *argvars, typval_T *rettv);
645static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
646static void f_len(typval_T *argvars, typval_T *rettv);
647static void f_libcall(typval_T *argvars, typval_T *rettv);
648static void f_libcallnr(typval_T *argvars, typval_T *rettv);
649static void f_line(typval_T *argvars, typval_T *rettv);
650static void f_line2byte(typval_T *argvars, typval_T *rettv);
651static void f_lispindent(typval_T *argvars, typval_T *rettv);
652static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000653#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100654static void f_log(typval_T *argvars, typval_T *rettv);
655static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000656#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200657#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200659#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_map(typval_T *argvars, typval_T *rettv);
661static void f_maparg(typval_T *argvars, typval_T *rettv);
662static void f_mapcheck(typval_T *argvars, typval_T *rettv);
663static void f_match(typval_T *argvars, typval_T *rettv);
664static void f_matchadd(typval_T *argvars, typval_T *rettv);
665static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
666static void f_matcharg(typval_T *argvars, typval_T *rettv);
667static void f_matchdelete(typval_T *argvars, typval_T *rettv);
668static void f_matchend(typval_T *argvars, typval_T *rettv);
669static void f_matchlist(typval_T *argvars, typval_T *rettv);
670static void f_matchstr(typval_T *argvars, typval_T *rettv);
671static void f_max(typval_T *argvars, typval_T *rettv);
672static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000673#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000675#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100677#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
681static void f_nr2char(typval_T *argvars, typval_T *rettv);
682static void f_or(typval_T *argvars, typval_T *rettv);
683static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100684#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100686#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000687#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
691static void f_printf(typval_T *argvars, typval_T *rettv);
692static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200693#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200695#endif
696#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_range(typval_T *argvars, typval_T *rettv);
700static void f_readfile(typval_T *argvars, typval_T *rettv);
701static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100702#ifdef FEAT_FLOAT
703static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
704#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_reltimestr(typval_T *argvars, typval_T *rettv);
706static void f_remote_expr(typval_T *argvars, typval_T *rettv);
707static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
708static void f_remote_peek(typval_T *argvars, typval_T *rettv);
709static void f_remote_read(typval_T *argvars, typval_T *rettv);
710static void f_remote_send(typval_T *argvars, typval_T *rettv);
711static void f_remove(typval_T *argvars, typval_T *rettv);
712static void f_rename(typval_T *argvars, typval_T *rettv);
713static void f_repeat(typval_T *argvars, typval_T *rettv);
714static void f_resolve(typval_T *argvars, typval_T *rettv);
715static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000716#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100717static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_screenattr(typval_T *argvars, typval_T *rettv);
720static void f_screenchar(typval_T *argvars, typval_T *rettv);
721static void f_screencol(typval_T *argvars, typval_T *rettv);
722static void f_screenrow(typval_T *argvars, typval_T *rettv);
723static void f_search(typval_T *argvars, typval_T *rettv);
724static void f_searchdecl(typval_T *argvars, typval_T *rettv);
725static void f_searchpair(typval_T *argvars, typval_T *rettv);
726static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
727static void f_searchpos(typval_T *argvars, typval_T *rettv);
728static void f_server2client(typval_T *argvars, typval_T *rettv);
729static void f_serverlist(typval_T *argvars, typval_T *rettv);
730static void f_setbufvar(typval_T *argvars, typval_T *rettv);
731static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
732static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
733static void f_setline(typval_T *argvars, typval_T *rettv);
734static void f_setloclist(typval_T *argvars, typval_T *rettv);
735static void f_setmatches(typval_T *argvars, typval_T *rettv);
736static void f_setpos(typval_T *argvars, typval_T *rettv);
737static void f_setqflist(typval_T *argvars, typval_T *rettv);
738static void f_setreg(typval_T *argvars, typval_T *rettv);
739static void f_settabvar(typval_T *argvars, typval_T *rettv);
740static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
741static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100742#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100743static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100744#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_shellescape(typval_T *argvars, typval_T *rettv);
746static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
747static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000748#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_sin(typval_T *argvars, typval_T *rettv);
750static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_sort(typval_T *argvars, typval_T *rettv);
753static void f_soundfold(typval_T *argvars, typval_T *rettv);
754static void f_spellbadword(typval_T *argvars, typval_T *rettv);
755static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
756static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000757#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sqrt(typval_T *argvars, typval_T *rettv);
759static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000760#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_str2nr(typval_T *argvars, typval_T *rettv);
762static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000763#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_stridx(typval_T *argvars, typval_T *rettv);
767static void f_string(typval_T *argvars, typval_T *rettv);
768static void f_strlen(typval_T *argvars, typval_T *rettv);
769static void f_strpart(typval_T *argvars, typval_T *rettv);
770static void f_strridx(typval_T *argvars, typval_T *rettv);
771static void f_strtrans(typval_T *argvars, typval_T *rettv);
772static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
773static void f_strwidth(typval_T *argvars, typval_T *rettv);
774static void f_submatch(typval_T *argvars, typval_T *rettv);
775static void f_substitute(typval_T *argvars, typval_T *rettv);
776static void f_synID(typval_T *argvars, typval_T *rettv);
777static void f_synIDattr(typval_T *argvars, typval_T *rettv);
778static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
779static void f_synstack(typval_T *argvars, typval_T *rettv);
780static void f_synconcealed(typval_T *argvars, typval_T *rettv);
781static void f_system(typval_T *argvars, typval_T *rettv);
782static void f_systemlist(typval_T *argvars, typval_T *rettv);
783static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
784static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
785static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
786static void f_taglist(typval_T *argvars, typval_T *rettv);
787static void f_tagfiles(typval_T *argvars, typval_T *rettv);
788static void f_tempname(typval_T *argvars, typval_T *rettv);
789static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200790#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100791static void f_tan(typval_T *argvars, typval_T *rettv);
792static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200793#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_tolower(typval_T *argvars, typval_T *rettv);
795static void f_toupper(typval_T *argvars, typval_T *rettv);
796static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000797#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100798static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000799#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_type(typval_T *argvars, typval_T *rettv);
801static void f_undofile(typval_T *argvars, typval_T *rettv);
802static void f_undotree(typval_T *argvars, typval_T *rettv);
803static void f_uniq(typval_T *argvars, typval_T *rettv);
804static void f_values(typval_T *argvars, typval_T *rettv);
805static void f_virtcol(typval_T *argvars, typval_T *rettv);
806static void f_visualmode(typval_T *argvars, typval_T *rettv);
807static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
808static void f_winbufnr(typval_T *argvars, typval_T *rettv);
809static void f_wincol(typval_T *argvars, typval_T *rettv);
810static void f_winheight(typval_T *argvars, typval_T *rettv);
811static void f_winline(typval_T *argvars, typval_T *rettv);
812static void f_winnr(typval_T *argvars, typval_T *rettv);
813static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
814static void f_winrestview(typval_T *argvars, typval_T *rettv);
815static void f_winsaveview(typval_T *argvars, typval_T *rettv);
816static void f_winwidth(typval_T *argvars, typval_T *rettv);
817static void f_writefile(typval_T *argvars, typval_T *rettv);
818static void f_wordcount(typval_T *argvars, typval_T *rettv);
819static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000820
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
822static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
823static int get_env_len(char_u **arg);
824static int get_id_len(char_u **arg);
825static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
826static 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 +0000827#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
828#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
829 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100830static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
831static int eval_isnamec(int c);
832static int eval_isnamec1(int c);
833static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
834static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static typval_T *alloc_string_tv(char_u *string);
836static void init_tv(typval_T *varp);
837static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100838#ifdef FEAT_FLOAT
839static float_T get_tv_float(typval_T *varp);
840#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static linenr_T get_tv_lnum(typval_T *argvars);
842static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
843static char_u *get_tv_string(typval_T *varp);
844static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
845static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
846static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
847static hashtab_T *find_var_ht(char_u *name, char_u **varname);
848static funccall_T *get_funccal(void);
849static void vars_clear_ext(hashtab_T *ht, int free_val);
850static void delete_var(hashtab_T *ht, hashitem_T *hi);
851static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
852static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
853static void set_var(char_u *name, typval_T *varp, int copy);
854static int var_check_ro(int flags, char_u *name, int use_gettext);
855static int var_check_fixed(int flags, char_u *name, int use_gettext);
856static int var_check_func_name(char_u *name, int new_var);
857static int valid_varname(char_u *varname);
858static int tv_check_lock(int lock, char_u *name, int use_gettext);
859static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
860static char_u *find_option_end(char_u **arg, int *opt_flags);
861static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
862static int eval_fname_script(char_u *p);
863static int eval_fname_sid(char_u *p);
864static void list_func_head(ufunc_T *fp, int indent);
865static ufunc_T *find_func(char_u *name);
866static int function_exists(char_u *name);
867static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static void func_do_profile(ufunc_T *fp);
870static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
871static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000872static int
873# ifdef __BORLANDC__
874 _RTLENTRYF
875# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100876 prof_total_cmp(const void *s1, const void *s2);
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_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000882#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100883static int script_autoload(char_u *name, int reload);
884static char_u *autoload_name(char_u *name);
885static void cat_func_name(char_u *buf, ufunc_T *fp);
886static void func_free(ufunc_T *fp);
887static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
888static int can_free_funccal(funccall_T *fc, int copyID) ;
889static void free_funccal(funccall_T *fc, int free_val);
890static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
891static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
892static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
893static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
894static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
895static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
896static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
897static int write_list(FILE *fd, list_T *list, int binary);
898static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000899
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200900
901#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100902static int compare_func_name(const void *s1, const void *s2);
903static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200904#endif
905
Bram Moolenaar33570922005-01-25 22:26:29 +0000906/*
907 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000908 */
909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100910eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000911{
Bram Moolenaar33570922005-01-25 22:26:29 +0000912 int i;
913 struct vimvar *p;
914
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200915 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
916 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200917 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000918 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000919 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000920
921 for (i = 0; i < VV_LEN; ++i)
922 {
923 p = &vimvars[i];
924 STRCPY(p->vv_di.di_key, p->vv_name);
925 if (p->vv_flags & VV_RO)
926 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
927 else if (p->vv_flags & VV_RO_SBX)
928 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
929 else
930 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000931
932 /* add to v: scope dict, unless the value is not always available */
933 if (p->vv_type != VAR_UNKNOWN)
934 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000935 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000936 /* add to compat scope dict */
937 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100939 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
940
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000941 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100942 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200943 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100944 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100945
946 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
947 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
948 set_vim_var_nr(VV_NONE, VVAL_NONE);
949 set_vim_var_nr(VV_NULL, VVAL_NULL);
950
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200951 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200952
953#ifdef EBCDIC
954 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100955 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200956 */
957 sortFunctions();
958#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000959}
960
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000961#if defined(EXITFREE) || defined(PROTO)
962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100963eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000964{
965 int i;
966 struct vimvar *p;
967
968 for (i = 0; i < VV_LEN; ++i)
969 {
970 p = &vimvars[i];
971 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000972 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000973 vim_free(p->vv_str);
974 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000975 }
976 else if (p->vv_di.di_tv.v_type == VAR_LIST)
977 {
978 list_unref(p->vv_list);
979 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000980 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000981 }
982 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000983 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000984 hash_clear(&compat_hashtab);
985
Bram Moolenaard9fba312005-06-26 22:34:35 +0000986 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100987# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200988 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100989# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990
991 /* global variables */
992 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000993
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000994 /* autoloaded script names */
995 ga_clear_strings(&ga_loaded);
996
Bram Moolenaarcca74132013-09-25 21:00:28 +0200997 /* Script-local variables. First clear all the variables and in a second
998 * loop free the scriptvar_T, because a variable in one script might hold
999 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001000 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001001 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001002 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001003 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001004 ga_clear(&ga_scripts);
1005
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001006 /* unreferenced lists and dicts */
1007 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001008
1009 /* functions */
1010 free_all_functions();
1011 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001012}
1013#endif
1014
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 * Return the name of the executed function.
1017 */
1018 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001019func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022}
1023
1024/*
1025 * Return the address holding the next breakpoint line for a funccall cookie.
1026 */
1027 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001028func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar33570922005-01-25 22:26:29 +00001030 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/*
1034 * Return the address holding the debug tick for a funccall cookie.
1035 */
1036 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001037func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
Bram Moolenaar33570922005-01-25 22:26:29 +00001039 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042/*
1043 * Return the nesting level for a funccall cookie.
1044 */
1045 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001046func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar33570922005-01-25 22:26:29 +00001048 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050
1051/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001052funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001054/* pointer to list of previously used funccal, still around because some
1055 * item in it is still being used. */
1056funccall_T *previous_funccal = NULL;
1057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058/*
1059 * Return TRUE when a function was ended by a ":return" command.
1060 */
1061 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001062current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063{
1064 return current_funccal->returned;
1065}
1066
1067
1068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 * Set an internal variable to a string value. Creates the variable if it does
1070 * not already exist.
1071 */
1072 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001073set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001075 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
1078 val = vim_strsave(value);
1079 if (val != NULL)
1080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001081 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001082 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001084 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001085 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 }
1088}
1089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001091static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092static char_u *redir_endp = NULL;
1093static char_u *redir_varname = NULL;
1094
1095/*
1096 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001097 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 * Returns OK if successfully completed the setup. FAIL otherwise.
1099 */
1100 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001101var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102{
1103 int save_emsg;
1104 int err;
1105 typval_T tv;
1106
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001108 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 {
1110 EMSG(_(e_invarg));
1111 return FAIL;
1112 }
1113
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001114 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 redir_varname = vim_strsave(name);
1116 if (redir_varname == NULL)
1117 return FAIL;
1118
1119 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1120 if (redir_lval == NULL)
1121 {
1122 var_redir_stop();
1123 return FAIL;
1124 }
1125
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 /* The output is stored in growarray "redir_ga" until redirection ends. */
1127 ga_init2(&redir_ga, (int)sizeof(char), 500);
1128
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001130 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001131 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1133 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001134 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_endp != NULL && *redir_endp != NUL)
1136 /* Trailing characters are present after the variable name */
1137 EMSG(_(e_trailing));
1138 else
1139 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 var_redir_stop();
1142 return FAIL;
1143 }
1144
1145 /* check if we can write to the variable: set it to or append an empty
1146 * string */
1147 save_emsg = did_emsg;
1148 did_emsg = FALSE;
1149 tv.v_type = VAR_STRING;
1150 tv.vval.v_string = (char_u *)"";
1151 if (append)
1152 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1153 else
1154 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001155 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001157 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 if (err)
1159 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001160 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 var_redir_stop();
1162 return FAIL;
1163 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164
1165 return OK;
1166}
1167
1168/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001169 * Append "value[value_len]" to the variable set by var_redir_start().
1170 * The actual appending is postponed until redirection ends, because the value
1171 * appended may in fact be the string we write to, changing it may cause freed
1172 * memory to be used:
1173 * :redir => foo
1174 * :let foo
1175 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 */
1177 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001178var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001180 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181
1182 if (redir_lval == NULL)
1183 return;
1184
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001185 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001186 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001190 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001191 {
1192 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001194 }
1195 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197}
1198
1199/*
1200 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 */
1203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001204var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206 typval_T tv;
1207
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 if (redir_lval != NULL)
1209 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001210 /* If there was no error: assign the text to the variable. */
1211 if (redir_endp != NULL)
1212 {
1213 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1214 tv.v_type = VAR_STRING;
1215 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001216 /* Call get_lval() again, if it's inside a Dict or List it may
1217 * have changed. */
1218 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001219 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001220 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1221 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1222 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001223 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001224
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001225 /* free the collected output */
1226 vim_free(redir_ga.ga_data);
1227 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001228
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229 vim_free(redir_lval);
1230 redir_lval = NULL;
1231 }
1232 vim_free(redir_varname);
1233 redir_varname = NULL;
1234}
1235
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236# if defined(FEAT_MBYTE) || defined(PROTO)
1237 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001238eval_charconvert(
1239 char_u *enc_from,
1240 char_u *enc_to,
1241 char_u *fname_from,
1242 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243{
1244 int err = FALSE;
1245
1246 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1247 set_vim_var_string(VV_CC_TO, enc_to, -1);
1248 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1249 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1250 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1251 err = TRUE;
1252 set_vim_var_string(VV_CC_FROM, NULL, -1);
1253 set_vim_var_string(VV_CC_TO, NULL, -1);
1254 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1255 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1256
1257 if (err)
1258 return FAIL;
1259 return OK;
1260}
1261# endif
1262
1263# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1264 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001265eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266{
1267 int err = FALSE;
1268
1269 set_vim_var_string(VV_FNAME_IN, fname, -1);
1270 set_vim_var_string(VV_CMDARG, args, -1);
1271 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1272 err = TRUE;
1273 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1274 set_vim_var_string(VV_CMDARG, NULL, -1);
1275
1276 if (err)
1277 {
1278 mch_remove(fname);
1279 return FAIL;
1280 }
1281 return OK;
1282}
1283# endif
1284
1285# if defined(FEAT_DIFF) || defined(PROTO)
1286 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001287eval_diff(
1288 char_u *origfile,
1289 char_u *newfile,
1290 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 int err = FALSE;
1293
1294 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1295 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1296 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1297 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1298 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1299 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1300 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1301}
1302
1303 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001304eval_patch(
1305 char_u *origfile,
1306 char_u *difffile,
1307 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308{
1309 int err;
1310
1311 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1312 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1313 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1314 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1315 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1316 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1317 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1318}
1319# endif
1320
1321/*
1322 * Top level evaluation function, returning a boolean.
1323 * Sets "error" to TRUE if there was an error.
1324 * Return TRUE or FALSE.
1325 */
1326 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327eval_to_bool(
1328 char_u *arg,
1329 int *error,
1330 char_u **nextcmd,
1331 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 int retval = FALSE;
1335
1336 if (skip)
1337 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001338 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 else
1341 {
1342 *error = FALSE;
1343 if (!skip)
1344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001345 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348 }
1349 if (skip)
1350 --emsg_skip;
1351
1352 return retval;
1353}
1354
1355/*
1356 * Top level evaluation function, returning a string. If "skip" is TRUE,
1357 * only parsing to "nextcmd" is done, without reporting errors. Return
1358 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1359 */
1360 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001361eval_to_string_skip(
1362 char_u *arg,
1363 char_u **nextcmd,
1364 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
Bram Moolenaar33570922005-01-25 22:26:29 +00001366 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 char_u *retval;
1368
1369 if (skip)
1370 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 retval = NULL;
1373 else
1374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001375 retval = vim_strsave(get_tv_string(&tv));
1376 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378 if (skip)
1379 --emsg_skip;
1380
1381 return retval;
1382}
1383
1384/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001385 * Skip over an expression at "*pp".
1386 * Return FAIL for an error, OK otherwise.
1387 */
1388 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001389skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390{
Bram Moolenaar33570922005-01-25 22:26:29 +00001391 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001392
1393 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395}
1396
1397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001399 * When "convert" is TRUE convert a List into a sequence of lines and convert
1400 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 * Return pointer to allocated memory, or NULL for failure.
1402 */
1403 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001404eval_to_string(
1405 char_u *arg,
1406 char_u **nextcmd,
1407 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001411 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001412#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001413 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 retval = NULL;
1418 else
1419 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001420 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001421 {
1422 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001423 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001424 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001425 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001426 if (tv.vval.v_list->lv_len > 0)
1427 ga_append(&ga, NL);
1428 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001429 ga_append(&ga, NUL);
1430 retval = (char_u *)ga.ga_data;
1431 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001432#ifdef FEAT_FLOAT
1433 else if (convert && tv.v_type == VAR_FLOAT)
1434 {
1435 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1436 retval = vim_strsave(numbuf);
1437 }
1438#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001439 else
1440 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001441 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443
1444 return retval;
1445}
1446
1447/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001448 * Call eval_to_string() without using current local variables and using
1449 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 */
1451 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001452eval_to_string_safe(
1453 char_u *arg,
1454 char_u **nextcmd,
1455 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456{
1457 char_u *retval;
1458 void *save_funccalp;
1459
1460 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001461 if (use_sandbox)
1462 ++sandbox;
1463 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001464 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001465 if (use_sandbox)
1466 --sandbox;
1467 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 restore_funccal(save_funccalp);
1469 return retval;
1470}
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472/*
1473 * Top level evaluation function, returning a number.
1474 * Evaluates "expr" silently.
1475 * Returns -1 for an error.
1476 */
1477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001478eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479{
Bram Moolenaar33570922005-01-25 22:26:29 +00001480 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001482 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 ++emsg_off;
1485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001486 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 retval = -1;
1488 else
1489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001490 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
1493 --emsg_off;
1494
1495 return retval;
1496}
1497
Bram Moolenaara40058a2005-07-11 22:42:07 +00001498/*
1499 * Prepare v: variable "idx" to be used.
1500 * Save the current typeval in "save_tv".
1501 * When not used yet add the variable to the v: hashtable.
1502 */
1503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001505{
1506 *save_tv = vimvars[idx].vv_tv;
1507 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1508 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1509}
1510
1511/*
1512 * Restore v: variable "idx" to typeval "save_tv".
1513 * When no longer defined, remove the variable from the v: hashtable.
1514 */
1515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001516restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001517{
1518 hashitem_T *hi;
1519
Bram Moolenaara40058a2005-07-11 22:42:07 +00001520 vimvars[idx].vv_tv = *save_tv;
1521 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1522 {
1523 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1524 if (HASHITEM_EMPTY(hi))
1525 EMSG2(_(e_intern2), "restore_vimvar()");
1526 else
1527 hash_remove(&vimvarht, hi);
1528 }
1529}
1530
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001531#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001532/*
1533 * Evaluate an expression to a list with suggestions.
1534 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001535 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001536 */
1537 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001538eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001539{
1540 typval_T save_val;
1541 typval_T rettv;
1542 list_T *list = NULL;
1543 char_u *p = skipwhite(expr);
1544
1545 /* Set "v:val" to the bad word. */
1546 prepare_vimvar(VV_VAL, &save_val);
1547 vimvars[VV_VAL].vv_type = VAR_STRING;
1548 vimvars[VV_VAL].vv_str = badword;
1549 if (p_verbose == 0)
1550 ++emsg_off;
1551
1552 if (eval1(&p, &rettv, TRUE) == OK)
1553 {
1554 if (rettv.v_type != VAR_LIST)
1555 clear_tv(&rettv);
1556 else
1557 list = rettv.vval.v_list;
1558 }
1559
1560 if (p_verbose == 0)
1561 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001562 restore_vimvar(VV_VAL, &save_val);
1563
1564 return list;
1565}
1566
1567/*
1568 * "list" is supposed to contain two items: a word and a number. Return the
1569 * word in "pp" and the number as the return value.
1570 * Return -1 if anything isn't right.
1571 * Used to get the good word and score from the eval_spell_expr() result.
1572 */
1573 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001575{
1576 listitem_T *li;
1577
1578 li = list->lv_first;
1579 if (li == NULL)
1580 return -1;
1581 *pp = get_tv_string(&li->li_tv);
1582
1583 li = li->li_next;
1584 if (li == NULL)
1585 return -1;
1586 return get_tv_number(&li->li_tv);
1587}
1588#endif
1589
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001590/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001591 * Top level evaluation function.
1592 * Returns an allocated typval_T with the result.
1593 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001594 */
1595 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001597{
1598 typval_T *tv;
1599
1600 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001601 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602 {
1603 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001604 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605 }
1606
1607 return tv;
1608}
1609
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001612 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001613 * Uses argv[argc] for the function arguments. Only Number and String
1614 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001618call_vim_function(
1619 char_u *func,
1620 int argc,
1621 char_u **argv,
1622 int safe, /* use the sandbox */
1623 int str_arg_only, /* all arguments are strings */
1624 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
Bram Moolenaar33570922005-01-25 22:26:29 +00001626 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 long n;
1628 int len;
1629 int i;
1630 int doesrange;
1631 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001634 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001636 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
1638 for (i = 0; i < argc; i++)
1639 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001640 /* Pass a NULL or empty argument as an empty string */
1641 if (argv[i] == NULL || *argv[i] == NUL)
1642 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001643 argvars[i].v_type = VAR_STRING;
1644 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001645 continue;
1646 }
1647
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001648 if (str_arg_only)
1649 len = 0;
1650 else
1651 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001652 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (len != 0 && len == (int)STRLEN(argv[i]))
1654 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001655 argvars[i].v_type = VAR_NUMBER;
1656 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 }
1658 else
1659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001660 argvars[i].v_type = VAR_STRING;
1661 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 }
1664
1665 if (safe)
1666 {
1667 save_funccalp = save_funccal();
1668 ++sandbox;
1669 }
1670
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001671 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1672 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (safe)
1676 {
1677 --sandbox;
1678 restore_funccal(save_funccalp);
1679 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 vim_free(argvars);
1681
1682 if (ret == FAIL)
1683 clear_tv(rettv);
1684
1685 return ret;
1686}
1687
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001688/*
1689 * Call vimL function "func" and return the result as a number.
1690 * Returns -1 when calling the function fails.
1691 * Uses argv[argc] for the function arguments.
1692 */
1693 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001694call_func_retnr(
1695 char_u *func,
1696 int argc,
1697 char_u **argv,
1698 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001699{
1700 typval_T rettv;
1701 long retval;
1702
1703 /* All arguments are passed as strings, no conversion to number. */
1704 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1705 return -1;
1706
1707 retval = get_tv_number_chk(&rettv, NULL);
1708 clear_tv(&rettv);
1709 return retval;
1710}
1711
1712#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1713 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1714
Bram Moolenaar4f688582007-07-24 12:34:30 +00001715# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001716/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001717 * Call vimL function "func" and return the result as a string.
1718 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719 * Uses argv[argc] for the function arguments.
1720 */
1721 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001722call_func_retstr(
1723 char_u *func,
1724 int argc,
1725 char_u **argv,
1726 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727{
1728 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001729 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001731 /* All arguments are passed as strings, no conversion to number. */
1732 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733 return NULL;
1734
1735 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 return retval;
1738}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001739# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001741/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001742 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001744 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745 */
1746 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747call_func_retlist(
1748 char_u *func,
1749 int argc,
1750 char_u **argv,
1751 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001752{
1753 typval_T rettv;
1754
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001755 /* All arguments are passed as strings, no conversion to number. */
1756 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757 return NULL;
1758
1759 if (rettv.v_type != VAR_LIST)
1760 {
1761 clear_tv(&rettv);
1762 return NULL;
1763 }
1764
1765 return rettv.vval.v_list;
1766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#endif
1768
1769/*
1770 * Save the current function call pointer, and set it to NULL.
1771 * Used when executing autocommands and for ":source".
1772 */
1773 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001774save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001776 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 current_funccal = NULL;
1779 return (void *)fc;
1780}
1781
1782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001785 funccall_T *fc = (funccall_T *)vfc;
1786
1787 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788}
1789
Bram Moolenaar05159a02005-02-26 23:04:13 +00001790#if defined(FEAT_PROFILE) || defined(PROTO)
1791/*
1792 * Prepare profiling for entering a child or something else that is not
1793 * counted for the script/function itself.
1794 * Should always be called in pair with prof_child_exit().
1795 */
1796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001797prof_child_enter(
1798 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001799{
1800 funccall_T *fc = current_funccal;
1801
1802 if (fc != NULL && fc->func->uf_profiling)
1803 profile_start(&fc->prof_child);
1804 script_prof_save(tm);
1805}
1806
1807/*
1808 * Take care of time spent in a child.
1809 * Should always be called after prof_child_enter().
1810 */
1811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001812prof_child_exit(
1813 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001814{
1815 funccall_T *fc = current_funccal;
1816
1817 if (fc != NULL && fc->func->uf_profiling)
1818 {
1819 profile_end(&fc->prof_child);
1820 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1821 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1822 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1823 }
1824 script_prof_restore(tm);
1825}
1826#endif
1827
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829#ifdef FEAT_FOLDING
1830/*
1831 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1832 * it in "*cp". Doesn't give error messages.
1833 */
1834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001835eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836{
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 int retval;
1839 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001840 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1841 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
1843 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001844 if (use_sandbox)
1845 ++sandbox;
1846 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 retval = 0;
1850 else
1851 {
1852 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 if (tv.v_type == VAR_NUMBER)
1854 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001855 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 retval = 0;
1857 else
1858 {
1859 /* If the result is a string, check if there is a non-digit before
1860 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (!VIM_ISDIGIT(*s) && *s != '-')
1863 *cp = *s++;
1864 retval = atol((char *)s);
1865 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001869 if (use_sandbox)
1870 --sandbox;
1871 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873 return retval;
1874}
1875#endif
1876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 * ":let" list all variable values
1879 * ":let var1 var2" list variable values
1880 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001881 * ":let var += expr" assignment command.
1882 * ":let var -= expr" assignment command.
1883 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 */
1886 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001887ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888{
1889 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001891 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 int var_count = 0;
1894 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001895 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001896 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001897 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Bram Moolenaardb552d602006-03-23 22:59:57 +00001899 argend = skip_var_list(arg, &var_count, &semicolon);
1900 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001901 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001902 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1903 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 expr = skipwhite(argend);
1905 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1906 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001908 /*
1909 * ":let" without "=": list variables
1910 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 if (*arg == '[')
1912 EMSG(_(e_invarg));
1913 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001915 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001919 list_glob_vars(&first);
1920 list_buf_vars(&first);
1921 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001922#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001924#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 list_script_vars(&first);
1926 list_func_vars(&first);
1927 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 eap->nextcmd = check_nextcmd(arg);
1930 }
1931 else
1932 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001933 op[0] = '=';
1934 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001935 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001937 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1938 op[0] = *expr; /* +=, -= or .= */
1939 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001940 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001941 else
1942 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (eap->skip)
1945 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (eap->skip)
1948 {
1949 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 --emsg_skip;
1952 }
1953 else if (i != FAIL)
1954 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 }
1959 }
1960}
1961
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001962/*
1963 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1964 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 * When "nextchars" is not NULL it points to a string with characters that
1966 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1967 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 * Returns OK or FAIL;
1969 */
1970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001971ex_let_vars(
1972 char_u *arg_start,
1973 typval_T *tv,
1974 int copy, /* copy values from "tv", don't move */
1975 int semicolon, /* from skip_var_list() */
1976 int var_count, /* from skip_var_list() */
1977 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978{
1979 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001980 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001982 listitem_T *item;
1983 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984
1985 if (*arg != '[')
1986 {
1987 /*
1988 * ":let var = expr" or ":for var in list"
1989 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 return FAIL;
1992 return OK;
1993 }
1994
1995 /*
1996 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1997 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001998 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 {
2000 EMSG(_(e_listreq));
2001 return FAIL;
2002 }
2003
2004 i = list_len(l);
2005 if (semicolon == 0 && var_count < i)
2006 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002007 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 return FAIL;
2009 }
2010 if (var_count - semicolon > i)
2011 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002012 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 return FAIL;
2014 }
2015
2016 item = l->lv_first;
2017 while (*arg != ']')
2018 {
2019 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002020 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 item = item->li_next;
2022 if (arg == NULL)
2023 return FAIL;
2024
2025 arg = skipwhite(arg);
2026 if (*arg == ';')
2027 {
2028 /* Put the rest of the list (may be empty) in the var after ';'.
2029 * Create a new list for this. */
2030 l = list_alloc();
2031 if (l == NULL)
2032 return FAIL;
2033 while (item != NULL)
2034 {
2035 list_append_tv(l, &item->li_tv);
2036 item = item->li_next;
2037 }
2038
2039 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002040 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 ltv.vval.v_list = l;
2042 l->lv_refcount = 1;
2043
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002044 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2045 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 clear_tv(&ltv);
2047 if (arg == NULL)
2048 return FAIL;
2049 break;
2050 }
2051 else if (*arg != ',' && *arg != ']')
2052 {
2053 EMSG2(_(e_intern2), "ex_let_vars()");
2054 return FAIL;
2055 }
2056 }
2057
2058 return OK;
2059}
2060
2061/*
2062 * Skip over assignable variable "var" or list of variables "[var, var]".
2063 * Used for ":let varvar = expr" and ":for varvar in expr".
2064 * For "[var, var]" increment "*var_count" for each variable.
2065 * for "[var, var; var]" set "semicolon".
2066 * Return NULL for an error.
2067 */
2068 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002069skip_var_list(
2070 char_u *arg,
2071 int *var_count,
2072 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002073{
2074 char_u *p, *s;
2075
2076 if (*arg == '[')
2077 {
2078 /* "[var, var]": find the matching ']'. */
2079 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002080 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 {
2082 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2083 s = skip_var_one(p);
2084 if (s == p)
2085 {
2086 EMSG2(_(e_invarg2), p);
2087 return NULL;
2088 }
2089 ++*var_count;
2090
2091 p = skipwhite(s);
2092 if (*p == ']')
2093 break;
2094 else if (*p == ';')
2095 {
2096 if (*semicolon == 1)
2097 {
2098 EMSG(_("Double ; in list of variables"));
2099 return NULL;
2100 }
2101 *semicolon = 1;
2102 }
2103 else if (*p != ',')
2104 {
2105 EMSG2(_(e_invarg2), p);
2106 return NULL;
2107 }
2108 }
2109 return p + 1;
2110 }
2111 else
2112 return skip_var_one(arg);
2113}
2114
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002115/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002116 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002117 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002119 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002120skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002122 if (*arg == '@' && arg[1] != NUL)
2123 return arg + 2;
2124 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2125 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126}
2127
Bram Moolenaara7043832005-01-21 11:56:39 +00002128/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002129 * List variables for hashtab "ht" with prefix "prefix".
2130 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002131 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002133list_hashtable_vars(
2134 hashtab_T *ht,
2135 char_u *prefix,
2136 int empty,
2137 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002138{
Bram Moolenaar33570922005-01-25 22:26:29 +00002139 hashitem_T *hi;
2140 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141 int todo;
2142
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002143 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002144 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2145 {
2146 if (!HASHITEM_EMPTY(hi))
2147 {
2148 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 di = HI2DI(hi);
2150 if (empty || di->di_tv.v_type != VAR_STRING
2151 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002153 }
2154 }
2155}
2156
2157/*
2158 * List global variables.
2159 */
2160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002162{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002164}
2165
2166/*
2167 * List buffer variables.
2168 */
2169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002170list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002171{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002172 char_u numbuf[NUMBUFLEN];
2173
Bram Moolenaar429fa852013-04-15 12:27:36 +02002174 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002176
2177 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2179 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002180}
2181
2182/*
2183 * List window variables.
2184 */
2185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002187{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002188 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002190}
2191
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002192#ifdef FEAT_WINDOWS
2193/*
2194 * List tab page variables.
2195 */
2196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002197list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002198{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002199 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201}
2202#endif
2203
Bram Moolenaara7043832005-01-21 11:56:39 +00002204/*
2205 * List Vim variables.
2206 */
2207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002210 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211}
2212
2213/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002214 * List script-local variables, if there is a script.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002218{
2219 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2221 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002222}
2223
2224/*
2225 * List function variables, if there is a function.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002229{
2230 if (current_funccal != NULL)
2231 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002232 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002233}
2234
2235/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 * List variables in "arg".
2237 */
2238 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240{
2241 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 char_u *name_start;
2245 char_u *arg_subsc;
2246 char_u *tofree;
2247 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248
2249 while (!ends_excmd(*arg) && !got_int)
2250 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002253 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2255 {
2256 emsg_severe = TRUE;
2257 EMSG(_(e_trailing));
2258 break;
2259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 /* get_name_len() takes care of expanding curly braces */
2264 name_start = name = arg;
2265 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2266 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 /* This is mainly to keep test 49 working: when expanding
2269 * curly braces fails overrule the exception error message. */
2270 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 emsg_severe = TRUE;
2273 EMSG2(_(e_invarg2), arg);
2274 break;
2275 }
2276 error = TRUE;
2277 }
2278 else
2279 {
2280 if (tofree != NULL)
2281 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002282 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 else
2285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 /* handle d.key, l[idx], f(expr) */
2287 arg_subsc = arg;
2288 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002289 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002296 case 'g': list_glob_vars(first); break;
2297 case 'b': list_buf_vars(first); break;
2298 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002299#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002300 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002301#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 'v': list_vim_vars(first); break;
2303 case 's': list_script_vars(first); break;
2304 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 default:
2306 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002307 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002308 }
2309 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 {
2311 char_u numbuf[NUMBUFLEN];
2312 char_u *tf;
2313 int c;
2314 char_u *s;
2315
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002316 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 c = *arg;
2318 *arg = NUL;
2319 list_one_var_a((char_u *)"",
2320 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002321 tv.v_type,
2322 s == NULL ? (char_u *)"" : s,
2323 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002324 *arg = c;
2325 vim_free(tf);
2326 }
2327 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 }
2330 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002331
2332 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334
2335 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 }
2337
2338 return arg;
2339}
2340
2341/*
2342 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2343 * Returns a pointer to the char just after the var name.
2344 * Returns NULL if there is an error.
2345 */
2346 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002347ex_let_one(
2348 char_u *arg, /* points to variable name */
2349 typval_T *tv, /* value to assign to variable */
2350 int copy, /* copy value from "tv" */
2351 char_u *endchars, /* valid chars after variable name or NULL */
2352 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353{
2354 int c1;
2355 char_u *name;
2356 char_u *p;
2357 char_u *arg_end = NULL;
2358 int len;
2359 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002360 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361
2362 /*
2363 * ":let $VAR = expr": Set environment variable.
2364 */
2365 if (*arg == '$')
2366 {
2367 /* Find the end of the name. */
2368 ++arg;
2369 name = arg;
2370 len = get_env_len(&arg);
2371 if (len == 0)
2372 EMSG2(_(e_invarg2), name - 1);
2373 else
2374 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002375 if (op != NULL && (*op == '+' || *op == '-'))
2376 EMSG2(_(e_letwrong), op);
2377 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002380 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 {
2382 c1 = name[len];
2383 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002384 p = get_tv_string_chk(tv);
2385 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 {
2387 int mustfree = FALSE;
2388 char_u *s = vim_getenv(name, &mustfree);
2389
2390 if (s != NULL)
2391 {
2392 p = tofree = concat_str(s, p);
2393 if (mustfree)
2394 vim_free(s);
2395 }
2396 }
2397 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002398 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 if (STRICMP(name, "HOME") == 0)
2401 init_homedir();
2402 else if (didset_vim && STRICMP(name, "VIM") == 0)
2403 didset_vim = FALSE;
2404 else if (didset_vimruntime
2405 && STRICMP(name, "VIMRUNTIME") == 0)
2406 didset_vimruntime = FALSE;
2407 arg_end = arg;
2408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002411 }
2412 }
2413 }
2414
2415 /*
2416 * ":let &option = expr": Set option value.
2417 * ":let &l:option = expr": Set local option value.
2418 * ":let &g:option = expr": Set global option value.
2419 */
2420 else if (*arg == '&')
2421 {
2422 /* Find the end of the name. */
2423 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002424 if (p == NULL || (endchars != NULL
2425 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 EMSG(_(e_letunexp));
2427 else
2428 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 long n;
2430 int opt_type;
2431 long numval;
2432 char_u *stringval = NULL;
2433 char_u *s;
2434
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 c1 = *p;
2436 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437
2438 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 s = get_tv_string_chk(tv); /* != NULL if number or string */
2440 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 {
2442 opt_type = get_option_value(arg, &numval,
2443 &stringval, opt_flags);
2444 if ((opt_type == 1 && *op == '.')
2445 || (opt_type == 0 && *op != '.'))
2446 EMSG2(_(e_letwrong), op);
2447 else
2448 {
2449 if (opt_type == 1) /* number */
2450 {
2451 if (*op == '+')
2452 n = numval + n;
2453 else
2454 n = numval - n;
2455 }
2456 else if (opt_type == 0 && stringval != NULL) /* string */
2457 {
2458 s = concat_str(stringval, s);
2459 vim_free(stringval);
2460 stringval = s;
2461 }
2462 }
2463 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 if (s != NULL)
2465 {
2466 set_option_value(arg, n, s, opt_flags);
2467 arg_end = p;
2468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 }
2472 }
2473
2474 /*
2475 * ":let @r = expr": Set register contents.
2476 */
2477 else if (*arg == '@')
2478 {
2479 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 if (op != NULL && (*op == '+' || *op == '-'))
2481 EMSG2(_(e_letwrong), op);
2482 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002483 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 EMSG(_(e_letunexp));
2485 else
2486 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002487 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 char_u *s;
2489
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002490 p = get_tv_string_chk(tv);
2491 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002493 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 if (s != NULL)
2495 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 vim_free(s);
2498 }
2499 }
2500 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 arg_end = arg + 1;
2504 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002505 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 }
2507 }
2508
2509 /*
2510 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002513 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002515 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002517 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002518 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2521 EMSG(_(e_letunexp));
2522 else
2523 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002524 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 arg_end = p;
2526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529 }
2530
2531 else
2532 EMSG2(_(e_invarg2), arg);
2533
2534 return arg_end;
2535}
2536
2537/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002538 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2539 */
2540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002541check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542{
2543 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2544 {
2545 EMSG2(_(e_readonlyvar), arg);
2546 return TRUE;
2547 }
2548 return FALSE;
2549}
2550
2551/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * Get an lval: variable, Dict item or List item that can be assigned a value
2553 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2554 * "name.key", "name.key[expr]" etc.
2555 * Indexing only works if "name" is an existing List or Dictionary.
2556 * "name" points to the start of the name.
2557 * If "rettv" is not NULL it points to the value to be assigned.
2558 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2559 * wrong; must end in space or cmd separator.
2560 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002561 * flags:
2562 * GLV_QUIET: do not give error messages
2563 * GLV_NO_AUTOLOAD: do not use script autoloading
2564 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002566 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568 */
2569 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002570get_lval(
2571 char_u *name,
2572 typval_T *rettv,
2573 lval_T *lp,
2574 int unlet,
2575 int skip,
2576 int flags, /* GLV_ values */
2577 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 char_u *expr_start, *expr_end;
2581 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002582 dictitem_T *v;
2583 typval_T var1;
2584 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002586 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002588 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002590 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594
2595 if (skip)
2596 {
2597 /* When skipping just find the end of the name. */
2598 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002599 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 }
2601
2602 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002603 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (expr_start != NULL)
2605 {
2606 /* Don't expand the name when we already know there is an error. */
2607 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2608 && *p != '[' && *p != '.')
2609 {
2610 EMSG(_(e_trailing));
2611 return NULL;
2612 }
2613
2614 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2615 if (lp->ll_exp_name == NULL)
2616 {
2617 /* Report an invalid expression in braces, unless the
2618 * expression evaluation has been cancelled due to an
2619 * aborting error, an interrupt, or an exception. */
2620 if (!aborting() && !quiet)
2621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002622 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 EMSG2(_(e_invarg2), name);
2624 return NULL;
2625 }
2626 }
2627 lp->ll_name = lp->ll_exp_name;
2628 }
2629 else
2630 lp->ll_name = name;
2631
2632 /* Without [idx] or .key we are done. */
2633 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2634 return p;
2635
2636 cc = *p;
2637 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002638 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 if (v == NULL && !quiet)
2640 EMSG2(_(e_undefvar), lp->ll_name);
2641 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002642 if (v == NULL)
2643 return NULL;
2644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 /*
2646 * Loop until no more [idx] or .key is following.
2647 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002648 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2652 && !(lp->ll_tv->v_type == VAR_DICT
2653 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
2656 EMSG(_("E689: Can only index a List or Dictionary"));
2657 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_("E708: [:] must come last"));
2663 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 len = -1;
2667 if (*p == '.')
2668 {
2669 key = p + 1;
2670 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2671 ;
2672 if (len == 0)
2673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (!quiet)
2675 EMSG(_(e_emptykey));
2676 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 }
2678 p = key + len;
2679 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002680 else
2681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 if (*p == ':')
2685 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 else
2687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 empty1 = FALSE;
2689 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002691 if (get_tv_string_chk(&var1) == NULL)
2692 {
2693 /* not a number or string */
2694 clear_tv(&var1);
2695 return NULL;
2696 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698
2699 /* Optionally get the second index [ :expr]. */
2700 if (*p == ':')
2701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002705 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (rettv != NULL && (rettv->v_type != VAR_LIST
2711 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
2714 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (!empty1)
2716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 p = skipwhite(p + 1);
2720 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 else
2723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2726 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002731 if (get_tv_string_chk(&var2) == NULL)
2732 {
2733 /* not a number or string */
2734 if (!empty1)
2735 clear_tv(&var1);
2736 clear_tv(&var2);
2737 return NULL;
2738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 if (!empty1)
2750 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 }
2755
2756 /* Skip to past ']'. */
2757 ++p;
2758 }
2759
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 {
2762 if (len == -1)
2763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002765 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (*key == NUL)
2767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (!quiet)
2769 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
2773 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002774 lp->ll_list = NULL;
2775 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002776 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002777
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002778 /* When assigning to a scope dictionary check that a function and
2779 * variable name is valid (only variable name unless it is l: or
2780 * g: dictionary). Disallow overwriting a builtin function. */
2781 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 int prevval;
2784 int wrong;
2785
2786 if (len != -1)
2787 {
2788 prevval = key[len];
2789 key[len] = NUL;
2790 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002791 else
2792 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002793 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2794 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 || !valid_varname(key);
2797 if (len != -1)
2798 key[len] = prevval;
2799 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 return NULL;
2801 }
2802
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002805 /* Can't add "v:" variable. */
2806 if (lp->ll_dict == &vimvardict)
2807 {
2808 EMSG2(_(e_illvar), name);
2809 return NULL;
2810 }
2811
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002812 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002816 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 if (len == -1)
2818 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 }
2821 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 if (len == -1)
2826 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 p = NULL;
2829 break;
2830 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002831 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002832 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 return NULL;
2834
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (len == -1)
2836 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 }
2839 else
2840 {
2841 /*
2842 * Get the number and item for the only or first index of the List.
2843 */
2844 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 else
2847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002848 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 clear_tv(&var1);
2850 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002851 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 lp->ll_list = lp->ll_tv->vval.v_list;
2853 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2854 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002856 if (lp->ll_n1 < 0)
2857 {
2858 lp->ll_n1 = 0;
2859 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2860 }
2861 }
2862 if (lp->ll_li == NULL)
2863 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002866 if (!quiet)
2867 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 }
2870
2871 /*
2872 * May need to find the item or absolute index for the second
2873 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 * When no index given: "lp->ll_empty2" is TRUE.
2875 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002879 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002885 {
2886 if (!quiet)
2887 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 }
2892
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2894 if (lp->ll_n1 < 0)
2895 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2896 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002897 {
2898 if (!quiet)
2899 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002901 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002902 }
2903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002906 }
2907
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 return p;
2909}
2910
2911/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002912 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 */
2914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002915clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916{
2917 vim_free(lp->ll_exp_name);
2918 vim_free(lp->ll_newkey);
2919}
2920
2921/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002922 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 */
2926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002927set_var_lval(
2928 lval_T *lp,
2929 char_u *endp,
2930 typval_T *rettv,
2931 int copy,
2932 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933{
2934 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002935 listitem_T *ri;
2936 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937
2938 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 cc = *endp;
2943 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 if (op != NULL && *op != '=')
2945 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002948 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002949 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002950 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002951 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002953 if ((di == NULL
2954 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2955 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2956 FALSE)))
2957 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 set_var(lp->ll_name, &tv, FALSE);
2959 clear_tv(&tv);
2960 }
2961 }
2962 else
2963 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002965 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002967 else if (tv_check_lock(lp->ll_newkey == NULL
2968 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002969 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002970 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 else if (lp->ll_range)
2972 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002973 listitem_T *ll_li = lp->ll_li;
2974 int ll_n1 = lp->ll_n1;
2975
2976 /*
2977 * Check whether any of the list items is locked
2978 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002979 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002980 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002981 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002982 return;
2983 ri = ri->li_next;
2984 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2985 break;
2986 ll_li = ll_li->li_next;
2987 ++ll_n1;
2988 }
2989
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990 /*
2991 * Assign the List values to the list items.
2992 */
2993 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002994 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002995 if (op != NULL && *op != '=')
2996 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2997 else
2998 {
2999 clear_tv(&lp->ll_li->li_tv);
3000 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3001 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 ri = ri->li_next;
3003 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3004 break;
3005 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003006 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003008 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 ri = NULL;
3011 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 lp->ll_li = lp->ll_li->li_next;
3015 ++lp->ll_n1;
3016 }
3017 if (ri != NULL)
3018 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003019 else if (lp->ll_empty2
3020 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 : lp->ll_n1 != lp->ll_n2)
3022 EMSG(_("E711: List value has not enough items"));
3023 }
3024 else
3025 {
3026 /*
3027 * Assign to a List or Dictionary item.
3028 */
3029 if (lp->ll_newkey != NULL)
3030 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003031 if (op != NULL && *op != '=')
3032 {
3033 EMSG2(_(e_letwrong), op);
3034 return;
3035 }
3036
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003038 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 if (di == NULL)
3040 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003041 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3042 {
3043 vim_free(di);
3044 return;
3045 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 else if (op != NULL && *op != '=')
3049 {
3050 tv_op(lp->ll_tv, rettv, op);
3051 return;
3052 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003053 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003055
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 /*
3057 * Assign the value to the variable or list item.
3058 */
3059 if (copy)
3060 copy_tv(rettv, lp->ll_tv);
3061 else
3062 {
3063 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003064 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003065 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 }
3067 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003068}
3069
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003070/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3072 * Returns OK or FAIL.
3073 */
3074 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003075tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076{
3077 long n;
3078 char_u numbuf[NUMBUFLEN];
3079 char_u *s;
3080
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003081 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3082 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3083 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 {
3085 switch (tv1->v_type)
3086 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003087 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088 case VAR_DICT:
3089 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003090 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003091 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003092 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 break;
3094
3095 case VAR_LIST:
3096 if (*op != '+' || tv2->v_type != VAR_LIST)
3097 break;
3098 /* List += List */
3099 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3100 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3101 return OK;
3102
3103 case VAR_NUMBER:
3104 case VAR_STRING:
3105 if (tv2->v_type == VAR_LIST)
3106 break;
3107 if (*op == '+' || *op == '-')
3108 {
3109 /* nr += nr or nr -= nr*/
3110 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111#ifdef FEAT_FLOAT
3112 if (tv2->v_type == VAR_FLOAT)
3113 {
3114 float_T f = n;
3115
3116 if (*op == '+')
3117 f += tv2->vval.v_float;
3118 else
3119 f -= tv2->vval.v_float;
3120 clear_tv(tv1);
3121 tv1->v_type = VAR_FLOAT;
3122 tv1->vval.v_float = f;
3123 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003125#endif
3126 {
3127 if (*op == '+')
3128 n += get_tv_number(tv2);
3129 else
3130 n -= get_tv_number(tv2);
3131 clear_tv(tv1);
3132 tv1->v_type = VAR_NUMBER;
3133 tv1->vval.v_number = n;
3134 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 }
3136 else
3137 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138 if (tv2->v_type == VAR_FLOAT)
3139 break;
3140
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 /* str .= str */
3142 s = get_tv_string(tv1);
3143 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3144 clear_tv(tv1);
3145 tv1->v_type = VAR_STRING;
3146 tv1->vval.v_string = s;
3147 }
3148 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149
3150#ifdef FEAT_FLOAT
3151 case VAR_FLOAT:
3152 {
3153 float_T f;
3154
3155 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3156 && tv2->v_type != VAR_NUMBER
3157 && tv2->v_type != VAR_STRING))
3158 break;
3159 if (tv2->v_type == VAR_FLOAT)
3160 f = tv2->vval.v_float;
3161 else
3162 f = get_tv_number(tv2);
3163 if (*op == '+')
3164 tv1->vval.v_float += f;
3165 else
3166 tv1->vval.v_float -= f;
3167 }
3168 return OK;
3169#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003170 }
3171 }
3172
3173 EMSG2(_(e_letwrong), op);
3174 return FAIL;
3175}
3176
3177/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 * Add a watcher to a list.
3179 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003181list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182{
3183 lw->lw_next = l->lv_watch;
3184 l->lv_watch = lw;
3185}
3186
3187/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003188 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 * No warning when it isn't found...
3190 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003192list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195
3196 lwp = &l->lv_watch;
3197 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3198 {
3199 if (lw == lwrem)
3200 {
3201 *lwp = lw->lw_next;
3202 break;
3203 }
3204 lwp = &lw->lw_next;
3205 }
3206}
3207
3208/*
3209 * Just before removing an item from a list: advance watchers to the next
3210 * item.
3211 */
3212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003213list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214{
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216
3217 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3218 if (lw->lw_item == item)
3219 lw->lw_item = item->li_next;
3220}
3221
3222/*
3223 * Evaluate the expression used in a ":for var in expr" command.
3224 * "arg" points to "var".
3225 * Set "*errp" to TRUE for an error, FALSE otherwise;
3226 * Return a pointer that holds the info. Null when there is an error.
3227 */
3228 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003229eval_for_line(
3230 char_u *arg,
3231 int *errp,
3232 char_u **nextcmdp,
3233 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234{
Bram Moolenaar33570922005-01-25 22:26:29 +00003235 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 typval_T tv;
3238 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239
3240 *errp = TRUE; /* default: there is an error */
3241
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 if (fi == NULL)
3244 return NULL;
3245
3246 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3247 if (expr == NULL)
3248 return fi;
3249
3250 expr = skipwhite(expr);
3251 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3252 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003253 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 return fi;
3255 }
3256
3257 if (skip)
3258 ++emsg_skip;
3259 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3260 {
3261 *errp = FALSE;
3262 if (!skip)
3263 {
3264 l = tv.vval.v_list;
3265 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003268 clear_tv(&tv);
3269 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 else
3271 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003272 /* No need to increment the refcount, it's already set for the
3273 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 fi->fi_list = l;
3275 list_add_watch(l, &fi->fi_lw);
3276 fi->fi_lw.lw_item = l->lv_first;
3277 }
3278 }
3279 }
3280 if (skip)
3281 --emsg_skip;
3282
3283 return fi;
3284}
3285
3286/*
3287 * Use the first item in a ":for" list. Advance to the next.
3288 * Assign the values to the variable (list). "arg" points to the first one.
3289 * Return TRUE when a valid item was found, FALSE when at end of list or
3290 * something wrong.
3291 */
3292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003295 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003296 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298
3299 item = fi->fi_lw.lw_item;
3300 if (item == NULL)
3301 result = FALSE;
3302 else
3303 {
3304 fi->fi_lw.lw_item = item->li_next;
3305 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3306 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3307 }
3308 return result;
3309}
3310
3311/*
3312 * Free the structure used to store info used by ":for".
3313 */
3314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003315free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003316{
Bram Moolenaar33570922005-01-25 22:26:29 +00003317 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003319 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 list_unref(fi->fi_list);
3323 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 vim_free(fi);
3325}
3326
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3328
3329 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003330set_context_for_expression(
3331 expand_T *xp,
3332 char_u *arg,
3333 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 int got_eq = FALSE;
3336 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339 if (cmdidx == CMD_let)
3340 {
3341 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003342 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 {
3344 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003348 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 if (vim_iswhite(*p))
3350 break;
3351 }
3352 return;
3353 }
3354 }
3355 else
3356 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3357 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 while ((xp->xp_pattern = vim_strpbrk(arg,
3359 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3360 {
3361 c = *xp->xp_pattern;
3362 if (c == '&')
3363 {
3364 c = xp->xp_pattern[1];
3365 if (c == '&')
3366 {
3367 ++xp->xp_pattern;
3368 xp->xp_context = cmdidx != CMD_let || got_eq
3369 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3370 }
3371 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003374 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3375 xp->xp_pattern += 2;
3376
3377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 }
3379 else if (c == '$')
3380 {
3381 /* environment variable */
3382 xp->xp_context = EXPAND_ENV_VARS;
3383 }
3384 else if (c == '=')
3385 {
3386 got_eq = TRUE;
3387 xp->xp_context = EXPAND_EXPRESSION;
3388 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003389 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 && xp->xp_context == EXPAND_FUNCTIONS
3391 && vim_strchr(xp->xp_pattern, '(') == NULL)
3392 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003393 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 break;
3395 }
3396 else if (cmdidx != CMD_let || got_eq)
3397 {
3398 if (c == '"') /* string */
3399 {
3400 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3401 if (c == '\\' && xp->xp_pattern[1] != NUL)
3402 ++xp->xp_pattern;
3403 xp->xp_context = EXPAND_NOTHING;
3404 }
3405 else if (c == '\'') /* literal string */
3406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003407 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3409 /* skip */ ;
3410 xp->xp_context = EXPAND_NOTHING;
3411 }
3412 else if (c == '|')
3413 {
3414 if (xp->xp_pattern[1] == '|')
3415 {
3416 ++xp->xp_pattern;
3417 xp->xp_context = EXPAND_EXPRESSION;
3418 }
3419 else
3420 xp->xp_context = EXPAND_COMMANDS;
3421 }
3422 else
3423 xp->xp_context = EXPAND_EXPRESSION;
3424 }
3425 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003426 /* Doesn't look like something valid, expand as an expression
3427 * anyway. */
3428 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 arg = xp->xp_pattern;
3430 if (*arg != NUL)
3431 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3432 /* skip */ ;
3433 }
3434 xp->xp_pattern = arg;
3435}
3436
3437#endif /* FEAT_CMDL_COMPL */
3438
3439/*
3440 * ":1,25call func(arg1, arg2)" function call.
3441 */
3442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003443ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
3445 char_u *arg = eap->arg;
3446 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003448 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 linenr_T lnum;
3452 int doesrange;
3453 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003454 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003456 if (eap->skip)
3457 {
3458 /* trans_function_name() doesn't work well when skipping, use eval0()
3459 * instead to skip to any following command, e.g. for:
3460 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003461 ++emsg_skip;
3462 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3463 clear_tv(&rettv);
3464 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003465 return;
3466 }
3467
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003468 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003469 if (fudi.fd_newkey != NULL)
3470 {
3471 /* Still need to give an error message for missing key. */
3472 EMSG2(_(e_dictkey), fudi.fd_newkey);
3473 vim_free(fudi.fd_newkey);
3474 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003475 if (tofree == NULL)
3476 return;
3477
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003478 /* Increase refcount on dictionary, it could get deleted when evaluating
3479 * the arguments. */
3480 if (fudi.fd_dict != NULL)
3481 ++fudi.fd_dict->dv_refcount;
3482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003483 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003484 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003485 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar532c7802005-01-27 14:44:31 +00003487 /* Skip white space to allow ":call func ()". Not good, but required for
3488 * backward compatibility. */
3489 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003490 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 if (*startarg != '(')
3493 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003494 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 goto end;
3496 }
3497
3498 /*
3499 * When skipping, evaluate the function once, to find the end of the
3500 * arguments.
3501 * When the function takes a range, this is discovered after the first
3502 * call, and the loop is broken.
3503 */
3504 if (eap->skip)
3505 {
3506 ++emsg_skip;
3507 lnum = eap->line2; /* do it once, also with an invalid range */
3508 }
3509 else
3510 lnum = eap->line1;
3511 for ( ; lnum <= eap->line2; ++lnum)
3512 {
3513 if (!eap->skip && eap->addr_count > 0)
3514 {
3515 curwin->w_cursor.lnum = lnum;
3516 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003517#ifdef FEAT_VIRTUALEDIT
3518 curwin->w_cursor.coladd = 0;
3519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 }
3521 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003522 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003523 eap->line1, eap->line2, &doesrange,
3524 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 failed = TRUE;
3527 break;
3528 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003529
3530 /* Handle a function returning a Funcref, Dictionary or List. */
3531 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3532 {
3533 failed = TRUE;
3534 break;
3535 }
3536
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (doesrange || eap->skip)
3539 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003542 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003544 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (aborting())
3546 break;
3547 }
3548 if (eap->skip)
3549 --emsg_skip;
3550
3551 if (!failed)
3552 {
3553 /* Check for trailing illegal characters and a following command. */
3554 if (!ends_excmd(*arg))
3555 {
3556 emsg_severe = TRUE;
3557 EMSG(_(e_trailing));
3558 }
3559 else
3560 eap->nextcmd = check_nextcmd(arg);
3561 }
3562
3563end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003564 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003565 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566}
3567
3568/*
3569 * ":unlet[!] var1 ... " command.
3570 */
3571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003572ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003574 ex_unletlock(eap, eap->arg, 0);
3575}
3576
3577/*
3578 * ":lockvar" and ":unlockvar" commands
3579 */
3580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003581ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 int deep = 2;
3585
3586 if (eap->forceit)
3587 deep = -1;
3588 else if (vim_isdigit(*arg))
3589 {
3590 deep = getdigits(&arg);
3591 arg = skipwhite(arg);
3592 }
3593
3594 ex_unletlock(eap, arg, deep);
3595}
3596
3597/*
3598 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3599 */
3600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601ex_unletlock(
3602 exarg_T *eap,
3603 char_u *argstart,
3604 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605{
3606 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
3611 do
3612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003614 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003615 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 if (lv.ll_name == NULL)
3617 error = TRUE; /* error but continue parsing */
3618 if (name_end == NULL || (!vim_iswhite(*name_end)
3619 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (name_end != NULL)
3622 {
3623 emsg_severe = TRUE;
3624 EMSG(_(e_trailing));
3625 }
3626 if (!(eap->skip || error))
3627 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 break;
3629 }
3630
3631 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003632 {
3633 if (eap->cmdidx == CMD_unlet)
3634 {
3635 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3636 error = TRUE;
3637 }
3638 else
3639 {
3640 if (do_lock_var(&lv, name_end, deep,
3641 eap->cmdidx == CMD_lockvar) == FAIL)
3642 error = TRUE;
3643 }
3644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 if (!eap->skip)
3647 clear_lval(&lv);
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 arg = skipwhite(name_end);
3650 } while (!ends_excmd(*arg));
3651
3652 eap->nextcmd = check_nextcmd(arg);
3653}
3654
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003656do_unlet_var(
3657 lval_T *lp,
3658 char_u *name_end,
3659 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 int ret = OK;
3662 int cc;
3663
3664 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 cc = *name_end;
3667 *name_end = NUL;
3668
3669 /* Normal name or expanded name. */
3670 if (check_changedtick(lp->ll_name))
3671 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003673 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003675 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003676 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003677 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003678 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003679 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003680 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003681 else if (lp->ll_range)
3682 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003683 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003684 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003685 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003686
3687 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3688 {
3689 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003690 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003691 return FAIL;
3692 ll_li = li;
3693 ++ll_n1;
3694 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695
3696 /* Delete a range of List items. */
3697 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3698 {
3699 li = lp->ll_li->li_next;
3700 listitem_remove(lp->ll_list, lp->ll_li);
3701 lp->ll_li = li;
3702 ++lp->ll_n1;
3703 }
3704 }
3705 else
3706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 /* unlet a List item. */
3709 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003712 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 }
3714
3715 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003716}
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718/*
3719 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003720 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 */
3722 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003723do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
Bram Moolenaar33570922005-01-25 22:26:29 +00003725 hashtab_T *ht;
3726 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003727 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003728 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003729 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 ht = find_var_ht(name, &varname);
3732 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003734 if (ht == &globvarht)
3735 d = &globvardict;
3736 else if (current_funccal != NULL
3737 && ht == &current_funccal->l_vars.dv_hashtab)
3738 d = &current_funccal->l_vars;
3739 else if (ht == &compat_hashtab)
3740 d = &vimvardict;
3741 else
3742 {
3743 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3744 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3745 }
3746 if (d == NULL)
3747 {
3748 EMSG2(_(e_intern2), "do_unlet()");
3749 return FAIL;
3750 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003751 hi = hash_find(ht, varname);
3752 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003753 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003754 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003755 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003756 || var_check_ro(di->di_flags, name, FALSE)
3757 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003758 return FAIL;
3759
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760 delete_var(ht, hi);
3761 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003764 if (forceit)
3765 return OK;
3766 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 return FAIL;
3768}
3769
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003770/*
3771 * Lock or unlock variable indicated by "lp".
3772 * "deep" is the levels to go (-1 for unlimited);
3773 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3774 */
3775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003776do_lock_var(
3777 lval_T *lp,
3778 char_u *name_end,
3779 int deep,
3780 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003781{
3782 int ret = OK;
3783 int cc;
3784 dictitem_T *di;
3785
3786 if (deep == 0) /* nothing to do */
3787 return OK;
3788
3789 if (lp->ll_tv == NULL)
3790 {
3791 cc = *name_end;
3792 *name_end = NUL;
3793
3794 /* Normal name or expanded name. */
3795 if (check_changedtick(lp->ll_name))
3796 ret = FAIL;
3797 else
3798 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003799 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800 if (di == NULL)
3801 ret = FAIL;
3802 else
3803 {
3804 if (lock)
3805 di->di_flags |= DI_FLAGS_LOCK;
3806 else
3807 di->di_flags &= ~DI_FLAGS_LOCK;
3808 item_lock(&di->di_tv, deep, lock);
3809 }
3810 }
3811 *name_end = cc;
3812 }
3813 else if (lp->ll_range)
3814 {
3815 listitem_T *li = lp->ll_li;
3816
3817 /* (un)lock a range of List items. */
3818 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3819 {
3820 item_lock(&li->li_tv, deep, lock);
3821 li = li->li_next;
3822 ++lp->ll_n1;
3823 }
3824 }
3825 else if (lp->ll_list != NULL)
3826 /* (un)lock a List item. */
3827 item_lock(&lp->ll_li->li_tv, deep, lock);
3828 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003829 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003830 item_lock(&lp->ll_di->di_tv, deep, lock);
3831
3832 return ret;
3833}
3834
3835/*
3836 * Lock or unlock an item. "deep" is nr of levels to go.
3837 */
3838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003839item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003840{
3841 static int recurse = 0;
3842 list_T *l;
3843 listitem_T *li;
3844 dict_T *d;
3845 hashitem_T *hi;
3846 int todo;
3847
3848 if (recurse >= DICT_MAXNEST)
3849 {
3850 EMSG(_("E743: variable nested too deep for (un)lock"));
3851 return;
3852 }
3853 if (deep == 0)
3854 return;
3855 ++recurse;
3856
3857 /* lock/unlock the item itself */
3858 if (lock)
3859 tv->v_lock |= VAR_LOCKED;
3860 else
3861 tv->v_lock &= ~VAR_LOCKED;
3862
3863 switch (tv->v_type)
3864 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003865 case VAR_UNKNOWN:
3866 case VAR_NUMBER:
3867 case VAR_STRING:
3868 case VAR_FUNC:
3869 case VAR_FLOAT:
3870 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003871 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003872 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003873 break;
3874
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003875 case VAR_LIST:
3876 if ((l = tv->vval.v_list) != NULL)
3877 {
3878 if (lock)
3879 l->lv_lock |= VAR_LOCKED;
3880 else
3881 l->lv_lock &= ~VAR_LOCKED;
3882 if (deep < 0 || deep > 1)
3883 /* recursive: lock/unlock the items the List contains */
3884 for (li = l->lv_first; li != NULL; li = li->li_next)
3885 item_lock(&li->li_tv, deep - 1, lock);
3886 }
3887 break;
3888 case VAR_DICT:
3889 if ((d = tv->vval.v_dict) != NULL)
3890 {
3891 if (lock)
3892 d->dv_lock |= VAR_LOCKED;
3893 else
3894 d->dv_lock &= ~VAR_LOCKED;
3895 if (deep < 0 || deep > 1)
3896 {
3897 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003898 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003899 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3900 {
3901 if (!HASHITEM_EMPTY(hi))
3902 {
3903 --todo;
3904 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3905 }
3906 }
3907 }
3908 }
3909 }
3910 --recurse;
3911}
3912
Bram Moolenaara40058a2005-07-11 22:42:07 +00003913/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003914 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3915 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003916 */
3917 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003918tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003919{
3920 return (tv->v_lock & VAR_LOCKED)
3921 || (tv->v_type == VAR_LIST
3922 && tv->vval.v_list != NULL
3923 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3924 || (tv->v_type == VAR_DICT
3925 && tv->vval.v_dict != NULL
3926 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3927}
3928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3930/*
3931 * Delete all "menutrans_" variables.
3932 */
3933 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003934del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
Bram Moolenaar33570922005-01-25 22:26:29 +00003936 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003937 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003940 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 {
3943 if (!HASHITEM_EMPTY(hi))
3944 {
3945 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3947 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 }
3949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951}
3952#endif
3953
3954#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3955
3956/*
3957 * Local string buffer for the next two functions to store a variable name
3958 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3959 * get_user_var_name().
3960 */
3961
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003962static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964static char_u *varnamebuf = NULL;
3965static int varnamebuflen = 0;
3966
3967/*
3968 * Function to concatenate a prefix and a variable name.
3969 */
3970 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003971cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972{
3973 int len;
3974
3975 len = (int)STRLEN(name) + 3;
3976 if (len > varnamebuflen)
3977 {
3978 vim_free(varnamebuf);
3979 len += 10; /* some additional space */
3980 varnamebuf = alloc(len);
3981 if (varnamebuf == NULL)
3982 {
3983 varnamebuflen = 0;
3984 return NULL;
3985 }
3986 varnamebuflen = len;
3987 }
3988 *varnamebuf = prefix;
3989 varnamebuf[1] = ':';
3990 STRCPY(varnamebuf + 2, name);
3991 return varnamebuf;
3992}
3993
3994/*
3995 * Function given to ExpandGeneric() to obtain the list of user defined
3996 * (global/buffer/window/built-in) variable names.
3997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003999get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004001 static long_u gdone;
4002 static long_u bdone;
4003 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004004#ifdef FEAT_WINDOWS
4005 static long_u tdone;
4006#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static int vidx;
4008 static hashitem_T *hi;
4009 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004012 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004013 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004014#ifdef FEAT_WINDOWS
4015 tdone = 0;
4016#endif
4017 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004018
4019 /* Global variables */
4020 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004022 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004023 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004024 else
4025 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004026 while (HASHITEM_EMPTY(hi))
4027 ++hi;
4028 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4029 return cat_prefix_varname('g', hi->hi_key);
4030 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004032
4033 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004034 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004035 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004039 else
4040 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004041 while (HASHITEM_EMPTY(hi))
4042 ++hi;
4043 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004045 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 return (char_u *)"b:changedtick";
4049 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004050
4051 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004052 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004055 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004057 else
4058 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004059 while (HASHITEM_EMPTY(hi))
4060 ++hi;
4061 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004063
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004064#ifdef FEAT_WINDOWS
4065 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004066 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 if (tdone < ht->ht_used)
4068 {
4069 if (tdone++ == 0)
4070 hi = ht->ht_array;
4071 else
4072 ++hi;
4073 while (HASHITEM_EMPTY(hi))
4074 ++hi;
4075 return cat_prefix_varname('t', hi->hi_key);
4076 }
4077#endif
4078
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 /* v: variables */
4080 if (vidx < VV_LEN)
4081 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083 vim_free(varnamebuf);
4084 varnamebuf = NULL;
4085 varnamebuflen = 0;
4086 return NULL;
4087}
4088
4089#endif /* FEAT_CMDL_COMPL */
4090
4091/*
4092 * types for expressions.
4093 */
4094typedef enum
4095{
4096 TYPE_UNKNOWN = 0
4097 , TYPE_EQUAL /* == */
4098 , TYPE_NEQUAL /* != */
4099 , TYPE_GREATER /* > */
4100 , TYPE_GEQUAL /* >= */
4101 , TYPE_SMALLER /* < */
4102 , TYPE_SEQUAL /* <= */
4103 , TYPE_MATCH /* =~ */
4104 , TYPE_NOMATCH /* !~ */
4105} exptype_T;
4106
4107/*
4108 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004109 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4111 */
4112
4113/*
4114 * Handle zero level expression.
4115 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004116 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004117 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 * Return OK or FAIL.
4119 */
4120 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004121eval0(
4122 char_u *arg,
4123 typval_T *rettv,
4124 char_u **nextcmd,
4125 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126{
4127 int ret;
4128 char_u *p;
4129
4130 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if (ret == FAIL || !ends_excmd(*p))
4133 {
4134 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004135 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 /*
4137 * Report the invalid expression unless the expression evaluation has
4138 * been cancelled due to an aborting error, an interrupt, or an
4139 * exception.
4140 */
4141 if (!aborting())
4142 EMSG2(_(e_invexpr2), arg);
4143 ret = FAIL;
4144 }
4145 if (nextcmd != NULL)
4146 *nextcmd = check_nextcmd(p);
4147
4148 return ret;
4149}
4150
4151/*
4152 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004153 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 *
4155 * "arg" must point to the first non-white of the expression.
4156 * "arg" is advanced to the next non-white after the recognized expression.
4157 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004158 * Note: "rettv.v_lock" is not set.
4159 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 * Return OK or FAIL.
4161 */
4162 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004163eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
4165 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004166 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
4168 /*
4169 * Get the first variable.
4170 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 return FAIL;
4173
4174 if ((*arg)[0] == '?')
4175 {
4176 result = FALSE;
4177 if (evaluate)
4178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 int error = FALSE;
4180
4181 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004183 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 if (error)
4185 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187
4188 /*
4189 * Get the second variable.
4190 */
4191 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 return FAIL;
4194
4195 /*
4196 * Check for the ":".
4197 */
4198 if ((*arg)[0] != ':')
4199 {
4200 EMSG(_("E109: Missing ':' after '?'"));
4201 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 return FAIL;
4204 }
4205
4206 /*
4207 * Get the third variable.
4208 */
4209 *arg = skipwhite(*arg + 1);
4210 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4211 {
4212 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return FAIL;
4215 }
4216 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219
4220 return OK;
4221}
4222
4223/*
4224 * Handle first level expression:
4225 * expr2 || expr2 || expr2 logical OR
4226 *
4227 * "arg" must point to the first non-white of the expression.
4228 * "arg" is advanced to the next non-white after the recognized expression.
4229 *
4230 * Return OK or FAIL.
4231 */
4232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004233eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
Bram Moolenaar33570922005-01-25 22:26:29 +00004235 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 long result;
4237 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
4240 /*
4241 * Get the first variable.
4242 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 return FAIL;
4245
4246 /*
4247 * Repeat until there is no following "||".
4248 */
4249 first = TRUE;
4250 result = FALSE;
4251 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4252 {
4253 if (evaluate && first)
4254 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (error)
4259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 first = FALSE;
4261 }
4262
4263 /*
4264 * Get the second variable.
4265 */
4266 *arg = skipwhite(*arg + 2);
4267 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4268 return FAIL;
4269
4270 /*
4271 * Compute the result.
4272 */
4273 if (evaluate && !result)
4274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 if (error)
4279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 if (evaluate)
4282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 rettv->v_type = VAR_NUMBER;
4284 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287
4288 return OK;
4289}
4290
4291/*
4292 * Handle second level expression:
4293 * expr3 && expr3 && expr3 logical AND
4294 *
4295 * "arg" must point to the first non-white of the expression.
4296 * "arg" is advanced to the next non-white after the recognized expression.
4297 *
4298 * Return OK or FAIL.
4299 */
4300 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004301eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302{
Bram Moolenaar33570922005-01-25 22:26:29 +00004303 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 long result;
4305 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004306 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 /*
4309 * Get the first variable.
4310 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004311 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 return FAIL;
4313
4314 /*
4315 * Repeat until there is no following "&&".
4316 */
4317 first = TRUE;
4318 result = TRUE;
4319 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4320 {
4321 if (evaluate && first)
4322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004325 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 if (error)
4327 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 first = FALSE;
4329 }
4330
4331 /*
4332 * Get the second variable.
4333 */
4334 *arg = skipwhite(*arg + 2);
4335 if (eval4(arg, &var2, evaluate && result) == FAIL)
4336 return FAIL;
4337
4338 /*
4339 * Compute the result.
4340 */
4341 if (evaluate && result)
4342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (error)
4347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 if (evaluate)
4350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004351 rettv->v_type = VAR_NUMBER;
4352 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 }
4355
4356 return OK;
4357}
4358
4359/*
4360 * Handle third level expression:
4361 * var1 == var2
4362 * var1 =~ var2
4363 * var1 != var2
4364 * var1 !~ var2
4365 * var1 > var2
4366 * var1 >= var2
4367 * var1 < var2
4368 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004369 * var1 is var2
4370 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 *
4372 * "arg" must point to the first non-white of the expression.
4373 * "arg" is advanced to the next non-white after the recognized expression.
4374 *
4375 * Return OK or FAIL.
4376 */
4377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004378eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379{
Bram Moolenaar33570922005-01-25 22:26:29 +00004380 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u *p;
4382 int i;
4383 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 int len = 2;
4386 long n1, n2;
4387 char_u *s1, *s2;
4388 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4389 regmatch_T regmatch;
4390 int ic;
4391 char_u *save_cpo;
4392
4393 /*
4394 * Get the first variable.
4395 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
4398
4399 p = *arg;
4400 switch (p[0])
4401 {
4402 case '=': if (p[1] == '=')
4403 type = TYPE_EQUAL;
4404 else if (p[1] == '~')
4405 type = TYPE_MATCH;
4406 break;
4407 case '!': if (p[1] == '=')
4408 type = TYPE_NEQUAL;
4409 else if (p[1] == '~')
4410 type = TYPE_NOMATCH;
4411 break;
4412 case '>': if (p[1] != '=')
4413 {
4414 type = TYPE_GREATER;
4415 len = 1;
4416 }
4417 else
4418 type = TYPE_GEQUAL;
4419 break;
4420 case '<': if (p[1] != '=')
4421 {
4422 type = TYPE_SMALLER;
4423 len = 1;
4424 }
4425 else
4426 type = TYPE_SEQUAL;
4427 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004428 case 'i': if (p[1] == 's')
4429 {
4430 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4431 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004432 i = p[len];
4433 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 {
4435 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4436 type_is = TRUE;
4437 }
4438 }
4439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441
4442 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004443 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 */
4445 if (type != TYPE_UNKNOWN)
4446 {
4447 /* extra question mark appended: ignore case */
4448 if (p[len] == '?')
4449 {
4450 ic = TRUE;
4451 ++len;
4452 }
4453 /* extra '#' appended: match case */
4454 else if (p[len] == '#')
4455 {
4456 ic = FALSE;
4457 ++len;
4458 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004459 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 else
4461 ic = p_ic;
4462
4463 /*
4464 * Get the second variable.
4465 */
4466 *arg = skipwhite(p + len);
4467 if (eval5(arg, &var2, evaluate) == FAIL)
4468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004469 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 return FAIL;
4471 }
4472
4473 if (evaluate)
4474 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004475 if (type_is && rettv->v_type != var2.v_type)
4476 {
4477 /* For "is" a different type always means FALSE, for "notis"
4478 * it means TRUE. */
4479 n1 = (type == TYPE_NEQUAL);
4480 }
4481 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4482 {
4483 if (type_is)
4484 {
4485 n1 = (rettv->v_type == var2.v_type
4486 && rettv->vval.v_list == var2.vval.v_list);
4487 if (type == TYPE_NEQUAL)
4488 n1 = !n1;
4489 }
4490 else if (rettv->v_type != var2.v_type
4491 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4492 {
4493 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004494 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004496 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 clear_tv(rettv);
4498 clear_tv(&var2);
4499 return FAIL;
4500 }
4501 else
4502 {
4503 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004504 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4505 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 if (type == TYPE_NEQUAL)
4507 n1 = !n1;
4508 }
4509 }
4510
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004511 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4512 {
4513 if (type_is)
4514 {
4515 n1 = (rettv->v_type == var2.v_type
4516 && rettv->vval.v_dict == var2.vval.v_dict);
4517 if (type == TYPE_NEQUAL)
4518 n1 = !n1;
4519 }
4520 else if (rettv->v_type != var2.v_type
4521 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4522 {
4523 if (rettv->v_type != var2.v_type)
4524 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4525 else
4526 EMSG(_("E736: Invalid operation for Dictionary"));
4527 clear_tv(rettv);
4528 clear_tv(&var2);
4529 return FAIL;
4530 }
4531 else
4532 {
4533 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004534 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4535 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004536 if (type == TYPE_NEQUAL)
4537 n1 = !n1;
4538 }
4539 }
4540
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004541 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4542 {
4543 if (rettv->v_type != var2.v_type
4544 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4545 {
4546 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004547 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004549 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 clear_tv(rettv);
4551 clear_tv(&var2);
4552 return FAIL;
4553 }
4554 else
4555 {
4556 /* Compare two Funcrefs for being equal or unequal. */
4557 if (rettv->vval.v_string == NULL
4558 || var2.vval.v_string == NULL)
4559 n1 = FALSE;
4560 else
4561 n1 = STRCMP(rettv->vval.v_string,
4562 var2.vval.v_string) == 0;
4563 if (type == TYPE_NEQUAL)
4564 n1 = !n1;
4565 }
4566 }
4567
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004568#ifdef FEAT_FLOAT
4569 /*
4570 * If one of the two variables is a float, compare as a float.
4571 * When using "=~" or "!~", always compare as string.
4572 */
4573 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4574 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4575 {
4576 float_T f1, f2;
4577
4578 if (rettv->v_type == VAR_FLOAT)
4579 f1 = rettv->vval.v_float;
4580 else
4581 f1 = get_tv_number(rettv);
4582 if (var2.v_type == VAR_FLOAT)
4583 f2 = var2.vval.v_float;
4584 else
4585 f2 = get_tv_number(&var2);
4586 n1 = FALSE;
4587 switch (type)
4588 {
4589 case TYPE_EQUAL: n1 = (f1 == f2); break;
4590 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4591 case TYPE_GREATER: n1 = (f1 > f2); break;
4592 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4593 case TYPE_SMALLER: n1 = (f1 < f2); break;
4594 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4595 case TYPE_UNKNOWN:
4596 case TYPE_MATCH:
4597 case TYPE_NOMATCH: break; /* avoid gcc warning */
4598 }
4599 }
4600#endif
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 /*
4603 * If one of the two variables is a number, compare as a number.
4604 * When using "=~" or "!~", always compare as string.
4605 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004606 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 n1 = get_tv_number(rettv);
4610 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 switch (type)
4612 {
4613 case TYPE_EQUAL: n1 = (n1 == n2); break;
4614 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4615 case TYPE_GREATER: n1 = (n1 > n2); break;
4616 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4617 case TYPE_SMALLER: n1 = (n1 < n2); break;
4618 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4619 case TYPE_UNKNOWN:
4620 case TYPE_MATCH:
4621 case TYPE_NOMATCH: break; /* avoid gcc warning */
4622 }
4623 }
4624 else
4625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004626 s1 = get_tv_string_buf(rettv, buf1);
4627 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4629 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4630 else
4631 i = 0;
4632 n1 = FALSE;
4633 switch (type)
4634 {
4635 case TYPE_EQUAL: n1 = (i == 0); break;
4636 case TYPE_NEQUAL: n1 = (i != 0); break;
4637 case TYPE_GREATER: n1 = (i > 0); break;
4638 case TYPE_GEQUAL: n1 = (i >= 0); break;
4639 case TYPE_SMALLER: n1 = (i < 0); break;
4640 case TYPE_SEQUAL: n1 = (i <= 0); break;
4641
4642 case TYPE_MATCH:
4643 case TYPE_NOMATCH:
4644 /* avoid 'l' flag in 'cpoptions' */
4645 save_cpo = p_cpo;
4646 p_cpo = (char_u *)"";
4647 regmatch.regprog = vim_regcomp(s2,
4648 RE_MAGIC + RE_STRING);
4649 regmatch.rm_ic = ic;
4650 if (regmatch.regprog != NULL)
4651 {
4652 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004653 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (type == TYPE_NOMATCH)
4655 n1 = !n1;
4656 }
4657 p_cpo = save_cpo;
4658 break;
4659
4660 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4661 }
4662 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004663 clear_tv(rettv);
4664 clear_tv(&var2);
4665 rettv->v_type = VAR_NUMBER;
4666 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
4668 }
4669
4670 return OK;
4671}
4672
4673/*
4674 * Handle fourth level expression:
4675 * + number addition
4676 * - number subtraction
4677 * . string concatenation
4678 *
4679 * "arg" must point to the first non-white of the expression.
4680 * "arg" is advanced to the next non-white after the recognized expression.
4681 *
4682 * Return OK or FAIL.
4683 */
4684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004685eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
Bram Moolenaar33570922005-01-25 22:26:29 +00004687 typval_T var2;
4688 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 int op;
4690 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004691#ifdef FEAT_FLOAT
4692 float_T f1 = 0, f2 = 0;
4693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 char_u *s1, *s2;
4695 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4696 char_u *p;
4697
4698 /*
4699 * Get the first variable.
4700 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004701 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 return FAIL;
4703
4704 /*
4705 * Repeat computing, until no '+', '-' or '.' is following.
4706 */
4707 for (;;)
4708 {
4709 op = **arg;
4710 if (op != '+' && op != '-' && op != '.')
4711 break;
4712
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713 if ((op != '+' || rettv->v_type != VAR_LIST)
4714#ifdef FEAT_FLOAT
4715 && (op == '.' || rettv->v_type != VAR_FLOAT)
4716#endif
4717 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 {
4719 /* For "list + ...", an illegal use of the first operand as
4720 * a number cannot be determined before evaluating the 2nd
4721 * operand: if this is also a list, all is ok.
4722 * For "something . ...", "something - ..." or "non-list + ...",
4723 * we know that the first operand needs to be a string or number
4724 * without evaluating the 2nd operand. So check before to avoid
4725 * side effects after an error. */
4726 if (evaluate && get_tv_string_chk(rettv) == NULL)
4727 {
4728 clear_tv(rettv);
4729 return FAIL;
4730 }
4731 }
4732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 /*
4734 * Get the second variable.
4735 */
4736 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004737 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004739 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 return FAIL;
4741 }
4742
4743 if (evaluate)
4744 {
4745 /*
4746 * Compute the result.
4747 */
4748 if (op == '.')
4749 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004750 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4751 s2 = get_tv_string_buf_chk(&var2, buf2);
4752 if (s2 == NULL) /* type error ? */
4753 {
4754 clear_tv(rettv);
4755 clear_tv(&var2);
4756 return FAIL;
4757 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004758 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004759 clear_tv(rettv);
4760 rettv->v_type = VAR_STRING;
4761 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004763 else if (op == '+' && rettv->v_type == VAR_LIST
4764 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004765 {
4766 /* concatenate Lists */
4767 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4768 &var3) == FAIL)
4769 {
4770 clear_tv(rettv);
4771 clear_tv(&var2);
4772 return FAIL;
4773 }
4774 clear_tv(rettv);
4775 *rettv = var3;
4776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 else
4778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004779 int error = FALSE;
4780
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004781#ifdef FEAT_FLOAT
4782 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004783 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004784 f1 = rettv->vval.v_float;
4785 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 else
4788#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790 n1 = get_tv_number_chk(rettv, &error);
4791 if (error)
4792 {
4793 /* This can only happen for "list + non-list". For
4794 * "non-list + ..." or "something - ...", we returned
4795 * before evaluating the 2nd operand. */
4796 clear_tv(rettv);
4797 return FAIL;
4798 }
4799#ifdef FEAT_FLOAT
4800 if (var2.v_type == VAR_FLOAT)
4801 f1 = n1;
4802#endif
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 {
4807 f2 = var2.vval.v_float;
4808 n2 = 0;
4809 }
4810 else
4811#endif
4812 {
4813 n2 = get_tv_number_chk(&var2, &error);
4814 if (error)
4815 {
4816 clear_tv(rettv);
4817 clear_tv(&var2);
4818 return FAIL;
4819 }
4820#ifdef FEAT_FLOAT
4821 if (rettv->v_type == VAR_FLOAT)
4822 f2 = n2;
4823#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004825 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826
4827#ifdef FEAT_FLOAT
4828 /* If there is a float on either side the result is a float. */
4829 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4830 {
4831 if (op == '+')
4832 f1 = f1 + f2;
4833 else
4834 f1 = f1 - f2;
4835 rettv->v_type = VAR_FLOAT;
4836 rettv->vval.v_float = f1;
4837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839#endif
4840 {
4841 if (op == '+')
4842 n1 = n1 + n2;
4843 else
4844 n1 = n1 - n2;
4845 rettv->v_type = VAR_NUMBER;
4846 rettv->vval.v_number = n1;
4847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 }
4851 }
4852 return OK;
4853}
4854
4855/*
4856 * Handle fifth level expression:
4857 * * number multiplication
4858 * / number division
4859 * % number modulo
4860 *
4861 * "arg" must point to the first non-white of the expression.
4862 * "arg" is advanced to the next non-white after the recognized expression.
4863 *
4864 * Return OK or FAIL.
4865 */
4866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004867eval6(
4868 char_u **arg,
4869 typval_T *rettv,
4870 int evaluate,
4871 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872{
Bram Moolenaar33570922005-01-25 22:26:29 +00004873 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 int op;
4875 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876#ifdef FEAT_FLOAT
4877 int use_float = FALSE;
4878 float_T f1 = 0, f2;
4879#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 /*
4883 * Get the first variable.
4884 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004885 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return FAIL;
4887
4888 /*
4889 * Repeat computing, until no '*', '/' or '%' is following.
4890 */
4891 for (;;)
4892 {
4893 op = **arg;
4894 if (op != '*' && op != '/' && op != '%')
4895 break;
4896
4897 if (evaluate)
4898 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899#ifdef FEAT_FLOAT
4900 if (rettv->v_type == VAR_FLOAT)
4901 {
4902 f1 = rettv->vval.v_float;
4903 use_float = TRUE;
4904 n1 = 0;
4905 }
4906 else
4907#endif
4908 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004909 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004910 if (error)
4911 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 else
4914 n1 = 0;
4915
4916 /*
4917 * Get the second variable.
4918 */
4919 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004920 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 return FAIL;
4922
4923 if (evaluate)
4924 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004925#ifdef FEAT_FLOAT
4926 if (var2.v_type == VAR_FLOAT)
4927 {
4928 if (!use_float)
4929 {
4930 f1 = n1;
4931 use_float = TRUE;
4932 }
4933 f2 = var2.vval.v_float;
4934 n2 = 0;
4935 }
4936 else
4937#endif
4938 {
4939 n2 = get_tv_number_chk(&var2, &error);
4940 clear_tv(&var2);
4941 if (error)
4942 return FAIL;
4943#ifdef FEAT_FLOAT
4944 if (use_float)
4945 f2 = n2;
4946#endif
4947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
4949 /*
4950 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004951 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953#ifdef FEAT_FLOAT
4954 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 if (op == '*')
4957 f1 = f1 * f2;
4958 else if (op == '/')
4959 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004960# ifdef VMS
4961 /* VMS crashes on divide by zero, work around it */
4962 if (f2 == 0.0)
4963 {
4964 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004965 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004967 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004968 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004969 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004970 }
4971 else
4972 f1 = f1 / f2;
4973# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974 /* We rely on the floating point library to handle divide
4975 * by zero to result in "inf" and not a crash. */
4976 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004977# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004981 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004982 return FAIL;
4983 }
4984 rettv->v_type = VAR_FLOAT;
4985 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990 if (op == '*')
4991 n1 = n1 * n2;
4992 else if (op == '/')
4993 {
4994 if (n2 == 0) /* give an error message? */
4995 {
4996 if (n1 == 0)
4997 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4998 else if (n1 < 0)
4999 n1 = -0x7fffffffL;
5000 else
5001 n1 = 0x7fffffffL;
5002 }
5003 else
5004 n1 = n1 / n2;
5005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005007 {
5008 if (n2 == 0) /* give an error message? */
5009 n1 = 0;
5010 else
5011 n1 = n1 % n2;
5012 }
5013 rettv->v_type = VAR_NUMBER;
5014 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 }
5018
5019 return OK;
5020}
5021
5022/*
5023 * Handle sixth level expression:
5024 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005025 * "string" string constant
5026 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 * &option-name option value
5028 * @r register contents
5029 * identifier variable value
5030 * function() function call
5031 * $VAR environment variable
5032 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005033 * [expr, expr] List
5034 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 *
5036 * Also handle:
5037 * ! in front logical NOT
5038 * - in front unary minus
5039 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 * trailing [] subscript in String or List
5041 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 *
5043 * "arg" must point to the first non-white of the expression.
5044 * "arg" is advanced to the next non-white after the recognized expression.
5045 *
5046 * Return OK or FAIL.
5047 */
5048 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005049eval7(
5050 char_u **arg,
5051 typval_T *rettv,
5052 int evaluate,
5053 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 long n;
5056 int len;
5057 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 char_u *start_leader, *end_leader;
5059 int ret = OK;
5060 char_u *alias;
5061
5062 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005063 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005064 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 /*
5069 * Skip '!' and '-' characters. They are handled later.
5070 */
5071 start_leader = *arg;
5072 while (**arg == '!' || **arg == '-' || **arg == '+')
5073 *arg = skipwhite(*arg + 1);
5074 end_leader = *arg;
5075
5076 switch (**arg)
5077 {
5078 /*
5079 * Number constant.
5080 */
5081 case '0':
5082 case '1':
5083 case '2':
5084 case '3':
5085 case '4':
5086 case '5':
5087 case '6':
5088 case '7':
5089 case '8':
5090 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005091 {
5092#ifdef FEAT_FLOAT
5093 char_u *p = skipdigits(*arg + 1);
5094 int get_float = FALSE;
5095
5096 /* We accept a float when the format matches
5097 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005098 * strict to avoid backwards compatibility problems.
5099 * Don't look for a float after the "." operator, so that
5100 * ":let vers = 1.2.3" doesn't fail. */
5101 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005103 get_float = TRUE;
5104 p = skipdigits(p + 2);
5105 if (*p == 'e' || *p == 'E')
5106 {
5107 ++p;
5108 if (*p == '-' || *p == '+')
5109 ++p;
5110 if (!vim_isdigit(*p))
5111 get_float = FALSE;
5112 else
5113 p = skipdigits(p + 1);
5114 }
5115 if (ASCII_ISALPHA(*p) || *p == '.')
5116 get_float = FALSE;
5117 }
5118 if (get_float)
5119 {
5120 float_T f;
5121
5122 *arg += string2float(*arg, &f);
5123 if (evaluate)
5124 {
5125 rettv->v_type = VAR_FLOAT;
5126 rettv->vval.v_float = f;
5127 }
5128 }
5129 else
5130#endif
5131 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005132 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005133 *arg += len;
5134 if (evaluate)
5135 {
5136 rettv->v_type = VAR_NUMBER;
5137 rettv->vval.v_number = n;
5138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143 /*
5144 * String constant: "string".
5145 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005146 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 break;
5148
5149 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005153 break;
5154
5155 /*
5156 * List: [expr, expr]
5157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005158 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 break;
5160
5161 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005162 * Dictionary: {key: val, key: val}
5163 */
5164 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5165 break;
5166
5167 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005168 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005170 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 break;
5172
5173 /*
5174 * Environment variable: $VAR.
5175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178
5179 /*
5180 * Register contents: @r.
5181 */
5182 case '@': ++*arg;
5183 if (evaluate)
5184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005186 rettv->vval.v_string = get_reg_contents(**arg,
5187 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189 if (**arg != NUL)
5190 ++*arg;
5191 break;
5192
5193 /*
5194 * nested expression: (expression).
5195 */
5196 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005197 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (**arg == ')')
5199 ++*arg;
5200 else if (ret == OK)
5201 {
5202 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005203 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 ret = FAIL;
5205 }
5206 break;
5207
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 break;
5210 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211
5212 if (ret == NOTDONE)
5213 {
5214 /*
5215 * Must be a variable or function name.
5216 * Can also be a curly-braces kind of name: {expr}.
5217 */
5218 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005219 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 if (alias != NULL)
5221 s = alias;
5222
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005223 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224 ret = FAIL;
5225 else
5226 {
5227 if (**arg == '(') /* recursive! */
5228 {
5229 /* If "s" is the name of a variable of type VAR_FUNC
5230 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005231 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232
5233 /* Invoke the function. */
5234 ret = get_func_tv(s, len, rettv, arg,
5235 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005236 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005237
5238 /* If evaluate is FALSE rettv->v_type was not set in
5239 * get_func_tv, but it's needed in handle_subscript() to parse
5240 * what follows. So set it here. */
5241 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5242 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005243 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005244 rettv->v_type = VAR_FUNC;
5245 }
5246
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 /* Stop the expression evaluation when immediately
5248 * aborting on error, or when an interrupt occurred or
5249 * an exception was thrown but not caught. */
5250 if (aborting())
5251 {
5252 if (ret == OK)
5253 clear_tv(rettv);
5254 ret = FAIL;
5255 }
5256 }
5257 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005258 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005259 else
5260 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005262 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 }
5264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 *arg = skipwhite(*arg);
5266
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005267 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5268 * expr(expr). */
5269 if (ret == OK)
5270 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
5272 /*
5273 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5274 */
5275 if (ret == OK && evaluate && end_leader > start_leader)
5276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005278 int val = 0;
5279#ifdef FEAT_FLOAT
5280 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005281
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005282 if (rettv->v_type == VAR_FLOAT)
5283 f = rettv->vval.v_float;
5284 else
5285#endif
5286 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 clear_tv(rettv);
5290 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 else
5293 {
5294 while (end_leader > start_leader)
5295 {
5296 --end_leader;
5297 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005298 {
5299#ifdef FEAT_FLOAT
5300 if (rettv->v_type == VAR_FLOAT)
5301 f = !f;
5302 else
5303#endif
5304 val = !val;
5305 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005307 {
5308#ifdef FEAT_FLOAT
5309 if (rettv->v_type == VAR_FLOAT)
5310 f = -f;
5311 else
5312#endif
5313 val = -val;
5314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005315 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005316#ifdef FEAT_FLOAT
5317 if (rettv->v_type == VAR_FLOAT)
5318 {
5319 clear_tv(rettv);
5320 rettv->vval.v_float = f;
5321 }
5322 else
5323#endif
5324 {
5325 clear_tv(rettv);
5326 rettv->v_type = VAR_NUMBER;
5327 rettv->vval.v_number = val;
5328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331
5332 return ret;
5333}
5334
5335/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005336 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5337 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005338 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5339 */
5340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005341eval_index(
5342 char_u **arg,
5343 typval_T *rettv,
5344 int evaluate,
5345 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346{
5347 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005348 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 long len = -1;
5351 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354
Bram Moolenaara03f2332016-02-06 18:09:59 +01005355 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005357 case VAR_FUNC:
5358 if (verbose)
5359 EMSG(_("E695: Cannot index a Funcref"));
5360 return FAIL;
5361 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005362#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 if (verbose)
5364 EMSG(_(e_float_as_string));
5365 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005366#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005367 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005368 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005369 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005370 if (verbose)
5371 EMSG(_("E909: Cannot index a special variable"));
5372 return FAIL;
5373 case VAR_UNKNOWN:
5374 if (evaluate)
5375 return FAIL;
5376 /* FALLTHROUGH */
5377
5378 case VAR_STRING:
5379 case VAR_NUMBER:
5380 case VAR_LIST:
5381 case VAR_DICT:
5382 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005383 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005385 init_tv(&var1);
5386 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389 /*
5390 * dict.name
5391 */
5392 key = *arg + 1;
5393 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5394 ;
5395 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005397 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005398 }
5399 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 /*
5402 * something[idx]
5403 *
5404 * Get the (first) variable from inside the [].
5405 */
5406 *arg = skipwhite(*arg + 1);
5407 if (**arg == ':')
5408 empty1 = TRUE;
5409 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5410 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005411 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5412 {
5413 /* not a number or string */
5414 clear_tv(&var1);
5415 return FAIL;
5416 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417
5418 /*
5419 * Get the second variable from inside the [:].
5420 */
5421 if (**arg == ':')
5422 {
5423 range = TRUE;
5424 *arg = skipwhite(*arg + 1);
5425 if (**arg == ']')
5426 empty2 = TRUE;
5427 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005429 if (!empty1)
5430 clear_tv(&var1);
5431 return FAIL;
5432 }
5433 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5434 {
5435 /* not a number or string */
5436 if (!empty1)
5437 clear_tv(&var1);
5438 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439 return FAIL;
5440 }
5441 }
5442
5443 /* Check for the ']'. */
5444 if (**arg != ']')
5445 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005446 if (verbose)
5447 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 clear_tv(&var1);
5449 if (range)
5450 clear_tv(&var2);
5451 return FAIL;
5452 }
5453 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 }
5455
5456 if (evaluate)
5457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 n1 = 0;
5459 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 n1 = get_tv_number(&var1);
5462 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 }
5464 if (range)
5465 {
5466 if (empty2)
5467 n2 = -1;
5468 else
5469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005470 n2 = get_tv_number(&var2);
5471 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 }
5473 }
5474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005477 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005478 case VAR_FUNC:
5479 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005480 case VAR_SPECIAL:
5481 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005482 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005483 break; /* not evaluating, skipping over subscript */
5484
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 case VAR_NUMBER:
5486 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 len = (long)STRLEN(s);
5489 if (range)
5490 {
5491 /* The resulting variable is a substring. If the indexes
5492 * are out of range the result is empty. */
5493 if (n1 < 0)
5494 {
5495 n1 = len + n1;
5496 if (n1 < 0)
5497 n1 = 0;
5498 }
5499 if (n2 < 0)
5500 n2 = len + n2;
5501 else if (n2 >= len)
5502 n2 = len;
5503 if (n1 >= len || n2 < 0 || n1 > n2)
5504 s = NULL;
5505 else
5506 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5507 }
5508 else
5509 {
5510 /* The resulting variable is a string of a single
5511 * character. If the index is too big or negative the
5512 * result is empty. */
5513 if (n1 >= len || n1 < 0)
5514 s = NULL;
5515 else
5516 s = vim_strnsave(s + n1, 1);
5517 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518 clear_tv(rettv);
5519 rettv->v_type = VAR_STRING;
5520 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 break;
5522
5523 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 if (n1 < 0)
5526 n1 = len + n1;
5527 if (!empty1 && (n1 < 0 || n1 >= len))
5528 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005529 /* For a range we allow invalid values and return an empty
5530 * list. A list index out of range is an error. */
5531 if (!range)
5532 {
5533 if (verbose)
5534 EMSGN(_(e_listidx), n1);
5535 return FAIL;
5536 }
5537 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 }
5539 if (range)
5540 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005541 list_T *l;
5542 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543
5544 if (n2 < 0)
5545 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005546 else if (n2 >= len)
5547 n2 = len - 1;
5548 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005549 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 l = list_alloc();
5551 if (l == NULL)
5552 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 n1 <= n2; ++n1)
5555 {
5556 if (list_append_tv(l, &item->li_tv) == FAIL)
5557 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005558 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 return FAIL;
5560 }
5561 item = item->li_next;
5562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 clear_tv(rettv);
5564 rettv->v_type = VAR_LIST;
5565 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005566 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 }
5568 else
5569 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005570 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 clear_tv(rettv);
5572 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 }
5574 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005575
5576 case VAR_DICT:
5577 if (range)
5578 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005579 if (verbose)
5580 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581 if (len == -1)
5582 clear_tv(&var1);
5583 return FAIL;
5584 }
5585 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587
5588 if (len == -1)
5589 {
5590 key = get_tv_string(&var1);
5591 if (*key == NUL)
5592 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005593 if (verbose)
5594 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 clear_tv(&var1);
5596 return FAIL;
5597 }
5598 }
5599
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005600 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005602 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005603 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 if (len == -1)
5605 clear_tv(&var1);
5606 if (item == NULL)
5607 return FAIL;
5608
5609 copy_tv(&item->di_tv, &var1);
5610 clear_tv(rettv);
5611 *rettv = var1;
5612 }
5613 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 }
5615 }
5616
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 return OK;
5618}
5619
5620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 * Get an option value.
5622 * "arg" points to the '&' or '+' before the option name.
5623 * "arg" is advanced to character after the option name.
5624 * Return OK or FAIL.
5625 */
5626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005627get_option_tv(
5628 char_u **arg,
5629 typval_T *rettv, /* when NULL, only check if option exists */
5630 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 char_u *option_end;
5633 long numval;
5634 char_u *stringval;
5635 int opt_type;
5636 int c;
5637 int working = (**arg == '+'); /* has("+option") */
5638 int ret = OK;
5639 int opt_flags;
5640
5641 /*
5642 * Isolate the option name and find its value.
5643 */
5644 option_end = find_option_end(arg, &opt_flags);
5645 if (option_end == NULL)
5646 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005647 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 EMSG2(_("E112: Option name missing: %s"), *arg);
5649 return FAIL;
5650 }
5651
5652 if (!evaluate)
5653 {
5654 *arg = option_end;
5655 return OK;
5656 }
5657
5658 c = *option_end;
5659 *option_end = NUL;
5660 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005661 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
5663 if (opt_type == -3) /* invalid name */
5664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 EMSG2(_("E113: Unknown option: %s"), *arg);
5667 ret = FAIL;
5668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 if (opt_type == -2) /* hidden string option */
5672 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 rettv->v_type = VAR_STRING;
5674 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 }
5676 else if (opt_type == -1) /* hidden number option */
5677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 rettv->v_type = VAR_NUMBER;
5679 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 else if (opt_type == 1) /* number option */
5682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 rettv->v_type = VAR_NUMBER;
5684 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 else /* string option */
5687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 rettv->v_type = VAR_STRING;
5689 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 }
5692 else if (working && (opt_type == -2 || opt_type == -1))
5693 ret = FAIL;
5694
5695 *option_end = c; /* put back for error messages */
5696 *arg = option_end;
5697
5698 return ret;
5699}
5700
5701/*
5702 * Allocate a variable for a string constant.
5703 * Return OK or FAIL.
5704 */
5705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005706get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 char_u *p;
5709 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 int extra = 0;
5711
5712 /*
5713 * Find the end of the string, skipping backslashed characters.
5714 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
5717 if (*p == '\\' && p[1] != NUL)
5718 {
5719 ++p;
5720 /* A "\<x>" form occupies at least 4 characters, and produces up
5721 * to 6 characters: reserve space for 2 extra */
5722 if (*p == '<')
5723 extra += 2;
5724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726
5727 if (*p != '"')
5728 {
5729 EMSG2(_("E114: Missing quote: %s"), *arg);
5730 return FAIL;
5731 }
5732
5733 /* If only parsing, set *arg and return here */
5734 if (!evaluate)
5735 {
5736 *arg = p + 1;
5737 return OK;
5738 }
5739
5740 /*
5741 * Copy the string into allocated memory, handling backslashed
5742 * characters.
5743 */
5744 name = alloc((unsigned)(p - *arg + extra));
5745 if (name == NULL)
5746 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 rettv->v_type = VAR_STRING;
5748 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 if (*p == '\\')
5753 {
5754 switch (*++p)
5755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 case 'b': *name++ = BS; ++p; break;
5757 case 'e': *name++ = ESC; ++p; break;
5758 case 'f': *name++ = FF; ++p; break;
5759 case 'n': *name++ = NL; ++p; break;
5760 case 'r': *name++ = CAR; ++p; break;
5761 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 case 'X': /* hex: "\x1", "\x12" */
5764 case 'x':
5765 case 'u': /* Unicode: "\u0023" */
5766 case 'U':
5767 if (vim_isxdigit(p[1]))
5768 {
5769 int n, nr;
5770 int c = toupper(*p);
5771
5772 if (c == 'X')
5773 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005774 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005776 else
5777 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 nr = 0;
5779 while (--n >= 0 && vim_isxdigit(p[1]))
5780 {
5781 ++p;
5782 nr = (nr << 4) + hex2nr(*p);
5783 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#ifdef FEAT_MBYTE
5786 /* For "\u" store the number according to
5787 * 'encoding'. */
5788 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 else
5791#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 break;
5795
5796 /* octal: "\1", "\12", "\123" */
5797 case '0':
5798 case '1':
5799 case '2':
5800 case '3':
5801 case '4':
5802 case '5':
5803 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 case '7': *name = *p++ - '0';
5805 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 *name = (*name << 3) + *p++ - '0';
5808 if (*p >= '0' && *p <= '7')
5809 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 break;
5813
5814 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (extra != 0)
5817 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 break;
5820 }
5821 /* FALLTHROUGH */
5822
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 break;
5825 }
5826 }
5827 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 *arg = p + 1;
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 return OK;
5835}
5836
5837/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 * Return OK or FAIL.
5840 */
5841 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005842get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843{
5844 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005845 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005846 int reduce = 0;
5847
5848 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 */
5851 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 break;
5857 ++reduce;
5858 ++p;
5859 }
5860 }
5861
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 return FAIL;
5866 }
5867
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 if (!evaluate)
5870 {
5871 *arg = p + 1;
5872 return OK;
5873 }
5874
5875 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 */
5878 str = alloc((unsigned)((p - *arg) - reduce));
5879 if (str == NULL)
5880 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 rettv->v_type = VAR_STRING;
5882 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 break;
5890 ++p;
5891 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 *arg = p + 1;
5896
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 return OK;
5898}
5899
5900/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 * Allocate a variable for a List and fill it from "*arg".
5902 * Return OK or FAIL.
5903 */
5904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005905get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906{
Bram Moolenaar33570922005-01-25 22:26:29 +00005907 list_T *l = NULL;
5908 typval_T tv;
5909 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910
5911 if (evaluate)
5912 {
5913 l = list_alloc();
5914 if (l == NULL)
5915 return FAIL;
5916 }
5917
5918 *arg = skipwhite(*arg + 1);
5919 while (**arg != ']' && **arg != NUL)
5920 {
5921 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5922 goto failret;
5923 if (evaluate)
5924 {
5925 item = listitem_alloc();
5926 if (item != NULL)
5927 {
5928 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005929 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930 list_append(l, item);
5931 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005932 else
5933 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934 }
5935
5936 if (**arg == ']')
5937 break;
5938 if (**arg != ',')
5939 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 goto failret;
5942 }
5943 *arg = skipwhite(*arg + 1);
5944 }
5945
5946 if (**arg != ']')
5947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005948 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005949failret:
5950 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005951 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 return FAIL;
5953 }
5954
5955 *arg = skipwhite(*arg + 1);
5956 if (evaluate)
5957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 rettv->v_type = VAR_LIST;
5959 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960 ++l->lv_refcount;
5961 }
5962
5963 return OK;
5964}
5965
5966/*
5967 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005968 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005970 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005973 list_T *l;
5974
5975 l = (list_T *)alloc_clear(sizeof(list_T));
5976 if (l != NULL)
5977 {
5978 /* Prepend the list to the list of lists for garbage collection. */
5979 if (first_list != NULL)
5980 first_list->lv_used_prev = l;
5981 l->lv_used_prev = NULL;
5982 l->lv_used_next = first_list;
5983 first_list = l;
5984 }
5985 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986}
5987
5988/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005989 * Allocate an empty list for a return value.
5990 * Returns OK or FAIL.
5991 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005992 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005993rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005994{
5995 list_T *l = list_alloc();
5996
5997 if (l == NULL)
5998 return FAIL;
5999
6000 rettv->vval.v_list = l;
6001 rettv->v_type = VAR_LIST;
6002 ++l->lv_refcount;
6003 return OK;
6004}
6005
6006/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 * Unreference a list: decrement the reference count and free it when it
6008 * becomes zero.
6009 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006011list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006013 if (l != NULL && --l->lv_refcount <= 0)
6014 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015}
6016
6017/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006018 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 * Ignores the reference count.
6020 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006022list_free(
6023 list_T *l,
6024 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025{
Bram Moolenaar33570922005-01-25 22:26:29 +00006026 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006027
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006028 /* Remove the list from the list of lists for garbage collection. */
6029 if (l->lv_used_prev == NULL)
6030 first_list = l->lv_used_next;
6031 else
6032 l->lv_used_prev->lv_used_next = l->lv_used_next;
6033 if (l->lv_used_next != NULL)
6034 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6035
Bram Moolenaard9fba312005-06-26 22:34:35 +00006036 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006038 /* Remove the item before deleting it. */
6039 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006040 if (recurse || (item->li_tv.v_type != VAR_LIST
6041 && item->li_tv.v_type != VAR_DICT))
6042 clear_tv(&item->li_tv);
6043 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 }
6045 vim_free(l);
6046}
6047
6048/*
6049 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006050 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006052 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006053listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056}
6057
6058/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006059 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006062listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006064 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 vim_free(item);
6066}
6067
6068/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006069 * Remove a list item from a List and free it. Also clears the value.
6070 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006072listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006073{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006074 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075 listitem_free(item);
6076}
6077
6078/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 * Get the number of items in a list.
6080 */
6081 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006082list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 if (l == NULL)
6085 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006086 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087}
6088
6089/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 * Return TRUE when two lists have exactly the same values.
6091 */
6092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006093list_equal(
6094 list_T *l1,
6095 list_T *l2,
6096 int ic, /* ignore case for strings */
6097 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006098{
Bram Moolenaar33570922005-01-25 22:26:29 +00006099 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006100
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006101 if (l1 == NULL || l2 == NULL)
6102 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006103 if (l1 == l2)
6104 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006105 if (list_len(l1) != list_len(l2))
6106 return FALSE;
6107
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006108 for (item1 = l1->lv_first, item2 = l2->lv_first;
6109 item1 != NULL && item2 != NULL;
6110 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112 return FALSE;
6113 return item1 == NULL && item2 == NULL;
6114}
6115
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006116/*
6117 * Return the dictitem that an entry in a hashtable points to.
6118 */
6119 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006120dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006121{
6122 return HI2DI(hi);
6123}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006124
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006126 * Return TRUE when two dictionaries have exactly the same key/values.
6127 */
6128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006129dict_equal(
6130 dict_T *d1,
6131 dict_T *d2,
6132 int ic, /* ignore case for strings */
6133 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006134{
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 hashitem_T *hi;
6136 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006137 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006138
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006139 if (d1 == NULL || d2 == NULL)
6140 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006141 if (d1 == d2)
6142 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143 if (dict_len(d1) != dict_len(d2))
6144 return FALSE;
6145
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006146 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006147 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006149 if (!HASHITEM_EMPTY(hi))
6150 {
6151 item2 = dict_find(d2, hi->hi_key, -1);
6152 if (item2 == NULL)
6153 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006154 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 return FALSE;
6156 --todo;
6157 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006158 }
6159 return TRUE;
6160}
6161
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006162static int tv_equal_recurse_limit;
6163
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006164/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006165 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006166 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006167 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168 */
6169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006170tv_equal(
6171 typval_T *tv1,
6172 typval_T *tv2,
6173 int ic, /* ignore case */
6174 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175{
6176 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006177 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006178 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006179 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006181 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006182 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006184 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185 * recursiveness to a limit. We guess they are equal then.
6186 * A fixed limit has the problem of still taking an awful long time.
6187 * Reduce the limit every time running into it. That should work fine for
6188 * deeply linked structures that are not recursively linked and catch
6189 * recursiveness quickly. */
6190 if (!recursive)
6191 tv_equal_recurse_limit = 1000;
6192 if (recursive_cnt >= tv_equal_recurse_limit)
6193 {
6194 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006195 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006197
6198 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006200 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 ++recursive_cnt;
6202 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6203 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006204 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006205
6206 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 ++recursive_cnt;
6208 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6209 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211
6212 case VAR_FUNC:
6213 return (tv1->vval.v_string != NULL
6214 && tv2->vval.v_string != NULL
6215 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6216
6217 case VAR_NUMBER:
6218 return tv1->vval.v_number == tv2->vval.v_number;
6219
6220 case VAR_STRING:
6221 s1 = get_tv_string_buf(tv1, buf1);
6222 s2 = get_tv_string_buf(tv2, buf2);
6223 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006224
6225 case VAR_SPECIAL:
6226 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006227
6228 case VAR_FLOAT:
6229#ifdef FEAT_FLOAT
6230 return tv1->vval.v_float == tv2->vval.v_float;
6231#endif
6232 case VAR_JOB:
6233#ifdef FEAT_JOB
6234 return tv1->vval.v_job == tv2->vval.v_job;
6235#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006236 case VAR_CHANNEL:
6237#ifdef FEAT_CHANNEL
6238 return tv1->vval.v_channel == tv2->vval.v_channel;
6239#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006240 case VAR_UNKNOWN:
6241 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006243
Bram Moolenaara03f2332016-02-06 18:09:59 +01006244 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6245 * does not equal anything, not even itself. */
6246 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006247}
6248
6249/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006250 * Locate item with index "n" in list "l" and return it.
6251 * A negative index is counted from the end; -1 is the last item.
6252 * Returns NULL when "n" is out of range.
6253 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006254 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006255list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256{
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006258 long idx;
6259
6260 if (l == NULL)
6261 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006262
6263 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006265 n = l->lv_len + n;
6266
6267 /* Check for index out of range. */
6268 if (n < 0 || n >= l->lv_len)
6269 return NULL;
6270
6271 /* When there is a cached index may start search from there. */
6272 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006273 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006274 if (n < l->lv_idx / 2)
6275 {
6276 /* closest to the start of the list */
6277 item = l->lv_first;
6278 idx = 0;
6279 }
6280 else if (n > (l->lv_idx + l->lv_len) / 2)
6281 {
6282 /* closest to the end of the list */
6283 item = l->lv_last;
6284 idx = l->lv_len - 1;
6285 }
6286 else
6287 {
6288 /* closest to the cached index */
6289 item = l->lv_idx_item;
6290 idx = l->lv_idx;
6291 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 }
6293 else
6294 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006295 if (n < l->lv_len / 2)
6296 {
6297 /* closest to the start of the list */
6298 item = l->lv_first;
6299 idx = 0;
6300 }
6301 else
6302 {
6303 /* closest to the end of the list */
6304 item = l->lv_last;
6305 idx = l->lv_len - 1;
6306 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308
6309 while (n > idx)
6310 {
6311 /* search forward */
6312 item = item->li_next;
6313 ++idx;
6314 }
6315 while (n < idx)
6316 {
6317 /* search backward */
6318 item = item->li_prev;
6319 --idx;
6320 }
6321
6322 /* cache the used index */
6323 l->lv_idx = idx;
6324 l->lv_idx_item = item;
6325
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 return item;
6327}
6328
6329/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006330 * Get list item "l[idx]" as a number.
6331 */
6332 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006333list_find_nr(
6334 list_T *l,
6335 long idx,
6336 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006337{
6338 listitem_T *li;
6339
6340 li = list_find(l, idx);
6341 if (li == NULL)
6342 {
6343 if (errorp != NULL)
6344 *errorp = TRUE;
6345 return -1L;
6346 }
6347 return get_tv_number_chk(&li->li_tv, errorp);
6348}
6349
6350/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006351 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6352 */
6353 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006354list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006355{
6356 listitem_T *li;
6357
6358 li = list_find(l, idx - 1);
6359 if (li == NULL)
6360 {
6361 EMSGN(_(e_listidx), idx);
6362 return NULL;
6363 }
6364 return get_tv_string(&li->li_tv);
6365}
6366
6367/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006368 * Locate "item" list "l" and return its index.
6369 * Returns -1 when "item" is not in the list.
6370 */
6371 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006372list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006373{
6374 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006376
6377 if (l == NULL)
6378 return -1;
6379 idx = 0;
6380 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6381 ++idx;
6382 if (li == NULL)
6383 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006384 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006385}
6386
6387/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006388 * Append item "item" to the end of list "l".
6389 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006391list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392{
6393 if (l->lv_last == NULL)
6394 {
6395 /* empty list */
6396 l->lv_first = item;
6397 l->lv_last = item;
6398 item->li_prev = NULL;
6399 }
6400 else
6401 {
6402 l->lv_last->li_next = item;
6403 item->li_prev = l->lv_last;
6404 l->lv_last = item;
6405 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006406 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 item->li_next = NULL;
6408}
6409
6410/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006414 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006415list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006417 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418
Bram Moolenaar05159a02005-02-26 23:04:13 +00006419 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006421 copy_tv(tv, &li->li_tv);
6422 list_append(l, li);
6423 return OK;
6424}
6425
6426/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006427 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006428 * Return FAIL when out of memory.
6429 */
6430 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006431list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432{
6433 listitem_T *li = listitem_alloc();
6434
6435 if (li == NULL)
6436 return FAIL;
6437 li->li_tv.v_type = VAR_DICT;
6438 li->li_tv.v_lock = 0;
6439 li->li_tv.vval.v_dict = dict;
6440 list_append(list, li);
6441 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 return OK;
6443}
6444
6445/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006446 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006447 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006448 * Returns FAIL when out of memory.
6449 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006450 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006451list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006452{
6453 listitem_T *li = listitem_alloc();
6454
6455 if (li == NULL)
6456 return FAIL;
6457 list_append(l, li);
6458 li->li_tv.v_type = VAR_STRING;
6459 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006460 if (str == NULL)
6461 li->li_tv.vval.v_string = NULL;
6462 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006463 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006464 return FAIL;
6465 return OK;
6466}
6467
6468/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006469 * Append "n" to list "l".
6470 * Returns FAIL when out of memory.
6471 */
6472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006473list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006474{
6475 listitem_T *li;
6476
6477 li = listitem_alloc();
6478 if (li == NULL)
6479 return FAIL;
6480 li->li_tv.v_type = VAR_NUMBER;
6481 li->li_tv.v_lock = 0;
6482 li->li_tv.vval.v_number = n;
6483 list_append(l, li);
6484 return OK;
6485}
6486
6487/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006488 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006489 * If "item" is NULL append at the end.
6490 * Return FAIL when out of memory.
6491 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006492 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006493list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494{
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496
6497 if (ni == NULL)
6498 return FAIL;
6499 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006500 list_insert(l, ni, item);
6501 return OK;
6502}
6503
6504 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006505list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006506{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 if (item == NULL)
6508 /* Append new item at end of list. */
6509 list_append(l, ni);
6510 else
6511 {
6512 /* Insert new item before existing item. */
6513 ni->li_prev = item->li_prev;
6514 ni->li_next = item;
6515 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006516 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006518 ++l->lv_idx;
6519 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 l->lv_idx_item = NULL;
6524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528}
6529
6530/*
6531 * Extend "l1" with "l2".
6532 * If "bef" is NULL append at the end, otherwise insert before this item.
6533 * Returns FAIL when out of memory.
6534 */
6535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006536list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537{
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006539 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006541 /* We also quit the loop when we have inserted the original item count of
6542 * the list, avoid a hang when we extend a list with itself. */
6543 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6545 return FAIL;
6546 return OK;
6547}
6548
6549/*
6550 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6551 * Return FAIL when out of memory.
6552 */
6553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006554list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006558 if (l1 == NULL || l2 == NULL)
6559 return FAIL;
6560
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 if (l == NULL)
6564 return FAIL;
6565 tv->v_type = VAR_LIST;
6566 tv->vval.v_list = l;
6567
6568 /* append all items from the second list */
6569 return list_extend(l, l2, NULL);
6570}
6571
6572/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006573 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006574 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006575 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576 * Returns NULL when out of memory.
6577 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006579list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580{
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 list_T *copy;
6582 listitem_T *item;
6583 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584
6585 if (orig == NULL)
6586 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587
6588 copy = list_alloc();
6589 if (copy != NULL)
6590 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 if (copyID != 0)
6592 {
6593 /* Do this before adding the items, because one of the items may
6594 * refer back to this list. */
6595 orig->lv_copyID = copyID;
6596 orig->lv_copylist = copy;
6597 }
6598 for (item = orig->lv_first; item != NULL && !got_int;
6599 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 {
6601 ni = listitem_alloc();
6602 if (ni == NULL)
6603 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006604 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006605 {
6606 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6607 {
6608 vim_free(ni);
6609 break;
6610 }
6611 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006613 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 list_append(copy, ni);
6615 }
6616 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006617 if (item != NULL)
6618 {
6619 list_unref(copy);
6620 copy = NULL;
6621 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 }
6623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006624 return copy;
6625}
6626
6627/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006628 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006629 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006630 * This used to be called list_remove, but that conflicts with a Sun header
6631 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006634vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635{
Bram Moolenaar33570922005-01-25 22:26:29 +00006636 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006638 /* notify watchers */
6639 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006641 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006642 list_fix_watch(l, ip);
6643 if (ip == item2)
6644 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646
6647 if (item2->li_next == NULL)
6648 l->lv_last = item->li_prev;
6649 else
6650 item2->li_next->li_prev = item->li_prev;
6651 if (item->li_prev == NULL)
6652 l->lv_first = item2->li_next;
6653 else
6654 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006655 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656}
6657
6658/*
6659 * Return an allocated string with the string representation of a list.
6660 * May return NULL.
6661 */
6662 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006663list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664{
6665 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666
6667 if (tv->vval.v_list == NULL)
6668 return NULL;
6669 ga_init2(&ga, (int)sizeof(char), 80);
6670 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006671 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006672 {
6673 vim_free(ga.ga_data);
6674 return NULL;
6675 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676 ga_append(&ga, ']');
6677 ga_append(&ga, NUL);
6678 return (char_u *)ga.ga_data;
6679}
6680
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006681typedef struct join_S {
6682 char_u *s;
6683 char_u *tofree;
6684} join_T;
6685
6686 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006687list_join_inner(
6688 garray_T *gap, /* to store the result in */
6689 list_T *l,
6690 char_u *sep,
6691 int echo_style,
6692 int copyID,
6693 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694{
6695 int i;
6696 join_T *p;
6697 int len;
6698 int sumlen = 0;
6699 int first = TRUE;
6700 char_u *tofree;
6701 char_u numbuf[NUMBUFLEN];
6702 listitem_T *item;
6703 char_u *s;
6704
6705 /* Stringify each item in the list. */
6706 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6707 {
6708 if (echo_style)
6709 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6710 else
6711 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6712 if (s == NULL)
6713 return FAIL;
6714
6715 len = (int)STRLEN(s);
6716 sumlen += len;
6717
Bram Moolenaarcde88542015-08-11 19:14:00 +02006718 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006719 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6720 if (tofree != NULL || s != numbuf)
6721 {
6722 p->s = s;
6723 p->tofree = tofree;
6724 }
6725 else
6726 {
6727 p->s = vim_strnsave(s, len);
6728 p->tofree = p->s;
6729 }
6730
6731 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006732 if (did_echo_string_emsg) /* recursion error, bail out */
6733 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006734 }
6735
6736 /* Allocate result buffer with its total size, avoid re-allocation and
6737 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6738 if (join_gap->ga_len >= 2)
6739 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6740 if (ga_grow(gap, sumlen + 2) == FAIL)
6741 return FAIL;
6742
6743 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6744 {
6745 if (first)
6746 first = FALSE;
6747 else
6748 ga_concat(gap, sep);
6749 p = ((join_T *)join_gap->ga_data) + i;
6750
6751 if (p->s != NULL)
6752 ga_concat(gap, p->s);
6753 line_breakcheck();
6754 }
6755
6756 return OK;
6757}
6758
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006759/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006760 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006761 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006762 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006763 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006765list_join(
6766 garray_T *gap,
6767 list_T *l,
6768 char_u *sep,
6769 int echo_style,
6770 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006772 garray_T join_ga;
6773 int retval;
6774 join_T *p;
6775 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776
Bram Moolenaard39a7512015-04-16 22:51:22 +02006777 if (l->lv_len < 1)
6778 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006779 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6780 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6781
6782 /* Dispose each item in join_ga. */
6783 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785 p = (join_T *)join_ga.ga_data;
6786 for (i = 0; i < join_ga.ga_len; ++i)
6787 {
6788 vim_free(p->tofree);
6789 ++p;
6790 }
6791 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006792 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006793
6794 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795}
6796
6797/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006798 * Return the next (unique) copy ID.
6799 * Used for serializing nested structures.
6800 */
6801 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006802get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006803{
6804 current_copyID += COPYID_INC;
6805 return current_copyID;
6806}
6807
6808/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809 * Garbage collection for lists and dictionaries.
6810 *
6811 * We use reference counts to be able to free most items right away when they
6812 * are no longer used. But for composite items it's possible that it becomes
6813 * unused while the reference count is > 0: When there is a recursive
6814 * reference. Example:
6815 * :let l = [1, 2, 3]
6816 * :let d = {9: l}
6817 * :let l[1] = d
6818 *
6819 * Since this is quite unusual we handle this with garbage collection: every
6820 * once in a while find out which lists and dicts are not referenced from any
6821 * variable.
6822 *
6823 * Here is a good reference text about garbage collection (refers to Python
6824 * but it applies to all reference-counting mechanisms):
6825 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006826 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006827
6828/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006829 * Do garbage collection for lists and dicts.
6830 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006833garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006836 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 buf_T *buf;
6838 win_T *wp;
6839 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006840 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006841 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006842 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006843#ifdef FEAT_WINDOWS
6844 tabpage_T *tp;
6845#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006847 /* Only do this once. */
6848 want_garbage_collect = FALSE;
6849 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006850 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006851
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006852 /* We advance by two because we add one for items referenced through
6853 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006854 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 /*
6857 * 1. Go through all accessible variables and mark all lists and dicts
6858 * with copyID.
6859 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860
6861 /* Don't free variables in the previous_funccal list unless they are only
6862 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006863 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006864 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6865 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006866 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6867 NULL);
6868 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6869 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 }
6871
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 /* script-local variables */
6873 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875
6876 /* buffer-local variables */
6877 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006878 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6879 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006880
6881 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006882 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6884 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006885#ifdef FEAT_AUTOCMD
6886 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6888 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006889#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006891#ifdef FEAT_WINDOWS
6892 /* tabpage-local variables */
6893 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6895 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006896#endif
6897
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006900
6901 /* function-local variables */
6902 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6903 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6905 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906 }
6907
Bram Moolenaard812df62008-11-09 12:46:09 +00006908 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006910
Bram Moolenaar1dced572012-04-05 16:54:08 +02006911#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006913#endif
6914
Bram Moolenaardb913952012-06-29 12:54:53 +02006915#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006917#endif
6918
6919#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006921#endif
6922
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006923#ifdef FEAT_CHANNEL
6924 abort = abort || set_ref_in_channel(copyID);
6925#endif
6926
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006928 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 /*
6930 * 2. Free lists and dictionaries that are not referenced.
6931 */
6932 did_free = free_unref_items(copyID);
6933
6934 /*
6935 * 3. Check if any funccal can be freed now.
6936 */
6937 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006938 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 if (can_free_funccal(*pfc, copyID))
6940 {
6941 fc = *pfc;
6942 *pfc = fc->caller;
6943 free_funccal(fc, TRUE);
6944 did_free = TRUE;
6945 did_free_funccal = TRUE;
6946 }
6947 else
6948 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 if (did_free_funccal)
6951 /* When a funccal was freed some more items might be garbage
6952 * collected, so run again. */
6953 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 else if (p_verbose > 0)
6956 {
6957 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6958 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959
6960 return did_free;
6961}
6962
6963/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006964 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 */
6966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006967free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006969 dict_T *dd, *dd_next;
6970 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 int did_free = FALSE;
6972
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006975 */
6976 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006977 {
6978 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006981 /* Free the Dictionary and ordinary items it contains, but don't
6982 * recurse into Lists and Dictionaries, they will be in the list
6983 * of dicts or list of lists. */
6984 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006987 dd = dd_next;
6988 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989
6990 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 * Go through the list of lists and free items without the copyID.
6992 * But don't free a list that has a watcher (used in a for loop), these
6993 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 */
6995 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 {
6997 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6999 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007001 /* Free the List and ordinary items it contains, but don't recurse
7002 * into Lists and Dictionaries, they will be in the list of dicts
7003 * or list of lists. */
7004 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 ll = ll_next;
7008 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007009
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 return did_free;
7011}
7012
7013/*
7014 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 * "list_stack" is used to add lists to be marked. Can be NULL.
7016 *
7017 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007019 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007020set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021{
7022 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 hashtab_T *cur_ht;
7026 ht_stack_T *ht_stack = NULL;
7027 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 cur_ht = ht;
7030 for (;;)
7031 {
7032 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 /* Mark each item in the hashtab. If the item contains a hashtab
7035 * it is added to ht_stack, if it contains a list it is added to
7036 * list_stack. */
7037 todo = (int)cur_ht->ht_used;
7038 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7039 if (!HASHITEM_EMPTY(hi))
7040 {
7041 --todo;
7042 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7043 &ht_stack, list_stack);
7044 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046
7047 if (ht_stack == NULL)
7048 break;
7049
7050 /* take an item from the stack */
7051 cur_ht = ht_stack->ht;
7052 tempitem = ht_stack;
7053 ht_stack = ht_stack->prev;
7054 free(tempitem);
7055 }
7056
7057 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058}
7059
7060/*
7061 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7063 *
7064 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007065 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007067set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069 listitem_T *li;
7070 int abort = FALSE;
7071 list_T *cur_l;
7072 list_stack_T *list_stack = NULL;
7073 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 cur_l = l;
7076 for (;;)
7077 {
7078 if (!abort)
7079 /* Mark each item in the list. If the item contains a hashtab
7080 * it is added to ht_stack, if it contains a list it is added to
7081 * list_stack. */
7082 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7083 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7084 ht_stack, &list_stack);
7085 if (list_stack == NULL)
7086 break;
7087
7088 /* take an item from the stack */
7089 cur_l = list_stack->list;
7090 tempitem = list_stack;
7091 list_stack = list_stack->prev;
7092 free(tempitem);
7093 }
7094
7095 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096}
7097
7098/*
7099 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 * "list_stack" is used to add lists to be marked. Can be NULL.
7101 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7102 *
7103 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007106set_ref_in_item(
7107 typval_T *tv,
7108 int copyID,
7109 ht_stack_T **ht_stack,
7110 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007111{
7112 dict_T *dd;
7113 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007114 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007115
Bram Moolenaara03f2332016-02-06 18:09:59 +01007116 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007117 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007118 dd = tv->vval.v_dict;
7119 if (dd != NULL && dd->dv_copyID != copyID)
7120 {
7121 /* Didn't see this dict yet. */
7122 dd->dv_copyID = copyID;
7123 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007124 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007125 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7126 }
7127 else
7128 {
7129 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7130 if (newitem == NULL)
7131 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007132 else
7133 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007134 newitem->ht = &dd->dv_hashtab;
7135 newitem->prev = *ht_stack;
7136 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007138 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007139 }
7140 }
7141 else if (tv->v_type == VAR_LIST)
7142 {
7143 ll = tv->vval.v_list;
7144 if (ll != NULL && ll->lv_copyID != copyID)
7145 {
7146 /* Didn't see this list yet. */
7147 ll->lv_copyID = copyID;
7148 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007149 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007150 abort = set_ref_in_list(ll, copyID, ht_stack);
7151 }
7152 else
7153 {
7154 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007155 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007156 if (newitem == NULL)
7157 abort = TRUE;
7158 else
7159 {
7160 newitem->list = ll;
7161 newitem->prev = *list_stack;
7162 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007163 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007164 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007165 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007167 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168}
7169
7170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171 * Allocate an empty header for a dictionary.
7172 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007173 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007174dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007175{
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007177
Bram Moolenaar33570922005-01-25 22:26:29 +00007178 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007180 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007181 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007182 if (first_dict != NULL)
7183 first_dict->dv_used_prev = d;
7184 d->dv_used_next = first_dict;
7185 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007186 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187
Bram Moolenaar33570922005-01-25 22:26:29 +00007188 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007189 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007190 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007193 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007194 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195}
7196
7197/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007198 * Allocate an empty dict for a return value.
7199 * Returns OK or FAIL.
7200 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007202rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007203{
7204 dict_T *d = dict_alloc();
7205
7206 if (d == NULL)
7207 return FAIL;
7208
7209 rettv->vval.v_dict = d;
7210 rettv->v_type = VAR_DICT;
7211 ++d->dv_refcount;
7212 return OK;
7213}
7214
7215
7216/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 * Unreference a Dictionary: decrement the reference count and free it when it
7218 * becomes zero.
7219 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007221dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007223 if (d != NULL && --d->dv_refcount <= 0)
7224 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225}
7226
7227/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007228 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229 * Ignores the reference count.
7230 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007232dict_free(
7233 dict_T *d,
7234 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007236 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007237 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007238 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007240 /* Remove the dict from the list of dicts for garbage collection. */
7241 if (d->dv_used_prev == NULL)
7242 first_dict = d->dv_used_next;
7243 else
7244 d->dv_used_prev->dv_used_next = d->dv_used_next;
7245 if (d->dv_used_next != NULL)
7246 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7247
7248 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007249 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007250 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007251 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253 if (!HASHITEM_EMPTY(hi))
7254 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007255 /* Remove the item before deleting it, just in case there is
7256 * something recursive causing trouble. */
7257 di = HI2DI(hi);
7258 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007259 if (recurse || (di->di_tv.v_type != VAR_LIST
7260 && di->di_tv.v_type != VAR_DICT))
7261 clear_tv(&di->di_tv);
7262 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263 --todo;
7264 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 vim_free(d);
7268}
7269
7270/*
7271 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007272 * The "key" is copied to the new item.
7273 * Note that the value of the item "di_tv" still needs to be initialized!
7274 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007276 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007277dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278{
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007281 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007282 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007283 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007284 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007285 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007286 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288}
7289
7290/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007291 * Make a copy of a Dictionary item.
7292 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007294dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295{
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7299 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300 if (di != NULL)
7301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007303 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 copy_tv(&org->di_tv, &di->di_tv);
7305 }
7306 return di;
7307}
7308
7309/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310 * Remove item "item" from Dictionary "dict" and free it.
7311 */
7312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314{
Bram Moolenaar33570922005-01-25 22:26:29 +00007315 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007318 if (HASHITEM_EMPTY(hi))
7319 EMSG2(_(e_intern2), "dictitem_remove()");
7320 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 dictitem_free(item);
7323}
7324
7325/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 * Free a dict item. Also clears the value.
7327 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007329dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007330{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007332 if (item->di_flags & DI_FLAGS_ALLOC)
7333 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334}
7335
7336/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007337 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7338 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007339 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 * Returns NULL when out of memory.
7341 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007343dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007344{
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 dict_T *copy;
7346 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007347 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349
7350 if (orig == NULL)
7351 return NULL;
7352
7353 copy = dict_alloc();
7354 if (copy != NULL)
7355 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007356 if (copyID != 0)
7357 {
7358 orig->dv_copyID = copyID;
7359 orig->dv_copydict = copy;
7360 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007361 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007362 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 --todo;
7367
7368 di = dictitem_alloc(hi->hi_key);
7369 if (di == NULL)
7370 break;
7371 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007372 {
7373 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7374 copyID) == FAIL)
7375 {
7376 vim_free(di);
7377 break;
7378 }
7379 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007380 else
7381 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7382 if (dict_add(copy, di) == FAIL)
7383 {
7384 dictitem_free(di);
7385 break;
7386 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007389
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 if (todo > 0)
7392 {
7393 dict_unref(copy);
7394 copy = NULL;
7395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 }
7397
7398 return copy;
7399}
7400
7401/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007403 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007405 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007406dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407{
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409}
7410
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007412 * Add a number or string entry to dictionary "d".
7413 * When "str" is NULL use number "nr", otherwise use "str".
7414 * Returns FAIL when out of memory and when key already exists.
7415 */
7416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007417dict_add_nr_str(
7418 dict_T *d,
7419 char *key,
7420 long nr,
7421 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007422{
7423 dictitem_T *item;
7424
7425 item = dictitem_alloc((char_u *)key);
7426 if (item == NULL)
7427 return FAIL;
7428 item->di_tv.v_lock = 0;
7429 if (str == NULL)
7430 {
7431 item->di_tv.v_type = VAR_NUMBER;
7432 item->di_tv.vval.v_number = nr;
7433 }
7434 else
7435 {
7436 item->di_tv.v_type = VAR_STRING;
7437 item->di_tv.vval.v_string = vim_strsave(str);
7438 }
7439 if (dict_add(d, item) == FAIL)
7440 {
7441 dictitem_free(item);
7442 return FAIL;
7443 }
7444 return OK;
7445}
7446
7447/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007448 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007449 * Returns FAIL when out of memory and when key already exists.
7450 */
7451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007452dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007453{
7454 dictitem_T *item;
7455
7456 item = dictitem_alloc((char_u *)key);
7457 if (item == NULL)
7458 return FAIL;
7459 item->di_tv.v_lock = 0;
7460 item->di_tv.v_type = VAR_LIST;
7461 item->di_tv.vval.v_list = list;
7462 if (dict_add(d, item) == FAIL)
7463 {
7464 dictitem_free(item);
7465 return FAIL;
7466 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007467 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007468 return OK;
7469}
7470
7471/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007472 * Get the number of items in a Dictionary.
7473 */
7474 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007475dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 if (d == NULL)
7478 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007479 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480}
7481
7482/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007483 * Find item "key[len]" in Dictionary "d".
7484 * If "len" is negative use strlen(key).
7485 * Returns NULL when not found.
7486 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007487 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007488dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007490#define AKEYLEN 200
7491 char_u buf[AKEYLEN];
7492 char_u *akey;
7493 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 if (len < 0)
7497 akey = key;
7498 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007499 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 tofree = akey = vim_strnsave(key, len);
7501 if (akey == NULL)
7502 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007503 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007504 else
7505 {
7506 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007507 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007508 akey = buf;
7509 }
7510
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512 vim_free(tofree);
7513 if (HASHITEM_EMPTY(hi))
7514 return NULL;
7515 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516}
7517
7518/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007519 * Get a string item from a dictionary.
7520 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007521 * Returns NULL if the entry doesn't exist or out of memory.
7522 */
7523 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007524get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007525{
7526 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007527 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007528
7529 di = dict_find(d, key, -1);
7530 if (di == NULL)
7531 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007532 s = get_tv_string(&di->di_tv);
7533 if (save && s != NULL)
7534 s = vim_strsave(s);
7535 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007536}
7537
7538/*
7539 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007540 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541 */
7542 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007543get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007544{
7545 dictitem_T *di;
7546
7547 di = dict_find(d, key, -1);
7548 if (di == NULL)
7549 return 0;
7550 return get_tv_number(&di->di_tv);
7551}
7552
7553/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 * Return an allocated string with the string representation of a Dictionary.
7555 * May return NULL.
7556 */
7557 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007558dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559{
7560 garray_T ga;
7561 int first = TRUE;
7562 char_u *tofree;
7563 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007564 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007567 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 return NULL;
7571 ga_init2(&ga, (int)sizeof(char), 80);
7572 ga_append(&ga, '{');
7573
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007574 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007575 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007579 --todo;
7580
7581 if (first)
7582 first = FALSE;
7583 else
7584 ga_concat(&ga, (char_u *)", ");
7585
7586 tofree = string_quote(hi->hi_key, FALSE);
7587 if (tofree != NULL)
7588 {
7589 ga_concat(&ga, tofree);
7590 vim_free(tofree);
7591 }
7592 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007593 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 if (s != NULL)
7595 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007597 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007598 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007599 line_breakcheck();
7600
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007603 if (todo > 0)
7604 {
7605 vim_free(ga.ga_data);
7606 return NULL;
7607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608
7609 ga_append(&ga, '}');
7610 ga_append(&ga, NUL);
7611 return (char_u *)ga.ga_data;
7612}
7613
7614/*
7615 * Allocate a variable for a Dictionary and fill it from "*arg".
7616 * Return OK or FAIL. Returns NOTDONE for {expr}.
7617 */
7618 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007619get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620{
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 dict_T *d = NULL;
7622 typval_T tvkey;
7623 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007624 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007628
7629 /*
7630 * First check if it's not a curly-braces thing: {expr}.
7631 * Must do this without evaluating, otherwise a function may be called
7632 * twice. Unfortunately this means we need to call eval1() twice for the
7633 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007634 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007635 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 if (*start != '}')
7637 {
7638 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7639 return FAIL;
7640 if (*start == '}')
7641 return NOTDONE;
7642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643
7644 if (evaluate)
7645 {
7646 d = dict_alloc();
7647 if (d == NULL)
7648 return FAIL;
7649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007650 tvkey.v_type = VAR_UNKNOWN;
7651 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652
7653 *arg = skipwhite(*arg + 1);
7654 while (**arg != '}' && **arg != NUL)
7655 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 goto failret;
7658 if (**arg != ':')
7659 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007660 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 goto failret;
7663 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007664 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007666 key = get_tv_string_buf_chk(&tvkey, buf);
7667 if (key == NULL || *key == NUL)
7668 {
7669 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7670 if (key != NULL)
7671 EMSG(_(e_emptykey));
7672 clear_tv(&tvkey);
7673 goto failret;
7674 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676
7677 *arg = skipwhite(*arg + 1);
7678 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7679 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007680 if (evaluate)
7681 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 goto failret;
7683 }
7684 if (evaluate)
7685 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007686 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 if (item != NULL)
7688 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007689 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007690 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 clear_tv(&tv);
7692 goto failret;
7693 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007694 item = dictitem_alloc(key);
7695 clear_tv(&tvkey);
7696 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007699 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007700 if (dict_add(d, item) == FAIL)
7701 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 }
7703 }
7704
7705 if (**arg == '}')
7706 break;
7707 if (**arg != ',')
7708 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007709 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 goto failret;
7711 }
7712 *arg = skipwhite(*arg + 1);
7713 }
7714
7715 if (**arg != '}')
7716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007717 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718failret:
7719 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007720 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 return FAIL;
7722 }
7723
7724 *arg = skipwhite(*arg + 1);
7725 if (evaluate)
7726 {
7727 rettv->v_type = VAR_DICT;
7728 rettv->vval.v_dict = d;
7729 ++d->dv_refcount;
7730 }
7731
7732 return OK;
7733}
7734
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007735#if defined(FEAT_CHANNEL) || defined(PROTO)
7736/*
7737 * Decrement the reference count on "channel" and free it when it goes down to
7738 * zero.
7739 * Returns TRUE when the channel was freed.
7740 */
7741 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007742channel_unref(channel_T *channel)
7743{
7744 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007745 {
Bram Moolenaar77073442016-02-13 23:23:53 +01007746 channel_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007747 return TRUE;
7748 }
7749 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007750}
7751#endif
7752
Bram Moolenaar835dc632016-02-07 14:27:38 +01007753#ifdef FEAT_JOB
7754 static void
7755job_free(job_T *job)
7756{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007757# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007758 if (job->jv_channel != NULL)
7759 {
7760 /* The channel doesn't count as a references for the job, we need to
7761 * NULL the reference when the job is freed. */
7762 job->jv_channel->ch_job = NULL;
7763 channel_unref(job->jv_channel);
7764 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007765# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007766 mch_clear_job(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007767 vim_free(job);
7768}
7769
7770 static void
7771job_unref(job_T *job)
7772{
7773 if (job != NULL && --job->jv_refcount <= 0)
7774 job_free(job);
7775}
7776
7777/*
7778 * Allocate a job. Sets the refcount to one.
7779 */
7780 static job_T *
7781job_alloc(void)
7782{
7783 job_T *job;
7784
7785 job = (job_T *)alloc_clear(sizeof(job_T));
7786 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007787 job->jv_refcount = 1;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007788 return job;
7789}
7790
7791#endif
7792
Bram Moolenaar17a13432016-01-24 14:22:10 +01007793 static char *
7794get_var_special_name(int nr)
7795{
7796 switch (nr)
7797 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007798 case VVAL_FALSE: return "v:false";
7799 case VVAL_TRUE: return "v:true";
7800 case VVAL_NONE: return "v:none";
7801 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007802 }
7803 EMSG2(_(e_intern2), "get_var_special_name()");
7804 return "42";
7805}
7806
Bram Moolenaar8c711452005-01-14 21:53:12 +00007807/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007808 * Return a string with the string representation of a variable.
7809 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007810 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007811 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007812 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007813 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814 */
7815 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007816echo_string(
7817 typval_T *tv,
7818 char_u **tofree,
7819 char_u *numbuf,
7820 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007821{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007822 static int recurse = 0;
7823 char_u *r = NULL;
7824
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007827 if (!did_echo_string_emsg)
7828 {
7829 /* Only give this message once for a recursive call to avoid
7830 * flooding the user with errors. And stop iterating over lists
7831 * and dicts. */
7832 did_echo_string_emsg = TRUE;
7833 EMSG(_("E724: variable nested too deep for displaying"));
7834 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007836 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 }
7838 ++recurse;
7839
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840 switch (tv->v_type)
7841 {
7842 case VAR_FUNC:
7843 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007844 r = tv->vval.v_string;
7845 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007846
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007848 if (tv->vval.v_list == NULL)
7849 {
7850 *tofree = NULL;
7851 r = NULL;
7852 }
7853 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7854 {
7855 *tofree = NULL;
7856 r = (char_u *)"[...]";
7857 }
7858 else
7859 {
7860 tv->vval.v_list->lv_copyID = copyID;
7861 *tofree = list2string(tv, copyID);
7862 r = *tofree;
7863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007865
Bram Moolenaar8c711452005-01-14 21:53:12 +00007866 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007867 if (tv->vval.v_dict == NULL)
7868 {
7869 *tofree = NULL;
7870 r = NULL;
7871 }
7872 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7873 {
7874 *tofree = NULL;
7875 r = (char_u *)"{...}";
7876 }
7877 else
7878 {
7879 tv->vval.v_dict->dv_copyID = copyID;
7880 *tofree = dict2string(tv, copyID);
7881 r = *tofree;
7882 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007883 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007884
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 case VAR_STRING:
7886 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007887 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007888 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007889 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007890 *tofree = NULL;
7891 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007892 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007893
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007894 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007895#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007896 *tofree = NULL;
7897 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7898 r = numbuf;
7899 break;
7900#endif
7901
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007902 case VAR_SPECIAL:
7903 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007904 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007905 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007907
Bram Moolenaar8502c702014-06-17 12:51:16 +02007908 if (--recurse == 0)
7909 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007910 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007911}
7912
7913/*
7914 * Return a string with the string representation of a variable.
7915 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7916 * "numbuf" is used for a number.
7917 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007918 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007919 */
7920 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007921tv2string(
7922 typval_T *tv,
7923 char_u **tofree,
7924 char_u *numbuf,
7925 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926{
7927 switch (tv->v_type)
7928 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007929 case VAR_FUNC:
7930 *tofree = string_quote(tv->vval.v_string, TRUE);
7931 return *tofree;
7932 case VAR_STRING:
7933 *tofree = string_quote(tv->vval.v_string, FALSE);
7934 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#ifdef FEAT_FLOAT
7936 case VAR_FLOAT:
7937 *tofree = NULL;
7938 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7939 return numbuf;
7940#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007942 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007944 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007945 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007946 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007947 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007948 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007949 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007950 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007951}
7952
7953/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 * Return string "str" in ' quotes, doubling ' characters.
7955 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007957 */
7958 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007959string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007960{
Bram Moolenaar33570922005-01-25 22:26:29 +00007961 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 char_u *p, *r, *s;
7963
Bram Moolenaar33570922005-01-25 22:26:29 +00007964 len = (function ? 13 : 3);
7965 if (str != NULL)
7966 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007967 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007968 for (p = str; *p != NUL; mb_ptr_adv(p))
7969 if (*p == '\'')
7970 ++len;
7971 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972 s = r = alloc(len);
7973 if (r != NULL)
7974 {
7975 if (function)
7976 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007978 r += 10;
7979 }
7980 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007982 if (str != NULL)
7983 for (p = str; *p != NUL; )
7984 {
7985 if (*p == '\'')
7986 *r++ = '\'';
7987 MB_COPY_CHAR(p, r);
7988 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007989 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 if (function)
7991 *r++ = ')';
7992 *r++ = NUL;
7993 }
7994 return s;
7995}
7996
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007997#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007998/*
7999 * Convert the string "text" to a floating point number.
8000 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8001 * this always uses a decimal point.
8002 * Returns the length of the text that was consumed.
8003 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008005string2float(
8006 char_u *text,
8007 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008008{
8009 char *s = (char *)text;
8010 float_T f;
8011
8012 f = strtod(s, &s);
8013 *value = f;
8014 return (int)((char_u *)s - text);
8015}
8016#endif
8017
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 * Get the value of an environment variable.
8020 * "arg" is pointing to the '$'. It is advanced to after the name.
8021 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008022 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 */
8024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008025get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026{
8027 char_u *string = NULL;
8028 int len;
8029 int cc;
8030 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008031 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
8033 ++*arg;
8034 name = *arg;
8035 len = get_env_len(arg);
8036 if (evaluate)
8037 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008038 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008039 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008040
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008041 cc = name[len];
8042 name[len] = NUL;
8043 /* first try vim_getenv(), fast for normal environment vars */
8044 string = vim_getenv(name, &mustfree);
8045 if (string != NULL && *string != NUL)
8046 {
8047 if (!mustfree)
8048 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008050 else
8051 {
8052 if (mustfree)
8053 vim_free(string);
8054
8055 /* next try expanding things like $VIM and ${HOME} */
8056 string = expand_env_save(name - 1);
8057 if (string != NULL && *string == '$')
8058 {
8059 vim_free(string);
8060 string = NULL;
8061 }
8062 }
8063 name[len] = cc;
8064
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008065 rettv->v_type = VAR_STRING;
8066 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 }
8068
8069 return OK;
8070}
8071
8072/*
8073 * Array with names and number of arguments of all internal functions
8074 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8075 */
8076static struct fst
8077{
8078 char *f_name; /* function name */
8079 char f_min_argc; /* minimal number of arguments */
8080 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008081 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008082 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083} functions[] =
8084{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008085#ifdef FEAT_FLOAT
8086 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008087 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008089 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008090 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008091 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"append", 2, 2, f_append},
8093 {"argc", 0, 0, f_argc},
8094 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008095 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008096 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008097#ifdef FEAT_FLOAT
8098 {"asin", 1, 1, f_asin}, /* WJMc */
8099#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008100 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008101 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008102 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008103 {"assert_false", 1, 2, f_assert_false},
8104 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008105#ifdef FEAT_FLOAT
8106 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008107 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008110 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"bufexists", 1, 1, f_bufexists},
8112 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8113 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8114 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8115 {"buflisted", 1, 1, f_buflisted},
8116 {"bufloaded", 1, 1, f_bufloaded},
8117 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008118 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"bufwinnr", 1, 1, f_bufwinnr},
8120 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008121 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008122 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008123 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008124#ifdef FEAT_FLOAT
8125 {"ceil", 1, 1, f_ceil},
8126#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008127#ifdef FEAT_CHANNEL
8128 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008129 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008130 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008131 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008132 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008133 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8134 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008135 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008136 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008137#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008138 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008139 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008141 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008143#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008144 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008145 {"complete_add", 1, 1, f_complete_add},
8146 {"complete_check", 0, 0, f_complete_check},
8147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008149 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008150#ifdef FEAT_FLOAT
8151 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008152 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008153#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008154 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008156 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008157 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008158 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008160 {"diff_filler", 1, 1, f_diff_filler},
8161 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008162 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008163 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008165 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"eventhandler", 0, 0, f_eventhandler},
8167 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008168 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008170#ifdef FEAT_FLOAT
8171 {"exp", 1, 1, f_exp},
8172#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008173 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008174 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008175 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8177 {"filereadable", 1, 1, f_filereadable},
8178 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008179 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008180 {"finddir", 1, 3, f_finddir},
8181 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182#ifdef FEAT_FLOAT
8183 {"float2nr", 1, 1, f_float2nr},
8184 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008185 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008186#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008187 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"fnamemodify", 2, 2, f_fnamemodify},
8189 {"foldclosed", 1, 1, f_foldclosed},
8190 {"foldclosedend", 1, 1, f_foldclosedend},
8191 {"foldlevel", 1, 1, f_foldlevel},
8192 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008193 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008195 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008196 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008197 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008198 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008199 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"getchar", 0, 1, f_getchar},
8201 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008202 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {"getcmdline", 0, 0, f_getcmdline},
8204 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008205 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008206 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008207 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008208 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008209 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008210 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {"getfsize", 1, 1, f_getfsize},
8212 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008213 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008214 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008215 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008216 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008217 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008218 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008219 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008220 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008222 {"gettabvar", 2, 3, f_gettabvar},
8223 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"getwinposx", 0, 0, f_getwinposx},
8225 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008226 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008227 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008228 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008229 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008231 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008232 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008233 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8235 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8236 {"histadd", 2, 2, f_histadd},
8237 {"histdel", 1, 2, f_histdel},
8238 {"histget", 1, 2, f_histget},
8239 {"histnr", 1, 1, f_histnr},
8240 {"hlID", 1, 1, f_hlID},
8241 {"hlexists", 1, 1, f_hlexists},
8242 {"hostname", 0, 0, f_hostname},
8243 {"iconv", 3, 3, f_iconv},
8244 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008245 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008246 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008248 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"inputrestore", 0, 0, f_inputrestore},
8250 {"inputsave", 0, 0, f_inputsave},
8251 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008252 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008253 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008255 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008256 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008257#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008258# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008259 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008260# endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008261 {"job_start", 1, 2, f_job_start},
8262 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008263 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008264#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008265 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008266 {"js_decode", 1, 1, f_js_decode},
8267 {"js_encode", 1, 1, f_js_encode},
8268 {"json_decode", 1, 1, f_json_decode},
8269 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008270 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008272 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"libcall", 3, 3, f_libcall},
8274 {"libcallnr", 3, 3, f_libcallnr},
8275 {"line", 1, 1, f_line},
8276 {"line2byte", 1, 1, f_line2byte},
8277 {"lispindent", 1, 1, f_lispindent},
8278 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008280 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008281 {"log10", 1, 1, f_log10},
8282#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008283#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008284 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008285#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008286 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008287 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008288 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008289 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008290 {"matchadd", 2, 5, f_matchadd},
8291 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008292 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008293 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008294 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008295 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008296 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008297 {"max", 1, 1, f_max},
8298 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008299#ifdef vim_mkdir
8300 {"mkdir", 1, 3, f_mkdir},
8301#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008302 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008303#ifdef FEAT_MZSCHEME
8304 {"mzeval", 1, 1, f_mzeval},
8305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008307 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008308 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008309 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008310#ifdef FEAT_PERL
8311 {"perleval", 1, 1, f_perleval},
8312#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008313#ifdef FEAT_FLOAT
8314 {"pow", 2, 2, f_pow},
8315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008317 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008318 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008319#ifdef FEAT_PYTHON3
8320 {"py3eval", 1, 1, f_py3eval},
8321#endif
8322#ifdef FEAT_PYTHON
8323 {"pyeval", 1, 1, f_pyeval},
8324#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008325 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008326 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008327 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008328 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008329 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"remote_expr", 2, 3, f_remote_expr},
8331 {"remote_foreground", 1, 1, f_remote_foreground},
8332 {"remote_peek", 1, 2, f_remote_peek},
8333 {"remote_read", 1, 1, f_remote_read},
8334 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008335 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008337 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008339 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008340#ifdef FEAT_FLOAT
8341 {"round", 1, 1, f_round},
8342#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008343 {"screenattr", 2, 2, f_screenattr},
8344 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008345 {"screencol", 0, 0, f_screencol},
8346 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008347 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008348 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008349 {"searchpair", 3, 7, f_searchpair},
8350 {"searchpairpos", 3, 7, f_searchpairpos},
8351 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"server2client", 2, 2, f_server2client},
8353 {"serverlist", 0, 0, f_serverlist},
8354 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008355 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"setcmdpos", 1, 1, f_setcmdpos},
8357 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008358 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008359 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008360 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008361 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008363 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008364 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008366#ifdef FEAT_CRYPT
8367 {"sha256", 1, 1, f_sha256},
8368#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008369 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008370 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008372#ifdef FEAT_FLOAT
8373 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008374 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008375#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008376 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008377 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008378 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008379 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008380 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008381#ifdef FEAT_FLOAT
8382 {"sqrt", 1, 1, f_sqrt},
8383 {"str2float", 1, 1, f_str2float},
8384#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008385 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008386 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008387 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388#ifdef HAVE_STRFTIME
8389 {"strftime", 1, 2, f_strftime},
8390#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008391 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008392 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"strlen", 1, 1, f_strlen},
8394 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008395 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008397 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008398 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"substitute", 4, 4, f_substitute},
8400 {"synID", 3, 3, f_synID},
8401 {"synIDattr", 2, 3, f_synIDattr},
8402 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008403 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008404 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008405 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008406 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008407 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008408 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008409 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008410 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008411 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008412#ifdef FEAT_FLOAT
8413 {"tan", 1, 1, f_tan},
8414 {"tanh", 1, 1, f_tanh},
8415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008417 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {"tolower", 1, 1, f_tolower},
8419 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008420 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008421#ifdef FEAT_FLOAT
8422 {"trunc", 1, 1, f_trunc},
8423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008425 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008426 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008427 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008428 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"virtcol", 1, 1, f_virtcol},
8430 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008431 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"winbufnr", 1, 1, f_winbufnr},
8433 {"wincol", 0, 0, f_wincol},
8434 {"winheight", 1, 1, f_winheight},
8435 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008436 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008438 {"winrestview", 1, 1, f_winrestview},
8439 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008441 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008442 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008443 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444};
8445
8446#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8447
8448/*
8449 * Function given to ExpandGeneric() to obtain the list of internal
8450 * or user defined function names.
8451 */
8452 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008453get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 static int intidx = -1;
8456 char_u *name;
8457
8458 if (idx == 0)
8459 intidx = -1;
8460 if (intidx < 0)
8461 {
8462 name = get_user_func_name(xp, idx);
8463 if (name != NULL)
8464 return name;
8465 }
8466 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8467 {
8468 STRCPY(IObuff, functions[intidx].f_name);
8469 STRCAT(IObuff, "(");
8470 if (functions[intidx].f_max_argc == 0)
8471 STRCAT(IObuff, ")");
8472 return IObuff;
8473 }
8474
8475 return NULL;
8476}
8477
8478/*
8479 * Function given to ExpandGeneric() to obtain the list of internal or
8480 * user defined variable or function names.
8481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008483get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 static int intidx = -1;
8486 char_u *name;
8487
8488 if (idx == 0)
8489 intidx = -1;
8490 if (intidx < 0)
8491 {
8492 name = get_function_name(xp, idx);
8493 if (name != NULL)
8494 return name;
8495 }
8496 return get_user_var_name(xp, ++intidx);
8497}
8498
8499#endif /* FEAT_CMDL_COMPL */
8500
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008501#if defined(EBCDIC) || defined(PROTO)
8502/*
8503 * Compare struct fst by function name.
8504 */
8505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008506compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008507{
8508 struct fst *p1 = (struct fst *)s1;
8509 struct fst *p2 = (struct fst *)s2;
8510
8511 return STRCMP(p1->f_name, p2->f_name);
8512}
8513
8514/*
8515 * Sort the function table by function name.
8516 * The sorting of the table above is ASCII dependant.
8517 * On machines using EBCDIC we have to sort it.
8518 */
8519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008520sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008521{
8522 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8523
8524 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8525}
8526#endif
8527
8528
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529/*
8530 * Find internal function in table above.
8531 * Return index, or -1 if not found
8532 */
8533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008534find_internal_func(
8535 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
8537 int first = 0;
8538 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8539 int cmp;
8540 int x;
8541
8542 /*
8543 * Find the function name in the table. Binary search.
8544 */
8545 while (first <= last)
8546 {
8547 x = first + ((unsigned)(last - first) >> 1);
8548 cmp = STRCMP(name, functions[x].f_name);
8549 if (cmp < 0)
8550 last = x - 1;
8551 else if (cmp > 0)
8552 first = x + 1;
8553 else
8554 return x;
8555 }
8556 return -1;
8557}
8558
8559/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008560 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8561 * name it contains, otherwise return "name".
8562 */
8563 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008564deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008565{
Bram Moolenaar33570922005-01-25 22:26:29 +00008566 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008567 int cc;
8568
8569 cc = name[*lenp];
8570 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008571 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008572 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008573 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008575 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008576 {
8577 *lenp = 0;
8578 return (char_u *)""; /* just in case */
8579 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008580 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008581 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008582 }
8583
8584 return name;
8585}
8586
8587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 * Allocate a variable for the result of a function.
8589 * Return OK or FAIL.
8590 */
8591 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008592get_func_tv(
8593 char_u *name, /* name of the function */
8594 int len, /* length of "name" */
8595 typval_T *rettv,
8596 char_u **arg, /* argument, pointing to the '(' */
8597 linenr_T firstline, /* first line of range */
8598 linenr_T lastline, /* last line of range */
8599 int *doesrange, /* return: function handled range */
8600 int evaluate,
8601 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603 char_u *argp;
8604 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008605 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 int argcount = 0; /* number of arguments found */
8607
8608 /*
8609 * Get the arguments.
8610 */
8611 argp = *arg;
8612 while (argcount < MAX_FUNC_ARGS)
8613 {
8614 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8615 if (*argp == ')' || *argp == ',' || *argp == NUL)
8616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8618 {
8619 ret = FAIL;
8620 break;
8621 }
8622 ++argcount;
8623 if (*argp != ',')
8624 break;
8625 }
8626 if (*argp == ')')
8627 ++argp;
8628 else
8629 ret = FAIL;
8630
8631 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008635 {
8636 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008637 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008638 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008639 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641
8642 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008643 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644
8645 *arg = skipwhite(argp);
8646 return ret;
8647}
8648
8649
8650/*
8651 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008652 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008653 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008655 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008656call_func(
8657 char_u *funcname, /* name of the function */
8658 int len, /* length of "name" */
8659 typval_T *rettv, /* return value goes here */
8660 int argcount, /* number of "argvars" */
8661 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008662 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008663 linenr_T firstline, /* first line of range */
8664 linenr_T lastline, /* last line of range */
8665 int *doesrange, /* return: function handled range */
8666 int evaluate,
8667 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668{
8669 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670#define ERROR_UNKNOWN 0
8671#define ERROR_TOOMANY 1
8672#define ERROR_TOOFEW 2
8673#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008674#define ERROR_DICT 4
8675#define ERROR_NONE 5
8676#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 int error = ERROR_NONE;
8678 int i;
8679 int llen;
8680 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681#define FLEN_FIXED 40
8682 char_u fname_buf[FLEN_FIXED + 1];
8683 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008684 char_u *name;
8685
8686 /* Make a copy of the name, if it comes from a funcref variable it could
8687 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008688 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008689 if (name == NULL)
8690 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691
8692 /*
8693 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8694 * Change <SNR>123_name() to K_SNR 123_name().
8695 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 llen = eval_fname_script(name);
8698 if (llen > 0)
8699 {
8700 fname_buf[0] = K_SPECIAL;
8701 fname_buf[1] = KS_EXTRA;
8702 fname_buf[2] = (int)KE_SNR;
8703 i = 3;
8704 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8705 {
8706 if (current_SID <= 0)
8707 error = ERROR_SCRIPT;
8708 else
8709 {
8710 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8711 i = (int)STRLEN(fname_buf);
8712 }
8713 }
8714 if (i + STRLEN(name + llen) < FLEN_FIXED)
8715 {
8716 STRCPY(fname_buf + i, name + llen);
8717 fname = fname_buf;
8718 }
8719 else
8720 {
8721 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8722 if (fname == NULL)
8723 error = ERROR_OTHER;
8724 else
8725 {
8726 mch_memmove(fname, fname_buf, (size_t)i);
8727 STRCPY(fname + i, name + llen);
8728 }
8729 }
8730 }
8731 else
8732 fname = name;
8733
8734 *doesrange = FALSE;
8735
8736
8737 /* execute the function if no errors detected and executing */
8738 if (evaluate && error == ERROR_NONE)
8739 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008740 char_u *rfname = fname;
8741
8742 /* Ignore "g:" before a function name. */
8743 if (fname[0] == 'g' && fname[1] == ':')
8744 rfname = fname + 2;
8745
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008746 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8747 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 error = ERROR_UNKNOWN;
8749
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008750 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {
8752 /*
8753 * User defined function.
8754 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008755 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008756
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008758 /* Trigger FuncUndefined event, may load the function. */
8759 if (fp == NULL
8760 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008761 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008762 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008764 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008765 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 }
8767#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008768 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008769 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008770 {
8771 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008772 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008773 }
8774
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (fp != NULL)
8776 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008777 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008779 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008781 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008783 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008784 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 else
8786 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008787 int did_save_redo = FALSE;
8788
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 /*
8790 * Call the user function.
8791 * Save and restore search patterns, script variables and
8792 * redo buffer.
8793 */
8794 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008795#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008796 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008797#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008798 {
8799 saveRedobuff();
8800 did_save_redo = TRUE;
8801 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008802 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008804 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008805 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8806 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8807 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008808 /* Function was unreferenced while being used, free it
8809 * now. */
8810 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008811 if (did_save_redo)
8812 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 restore_search_patterns();
8814 error = ERROR_NONE;
8815 }
8816 }
8817 }
8818 else
8819 {
8820 /*
8821 * Find the function name in the table, call its implementation.
8822 */
8823 i = find_internal_func(fname);
8824 if (i >= 0)
8825 {
8826 if (argcount < functions[i].f_min_argc)
8827 error = ERROR_TOOFEW;
8828 else if (argcount > functions[i].f_max_argc)
8829 error = ERROR_TOOMANY;
8830 else
8831 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008832 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008833 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 error = ERROR_NONE;
8835 }
8836 }
8837 }
8838 /*
8839 * The function call (or "FuncUndefined" autocommand sequence) might
8840 * have been aborted by an error, an interrupt, or an explicitly thrown
8841 * exception that has not been caught so far. This situation can be
8842 * tested for by calling aborting(). For an error in an internal
8843 * function or for the "E132" error in call_user_func(), however, the
8844 * throw point at which the "force_abort" flag (temporarily reset by
8845 * emsg()) is normally updated has not been reached yet. We need to
8846 * update that flag first to make aborting() reliable.
8847 */
8848 update_force_abort();
8849 }
8850 if (error == ERROR_NONE)
8851 ret = OK;
8852
8853 /*
8854 * Report an error unless the argument evaluation or function call has been
8855 * cancelled due to an aborting error, an interrupt, or an exception.
8856 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008857 if (!aborting())
8858 {
8859 switch (error)
8860 {
8861 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008862 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008863 break;
8864 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008865 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008866 break;
8867 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008868 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008869 name);
8870 break;
8871 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008872 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008873 name);
8874 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008876 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008877 name);
8878 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008879 }
8880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 if (fname != name && fname != fname_buf)
8883 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008884 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
8886 return ret;
8887}
8888
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008889/*
8890 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008891 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008892 */
8893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008894emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008895{
8896 char_u *p;
8897
8898 if (*name == K_SPECIAL)
8899 p = concat_str((char_u *)"<SNR>", name + 3);
8900 else
8901 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008902 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008903 if (p != name)
8904 vim_free(p);
8905}
8906
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008907/*
8908 * Return TRUE for a non-zero Number and a non-empty String.
8909 */
8910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008911non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008912{
8913 return ((argvars[0].v_type == VAR_NUMBER
8914 && argvars[0].vval.v_number != 0)
8915 || (argvars[0].v_type == VAR_STRING
8916 && argvars[0].vval.v_string != NULL
8917 && *argvars[0].vval.v_string != NUL));
8918}
8919
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920/*********************************************
8921 * Implementation of the built-in functions
8922 */
8923
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008924#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008925static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008926
8927/*
8928 * Get the float value of "argvars[0]" into "f".
8929 * Returns FAIL when the argument is not a Number or Float.
8930 */
8931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008932get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008933{
8934 if (argvars[0].v_type == VAR_FLOAT)
8935 {
8936 *f = argvars[0].vval.v_float;
8937 return OK;
8938 }
8939 if (argvars[0].v_type == VAR_NUMBER)
8940 {
8941 *f = (float_T)argvars[0].vval.v_number;
8942 return OK;
8943 }
8944 EMSG(_("E808: Number or Float required"));
8945 return FAIL;
8946}
8947
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008948/*
8949 * "abs(expr)" function
8950 */
8951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008952f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008953{
8954 if (argvars[0].v_type == VAR_FLOAT)
8955 {
8956 rettv->v_type = VAR_FLOAT;
8957 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8958 }
8959 else
8960 {
8961 varnumber_T n;
8962 int error = FALSE;
8963
8964 n = get_tv_number_chk(&argvars[0], &error);
8965 if (error)
8966 rettv->vval.v_number = -1;
8967 else if (n > 0)
8968 rettv->vval.v_number = n;
8969 else
8970 rettv->vval.v_number = -n;
8971 }
8972}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008973
8974/*
8975 * "acos()" function
8976 */
8977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008978f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008979{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01008980 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008981
8982 rettv->v_type = VAR_FLOAT;
8983 if (get_float_arg(argvars, &f) == OK)
8984 rettv->vval.v_float = acos(f);
8985 else
8986 rettv->vval.v_float = 0.0;
8987}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008988#endif
8989
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008991 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 */
8993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008994f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995{
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008999 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009001 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009002 && !tv_check_lock(l->lv_lock,
9003 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009004 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009006 }
9007 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009008 EMSG(_(e_listreq));
9009}
9010
9011/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009012 * "alloc_fail(id, countdown, repeat)" function
9013 */
9014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009015f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009016{
9017 if (argvars[0].v_type != VAR_NUMBER
9018 || argvars[0].vval.v_number <= 0
9019 || argvars[1].v_type != VAR_NUMBER
9020 || argvars[1].vval.v_number < 0
9021 || argvars[2].v_type != VAR_NUMBER)
9022 EMSG(_(e_invarg));
9023 else
9024 {
9025 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009026 if (alloc_fail_id >= aid_last)
9027 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009028 alloc_fail_countdown = argvars[1].vval.v_number;
9029 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009030 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009031 }
9032}
9033
9034/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009035 * "and(expr, expr)" function
9036 */
9037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009038f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009039{
9040 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9041 & get_tv_number_chk(&argvars[1], NULL);
9042}
9043
9044/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009045 * "append(lnum, string/list)" function
9046 */
9047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009048f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009049{
9050 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009052 list_T *l = NULL;
9053 listitem_T *li = NULL;
9054 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009055 long added = 0;
9056
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009057 /* When coming here from Insert mode, sync undo, so that this can be
9058 * undone separately from what was previously inserted. */
9059 if (u_sync_once == 2)
9060 {
9061 u_sync_once = 1; /* notify that u_sync() was called */
9062 u_sync(TRUE);
9063 }
9064
Bram Moolenaar0d660222005-01-07 21:51:51 +00009065 lnum = get_tv_lnum(argvars);
9066 if (lnum >= 0
9067 && lnum <= curbuf->b_ml.ml_line_count
9068 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009069 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009070 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009071 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009072 l = argvars[1].vval.v_list;
9073 if (l == NULL)
9074 return;
9075 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009076 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009077 for (;;)
9078 {
9079 if (l == NULL)
9080 tv = &argvars[1]; /* append a string */
9081 else if (li == NULL)
9082 break; /* end of list */
9083 else
9084 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009085 line = get_tv_string_chk(tv);
9086 if (line == NULL) /* type error */
9087 {
9088 rettv->vval.v_number = 1; /* Failed */
9089 break;
9090 }
9091 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009092 ++added;
9093 if (l == NULL)
9094 break;
9095 li = li->li_next;
9096 }
9097
9098 appended_lines_mark(lnum, added);
9099 if (curwin->w_cursor.lnum > lnum)
9100 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009102 else
9103 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104}
9105
9106/*
9107 * "argc()" function
9108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009110f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113}
9114
9115/*
9116 * "argidx()" function
9117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009119f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122}
9123
9124/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009125 * "arglistid()" function
9126 */
9127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009128f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009129{
9130 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009131
9132 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009133 wp = find_tabwin(&argvars[0], &argvars[1]);
9134 if (wp != NULL)
9135 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009136}
9137
9138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 * "argv(nr)" function
9140 */
9141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009142f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144 int idx;
9145
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009146 if (argvars[0].v_type != VAR_UNKNOWN)
9147 {
9148 idx = get_tv_number_chk(&argvars[0], NULL);
9149 if (idx >= 0 && idx < ARGCOUNT)
9150 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9151 else
9152 rettv->vval.v_string = NULL;
9153 rettv->v_type = VAR_STRING;
9154 }
9155 else if (rettv_list_alloc(rettv) == OK)
9156 for (idx = 0; idx < ARGCOUNT; ++idx)
9157 list_append_string(rettv->vval.v_list,
9158 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159}
9160
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009161static void prepare_assert_error(garray_T*gap);
9162static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9163static void assert_error(garray_T *gap);
9164static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009165
9166/*
9167 * Prepare "gap" for an assert error and add the sourcing position.
9168 */
9169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009170prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009171{
9172 char buf[NUMBUFLEN];
9173
9174 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009175 if (sourcing_name != NULL)
9176 {
9177 ga_concat(gap, sourcing_name);
9178 if (sourcing_lnum > 0)
9179 ga_concat(gap, (char_u *)" ");
9180 }
9181 if (sourcing_lnum > 0)
9182 {
9183 sprintf(buf, "line %ld", (long)sourcing_lnum);
9184 ga_concat(gap, (char_u *)buf);
9185 }
9186 if (sourcing_name != NULL || sourcing_lnum > 0)
9187 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009188}
9189
9190/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009191 * Append "str" to "gap", escaping unprintable characters.
9192 * Changes NL to \n, CR to \r, etc.
9193 */
9194 static void
9195ga_concat_esc(garray_T *gap, char_u *str)
9196{
9197 char_u *p;
9198 char_u buf[NUMBUFLEN];
9199
9200 for (p = str; *p != NUL; ++p)
9201 switch (*p)
9202 {
9203 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9204 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9205 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9206 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9207 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9208 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9209 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9210 default:
9211 if (*p < ' ')
9212 {
9213 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9214 ga_concat(gap, buf);
9215 }
9216 else
9217 ga_append(gap, *p);
9218 break;
9219 }
9220}
9221
9222/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009223 * Fill "gap" with information about an assert error.
9224 */
9225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009226fill_assert_error(
9227 garray_T *gap,
9228 typval_T *opt_msg_tv,
9229 char_u *exp_str,
9230 typval_T *exp_tv,
9231 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009232{
9233 char_u numbuf[NUMBUFLEN];
9234 char_u *tofree;
9235
9236 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9237 {
9238 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9239 vim_free(tofree);
9240 }
9241 else
9242 {
9243 ga_concat(gap, (char_u *)"Expected ");
9244 if (exp_str == NULL)
9245 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009246 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009247 vim_free(tofree);
9248 }
9249 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009250 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009251 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009252 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009253 vim_free(tofree);
9254 }
9255}
Bram Moolenaar43345542015-11-29 17:35:35 +01009256
9257/*
9258 * Add an assert error to v:errors.
9259 */
9260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009261assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009262{
9263 struct vimvar *vp = &vimvars[VV_ERRORS];
9264
9265 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9266 /* Make sure v:errors is a list. */
9267 set_vim_var_list(VV_ERRORS, list_alloc());
9268 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9269}
9270
Bram Moolenaar43345542015-11-29 17:35:35 +01009271/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009272 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009273 */
9274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009275f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009276{
9277 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009278
9279 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9280 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009281 prepare_assert_error(&ga);
9282 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9283 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009284 ga_clear(&ga);
9285 }
9286}
9287
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009288/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009289 * "assert_exception(string[, msg])" function
9290 */
9291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009292f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009293{
9294 garray_T ga;
9295 char *error;
9296
9297 error = (char *)get_tv_string_chk(&argvars[0]);
9298 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9299 {
9300 prepare_assert_error(&ga);
9301 ga_concat(&ga, (char_u *)"v:exception is not set");
9302 assert_error(&ga);
9303 ga_clear(&ga);
9304 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009305 else if (error != NULL
9306 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009307 {
9308 prepare_assert_error(&ga);
9309 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9310 &vimvars[VV_EXCEPTION].vv_tv);
9311 assert_error(&ga);
9312 ga_clear(&ga);
9313 }
9314}
9315
9316/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009317 * "assert_fails(cmd [, error])" function
9318 */
9319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009320f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009321{
9322 char_u *cmd = get_tv_string_chk(&argvars[0]);
9323 garray_T ga;
9324
9325 called_emsg = FALSE;
9326 suppress_errthrow = TRUE;
9327 emsg_silent = TRUE;
9328 do_cmdline_cmd(cmd);
9329 if (!called_emsg)
9330 {
9331 prepare_assert_error(&ga);
9332 ga_concat(&ga, (char_u *)"command did not fail: ");
9333 ga_concat(&ga, cmd);
9334 assert_error(&ga);
9335 ga_clear(&ga);
9336 }
9337 else if (argvars[1].v_type != VAR_UNKNOWN)
9338 {
9339 char_u buf[NUMBUFLEN];
9340 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9341
9342 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9343 {
9344 prepare_assert_error(&ga);
9345 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9346 &vimvars[VV_ERRMSG].vv_tv);
9347 assert_error(&ga);
9348 ga_clear(&ga);
9349 }
9350 }
9351
9352 called_emsg = FALSE;
9353 suppress_errthrow = FALSE;
9354 emsg_silent = FALSE;
9355 emsg_on_display = FALSE;
9356 set_vim_var_string(VV_ERRMSG, NULL, 0);
9357}
9358
9359/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009360 * Common for assert_true() and assert_false().
9361 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009363assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009364{
9365 int error = FALSE;
9366 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009367
Bram Moolenaar37127922016-02-06 20:29:28 +01009368 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009369 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009370 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009371 if (argvars[0].v_type != VAR_NUMBER
9372 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9373 || error)
9374 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009375 prepare_assert_error(&ga);
9376 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009377 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009378 NULL, &argvars[0]);
9379 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009380 ga_clear(&ga);
9381 }
9382}
9383
9384/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009385 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009386 */
9387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009388f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009389{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009390 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009391}
9392
9393/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009394 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009395 */
9396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009397f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009398{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009399 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009400}
9401
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009402#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009403/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009404 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009405 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009407f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009408{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009409 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009410
9411 rettv->v_type = VAR_FLOAT;
9412 if (get_float_arg(argvars, &f) == OK)
9413 rettv->vval.v_float = asin(f);
9414 else
9415 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009416}
9417
9418/*
9419 * "atan()" function
9420 */
9421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009422f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009423{
9424 float_T f;
9425
9426 rettv->v_type = VAR_FLOAT;
9427 if (get_float_arg(argvars, &f) == OK)
9428 rettv->vval.v_float = atan(f);
9429 else
9430 rettv->vval.v_float = 0.0;
9431}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009432
9433/*
9434 * "atan2()" function
9435 */
9436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009437f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009438{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009439 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009440
9441 rettv->v_type = VAR_FLOAT;
9442 if (get_float_arg(argvars, &fx) == OK
9443 && get_float_arg(&argvars[1], &fy) == OK)
9444 rettv->vval.v_float = atan2(fx, fy);
9445 else
9446 rettv->vval.v_float = 0.0;
9447}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009448#endif
9449
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450/*
9451 * "browse(save, title, initdir, default)" function
9452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009454f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455{
9456#ifdef FEAT_BROWSE
9457 int save;
9458 char_u *title;
9459 char_u *initdir;
9460 char_u *defname;
9461 char_u buf[NUMBUFLEN];
9462 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009465 save = get_tv_number_chk(&argvars[0], &error);
9466 title = get_tv_string_chk(&argvars[1]);
9467 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9468 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 if (error || title == NULL || initdir == NULL || defname == NULL)
9471 rettv->vval.v_string = NULL;
9472 else
9473 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009474 do_browse(save ? BROWSE_SAVE : 0,
9475 title, defname, NULL, initdir, NULL, curbuf);
9476#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009478#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009479 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009480}
9481
9482/*
9483 * "browsedir(title, initdir)" function
9484 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009486f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009487{
9488#ifdef FEAT_BROWSE
9489 char_u *title;
9490 char_u *initdir;
9491 char_u buf[NUMBUFLEN];
9492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009493 title = get_tv_string_chk(&argvars[0]);
9494 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009496 if (title == NULL || initdir == NULL)
9497 rettv->vval.v_string = NULL;
9498 else
9499 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009500 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009504 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505}
9506
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009507static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009508
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509/*
9510 * Find a buffer by number or exact name.
9511 */
9512 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009513find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514{
9515 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009517 if (avar->v_type == VAR_NUMBER)
9518 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009519 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009521 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009522 if (buf == NULL)
9523 {
9524 /* No full path name match, try a match with a URL or a "nofile"
9525 * buffer, these don't use the full path. */
9526 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9527 if (buf->b_fname != NULL
9528 && (path_with_url(buf->b_fname)
9529#ifdef FEAT_QUICKFIX
9530 || bt_nofile(buf)
9531#endif
9532 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009533 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009534 break;
9535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 }
9537 return buf;
9538}
9539
9540/*
9541 * "bufexists(expr)" function
9542 */
9543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009544f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547}
9548
9549/*
9550 * "buflisted(expr)" function
9551 */
9552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009553f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
9555 buf_T *buf;
9556
9557 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559}
9560
9561/*
9562 * "bufloaded(expr)" function
9563 */
9564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009565f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566{
9567 buf_T *buf;
9568
9569 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009570 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571}
9572
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009573static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009574
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575/*
9576 * Get buffer by number or pattern.
9577 */
9578 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009579get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 int save_magic;
9583 char_u *save_cpo;
9584 buf_T *buf;
9585
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 if (tv->v_type == VAR_NUMBER)
9587 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009588 if (tv->v_type != VAR_STRING)
9589 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 if (name == NULL || *name == NUL)
9591 return curbuf;
9592 if (name[0] == '$' && name[1] == NUL)
9593 return lastbuf;
9594
9595 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9596 save_magic = p_magic;
9597 p_magic = TRUE;
9598 save_cpo = p_cpo;
9599 p_cpo = (char_u *)"";
9600
9601 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009602 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603
9604 p_magic = save_magic;
9605 p_cpo = save_cpo;
9606
9607 /* If not found, try expanding the name, like done for bufexists(). */
9608 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
9611 return buf;
9612}
9613
9614/*
9615 * "bufname(expr)" function
9616 */
9617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009618f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619{
9620 buf_T *buf;
9621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009624 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 --emsg_off;
9631}
9632
9633/*
9634 * "bufnr(expr)" function
9635 */
9636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009637f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638{
9639 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009640 int error = FALSE;
9641 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009645 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009646 --emsg_off;
9647
9648 /* If the buffer isn't found and the second argument is not zero create a
9649 * new buffer. */
9650 if (buf == NULL
9651 && argvars[1].v_type != VAR_UNKNOWN
9652 && get_tv_number_chk(&argvars[1], &error) != 0
9653 && !error
9654 && (name = get_tv_string_chk(&argvars[0])) != NULL
9655 && !error)
9656 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9657
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662}
9663
9664/*
9665 * "bufwinnr(nr)" function
9666 */
9667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009668f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
9670#ifdef FEAT_WINDOWS
9671 win_T *wp;
9672 int winnr = 0;
9673#endif
9674 buf_T *buf;
9675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009676 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009678 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679#ifdef FEAT_WINDOWS
9680 for (wp = firstwin; wp; wp = wp->w_next)
9681 {
9682 ++winnr;
9683 if (wp->w_buffer == buf)
9684 break;
9685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009686 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009688 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689#endif
9690 --emsg_off;
9691}
9692
9693/*
9694 * "byte2line(byte)" function
9695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009697f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698{
9699#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009700 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701#else
9702 long boff = 0;
9703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009704 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 (linenr_T)0, &boff);
9710#endif
9711}
9712
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009714byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009715{
9716#ifdef FEAT_MBYTE
9717 char_u *t;
9718#endif
9719 char_u *str;
9720 long idx;
9721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009722 str = get_tv_string_chk(&argvars[0]);
9723 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009724 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009725 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009726 return;
9727
9728#ifdef FEAT_MBYTE
9729 t = str;
9730 for ( ; idx > 0; idx--)
9731 {
9732 if (*t == NUL) /* EOL reached */
9733 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009734 if (enc_utf8 && comp)
9735 t += utf_ptr2len(t);
9736 else
9737 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009738 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009739 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009740#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009741 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009743#endif
9744}
9745
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009746/*
9747 * "byteidx()" function
9748 */
9749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009750f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009751{
9752 byteidx(argvars, rettv, FALSE);
9753}
9754
9755/*
9756 * "byteidxcomp()" function
9757 */
9758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009759f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009760{
9761 byteidx(argvars, rettv, TRUE);
9762}
9763
Bram Moolenaardb913952012-06-29 12:54:53 +02009764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009765func_call(
9766 char_u *name,
9767 typval_T *args,
9768 dict_T *selfdict,
9769 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009770{
9771 listitem_T *item;
9772 typval_T argv[MAX_FUNC_ARGS + 1];
9773 int argc = 0;
9774 int dummy;
9775 int r = 0;
9776
9777 for (item = args->vval.v_list->lv_first; item != NULL;
9778 item = item->li_next)
9779 {
9780 if (argc == MAX_FUNC_ARGS)
9781 {
9782 EMSG(_("E699: Too many arguments"));
9783 break;
9784 }
9785 /* Make a copy of each argument. This is needed to be able to set
9786 * v_lock to VAR_FIXED in the copy without changing the original list.
9787 */
9788 copy_tv(&item->li_tv, &argv[argc++]);
9789 }
9790
9791 if (item == NULL)
9792 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9793 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9794 &dummy, TRUE, selfdict);
9795
9796 /* Free the arguments. */
9797 while (argc > 0)
9798 clear_tv(&argv[--argc]);
9799
9800 return r;
9801}
9802
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009803/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009804 * "call(func, arglist)" function
9805 */
9806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009807f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009808{
9809 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009810 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009811
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009812 if (argvars[1].v_type != VAR_LIST)
9813 {
9814 EMSG(_(e_listreq));
9815 return;
9816 }
9817 if (argvars[1].vval.v_list == NULL)
9818 return;
9819
9820 if (argvars[0].v_type == VAR_FUNC)
9821 func = argvars[0].vval.v_string;
9822 else
9823 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 if (*func == NUL)
9825 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009826
Bram Moolenaare9a41262005-01-15 22:18:47 +00009827 if (argvars[2].v_type != VAR_UNKNOWN)
9828 {
9829 if (argvars[2].v_type != VAR_DICT)
9830 {
9831 EMSG(_(e_dictreq));
9832 return;
9833 }
9834 selfdict = argvars[2].vval.v_dict;
9835 }
9836
Bram Moolenaardb913952012-06-29 12:54:53 +02009837 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009838}
9839
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009840#ifdef FEAT_FLOAT
9841/*
9842 * "ceil({float})" function
9843 */
9844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009845f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009846{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009847 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009848
9849 rettv->v_type = VAR_FLOAT;
9850 if (get_float_arg(argvars, &f) == OK)
9851 rettv->vval.v_float = ceil(f);
9852 else
9853 rettv->vval.v_float = 0.0;
9854}
9855#endif
9856
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009857#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009858/*
9859 * Get a callback from "arg". It can be a Funcref or a function name.
9860 * When "arg" is zero return an empty string.
9861 * Return NULL for an invalid argument.
9862 */
9863 static char_u *
9864get_callback(typval_T *arg)
9865{
9866 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9867 return arg->vval.v_string;
9868 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9869 return (char_u *)"";
9870 EMSG(_("E999: Invalid callback argument"));
9871 return NULL;
9872}
9873
9874/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009875 * Get the option entries from the dict in "tv", parse them and put the result
9876 * in "opt".
9877 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009878 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009879 */
9880 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009881get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009882{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009883 typval_T *item;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009884 char_u *mode;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009885 dict_T *dict;
9886 int todo;
9887 hashitem_T *hi;
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009888
Bram Moolenaar8600ace2016-02-19 23:31:40 +01009889 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009890 if (tv->v_type == VAR_UNKNOWN)
9891 return OK;
9892 if (tv->v_type != VAR_DICT)
9893 {
9894 EMSG(_(e_invarg));
9895 return FAIL;
9896 }
9897 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009898 if (dict == NULL)
9899 return OK;
9900
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009901 todo = (int)dict->dv_hashtab.ht_used;
9902 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
9903 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009904 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009905 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009906
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009907 if (STRCMP(hi->hi_key, "mode") == 0)
9908 {
9909 if (!(supported & JO_MODE))
9910 break;
9911 opt->jo_set |= JO_MODE;
9912 mode = get_tv_string(item);
9913 if (STRCMP(mode, "nl") == 0)
9914 opt->jo_mode = MODE_NL;
9915 else if (STRCMP(mode, "raw") == 0)
9916 opt->jo_mode = MODE_RAW;
9917 else if (STRCMP(mode, "js") == 0)
9918 opt->jo_mode = MODE_JS;
9919 else if (STRCMP(mode, "json") == 0)
9920 opt->jo_mode = MODE_JSON;
9921 else
9922 {
9923 EMSG2(_(e_invarg2), mode);
9924 return FAIL;
9925 }
9926 }
9927 else if (STRCMP(hi->hi_key, "callback") == 0)
9928 {
9929 if (!(supported & JO_CALLBACK))
9930 break;
9931 opt->jo_set |= JO_CALLBACK;
9932 opt->jo_callback = get_callback(item);
9933 if (opt->jo_callback == NULL)
9934 {
9935 EMSG2(_(e_invarg2), "callback");
9936 return FAIL;
9937 }
9938 }
9939 else if (STRCMP(hi->hi_key, "waittime") == 0)
9940 {
9941 if (!(supported & JO_WAITTIME))
9942 break;
9943 opt->jo_set |= JO_WAITTIME;
9944 opt->jo_waittime = get_tv_number(item);
9945 }
9946 else if (STRCMP(hi->hi_key, "timeout") == 0)
9947 {
9948 if (!(supported & JO_TIMEOUT))
9949 break;
9950 opt->jo_set |= JO_TIMEOUT;
9951 opt->jo_timeout = get_tv_number(item);
9952 }
9953 else
9954 break;
9955 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009956 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009957 if (todo > 0)
9958 {
9959 EMSG2(_(e_invarg2), hi->hi_key);
9960 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009961 }
9962
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009963 return OK;
9964}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009965#endif
9966
9967#ifdef FEAT_CHANNEL
9968/*
9969 * Get the channel from the argument.
9970 * Returns NULL if the handle is invalid.
9971 */
9972 static channel_T *
9973get_channel_arg(typval_T *tv)
9974{
9975 channel_T *channel;
9976
9977 if (tv->v_type != VAR_CHANNEL)
9978 {
9979 EMSG2(_(e_invarg2), get_tv_string(tv));
9980 return NULL;
9981 }
9982 channel = tv->vval.v_channel;
9983
9984 if (channel == NULL || !channel_is_open(channel))
9985 {
9986 EMSG(_("E906: not an open channel"));
9987 return NULL;
9988 }
9989 return channel;
9990}
9991
9992/*
9993 * "ch_close()" function
9994 */
9995 static void
9996f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9997{
9998 channel_T *channel = get_channel_arg(&argvars[0]);
9999
10000 if (channel != NULL)
10001 channel_close(channel);
10002}
10003
10004/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010005 * "ch_log()" function
10006 */
10007 static void
10008f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10009{
10010 char_u *msg = get_tv_string(&argvars[0]);
10011 channel_T *channel = NULL;
10012
10013 if (argvars[1].v_type != VAR_UNKNOWN)
10014 channel = get_channel_arg(&argvars[1]);
10015
10016 ch_log(channel, (char *)msg);
10017}
10018
10019/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010020 * "ch_logfile()" function
10021 */
10022 static void
10023f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10024{
10025 char_u *fname;
10026 char_u *opt = (char_u *)"";
10027 char_u buf[NUMBUFLEN];
10028 FILE *file = NULL;
10029
10030 fname = get_tv_string(&argvars[0]);
10031 if (argvars[1].v_type == VAR_STRING)
10032 opt = get_tv_string_buf(&argvars[1], buf);
10033 if (*fname != NUL)
10034 {
10035 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10036 if (file == NULL)
10037 {
10038 EMSG2(_(e_notopen), fname);
10039 return;
10040 }
10041 }
10042 ch_logfile(file);
10043}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010044
10045/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010046 * "ch_open()" function
10047 */
10048 static void
10049f_ch_open(typval_T *argvars, typval_T *rettv)
10050{
10051 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010052 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010053 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010054 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010055 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010056 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010057
10058 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010059 rettv->v_type = VAR_CHANNEL;
10060 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010061
10062 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010063 if (argvars[1].v_type != VAR_UNKNOWN
10064 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010065 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010066 EMSG(_(e_invarg));
10067 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010068 }
10069
10070 /* parse address */
10071 p = vim_strchr(address, ':');
10072 if (p == NULL)
10073 {
10074 EMSG2(_(e_invarg2), address);
10075 return;
10076 }
10077 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010078 port = strtol((char *)p, &rest, 10);
10079 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010080 {
10081 p[-1] = ':';
10082 EMSG2(_(e_invarg2), address);
10083 return;
10084 }
10085
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010086 /* parse options */
10087 opt.jo_mode = MODE_JSON;
10088 opt.jo_callback = NULL;
10089 opt.jo_waittime = 0;
10090 opt.jo_timeout = 2000;
10091 if (get_job_options(&argvars[1], &opt,
10092 JO_MODE + JO_CALLBACK + JO_WAITTIME + JO_TIMEOUT) == FAIL)
10093 return;
10094 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010095 {
10096 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010097 return;
10098 }
10099
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010100 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010101 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010102 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010103 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010104 opt.jo_set = JO_ALL;
10105 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010106 }
10107}
10108
10109/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010110 * "ch_readraw()" function
10111 */
10112 static void
10113f_ch_readraw(typval_T *argvars, typval_T *rettv)
10114{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010115 channel_T *channel;
10116 int part;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010117
10118 /* return an empty string by default */
10119 rettv->v_type = VAR_STRING;
10120 rettv->vval.v_string = NULL;
10121
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010122 /* TODO: use timeout from the options */
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010123 /* TODO: read from stderr */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010124
Bram Moolenaar77073442016-02-13 23:23:53 +010010125 channel = get_channel_arg(&argvars[0]);
10126 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010127 {
10128 part = channel_part_read(channel);
10129 rettv->vval.v_string = channel_read_block(channel, part);
10130 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010131}
10132
10133/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010134 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010135 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010136 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010137 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010138 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010139 static channel_T *
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010140send_common(typval_T *argvars, char_u *text, int id, char *fun, int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010141{
Bram Moolenaar77073442016-02-13 23:23:53 +010010142 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010143 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010144 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010145
Bram Moolenaar77073442016-02-13 23:23:53 +010010146 channel = get_channel_arg(&argvars[0]);
10147 if (channel == NULL)
10148 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010149 part_send = channel_part_send(channel);
10150 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010151
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010152 opt.jo_callback = NULL;
10153 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10154 return NULL;
10155
Bram Moolenaara07fec92016-02-05 21:04:08 +010010156 /* Set the callback. An empty callback means no callback and not reading
10157 * the response. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010158 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010159 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010160
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010161 if (channel_send(channel, part_send, text, fun) == OK
10162 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010163 return channel;
10164 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010165}
10166
10167/*
10168 * "ch_sendexpr()" function
10169 */
10170 static void
10171f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10172{
10173 char_u *text;
10174 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010175 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010176 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010177 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010178 int part_send;
10179 int part_read;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010180
10181 /* return an empty string by default */
10182 rettv->v_type = VAR_STRING;
10183 rettv->vval.v_string = NULL;
10184
Bram Moolenaar77073442016-02-13 23:23:53 +010010185 channel = get_channel_arg(&argvars[0]);
10186 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010187 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010188 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010189
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010190 ch_mode = channel_get_mode(channel, part_send);
10191 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010192 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010193 EMSG(_("E912: cannot use ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010194 return;
10195 }
10196
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010197 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010198 text = json_encode_nr_expr(id, &argvars[1],
10199 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010200 if (text == NULL)
10201 return;
10202
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010203 channel = send_common(argvars, text, id, "sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010204 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010205 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010206 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010207 if (channel_read_json_block(channel, part_read, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010208 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010209 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010210
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010211 /* Move the item from the list and then change the type to
10212 * avoid the value being freed. */
10213 *rettv = list->lv_last->li_tv;
10214 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010215 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010216 }
10217 }
10218}
10219
10220/*
10221 * "ch_sendraw()" function
10222 */
10223 static void
10224f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10225{
10226 char_u buf[NUMBUFLEN];
10227 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010228 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010229 int part_read;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010230
10231 /* return an empty string by default */
10232 rettv->v_type = VAR_STRING;
10233 rettv->vval.v_string = NULL;
10234
10235 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010236 channel = send_common(argvars, text, 0, "sendraw", &part_read);
Bram Moolenaar77073442016-02-13 23:23:53 +010010237 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010238 rettv->vval.v_string = channel_read_block(channel, part_read);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010239}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010240
10241/*
10242 * "ch_setoptions()" function
10243 */
10244 static void
10245f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10246{
10247 channel_T *channel;
10248 jobopt_T opt;
10249
10250 channel = get_channel_arg(&argvars[0]);
10251 if (channel == NULL)
10252 return;
10253 if (get_job_options(&argvars[1], &opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010254 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010255 channel_set_options(channel, &opt);
10256}
10257
10258/*
10259 * "ch_status()" function
10260 */
10261 static void
10262f_ch_status(typval_T *argvars, typval_T *rettv)
10263{
10264 /* return an empty string by default */
10265 rettv->v_type = VAR_STRING;
10266
10267 if (argvars[0].v_type != VAR_CHANNEL)
10268 {
10269 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10270 rettv->vval.v_string = NULL;
10271 }
10272 else
10273 rettv->vval.v_string = vim_strsave(
10274 (char_u *)channel_status(argvars[0].vval.v_channel));
10275}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010276#endif
10277
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010278/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010279 * "changenr()" function
10280 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010282f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010283{
10284 rettv->vval.v_number = curbuf->b_u_seq_cur;
10285}
10286
10287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 * "char2nr(string)" function
10289 */
10290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010291f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292{
10293#ifdef FEAT_MBYTE
10294 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010295 {
10296 int utf8 = 0;
10297
10298 if (argvars[1].v_type != VAR_UNKNOWN)
10299 utf8 = get_tv_number_chk(&argvars[1], NULL);
10300
10301 if (utf8)
10302 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10303 else
10304 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 else
10307#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309}
10310
10311/*
10312 * "cindent(lnum)" function
10313 */
10314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010315f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316{
10317#ifdef FEAT_CINDENT
10318 pos_T pos;
10319 linenr_T lnum;
10320
10321 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010322 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10324 {
10325 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 curwin->w_cursor = pos;
10328 }
10329 else
10330#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332}
10333
10334/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010335 * "clearmatches()" function
10336 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010338f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010339{
10340#ifdef FEAT_SEARCH_EXTRA
10341 clear_matches(curwin);
10342#endif
10343}
10344
10345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 * "col(string)" function
10347 */
10348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010349f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350{
10351 colnr_T col = 0;
10352 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010353 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010355 fp = var2fpos(&argvars[0], FALSE, &fnum);
10356 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 {
10358 if (fp->col == MAXCOL)
10359 {
10360 /* '> can be MAXCOL, get the length of the line then */
10361 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010362 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 else
10364 col = MAXCOL;
10365 }
10366 else
10367 {
10368 col = fp->col + 1;
10369#ifdef FEAT_VIRTUALEDIT
10370 /* col(".") when the cursor is on the NUL at the end of the line
10371 * because of "coladd" can be seen as an extra column. */
10372 if (virtual_active() && fp == &curwin->w_cursor)
10373 {
10374 char_u *p = ml_get_cursor();
10375
10376 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10377 curwin->w_virtcol - curwin->w_cursor.coladd))
10378 {
10379# ifdef FEAT_MBYTE
10380 int l;
10381
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010382 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 col += l;
10384# else
10385 if (*p != NUL && p[1] == NUL)
10386 ++col;
10387# endif
10388 }
10389 }
10390#endif
10391 }
10392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394}
10395
Bram Moolenaar572cb562005-08-05 21:35:02 +000010396#if defined(FEAT_INS_EXPAND)
10397/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010398 * "complete()" function
10399 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010401f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010402{
10403 int startcol;
10404
10405 if ((State & INSERT) == 0)
10406 {
10407 EMSG(_("E785: complete() can only be used in Insert mode"));
10408 return;
10409 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010410
10411 /* Check for undo allowed here, because if something was already inserted
10412 * the line was already saved for undo and this check isn't done. */
10413 if (!undo_allowed())
10414 return;
10415
Bram Moolenaarade00832006-03-10 21:46:58 +000010416 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10417 {
10418 EMSG(_(e_invarg));
10419 return;
10420 }
10421
10422 startcol = get_tv_number_chk(&argvars[0], NULL);
10423 if (startcol <= 0)
10424 return;
10425
10426 set_completion(startcol - 1, argvars[1].vval.v_list);
10427}
10428
10429/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010430 * "complete_add()" function
10431 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010433f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010434{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010435 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010436}
10437
10438/*
10439 * "complete_check()" function
10440 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010442f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010443{
10444 int saved = RedrawingDisabled;
10445
10446 RedrawingDisabled = 0;
10447 ins_compl_check_keys(0);
10448 rettv->vval.v_number = compl_interrupted;
10449 RedrawingDisabled = saved;
10450}
10451#endif
10452
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453/*
10454 * "confirm(message, buttons[, default [, type]])" function
10455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010457f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458{
10459#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10460 char_u *message;
10461 char_u *buttons = NULL;
10462 char_u buf[NUMBUFLEN];
10463 char_u buf2[NUMBUFLEN];
10464 int def = 1;
10465 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 char_u *typestr;
10467 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 message = get_tv_string_chk(&argvars[0]);
10470 if (message == NULL)
10471 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010472 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10475 if (buttons == NULL)
10476 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010477 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010480 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10483 if (typestr == NULL)
10484 error = TRUE;
10485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010487 switch (TOUPPER_ASC(*typestr))
10488 {
10489 case 'E': type = VIM_ERROR; break;
10490 case 'Q': type = VIM_QUESTION; break;
10491 case 'I': type = VIM_INFO; break;
10492 case 'W': type = VIM_WARNING; break;
10493 case 'G': type = VIM_GENERIC; break;
10494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 }
10496 }
10497 }
10498 }
10499
10500 if (buttons == NULL || *buttons == NUL)
10501 buttons = (char_u *)_("&Ok");
10502
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010503 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010504 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010505 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506#endif
10507}
10508
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010509/*
10510 * "copy()" function
10511 */
10512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010513f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010514{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010515 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010516}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010518#ifdef FEAT_FLOAT
10519/*
10520 * "cos()" function
10521 */
10522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010523f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010524{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010525 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010526
10527 rettv->v_type = VAR_FLOAT;
10528 if (get_float_arg(argvars, &f) == OK)
10529 rettv->vval.v_float = cos(f);
10530 else
10531 rettv->vval.v_float = 0.0;
10532}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010533
10534/*
10535 * "cosh()" function
10536 */
10537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010538f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010539{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010540 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010541
10542 rettv->v_type = VAR_FLOAT;
10543 if (get_float_arg(argvars, &f) == OK)
10544 rettv->vval.v_float = cosh(f);
10545 else
10546 rettv->vval.v_float = 0.0;
10547}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010548#endif
10549
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010551 * "count()" function
10552 */
10553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010554f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010555{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010556 long n = 0;
10557 int ic = FALSE;
10558
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010560 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010561 listitem_T *li;
10562 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010564
Bram Moolenaare9a41262005-01-15 22:18:47 +000010565 if ((l = argvars[0].vval.v_list) != NULL)
10566 {
10567 li = l->lv_first;
10568 if (argvars[2].v_type != VAR_UNKNOWN)
10569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 int error = FALSE;
10571
10572 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 if (argvars[3].v_type != VAR_UNKNOWN)
10574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 idx = get_tv_number_chk(&argvars[3], &error);
10576 if (!error)
10577 {
10578 li = list_find(l, idx);
10579 if (li == NULL)
10580 EMSGN(_(e_listidx), idx);
10581 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 if (error)
10584 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585 }
10586
10587 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010588 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010589 ++n;
10590 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010591 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 else if (argvars[0].v_type == VAR_DICT)
10593 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010594 int todo;
10595 dict_T *d;
10596 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010598 if ((d = argvars[0].vval.v_dict) != NULL)
10599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010600 int error = FALSE;
10601
Bram Moolenaare9a41262005-01-15 22:18:47 +000010602 if (argvars[2].v_type != VAR_UNKNOWN)
10603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 if (argvars[3].v_type != VAR_UNKNOWN)
10606 EMSG(_(e_invarg));
10607 }
10608
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010609 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010610 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010611 {
10612 if (!HASHITEM_EMPTY(hi))
10613 {
10614 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010615 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010616 ++n;
10617 }
10618 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010619 }
10620 }
10621 else
10622 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010623 rettv->vval.v_number = n;
10624}
10625
10626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10628 *
10629 * Checks the existence of a cscope connection.
10630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010632f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633{
10634#ifdef FEAT_CSCOPE
10635 int num = 0;
10636 char_u *dbpath = NULL;
10637 char_u *prepend = NULL;
10638 char_u buf[NUMBUFLEN];
10639
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010640 if (argvars[0].v_type != VAR_UNKNOWN
10641 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643 num = (int)get_tv_number(&argvars[0]);
10644 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010645 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010646 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 }
10648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010649 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650#endif
10651}
10652
10653/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010654 * "cursor(lnum, col)" function, or
10655 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010657 * Moves the cursor to the specified line and column.
10658 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010661f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662{
10663 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010664#ifdef FEAT_VIRTUALEDIT
10665 long coladd = 0;
10666#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010667 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010669 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010670 if (argvars[1].v_type == VAR_UNKNOWN)
10671 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010672 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010673 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010674
Bram Moolenaar493c1782014-05-28 14:34:46 +020010675 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010676 {
10677 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010678 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010679 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010680 line = pos.lnum;
10681 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010682#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010683 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010684#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010685 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010686 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010687 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010688 set_curswant = FALSE;
10689 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010690 }
10691 else
10692 {
10693 line = get_tv_lnum(argvars);
10694 col = get_tv_number_chk(&argvars[1], NULL);
10695#ifdef FEAT_VIRTUALEDIT
10696 if (argvars[2].v_type != VAR_UNKNOWN)
10697 coladd = get_tv_number_chk(&argvars[2], NULL);
10698#endif
10699 }
10700 if (line < 0 || col < 0
10701#ifdef FEAT_VIRTUALEDIT
10702 || coladd < 0
10703#endif
10704 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 if (line > 0)
10707 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 if (col > 0)
10709 curwin->w_cursor.col = col - 1;
10710#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010711 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712#endif
10713
10714 /* Make sure the cursor is in a valid position. */
10715 check_cursor();
10716#ifdef FEAT_MBYTE
10717 /* Correct cursor for multi-byte character. */
10718 if (has_mbyte)
10719 mb_adjust_cursor();
10720#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010721
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010722 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010723 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724}
10725
10726/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010727 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 */
10729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010730f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010732 int noref = 0;
10733
10734 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010735 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010736 if (noref < 0 || noref > 1)
10737 EMSG(_(e_invarg));
10738 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010739 {
10740 current_copyID += COPYID_INC;
10741 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743}
10744
10745/*
10746 * "delete()" function
10747 */
10748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010749f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010751 char_u nbuf[NUMBUFLEN];
10752 char_u *name;
10753 char_u *flags;
10754
10755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010757 return;
10758
10759 name = get_tv_string(&argvars[0]);
10760 if (name == NULL || *name == NUL)
10761 {
10762 EMSG(_(e_invarg));
10763 return;
10764 }
10765
10766 if (argvars[1].v_type != VAR_UNKNOWN)
10767 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010769 flags = (char_u *)"";
10770
10771 if (*flags == NUL)
10772 /* delete a file */
10773 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10774 else if (STRCMP(flags, "d") == 0)
10775 /* delete an empty directory */
10776 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10777 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010778 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010779 rettv->vval.v_number = delete_recursive(name);
10780 else
10781 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782}
10783
10784/*
10785 * "did_filetype()" function
10786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010788f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789{
10790#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792#endif
10793}
10794
10795/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010796 * "diff_filler()" function
10797 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010799f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010800{
10801#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010803#endif
10804}
10805
10806/*
10807 * "diff_hlID()" function
10808 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010810f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010811{
10812#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010814 static linenr_T prev_lnum = 0;
10815 static int changedtick = 0;
10816 static int fnum = 0;
10817 static int change_start = 0;
10818 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010819 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010820 int filler_lines;
10821 int col;
10822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010823 if (lnum < 0) /* ignore type error in {lnum} arg */
10824 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010825 if (lnum != prev_lnum
10826 || changedtick != curbuf->b_changedtick
10827 || fnum != curbuf->b_fnum)
10828 {
10829 /* New line, buffer, change: need to get the values. */
10830 filler_lines = diff_check(curwin, lnum);
10831 if (filler_lines < 0)
10832 {
10833 if (filler_lines == -1)
10834 {
10835 change_start = MAXCOL;
10836 change_end = -1;
10837 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10838 hlID = HLF_ADD; /* added line */
10839 else
10840 hlID = HLF_CHD; /* changed line */
10841 }
10842 else
10843 hlID = HLF_ADD; /* added line */
10844 }
10845 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010846 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010847 prev_lnum = lnum;
10848 changedtick = curbuf->b_changedtick;
10849 fnum = curbuf->b_fnum;
10850 }
10851
10852 if (hlID == HLF_CHD || hlID == HLF_TXD)
10853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010854 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010855 if (col >= change_start && col <= change_end)
10856 hlID = HLF_TXD; /* changed text */
10857 else
10858 hlID = HLF_CHD; /* changed line */
10859 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010860 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010861#endif
10862}
10863
10864/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010865 * "disable_char_avail_for_testing({expr})" function
10866 */
10867 static void
10868f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10869{
10870 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10871}
10872
10873/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010874 * "empty({expr})" function
10875 */
10876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010877f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010878{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010879 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010880
10881 switch (argvars[0].v_type)
10882 {
10883 case VAR_STRING:
10884 case VAR_FUNC:
10885 n = argvars[0].vval.v_string == NULL
10886 || *argvars[0].vval.v_string == NUL;
10887 break;
10888 case VAR_NUMBER:
10889 n = argvars[0].vval.v_number == 0;
10890 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010891 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010892#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010893 n = argvars[0].vval.v_float == 0.0;
10894 break;
10895#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010896 case VAR_LIST:
10897 n = argvars[0].vval.v_list == NULL
10898 || argvars[0].vval.v_list->lv_first == NULL;
10899 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010900 case VAR_DICT:
10901 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010902 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010903 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010904 case VAR_SPECIAL:
10905 n = argvars[0].vval.v_number != VVAL_TRUE;
10906 break;
10907
Bram Moolenaar835dc632016-02-07 14:27:38 +010010908 case VAR_JOB:
10909#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010910 n = argvars[0].vval.v_job == NULL
10911 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10912 break;
10913#endif
10914 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010010915#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010916 n = argvars[0].vval.v_channel == NULL
10917 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010918 break;
10919#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010920 case VAR_UNKNOWN:
10921 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10922 n = TRUE;
10923 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010924 }
10925
10926 rettv->vval.v_number = n;
10927}
10928
10929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 * "escape({string}, {chars})" function
10931 */
10932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010933f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934{
10935 char_u buf[NUMBUFLEN];
10936
Bram Moolenaar758711c2005-02-02 23:11:38 +000010937 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10938 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940}
10941
10942/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010943 * "eval()" function
10944 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010946f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010947{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010948 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 s = get_tv_string_chk(&argvars[0]);
10951 if (s != NULL)
10952 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010953
Bram Moolenaar615b9972015-01-14 17:15:05 +010010954 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010955 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10956 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010957 if (p != NULL && !aborting())
10958 EMSG2(_(e_invexpr2), p);
10959 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010960 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010961 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010962 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010963 else if (*s != NUL)
10964 EMSG(_(e_trailing));
10965}
10966
10967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968 * "eventhandler()" function
10969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010971f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974}
10975
10976/*
10977 * "executable()" function
10978 */
10979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010980f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010982 char_u *name = get_tv_string(&argvars[0]);
10983
10984 /* Check in $PATH and also check directly if there is a directory name. */
10985 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10986 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010987}
10988
10989/*
10990 * "exepath()" function
10991 */
10992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010993f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010994{
10995 char_u *p = NULL;
10996
Bram Moolenaarb5971142015-03-21 17:32:19 +010010997 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010998 rettv->v_type = VAR_STRING;
10999 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000}
11001
11002/*
11003 * "exists()" function
11004 */
11005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011006f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007{
11008 char_u *p;
11009 char_u *name;
11010 int n = FALSE;
11011 int len = 0;
11012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 if (*p == '$') /* environment variable */
11015 {
11016 /* first try "normal" environment variables (fast) */
11017 if (mch_getenv(p + 1) != NULL)
11018 n = TRUE;
11019 else
11020 {
11021 /* try expanding things like $VIM and ${HOME} */
11022 p = expand_env_save(p);
11023 if (p != NULL && *p != '$')
11024 n = TRUE;
11025 vim_free(p);
11026 }
11027 }
11028 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011031 if (*skipwhite(p) != NUL)
11032 n = FALSE; /* trailing garbage */
11033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 else if (*p == '*') /* internal or user defined function */
11035 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011036 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 }
11038 else if (*p == ':')
11039 {
11040 n = cmd_exists(p + 1);
11041 }
11042 else if (*p == '#')
11043 {
11044#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011045 if (p[1] == '#')
11046 n = autocmd_supported(p + 2);
11047 else
11048 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049#endif
11050 }
11051 else /* internal variable */
11052 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011053 char_u *tofree;
11054 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011056 /* get_name_len() takes care of expanding curly braces */
11057 name = p;
11058 len = get_name_len(&p, &tofree, TRUE, FALSE);
11059 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011061 if (tofree != NULL)
11062 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011063 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011064 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011066 /* handle d.key, l[idx], f(expr) */
11067 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11068 if (n)
11069 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 }
11071 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011072 if (*p != NUL)
11073 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011075 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 }
11077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011078 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079}
11080
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011081#ifdef FEAT_FLOAT
11082/*
11083 * "exp()" function
11084 */
11085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011086f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011087{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011088 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011089
11090 rettv->v_type = VAR_FLOAT;
11091 if (get_float_arg(argvars, &f) == OK)
11092 rettv->vval.v_float = exp(f);
11093 else
11094 rettv->vval.v_float = 0.0;
11095}
11096#endif
11097
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098/*
11099 * "expand()" function
11100 */
11101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011102f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103{
11104 char_u *s;
11105 int len;
11106 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011107 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011110 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011113 if (argvars[1].v_type != VAR_UNKNOWN
11114 && argvars[2].v_type != VAR_UNKNOWN
11115 && get_tv_number_chk(&argvars[2], &error)
11116 && !error)
11117 {
11118 rettv->v_type = VAR_LIST;
11119 rettv->vval.v_list = NULL;
11120 }
11121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 if (*s == '%' || *s == '#' || *s == '<')
11124 {
11125 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011126 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011128 if (rettv->v_type == VAR_LIST)
11129 {
11130 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11131 list_append_string(rettv->vval.v_list, result, -1);
11132 else
11133 vim_free(result);
11134 }
11135 else
11136 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 }
11138 else
11139 {
11140 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011141 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011142 if (argvars[1].v_type != VAR_UNKNOWN
11143 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011144 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 if (!error)
11146 {
11147 ExpandInit(&xpc);
11148 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011149 if (p_wic)
11150 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011151 if (rettv->v_type == VAR_STRING)
11152 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11153 options, WILD_ALL);
11154 else if (rettv_list_alloc(rettv) != FAIL)
11155 {
11156 int i;
11157
11158 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11159 for (i = 0; i < xpc.xp_numfiles; i++)
11160 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11161 ExpandCleanup(&xpc);
11162 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011163 }
11164 else
11165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 }
11167}
11168
11169/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011170 * Go over all entries in "d2" and add them to "d1".
11171 * When "action" is "error" then a duplicate key is an error.
11172 * When "action" is "force" then a duplicate key is overwritten.
11173 * Otherwise duplicate keys are ignored ("action" is "keep").
11174 */
11175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011176dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011177{
11178 dictitem_T *di1;
11179 hashitem_T *hi2;
11180 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011181 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011182
11183 todo = (int)d2->dv_hashtab.ht_used;
11184 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11185 {
11186 if (!HASHITEM_EMPTY(hi2))
11187 {
11188 --todo;
11189 di1 = dict_find(d1, hi2->hi_key, -1);
11190 if (d1->dv_scope != 0)
11191 {
11192 /* Disallow replacing a builtin function in l: and g:.
11193 * Check the key to be valid when adding to any
11194 * scope. */
11195 if (d1->dv_scope == VAR_DEF_SCOPE
11196 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11197 && var_check_func_name(hi2->hi_key,
11198 di1 == NULL))
11199 break;
11200 if (!valid_varname(hi2->hi_key))
11201 break;
11202 }
11203 if (di1 == NULL)
11204 {
11205 di1 = dictitem_copy(HI2DI(hi2));
11206 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11207 dictitem_free(di1);
11208 }
11209 else if (*action == 'e')
11210 {
11211 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11212 break;
11213 }
11214 else if (*action == 'f' && HI2DI(hi2) != di1)
11215 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011216 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11217 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011218 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011219 clear_tv(&di1->di_tv);
11220 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11221 }
11222 }
11223 }
11224}
11225
11226/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011227 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011228 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011229 */
11230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011231f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011232{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011233 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011234
Bram Moolenaare9a41262005-01-15 22:18:47 +000011235 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011236 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011237 list_T *l1, *l2;
11238 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011240 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011241
Bram Moolenaare9a41262005-01-15 22:18:47 +000011242 l1 = argvars[0].vval.v_list;
11243 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011244 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011245 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011246 {
11247 if (argvars[2].v_type != VAR_UNKNOWN)
11248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011249 before = get_tv_number_chk(&argvars[2], &error);
11250 if (error)
11251 return; /* type error; errmsg already given */
11252
Bram Moolenaar758711c2005-02-02 23:11:38 +000011253 if (before == l1->lv_len)
11254 item = NULL;
11255 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011256 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011257 item = list_find(l1, before);
11258 if (item == NULL)
11259 {
11260 EMSGN(_(e_listidx), before);
11261 return;
11262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011263 }
11264 }
11265 else
11266 item = NULL;
11267 list_extend(l1, l2, item);
11268
Bram Moolenaare9a41262005-01-15 22:18:47 +000011269 copy_tv(&argvars[0], rettv);
11270 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011271 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011272 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11273 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011274 dict_T *d1, *d2;
11275 char_u *action;
11276 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011277
11278 d1 = argvars[0].vval.v_dict;
11279 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011280 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011281 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011282 {
11283 /* Check the third argument. */
11284 if (argvars[2].v_type != VAR_UNKNOWN)
11285 {
11286 static char *(av[]) = {"keep", "force", "error"};
11287
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011288 action = get_tv_string_chk(&argvars[2]);
11289 if (action == NULL)
11290 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011291 for (i = 0; i < 3; ++i)
11292 if (STRCMP(action, av[i]) == 0)
11293 break;
11294 if (i == 3)
11295 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011296 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011297 return;
11298 }
11299 }
11300 else
11301 action = (char_u *)"force";
11302
Bram Moolenaara9922d62013-05-30 13:01:18 +020011303 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011304
Bram Moolenaare9a41262005-01-15 22:18:47 +000011305 copy_tv(&argvars[0], rettv);
11306 }
11307 }
11308 else
11309 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011310}
11311
11312/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011313 * "feedkeys()" function
11314 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011316f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011317{
11318 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011319 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011320 char_u *keys, *flags;
11321 char_u nbuf[NUMBUFLEN];
11322 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011323 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011324 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011325
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011326 /* This is not allowed in the sandbox. If the commands would still be
11327 * executed in the sandbox it would be OK, but it probably happens later,
11328 * when "sandbox" is no longer set. */
11329 if (check_secure())
11330 return;
11331
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011332 keys = get_tv_string(&argvars[0]);
11333 if (*keys != NUL)
11334 {
11335 if (argvars[1].v_type != VAR_UNKNOWN)
11336 {
11337 flags = get_tv_string_buf(&argvars[1], nbuf);
11338 for ( ; *flags != NUL; ++flags)
11339 {
11340 switch (*flags)
11341 {
11342 case 'n': remap = FALSE; break;
11343 case 'm': remap = TRUE; break;
11344 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011345 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011346 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011347 }
11348 }
11349 }
11350
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011351 /* Need to escape K_SPECIAL and CSI before putting the string in the
11352 * typeahead buffer. */
11353 keys_esc = vim_strsave_escape_csi(keys);
11354 if (keys_esc != NULL)
11355 {
11356 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011357 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011358 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011359 if (vgetc_busy)
11360 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011361 if (execute)
11362 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011363 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011364 }
11365}
11366
11367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 * "filereadable()" function
11369 */
11370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011371f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011373 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 char_u *p;
11375 int n;
11376
Bram Moolenaarc236c162008-07-13 17:41:49 +000011377#ifndef O_NONBLOCK
11378# define O_NONBLOCK 0
11379#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011381 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11382 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 {
11384 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011385 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 }
11387 else
11388 n = FALSE;
11389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391}
11392
11393/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011394 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 * rights to write into.
11396 */
11397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011398f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011400 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401}
11402
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011403 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011404findfilendir(
11405 typval_T *argvars UNUSED,
11406 typval_T *rettv,
11407 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011408{
11409#ifdef FEAT_SEARCHPATH
11410 char_u *fname;
11411 char_u *fresult = NULL;
11412 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11413 char_u *p;
11414 char_u pathbuf[NUMBUFLEN];
11415 int count = 1;
11416 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011417 int error = FALSE;
11418#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011419
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011420 rettv->vval.v_string = NULL;
11421 rettv->v_type = VAR_STRING;
11422
11423#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011425
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011426 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011428 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11429 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011430 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431 else
11432 {
11433 if (*p != NUL)
11434 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011437 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011439 }
11440
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011441 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11442 error = TRUE;
11443
11444 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011446 do
11447 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011448 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011449 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011450 fresult = find_file_in_path_option(first ? fname : NULL,
11451 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011452 0, first, path,
11453 find_what,
11454 curbuf->b_ffname,
11455 find_what == FINDFILE_DIR
11456 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011458
11459 if (fresult != NULL && rettv->v_type == VAR_LIST)
11460 list_append_string(rettv->vval.v_list, fresult, -1);
11461
11462 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011464
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011465 if (rettv->v_type == VAR_STRING)
11466 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011467#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011468}
11469
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011470static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11471static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011472
11473/*
11474 * Implementation of map() and filter().
11475 */
11476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011477filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011478{
11479 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011480 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011481 listitem_T *li, *nli;
11482 list_T *l = NULL;
11483 dictitem_T *di;
11484 hashtab_T *ht;
11485 hashitem_T *hi;
11486 dict_T *d = NULL;
11487 typval_T save_val;
11488 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011489 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011490 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011491 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011492 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011493 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011494 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011495 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011496
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011498 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011499 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011500 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011501 return;
11502 }
11503 else if (argvars[0].v_type == VAR_DICT)
11504 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011505 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011506 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011507 return;
11508 }
11509 else
11510 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011511 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 return;
11513 }
11514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011515 expr = get_tv_string_buf_chk(&argvars[1], buf);
11516 /* On type errors, the preceding call has already displayed an error
11517 * message. Avoid a misleading error message for an empty string that
11518 * was not passed as argument. */
11519 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011521 prepare_vimvar(VV_VAL, &save_val);
11522 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011523
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011524 /* We reset "did_emsg" to be able to detect whether an error
11525 * occurred during evaluation of the expression. */
11526 save_did_emsg = did_emsg;
11527 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011528
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011529 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011530 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011532 vimvars[VV_KEY].vv_type = VAR_STRING;
11533
11534 ht = &d->dv_hashtab;
11535 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011536 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539 if (!HASHITEM_EMPTY(hi))
11540 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011541 int r;
11542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 --todo;
11544 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011545 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011546 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11547 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011548 break;
11549 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011550 r = filter_map_one(&di->di_tv, expr, map, &rem);
11551 clear_tv(&vimvars[VV_KEY].vv_tv);
11552 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011553 break;
11554 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011555 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011556 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11557 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011558 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011561 }
11562 }
11563 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564 }
11565 else
11566 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011567 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11568
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011569 for (li = l->lv_first; li != NULL; li = nli)
11570 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011571 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011572 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011574 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011575 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011576 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011577 break;
11578 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011579 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011580 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011581 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011582 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011583
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011584 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011585 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011586
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011587 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011588 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011589
11590 copy_tv(&argvars[0], rettv);
11591}
11592
11593 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011594filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011595{
Bram Moolenaar33570922005-01-25 22:26:29 +000011596 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011597 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011598 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011599
Bram Moolenaar33570922005-01-25 22:26:29 +000011600 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011601 s = expr;
11602 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011603 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011604 if (*s != NUL) /* check for trailing chars after expr */
11605 {
11606 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011607 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011608 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011609 }
11610 if (map)
11611 {
11612 /* map(): replace the list item value */
11613 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011614 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011615 *tv = rettv;
11616 }
11617 else
11618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011619 int error = FALSE;
11620
Bram Moolenaare9a41262005-01-15 22:18:47 +000011621 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011623 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 /* On type error, nothing has been removed; return FAIL to stop the
11625 * loop. The error message was given by get_tv_number_chk(). */
11626 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011627 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011628 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011629 retval = OK;
11630theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011631 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011632 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011633}
11634
11635/*
11636 * "filter()" function
11637 */
11638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011639f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011640{
11641 filter_map(argvars, rettv, FALSE);
11642}
11643
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011644/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645 * "finddir({fname}[, {path}[, {count}]])" function
11646 */
11647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011648f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011649{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011650 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011651}
11652
11653/*
11654 * "findfile({fname}[, {path}[, {count}]])" function
11655 */
11656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011657f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011658{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011659 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011660}
11661
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011662#ifdef FEAT_FLOAT
11663/*
11664 * "float2nr({float})" function
11665 */
11666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011667f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011668{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011669 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011670
11671 if (get_float_arg(argvars, &f) == OK)
11672 {
11673 if (f < -0x7fffffff)
11674 rettv->vval.v_number = -0x7fffffff;
11675 else if (f > 0x7fffffff)
11676 rettv->vval.v_number = 0x7fffffff;
11677 else
11678 rettv->vval.v_number = (varnumber_T)f;
11679 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011680}
11681
11682/*
11683 * "floor({float})" function
11684 */
11685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011686f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011687{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011688 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011689
11690 rettv->v_type = VAR_FLOAT;
11691 if (get_float_arg(argvars, &f) == OK)
11692 rettv->vval.v_float = floor(f);
11693 else
11694 rettv->vval.v_float = 0.0;
11695}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011696
11697/*
11698 * "fmod()" function
11699 */
11700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011701f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011702{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011703 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011704
11705 rettv->v_type = VAR_FLOAT;
11706 if (get_float_arg(argvars, &fx) == OK
11707 && get_float_arg(&argvars[1], &fy) == OK)
11708 rettv->vval.v_float = fmod(fx, fy);
11709 else
11710 rettv->vval.v_float = 0.0;
11711}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011712#endif
11713
Bram Moolenaar0d660222005-01-07 21:51:51 +000011714/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011715 * "fnameescape({string})" function
11716 */
11717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011718f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011719{
11720 rettv->vval.v_string = vim_strsave_fnameescape(
11721 get_tv_string(&argvars[0]), FALSE);
11722 rettv->v_type = VAR_STRING;
11723}
11724
11725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 * "fnamemodify({fname}, {mods})" function
11727 */
11728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011729f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730{
11731 char_u *fname;
11732 char_u *mods;
11733 int usedlen = 0;
11734 int len;
11735 char_u *fbuf = NULL;
11736 char_u buf[NUMBUFLEN];
11737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011738 fname = get_tv_string_chk(&argvars[0]);
11739 mods = get_tv_string_buf_chk(&argvars[1], buf);
11740 if (fname == NULL || mods == NULL)
11741 fname = NULL;
11742 else
11743 {
11744 len = (int)STRLEN(fname);
11745 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 vim_free(fbuf);
11754}
11755
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011756static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757
11758/*
11759 * "foldclosed()" function
11760 */
11761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011762foldclosed_both(
11763 typval_T *argvars UNUSED,
11764 typval_T *rettv,
11765 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766{
11767#ifdef FEAT_FOLDING
11768 linenr_T lnum;
11769 linenr_T first, last;
11770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011771 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11773 {
11774 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11775 {
11776 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 return;
11781 }
11782 }
11783#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785}
11786
11787/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011788 * "foldclosed()" function
11789 */
11790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011791f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011792{
11793 foldclosed_both(argvars, rettv, FALSE);
11794}
11795
11796/*
11797 * "foldclosedend()" function
11798 */
11799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011800f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011801{
11802 foldclosed_both(argvars, rettv, TRUE);
11803}
11804
11805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 * "foldlevel()" function
11807 */
11808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011809f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810{
11811#ifdef FEAT_FOLDING
11812 linenr_T lnum;
11813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011814 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818}
11819
11820/*
11821 * "foldtext()" function
11822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011824f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825{
11826#ifdef FEAT_FOLDING
11827 linenr_T lnum;
11828 char_u *s;
11829 char_u *r;
11830 int len;
11831 char *txt;
11832#endif
11833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011834 rettv->v_type = VAR_STRING;
11835 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011837 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11838 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11839 <= curbuf->b_ml.ml_line_count
11840 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 {
11842 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11844 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 {
11846 if (!linewhite(lnum))
11847 break;
11848 ++lnum;
11849 }
11850
11851 /* Find interesting text in this line. */
11852 s = skipwhite(ml_get(lnum));
11853 /* skip C comment-start */
11854 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011857 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011858 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011859 {
11860 s = skipwhite(ml_get(lnum + 1));
11861 if (*s == '*')
11862 s = skipwhite(s + 1);
11863 }
11864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 txt = _("+-%s%3ld lines: ");
11866 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011867 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 + 20 /* for %3ld */
11869 + STRLEN(s))); /* concatenated */
11870 if (r != NULL)
11871 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011872 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11873 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11874 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 len = (int)STRLEN(r);
11876 STRCAT(r, s);
11877 /* remove 'foldmarker' and 'commentstring' */
11878 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880 }
11881 }
11882#endif
11883}
11884
11885/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011886 * "foldtextresult(lnum)" function
11887 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011889f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011890{
11891#ifdef FEAT_FOLDING
11892 linenr_T lnum;
11893 char_u *text;
11894 char_u buf[51];
11895 foldinfo_T foldinfo;
11896 int fold_count;
11897#endif
11898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->v_type = VAR_STRING;
11900 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011901#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 /* treat illegal types and illegal string values for {lnum} the same */
11904 if (lnum < 0)
11905 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011906 fold_count = foldedCount(curwin, lnum, &foldinfo);
11907 if (fold_count > 0)
11908 {
11909 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11910 &foldinfo, buf);
11911 if (text == buf)
11912 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011914 }
11915#endif
11916}
11917
11918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919 * "foreground()" function
11920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011922f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924#ifdef FEAT_GUI
11925 if (gui.in_use)
11926 gui_mch_set_foreground();
11927#else
11928# ifdef WIN32
11929 win32_set_foreground();
11930# endif
11931#endif
11932}
11933
11934/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011935 * "function()" function
11936 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011938f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011939{
11940 char_u *s;
11941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011942 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011943 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011944 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011945 /* Don't check an autoload name for existence here. */
11946 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011947 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011948 else
11949 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011950 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011951 {
11952 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011953 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011954
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011955 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11956 * also be called from another script. Using trans_function_name()
11957 * would also work, but some plugins depend on the name being
11958 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011959 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011960 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011961 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011962 if (rettv->vval.v_string != NULL)
11963 {
11964 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011965 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011966 }
11967 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011968 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011969 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011970 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011971 }
11972}
11973
11974/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011975 * "garbagecollect()" function
11976 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011978f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011979{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011980 /* This is postponed until we are back at the toplevel, because we may be
11981 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11982 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011983
11984 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11985 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011986}
11987
11988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011989 * "get()" function
11990 */
11991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011992f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011993{
Bram Moolenaar33570922005-01-25 22:26:29 +000011994 listitem_T *li;
11995 list_T *l;
11996 dictitem_T *di;
11997 dict_T *d;
11998 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011999
Bram Moolenaare9a41262005-01-15 22:18:47 +000012000 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012001 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012002 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012003 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012004 int error = FALSE;
12005
12006 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12007 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012008 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012009 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012010 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012011 else if (argvars[0].v_type == VAR_DICT)
12012 {
12013 if ((d = argvars[0].vval.v_dict) != NULL)
12014 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012015 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012016 if (di != NULL)
12017 tv = &di->di_tv;
12018 }
12019 }
12020 else
12021 EMSG2(_(e_listdictarg), "get()");
12022
12023 if (tv == NULL)
12024 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012025 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012026 copy_tv(&argvars[2], rettv);
12027 }
12028 else
12029 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012030}
12031
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012032static 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 +000012033
12034/*
12035 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012036 * Return a range (from start to end) of lines in rettv from the specified
12037 * buffer.
12038 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012039 */
12040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012041get_buffer_lines(
12042 buf_T *buf,
12043 linenr_T start,
12044 linenr_T end,
12045 int retlist,
12046 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012047{
12048 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012049
Bram Moolenaar959a1432013-12-14 12:17:38 +010012050 rettv->v_type = VAR_STRING;
12051 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012052 if (retlist && rettv_list_alloc(rettv) == FAIL)
12053 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012054
12055 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12056 return;
12057
12058 if (!retlist)
12059 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012060 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12061 p = ml_get_buf(buf, start, FALSE);
12062 else
12063 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012064 rettv->vval.v_string = vim_strsave(p);
12065 }
12066 else
12067 {
12068 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012069 return;
12070
12071 if (start < 1)
12072 start = 1;
12073 if (end > buf->b_ml.ml_line_count)
12074 end = buf->b_ml.ml_line_count;
12075 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012076 if (list_append_string(rettv->vval.v_list,
12077 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012078 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012079 }
12080}
12081
12082/*
12083 * "getbufline()" function
12084 */
12085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012086f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012087{
12088 linenr_T lnum;
12089 linenr_T end;
12090 buf_T *buf;
12091
12092 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12093 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012094 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012095 --emsg_off;
12096
Bram Moolenaar661b1822005-07-28 22:36:45 +000012097 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012098 if (argvars[2].v_type == VAR_UNKNOWN)
12099 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012100 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012101 end = get_tv_lnum_buf(&argvars[2], buf);
12102
Bram Moolenaar342337a2005-07-21 21:11:17 +000012103 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012104}
12105
Bram Moolenaar0d660222005-01-07 21:51:51 +000012106/*
12107 * "getbufvar()" function
12108 */
12109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012110f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012111{
12112 buf_T *buf;
12113 buf_T *save_curbuf;
12114 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012115 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012116 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012117
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012118 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12119 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012120 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012121 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012122
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012123 rettv->v_type = VAR_STRING;
12124 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012125
12126 if (buf != NULL && varname != NULL)
12127 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012128 /* set curbuf to be our buf, temporarily */
12129 save_curbuf = curbuf;
12130 curbuf = buf;
12131
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012133 {
12134 if (get_option_tv(&varname, rettv, TRUE) == OK)
12135 done = TRUE;
12136 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012137 else if (STRCMP(varname, "changedtick") == 0)
12138 {
12139 rettv->v_type = VAR_NUMBER;
12140 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012141 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012142 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143 else
12144 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012145 /* Look up the variable. */
12146 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12147 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12148 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012149 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012150 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012151 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012152 done = TRUE;
12153 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012154 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012155
12156 /* restore previous notion of curbuf */
12157 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012158 }
12159
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012160 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12161 /* use the default value */
12162 copy_tv(&argvars[2], rettv);
12163
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164 --emsg_off;
12165}
12166
12167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 * "getchar()" function
12169 */
12170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012171f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172{
12173 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012174 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012176 /* Position the cursor. Needed after a message that ends in a space. */
12177 windgoto(msg_row, msg_col);
12178
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179 ++no_mapping;
12180 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012181 for (;;)
12182 {
12183 if (argvars[0].v_type == VAR_UNKNOWN)
12184 /* getchar(): blocking wait. */
12185 n = safe_vgetc();
12186 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12187 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012188 n = vpeekc_any();
12189 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012190 /* illegal argument or getchar(0) and no char avail: return zero */
12191 n = 0;
12192 else
12193 /* getchar(0) and char avail: return char */
12194 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012195
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012196 if (n == K_IGNORE)
12197 continue;
12198 break;
12199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 --no_mapping;
12201 --allow_keys;
12202
Bram Moolenaar219b8702006-11-01 14:32:36 +000012203 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12204 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12205 vimvars[VV_MOUSE_COL].vv_nr = 0;
12206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012207 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208 if (IS_SPECIAL(n) || mod_mask != 0)
12209 {
12210 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12211 int i = 0;
12212
12213 /* Turn a special key into three bytes, plus modifier. */
12214 if (mod_mask != 0)
12215 {
12216 temp[i++] = K_SPECIAL;
12217 temp[i++] = KS_MODIFIER;
12218 temp[i++] = mod_mask;
12219 }
12220 if (IS_SPECIAL(n))
12221 {
12222 temp[i++] = K_SPECIAL;
12223 temp[i++] = K_SECOND(n);
12224 temp[i++] = K_THIRD(n);
12225 }
12226#ifdef FEAT_MBYTE
12227 else if (has_mbyte)
12228 i += (*mb_char2bytes)(n, temp + i);
12229#endif
12230 else
12231 temp[i++] = n;
12232 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->v_type = VAR_STRING;
12234 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012235
12236#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012237 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012238 {
12239 int row = mouse_row;
12240 int col = mouse_col;
12241 win_T *win;
12242 linenr_T lnum;
12243# ifdef FEAT_WINDOWS
12244 win_T *wp;
12245# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012246 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012247
12248 if (row >= 0 && col >= 0)
12249 {
12250 /* Find the window at the mouse coordinates and compute the
12251 * text position. */
12252 win = mouse_find_win(&row, &col);
12253 (void)mouse_comp_pos(win, &row, &col, &lnum);
12254# ifdef FEAT_WINDOWS
12255 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012256 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012257# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012258 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012259 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12260 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12261 }
12262 }
12263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 }
12265}
12266
12267/*
12268 * "getcharmod()" function
12269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012271f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274}
12275
12276/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012277 * "getcharsearch()" function
12278 */
12279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012280f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012281{
12282 if (rettv_dict_alloc(rettv) != FAIL)
12283 {
12284 dict_T *dict = rettv->vval.v_dict;
12285
12286 dict_add_nr_str(dict, "char", 0L, last_csearch());
12287 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12288 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12289 }
12290}
12291
12292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 * "getcmdline()" function
12294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012296f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012298 rettv->v_type = VAR_STRING;
12299 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300}
12301
12302/*
12303 * "getcmdpos()" function
12304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012306f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309}
12310
12311/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012312 * "getcmdtype()" function
12313 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012315f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012316{
12317 rettv->v_type = VAR_STRING;
12318 rettv->vval.v_string = alloc(2);
12319 if (rettv->vval.v_string != NULL)
12320 {
12321 rettv->vval.v_string[0] = get_cmdline_type();
12322 rettv->vval.v_string[1] = NUL;
12323 }
12324}
12325
12326/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012327 * "getcmdwintype()" function
12328 */
12329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012330f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012331{
12332 rettv->v_type = VAR_STRING;
12333 rettv->vval.v_string = NULL;
12334#ifdef FEAT_CMDWIN
12335 rettv->vval.v_string = alloc(2);
12336 if (rettv->vval.v_string != NULL)
12337 {
12338 rettv->vval.v_string[0] = cmdwin_type;
12339 rettv->vval.v_string[1] = NUL;
12340 }
12341#endif
12342}
12343
12344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 * "getcwd()" function
12346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012348f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012350 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012351 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012353 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012354 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012355
12356 wp = find_tabwin(&argvars[0], &argvars[1]);
12357 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012359 if (wp->w_localdir != NULL)
12360 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12361 else if(globaldir != NULL)
12362 rettv->vval.v_string = vim_strsave(globaldir);
12363 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012364 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012365 cwd = alloc(MAXPATHL);
12366 if (cwd != NULL)
12367 {
12368 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12369 rettv->vval.v_string = vim_strsave(cwd);
12370 vim_free(cwd);
12371 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012372 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012373#ifdef BACKSLASH_IN_FILENAME
12374 if (rettv->vval.v_string != NULL)
12375 slash_adjust(rettv->vval.v_string);
12376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 }
12378}
12379
12380/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012381 * "getfontname()" function
12382 */
12383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012384f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012385{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012386 rettv->v_type = VAR_STRING;
12387 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012388#ifdef FEAT_GUI
12389 if (gui.in_use)
12390 {
12391 GuiFont font;
12392 char_u *name = NULL;
12393
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012394 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012395 {
12396 /* Get the "Normal" font. Either the name saved by
12397 * hl_set_font_name() or from the font ID. */
12398 font = gui.norm_font;
12399 name = hl_get_font_name();
12400 }
12401 else
12402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012403 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012404 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12405 return;
12406 font = gui_mch_get_font(name, FALSE);
12407 if (font == NOFONT)
12408 return; /* Invalid font name, return empty string. */
12409 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012410 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012411 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012412 gui_mch_free_font(font);
12413 }
12414#endif
12415}
12416
12417/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012418 * "getfperm({fname})" function
12419 */
12420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012421f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012422{
12423 char_u *fname;
12424 struct stat st;
12425 char_u *perm = NULL;
12426 char_u flags[] = "rwx";
12427 int i;
12428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012429 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012432 if (mch_stat((char *)fname, &st) >= 0)
12433 {
12434 perm = vim_strsave((char_u *)"---------");
12435 if (perm != NULL)
12436 {
12437 for (i = 0; i < 9; i++)
12438 {
12439 if (st.st_mode & (1 << (8 - i)))
12440 perm[i] = flags[i % 3];
12441 }
12442 }
12443 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012445}
12446
12447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448 * "getfsize({fname})" function
12449 */
12450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012451f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012452{
12453 char_u *fname;
12454 struct stat st;
12455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012456 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459
12460 if (mch_stat((char *)fname, &st) >= 0)
12461 {
12462 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012463 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012464 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012466 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012467
12468 /* non-perfect check for overflow */
12469 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12470 rettv->vval.v_number = -2;
12471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 }
12473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475}
12476
12477/*
12478 * "getftime({fname})" function
12479 */
12480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012481f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482{
12483 char_u *fname;
12484 struct stat st;
12485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487
12488 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012489 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492}
12493
12494/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012495 * "getftype({fname})" function
12496 */
12497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012498f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012499{
12500 char_u *fname;
12501 struct stat st;
12502 char_u *type = NULL;
12503 char *t;
12504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012505 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012507 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012508 if (mch_lstat((char *)fname, &st) >= 0)
12509 {
12510#ifdef S_ISREG
12511 if (S_ISREG(st.st_mode))
12512 t = "file";
12513 else if (S_ISDIR(st.st_mode))
12514 t = "dir";
12515# ifdef S_ISLNK
12516 else if (S_ISLNK(st.st_mode))
12517 t = "link";
12518# endif
12519# ifdef S_ISBLK
12520 else if (S_ISBLK(st.st_mode))
12521 t = "bdev";
12522# endif
12523# ifdef S_ISCHR
12524 else if (S_ISCHR(st.st_mode))
12525 t = "cdev";
12526# endif
12527# ifdef S_ISFIFO
12528 else if (S_ISFIFO(st.st_mode))
12529 t = "fifo";
12530# endif
12531# ifdef S_ISSOCK
12532 else if (S_ISSOCK(st.st_mode))
12533 t = "fifo";
12534# endif
12535 else
12536 t = "other";
12537#else
12538# ifdef S_IFMT
12539 switch (st.st_mode & S_IFMT)
12540 {
12541 case S_IFREG: t = "file"; break;
12542 case S_IFDIR: t = "dir"; break;
12543# ifdef S_IFLNK
12544 case S_IFLNK: t = "link"; break;
12545# endif
12546# ifdef S_IFBLK
12547 case S_IFBLK: t = "bdev"; break;
12548# endif
12549# ifdef S_IFCHR
12550 case S_IFCHR: t = "cdev"; break;
12551# endif
12552# ifdef S_IFIFO
12553 case S_IFIFO: t = "fifo"; break;
12554# endif
12555# ifdef S_IFSOCK
12556 case S_IFSOCK: t = "socket"; break;
12557# endif
12558 default: t = "other";
12559 }
12560# else
12561 if (mch_isdir(fname))
12562 t = "dir";
12563 else
12564 t = "file";
12565# endif
12566#endif
12567 type = vim_strsave((char_u *)t);
12568 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012569 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012570}
12571
12572/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012573 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012574 */
12575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012576f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012577{
12578 linenr_T lnum;
12579 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012580 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012581
12582 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012583 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012584 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012585 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012586 retlist = FALSE;
12587 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012588 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012589 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012590 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012591 retlist = TRUE;
12592 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012593
Bram Moolenaar342337a2005-07-21 21:11:17 +000012594 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012595}
12596
12597/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012598 * "getmatches()" function
12599 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012601f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012602{
12603#ifdef FEAT_SEARCH_EXTRA
12604 dict_T *dict;
12605 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012606 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012607
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012608 if (rettv_list_alloc(rettv) == OK)
12609 {
12610 while (cur != NULL)
12611 {
12612 dict = dict_alloc();
12613 if (dict == NULL)
12614 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012615 if (cur->match.regprog == NULL)
12616 {
12617 /* match added with matchaddpos() */
12618 for (i = 0; i < MAXPOSMATCH; ++i)
12619 {
12620 llpos_T *llpos;
12621 char buf[6];
12622 list_T *l;
12623
12624 llpos = &cur->pos.pos[i];
12625 if (llpos->lnum == 0)
12626 break;
12627 l = list_alloc();
12628 if (l == NULL)
12629 break;
12630 list_append_number(l, (varnumber_T)llpos->lnum);
12631 if (llpos->col > 0)
12632 {
12633 list_append_number(l, (varnumber_T)llpos->col);
12634 list_append_number(l, (varnumber_T)llpos->len);
12635 }
12636 sprintf(buf, "pos%d", i + 1);
12637 dict_add_list(dict, buf, l);
12638 }
12639 }
12640 else
12641 {
12642 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12643 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012644 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012645 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12646 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012647# ifdef FEAT_CONCEAL
12648 if (cur->conceal_char)
12649 {
12650 char_u buf[MB_MAXBYTES + 1];
12651
12652 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12653 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12654 }
12655# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012656 list_append_dict(rettv->vval.v_list, dict);
12657 cur = cur->next;
12658 }
12659 }
12660#endif
12661}
12662
12663/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012664 * "getpid()" function
12665 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012667f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012668{
12669 rettv->vval.v_number = mch_get_pid();
12670}
12671
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012672static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012673
12674/*
12675 * "getcurpos()" function
12676 */
12677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012678f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012679{
12680 getpos_both(argvars, rettv, TRUE);
12681}
12682
Bram Moolenaar18081e32008-02-20 19:11:07 +000012683/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012684 * "getpos(string)" function
12685 */
12686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012687f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012688{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012689 getpos_both(argvars, rettv, FALSE);
12690}
12691
12692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012693getpos_both(
12694 typval_T *argvars,
12695 typval_T *rettv,
12696 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012697{
Bram Moolenaara5525202006-03-02 22:52:09 +000012698 pos_T *fp;
12699 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012700 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012701
12702 if (rettv_list_alloc(rettv) == OK)
12703 {
12704 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012705 if (getcurpos)
12706 fp = &curwin->w_cursor;
12707 else
12708 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012709 if (fnum != -1)
12710 list_append_number(l, (varnumber_T)fnum);
12711 else
12712 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012713 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12714 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012715 list_append_number(l, (fp != NULL)
12716 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012717 : (varnumber_T)0);
12718 list_append_number(l,
12719#ifdef FEAT_VIRTUALEDIT
12720 (fp != NULL) ? (varnumber_T)fp->coladd :
12721#endif
12722 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012723 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012724 {
12725 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012726 list_append_number(l, curwin->w_curswant == MAXCOL ?
12727 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012728 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012729 }
12730 else
12731 rettv->vval.v_number = FALSE;
12732}
12733
12734/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012735 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012736 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012738f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012739{
12740#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012741 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012742#endif
12743
Bram Moolenaar2641f772005-03-25 21:58:17 +000012744#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012745 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012746 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012747 wp = NULL;
12748 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12749 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012750 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012751 if (wp == NULL)
12752 return;
12753 }
12754
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012755 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012756 }
12757#endif
12758}
12759
12760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761 * "getreg()" function
12762 */
12763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012764f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765{
12766 char_u *strregname;
12767 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012768 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012769 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012770 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012772 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012774 strregname = get_tv_string_chk(&argvars[0]);
12775 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012776 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012778 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012779 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12780 return_list = get_tv_number_chk(&argvars[2], &error);
12781 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012784 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012785
12786 if (error)
12787 return;
12788
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 regname = (strregname == NULL ? '"' : *strregname);
12790 if (regname == 0)
12791 regname = '"';
12792
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012793 if (return_list)
12794 {
12795 rettv->v_type = VAR_LIST;
12796 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12797 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012798 if (rettv->vval.v_list != NULL)
12799 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012800 }
12801 else
12802 {
12803 rettv->v_type = VAR_STRING;
12804 rettv->vval.v_string = get_reg_contents(regname,
12805 arg2 ? GREG_EXPR_SRC : 0);
12806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807}
12808
12809/*
12810 * "getregtype()" function
12811 */
12812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012813f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814{
12815 char_u *strregname;
12816 int regname;
12817 char_u buf[NUMBUFLEN + 2];
12818 long reglen = 0;
12819
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012820 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012821 {
12822 strregname = get_tv_string_chk(&argvars[0]);
12823 if (strregname == NULL) /* type error; errmsg already given */
12824 {
12825 rettv->v_type = VAR_STRING;
12826 rettv->vval.v_string = NULL;
12827 return;
12828 }
12829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 else
12831 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012832 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833
12834 regname = (strregname == NULL ? '"' : *strregname);
12835 if (regname == 0)
12836 regname = '"';
12837
12838 buf[0] = NUL;
12839 buf[1] = NUL;
12840 switch (get_reg_type(regname, &reglen))
12841 {
12842 case MLINE: buf[0] = 'V'; break;
12843 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 case MBLOCK:
12845 buf[0] = Ctrl_V;
12846 sprintf((char *)buf + 1, "%ld", reglen + 1);
12847 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->v_type = VAR_STRING;
12850 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851}
12852
12853/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012854 * "gettabvar()" function
12855 */
12856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012857f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012858{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012859 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012860 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012861 dictitem_T *v;
12862 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012863 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012864
12865 rettv->v_type = VAR_STRING;
12866 rettv->vval.v_string = NULL;
12867
12868 varname = get_tv_string_chk(&argvars[1]);
12869 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12870 if (tp != NULL && varname != NULL)
12871 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012872 /* Set tp to be our tabpage, temporarily. Also set the window to the
12873 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012874 if (switch_win(&oldcurwin, &oldtabpage,
12875 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012876 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012877 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012878 /* look up the variable */
12879 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12880 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12881 if (v != NULL)
12882 {
12883 copy_tv(&v->di_tv, rettv);
12884 done = TRUE;
12885 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012886 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012887
12888 /* restore previous notion of curwin */
12889 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012890 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012891
12892 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012893 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012894}
12895
12896/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012897 * "gettabwinvar()" function
12898 */
12899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012900f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012901{
12902 getwinvar(argvars, rettv, 1);
12903}
12904
12905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 * "getwinposx()" function
12907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012909f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912#ifdef FEAT_GUI
12913 if (gui.in_use)
12914 {
12915 int x, y;
12916
12917 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 }
12920#endif
12921}
12922
12923/*
12924 * "getwinposy()" function
12925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012927f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930#ifdef FEAT_GUI
12931 if (gui.in_use)
12932 {
12933 int x, y;
12934
12935 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012936 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 }
12938#endif
12939}
12940
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012941/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012942 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012943 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012944 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012945find_win_by_nr(
12946 typval_T *vp,
12947 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012948{
12949#ifdef FEAT_WINDOWS
12950 win_T *wp;
12951#endif
12952 int nr;
12953
12954 nr = get_tv_number_chk(vp, NULL);
12955
12956#ifdef FEAT_WINDOWS
12957 if (nr < 0)
12958 return NULL;
12959 if (nr == 0)
12960 return curwin;
12961
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012962 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12963 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012964 if (--nr <= 0)
12965 break;
12966 return wp;
12967#else
12968 if (nr == 0 || nr == 1)
12969 return curwin;
12970 return NULL;
12971#endif
12972}
12973
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012975 * Find window specified by "wvp" in tabpage "tvp".
12976 */
12977 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012978find_tabwin(
12979 typval_T *wvp, /* VAR_UNKNOWN for current window */
12980 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012981{
12982 win_T *wp = NULL;
12983 tabpage_T *tp = NULL;
12984 long n;
12985
12986 if (wvp->v_type != VAR_UNKNOWN)
12987 {
12988 if (tvp->v_type != VAR_UNKNOWN)
12989 {
12990 n = get_tv_number(tvp);
12991 if (n >= 0)
12992 tp = find_tabpage(n);
12993 }
12994 else
12995 tp = curtab;
12996
12997 if (tp != NULL)
12998 wp = find_win_by_nr(wvp, tp);
12999 }
13000 else
13001 wp = curwin;
13002
13003 return wp;
13004}
13005
13006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 * "getwinvar()" function
13008 */
13009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013010f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013012 getwinvar(argvars, rettv, 0);
13013}
13014
13015/*
13016 * getwinvar() and gettabwinvar()
13017 */
13018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013019getwinvar(
13020 typval_T *argvars,
13021 typval_T *rettv,
13022 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013023{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013024 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013026 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013027 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013028 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013029#ifdef FEAT_WINDOWS
13030 win_T *oldcurwin;
13031 tabpage_T *oldtabpage;
13032 int need_switch_win;
13033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013035#ifdef FEAT_WINDOWS
13036 if (off == 1)
13037 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13038 else
13039 tp = curtab;
13040#endif
13041 win = find_win_by_nr(&argvars[off], tp);
13042 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013043 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013045 rettv->v_type = VAR_STRING;
13046 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047
13048 if (win != NULL && varname != NULL)
13049 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013050#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013051 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013052 * otherwise the window is not valid. Only do this when needed,
13053 * autocommands get blocked. */
13054 need_switch_win = !(tp == curtab && win == curwin);
13055 if (!need_switch_win
13056 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13057#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013058 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013059 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013060 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013061 if (get_option_tv(&varname, rettv, 1) == OK)
13062 done = TRUE;
13063 }
13064 else
13065 {
13066 /* Look up the variable. */
13067 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13068 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13069 varname, FALSE);
13070 if (v != NULL)
13071 {
13072 copy_tv(&v->di_tv, rettv);
13073 done = TRUE;
13074 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013077
Bram Moolenaarba117c22015-09-29 16:53:22 +020013078#ifdef FEAT_WINDOWS
13079 if (need_switch_win)
13080 /* restore previous notion of curwin */
13081 restore_win(oldcurwin, oldtabpage, TRUE);
13082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 }
13084
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013085 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13086 /* use the default return value */
13087 copy_tv(&argvars[off + 2], rettv);
13088
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 --emsg_off;
13090}
13091
13092/*
13093 * "glob()" function
13094 */
13095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013096f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013098 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013100 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013102 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013103 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013104 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013105 if (argvars[1].v_type != VAR_UNKNOWN)
13106 {
13107 if (get_tv_number_chk(&argvars[1], &error))
13108 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013110 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013111 if (get_tv_number_chk(&argvars[2], &error))
13112 {
13113 rettv->v_type = VAR_LIST;
13114 rettv->vval.v_list = NULL;
13115 }
13116 if (argvars[3].v_type != VAR_UNKNOWN
13117 && get_tv_number_chk(&argvars[3], &error))
13118 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013119 }
13120 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013121 if (!error)
13122 {
13123 ExpandInit(&xpc);
13124 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013125 if (p_wic)
13126 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013127 if (rettv->v_type == VAR_STRING)
13128 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013129 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013130 else if (rettv_list_alloc(rettv) != FAIL)
13131 {
13132 int i;
13133
13134 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13135 NULL, options, WILD_ALL_KEEP);
13136 for (i = 0; i < xpc.xp_numfiles; i++)
13137 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13138
13139 ExpandCleanup(&xpc);
13140 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013141 }
13142 else
13143 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013144}
13145
13146/*
13147 * "globpath()" function
13148 */
13149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013150f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013152 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013154 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013155 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013156 garray_T ga;
13157 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013159 /* When the optional second argument is non-zero, don't remove matches
13160 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013162 if (argvars[2].v_type != VAR_UNKNOWN)
13163 {
13164 if (get_tv_number_chk(&argvars[2], &error))
13165 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013166 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013167 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013168 if (get_tv_number_chk(&argvars[3], &error))
13169 {
13170 rettv->v_type = VAR_LIST;
13171 rettv->vval.v_list = NULL;
13172 }
13173 if (argvars[4].v_type != VAR_UNKNOWN
13174 && get_tv_number_chk(&argvars[4], &error))
13175 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013176 }
13177 }
13178 if (file != NULL && !error)
13179 {
13180 ga_init2(&ga, (int)sizeof(char_u *), 10);
13181 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13182 if (rettv->v_type == VAR_STRING)
13183 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13184 else if (rettv_list_alloc(rettv) != FAIL)
13185 for (i = 0; i < ga.ga_len; ++i)
13186 list_append_string(rettv->vval.v_list,
13187 ((char_u **)(ga.ga_data))[i], -1);
13188 ga_clear_strings(&ga);
13189 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013190 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013191 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192}
13193
13194/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013195 * "glob2regpat()" function
13196 */
13197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013198f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013199{
13200 char_u *pat = get_tv_string_chk(&argvars[0]);
13201
13202 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013203 rettv->vval.v_string = (pat == NULL)
13204 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013205}
13206
13207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208 * "has()" function
13209 */
13210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013211f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212{
13213 int i;
13214 char_u *name;
13215 int n = FALSE;
13216 static char *(has_list[]) =
13217 {
13218#ifdef AMIGA
13219 "amiga",
13220# ifdef FEAT_ARP
13221 "arp",
13222# endif
13223#endif
13224#ifdef __BEOS__
13225 "beos",
13226#endif
13227#ifdef MSDOS
13228# ifdef DJGPP
13229 "dos32",
13230# else
13231 "dos16",
13232# endif
13233#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013234#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 "mac",
13236#endif
13237#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013238 "macunix", /* built with 'darwin' enabled */
13239#endif
13240#if defined(__APPLE__) && __APPLE__ == 1
13241 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243#ifdef __QNX__
13244 "qnx",
13245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246#ifdef UNIX
13247 "unix",
13248#endif
13249#ifdef VMS
13250 "vms",
13251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252#ifdef WIN32
13253 "win32",
13254#endif
13255#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13256 "win32unix",
13257#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013258#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 "win64",
13260#endif
13261#ifdef EBCDIC
13262 "ebcdic",
13263#endif
13264#ifndef CASE_INSENSITIVE_FILENAME
13265 "fname_case",
13266#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013267#ifdef HAVE_ACL
13268 "acl",
13269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270#ifdef FEAT_ARABIC
13271 "arabic",
13272#endif
13273#ifdef FEAT_AUTOCMD
13274 "autocmd",
13275#endif
13276#ifdef FEAT_BEVAL
13277 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013278# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13279 "balloon_multiline",
13280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281#endif
13282#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13283 "builtin_terms",
13284# ifdef ALL_BUILTIN_TCAPS
13285 "all_builtin_terms",
13286# endif
13287#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013288#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13289 || defined(FEAT_GUI_W32) \
13290 || defined(FEAT_GUI_MOTIF))
13291 "browsefilter",
13292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293#ifdef FEAT_BYTEOFF
13294 "byte_offset",
13295#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013296#ifdef FEAT_CHANNEL
13297 "channel",
13298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299#ifdef FEAT_CINDENT
13300 "cindent",
13301#endif
13302#ifdef FEAT_CLIENTSERVER
13303 "clientserver",
13304#endif
13305#ifdef FEAT_CLIPBOARD
13306 "clipboard",
13307#endif
13308#ifdef FEAT_CMDL_COMPL
13309 "cmdline_compl",
13310#endif
13311#ifdef FEAT_CMDHIST
13312 "cmdline_hist",
13313#endif
13314#ifdef FEAT_COMMENTS
13315 "comments",
13316#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013317#ifdef FEAT_CONCEAL
13318 "conceal",
13319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320#ifdef FEAT_CRYPT
13321 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013322 "crypt-blowfish",
13323 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324#endif
13325#ifdef FEAT_CSCOPE
13326 "cscope",
13327#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013328#ifdef FEAT_CURSORBIND
13329 "cursorbind",
13330#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013331#ifdef CURSOR_SHAPE
13332 "cursorshape",
13333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334#ifdef DEBUG
13335 "debug",
13336#endif
13337#ifdef FEAT_CON_DIALOG
13338 "dialog_con",
13339#endif
13340#ifdef FEAT_GUI_DIALOG
13341 "dialog_gui",
13342#endif
13343#ifdef FEAT_DIFF
13344 "diff",
13345#endif
13346#ifdef FEAT_DIGRAPHS
13347 "digraphs",
13348#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013349#ifdef FEAT_DIRECTX
13350 "directx",
13351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352#ifdef FEAT_DND
13353 "dnd",
13354#endif
13355#ifdef FEAT_EMACS_TAGS
13356 "emacs_tags",
13357#endif
13358 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013359 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360#ifdef FEAT_SEARCH_EXTRA
13361 "extra_search",
13362#endif
13363#ifdef FEAT_FKMAP
13364 "farsi",
13365#endif
13366#ifdef FEAT_SEARCHPATH
13367 "file_in_path",
13368#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013369#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013370 "filterpipe",
13371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372#ifdef FEAT_FIND_ID
13373 "find_in_path",
13374#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013375#ifdef FEAT_FLOAT
13376 "float",
13377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378#ifdef FEAT_FOLDING
13379 "folding",
13380#endif
13381#ifdef FEAT_FOOTER
13382 "footer",
13383#endif
13384#if !defined(USE_SYSTEM) && defined(UNIX)
13385 "fork",
13386#endif
13387#ifdef FEAT_GETTEXT
13388 "gettext",
13389#endif
13390#ifdef FEAT_GUI
13391 "gui",
13392#endif
13393#ifdef FEAT_GUI_ATHENA
13394# ifdef FEAT_GUI_NEXTAW
13395 "gui_neXtaw",
13396# else
13397 "gui_athena",
13398# endif
13399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400#ifdef FEAT_GUI_GTK
13401 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013404#ifdef FEAT_GUI_GNOME
13405 "gui_gnome",
13406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407#ifdef FEAT_GUI_MAC
13408 "gui_mac",
13409#endif
13410#ifdef FEAT_GUI_MOTIF
13411 "gui_motif",
13412#endif
13413#ifdef FEAT_GUI_PHOTON
13414 "gui_photon",
13415#endif
13416#ifdef FEAT_GUI_W16
13417 "gui_win16",
13418#endif
13419#ifdef FEAT_GUI_W32
13420 "gui_win32",
13421#endif
13422#ifdef FEAT_HANGULIN
13423 "hangul_input",
13424#endif
13425#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13426 "iconv",
13427#endif
13428#ifdef FEAT_INS_EXPAND
13429 "insert_expand",
13430#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013431#ifdef FEAT_JOB
13432 "job",
13433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434#ifdef FEAT_JUMPLIST
13435 "jumplist",
13436#endif
13437#ifdef FEAT_KEYMAP
13438 "keymap",
13439#endif
13440#ifdef FEAT_LANGMAP
13441 "langmap",
13442#endif
13443#ifdef FEAT_LIBCALL
13444 "libcall",
13445#endif
13446#ifdef FEAT_LINEBREAK
13447 "linebreak",
13448#endif
13449#ifdef FEAT_LISP
13450 "lispindent",
13451#endif
13452#ifdef FEAT_LISTCMDS
13453 "listcmds",
13454#endif
13455#ifdef FEAT_LOCALMAP
13456 "localmap",
13457#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013458#ifdef FEAT_LUA
13459# ifndef DYNAMIC_LUA
13460 "lua",
13461# endif
13462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463#ifdef FEAT_MENU
13464 "menu",
13465#endif
13466#ifdef FEAT_SESSION
13467 "mksession",
13468#endif
13469#ifdef FEAT_MODIFY_FNAME
13470 "modify_fname",
13471#endif
13472#ifdef FEAT_MOUSE
13473 "mouse",
13474#endif
13475#ifdef FEAT_MOUSESHAPE
13476 "mouseshape",
13477#endif
13478#if defined(UNIX) || defined(VMS)
13479# ifdef FEAT_MOUSE_DEC
13480 "mouse_dec",
13481# endif
13482# ifdef FEAT_MOUSE_GPM
13483 "mouse_gpm",
13484# endif
13485# ifdef FEAT_MOUSE_JSB
13486 "mouse_jsbterm",
13487# endif
13488# ifdef FEAT_MOUSE_NET
13489 "mouse_netterm",
13490# endif
13491# ifdef FEAT_MOUSE_PTERM
13492 "mouse_pterm",
13493# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013494# ifdef FEAT_MOUSE_SGR
13495 "mouse_sgr",
13496# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013497# ifdef FEAT_SYSMOUSE
13498 "mouse_sysmouse",
13499# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013500# ifdef FEAT_MOUSE_URXVT
13501 "mouse_urxvt",
13502# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503# ifdef FEAT_MOUSE_XTERM
13504 "mouse_xterm",
13505# endif
13506#endif
13507#ifdef FEAT_MBYTE
13508 "multi_byte",
13509#endif
13510#ifdef FEAT_MBYTE_IME
13511 "multi_byte_ime",
13512#endif
13513#ifdef FEAT_MULTI_LANG
13514 "multi_lang",
13515#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013516#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013517#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013518 "mzscheme",
13519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521#ifdef FEAT_OLE
13522 "ole",
13523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524#ifdef FEAT_PATH_EXTRA
13525 "path_extra",
13526#endif
13527#ifdef FEAT_PERL
13528#ifndef DYNAMIC_PERL
13529 "perl",
13530#endif
13531#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013532#ifdef FEAT_PERSISTENT_UNDO
13533 "persistent_undo",
13534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535#ifdef FEAT_PYTHON
13536#ifndef DYNAMIC_PYTHON
13537 "python",
13538#endif
13539#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013540#ifdef FEAT_PYTHON3
13541#ifndef DYNAMIC_PYTHON3
13542 "python3",
13543#endif
13544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545#ifdef FEAT_POSTSCRIPT
13546 "postscript",
13547#endif
13548#ifdef FEAT_PRINTER
13549 "printer",
13550#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013551#ifdef FEAT_PROFILE
13552 "profile",
13553#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013554#ifdef FEAT_RELTIME
13555 "reltime",
13556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557#ifdef FEAT_QUICKFIX
13558 "quickfix",
13559#endif
13560#ifdef FEAT_RIGHTLEFT
13561 "rightleft",
13562#endif
13563#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13564 "ruby",
13565#endif
13566#ifdef FEAT_SCROLLBIND
13567 "scrollbind",
13568#endif
13569#ifdef FEAT_CMDL_INFO
13570 "showcmd",
13571 "cmdline_info",
13572#endif
13573#ifdef FEAT_SIGNS
13574 "signs",
13575#endif
13576#ifdef FEAT_SMARTINDENT
13577 "smartindent",
13578#endif
13579#ifdef FEAT_SNIFF
13580 "sniff",
13581#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013582#ifdef STARTUPTIME
13583 "startuptime",
13584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585#ifdef FEAT_STL_OPT
13586 "statusline",
13587#endif
13588#ifdef FEAT_SUN_WORKSHOP
13589 "sun_workshop",
13590#endif
13591#ifdef FEAT_NETBEANS_INTG
13592 "netbeans_intg",
13593#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013594#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013595 "spell",
13596#endif
13597#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 "syntax",
13599#endif
13600#if defined(USE_SYSTEM) || !defined(UNIX)
13601 "system",
13602#endif
13603#ifdef FEAT_TAG_BINS
13604 "tag_binary",
13605#endif
13606#ifdef FEAT_TAG_OLDSTATIC
13607 "tag_old_static",
13608#endif
13609#ifdef FEAT_TAG_ANYWHITE
13610 "tag_any_white",
13611#endif
13612#ifdef FEAT_TCL
13613# ifndef DYNAMIC_TCL
13614 "tcl",
13615# endif
13616#endif
13617#ifdef TERMINFO
13618 "terminfo",
13619#endif
13620#ifdef FEAT_TERMRESPONSE
13621 "termresponse",
13622#endif
13623#ifdef FEAT_TEXTOBJ
13624 "textobjects",
13625#endif
13626#ifdef HAVE_TGETENT
13627 "tgetent",
13628#endif
13629#ifdef FEAT_TITLE
13630 "title",
13631#endif
13632#ifdef FEAT_TOOLBAR
13633 "toolbar",
13634#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013635#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13636 "unnamedplus",
13637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638#ifdef FEAT_USR_CMDS
13639 "user-commands", /* was accidentally included in 5.4 */
13640 "user_commands",
13641#endif
13642#ifdef FEAT_VIMINFO
13643 "viminfo",
13644#endif
13645#ifdef FEAT_VERTSPLIT
13646 "vertsplit",
13647#endif
13648#ifdef FEAT_VIRTUALEDIT
13649 "virtualedit",
13650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652#ifdef FEAT_VISUALEXTRA
13653 "visualextra",
13654#endif
13655#ifdef FEAT_VREPLACE
13656 "vreplace",
13657#endif
13658#ifdef FEAT_WILDIGN
13659 "wildignore",
13660#endif
13661#ifdef FEAT_WILDMENU
13662 "wildmenu",
13663#endif
13664#ifdef FEAT_WINDOWS
13665 "windows",
13666#endif
13667#ifdef FEAT_WAK
13668 "winaltkeys",
13669#endif
13670#ifdef FEAT_WRITEBACKUP
13671 "writebackup",
13672#endif
13673#ifdef FEAT_XIM
13674 "xim",
13675#endif
13676#ifdef FEAT_XFONTSET
13677 "xfontset",
13678#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013679#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013680 "xpm",
13681 "xpm_w32", /* for backward compatibility */
13682#else
13683# if defined(HAVE_XPM)
13684 "xpm",
13685# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687#ifdef USE_XSMP
13688 "xsmp",
13689#endif
13690#ifdef USE_XSMP_INTERACT
13691 "xsmp_interact",
13692#endif
13693#ifdef FEAT_XCLIPBOARD
13694 "xterm_clipboard",
13695#endif
13696#ifdef FEAT_XTERM_SAVE
13697 "xterm_save",
13698#endif
13699#if defined(UNIX) && defined(FEAT_X11)
13700 "X11",
13701#endif
13702 NULL
13703 };
13704
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013705 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 for (i = 0; has_list[i] != NULL; ++i)
13707 if (STRICMP(name, has_list[i]) == 0)
13708 {
13709 n = TRUE;
13710 break;
13711 }
13712
13713 if (n == FALSE)
13714 {
13715 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013716 {
13717 if (name[5] == '-'
13718 && STRLEN(name) > 11
13719 && vim_isdigit(name[6])
13720 && vim_isdigit(name[8])
13721 && vim_isdigit(name[10]))
13722 {
13723 int major = atoi((char *)name + 6);
13724 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013725
13726 /* Expect "patch-9.9.01234". */
13727 n = (major < VIM_VERSION_MAJOR
13728 || (major == VIM_VERSION_MAJOR
13729 && (minor < VIM_VERSION_MINOR
13730 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013731 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013732 }
13733 else
13734 n = has_patch(atoi((char *)name + 5));
13735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736 else if (STRICMP(name, "vim_starting") == 0)
13737 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013738#ifdef FEAT_MBYTE
13739 else if (STRICMP(name, "multi_byte_encoding") == 0)
13740 n = has_mbyte;
13741#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013742#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13743 else if (STRICMP(name, "balloon_multiline") == 0)
13744 n = multiline_balloon_available();
13745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746#ifdef DYNAMIC_TCL
13747 else if (STRICMP(name, "tcl") == 0)
13748 n = tcl_enabled(FALSE);
13749#endif
13750#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13751 else if (STRICMP(name, "iconv") == 0)
13752 n = iconv_enabled(FALSE);
13753#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013754#ifdef DYNAMIC_LUA
13755 else if (STRICMP(name, "lua") == 0)
13756 n = lua_enabled(FALSE);
13757#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013758#ifdef DYNAMIC_MZSCHEME
13759 else if (STRICMP(name, "mzscheme") == 0)
13760 n = mzscheme_enabled(FALSE);
13761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762#ifdef DYNAMIC_RUBY
13763 else if (STRICMP(name, "ruby") == 0)
13764 n = ruby_enabled(FALSE);
13765#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013766#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767#ifdef DYNAMIC_PYTHON
13768 else if (STRICMP(name, "python") == 0)
13769 n = python_enabled(FALSE);
13770#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013771#endif
13772#ifdef FEAT_PYTHON3
13773#ifdef DYNAMIC_PYTHON3
13774 else if (STRICMP(name, "python3") == 0)
13775 n = python3_enabled(FALSE);
13776#endif
13777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778#ifdef DYNAMIC_PERL
13779 else if (STRICMP(name, "perl") == 0)
13780 n = perl_enabled(FALSE);
13781#endif
13782#ifdef FEAT_GUI
13783 else if (STRICMP(name, "gui_running") == 0)
13784 n = (gui.in_use || gui.starting);
13785# ifdef FEAT_GUI_W32
13786 else if (STRICMP(name, "gui_win32s") == 0)
13787 n = gui_is_win32s();
13788# endif
13789# ifdef FEAT_BROWSE
13790 else if (STRICMP(name, "browse") == 0)
13791 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13792# endif
13793#endif
13794#ifdef FEAT_SYN_HL
13795 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013796 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797#endif
13798#if defined(WIN3264)
13799 else if (STRICMP(name, "win95") == 0)
13800 n = mch_windows95();
13801#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013802#ifdef FEAT_NETBEANS_INTG
13803 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013804 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806 }
13807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013808 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809}
13810
13811/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013812 * "has_key()" function
13813 */
13814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013815f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013816{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013817 if (argvars[0].v_type != VAR_DICT)
13818 {
13819 EMSG(_(e_dictreq));
13820 return;
13821 }
13822 if (argvars[0].vval.v_dict == NULL)
13823 return;
13824
13825 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013826 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013827}
13828
13829/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013830 * "haslocaldir()" function
13831 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013833f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013834{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013835 win_T *wp = NULL;
13836
13837 wp = find_tabwin(&argvars[0], &argvars[1]);
13838 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013839}
13840
13841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 * "hasmapto()" function
13843 */
13844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013845f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846{
13847 char_u *name;
13848 char_u *mode;
13849 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013850 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013852 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013853 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854 mode = (char_u *)"nvo";
13855 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013857 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013858 if (argvars[2].v_type != VAR_UNKNOWN)
13859 abbr = get_tv_number(&argvars[2]);
13860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861
Bram Moolenaar2c932302006-03-18 21:42:09 +000013862 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013863 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013865 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866}
13867
13868/*
13869 * "histadd()" function
13870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013872f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873{
13874#ifdef FEAT_CMDHIST
13875 int histype;
13876 char_u *str;
13877 char_u buf[NUMBUFLEN];
13878#endif
13879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013880 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 if (check_restricted() || check_secure())
13882 return;
13883#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013884 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13885 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 if (histype >= 0)
13887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013888 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 if (*str != NUL)
13890 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013891 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013893 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 return;
13895 }
13896 }
13897#endif
13898}
13899
13900/*
13901 * "histdel()" function
13902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013904f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905{
13906#ifdef FEAT_CMDHIST
13907 int n;
13908 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013909 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013911 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13912 if (str == NULL)
13913 n = 0;
13914 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013916 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013917 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013919 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 else
13922 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013923 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013924 get_tv_string_buf(&argvars[1], buf));
13925 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926#endif
13927}
13928
13929/*
13930 * "histget()" function
13931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013933f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934{
13935#ifdef FEAT_CMDHIST
13936 int type;
13937 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013938 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013940 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13941 if (str == NULL)
13942 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013944 {
13945 type = get_histtype(str);
13946 if (argvars[1].v_type == VAR_UNKNOWN)
13947 idx = get_history_idx(type);
13948 else
13949 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13950 /* -1 on type error */
13951 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013954 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013956 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957}
13958
13959/*
13960 * "histnr()" function
13961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013963f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964{
13965 int i;
13966
13967#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 char_u *history = get_tv_string_chk(&argvars[0]);
13969
13970 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 if (i >= HIST_CMD && i < HIST_COUNT)
13972 i = get_history_idx(i);
13973 else
13974#endif
13975 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013976 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977}
13978
13979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 * "highlightID(name)" function
13981 */
13982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013983f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986}
13987
13988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013989 * "highlight_exists()" function
13990 */
13991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013992f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013993{
13994 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13995}
13996
13997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998 * "hostname()" function
13999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014001f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002{
14003 char_u hostname[256];
14004
14005 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014006 rettv->v_type = VAR_STRING;
14007 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008}
14009
14010/*
14011 * iconv() function
14012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014014f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015{
14016#ifdef FEAT_MBYTE
14017 char_u buf1[NUMBUFLEN];
14018 char_u buf2[NUMBUFLEN];
14019 char_u *from, *to, *str;
14020 vimconv_T vimconv;
14021#endif
14022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 rettv->v_type = VAR_STRING;
14024 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025
14026#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014027 str = get_tv_string(&argvars[0]);
14028 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14029 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 vimconv.vc_type = CONV_NONE;
14031 convert_setup(&vimconv, from, to);
14032
14033 /* If the encodings are equal, no conversion needed. */
14034 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014035 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038
14039 convert_setup(&vimconv, NULL, NULL);
14040 vim_free(from);
14041 vim_free(to);
14042#endif
14043}
14044
14045/*
14046 * "indent()" function
14047 */
14048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014049f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050{
14051 linenr_T lnum;
14052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014053 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014055 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014057 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058}
14059
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014060/*
14061 * "index()" function
14062 */
14063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014064f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014065{
Bram Moolenaar33570922005-01-25 22:26:29 +000014066 list_T *l;
14067 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014068 long idx = 0;
14069 int ic = FALSE;
14070
14071 rettv->vval.v_number = -1;
14072 if (argvars[0].v_type != VAR_LIST)
14073 {
14074 EMSG(_(e_listreq));
14075 return;
14076 }
14077 l = argvars[0].vval.v_list;
14078 if (l != NULL)
14079 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014080 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014081 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014083 int error = FALSE;
14084
Bram Moolenaar758711c2005-02-02 23:11:38 +000014085 /* Start at specified item. Use the cached index that list_find()
14086 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014087 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014088 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014089 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014090 ic = get_tv_number_chk(&argvars[3], &error);
14091 if (error)
14092 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014093 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014094
Bram Moolenaar758711c2005-02-02 23:11:38 +000014095 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014096 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014097 {
14098 rettv->vval.v_number = idx;
14099 break;
14100 }
14101 }
14102}
14103
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104static int inputsecret_flag = 0;
14105
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014106static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014107
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014109 * This function is used by f_input() and f_inputdialog() functions. The third
14110 * argument to f_input() specifies the type of completion to use at the
14111 * prompt. The third argument to f_inputdialog() specifies the value to return
14112 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113 */
14114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014115get_user_input(
14116 typval_T *argvars,
14117 typval_T *rettv,
14118 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 char_u *p = NULL;
14122 int c;
14123 char_u buf[NUMBUFLEN];
14124 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014125 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014126 int xp_type = EXPAND_NOTHING;
14127 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014130 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131
14132#ifdef NO_CONSOLE_INPUT
14133 /* While starting up, there is no place to enter text. */
14134 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136#endif
14137
14138 cmd_silent = FALSE; /* Want to see the prompt. */
14139 if (prompt != NULL)
14140 {
14141 /* Only the part of the message after the last NL is considered as
14142 * prompt for the command line */
14143 p = vim_strrchr(prompt, '\n');
14144 if (p == NULL)
14145 p = prompt;
14146 else
14147 {
14148 ++p;
14149 c = *p;
14150 *p = NUL;
14151 msg_start();
14152 msg_clr_eos();
14153 msg_puts_attr(prompt, echo_attr);
14154 msg_didout = FALSE;
14155 msg_starthere();
14156 *p = c;
14157 }
14158 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014160 if (argvars[1].v_type != VAR_UNKNOWN)
14161 {
14162 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14163 if (defstr != NULL)
14164 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014166 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014167 {
14168 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014169 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014170 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014171
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014172 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014173 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014174
Bram Moolenaar4463f292005-09-25 22:20:24 +000014175 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14176 if (xp_name == NULL)
14177 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014178
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014179 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014180
Bram Moolenaar4463f292005-09-25 22:20:24 +000014181 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14182 &xp_arg) == FAIL)
14183 return;
14184 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014185 }
14186
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014188 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014189 int save_ex_normal_busy = ex_normal_busy;
14190 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014192 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14193 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014194 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014195 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014196 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014197 && argvars[1].v_type != VAR_UNKNOWN
14198 && argvars[2].v_type != VAR_UNKNOWN)
14199 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14200 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014201
14202 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 /* since the user typed this, no need to wait for return */
14205 need_wait_return = FALSE;
14206 msg_didout = FALSE;
14207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 cmd_silent = cmd_silent_save;
14209}
14210
14211/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014212 * "input()" function
14213 * Also handles inputsecret() when inputsecret is set.
14214 */
14215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014216f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014217{
14218 get_user_input(argvars, rettv, FALSE);
14219}
14220
14221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222 * "inputdialog()" function
14223 */
14224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014225f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226{
14227#if defined(FEAT_GUI_TEXTDIALOG)
14228 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14229 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14230 {
14231 char_u *message;
14232 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014233 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 message = get_tv_string_chk(&argvars[0]);
14236 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014237 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014238 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 else
14240 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014241 if (message != NULL && defstr != NULL
14242 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014243 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 else
14246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014247 if (message != NULL && defstr != NULL
14248 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014249 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014250 rettv->vval.v_string = vim_strsave(
14251 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014253 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 }
14257 else
14258#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014259 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260}
14261
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014262/*
14263 * "inputlist()" function
14264 */
14265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014266f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014267{
14268 listitem_T *li;
14269 int selected;
14270 int mouse_used;
14271
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014272#ifdef NO_CONSOLE_INPUT
14273 /* While starting up, there is no place to enter text. */
14274 if (no_console_input())
14275 return;
14276#endif
14277 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14278 {
14279 EMSG2(_(e_listarg), "inputlist()");
14280 return;
14281 }
14282
14283 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014284 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014285 lines_left = Rows; /* avoid more prompt */
14286 msg_scroll = TRUE;
14287 msg_clr_eos();
14288
14289 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14290 {
14291 msg_puts(get_tv_string(&li->li_tv));
14292 msg_putchar('\n');
14293 }
14294
14295 /* Ask for choice. */
14296 selected = prompt_for_number(&mouse_used);
14297 if (mouse_used)
14298 selected -= lines_left;
14299
14300 rettv->vval.v_number = selected;
14301}
14302
14303
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14305
14306/*
14307 * "inputrestore()" function
14308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014310f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311{
14312 if (ga_userinput.ga_len > 0)
14313 {
14314 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14316 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014317 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 }
14319 else if (p_verbose > 1)
14320 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014321 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014322 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 }
14324}
14325
14326/*
14327 * "inputsave()" function
14328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014330f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014332 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 if (ga_grow(&ga_userinput, 1) == OK)
14334 {
14335 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14336 + ga_userinput.ga_len);
14337 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014338 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 }
14340 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014341 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342}
14343
14344/*
14345 * "inputsecret()" function
14346 */
14347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014348f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349{
14350 ++cmdline_star;
14351 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014352 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 --cmdline_star;
14354 --inputsecret_flag;
14355}
14356
14357/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014358 * "insert()" function
14359 */
14360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014361f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014362{
14363 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014364 listitem_T *item;
14365 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014367
14368 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014369 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014370 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014371 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014372 {
14373 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014374 before = get_tv_number_chk(&argvars[2], &error);
14375 if (error)
14376 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014377
Bram Moolenaar758711c2005-02-02 23:11:38 +000014378 if (before == l->lv_len)
14379 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014380 else
14381 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014382 item = list_find(l, before);
14383 if (item == NULL)
14384 {
14385 EMSGN(_(e_listidx), before);
14386 l = NULL;
14387 }
14388 }
14389 if (l != NULL)
14390 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014391 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014392 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014393 }
14394 }
14395}
14396
14397/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014398 * "invert(expr)" function
14399 */
14400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014401f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014402{
14403 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14404}
14405
14406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 * "isdirectory()" function
14408 */
14409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014410f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014412 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413}
14414
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014415/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014416 * "islocked()" function
14417 */
14418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014419f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014420{
14421 lval_T lv;
14422 char_u *end;
14423 dictitem_T *di;
14424
14425 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014426 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14427 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014428 if (end != NULL && lv.ll_name != NULL)
14429 {
14430 if (*end != NUL)
14431 EMSG(_(e_trailing));
14432 else
14433 {
14434 if (lv.ll_tv == NULL)
14435 {
14436 if (check_changedtick(lv.ll_name))
14437 rettv->vval.v_number = 1; /* always locked */
14438 else
14439 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014440 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014441 if (di != NULL)
14442 {
14443 /* Consider a variable locked when:
14444 * 1. the variable itself is locked
14445 * 2. the value of the variable is locked.
14446 * 3. the List or Dict value is locked.
14447 */
14448 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14449 || tv_islocked(&di->di_tv));
14450 }
14451 }
14452 }
14453 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014454 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014455 else if (lv.ll_newkey != NULL)
14456 EMSG2(_(e_dictkey), lv.ll_newkey);
14457 else if (lv.ll_list != NULL)
14458 /* List item. */
14459 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14460 else
14461 /* Dictionary item. */
14462 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14463 }
14464 }
14465
14466 clear_lval(&lv);
14467}
14468
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014469static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014470
14471/*
14472 * Turn a dict into a list:
14473 * "what" == 0: list of keys
14474 * "what" == 1: list of values
14475 * "what" == 2: list of items
14476 */
14477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014478dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014479{
Bram Moolenaar33570922005-01-25 22:26:29 +000014480 list_T *l2;
14481 dictitem_T *di;
14482 hashitem_T *hi;
14483 listitem_T *li;
14484 listitem_T *li2;
14485 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014486 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014487
Bram Moolenaar8c711452005-01-14 21:53:12 +000014488 if (argvars[0].v_type != VAR_DICT)
14489 {
14490 EMSG(_(e_dictreq));
14491 return;
14492 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014493 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014494 return;
14495
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014496 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014497 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014498
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014499 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014500 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014501 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014502 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014503 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014504 --todo;
14505 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014506
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014507 li = listitem_alloc();
14508 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014509 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014510 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014511
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014512 if (what == 0)
14513 {
14514 /* keys() */
14515 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014516 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014517 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14518 }
14519 else if (what == 1)
14520 {
14521 /* values() */
14522 copy_tv(&di->di_tv, &li->li_tv);
14523 }
14524 else
14525 {
14526 /* items() */
14527 l2 = list_alloc();
14528 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014529 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014530 li->li_tv.vval.v_list = l2;
14531 if (l2 == NULL)
14532 break;
14533 ++l2->lv_refcount;
14534
14535 li2 = listitem_alloc();
14536 if (li2 == NULL)
14537 break;
14538 list_append(l2, li2);
14539 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014540 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014541 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14542
14543 li2 = listitem_alloc();
14544 if (li2 == NULL)
14545 break;
14546 list_append(l2, li2);
14547 copy_tv(&di->di_tv, &li2->li_tv);
14548 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014549 }
14550 }
14551}
14552
14553/*
14554 * "items(dict)" function
14555 */
14556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014557f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014558{
14559 dict_list(argvars, rettv, 2);
14560}
14561
Bram Moolenaar835dc632016-02-07 14:27:38 +010014562#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014563
14564# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014565/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014566 * "job_getchannel()" function
14567 */
14568 static void
14569f_job_getchannel(typval_T *argvars, typval_T *rettv)
14570{
14571 if (argvars[0].v_type != VAR_JOB)
14572 EMSG(_(e_invarg));
14573 else
14574 {
14575 job_T *job = argvars[0].vval.v_job;
14576
Bram Moolenaar77073442016-02-13 23:23:53 +010014577 rettv->v_type = VAR_CHANNEL;
14578 rettv->vval.v_channel = job->jv_channel;
14579 if (job->jv_channel != NULL)
14580 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014581 }
14582}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014583# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014584
14585/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014586 * "job_start()" function
14587 */
14588 static void
14589f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14590{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014591 job_T *job;
14592 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014593#if defined(UNIX)
14594# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014595 char **argv = NULL;
14596 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014597#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014598 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014599#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014600 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014601
14602 rettv->v_type = VAR_JOB;
14603 job = job_alloc();
14604 rettv->vval.v_job = job;
14605 if (job == NULL)
14606 return;
14607
14608 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014609
14610 /* Default mode is NL. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014611 opt.jo_mode = MODE_NL;
14612 opt.jo_callback = NULL;
14613 if (get_job_options(&argvars[1], &opt, JO_MODE + JO_CALLBACK) == FAIL)
14614 return;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014615
Bram Moolenaar835dc632016-02-07 14:27:38 +010014616#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014617 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014618#endif
14619
14620 if (argvars[0].v_type == VAR_STRING)
14621 {
14622 /* Command is a string. */
14623 cmd = argvars[0].vval.v_string;
14624#ifdef USE_ARGV
14625 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14626 return;
14627 argv[argc] = NULL;
14628#endif
14629 }
14630 else if (argvars[0].v_type != VAR_LIST
14631 || argvars[0].vval.v_list == NULL
14632 || argvars[0].vval.v_list->lv_len < 1)
14633 {
14634 EMSG(_(e_invarg));
14635 return;
14636 }
14637 else
14638 {
14639 list_T *l = argvars[0].vval.v_list;
14640 listitem_T *li;
14641 char_u *s;
14642
14643#ifdef USE_ARGV
14644 /* Pass argv[] to mch_call_shell(). */
14645 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14646 if (argv == NULL)
14647 return;
14648#endif
14649 for (li = l->lv_first; li != NULL; li = li->li_next)
14650 {
14651 s = get_tv_string_chk(&li->li_tv);
14652 if (s == NULL)
14653 goto theend;
14654#ifdef USE_ARGV
14655 argv[argc++] = (char *)s;
14656#else
14657 if (li != l->lv_first)
14658 {
14659 s = vim_strsave_shellescape(s, FALSE, TRUE);
14660 if (s == NULL)
14661 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014662 ga_concat(&ga, s);
14663 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014664 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014665 else
14666 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014667 if (li->li_next != NULL)
14668 ga_append(&ga, ' ');
14669#endif
14670 }
14671#ifdef USE_ARGV
14672 argv[argc] = NULL;
14673#else
14674 cmd = ga.ga_data;
14675#endif
14676 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014677
Bram Moolenaar835dc632016-02-07 14:27:38 +010014678#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014679# ifdef FEAT_CHANNEL
14680 if (ch_log_active())
14681 {
14682 garray_T ga;
14683 int i;
14684
14685 ga_init2(&ga, (int)sizeof(char), 200);
14686 for (i = 0; i < argc; ++i)
14687 {
14688 if (i > 0)
14689 ga_concat(&ga, (char_u *)" ");
14690 ga_concat(&ga, (char_u *)argv[i]);
14691 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010014692 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014693 ga_clear(&ga);
14694 }
14695# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014696 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014697#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014698# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010014699 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014700# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014701 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014702#endif
14703
14704theend:
14705#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014706 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014707#else
14708 vim_free(ga.ga_data);
14709#endif
14710}
14711
14712/*
14713 * "job_status()" function
14714 */
14715 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014716f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014717{
14718 char *result;
14719
14720 if (argvars[0].v_type != VAR_JOB)
14721 EMSG(_(e_invarg));
14722 else
14723 {
14724 job_T *job = argvars[0].vval.v_job;
14725
14726 if (job->jv_status == JOB_ENDED)
14727 /* No need to check, dead is dead. */
14728 result = "dead";
14729 else if (job->jv_status == JOB_FAILED)
14730 result = "fail";
14731 else
14732 result = mch_job_status(job);
14733 rettv->v_type = VAR_STRING;
14734 rettv->vval.v_string = vim_strsave((char_u *)result);
14735 }
14736}
14737
14738/*
14739 * "job_stop()" function
14740 */
14741 static void
14742f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14743{
14744 if (argvars[0].v_type != VAR_JOB)
14745 EMSG(_(e_invarg));
14746 else
14747 {
14748 char_u *arg;
14749
14750 if (argvars[1].v_type == VAR_UNKNOWN)
14751 arg = (char_u *)"";
14752 else
14753 {
14754 arg = get_tv_string_chk(&argvars[1]);
14755 if (arg == NULL)
14756 {
14757 EMSG(_(e_invarg));
14758 return;
14759 }
14760 }
14761 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14762 rettv->vval.v_number = 0;
14763 else
14764 rettv->vval.v_number = 1;
14765 }
14766}
14767#endif
14768
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014770 * "join()" function
14771 */
14772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014773f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014774{
14775 garray_T ga;
14776 char_u *sep;
14777
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014778 if (argvars[0].v_type != VAR_LIST)
14779 {
14780 EMSG(_(e_listreq));
14781 return;
14782 }
14783 if (argvars[0].vval.v_list == NULL)
14784 return;
14785 if (argvars[1].v_type == VAR_UNKNOWN)
14786 sep = (char_u *)" ";
14787 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014788 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014789
14790 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014791
14792 if (sep != NULL)
14793 {
14794 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014795 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014796 ga_append(&ga, NUL);
14797 rettv->vval.v_string = (char_u *)ga.ga_data;
14798 }
14799 else
14800 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014801}
14802
14803/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014804 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014805 */
14806 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014807f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014808{
14809 js_read_T reader;
14810
14811 reader.js_buf = get_tv_string(&argvars[0]);
14812 reader.js_fill = NULL;
14813 reader.js_used = 0;
14814 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14815 EMSG(_(e_invarg));
14816}
14817
14818/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014819 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014820 */
14821 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014822f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014823{
14824 rettv->v_type = VAR_STRING;
14825 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14826}
14827
14828/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014829 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014830 */
14831 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014832f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014833{
14834 js_read_T reader;
14835
14836 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014837 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014838 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014839 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014840 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014841}
14842
14843/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014844 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014845 */
14846 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014847f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014848{
14849 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014850 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014851}
14852
14853/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014854 * "keys()" function
14855 */
14856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014857f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014858{
14859 dict_list(argvars, rettv, 0);
14860}
14861
14862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 * "last_buffer_nr()" function.
14864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014866f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014867{
14868 int n = 0;
14869 buf_T *buf;
14870
14871 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14872 if (n < buf->b_fnum)
14873 n = buf->b_fnum;
14874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014875 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014876}
14877
14878/*
14879 * "len()" function
14880 */
14881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014882f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014883{
14884 switch (argvars[0].v_type)
14885 {
14886 case VAR_STRING:
14887 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014888 rettv->vval.v_number = (varnumber_T)STRLEN(
14889 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014890 break;
14891 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014892 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014893 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014894 case VAR_DICT:
14895 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14896 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014897 case VAR_UNKNOWN:
14898 case VAR_SPECIAL:
14899 case VAR_FLOAT:
14900 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014901 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014902 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014903 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014904 break;
14905 }
14906}
14907
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014908static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014909
14910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014911libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014912{
14913#ifdef FEAT_LIBCALL
14914 char_u *string_in;
14915 char_u **string_result;
14916 int nr_result;
14917#endif
14918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014919 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014920 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014921 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014922
14923 if (check_restricted() || check_secure())
14924 return;
14925
14926#ifdef FEAT_LIBCALL
14927 /* The first two args must be strings, otherwise its meaningless */
14928 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14929 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014930 string_in = NULL;
14931 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014932 string_in = argvars[2].vval.v_string;
14933 if (type == VAR_NUMBER)
14934 string_result = NULL;
14935 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014936 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014937 if (mch_libcall(argvars[0].vval.v_string,
14938 argvars[1].vval.v_string,
14939 string_in,
14940 argvars[2].vval.v_number,
14941 string_result,
14942 &nr_result) == OK
14943 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014944 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014945 }
14946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947}
14948
14949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014950 * "libcall()" function
14951 */
14952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014953f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014954{
14955 libcall_common(argvars, rettv, VAR_STRING);
14956}
14957
14958/*
14959 * "libcallnr()" function
14960 */
14961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014962f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014963{
14964 libcall_common(argvars, rettv, VAR_NUMBER);
14965}
14966
14967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 * "line(string)" function
14969 */
14970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014971f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972{
14973 linenr_T lnum = 0;
14974 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014975 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014977 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978 if (fp != NULL)
14979 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014980 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981}
14982
14983/*
14984 * "line2byte(lnum)" function
14985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014987f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988{
14989#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014990 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991#else
14992 linenr_T lnum;
14993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014994 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014998 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14999 if (rettv->vval.v_number >= 0)
15000 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001#endif
15002}
15003
15004/*
15005 * "lispindent(lnum)" function
15006 */
15007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015008f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009{
15010#ifdef FEAT_LISP
15011 pos_T pos;
15012 linenr_T lnum;
15013
15014 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15017 {
15018 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015019 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 curwin->w_cursor = pos;
15021 }
15022 else
15023#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015024 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025}
15026
15027/*
15028 * "localtime()" function
15029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015031f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015033 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034}
15035
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015036static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037
15038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015039get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040{
15041 char_u *keys;
15042 char_u *which;
15043 char_u buf[NUMBUFLEN];
15044 char_u *keys_buf = NULL;
15045 char_u *rhs;
15046 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015047 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015048 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015049 mapblock_T *mp;
15050 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051
15052 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 rettv->v_type = VAR_STRING;
15054 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015056 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 if (*keys == NUL)
15058 return;
15059
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015060 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015061 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015062 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015063 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015064 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015065 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015066 if (argvars[3].v_type != VAR_UNKNOWN)
15067 get_dict = get_tv_number(&argvars[3]);
15068 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070 else
15071 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015072 if (which == NULL)
15073 return;
15074
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075 mode = get_map_mode(&which, 0);
15076
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015077 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015078 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015080
15081 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015083 /* Return a string. */
15084 if (rhs != NULL)
15085 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086
Bram Moolenaarbd743252010-10-20 21:23:33 +020015087 }
15088 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15089 {
15090 /* Return a dictionary. */
15091 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15092 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15093 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094
Bram Moolenaarbd743252010-10-20 21:23:33 +020015095 dict_add_nr_str(dict, "lhs", 0L, lhs);
15096 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15097 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15098 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15099 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15100 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15101 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015102 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015103 dict_add_nr_str(dict, "mode", 0L, mapmode);
15104
15105 vim_free(lhs);
15106 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 }
15108}
15109
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015110#ifdef FEAT_FLOAT
15111/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015112 * "log()" function
15113 */
15114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015115f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015116{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015117 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015118
15119 rettv->v_type = VAR_FLOAT;
15120 if (get_float_arg(argvars, &f) == OK)
15121 rettv->vval.v_float = log(f);
15122 else
15123 rettv->vval.v_float = 0.0;
15124}
15125
15126/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015127 * "log10()" function
15128 */
15129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015130f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015131{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015132 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015133
15134 rettv->v_type = VAR_FLOAT;
15135 if (get_float_arg(argvars, &f) == OK)
15136 rettv->vval.v_float = log10(f);
15137 else
15138 rettv->vval.v_float = 0.0;
15139}
15140#endif
15141
Bram Moolenaar1dced572012-04-05 16:54:08 +020015142#ifdef FEAT_LUA
15143/*
15144 * "luaeval()" function
15145 */
15146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015147f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015148{
15149 char_u *str;
15150 char_u buf[NUMBUFLEN];
15151
15152 str = get_tv_string_buf(&argvars[0], buf);
15153 do_luaeval(str, argvars + 1, rettv);
15154}
15155#endif
15156
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015158 * "map()" function
15159 */
15160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015161f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015162{
15163 filter_map(argvars, rettv, TRUE);
15164}
15165
15166/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015167 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168 */
15169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015170f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015172 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173}
15174
15175/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 */
15178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015179f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015181 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182}
15183
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015184static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185
15186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015187find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015188{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015189 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015190 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015191 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 char_u *pat;
15193 regmatch_T regmatch;
15194 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015195 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196 char_u *save_cpo;
15197 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015198 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015199 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015200 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015201 list_T *l = NULL;
15202 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015203 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015204 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205
15206 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15207 save_cpo = p_cpo;
15208 p_cpo = (char_u *)"";
15209
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015210 rettv->vval.v_number = -1;
15211 if (type == 3)
15212 {
15213 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015214 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015215 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015216 }
15217 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015219 rettv->v_type = VAR_STRING;
15220 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015223 if (argvars[0].v_type == VAR_LIST)
15224 {
15225 if ((l = argvars[0].vval.v_list) == NULL)
15226 goto theend;
15227 li = l->lv_first;
15228 }
15229 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015230 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015231 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015232 len = (long)STRLEN(str);
15233 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015235 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15236 if (pat == NULL)
15237 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015238
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015239 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015241 int error = FALSE;
15242
15243 start = get_tv_number_chk(&argvars[2], &error);
15244 if (error)
15245 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015246 if (l != NULL)
15247 {
15248 li = list_find(l, start);
15249 if (li == NULL)
15250 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015251 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015252 }
15253 else
15254 {
15255 if (start < 0)
15256 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015257 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015258 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015259 /* When "count" argument is there ignore matches before "start",
15260 * otherwise skip part of the string. Differs when pattern is "^"
15261 * or "\<". */
15262 if (argvars[3].v_type != VAR_UNKNOWN)
15263 startcol = start;
15264 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015265 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015266 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015267 len -= start;
15268 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015269 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015270
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015271 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015272 nth = get_tv_number_chk(&argvars[3], &error);
15273 if (error)
15274 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275 }
15276
15277 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15278 if (regmatch.regprog != NULL)
15279 {
15280 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015281
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015282 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015283 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015284 if (l != NULL)
15285 {
15286 if (li == NULL)
15287 {
15288 match = FALSE;
15289 break;
15290 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015291 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015292 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015293 if (str == NULL)
15294 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015295 }
15296
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015297 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015298
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015299 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015300 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015301 if (l == NULL && !match)
15302 break;
15303
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015304 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015305 if (l != NULL)
15306 {
15307 li = li->li_next;
15308 ++idx;
15309 }
15310 else
15311 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015312#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015313 startcol = (colnr_T)(regmatch.startp[0]
15314 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015315#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015316 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015317#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015318 if (startcol > (colnr_T)len
15319 || str + startcol <= regmatch.startp[0])
15320 {
15321 match = FALSE;
15322 break;
15323 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015324 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015325 }
15326
15327 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015329 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015330 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015331 int i;
15332
15333 /* return list with matched string and submatches */
15334 for (i = 0; i < NSUBEXP; ++i)
15335 {
15336 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015337 {
15338 if (list_append_string(rettv->vval.v_list,
15339 (char_u *)"", 0) == FAIL)
15340 break;
15341 }
15342 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015343 regmatch.startp[i],
15344 (int)(regmatch.endp[i] - regmatch.startp[i]))
15345 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015346 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015347 }
15348 }
15349 else if (type == 2)
15350 {
15351 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015352 if (l != NULL)
15353 copy_tv(&li->li_tv, rettv);
15354 else
15355 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015357 }
15358 else if (l != NULL)
15359 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 else
15361 {
15362 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 (varnumber_T)(regmatch.startp[0] - str);
15365 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015368 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 }
15370 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015371 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372 }
15373
15374theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015375 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376 p_cpo = save_cpo;
15377}
15378
15379/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015380 * "match()" function
15381 */
15382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015383f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015384{
15385 find_some_match(argvars, rettv, 1);
15386}
15387
15388/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015389 * "matchadd()" function
15390 */
15391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015392f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015393{
15394#ifdef FEAT_SEARCH_EXTRA
15395 char_u buf[NUMBUFLEN];
15396 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15397 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15398 int prio = 10; /* default priority */
15399 int id = -1;
15400 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015401 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015402
15403 rettv->vval.v_number = -1;
15404
15405 if (grp == NULL || pat == NULL)
15406 return;
15407 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015408 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015409 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015410 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015411 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015412 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015413 if (argvars[4].v_type != VAR_UNKNOWN)
15414 {
15415 if (argvars[4].v_type != VAR_DICT)
15416 {
15417 EMSG(_(e_dictreq));
15418 return;
15419 }
15420 if (dict_find(argvars[4].vval.v_dict,
15421 (char_u *)"conceal", -1) != NULL)
15422 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15423 (char_u *)"conceal", FALSE);
15424 }
15425 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015426 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015427 if (error == TRUE)
15428 return;
15429 if (id >= 1 && id <= 3)
15430 {
15431 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15432 return;
15433 }
15434
Bram Moolenaar6561d522015-07-21 15:48:27 +020015435 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15436 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015437#endif
15438}
15439
15440/*
15441 * "matchaddpos()" function
15442 */
15443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015444f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015445{
15446#ifdef FEAT_SEARCH_EXTRA
15447 char_u buf[NUMBUFLEN];
15448 char_u *group;
15449 int prio = 10;
15450 int id = -1;
15451 int error = FALSE;
15452 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015453 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015454
15455 rettv->vval.v_number = -1;
15456
15457 group = get_tv_string_buf_chk(&argvars[0], buf);
15458 if (group == NULL)
15459 return;
15460
15461 if (argvars[1].v_type != VAR_LIST)
15462 {
15463 EMSG2(_(e_listarg), "matchaddpos()");
15464 return;
15465 }
15466 l = argvars[1].vval.v_list;
15467 if (l == NULL)
15468 return;
15469
15470 if (argvars[2].v_type != VAR_UNKNOWN)
15471 {
15472 prio = get_tv_number_chk(&argvars[2], &error);
15473 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015474 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015475 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015476 if (argvars[4].v_type != VAR_UNKNOWN)
15477 {
15478 if (argvars[4].v_type != VAR_DICT)
15479 {
15480 EMSG(_(e_dictreq));
15481 return;
15482 }
15483 if (dict_find(argvars[4].vval.v_dict,
15484 (char_u *)"conceal", -1) != NULL)
15485 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15486 (char_u *)"conceal", FALSE);
15487 }
15488 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015489 }
15490 if (error == TRUE)
15491 return;
15492
15493 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15494 if (id == 1 || id == 2)
15495 {
15496 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15497 return;
15498 }
15499
Bram Moolenaar6561d522015-07-21 15:48:27 +020015500 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15501 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015502#endif
15503}
15504
15505/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015506 * "matcharg()" function
15507 */
15508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015509f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015510{
15511 if (rettv_list_alloc(rettv) == OK)
15512 {
15513#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015514 int id = get_tv_number(&argvars[0]);
15515 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015516
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015517 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015518 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015519 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15520 {
15521 list_append_string(rettv->vval.v_list,
15522 syn_id2name(m->hlg_id), -1);
15523 list_append_string(rettv->vval.v_list, m->pattern, -1);
15524 }
15525 else
15526 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015527 list_append_string(rettv->vval.v_list, NULL, -1);
15528 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015529 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015530 }
15531#endif
15532 }
15533}
15534
15535/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015536 * "matchdelete()" function
15537 */
15538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015539f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015540{
15541#ifdef FEAT_SEARCH_EXTRA
15542 rettv->vval.v_number = match_delete(curwin,
15543 (int)get_tv_number(&argvars[0]), TRUE);
15544#endif
15545}
15546
15547/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015548 * "matchend()" function
15549 */
15550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015551f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015552{
15553 find_some_match(argvars, rettv, 0);
15554}
15555
15556/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015557 * "matchlist()" function
15558 */
15559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015560f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015561{
15562 find_some_match(argvars, rettv, 3);
15563}
15564
15565/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015566 * "matchstr()" function
15567 */
15568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015569f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015570{
15571 find_some_match(argvars, rettv, 2);
15572}
15573
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015574static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015575
15576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015577max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015578{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015579 long n = 0;
15580 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015581 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015582
15583 if (argvars[0].v_type == VAR_LIST)
15584 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015585 list_T *l;
15586 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015587
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015588 l = argvars[0].vval.v_list;
15589 if (l != NULL)
15590 {
15591 li = l->lv_first;
15592 if (li != NULL)
15593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015594 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015595 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015596 {
15597 li = li->li_next;
15598 if (li == NULL)
15599 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015600 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015601 if (domax ? i > n : i < n)
15602 n = i;
15603 }
15604 }
15605 }
15606 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015607 else if (argvars[0].v_type == VAR_DICT)
15608 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015609 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015610 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015611 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015612 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015613
15614 d = argvars[0].vval.v_dict;
15615 if (d != NULL)
15616 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015617 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015618 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015619 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015620 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015621 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015622 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015623 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015624 if (first)
15625 {
15626 n = i;
15627 first = FALSE;
15628 }
15629 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015630 n = i;
15631 }
15632 }
15633 }
15634 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015635 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015636 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015637 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015638}
15639
15640/*
15641 * "max()" function
15642 */
15643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015644f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015645{
15646 max_min(argvars, rettv, TRUE);
15647}
15648
15649/*
15650 * "min()" function
15651 */
15652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015653f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015654{
15655 max_min(argvars, rettv, FALSE);
15656}
15657
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015658static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015659
15660/*
15661 * Create the directory in which "dir" is located, and higher levels when
15662 * needed.
15663 */
15664 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015665mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015666{
15667 char_u *p;
15668 char_u *updir;
15669 int r = FAIL;
15670
15671 /* Get end of directory name in "dir".
15672 * We're done when it's "/" or "c:/". */
15673 p = gettail_sep(dir);
15674 if (p <= get_past_head(dir))
15675 return OK;
15676
15677 /* If the directory exists we're done. Otherwise: create it.*/
15678 updir = vim_strnsave(dir, (int)(p - dir));
15679 if (updir == NULL)
15680 return FAIL;
15681 if (mch_isdir(updir))
15682 r = OK;
15683 else if (mkdir_recurse(updir, prot) == OK)
15684 r = vim_mkdir_emsg(updir, prot);
15685 vim_free(updir);
15686 return r;
15687}
15688
15689#ifdef vim_mkdir
15690/*
15691 * "mkdir()" function
15692 */
15693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015694f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015695{
15696 char_u *dir;
15697 char_u buf[NUMBUFLEN];
15698 int prot = 0755;
15699
15700 rettv->vval.v_number = FAIL;
15701 if (check_restricted() || check_secure())
15702 return;
15703
15704 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015705 if (*dir == NUL)
15706 rettv->vval.v_number = FAIL;
15707 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015708 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015709 if (*gettail(dir) == NUL)
15710 /* remove trailing slashes */
15711 *gettail_sep(dir) = NUL;
15712
15713 if (argvars[1].v_type != VAR_UNKNOWN)
15714 {
15715 if (argvars[2].v_type != VAR_UNKNOWN)
15716 prot = get_tv_number_chk(&argvars[2], NULL);
15717 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15718 mkdir_recurse(dir, prot);
15719 }
15720 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015721 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015722}
15723#endif
15724
Bram Moolenaar0d660222005-01-07 21:51:51 +000015725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015726 * "mode()" function
15727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015729f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015731 char_u buf[3];
15732
15733 buf[1] = NUL;
15734 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 if (VIsual_active)
15737 {
15738 if (VIsual_select)
15739 buf[0] = VIsual_mode + 's' - 'v';
15740 else
15741 buf[0] = VIsual_mode;
15742 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015743 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015744 || State == CONFIRM)
15745 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015747 if (State == ASKMORE)
15748 buf[1] = 'm';
15749 else if (State == CONFIRM)
15750 buf[1] = '?';
15751 }
15752 else if (State == EXTERNCMD)
15753 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 else if (State & INSERT)
15755 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015756#ifdef FEAT_VREPLACE
15757 if (State & VREPLACE_FLAG)
15758 {
15759 buf[0] = 'R';
15760 buf[1] = 'v';
15761 }
15762 else
15763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 if (State & REPLACE_FLAG)
15765 buf[0] = 'R';
15766 else
15767 buf[0] = 'i';
15768 }
15769 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015772 if (exmode_active)
15773 buf[1] = 'v';
15774 }
15775 else if (exmode_active)
15776 {
15777 buf[0] = 'c';
15778 buf[1] = 'e';
15779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015783 if (finish_op)
15784 buf[1] = 'o';
15785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015786
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015787 /* Clear out the minor mode when the argument is not a non-zero number or
15788 * non-empty string. */
15789 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015790 buf[1] = NUL;
15791
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015792 rettv->vval.v_string = vim_strsave(buf);
15793 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794}
15795
Bram Moolenaar429fa852013-04-15 12:27:36 +020015796#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015797/*
15798 * "mzeval()" function
15799 */
15800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015801f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015802{
15803 char_u *str;
15804 char_u buf[NUMBUFLEN];
15805
15806 str = get_tv_string_buf(&argvars[0], buf);
15807 do_mzeval(str, rettv);
15808}
Bram Moolenaar75676462013-01-30 14:55:42 +010015809
15810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015811mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015812{
15813 typval_T argvars[3];
15814
15815 argvars[0].v_type = VAR_STRING;
15816 argvars[0].vval.v_string = name;
15817 copy_tv(args, &argvars[1]);
15818 argvars[2].v_type = VAR_UNKNOWN;
15819 f_call(argvars, rettv);
15820 clear_tv(&argvars[1]);
15821}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015822#endif
15823
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015825 * "nextnonblank()" function
15826 */
15827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015828f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015829{
15830 linenr_T lnum;
15831
15832 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015834 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015835 {
15836 lnum = 0;
15837 break;
15838 }
15839 if (*skipwhite(ml_get(lnum)) != NUL)
15840 break;
15841 }
15842 rettv->vval.v_number = lnum;
15843}
15844
15845/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 * "nr2char()" function
15847 */
15848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015849f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850{
15851 char_u buf[NUMBUFLEN];
15852
15853#ifdef FEAT_MBYTE
15854 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015855 {
15856 int utf8 = 0;
15857
15858 if (argvars[1].v_type != VAR_UNKNOWN)
15859 utf8 = get_tv_number_chk(&argvars[1], NULL);
15860 if (utf8)
15861 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15862 else
15863 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 else
15866#endif
15867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015868 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 buf[1] = NUL;
15870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015871 rettv->v_type = VAR_STRING;
15872 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015873}
15874
15875/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015876 * "or(expr, expr)" function
15877 */
15878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015879f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015880{
15881 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15882 | get_tv_number_chk(&argvars[1], NULL);
15883}
15884
15885/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015886 * "pathshorten()" function
15887 */
15888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015889f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015890{
15891 char_u *p;
15892
15893 rettv->v_type = VAR_STRING;
15894 p = get_tv_string_chk(&argvars[0]);
15895 if (p == NULL)
15896 rettv->vval.v_string = NULL;
15897 else
15898 {
15899 p = vim_strsave(p);
15900 rettv->vval.v_string = p;
15901 if (p != NULL)
15902 shorten_dir(p);
15903 }
15904}
15905
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015906#ifdef FEAT_PERL
15907/*
15908 * "perleval()" function
15909 */
15910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015911f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015912{
15913 char_u *str;
15914 char_u buf[NUMBUFLEN];
15915
15916 str = get_tv_string_buf(&argvars[0], buf);
15917 do_perleval(str, rettv);
15918}
15919#endif
15920
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015921#ifdef FEAT_FLOAT
15922/*
15923 * "pow()" function
15924 */
15925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015926f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015927{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015928 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015929
15930 rettv->v_type = VAR_FLOAT;
15931 if (get_float_arg(argvars, &fx) == OK
15932 && get_float_arg(&argvars[1], &fy) == OK)
15933 rettv->vval.v_float = pow(fx, fy);
15934 else
15935 rettv->vval.v_float = 0.0;
15936}
15937#endif
15938
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015939/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015940 * "prevnonblank()" function
15941 */
15942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015943f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015944{
15945 linenr_T lnum;
15946
15947 lnum = get_tv_lnum(argvars);
15948 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15949 lnum = 0;
15950 else
15951 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15952 --lnum;
15953 rettv->vval.v_number = lnum;
15954}
15955
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015956/* This dummy va_list is here because:
15957 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15958 * - locally in the function results in a "used before set" warning
15959 * - using va_start() to initialize it gives "function with fixed args" error */
15960static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015961
Bram Moolenaar8c711452005-01-14 21:53:12 +000015962/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015963 * "printf()" function
15964 */
15965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015966f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015967{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015968 char_u buf[NUMBUFLEN];
15969 int len;
15970 char_u *s;
15971 int saved_did_emsg = did_emsg;
15972 char *fmt;
15973
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015974 rettv->v_type = VAR_STRING;
15975 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015976
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015977 /* Get the required length, allocate the buffer and do it for real. */
15978 did_emsg = FALSE;
15979 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15980 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15981 if (!did_emsg)
15982 {
15983 s = alloc(len + 1);
15984 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015985 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015986 rettv->vval.v_string = s;
15987 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015988 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015989 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015990 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015991}
15992
15993/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015994 * "pumvisible()" function
15995 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015997f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015998{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015999#ifdef FEAT_INS_EXPAND
16000 if (pum_visible())
16001 rettv->vval.v_number = 1;
16002#endif
16003}
16004
Bram Moolenaardb913952012-06-29 12:54:53 +020016005#ifdef FEAT_PYTHON3
16006/*
16007 * "py3eval()" function
16008 */
16009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016010f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016011{
16012 char_u *str;
16013 char_u buf[NUMBUFLEN];
16014
16015 str = get_tv_string_buf(&argvars[0], buf);
16016 do_py3eval(str, rettv);
16017}
16018#endif
16019
16020#ifdef FEAT_PYTHON
16021/*
16022 * "pyeval()" function
16023 */
16024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016025f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016026{
16027 char_u *str;
16028 char_u buf[NUMBUFLEN];
16029
16030 str = get_tv_string_buf(&argvars[0], buf);
16031 do_pyeval(str, rettv);
16032}
16033#endif
16034
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016035/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016036 * "range()" function
16037 */
16038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016039f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016040{
16041 long start;
16042 long end;
16043 long stride = 1;
16044 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016045 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016047 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016048 if (argvars[1].v_type == VAR_UNKNOWN)
16049 {
16050 end = start - 1;
16051 start = 0;
16052 }
16053 else
16054 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016055 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016056 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016057 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016058 }
16059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016060 if (error)
16061 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016062 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016063 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016064 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016065 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016066 else
16067 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016068 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016069 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016070 if (list_append_number(rettv->vval.v_list,
16071 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016072 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016073 }
16074}
16075
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016076/*
16077 * "readfile()" function
16078 */
16079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016080f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016081{
16082 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016083 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016084 char_u *fname;
16085 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016086 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16087 int io_size = sizeof(buf);
16088 int readlen; /* size of last fread() */
16089 char_u *prev = NULL; /* previously read bytes, if any */
16090 long prevlen = 0; /* length of data in prev */
16091 long prevsize = 0; /* size of prev buffer */
16092 long maxline = MAXLNUM;
16093 long cnt = 0;
16094 char_u *p; /* position in buf */
16095 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016096
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016097 if (argvars[1].v_type != VAR_UNKNOWN)
16098 {
16099 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16100 binary = TRUE;
16101 if (argvars[2].v_type != VAR_UNKNOWN)
16102 maxline = get_tv_number(&argvars[2]);
16103 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016104
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016105 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016106 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016107
16108 /* Always open the file in binary mode, library functions have a mind of
16109 * their own about CR-LF conversion. */
16110 fname = get_tv_string(&argvars[0]);
16111 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16112 {
16113 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16114 return;
16115 }
16116
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016117 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016118 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016119 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016120
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016121 /* This for loop processes what was read, but is also entered at end
16122 * of file so that either:
16123 * - an incomplete line gets written
16124 * - a "binary" file gets an empty line at the end if it ends in a
16125 * newline. */
16126 for (p = buf, start = buf;
16127 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16128 ++p)
16129 {
16130 if (*p == '\n' || readlen <= 0)
16131 {
16132 listitem_T *li;
16133 char_u *s = NULL;
16134 long_u len = p - start;
16135
16136 /* Finished a line. Remove CRs before NL. */
16137 if (readlen > 0 && !binary)
16138 {
16139 while (len > 0 && start[len - 1] == '\r')
16140 --len;
16141 /* removal may cross back to the "prev" string */
16142 if (len == 0)
16143 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16144 --prevlen;
16145 }
16146 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016147 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016148 else
16149 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016150 /* Change "prev" buffer to be the right size. This way
16151 * the bytes are only copied once, and very long lines are
16152 * allocated only once. */
16153 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016154 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016155 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016156 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016157 prev = NULL; /* the list will own the string */
16158 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016159 }
16160 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016161 if (s == NULL)
16162 {
16163 do_outofmem_msg((long_u) prevlen + len + 1);
16164 failed = TRUE;
16165 break;
16166 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016167
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016168 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016169 {
16170 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016171 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016172 break;
16173 }
16174 li->li_tv.v_type = VAR_STRING;
16175 li->li_tv.v_lock = 0;
16176 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016177 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016178
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016179 start = p + 1; /* step over newline */
16180 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016181 break;
16182 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016183 else if (*p == NUL)
16184 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016185#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016186 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16187 * when finding the BF and check the previous two bytes. */
16188 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016189 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016190 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16191 * + 1, these may be in the "prev" string. */
16192 char_u back1 = p >= buf + 1 ? p[-1]
16193 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16194 char_u back2 = p >= buf + 2 ? p[-2]
16195 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16196 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016197
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016198 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016199 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016200 char_u *dest = p - 2;
16201
16202 /* Usually a BOM is at the beginning of a file, and so at
16203 * the beginning of a line; then we can just step over it.
16204 */
16205 if (start == dest)
16206 start = p + 1;
16207 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016208 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016209 /* have to shuffle buf to close gap */
16210 int adjust_prevlen = 0;
16211
16212 if (dest < buf)
16213 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016214 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016215 dest = buf;
16216 }
16217 if (readlen > p - buf + 1)
16218 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16219 readlen -= 3 - adjust_prevlen;
16220 prevlen -= adjust_prevlen;
16221 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016222 }
16223 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016224 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016225#endif
16226 } /* for */
16227
16228 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16229 break;
16230 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016231 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016232 /* There's part of a line in buf, store it in "prev". */
16233 if (p - start + prevlen >= prevsize)
16234 {
16235 /* need bigger "prev" buffer */
16236 char_u *newprev;
16237
16238 /* A common use case is ordinary text files and "prev" gets a
16239 * fragment of a line, so the first allocation is made
16240 * small, to avoid repeatedly 'allocing' large and
16241 * 'reallocing' small. */
16242 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016243 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016244 else
16245 {
16246 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016247 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016248 prevsize = grow50pc > growmin ? grow50pc : growmin;
16249 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016250 newprev = prev == NULL ? alloc(prevsize)
16251 : vim_realloc(prev, prevsize);
16252 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016253 {
16254 do_outofmem_msg((long_u)prevsize);
16255 failed = TRUE;
16256 break;
16257 }
16258 prev = newprev;
16259 }
16260 /* Add the line part to end of "prev". */
16261 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016262 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016263 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016264 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016265
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016266 /*
16267 * For a negative line count use only the lines at the end of the file,
16268 * free the rest.
16269 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016270 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016271 while (cnt > -maxline)
16272 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016273 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016274 --cnt;
16275 }
16276
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016277 if (failed)
16278 {
16279 list_free(rettv->vval.v_list, TRUE);
16280 /* readfile doc says an empty list is returned on error */
16281 rettv->vval.v_list = list_alloc();
16282 }
16283
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016284 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016285 fclose(fd);
16286}
16287
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016288#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016289static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016290
16291/*
16292 * Convert a List to proftime_T.
16293 * Return FAIL when there is something wrong.
16294 */
16295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016296list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016297{
16298 long n1, n2;
16299 int error = FALSE;
16300
16301 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16302 || arg->vval.v_list->lv_len != 2)
16303 return FAIL;
16304 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16305 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16306# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016307 tm->HighPart = n1;
16308 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016309# else
16310 tm->tv_sec = n1;
16311 tm->tv_usec = n2;
16312# endif
16313 return error ? FAIL : OK;
16314}
16315#endif /* FEAT_RELTIME */
16316
16317/*
16318 * "reltime()" function
16319 */
16320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016321f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016322{
16323#ifdef FEAT_RELTIME
16324 proftime_T res;
16325 proftime_T start;
16326
16327 if (argvars[0].v_type == VAR_UNKNOWN)
16328 {
16329 /* No arguments: get current time. */
16330 profile_start(&res);
16331 }
16332 else if (argvars[1].v_type == VAR_UNKNOWN)
16333 {
16334 if (list2proftime(&argvars[0], &res) == FAIL)
16335 return;
16336 profile_end(&res);
16337 }
16338 else
16339 {
16340 /* Two arguments: compute the difference. */
16341 if (list2proftime(&argvars[0], &start) == FAIL
16342 || list2proftime(&argvars[1], &res) == FAIL)
16343 return;
16344 profile_sub(&res, &start);
16345 }
16346
16347 if (rettv_list_alloc(rettv) == OK)
16348 {
16349 long n1, n2;
16350
16351# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016352 n1 = res.HighPart;
16353 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016354# else
16355 n1 = res.tv_sec;
16356 n2 = res.tv_usec;
16357# endif
16358 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16359 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16360 }
16361#endif
16362}
16363
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016364#ifdef FEAT_FLOAT
16365/*
16366 * "reltimefloat()" function
16367 */
16368 static void
16369f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16370{
16371# ifdef FEAT_RELTIME
16372 proftime_T tm;
16373# endif
16374
16375 rettv->v_type = VAR_FLOAT;
16376 rettv->vval.v_float = 0;
16377# ifdef FEAT_RELTIME
16378 if (list2proftime(&argvars[0], &tm) == OK)
16379 rettv->vval.v_float = profile_float(&tm);
16380# endif
16381}
16382#endif
16383
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016384/*
16385 * "reltimestr()" function
16386 */
16387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016388f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016389{
16390#ifdef FEAT_RELTIME
16391 proftime_T tm;
16392#endif
16393
16394 rettv->v_type = VAR_STRING;
16395 rettv->vval.v_string = NULL;
16396#ifdef FEAT_RELTIME
16397 if (list2proftime(&argvars[0], &tm) == OK)
16398 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16399#endif
16400}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016401
Bram Moolenaar0d660222005-01-07 21:51:51 +000016402#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016403static void make_connection(void);
16404static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016405
16406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016407make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016408{
16409 if (X_DISPLAY == NULL
16410# ifdef FEAT_GUI
16411 && !gui.in_use
16412# endif
16413 )
16414 {
16415 x_force_connect = TRUE;
16416 setup_term_clip();
16417 x_force_connect = FALSE;
16418 }
16419}
16420
16421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016422check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016423{
16424 make_connection();
16425 if (X_DISPLAY == NULL)
16426 {
16427 EMSG(_("E240: No connection to Vim server"));
16428 return FAIL;
16429 }
16430 return OK;
16431}
16432#endif
16433
16434#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016435static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436
16437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016438remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016439{
16440 char_u *server_name;
16441 char_u *keys;
16442 char_u *r = NULL;
16443 char_u buf[NUMBUFLEN];
16444# ifdef WIN32
16445 HWND w;
16446# else
16447 Window w;
16448# endif
16449
16450 if (check_restricted() || check_secure())
16451 return;
16452
16453# ifdef FEAT_X11
16454 if (check_connection() == FAIL)
16455 return;
16456# endif
16457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016458 server_name = get_tv_string_chk(&argvars[0]);
16459 if (server_name == NULL)
16460 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016461 keys = get_tv_string_buf(&argvars[1], buf);
16462# ifdef WIN32
16463 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16464# else
16465 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16466 < 0)
16467# endif
16468 {
16469 if (r != NULL)
16470 EMSG(r); /* sending worked but evaluation failed */
16471 else
16472 EMSG2(_("E241: Unable to send to %s"), server_name);
16473 return;
16474 }
16475
16476 rettv->vval.v_string = r;
16477
16478 if (argvars[2].v_type != VAR_UNKNOWN)
16479 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016480 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016481 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016482 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016483
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016484 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016485 v.di_tv.v_type = VAR_STRING;
16486 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016487 idvar = get_tv_string_chk(&argvars[2]);
16488 if (idvar != NULL)
16489 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016490 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016491 }
16492}
16493#endif
16494
16495/*
16496 * "remote_expr()" function
16497 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016499f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016500{
16501 rettv->v_type = VAR_STRING;
16502 rettv->vval.v_string = NULL;
16503#ifdef FEAT_CLIENTSERVER
16504 remote_common(argvars, rettv, TRUE);
16505#endif
16506}
16507
16508/*
16509 * "remote_foreground()" function
16510 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016512f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016513{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514#ifdef FEAT_CLIENTSERVER
16515# ifdef WIN32
16516 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016517 {
16518 char_u *server_name = get_tv_string_chk(&argvars[0]);
16519
16520 if (server_name != NULL)
16521 serverForeground(server_name);
16522 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016523# else
16524 /* Send a foreground() expression to the server. */
16525 argvars[1].v_type = VAR_STRING;
16526 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16527 argvars[2].v_type = VAR_UNKNOWN;
16528 remote_common(argvars, rettv, TRUE);
16529 vim_free(argvars[1].vval.v_string);
16530# endif
16531#endif
16532}
16533
Bram Moolenaar0d660222005-01-07 21:51:51 +000016534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016535f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016536{
16537#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016538 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016539 char_u *s = NULL;
16540# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016541 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016542# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016543 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016544
16545 if (check_restricted() || check_secure())
16546 {
16547 rettv->vval.v_number = -1;
16548 return;
16549 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016550 serverid = get_tv_string_chk(&argvars[0]);
16551 if (serverid == NULL)
16552 {
16553 rettv->vval.v_number = -1;
16554 return; /* type error; errmsg already given */
16555 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016556# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016557 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016558 if (n == 0)
16559 rettv->vval.v_number = -1;
16560 else
16561 {
16562 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16563 rettv->vval.v_number = (s != NULL);
16564 }
16565# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016566 if (check_connection() == FAIL)
16567 return;
16568
16569 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016570 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016571# endif
16572
16573 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016575 char_u *retvar;
16576
Bram Moolenaar33570922005-01-25 22:26:29 +000016577 v.di_tv.v_type = VAR_STRING;
16578 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016579 retvar = get_tv_string_chk(&argvars[1]);
16580 if (retvar != NULL)
16581 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016582 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016583 }
16584#else
16585 rettv->vval.v_number = -1;
16586#endif
16587}
16588
Bram Moolenaar0d660222005-01-07 21:51:51 +000016589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016590f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016591{
16592 char_u *r = NULL;
16593
16594#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016595 char_u *serverid = get_tv_string_chk(&argvars[0]);
16596
16597 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016598 {
16599# ifdef WIN32
16600 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016601 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016602
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016603 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016604 if (n != 0)
16605 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16606 if (r == NULL)
16607# else
16608 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016609 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016610# endif
16611 EMSG(_("E277: Unable to read a server reply"));
16612 }
16613#endif
16614 rettv->v_type = VAR_STRING;
16615 rettv->vval.v_string = r;
16616}
16617
16618/*
16619 * "remote_send()" function
16620 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016622f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016623{
16624 rettv->v_type = VAR_STRING;
16625 rettv->vval.v_string = NULL;
16626#ifdef FEAT_CLIENTSERVER
16627 remote_common(argvars, rettv, FALSE);
16628#endif
16629}
16630
16631/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016632 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016633 */
16634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016635f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016636{
Bram Moolenaar33570922005-01-25 22:26:29 +000016637 list_T *l;
16638 listitem_T *item, *item2;
16639 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016640 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016641 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016642 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016643 dict_T *d;
16644 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016645 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016646
Bram Moolenaar8c711452005-01-14 21:53:12 +000016647 if (argvars[0].v_type == VAR_DICT)
16648 {
16649 if (argvars[2].v_type != VAR_UNKNOWN)
16650 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016651 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016652 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016654 key = get_tv_string_chk(&argvars[1]);
16655 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016657 di = dict_find(d, key, -1);
16658 if (di == NULL)
16659 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016660 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16661 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016662 {
16663 *rettv = di->di_tv;
16664 init_tv(&di->di_tv);
16665 dictitem_remove(d, di);
16666 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016667 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016668 }
16669 }
16670 else if (argvars[0].v_type != VAR_LIST)
16671 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016672 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016673 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016675 int error = FALSE;
16676
16677 idx = get_tv_number_chk(&argvars[1], &error);
16678 if (error)
16679 ; /* type error: do nothing, errmsg already given */
16680 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016681 EMSGN(_(e_listidx), idx);
16682 else
16683 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016684 if (argvars[2].v_type == VAR_UNKNOWN)
16685 {
16686 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016687 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016688 *rettv = item->li_tv;
16689 vim_free(item);
16690 }
16691 else
16692 {
16693 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016694 end = get_tv_number_chk(&argvars[2], &error);
16695 if (error)
16696 ; /* type error: do nothing */
16697 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016698 EMSGN(_(e_listidx), end);
16699 else
16700 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016701 int cnt = 0;
16702
16703 for (li = item; li != NULL; li = li->li_next)
16704 {
16705 ++cnt;
16706 if (li == item2)
16707 break;
16708 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016709 if (li == NULL) /* didn't find "item2" after "item" */
16710 EMSG(_(e_invrange));
16711 else
16712 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016713 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016714 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016715 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016716 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016717 l->lv_first = item;
16718 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016719 item->li_prev = NULL;
16720 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016721 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016722 }
16723 }
16724 }
16725 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016726 }
16727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728}
16729
16730/*
16731 * "rename({from}, {to})" function
16732 */
16733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016734f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735{
16736 char_u buf[NUMBUFLEN];
16737
16738 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016739 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016741 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16742 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743}
16744
16745/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016746 * "repeat()" function
16747 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016749f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016750{
16751 char_u *p;
16752 int n;
16753 int slen;
16754 int len;
16755 char_u *r;
16756 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016757
16758 n = get_tv_number(&argvars[1]);
16759 if (argvars[0].v_type == VAR_LIST)
16760 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016761 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016762 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016763 if (list_extend(rettv->vval.v_list,
16764 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016765 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016766 }
16767 else
16768 {
16769 p = get_tv_string(&argvars[0]);
16770 rettv->v_type = VAR_STRING;
16771 rettv->vval.v_string = NULL;
16772
16773 slen = (int)STRLEN(p);
16774 len = slen * n;
16775 if (len <= 0)
16776 return;
16777
16778 r = alloc(len + 1);
16779 if (r != NULL)
16780 {
16781 for (i = 0; i < n; i++)
16782 mch_memmove(r + i * slen, p, (size_t)slen);
16783 r[len] = NUL;
16784 }
16785
16786 rettv->vval.v_string = r;
16787 }
16788}
16789
16790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 * "resolve()" function
16792 */
16793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016794f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795{
16796 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016797#ifdef HAVE_READLINK
16798 char_u *buf = NULL;
16799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016801 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802#ifdef FEAT_SHORTCUT
16803 {
16804 char_u *v = NULL;
16805
16806 v = mch_resolve_shortcut(p);
16807 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016808 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016810 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811 }
16812#else
16813# ifdef HAVE_READLINK
16814 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815 char_u *cpy;
16816 int len;
16817 char_u *remain = NULL;
16818 char_u *q;
16819 int is_relative_to_current = FALSE;
16820 int has_trailing_pathsep = FALSE;
16821 int limit = 100;
16822
16823 p = vim_strsave(p);
16824
16825 if (p[0] == '.' && (vim_ispathsep(p[1])
16826 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16827 is_relative_to_current = TRUE;
16828
16829 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016830 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016831 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016833 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835
16836 q = getnextcomp(p);
16837 if (*q != NUL)
16838 {
16839 /* Separate the first path component in "p", and keep the
16840 * remainder (beginning with the path separator). */
16841 remain = vim_strsave(q - 1);
16842 q[-1] = NUL;
16843 }
16844
Bram Moolenaard9462e32011-04-11 21:35:11 +020016845 buf = alloc(MAXPATHL + 1);
16846 if (buf == NULL)
16847 goto fail;
16848
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 for (;;)
16850 {
16851 for (;;)
16852 {
16853 len = readlink((char *)p, (char *)buf, MAXPATHL);
16854 if (len <= 0)
16855 break;
16856 buf[len] = NUL;
16857
16858 if (limit-- == 0)
16859 {
16860 vim_free(p);
16861 vim_free(remain);
16862 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016863 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864 goto fail;
16865 }
16866
16867 /* Ensure that the result will have a trailing path separator
16868 * if the argument has one. */
16869 if (remain == NULL && has_trailing_pathsep)
16870 add_pathsep(buf);
16871
16872 /* Separate the first path component in the link value and
16873 * concatenate the remainders. */
16874 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16875 if (*q != NUL)
16876 {
16877 if (remain == NULL)
16878 remain = vim_strsave(q - 1);
16879 else
16880 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016881 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882 if (cpy != NULL)
16883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 vim_free(remain);
16885 remain = cpy;
16886 }
16887 }
16888 q[-1] = NUL;
16889 }
16890
16891 q = gettail(p);
16892 if (q > p && *q == NUL)
16893 {
16894 /* Ignore trailing path separator. */
16895 q[-1] = NUL;
16896 q = gettail(p);
16897 }
16898 if (q > p && !mch_isFullName(buf))
16899 {
16900 /* symlink is relative to directory of argument */
16901 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16902 if (cpy != NULL)
16903 {
16904 STRCPY(cpy, p);
16905 STRCPY(gettail(cpy), buf);
16906 vim_free(p);
16907 p = cpy;
16908 }
16909 }
16910 else
16911 {
16912 vim_free(p);
16913 p = vim_strsave(buf);
16914 }
16915 }
16916
16917 if (remain == NULL)
16918 break;
16919
16920 /* Append the first path component of "remain" to "p". */
16921 q = getnextcomp(remain + 1);
16922 len = q - remain - (*q != NUL);
16923 cpy = vim_strnsave(p, STRLEN(p) + len);
16924 if (cpy != NULL)
16925 {
16926 STRNCAT(cpy, remain, len);
16927 vim_free(p);
16928 p = cpy;
16929 }
16930 /* Shorten "remain". */
16931 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016932 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933 else
16934 {
16935 vim_free(remain);
16936 remain = NULL;
16937 }
16938 }
16939
16940 /* If the result is a relative path name, make it explicitly relative to
16941 * the current directory if and only if the argument had this form. */
16942 if (!vim_ispathsep(*p))
16943 {
16944 if (is_relative_to_current
16945 && *p != NUL
16946 && !(p[0] == '.'
16947 && (p[1] == NUL
16948 || vim_ispathsep(p[1])
16949 || (p[1] == '.'
16950 && (p[2] == NUL
16951 || vim_ispathsep(p[2]))))))
16952 {
16953 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016954 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016955 if (cpy != NULL)
16956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 vim_free(p);
16958 p = cpy;
16959 }
16960 }
16961 else if (!is_relative_to_current)
16962 {
16963 /* Strip leading "./". */
16964 q = p;
16965 while (q[0] == '.' && vim_ispathsep(q[1]))
16966 q += 2;
16967 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016968 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 }
16970 }
16971
16972 /* Ensure that the result will have no trailing path separator
16973 * if the argument had none. But keep "/" or "//". */
16974 if (!has_trailing_pathsep)
16975 {
16976 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016977 if (after_pathsep(p, q))
16978 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 }
16980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016981 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982 }
16983# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016984 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985# endif
16986#endif
16987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016988 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989
16990#ifdef HAVE_READLINK
16991fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016992 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016994 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995}
16996
16997/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 */
17000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017001f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002{
Bram Moolenaar33570922005-01-25 22:26:29 +000017003 list_T *l;
17004 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006 if (argvars[0].v_type != VAR_LIST)
17007 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017008 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017009 && !tv_check_lock(l->lv_lock,
17010 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011 {
17012 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017013 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017014 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015 while (li != NULL)
17016 {
17017 ni = li->li_prev;
17018 list_append(l, li);
17019 li = ni;
17020 }
17021 rettv->vval.v_list = l;
17022 rettv->v_type = VAR_LIST;
17023 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017024 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017026}
17027
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017028#define SP_NOMOVE 0x01 /* don't move cursor */
17029#define SP_REPEAT 0x02 /* repeat to find outer pair */
17030#define SP_RETCOUNT 0x04 /* return matchcount */
17031#define SP_SETPCMARK 0x08 /* set previous context mark */
17032#define SP_START 0x10 /* accept match at start position */
17033#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17034#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017035#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017036
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017037static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038
17039/*
17040 * Get flags for a search function.
17041 * Possibly sets "p_ws".
17042 * Returns BACKWARD, FORWARD or zero (for an error).
17043 */
17044 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017045get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017046{
17047 int dir = FORWARD;
17048 char_u *flags;
17049 char_u nbuf[NUMBUFLEN];
17050 int mask;
17051
17052 if (varp->v_type != VAR_UNKNOWN)
17053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017054 flags = get_tv_string_buf_chk(varp, nbuf);
17055 if (flags == NULL)
17056 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057 while (*flags != NUL)
17058 {
17059 switch (*flags)
17060 {
17061 case 'b': dir = BACKWARD; break;
17062 case 'w': p_ws = TRUE; break;
17063 case 'W': p_ws = FALSE; break;
17064 default: mask = 0;
17065 if (flagsp != NULL)
17066 switch (*flags)
17067 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017068 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017069 case 'e': mask = SP_END; break;
17070 case 'm': mask = SP_RETCOUNT; break;
17071 case 'n': mask = SP_NOMOVE; break;
17072 case 'p': mask = SP_SUBPAT; break;
17073 case 'r': mask = SP_REPEAT; break;
17074 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017075 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076 }
17077 if (mask == 0)
17078 {
17079 EMSG2(_(e_invarg2), flags);
17080 dir = 0;
17081 }
17082 else
17083 *flagsp |= mask;
17084 }
17085 if (dir == 0)
17086 break;
17087 ++flags;
17088 }
17089 }
17090 return dir;
17091}
17092
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017094 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017095 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017096 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017097search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017099 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100 char_u *pat;
17101 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017102 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017103 int save_p_ws = p_ws;
17104 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017105 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017106 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017107 proftime_T tm;
17108#ifdef FEAT_RELTIME
17109 long time_limit = 0;
17110#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017111 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017112 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017114 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017115 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017116 if (dir == 0)
17117 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017118 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017119 if (flags & SP_START)
17120 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017121 if (flags & SP_END)
17122 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017123 if (flags & SP_COLUMN)
17124 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017125
Bram Moolenaar76929292008-01-06 19:07:36 +000017126 /* Optional arguments: line number to stop searching and timeout. */
17127 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017128 {
17129 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17130 if (lnum_stop < 0)
17131 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017132#ifdef FEAT_RELTIME
17133 if (argvars[3].v_type != VAR_UNKNOWN)
17134 {
17135 time_limit = get_tv_number_chk(&argvars[3], NULL);
17136 if (time_limit < 0)
17137 goto theend;
17138 }
17139#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017140 }
17141
Bram Moolenaar76929292008-01-06 19:07:36 +000017142#ifdef FEAT_RELTIME
17143 /* Set the time limit, if there is one. */
17144 profile_setlimit(time_limit, &tm);
17145#endif
17146
Bram Moolenaar231334e2005-07-25 20:46:57 +000017147 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017148 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017149 * Check to make sure only those flags are set.
17150 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17151 * flags cannot be set. Check for that condition also.
17152 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017153 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017154 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017156 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017157 goto theend;
17158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017160 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017161 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017162 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017163 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017165 if (flags & SP_SUBPAT)
17166 retval = subpatnum;
17167 else
17168 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017169 if (flags & SP_SETPCMARK)
17170 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017172 if (match_pos != NULL)
17173 {
17174 /* Store the match cursor position */
17175 match_pos->lnum = pos.lnum;
17176 match_pos->col = pos.col + 1;
17177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 /* "/$" will put the cursor after the end of the line, may need to
17179 * correct that here */
17180 check_cursor();
17181 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017182
17183 /* If 'n' flag is used: restore cursor position. */
17184 if (flags & SP_NOMOVE)
17185 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017186 else
17187 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017188theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017190
17191 return retval;
17192}
17193
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017194#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017195
17196/*
17197 * round() is not in C90, use ceil() or floor() instead.
17198 */
17199 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017200vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017201{
17202 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17203}
17204
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017205/*
17206 * "round({float})" function
17207 */
17208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017209f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017210{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017211 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017212
17213 rettv->v_type = VAR_FLOAT;
17214 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017215 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017216 else
17217 rettv->vval.v_float = 0.0;
17218}
17219#endif
17220
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017221/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017222 * "screenattr()" function
17223 */
17224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017225f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017226{
17227 int row;
17228 int col;
17229 int c;
17230
17231 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17232 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17233 if (row < 0 || row >= screen_Rows
17234 || col < 0 || col >= screen_Columns)
17235 c = -1;
17236 else
17237 c = ScreenAttrs[LineOffset[row] + col];
17238 rettv->vval.v_number = c;
17239}
17240
17241/*
17242 * "screenchar()" function
17243 */
17244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017245f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017246{
17247 int row;
17248 int col;
17249 int off;
17250 int c;
17251
17252 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17253 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17254 if (row < 0 || row >= screen_Rows
17255 || col < 0 || col >= screen_Columns)
17256 c = -1;
17257 else
17258 {
17259 off = LineOffset[row] + col;
17260#ifdef FEAT_MBYTE
17261 if (enc_utf8 && ScreenLinesUC[off] != 0)
17262 c = ScreenLinesUC[off];
17263 else
17264#endif
17265 c = ScreenLines[off];
17266 }
17267 rettv->vval.v_number = c;
17268}
17269
17270/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017271 * "screencol()" function
17272 *
17273 * First column is 1 to be consistent with virtcol().
17274 */
17275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017276f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017277{
17278 rettv->vval.v_number = screen_screencol() + 1;
17279}
17280
17281/*
17282 * "screenrow()" function
17283 */
17284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017285f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017286{
17287 rettv->vval.v_number = screen_screenrow() + 1;
17288}
17289
17290/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017291 * "search()" function
17292 */
17293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017294f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017295{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017296 int flags = 0;
17297
17298 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299}
17300
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017302 * "searchdecl()" function
17303 */
17304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017305f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017306{
17307 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017308 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017309 int error = FALSE;
17310 char_u *name;
17311
17312 rettv->vval.v_number = 1; /* default: FAIL */
17313
17314 name = get_tv_string_chk(&argvars[0]);
17315 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017316 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017317 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017318 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17319 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17320 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017321 if (!error && name != NULL)
17322 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017323 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017324}
17325
17326/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017327 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017330searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331{
17332 char_u *spat, *mpat, *epat;
17333 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 int dir;
17336 int flags = 0;
17337 char_u nbuf1[NUMBUFLEN];
17338 char_u nbuf2[NUMBUFLEN];
17339 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017340 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017341 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017342 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017345 spat = get_tv_string_chk(&argvars[0]);
17346 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17347 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17348 if (spat == NULL || mpat == NULL || epat == NULL)
17349 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 /* Handle the optional fourth argument: flags */
17352 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017353 if (dir == 0)
17354 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017355
17356 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017357 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17358 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017359 if ((flags & (SP_END | SP_SUBPAT)) != 0
17360 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017361 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017362 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017363 goto theend;
17364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017366 /* Using 'r' implies 'W', otherwise it doesn't work. */
17367 if (flags & SP_REPEAT)
17368 p_ws = FALSE;
17369
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017370 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017371 if (argvars[3].v_type == VAR_UNKNOWN
17372 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 skip = (char_u *)"";
17374 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017376 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017377 if (argvars[5].v_type != VAR_UNKNOWN)
17378 {
17379 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17380 if (lnum_stop < 0)
17381 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017382#ifdef FEAT_RELTIME
17383 if (argvars[6].v_type != VAR_UNKNOWN)
17384 {
17385 time_limit = get_tv_number_chk(&argvars[6], NULL);
17386 if (time_limit < 0)
17387 goto theend;
17388 }
17389#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017390 }
17391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017392 if (skip == NULL)
17393 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017395 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017396 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017397
17398theend:
17399 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017400
17401 return retval;
17402}
17403
17404/*
17405 * "searchpair()" function
17406 */
17407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017408f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017409{
17410 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17411}
17412
17413/*
17414 * "searchpairpos()" function
17415 */
17416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017417f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017418{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017419 pos_T match_pos;
17420 int lnum = 0;
17421 int col = 0;
17422
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017423 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017424 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017425
17426 if (searchpair_cmn(argvars, &match_pos) > 0)
17427 {
17428 lnum = match_pos.lnum;
17429 col = match_pos.col;
17430 }
17431
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017432 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17433 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017434}
17435
17436/*
17437 * Search for a start/middle/end thing.
17438 * Used by searchpair(), see its documentation for the details.
17439 * Returns 0 or -1 for no match,
17440 */
17441 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017442do_searchpair(
17443 char_u *spat, /* start pattern */
17444 char_u *mpat, /* middle pattern */
17445 char_u *epat, /* end pattern */
17446 int dir, /* BACKWARD or FORWARD */
17447 char_u *skip, /* skip expression */
17448 int flags, /* SP_SETPCMARK and other SP_ values */
17449 pos_T *match_pos,
17450 linenr_T lnum_stop, /* stop at this line if not zero */
17451 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017452{
17453 char_u *save_cpo;
17454 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17455 long retval = 0;
17456 pos_T pos;
17457 pos_T firstpos;
17458 pos_T foundpos;
17459 pos_T save_cursor;
17460 pos_T save_pos;
17461 int n;
17462 int r;
17463 int nest = 1;
17464 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017465 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017466 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017467
17468 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17469 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017470 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017471
Bram Moolenaar76929292008-01-06 19:07:36 +000017472#ifdef FEAT_RELTIME
17473 /* Set the time limit, if there is one. */
17474 profile_setlimit(time_limit, &tm);
17475#endif
17476
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017477 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17478 * start/middle/end (pat3, for the top pair). */
17479 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17480 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17481 if (pat2 == NULL || pat3 == NULL)
17482 goto theend;
17483 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17484 if (*mpat == NUL)
17485 STRCPY(pat3, pat2);
17486 else
17487 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17488 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017489 if (flags & SP_START)
17490 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017491
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 save_cursor = curwin->w_cursor;
17493 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017494 clearpos(&firstpos);
17495 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 pat = pat3;
17497 for (;;)
17498 {
17499 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017500 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17502 /* didn't find it or found the first match again: FAIL */
17503 break;
17504
17505 if (firstpos.lnum == 0)
17506 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017507 if (equalpos(pos, foundpos))
17508 {
17509 /* Found the same position again. Can happen with a pattern that
17510 * has "\zs" at the end and searching backwards. Advance one
17511 * character and try again. */
17512 if (dir == BACKWARD)
17513 decl(&pos);
17514 else
17515 incl(&pos);
17516 }
17517 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017519 /* clear the start flag to avoid getting stuck here */
17520 options &= ~SEARCH_START;
17521
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 /* If the skip pattern matches, ignore this match. */
17523 if (*skip != NUL)
17524 {
17525 save_pos = curwin->w_cursor;
17526 curwin->w_cursor = pos;
17527 r = eval_to_bool(skip, &err, NULL, FALSE);
17528 curwin->w_cursor = save_pos;
17529 if (err)
17530 {
17531 /* Evaluating {skip} caused an error, break here. */
17532 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017533 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 break;
17535 }
17536 if (r)
17537 continue;
17538 }
17539
17540 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17541 {
17542 /* Found end when searching backwards or start when searching
17543 * forward: nested pair. */
17544 ++nest;
17545 pat = pat2; /* nested, don't search for middle */
17546 }
17547 else
17548 {
17549 /* Found end when searching forward or start when searching
17550 * backward: end of (nested) pair; or found middle in outer pair. */
17551 if (--nest == 1)
17552 pat = pat3; /* outer level, search for middle */
17553 }
17554
17555 if (nest == 0)
17556 {
17557 /* Found the match: return matchcount or line number. */
17558 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017559 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017561 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017562 if (flags & SP_SETPCMARK)
17563 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 curwin->w_cursor = pos;
17565 if (!(flags & SP_REPEAT))
17566 break;
17567 nest = 1; /* search for next unmatched */
17568 }
17569 }
17570
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017571 if (match_pos != NULL)
17572 {
17573 /* Store the match cursor position */
17574 match_pos->lnum = curwin->w_cursor.lnum;
17575 match_pos->col = curwin->w_cursor.col + 1;
17576 }
17577
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017579 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 curwin->w_cursor = save_cursor;
17581
17582theend:
17583 vim_free(pat2);
17584 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017585 if (p_cpo == empty_option)
17586 p_cpo = save_cpo;
17587 else
17588 /* Darn, evaluating the {skip} expression changed the value. */
17589 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017590
17591 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592}
17593
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017594/*
17595 * "searchpos()" function
17596 */
17597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017598f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017599{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017600 pos_T match_pos;
17601 int lnum = 0;
17602 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017603 int n;
17604 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017605
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017606 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017607 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017608
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017609 n = search_cmn(argvars, &match_pos, &flags);
17610 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017611 {
17612 lnum = match_pos.lnum;
17613 col = match_pos.col;
17614 }
17615
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017616 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17617 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017618 if (flags & SP_SUBPAT)
17619 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017620}
17621
Bram Moolenaar0d660222005-01-07 21:51:51 +000017622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017623f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017625#ifdef FEAT_CLIENTSERVER
17626 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017627 char_u *server = get_tv_string_chk(&argvars[0]);
17628 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629
Bram Moolenaar0d660222005-01-07 21:51:51 +000017630 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017631 if (server == NULL || reply == NULL)
17632 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017633 if (check_restricted() || check_secure())
17634 return;
17635# ifdef FEAT_X11
17636 if (check_connection() == FAIL)
17637 return;
17638# endif
17639
17640 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017642 EMSG(_("E258: Unable to send to client"));
17643 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017645 rettv->vval.v_number = 0;
17646#else
17647 rettv->vval.v_number = -1;
17648#endif
17649}
17650
Bram Moolenaar0d660222005-01-07 21:51:51 +000017651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017652f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017653{
17654 char_u *r = NULL;
17655
17656#ifdef FEAT_CLIENTSERVER
17657# ifdef WIN32
17658 r = serverGetVimNames();
17659# else
17660 make_connection();
17661 if (X_DISPLAY != NULL)
17662 r = serverGetVimNames(X_DISPLAY);
17663# endif
17664#endif
17665 rettv->v_type = VAR_STRING;
17666 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667}
17668
17669/*
17670 * "setbufvar()" function
17671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017673f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674{
17675 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017678 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 char_u nbuf[NUMBUFLEN];
17680
17681 if (check_restricted() || check_secure())
17682 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017683 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17684 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017685 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 varp = &argvars[2];
17687
17688 if (buf != NULL && varname != NULL && varp != NULL)
17689 {
17690 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692
17693 if (*varname == '&')
17694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017695 long numval;
17696 char_u *strval;
17697 int error = FALSE;
17698
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017700 numval = get_tv_number_chk(varp, &error);
17701 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017702 if (!error && strval != NULL)
17703 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 }
17705 else
17706 {
17707 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17708 if (bufvarname != NULL)
17709 {
17710 STRCPY(bufvarname, "b:");
17711 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017712 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017713 vim_free(bufvarname);
17714 }
17715 }
17716
17717 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720}
17721
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017723f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017724{
17725 dict_T *d;
17726 dictitem_T *di;
17727 char_u *csearch;
17728
17729 if (argvars[0].v_type != VAR_DICT)
17730 {
17731 EMSG(_(e_dictreq));
17732 return;
17733 }
17734
17735 if ((d = argvars[0].vval.v_dict) != NULL)
17736 {
17737 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17738 if (csearch != NULL)
17739 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017740#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017741 if (enc_utf8)
17742 {
17743 int pcc[MAX_MCO];
17744 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017745
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017746 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17747 }
17748 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017749#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017750 set_last_csearch(PTR2CHAR(csearch),
17751 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017752 }
17753
17754 di = dict_find(d, (char_u *)"forward", -1);
17755 if (di != NULL)
17756 set_csearch_direction(get_tv_number(&di->di_tv)
17757 ? FORWARD : BACKWARD);
17758
17759 di = dict_find(d, (char_u *)"until", -1);
17760 if (di != NULL)
17761 set_csearch_until(!!get_tv_number(&di->di_tv));
17762 }
17763}
17764
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765/*
17766 * "setcmdpos()" function
17767 */
17768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017769f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017771 int pos = (int)get_tv_number(&argvars[0]) - 1;
17772
17773 if (pos >= 0)
17774 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775}
17776
17777/*
17778 * "setline()" function
17779 */
17780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017781f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782{
17783 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017784 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017785 list_T *l = NULL;
17786 listitem_T *li = NULL;
17787 long added = 0;
17788 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017790 lnum = get_tv_lnum(&argvars[0]);
17791 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017793 l = argvars[1].vval.v_list;
17794 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017796 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017797 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017798
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017799 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017800 for (;;)
17801 {
17802 if (l != NULL)
17803 {
17804 /* list argument, get next string */
17805 if (li == NULL)
17806 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017807 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017808 li = li->li_next;
17809 }
17810
17811 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017812 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017813 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017814
17815 /* When coming here from Insert mode, sync undo, so that this can be
17816 * undone separately from what was previously inserted. */
17817 if (u_sync_once == 2)
17818 {
17819 u_sync_once = 1; /* notify that u_sync() was called */
17820 u_sync(TRUE);
17821 }
17822
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017823 if (lnum <= curbuf->b_ml.ml_line_count)
17824 {
17825 /* existing line, replace it */
17826 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17827 {
17828 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017829 if (lnum == curwin->w_cursor.lnum)
17830 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017831 rettv->vval.v_number = 0; /* OK */
17832 }
17833 }
17834 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17835 {
17836 /* lnum is one past the last line, append the line */
17837 ++added;
17838 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17839 rettv->vval.v_number = 0; /* OK */
17840 }
17841
17842 if (l == NULL) /* only one string argument */
17843 break;
17844 ++lnum;
17845 }
17846
17847 if (added > 0)
17848 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849}
17850
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017851static 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 +000017852
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017854 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017855 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017857set_qf_ll_list(
17858 win_T *wp UNUSED,
17859 typval_T *list_arg UNUSED,
17860 typval_T *action_arg UNUSED,
17861 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017862{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017863#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017864 char_u *act;
17865 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017866#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017867
Bram Moolenaar2641f772005-03-25 21:58:17 +000017868 rettv->vval.v_number = -1;
17869
17870#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017871 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017872 EMSG(_(e_listreq));
17873 else
17874 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017875 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017876
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017877 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017878 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017879 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017880 if (act == NULL)
17881 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017882 if (*act == 'a' || *act == 'r')
17883 action = *act;
17884 }
17885
Bram Moolenaar81484f42012-12-05 15:16:47 +010017886 if (l != NULL && set_errorlist(wp, l, action,
17887 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017888 rettv->vval.v_number = 0;
17889 }
17890#endif
17891}
17892
17893/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017894 * "setloclist()" function
17895 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017897f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017898{
17899 win_T *win;
17900
17901 rettv->vval.v_number = -1;
17902
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017903 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017904 if (win != NULL)
17905 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17906}
17907
17908/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017909 * "setmatches()" function
17910 */
17911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017912f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017913{
17914#ifdef FEAT_SEARCH_EXTRA
17915 list_T *l;
17916 listitem_T *li;
17917 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017918 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017919
17920 rettv->vval.v_number = -1;
17921 if (argvars[0].v_type != VAR_LIST)
17922 {
17923 EMSG(_(e_listreq));
17924 return;
17925 }
17926 if ((l = argvars[0].vval.v_list) != NULL)
17927 {
17928
17929 /* To some extent make sure that we are dealing with a list from
17930 * "getmatches()". */
17931 li = l->lv_first;
17932 while (li != NULL)
17933 {
17934 if (li->li_tv.v_type != VAR_DICT
17935 || (d = li->li_tv.vval.v_dict) == NULL)
17936 {
17937 EMSG(_(e_invarg));
17938 return;
17939 }
17940 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017941 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17942 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017943 && dict_find(d, (char_u *)"priority", -1) != NULL
17944 && dict_find(d, (char_u *)"id", -1) != NULL))
17945 {
17946 EMSG(_(e_invarg));
17947 return;
17948 }
17949 li = li->li_next;
17950 }
17951
17952 clear_matches(curwin);
17953 li = l->lv_first;
17954 while (li != NULL)
17955 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017956 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017957 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017958 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017959 char_u *group;
17960 int priority;
17961 int id;
17962 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017963
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017964 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017965 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17966 {
17967 if (s == NULL)
17968 {
17969 s = list_alloc();
17970 if (s == NULL)
17971 return;
17972 }
17973
17974 /* match from matchaddpos() */
17975 for (i = 1; i < 9; i++)
17976 {
17977 sprintf((char *)buf, (char *)"pos%d", i);
17978 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17979 {
17980 if (di->di_tv.v_type != VAR_LIST)
17981 return;
17982
17983 list_append_tv(s, &di->di_tv);
17984 s->lv_refcount++;
17985 }
17986 else
17987 break;
17988 }
17989 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017990
17991 group = get_dict_string(d, (char_u *)"group", FALSE);
17992 priority = (int)get_dict_number(d, (char_u *)"priority");
17993 id = (int)get_dict_number(d, (char_u *)"id");
17994 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17995 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17996 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017997 if (i == 0)
17998 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017999 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018000 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018001 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018002 }
18003 else
18004 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018005 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018006 list_unref(s);
18007 s = NULL;
18008 }
18009
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018010 li = li->li_next;
18011 }
18012 rettv->vval.v_number = 0;
18013 }
18014#endif
18015}
18016
18017/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018018 * "setpos()" function
18019 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018021f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018022{
18023 pos_T pos;
18024 int fnum;
18025 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018026 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018027
Bram Moolenaar08250432008-02-13 11:42:46 +000018028 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018029 name = get_tv_string_chk(argvars);
18030 if (name != NULL)
18031 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018032 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018033 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018034 if (--pos.col < 0)
18035 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018036 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018037 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018038 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018039 if (fnum == curbuf->b_fnum)
18040 {
18041 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018042 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018043 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018044 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018045 curwin->w_set_curswant = FALSE;
18046 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018047 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018048 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018049 }
18050 else
18051 EMSG(_(e_invarg));
18052 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018053 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18054 {
18055 /* set mark */
18056 if (setmark_pos(name[1], &pos, fnum) == OK)
18057 rettv->vval.v_number = 0;
18058 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018059 else
18060 EMSG(_(e_invarg));
18061 }
18062 }
18063}
18064
18065/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018066 * "setqflist()" function
18067 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018069f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018070{
18071 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18072}
18073
18074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 * "setreg()" function
18076 */
18077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018078f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079{
18080 int regname;
18081 char_u *strregname;
18082 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018083 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 int append;
18085 char_u yank_type;
18086 long block_len;
18087
18088 block_len = -1;
18089 yank_type = MAUTO;
18090 append = FALSE;
18091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018092 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018093 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018095 if (strregname == NULL)
18096 return; /* type error; errmsg already given */
18097 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 if (regname == 0 || regname == '@')
18099 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018101 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018103 stropt = get_tv_string_chk(&argvars[2]);
18104 if (stropt == NULL)
18105 return; /* type error */
18106 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 switch (*stropt)
18108 {
18109 case 'a': case 'A': /* append */
18110 append = TRUE;
18111 break;
18112 case 'v': case 'c': /* character-wise selection */
18113 yank_type = MCHAR;
18114 break;
18115 case 'V': case 'l': /* line-wise selection */
18116 yank_type = MLINE;
18117 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 case 'b': case Ctrl_V: /* block-wise selection */
18119 yank_type = MBLOCK;
18120 if (VIM_ISDIGIT(stropt[1]))
18121 {
18122 ++stropt;
18123 block_len = getdigits(&stropt) - 1;
18124 --stropt;
18125 }
18126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 }
18128 }
18129
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018130 if (argvars[1].v_type == VAR_LIST)
18131 {
18132 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018133 char_u **allocval;
18134 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018135 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018136 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018137 int len = argvars[1].vval.v_list->lv_len;
18138 listitem_T *li;
18139
Bram Moolenaar7d647822014-04-05 21:28:56 +020018140 /* First half: use for pointers to result lines; second half: use for
18141 * pointers to allocated copies. */
18142 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018143 if (lstval == NULL)
18144 return;
18145 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018146 allocval = lstval + len + 2;
18147 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018148
18149 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18150 li = li->li_next)
18151 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018152 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018153 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018154 goto free_lstval;
18155 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018156 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018157 /* Need to make a copy, next get_tv_string_buf_chk() will
18158 * overwrite the string. */
18159 strval = vim_strsave(buf);
18160 if (strval == NULL)
18161 goto free_lstval;
18162 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018163 }
18164 *curval++ = strval;
18165 }
18166 *curval++ = NULL;
18167
18168 write_reg_contents_lst(regname, lstval, -1,
18169 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018170free_lstval:
18171 while (curallocval > allocval)
18172 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018173 vim_free(lstval);
18174 }
18175 else
18176 {
18177 strval = get_tv_string_chk(&argvars[1]);
18178 if (strval == NULL)
18179 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018180 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018183 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184}
18185
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018186/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018187 * "settabvar()" function
18188 */
18189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018190f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018191{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018192#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018193 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018194 tabpage_T *tp;
18195#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018196 char_u *varname, *tabvarname;
18197 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018198
18199 rettv->vval.v_number = 0;
18200
18201 if (check_restricted() || check_secure())
18202 return;
18203
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018204#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018205 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018206#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018207 varname = get_tv_string_chk(&argvars[1]);
18208 varp = &argvars[2];
18209
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018210 if (varname != NULL && varp != NULL
18211#ifdef FEAT_WINDOWS
18212 && tp != NULL
18213#endif
18214 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018215 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018216#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018217 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018218 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018219#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018220
18221 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18222 if (tabvarname != NULL)
18223 {
18224 STRCPY(tabvarname, "t:");
18225 STRCPY(tabvarname + 2, varname);
18226 set_var(tabvarname, varp, TRUE);
18227 vim_free(tabvarname);
18228 }
18229
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018230#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018231 /* Restore current tabpage */
18232 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018233 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018234#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018235 }
18236}
18237
18238/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018239 * "settabwinvar()" function
18240 */
18241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018242f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018243{
18244 setwinvar(argvars, rettv, 1);
18245}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246
18247/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018248 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018251f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018253 setwinvar(argvars, rettv, 0);
18254}
18255
18256/*
18257 * "setwinvar()" and "settabwinvar()" functions
18258 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018259
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018261setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018262{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 win_T *win;
18264#ifdef FEAT_WINDOWS
18265 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018266 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018267 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018268#endif
18269 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018270 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018272 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273
18274 if (check_restricted() || check_secure())
18275 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018276
18277#ifdef FEAT_WINDOWS
18278 if (off == 1)
18279 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18280 else
18281 tp = curtab;
18282#endif
18283 win = find_win_by_nr(&argvars[off], tp);
18284 varname = get_tv_string_chk(&argvars[off + 1]);
18285 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286
18287 if (win != NULL && varname != NULL && varp != NULL)
18288 {
18289#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018290 need_switch_win = !(tp == curtab && win == curwin);
18291 if (!need_switch_win
18292 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018295 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018297 long numval;
18298 char_u *strval;
18299 int error = FALSE;
18300
18301 ++varname;
18302 numval = get_tv_number_chk(varp, &error);
18303 strval = get_tv_string_buf_chk(varp, nbuf);
18304 if (!error && strval != NULL)
18305 set_option_value(varname, numval, strval, OPT_LOCAL);
18306 }
18307 else
18308 {
18309 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18310 if (winvarname != NULL)
18311 {
18312 STRCPY(winvarname, "w:");
18313 STRCPY(winvarname + 2, varname);
18314 set_var(winvarname, varp, TRUE);
18315 vim_free(winvarname);
18316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317 }
18318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018320 if (need_switch_win)
18321 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322#endif
18323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324}
18325
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018326#ifdef FEAT_CRYPT
18327/*
18328 * "sha256({string})" function
18329 */
18330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018331f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018332{
18333 char_u *p;
18334
18335 p = get_tv_string(&argvars[0]);
18336 rettv->vval.v_string = vim_strsave(
18337 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18338 rettv->v_type = VAR_STRING;
18339}
18340#endif /* FEAT_CRYPT */
18341
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018343 * "shellescape({string})" function
18344 */
18345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018346f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018347{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018348 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018349 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018350 rettv->v_type = VAR_STRING;
18351}
18352
18353/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018354 * shiftwidth() function
18355 */
18356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018357f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018358{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018359 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018360}
18361
18362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018363 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 */
18365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018366f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018368 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369
Bram Moolenaar0d660222005-01-07 21:51:51 +000018370 p = get_tv_string(&argvars[0]);
18371 rettv->vval.v_string = vim_strsave(p);
18372 simplify_filename(rettv->vval.v_string); /* simplify in place */
18373 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374}
18375
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018376#ifdef FEAT_FLOAT
18377/*
18378 * "sin()" function
18379 */
18380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018381f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018382{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018383 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018384
18385 rettv->v_type = VAR_FLOAT;
18386 if (get_float_arg(argvars, &f) == OK)
18387 rettv->vval.v_float = sin(f);
18388 else
18389 rettv->vval.v_float = 0.0;
18390}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018391
18392/*
18393 * "sinh()" function
18394 */
18395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018396f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018397{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018398 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018399
18400 rettv->v_type = VAR_FLOAT;
18401 if (get_float_arg(argvars, &f) == OK)
18402 rettv->vval.v_float = sinh(f);
18403 else
18404 rettv->vval.v_float = 0.0;
18405}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018406#endif
18407
Bram Moolenaar0d660222005-01-07 21:51:51 +000018408static int
18409#ifdef __BORLANDC__
18410 _RTLENTRYF
18411#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018412 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413static int
18414#ifdef __BORLANDC__
18415 _RTLENTRYF
18416#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018417 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018418
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018419/* struct used in the array that's given to qsort() */
18420typedef struct
18421{
18422 listitem_T *item;
18423 int idx;
18424} sortItem_T;
18425
Bram Moolenaar0d660222005-01-07 21:51:51 +000018426static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018427static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018428static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018429#ifdef FEAT_FLOAT
18430static int item_compare_float;
18431#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018432static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018433static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018434static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018435static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018436static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018437#define ITEM_COMPARE_FAIL 999
18438
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018440 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018442 static int
18443#ifdef __BORLANDC__
18444_RTLENTRYF
18445#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018446item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018448 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018449 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018450 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018451 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018452 int res;
18453 char_u numbuf1[NUMBUFLEN];
18454 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018456 si1 = (sortItem_T *)s1;
18457 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018458 tv1 = &si1->item->li_tv;
18459 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018460
18461 if (item_compare_numbers)
18462 {
18463 long v1 = get_tv_number(tv1);
18464 long v2 = get_tv_number(tv2);
18465
18466 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18467 }
18468
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018469#ifdef FEAT_FLOAT
18470 if (item_compare_float)
18471 {
18472 float_T v1 = get_tv_float(tv1);
18473 float_T v2 = get_tv_float(tv2);
18474
18475 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18476 }
18477#endif
18478
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018479 /* tv2string() puts quotes around a string and allocates memory. Don't do
18480 * that for string variables. Use a single quote when comparing with a
18481 * non-string to do what the docs promise. */
18482 if (tv1->v_type == VAR_STRING)
18483 {
18484 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18485 p1 = (char_u *)"'";
18486 else
18487 p1 = tv1->vval.v_string;
18488 }
18489 else
18490 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18491 if (tv2->v_type == VAR_STRING)
18492 {
18493 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18494 p2 = (char_u *)"'";
18495 else
18496 p2 = tv2->vval.v_string;
18497 }
18498 else
18499 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018500 if (p1 == NULL)
18501 p1 = (char_u *)"";
18502 if (p2 == NULL)
18503 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018504 if (!item_compare_numeric)
18505 {
18506 if (item_compare_ic)
18507 res = STRICMP(p1, p2);
18508 else
18509 res = STRCMP(p1, p2);
18510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018512 {
18513 double n1, n2;
18514 n1 = strtod((char *)p1, (char **)&p1);
18515 n2 = strtod((char *)p2, (char **)&p2);
18516 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18517 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018518
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018519 /* When the result would be zero, compare the item indexes. Makes the
18520 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018521 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018522 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018523
Bram Moolenaar0d660222005-01-07 21:51:51 +000018524 vim_free(tofree1);
18525 vim_free(tofree2);
18526 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527}
18528
18529 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018530#ifdef __BORLANDC__
18531_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018533item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018534{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018535 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018536 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018537 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018538 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018539 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018541 /* shortcut after failure in previous call; compare all items equal */
18542 if (item_compare_func_err)
18543 return 0;
18544
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018545 si1 = (sortItem_T *)s1;
18546 si2 = (sortItem_T *)s2;
18547
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018548 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018549 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018550 copy_tv(&si1->item->li_tv, &argv[0]);
18551 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018552
18553 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018554 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018555 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18556 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018557 clear_tv(&argv[0]);
18558 clear_tv(&argv[1]);
18559
18560 if (res == FAIL)
18561 res = ITEM_COMPARE_FAIL;
18562 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018563 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18564 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018565 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018566 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018567
18568 /* When the result would be zero, compare the pointers themselves. Makes
18569 * the sort stable. */
18570 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018571 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018572
Bram Moolenaar0d660222005-01-07 21:51:51 +000018573 return res;
18574}
18575
18576/*
18577 * "sort({list})" function
18578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018580do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581{
Bram Moolenaar33570922005-01-25 22:26:29 +000018582 list_T *l;
18583 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018584 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018585 long len;
18586 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587
Bram Moolenaar0d660222005-01-07 21:51:51 +000018588 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018589 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 else
18591 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018592 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018593 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018594 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18595 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018596 return;
18597 rettv->vval.v_list = l;
18598 rettv->v_type = VAR_LIST;
18599 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600
Bram Moolenaar0d660222005-01-07 21:51:51 +000018601 len = list_len(l);
18602 if (len <= 1)
18603 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018604
Bram Moolenaar0d660222005-01-07 21:51:51 +000018605 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018606 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018607 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018608#ifdef FEAT_FLOAT
18609 item_compare_float = FALSE;
18610#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018611 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018612 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018613 if (argvars[1].v_type != VAR_UNKNOWN)
18614 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018615 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018616 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018617 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018618 else
18619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018620 int error = FALSE;
18621
18622 i = get_tv_number_chk(&argvars[1], &error);
18623 if (error)
18624 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018625 if (i == 1)
18626 item_compare_ic = TRUE;
18627 else
18628 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018629 if (item_compare_func != NULL)
18630 {
18631 if (STRCMP(item_compare_func, "n") == 0)
18632 {
18633 item_compare_func = NULL;
18634 item_compare_numeric = TRUE;
18635 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018636 else if (STRCMP(item_compare_func, "N") == 0)
18637 {
18638 item_compare_func = NULL;
18639 item_compare_numbers = TRUE;
18640 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018641#ifdef FEAT_FLOAT
18642 else if (STRCMP(item_compare_func, "f") == 0)
18643 {
18644 item_compare_func = NULL;
18645 item_compare_float = TRUE;
18646 }
18647#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018648 else if (STRCMP(item_compare_func, "i") == 0)
18649 {
18650 item_compare_func = NULL;
18651 item_compare_ic = TRUE;
18652 }
18653 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018655
18656 if (argvars[2].v_type != VAR_UNKNOWN)
18657 {
18658 /* optional third argument: {dict} */
18659 if (argvars[2].v_type != VAR_DICT)
18660 {
18661 EMSG(_(e_dictreq));
18662 return;
18663 }
18664 item_compare_selfdict = argvars[2].vval.v_dict;
18665 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667
Bram Moolenaar0d660222005-01-07 21:51:51 +000018668 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018669 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018670 if (ptrs == NULL)
18671 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672
Bram Moolenaar327aa022014-03-25 18:24:23 +010018673 i = 0;
18674 if (sort)
18675 {
18676 /* sort(): ptrs will be the list to sort */
18677 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018678 {
18679 ptrs[i].item = li;
18680 ptrs[i].idx = i;
18681 ++i;
18682 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018683
18684 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018685 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018686 /* test the compare function */
18687 if (item_compare_func != NULL
18688 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018689 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018690 EMSG(_("E702: Sort compare function failed"));
18691 else
18692 {
18693 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018694 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018695 item_compare_func == NULL ? item_compare : item_compare2);
18696
18697 if (!item_compare_func_err)
18698 {
18699 /* Clear the List and append the items in sorted order. */
18700 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18701 l->lv_len = 0;
18702 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018703 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018704 }
18705 }
18706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018708 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018709 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018710
18711 /* f_uniq(): ptrs will be a stack of items to remove */
18712 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018713 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018714 item_compare_func_ptr = item_compare_func
18715 ? item_compare2 : item_compare;
18716
18717 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18718 li = li->li_next)
18719 {
18720 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18721 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018722 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018723 if (item_compare_func_err)
18724 {
18725 EMSG(_("E882: Uniq compare function failed"));
18726 break;
18727 }
18728 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018730 if (!item_compare_func_err)
18731 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018732 while (--i >= 0)
18733 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018734 li = ptrs[i].item->li_next;
18735 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018736 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018737 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018738 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018739 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018740 list_fix_watch(l, li);
18741 listitem_free(li);
18742 l->lv_len--;
18743 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018744 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018745 }
18746
18747 vim_free(ptrs);
18748 }
18749}
18750
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018751/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018752 * "sort({list})" function
18753 */
18754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018755f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018756{
18757 do_sort_uniq(argvars, rettv, TRUE);
18758}
18759
18760/*
18761 * "uniq({list})" function
18762 */
18763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018764f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018765{
18766 do_sort_uniq(argvars, rettv, FALSE);
18767}
18768
18769/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018770 * "soundfold({word})" function
18771 */
18772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018773f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018774{
18775 char_u *s;
18776
18777 rettv->v_type = VAR_STRING;
18778 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018779#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018780 rettv->vval.v_string = eval_soundfold(s);
18781#else
18782 rettv->vval.v_string = vim_strsave(s);
18783#endif
18784}
18785
18786/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018787 * "spellbadword()" function
18788 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018790f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018791{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018792 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018793 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018794 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018795
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018796 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018797 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018798
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018799#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018800 if (argvars[0].v_type == VAR_UNKNOWN)
18801 {
18802 /* Find the start and length of the badly spelled word. */
18803 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18804 if (len != 0)
18805 word = ml_get_cursor();
18806 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018807 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018808 {
18809 char_u *str = get_tv_string_chk(&argvars[0]);
18810 int capcol = -1;
18811
18812 if (str != NULL)
18813 {
18814 /* Check the argument for spelling. */
18815 while (*str != NUL)
18816 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018817 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018818 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018819 {
18820 word = str;
18821 break;
18822 }
18823 str += len;
18824 }
18825 }
18826 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018827#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018828
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018829 list_append_string(rettv->vval.v_list, word, len);
18830 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018831 attr == HLF_SPB ? "bad" :
18832 attr == HLF_SPR ? "rare" :
18833 attr == HLF_SPL ? "local" :
18834 attr == HLF_SPC ? "caps" :
18835 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018836}
18837
18838/*
18839 * "spellsuggest()" function
18840 */
18841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018842f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018843{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018844#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018845 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018846 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018847 int maxcount;
18848 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018849 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018850 listitem_T *li;
18851 int need_capital = FALSE;
18852#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018853
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018854 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018855 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018856
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018857#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018858 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018859 {
18860 str = get_tv_string(&argvars[0]);
18861 if (argvars[1].v_type != VAR_UNKNOWN)
18862 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018863 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018864 if (maxcount <= 0)
18865 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018866 if (argvars[2].v_type != VAR_UNKNOWN)
18867 {
18868 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18869 if (typeerr)
18870 return;
18871 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018872 }
18873 else
18874 maxcount = 25;
18875
Bram Moolenaar4770d092006-01-12 23:22:24 +000018876 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018877
18878 for (i = 0; i < ga.ga_len; ++i)
18879 {
18880 str = ((char_u **)ga.ga_data)[i];
18881
18882 li = listitem_alloc();
18883 if (li == NULL)
18884 vim_free(str);
18885 else
18886 {
18887 li->li_tv.v_type = VAR_STRING;
18888 li->li_tv.v_lock = 0;
18889 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018890 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018891 }
18892 }
18893 ga_clear(&ga);
18894 }
18895#endif
18896}
18897
Bram Moolenaar0d660222005-01-07 21:51:51 +000018898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018899f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018900{
18901 char_u *str;
18902 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018903 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018904 regmatch_T regmatch;
18905 char_u patbuf[NUMBUFLEN];
18906 char_u *save_cpo;
18907 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018908 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018909 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018910 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018911
18912 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18913 save_cpo = p_cpo;
18914 p_cpo = (char_u *)"";
18915
18916 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018917 if (argvars[1].v_type != VAR_UNKNOWN)
18918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018919 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18920 if (pat == NULL)
18921 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018922 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018923 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018924 }
18925 if (pat == NULL || *pat == NUL)
18926 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018927
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018928 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018930 if (typeerr)
18931 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932
Bram Moolenaar0d660222005-01-07 21:51:51 +000018933 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18934 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018936 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018937 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018938 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018939 if (*str == NUL)
18940 match = FALSE; /* empty item at the end */
18941 else
18942 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018943 if (match)
18944 end = regmatch.startp[0];
18945 else
18946 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018947 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18948 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018949 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018950 if (list_append_string(rettv->vval.v_list, str,
18951 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018952 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018953 }
18954 if (!match)
18955 break;
18956 /* Advance to just after the match. */
18957 if (regmatch.endp[0] > str)
18958 col = 0;
18959 else
18960 {
18961 /* Don't get stuck at the same match. */
18962#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018963 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018964#else
18965 col = 1;
18966#endif
18967 }
18968 str = regmatch.endp[0];
18969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970
Bram Moolenaar473de612013-06-08 18:19:48 +020018971 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973
Bram Moolenaar0d660222005-01-07 21:51:51 +000018974 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975}
18976
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018977#ifdef FEAT_FLOAT
18978/*
18979 * "sqrt()" function
18980 */
18981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018982f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018983{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018984 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018985
18986 rettv->v_type = VAR_FLOAT;
18987 if (get_float_arg(argvars, &f) == OK)
18988 rettv->vval.v_float = sqrt(f);
18989 else
18990 rettv->vval.v_float = 0.0;
18991}
18992
18993/*
18994 * "str2float()" function
18995 */
18996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018997f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018998{
18999 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19000
19001 if (*p == '+')
19002 p = skipwhite(p + 1);
19003 (void)string2float(p, &rettv->vval.v_float);
19004 rettv->v_type = VAR_FLOAT;
19005}
19006#endif
19007
Bram Moolenaar2c932302006-03-18 21:42:09 +000019008/*
19009 * "str2nr()" function
19010 */
19011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019012f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019013{
19014 int base = 10;
19015 char_u *p;
19016 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019017 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019018
19019 if (argvars[1].v_type != VAR_UNKNOWN)
19020 {
19021 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019022 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019023 {
19024 EMSG(_(e_invarg));
19025 return;
19026 }
19027 }
19028
19029 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019030 if (*p == '+')
19031 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019032 switch (base)
19033 {
19034 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19035 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19036 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19037 default: what = 0;
19038 }
19039 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019040 rettv->vval.v_number = n;
19041}
19042
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043#ifdef HAVE_STRFTIME
19044/*
19045 * "strftime({format}[, {time}])" function
19046 */
19047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019048f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049{
19050 char_u result_buf[256];
19051 struct tm *curtime;
19052 time_t seconds;
19053 char_u *p;
19054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019055 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019057 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019058 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059 seconds = time(NULL);
19060 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019061 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 curtime = localtime(&seconds);
19063 /* MSVC returns NULL for an invalid value of seconds. */
19064 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019065 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066 else
19067 {
19068# ifdef FEAT_MBYTE
19069 vimconv_T conv;
19070 char_u *enc;
19071
19072 conv.vc_type = CONV_NONE;
19073 enc = enc_locale();
19074 convert_setup(&conv, p_enc, enc);
19075 if (conv.vc_type != CONV_NONE)
19076 p = string_convert(&conv, p, NULL);
19077# endif
19078 if (p != NULL)
19079 (void)strftime((char *)result_buf, sizeof(result_buf),
19080 (char *)p, curtime);
19081 else
19082 result_buf[0] = NUL;
19083
19084# ifdef FEAT_MBYTE
19085 if (conv.vc_type != CONV_NONE)
19086 vim_free(p);
19087 convert_setup(&conv, enc, p_enc);
19088 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019089 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 else
19091# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019092 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093
19094# ifdef FEAT_MBYTE
19095 /* Release conversion descriptors */
19096 convert_setup(&conv, NULL, NULL);
19097 vim_free(enc);
19098# endif
19099 }
19100}
19101#endif
19102
19103/*
19104 * "stridx()" function
19105 */
19106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019107f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108{
19109 char_u buf[NUMBUFLEN];
19110 char_u *needle;
19111 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019112 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019114 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019116 needle = get_tv_string_chk(&argvars[1]);
19117 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019118 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019119 if (needle == NULL || haystack == NULL)
19120 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121
Bram Moolenaar33570922005-01-25 22:26:29 +000019122 if (argvars[2].v_type != VAR_UNKNOWN)
19123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019124 int error = FALSE;
19125
19126 start_idx = get_tv_number_chk(&argvars[2], &error);
19127 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019128 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019129 if (start_idx >= 0)
19130 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019131 }
19132
19133 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19134 if (pos != NULL)
19135 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136}
19137
19138/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019139 * "string()" function
19140 */
19141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019142f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019143{
19144 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019145 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019147 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019148 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019149 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019150 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019151 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152}
19153
19154/*
19155 * "strlen()" function
19156 */
19157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019158f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019159{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019160 rettv->vval.v_number = (varnumber_T)(STRLEN(
19161 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162}
19163
19164/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019165 * "strchars()" function
19166 */
19167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019168f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019169{
19170 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019171 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019172#ifdef FEAT_MBYTE
19173 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019174 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019175#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019176
19177 if (argvars[1].v_type != VAR_UNKNOWN)
19178 skipcc = get_tv_number_chk(&argvars[1], NULL);
19179 if (skipcc < 0 || skipcc > 1)
19180 EMSG(_(e_invarg));
19181 else
19182 {
19183#ifdef FEAT_MBYTE
19184 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19185 while (*s != NUL)
19186 {
19187 func_mb_ptr2char_adv(&s);
19188 ++len;
19189 }
19190 rettv->vval.v_number = len;
19191#else
19192 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19193#endif
19194 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019195}
19196
19197/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019198 * "strdisplaywidth()" function
19199 */
19200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019201f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019202{
19203 char_u *s = get_tv_string(&argvars[0]);
19204 int col = 0;
19205
19206 if (argvars[1].v_type != VAR_UNKNOWN)
19207 col = get_tv_number(&argvars[1]);
19208
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019209 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019210}
19211
19212/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019213 * "strwidth()" function
19214 */
19215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019216f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019217{
19218 char_u *s = get_tv_string(&argvars[0]);
19219
19220 rettv->vval.v_number = (varnumber_T)(
19221#ifdef FEAT_MBYTE
19222 mb_string2cells(s, -1)
19223#else
19224 STRLEN(s)
19225#endif
19226 );
19227}
19228
19229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 * "strpart()" function
19231 */
19232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019233f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234{
19235 char_u *p;
19236 int n;
19237 int len;
19238 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019239 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019241 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 slen = (int)STRLEN(p);
19243
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019244 n = get_tv_number_chk(&argvars[1], &error);
19245 if (error)
19246 len = 0;
19247 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019248 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 else
19250 len = slen - n; /* default len: all bytes that are available. */
19251
19252 /*
19253 * Only return the overlap between the specified part and the actual
19254 * string.
19255 */
19256 if (n < 0)
19257 {
19258 len += n;
19259 n = 0;
19260 }
19261 else if (n > slen)
19262 n = slen;
19263 if (len < 0)
19264 len = 0;
19265 else if (n + len > slen)
19266 len = slen - n;
19267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019268 rettv->v_type = VAR_STRING;
19269 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270}
19271
19272/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019273 * "strridx()" function
19274 */
19275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019276f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019277{
19278 char_u buf[NUMBUFLEN];
19279 char_u *needle;
19280 char_u *haystack;
19281 char_u *rest;
19282 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019283 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019284
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019285 needle = get_tv_string_chk(&argvars[1]);
19286 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019287
19288 rettv->vval.v_number = -1;
19289 if (needle == NULL || haystack == NULL)
19290 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019291
19292 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019293 if (argvars[2].v_type != VAR_UNKNOWN)
19294 {
19295 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019296 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019297 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019298 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019299 }
19300 else
19301 end_idx = haystack_len;
19302
Bram Moolenaar0d660222005-01-07 21:51:51 +000019303 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019304 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019305 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019306 lastmatch = haystack + end_idx;
19307 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019308 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019309 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019310 for (rest = haystack; *rest != '\0'; ++rest)
19311 {
19312 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019313 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019314 break;
19315 lastmatch = rest;
19316 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019317 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019318
19319 if (lastmatch == NULL)
19320 rettv->vval.v_number = -1;
19321 else
19322 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19323}
19324
19325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 * "strtrans()" function
19327 */
19328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019329f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019331 rettv->v_type = VAR_STRING;
19332 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333}
19334
19335/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019336 * "submatch()" function
19337 */
19338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019339f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019340{
Bram Moolenaar41571762014-04-02 19:00:58 +020019341 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019342 int no;
19343 int retList = 0;
19344
19345 no = (int)get_tv_number_chk(&argvars[0], &error);
19346 if (error)
19347 return;
19348 error = FALSE;
19349 if (argvars[1].v_type != VAR_UNKNOWN)
19350 retList = get_tv_number_chk(&argvars[1], &error);
19351 if (error)
19352 return;
19353
19354 if (retList == 0)
19355 {
19356 rettv->v_type = VAR_STRING;
19357 rettv->vval.v_string = reg_submatch(no);
19358 }
19359 else
19360 {
19361 rettv->v_type = VAR_LIST;
19362 rettv->vval.v_list = reg_submatch_list(no);
19363 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019364}
19365
19366/*
19367 * "substitute()" function
19368 */
19369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019370f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019371{
19372 char_u patbuf[NUMBUFLEN];
19373 char_u subbuf[NUMBUFLEN];
19374 char_u flagsbuf[NUMBUFLEN];
19375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019376 char_u *str = get_tv_string_chk(&argvars[0]);
19377 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19378 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19379 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19380
Bram Moolenaar0d660222005-01-07 21:51:51 +000019381 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019382 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19383 rettv->vval.v_string = NULL;
19384 else
19385 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019386}
19387
19388/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019389 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019392f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393{
19394 int id = 0;
19395#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019396 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 long col;
19398 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019399 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019401 lnum = get_tv_lnum(argvars); /* -1 on type error */
19402 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19403 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019405 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019406 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019407 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408#endif
19409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019410 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411}
19412
19413/*
19414 * "synIDattr(id, what [, mode])" function
19415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019417f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418{
19419 char_u *p = NULL;
19420#ifdef FEAT_SYN_HL
19421 int id;
19422 char_u *what;
19423 char_u *mode;
19424 char_u modebuf[NUMBUFLEN];
19425 int modec;
19426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019427 id = get_tv_number(&argvars[0]);
19428 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019429 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019431 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019433 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 modec = 0; /* replace invalid with current */
19435 }
19436 else
19437 {
19438#ifdef FEAT_GUI
19439 if (gui.in_use)
19440 modec = 'g';
19441 else
19442#endif
19443 if (t_colors > 1)
19444 modec = 'c';
19445 else
19446 modec = 't';
19447 }
19448
19449
19450 switch (TOLOWER_ASC(what[0]))
19451 {
19452 case 'b':
19453 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19454 p = highlight_color(id, what, modec);
19455 else /* bold */
19456 p = highlight_has_attr(id, HL_BOLD, modec);
19457 break;
19458
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019459 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 p = highlight_color(id, what, modec);
19461 break;
19462
19463 case 'i':
19464 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19465 p = highlight_has_attr(id, HL_INVERSE, modec);
19466 else /* italic */
19467 p = highlight_has_attr(id, HL_ITALIC, modec);
19468 break;
19469
19470 case 'n': /* name */
19471 p = get_highlight_name(NULL, id - 1);
19472 break;
19473
19474 case 'r': /* reverse */
19475 p = highlight_has_attr(id, HL_INVERSE, modec);
19476 break;
19477
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019478 case 's':
19479 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19480 p = highlight_color(id, what, modec);
19481 else /* standout */
19482 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 break;
19484
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019485 case 'u':
19486 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19487 /* underline */
19488 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19489 else
19490 /* undercurl */
19491 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 break;
19493 }
19494
19495 if (p != NULL)
19496 p = vim_strsave(p);
19497#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019498 rettv->v_type = VAR_STRING;
19499 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500}
19501
19502/*
19503 * "synIDtrans(id)" function
19504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019506f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019507{
19508 int id;
19509
19510#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019511 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512
19513 if (id > 0)
19514 id = syn_get_final_id(id);
19515 else
19516#endif
19517 id = 0;
19518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019519 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520}
19521
19522/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019523 * "synconcealed(lnum, col)" function
19524 */
19525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019526f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019527{
19528#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19529 long lnum;
19530 long col;
19531 int syntax_flags = 0;
19532 int cchar;
19533 int matchid = 0;
19534 char_u str[NUMBUFLEN];
19535#endif
19536
19537 rettv->v_type = VAR_LIST;
19538 rettv->vval.v_list = NULL;
19539
19540#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19541 lnum = get_tv_lnum(argvars); /* -1 on type error */
19542 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19543
19544 vim_memset(str, NUL, sizeof(str));
19545
19546 if (rettv_list_alloc(rettv) != FAIL)
19547 {
19548 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19549 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19550 && curwin->w_p_cole > 0)
19551 {
19552 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19553 syntax_flags = get_syntax_info(&matchid);
19554
19555 /* get the conceal character */
19556 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19557 {
19558 cchar = syn_get_sub_char();
19559 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19560 cchar = lcs_conceal;
19561 if (cchar != NUL)
19562 {
19563# ifdef FEAT_MBYTE
19564 if (has_mbyte)
19565 (*mb_char2bytes)(cchar, str);
19566 else
19567# endif
19568 str[0] = cchar;
19569 }
19570 }
19571 }
19572
19573 list_append_number(rettv->vval.v_list,
19574 (syntax_flags & HL_CONCEAL) != 0);
19575 /* -1 to auto-determine strlen */
19576 list_append_string(rettv->vval.v_list, str, -1);
19577 list_append_number(rettv->vval.v_list, matchid);
19578 }
19579#endif
19580}
19581
19582/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019583 * "synstack(lnum, col)" function
19584 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019586f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019587{
19588#ifdef FEAT_SYN_HL
19589 long lnum;
19590 long col;
19591 int i;
19592 int id;
19593#endif
19594
19595 rettv->v_type = VAR_LIST;
19596 rettv->vval.v_list = NULL;
19597
19598#ifdef FEAT_SYN_HL
19599 lnum = get_tv_lnum(argvars); /* -1 on type error */
19600 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19601
19602 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019603 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019604 && rettv_list_alloc(rettv) != FAIL)
19605 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019606 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019607 for (i = 0; ; ++i)
19608 {
19609 id = syn_get_stack_item(i);
19610 if (id < 0)
19611 break;
19612 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19613 break;
19614 }
19615 }
19616#endif
19617}
19618
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019620get_cmd_output_as_rettv(
19621 typval_T *argvars,
19622 typval_T *rettv,
19623 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019625 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019627 char_u *infile = NULL;
19628 char_u buf[NUMBUFLEN];
19629 int err = FALSE;
19630 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019631 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019632 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019634 rettv->v_type = VAR_STRING;
19635 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019636 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019637 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019638
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019639 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019640 {
19641 /*
19642 * Write the string to a temp file, to be used for input of the shell
19643 * command.
19644 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019645 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019646 {
19647 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019648 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019649 }
19650
19651 fd = mch_fopen((char *)infile, WRITEBIN);
19652 if (fd == NULL)
19653 {
19654 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019655 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019656 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019657 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019658 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019659 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19660 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019661 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019662 else
19663 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019664 size_t len;
19665
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019666 p = get_tv_string_buf_chk(&argvars[1], buf);
19667 if (p == NULL)
19668 {
19669 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019670 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019671 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019672 len = STRLEN(p);
19673 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019674 err = TRUE;
19675 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019676 if (fclose(fd) != 0)
19677 err = TRUE;
19678 if (err)
19679 {
19680 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019681 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019682 }
19683 }
19684
Bram Moolenaar52a72462014-08-29 15:53:52 +020019685 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19686 * echoes typeahead, that messes up the display. */
19687 if (!msg_silent)
19688 flags += SHELL_COOKED;
19689
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019690 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019692 int len;
19693 listitem_T *li;
19694 char_u *s = NULL;
19695 char_u *start;
19696 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019697 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698
Bram Moolenaar52a72462014-08-29 15:53:52 +020019699 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019700 if (res == NULL)
19701 goto errret;
19702
19703 list = list_alloc();
19704 if (list == NULL)
19705 goto errret;
19706
19707 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019709 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019710 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019711 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019712 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019713
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019714 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019715 if (s == NULL)
19716 goto errret;
19717
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019718 for (p = s; start < end; ++p, ++start)
19719 *p = *start == NUL ? NL : *start;
19720 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019721
19722 li = listitem_alloc();
19723 if (li == NULL)
19724 {
19725 vim_free(s);
19726 goto errret;
19727 }
19728 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019729 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019730 li->li_tv.vval.v_string = s;
19731 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019733
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019734 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019735 rettv->v_type = VAR_LIST;
19736 rettv->vval.v_list = list;
19737 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019739 else
19740 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019741 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019742#ifdef USE_CR
19743 /* translate <CR> into <NL> */
19744 if (res != NULL)
19745 {
19746 char_u *s;
19747
19748 for (s = res; *s; ++s)
19749 {
19750 if (*s == CAR)
19751 *s = NL;
19752 }
19753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754#else
19755# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019756 /* translate <CR><NL> into <NL> */
19757 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019759 char_u *s, *d;
19760
19761 d = res;
19762 for (s = res; *s; ++s)
19763 {
19764 if (s[0] == CAR && s[1] == NL)
19765 ++s;
19766 *d++ = *s;
19767 }
19768 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770# endif
19771#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019772 rettv->vval.v_string = res;
19773 res = NULL;
19774 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019775
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019776errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019777 if (infile != NULL)
19778 {
19779 mch_remove(infile);
19780 vim_free(infile);
19781 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019782 if (res != NULL)
19783 vim_free(res);
19784 if (list != NULL)
19785 list_free(list, TRUE);
19786}
19787
19788/*
19789 * "system()" function
19790 */
19791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019792f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019793{
19794 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19795}
19796
19797/*
19798 * "systemlist()" function
19799 */
19800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019801f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019802{
19803 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804}
19805
19806/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019807 * "tabpagebuflist()" function
19808 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019810f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019811{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019812#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019813 tabpage_T *tp;
19814 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019815
19816 if (argvars[0].v_type == VAR_UNKNOWN)
19817 wp = firstwin;
19818 else
19819 {
19820 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19821 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019822 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019823 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019824 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019825 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019826 for (; wp != NULL; wp = wp->w_next)
19827 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019828 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019829 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019830 }
19831#endif
19832}
19833
19834
19835/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019836 * "tabpagenr()" function
19837 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019839f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019840{
19841 int nr = 1;
19842#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019843 char_u *arg;
19844
19845 if (argvars[0].v_type != VAR_UNKNOWN)
19846 {
19847 arg = get_tv_string_chk(&argvars[0]);
19848 nr = 0;
19849 if (arg != NULL)
19850 {
19851 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019852 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019853 else
19854 EMSG2(_(e_invexpr2), arg);
19855 }
19856 }
19857 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019858 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019859#endif
19860 rettv->vval.v_number = nr;
19861}
19862
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019863
19864#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019865static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019866
19867/*
19868 * Common code for tabpagewinnr() and winnr().
19869 */
19870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019871get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019872{
19873 win_T *twin;
19874 int nr = 1;
19875 win_T *wp;
19876 char_u *arg;
19877
19878 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19879 if (argvar->v_type != VAR_UNKNOWN)
19880 {
19881 arg = get_tv_string_chk(argvar);
19882 if (arg == NULL)
19883 nr = 0; /* type error; errmsg already given */
19884 else if (STRCMP(arg, "$") == 0)
19885 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19886 else if (STRCMP(arg, "#") == 0)
19887 {
19888 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19889 if (twin == NULL)
19890 nr = 0;
19891 }
19892 else
19893 {
19894 EMSG2(_(e_invexpr2), arg);
19895 nr = 0;
19896 }
19897 }
19898
19899 if (nr > 0)
19900 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19901 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019902 {
19903 if (wp == NULL)
19904 {
19905 /* didn't find it in this tabpage */
19906 nr = 0;
19907 break;
19908 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019909 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019910 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019911 return nr;
19912}
19913#endif
19914
19915/*
19916 * "tabpagewinnr()" function
19917 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019919f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019920{
19921 int nr = 1;
19922#ifdef FEAT_WINDOWS
19923 tabpage_T *tp;
19924
19925 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19926 if (tp == NULL)
19927 nr = 0;
19928 else
19929 nr = get_winnr(tp, &argvars[1]);
19930#endif
19931 rettv->vval.v_number = nr;
19932}
19933
19934
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019935/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019936 * "tagfiles()" function
19937 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019939f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019940{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019941 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019942 tagname_T tn;
19943 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019944
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019945 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019946 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019947 fname = alloc(MAXPATHL);
19948 if (fname == NULL)
19949 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019950
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019951 for (first = TRUE; ; first = FALSE)
19952 if (get_tagfname(&tn, first, fname) == FAIL
19953 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019954 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019955 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019956 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019957}
19958
19959/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019960 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019961 */
19962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019963f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019964{
19965 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019966
19967 tag_pattern = get_tv_string(&argvars[0]);
19968
19969 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019970 if (*tag_pattern == NUL)
19971 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019972
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019973 if (rettv_list_alloc(rettv) == OK)
19974 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019975}
19976
19977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 * "tempname()" function
19979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019981f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019982{
19983 static int x = 'A';
19984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019985 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019986 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987
19988 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19989 * names. Skip 'I' and 'O', they are used for shell redirection. */
19990 do
19991 {
19992 if (x == 'Z')
19993 x = '0';
19994 else if (x == '9')
19995 x = 'A';
19996 else
19997 {
19998#ifdef EBCDIC
19999 if (x == 'I')
20000 x = 'J';
20001 else if (x == 'R')
20002 x = 'S';
20003 else
20004#endif
20005 ++x;
20006 }
20007 } while (x == 'I' || x == 'O');
20008}
20009
20010/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020011 * "test(list)" function: Just checking the walls...
20012 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020014f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020015{
20016 /* Used for unit testing. Change the code below to your liking. */
20017#if 0
20018 listitem_T *li;
20019 list_T *l;
20020 char_u *bad, *good;
20021
20022 if (argvars[0].v_type != VAR_LIST)
20023 return;
20024 l = argvars[0].vval.v_list;
20025 if (l == NULL)
20026 return;
20027 li = l->lv_first;
20028 if (li == NULL)
20029 return;
20030 bad = get_tv_string(&li->li_tv);
20031 li = li->li_next;
20032 if (li == NULL)
20033 return;
20034 good = get_tv_string(&li->li_tv);
20035 rettv->vval.v_number = test_edit_score(bad, good);
20036#endif
20037}
20038
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020039#ifdef FEAT_FLOAT
20040/*
20041 * "tan()" function
20042 */
20043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020044f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020045{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020046 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020047
20048 rettv->v_type = VAR_FLOAT;
20049 if (get_float_arg(argvars, &f) == OK)
20050 rettv->vval.v_float = tan(f);
20051 else
20052 rettv->vval.v_float = 0.0;
20053}
20054
20055/*
20056 * "tanh()" function
20057 */
20058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020059f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020060{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020061 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020062
20063 rettv->v_type = VAR_FLOAT;
20064 if (get_float_arg(argvars, &f) == OK)
20065 rettv->vval.v_float = tanh(f);
20066 else
20067 rettv->vval.v_float = 0.0;
20068}
20069#endif
20070
Bram Moolenaard52d9742005-08-21 22:20:28 +000020071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 * "tolower(string)" function
20073 */
20074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020075f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076{
20077 char_u *p;
20078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020079 p = vim_strsave(get_tv_string(&argvars[0]));
20080 rettv->v_type = VAR_STRING;
20081 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082
20083 if (p != NULL)
20084 while (*p != NUL)
20085 {
20086#ifdef FEAT_MBYTE
20087 int l;
20088
20089 if (enc_utf8)
20090 {
20091 int c, lc;
20092
20093 c = utf_ptr2char(p);
20094 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020095 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 /* TODO: reallocate string when byte count changes. */
20097 if (utf_char2len(lc) == l)
20098 utf_char2bytes(lc, p);
20099 p += l;
20100 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020101 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102 p += l; /* skip multi-byte character */
20103 else
20104#endif
20105 {
20106 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20107 ++p;
20108 }
20109 }
20110}
20111
20112/*
20113 * "toupper(string)" function
20114 */
20115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020116f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020118 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020119 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120}
20121
20122/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020123 * "tr(string, fromstr, tostr)" function
20124 */
20125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020126f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020127{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020128 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020129 char_u *fromstr;
20130 char_u *tostr;
20131 char_u *p;
20132#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020133 int inlen;
20134 int fromlen;
20135 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020136 int idx;
20137 char_u *cpstr;
20138 int cplen;
20139 int first = TRUE;
20140#endif
20141 char_u buf[NUMBUFLEN];
20142 char_u buf2[NUMBUFLEN];
20143 garray_T ga;
20144
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020145 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020146 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20147 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020148
20149 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020150 rettv->v_type = VAR_STRING;
20151 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020152 if (fromstr == NULL || tostr == NULL)
20153 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020154 ga_init2(&ga, (int)sizeof(char), 80);
20155
20156#ifdef FEAT_MBYTE
20157 if (!has_mbyte)
20158#endif
20159 /* not multi-byte: fromstr and tostr must be the same length */
20160 if (STRLEN(fromstr) != STRLEN(tostr))
20161 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020162#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020163error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020164#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020165 EMSG2(_(e_invarg2), fromstr);
20166 ga_clear(&ga);
20167 return;
20168 }
20169
20170 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020171 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020172 {
20173#ifdef FEAT_MBYTE
20174 if (has_mbyte)
20175 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020176 inlen = (*mb_ptr2len)(in_str);
20177 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020178 cplen = inlen;
20179 idx = 0;
20180 for (p = fromstr; *p != NUL; p += fromlen)
20181 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020182 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020183 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020184 {
20185 for (p = tostr; *p != NUL; p += tolen)
20186 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020187 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020188 if (idx-- == 0)
20189 {
20190 cplen = tolen;
20191 cpstr = p;
20192 break;
20193 }
20194 }
20195 if (*p == NUL) /* tostr is shorter than fromstr */
20196 goto error;
20197 break;
20198 }
20199 ++idx;
20200 }
20201
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020202 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020203 {
20204 /* Check that fromstr and tostr have the same number of
20205 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020206 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020207 first = FALSE;
20208 for (p = tostr; *p != NUL; p += tolen)
20209 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020210 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020211 --idx;
20212 }
20213 if (idx != 0)
20214 goto error;
20215 }
20216
Bram Moolenaarcde88542015-08-11 19:14:00 +020020217 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020218 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020219 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020220
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020221 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020222 }
20223 else
20224#endif
20225 {
20226 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020227 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020228 if (p != NULL)
20229 ga_append(&ga, tostr[p - fromstr]);
20230 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020231 ga_append(&ga, *in_str);
20232 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020233 }
20234 }
20235
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020236 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020237 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020238 ga_append(&ga, NUL);
20239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020240 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020241}
20242
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020243#ifdef FEAT_FLOAT
20244/*
20245 * "trunc({float})" function
20246 */
20247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020248f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020249{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020250 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020251
20252 rettv->v_type = VAR_FLOAT;
20253 if (get_float_arg(argvars, &f) == OK)
20254 /* trunc() is not in C90, use floor() or ceil() instead. */
20255 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20256 else
20257 rettv->vval.v_float = 0.0;
20258}
20259#endif
20260
Bram Moolenaar8299df92004-07-10 09:47:34 +000020261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 * "type(expr)" function
20263 */
20264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020265f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020267 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020268
20269 switch (argvars[0].v_type)
20270 {
20271 case VAR_NUMBER: n = 0; break;
20272 case VAR_STRING: n = 1; break;
20273 case VAR_FUNC: n = 2; break;
20274 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020275 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020276 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020277 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020278 if (argvars[0].vval.v_number == VVAL_FALSE
20279 || argvars[0].vval.v_number == VVAL_TRUE)
20280 n = 6;
20281 else
20282 n = 7;
20283 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020284 case VAR_JOB: n = 8; break;
20285 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020286 case VAR_UNKNOWN:
20287 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20288 n = -1;
20289 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020290 }
20291 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292}
20293
20294/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020295 * "undofile(name)" function
20296 */
20297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020298f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020299{
20300 rettv->v_type = VAR_STRING;
20301#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020302 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020303 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020304
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020305 if (*fname == NUL)
20306 {
20307 /* If there is no file name there will be no undo file. */
20308 rettv->vval.v_string = NULL;
20309 }
20310 else
20311 {
20312 char_u *ffname = FullName_save(fname, FALSE);
20313
20314 if (ffname != NULL)
20315 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20316 vim_free(ffname);
20317 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020318 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020319#else
20320 rettv->vval.v_string = NULL;
20321#endif
20322}
20323
20324/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020325 * "undotree()" function
20326 */
20327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020328f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020329{
20330 if (rettv_dict_alloc(rettv) == OK)
20331 {
20332 dict_T *dict = rettv->vval.v_dict;
20333 list_T *list;
20334
Bram Moolenaar730cde92010-06-27 05:18:54 +020020335 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020336 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020337 dict_add_nr_str(dict, "save_last",
20338 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020339 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20340 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020341 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020342
20343 list = list_alloc();
20344 if (list != NULL)
20345 {
20346 u_eval_tree(curbuf->b_u_oldhead, list);
20347 dict_add_list(dict, "entries", list);
20348 }
20349 }
20350}
20351
20352/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020353 * "values(dict)" function
20354 */
20355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020356f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020357{
20358 dict_list(argvars, rettv, 1);
20359}
20360
20361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 * "virtcol(string)" function
20363 */
20364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020365f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366{
20367 colnr_T vcol = 0;
20368 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020369 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020371 fp = var2fpos(&argvars[0], FALSE, &fnum);
20372 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20373 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020374 {
20375 getvvcol(curwin, fp, NULL, NULL, &vcol);
20376 ++vcol;
20377 }
20378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020379 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380}
20381
20382/*
20383 * "visualmode()" function
20384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020386f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388 char_u str[2];
20389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020390 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 str[0] = curbuf->b_visual_mode_eval;
20392 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020393 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394
20395 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020396 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398}
20399
20400/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020401 * "wildmenumode()" function
20402 */
20403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020404f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020405{
20406#ifdef FEAT_WILDMENU
20407 if (wild_menu_showing)
20408 rettv->vval.v_number = 1;
20409#endif
20410}
20411
20412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413 * "winbufnr(nr)" function
20414 */
20415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020416f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417{
20418 win_T *wp;
20419
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020420 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020422 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020424 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425}
20426
20427/*
20428 * "wincol()" function
20429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020431f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432{
20433 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020434 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435}
20436
20437/*
20438 * "winheight(nr)" function
20439 */
20440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020441f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442{
20443 win_T *wp;
20444
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020445 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020447 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020449 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450}
20451
20452/*
20453 * "winline()" function
20454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020456f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457{
20458 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020459 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460}
20461
20462/*
20463 * "winnr()" function
20464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020466f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467{
20468 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020469
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020471 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020473 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474}
20475
20476/*
20477 * "winrestcmd()" function
20478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020480f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481{
20482#ifdef FEAT_WINDOWS
20483 win_T *wp;
20484 int winnr = 1;
20485 garray_T ga;
20486 char_u buf[50];
20487
20488 ga_init2(&ga, (int)sizeof(char), 70);
20489 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20490 {
20491 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20492 ga_concat(&ga, buf);
20493# ifdef FEAT_VERTSPLIT
20494 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20495 ga_concat(&ga, buf);
20496# endif
20497 ++winnr;
20498 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020499 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020501 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020503 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020505 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506}
20507
20508/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020509 * "winrestview()" function
20510 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020512f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020513{
20514 dict_T *dict;
20515
20516 if (argvars[0].v_type != VAR_DICT
20517 || (dict = argvars[0].vval.v_dict) == NULL)
20518 EMSG(_(e_invarg));
20519 else
20520 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020521 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20522 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20523 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20524 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020525#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020526 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20527 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020528#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020529 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20530 {
20531 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20532 curwin->w_set_curswant = FALSE;
20533 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020534
Bram Moolenaar82c25852014-05-28 16:47:16 +020020535 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20536 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020537#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020538 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20539 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020540#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020541 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20542 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20543 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20544 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020545
20546 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020547 win_new_height(curwin, curwin->w_height);
20548# ifdef FEAT_VERTSPLIT
20549 win_new_width(curwin, W_WIDTH(curwin));
20550# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020551 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020552
Bram Moolenaarb851a962014-10-31 15:45:52 +010020553 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020554 curwin->w_topline = 1;
20555 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20556 curwin->w_topline = curbuf->b_ml.ml_line_count;
20557#ifdef FEAT_DIFF
20558 check_topfill(curwin, TRUE);
20559#endif
20560 }
20561}
20562
20563/*
20564 * "winsaveview()" function
20565 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020567f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020568{
20569 dict_T *dict;
20570
Bram Moolenaara800b422010-06-27 01:15:55 +020020571 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020572 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020573 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020574
20575 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20576 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20577#ifdef FEAT_VIRTUALEDIT
20578 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20579#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020580 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020581 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20582
20583 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20584#ifdef FEAT_DIFF
20585 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20586#endif
20587 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20588 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20589}
20590
20591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 * "winwidth(nr)" function
20593 */
20594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020595f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596{
20597 win_T *wp;
20598
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020599 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020601 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020602 else
20603#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020604 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020606 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607#endif
20608}
20609
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020611 * "wordcount()" function
20612 */
20613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020614f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020615{
20616 if (rettv_dict_alloc(rettv) == FAIL)
20617 return;
20618 cursor_pos_info(rettv->vval.v_dict);
20619}
20620
20621/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020622 * Write list of strings to file
20623 */
20624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020625write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020626{
20627 listitem_T *li;
20628 int c;
20629 int ret = OK;
20630 char_u *s;
20631
20632 for (li = list->lv_first; li != NULL; li = li->li_next)
20633 {
20634 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20635 {
20636 if (*s == '\n')
20637 c = putc(NUL, fd);
20638 else
20639 c = putc(*s, fd);
20640 if (c == EOF)
20641 {
20642 ret = FAIL;
20643 break;
20644 }
20645 }
20646 if (!binary || li->li_next != NULL)
20647 if (putc('\n', fd) == EOF)
20648 {
20649 ret = FAIL;
20650 break;
20651 }
20652 if (ret == FAIL)
20653 {
20654 EMSG(_(e_write));
20655 break;
20656 }
20657 }
20658 return ret;
20659}
20660
20661/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020662 * "writefile()" function
20663 */
20664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020665f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020666{
20667 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020668 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020669 char_u *fname;
20670 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020671 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020672
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020673 if (check_restricted() || check_secure())
20674 return;
20675
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020676 if (argvars[0].v_type != VAR_LIST)
20677 {
20678 EMSG2(_(e_listarg), "writefile()");
20679 return;
20680 }
20681 if (argvars[0].vval.v_list == NULL)
20682 return;
20683
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020684 if (argvars[2].v_type != VAR_UNKNOWN)
20685 {
20686 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20687 binary = TRUE;
20688 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20689 append = TRUE;
20690 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020691
20692 /* Always open the file in binary mode, library functions have a mind of
20693 * their own about CR-LF conversion. */
20694 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020695 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20696 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020697 {
20698 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20699 ret = -1;
20700 }
20701 else
20702 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020703 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20704 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020705 fclose(fd);
20706 }
20707
20708 rettv->vval.v_number = ret;
20709}
20710
20711/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020712 * "xor(expr, expr)" function
20713 */
20714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020715f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020716{
20717 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20718 ^ get_tv_number_chk(&argvars[1], NULL);
20719}
20720
20721
20722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020724 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 */
20726 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020727var2fpos(
20728 typval_T *varp,
20729 int dollar_lnum, /* TRUE when $ is last line */
20730 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020732 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020734 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735
Bram Moolenaara5525202006-03-02 22:52:09 +000020736 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020737 if (varp->v_type == VAR_LIST)
20738 {
20739 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020740 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020741 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020742 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020743
20744 l = varp->vval.v_list;
20745 if (l == NULL)
20746 return NULL;
20747
20748 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020749 pos.lnum = list_find_nr(l, 0L, &error);
20750 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020751 return NULL; /* invalid line number */
20752
20753 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020754 pos.col = list_find_nr(l, 1L, &error);
20755 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020756 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020757 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020758
20759 /* We accept "$" for the column number: last column. */
20760 li = list_find(l, 1L);
20761 if (li != NULL && li->li_tv.v_type == VAR_STRING
20762 && li->li_tv.vval.v_string != NULL
20763 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20764 pos.col = len + 1;
20765
Bram Moolenaara5525202006-03-02 22:52:09 +000020766 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020767 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020768 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020769 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020770
Bram Moolenaara5525202006-03-02 22:52:09 +000020771#ifdef FEAT_VIRTUALEDIT
20772 /* Get the virtual offset. Defaults to zero. */
20773 pos.coladd = list_find_nr(l, 2L, &error);
20774 if (error)
20775 pos.coladd = 0;
20776#endif
20777
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020778 return &pos;
20779 }
20780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020781 name = get_tv_string_chk(varp);
20782 if (name == NULL)
20783 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020784 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020785 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020786 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20787 {
20788 if (VIsual_active)
20789 return &VIsual;
20790 return &curwin->w_cursor;
20791 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020792 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020794 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20796 return NULL;
20797 return pp;
20798 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020799
20800#ifdef FEAT_VIRTUALEDIT
20801 pos.coladd = 0;
20802#endif
20803
Bram Moolenaar477933c2007-07-17 14:32:23 +000020804 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020805 {
20806 pos.col = 0;
20807 if (name[1] == '0') /* "w0": first visible line */
20808 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020809 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020810 pos.lnum = curwin->w_topline;
20811 return &pos;
20812 }
20813 else if (name[1] == '$') /* "w$": last visible line */
20814 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020815 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020816 pos.lnum = curwin->w_botline - 1;
20817 return &pos;
20818 }
20819 }
20820 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020822 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020823 {
20824 pos.lnum = curbuf->b_ml.ml_line_count;
20825 pos.col = 0;
20826 }
20827 else
20828 {
20829 pos.lnum = curwin->w_cursor.lnum;
20830 pos.col = (colnr_T)STRLEN(ml_get_curline());
20831 }
20832 return &pos;
20833 }
20834 return NULL;
20835}
20836
20837/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020838 * Convert list in "arg" into a position and optional file number.
20839 * When "fnump" is NULL there is no file number, only 3 items.
20840 * Note that the column is passed on as-is, the caller may want to decrement
20841 * it to use 1 for the first column.
20842 * Return FAIL when conversion is not possible, doesn't check the position for
20843 * validity.
20844 */
20845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020846list2fpos(
20847 typval_T *arg,
20848 pos_T *posp,
20849 int *fnump,
20850 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020851{
20852 list_T *l = arg->vval.v_list;
20853 long i = 0;
20854 long n;
20855
Bram Moolenaar493c1782014-05-28 14:34:46 +020020856 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20857 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020858 if (arg->v_type != VAR_LIST
20859 || l == NULL
20860 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020861 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020862 return FAIL;
20863
20864 if (fnump != NULL)
20865 {
20866 n = list_find_nr(l, i++, NULL); /* fnum */
20867 if (n < 0)
20868 return FAIL;
20869 if (n == 0)
20870 n = curbuf->b_fnum; /* current buffer */
20871 *fnump = n;
20872 }
20873
20874 n = list_find_nr(l, i++, NULL); /* lnum */
20875 if (n < 0)
20876 return FAIL;
20877 posp->lnum = n;
20878
20879 n = list_find_nr(l, i++, NULL); /* col */
20880 if (n < 0)
20881 return FAIL;
20882 posp->col = n;
20883
20884#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020885 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020886 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020887 posp->coladd = 0;
20888 else
20889 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020890#endif
20891
Bram Moolenaar493c1782014-05-28 14:34:46 +020020892 if (curswantp != NULL)
20893 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20894
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020895 return OK;
20896}
20897
20898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 * Get the length of an environment variable name.
20900 * Advance "arg" to the first character after the name.
20901 * Return 0 for error.
20902 */
20903 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020904get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905{
20906 char_u *p;
20907 int len;
20908
20909 for (p = *arg; vim_isIDc(*p); ++p)
20910 ;
20911 if (p == *arg) /* no name found */
20912 return 0;
20913
20914 len = (int)(p - *arg);
20915 *arg = p;
20916 return len;
20917}
20918
20919/*
20920 * Get the length of the name of a function or internal variable.
20921 * "arg" is advanced to the first non-white character after the name.
20922 * Return 0 if something is wrong.
20923 */
20924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020925get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926{
20927 char_u *p;
20928 int len;
20929
20930 /* Find the end of the name. */
20931 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020932 {
20933 if (*p == ':')
20934 {
20935 /* "s:" is start of "s:var", but "n:" is not and can be used in
20936 * slice "[n:]". Also "xx:" is not a namespace. */
20937 len = (int)(p - *arg);
20938 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20939 || len > 1)
20940 break;
20941 }
20942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 if (p == *arg) /* no name found */
20944 return 0;
20945
20946 len = (int)(p - *arg);
20947 *arg = skipwhite(p);
20948
20949 return len;
20950}
20951
20952/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020953 * Get the length of the name of a variable or function.
20954 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020956 * Return -1 if curly braces expansion failed.
20957 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 * If the name contains 'magic' {}'s, expand them and return the
20959 * expanded name in an allocated string via 'alias' - caller must free.
20960 */
20961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020962get_name_len(
20963 char_u **arg,
20964 char_u **alias,
20965 int evaluate,
20966 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967{
20968 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969 char_u *p;
20970 char_u *expr_start;
20971 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972
20973 *alias = NULL; /* default to no alias */
20974
20975 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20976 && (*arg)[2] == (int)KE_SNR)
20977 {
20978 /* hard coded <SNR>, already translated */
20979 *arg += 3;
20980 return get_id_len(arg) + 3;
20981 }
20982 len = eval_fname_script(*arg);
20983 if (len > 0)
20984 {
20985 /* literal "<SID>", "s:" or "<SNR>" */
20986 *arg += len;
20987 }
20988
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020990 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020992 p = find_name_end(*arg, &expr_start, &expr_end,
20993 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 if (expr_start != NULL)
20995 {
20996 char_u *temp_string;
20997
20998 if (!evaluate)
20999 {
21000 len += (int)(p - *arg);
21001 *arg = skipwhite(p);
21002 return len;
21003 }
21004
21005 /*
21006 * Include any <SID> etc in the expanded string:
21007 * Thus the -len here.
21008 */
21009 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21010 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021011 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 *alias = temp_string;
21013 *arg = skipwhite(p);
21014 return (int)STRLEN(temp_string);
21015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016
21017 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021018 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 EMSG2(_(e_invexpr2), *arg);
21020
21021 return len;
21022}
21023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021024/*
21025 * Find the end of a variable or function name, taking care of magic braces.
21026 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21027 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021028 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021029 * Return a pointer to just after the name. Equal to "arg" if there is no
21030 * valid name.
21031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021033find_name_end(
21034 char_u *arg,
21035 char_u **expr_start,
21036 char_u **expr_end,
21037 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021039 int mb_nest = 0;
21040 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021042 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021044 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021046 *expr_start = NULL;
21047 *expr_end = NULL;
21048 }
21049
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021050 /* Quick check for valid starting character. */
21051 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21052 return arg;
21053
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021054 for (p = arg; *p != NUL
21055 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021056 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021057 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021058 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021059 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021060 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021061 if (*p == '\'')
21062 {
21063 /* skip over 'string' to avoid counting [ and ] inside it. */
21064 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21065 ;
21066 if (*p == NUL)
21067 break;
21068 }
21069 else if (*p == '"')
21070 {
21071 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21072 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21073 if (*p == '\\' && p[1] != NUL)
21074 ++p;
21075 if (*p == NUL)
21076 break;
21077 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021078 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21079 {
21080 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021081 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021082 len = (int)(p - arg);
21083 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021084 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021085 break;
21086 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021087
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021088 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021090 if (*p == '[')
21091 ++br_nest;
21092 else if (*p == ']')
21093 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021096 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021098 if (*p == '{')
21099 {
21100 mb_nest++;
21101 if (expr_start != NULL && *expr_start == NULL)
21102 *expr_start = p;
21103 }
21104 else if (*p == '}')
21105 {
21106 mb_nest--;
21107 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21108 *expr_end = p;
21109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 }
21112
21113 return p;
21114}
21115
21116/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021117 * Expands out the 'magic' {}'s in a variable/function name.
21118 * Note that this can call itself recursively, to deal with
21119 * constructs like foo{bar}{baz}{bam}
21120 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21121 * "in_start" ^
21122 * "expr_start" ^
21123 * "expr_end" ^
21124 * "in_end" ^
21125 *
21126 * Returns a new allocated string, which the caller must free.
21127 * Returns NULL for failure.
21128 */
21129 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021130make_expanded_name(
21131 char_u *in_start,
21132 char_u *expr_start,
21133 char_u *expr_end,
21134 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021135{
21136 char_u c1;
21137 char_u *retval = NULL;
21138 char_u *temp_result;
21139 char_u *nextcmd = NULL;
21140
21141 if (expr_end == NULL || in_end == NULL)
21142 return NULL;
21143 *expr_start = NUL;
21144 *expr_end = NUL;
21145 c1 = *in_end;
21146 *in_end = NUL;
21147
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021148 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021149 if (temp_result != NULL && nextcmd == NULL)
21150 {
21151 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21152 + (in_end - expr_end) + 1));
21153 if (retval != NULL)
21154 {
21155 STRCPY(retval, in_start);
21156 STRCAT(retval, temp_result);
21157 STRCAT(retval, expr_end + 1);
21158 }
21159 }
21160 vim_free(temp_result);
21161
21162 *in_end = c1; /* put char back for error messages */
21163 *expr_start = '{';
21164 *expr_end = '}';
21165
21166 if (retval != NULL)
21167 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021168 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021169 if (expr_start != NULL)
21170 {
21171 /* Further expansion! */
21172 temp_result = make_expanded_name(retval, expr_start,
21173 expr_end, temp_result);
21174 vim_free(retval);
21175 retval = temp_result;
21176 }
21177 }
21178
21179 return retval;
21180}
21181
21182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021184 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 */
21186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021187eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021189 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21190}
21191
21192/*
21193 * Return TRUE if character "c" can be used as the first character in a
21194 * variable or function name (excluding '{' and '}').
21195 */
21196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021197eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021198{
21199 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200}
21201
21202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 * Set number v: variable to "val".
21204 */
21205 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021206set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021208 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209}
21210
21211/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021212 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 */
21214 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021215get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021217 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218}
21219
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021220/*
21221 * Get string v: variable value. Uses a static buffer, can only be used once.
21222 */
21223 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021224get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021225{
21226 return get_tv_string(&vimvars[idx].vv_tv);
21227}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021228
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021230 * Get List v: variable value. Caller must take care of reference count when
21231 * needed.
21232 */
21233 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021234get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021235{
21236 return vimvars[idx].vv_list;
21237}
21238
21239/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021240 * Set v:char to character "c".
21241 */
21242 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021243set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021244{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021245 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021246
21247#ifdef FEAT_MBYTE
21248 if (has_mbyte)
21249 buf[(*mb_char2bytes)(c, buf)] = NUL;
21250 else
21251#endif
21252 {
21253 buf[0] = c;
21254 buf[1] = NUL;
21255 }
21256 set_vim_var_string(VV_CHAR, buf, -1);
21257}
21258
21259/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021260 * Set v:count to "count" and v:count1 to "count1".
21261 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 */
21263 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021264set_vcount(
21265 long count,
21266 long count1,
21267 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021269 if (set_prevcount)
21270 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021271 vimvars[VV_COUNT].vv_nr = count;
21272 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273}
21274
21275/*
21276 * Set string v: variable to a copy of "val".
21277 */
21278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021279set_vim_var_string(
21280 int idx,
21281 char_u *val,
21282 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283{
Bram Moolenaara542c682016-01-31 16:28:04 +010021284 clear_tv(&vimvars[idx].vv_di.di_tv);
21285 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021287 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021289 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021291 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292}
21293
21294/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021295 * Set List v: variable to "val".
21296 */
21297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021298set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021299{
Bram Moolenaara542c682016-01-31 16:28:04 +010021300 clear_tv(&vimvars[idx].vv_di.di_tv);
21301 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021302 vimvars[idx].vv_list = val;
21303 if (val != NULL)
21304 ++val->lv_refcount;
21305}
21306
21307/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021308 * Set Dictionary v: variable to "val".
21309 */
21310 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021311set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021312{
21313 int todo;
21314 hashitem_T *hi;
21315
Bram Moolenaara542c682016-01-31 16:28:04 +010021316 clear_tv(&vimvars[idx].vv_di.di_tv);
21317 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021318 vimvars[idx].vv_dict = val;
21319 if (val != NULL)
21320 {
21321 ++val->dv_refcount;
21322
21323 /* Set readonly */
21324 todo = (int)val->dv_hashtab.ht_used;
21325 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21326 {
21327 if (HASHITEM_EMPTY(hi))
21328 continue;
21329 --todo;
21330 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21331 }
21332 }
21333}
21334
21335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 * Set v:register if needed.
21337 */
21338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021339set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340{
21341 char_u regname;
21342
21343 if (c == 0 || c == ' ')
21344 regname = '"';
21345 else
21346 regname = c;
21347 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021348 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 set_vim_var_string(VV_REG, &regname, 1);
21350}
21351
21352/*
21353 * Get or set v:exception. If "oldval" == NULL, return the current value.
21354 * Otherwise, restore the value to "oldval" and return NULL.
21355 * Must always be called in pairs to save and restore v:exception! Does not
21356 * take care of memory allocations.
21357 */
21358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021359v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360{
21361 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021362 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363
Bram Moolenaare9a41262005-01-15 22:18:47 +000021364 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 return NULL;
21366}
21367
21368/*
21369 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21370 * Otherwise, restore the value to "oldval" and return NULL.
21371 * Must always be called in pairs to save and restore v:throwpoint! Does not
21372 * take care of memory allocations.
21373 */
21374 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021375v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376{
21377 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021378 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379
Bram Moolenaare9a41262005-01-15 22:18:47 +000021380 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381 return NULL;
21382}
21383
21384#if defined(FEAT_AUTOCMD) || defined(PROTO)
21385/*
21386 * Set v:cmdarg.
21387 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21388 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21389 * Must always be called in pairs!
21390 */
21391 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021392set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393{
21394 char_u *oldval;
21395 char_u *newval;
21396 unsigned len;
21397
Bram Moolenaare9a41262005-01-15 22:18:47 +000021398 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021399 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021400 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021401 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021402 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021403 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 }
21405
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021406 if (eap->force_bin == FORCE_BIN)
21407 len = 6;
21408 else if (eap->force_bin == FORCE_NOBIN)
21409 len = 8;
21410 else
21411 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021412
21413 if (eap->read_edit)
21414 len += 7;
21415
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021416 if (eap->force_ff != 0)
21417 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21418# ifdef FEAT_MBYTE
21419 if (eap->force_enc != 0)
21420 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021421 if (eap->bad_char != 0)
21422 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021423# endif
21424
21425 newval = alloc(len + 1);
21426 if (newval == NULL)
21427 return NULL;
21428
21429 if (eap->force_bin == FORCE_BIN)
21430 sprintf((char *)newval, " ++bin");
21431 else if (eap->force_bin == FORCE_NOBIN)
21432 sprintf((char *)newval, " ++nobin");
21433 else
21434 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021435
21436 if (eap->read_edit)
21437 STRCAT(newval, " ++edit");
21438
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021439 if (eap->force_ff != 0)
21440 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21441 eap->cmd + eap->force_ff);
21442# ifdef FEAT_MBYTE
21443 if (eap->force_enc != 0)
21444 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21445 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021446 if (eap->bad_char == BAD_KEEP)
21447 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21448 else if (eap->bad_char == BAD_DROP)
21449 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21450 else if (eap->bad_char != 0)
21451 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021452# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021453 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021454 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455}
21456#endif
21457
21458/*
21459 * Get the value of internal variable "name".
21460 * Return OK or FAIL.
21461 */
21462 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021463get_var_tv(
21464 char_u *name,
21465 int len, /* length of "name" */
21466 typval_T *rettv, /* NULL when only checking existence */
21467 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21468 int verbose, /* may give error message */
21469 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470{
21471 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021472 typval_T *tv = NULL;
21473 typval_T atv;
21474 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476
21477 /* truncate the name, so that we can use strcmp() */
21478 cc = name[len];
21479 name[len] = NUL;
21480
21481 /*
21482 * Check for "b:changedtick".
21483 */
21484 if (STRCMP(name, "b:changedtick") == 0)
21485 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021486 atv.v_type = VAR_NUMBER;
21487 atv.vval.v_number = curbuf->b_changedtick;
21488 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021489 }
21490
21491 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 * Check for user-defined variables.
21493 */
21494 else
21495 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021496 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021498 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021499 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021500 if (dip != NULL)
21501 *dip = v;
21502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 }
21504
Bram Moolenaare9a41262005-01-15 22:18:47 +000021505 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021507 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021508 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 ret = FAIL;
21510 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021511 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021512 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513
21514 name[len] = cc;
21515
21516 return ret;
21517}
21518
21519/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021520 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21521 * Also handle function call with Funcref variable: func(expr)
21522 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21523 */
21524 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021525handle_subscript(
21526 char_u **arg,
21527 typval_T *rettv,
21528 int evaluate, /* do more than finding the end */
21529 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021530{
21531 int ret = OK;
21532 dict_T *selfdict = NULL;
21533 char_u *s;
21534 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021535 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021536
21537 while (ret == OK
21538 && (**arg == '['
21539 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021540 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021541 && !vim_iswhite(*(*arg - 1)))
21542 {
21543 if (**arg == '(')
21544 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021545 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021546 if (evaluate)
21547 {
21548 functv = *rettv;
21549 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021550
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021551 /* Invoke the function. Recursive! */
21552 s = functv.vval.v_string;
21553 }
21554 else
21555 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021556 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021557 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21558 &len, evaluate, selfdict);
21559
21560 /* Clear the funcref afterwards, so that deleting it while
21561 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021562 if (evaluate)
21563 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021564
21565 /* Stop the expression evaluation when immediately aborting on
21566 * error, or when an interrupt occurred or an exception was thrown
21567 * but not caught. */
21568 if (aborting())
21569 {
21570 if (ret == OK)
21571 clear_tv(rettv);
21572 ret = FAIL;
21573 }
21574 dict_unref(selfdict);
21575 selfdict = NULL;
21576 }
21577 else /* **arg == '[' || **arg == '.' */
21578 {
21579 dict_unref(selfdict);
21580 if (rettv->v_type == VAR_DICT)
21581 {
21582 selfdict = rettv->vval.v_dict;
21583 if (selfdict != NULL)
21584 ++selfdict->dv_refcount;
21585 }
21586 else
21587 selfdict = NULL;
21588 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21589 {
21590 clear_tv(rettv);
21591 ret = FAIL;
21592 }
21593 }
21594 }
21595 dict_unref(selfdict);
21596 return ret;
21597}
21598
21599/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021600 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021601 * value).
21602 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021603 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021604alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021605{
Bram Moolenaar33570922005-01-25 22:26:29 +000021606 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021607}
21608
21609/*
21610 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 * The string "s" must have been allocated, it is consumed.
21612 * Return NULL for out of memory, the variable otherwise.
21613 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021614 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021615alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616{
Bram Moolenaar33570922005-01-25 22:26:29 +000021617 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021619 rettv = alloc_tv();
21620 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021622 rettv->v_type = VAR_STRING;
21623 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 }
21625 else
21626 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021627 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628}
21629
21630/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021631 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021634free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635{
21636 if (varp != NULL)
21637 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021638 switch (varp->v_type)
21639 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021640 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021641 func_unref(varp->vval.v_string);
21642 /*FALLTHROUGH*/
21643 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021644 vim_free(varp->vval.v_string);
21645 break;
21646 case VAR_LIST:
21647 list_unref(varp->vval.v_list);
21648 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021649 case VAR_DICT:
21650 dict_unref(varp->vval.v_dict);
21651 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021652 case VAR_JOB:
21653#ifdef FEAT_JOB
21654 job_unref(varp->vval.v_job);
21655 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021656#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021657 case VAR_CHANNEL:
21658#ifdef FEAT_CHANNEL
21659 channel_unref(varp->vval.v_channel);
21660 break;
21661#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021662 case VAR_NUMBER:
21663 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021664 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021665 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021666 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 vim_free(varp);
21669 }
21670}
21671
21672/*
21673 * Free the memory for a variable value and set the value to NULL or 0.
21674 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021676clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677{
21678 if (varp != NULL)
21679 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021680 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021682 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021683 func_unref(varp->vval.v_string);
21684 /*FALLTHROUGH*/
21685 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021686 vim_free(varp->vval.v_string);
21687 varp->vval.v_string = NULL;
21688 break;
21689 case VAR_LIST:
21690 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021691 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021692 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021693 case VAR_DICT:
21694 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021695 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021696 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021697 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021698 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021699 varp->vval.v_number = 0;
21700 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021701 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021702#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021703 varp->vval.v_float = 0.0;
21704 break;
21705#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021706 case VAR_JOB:
21707#ifdef FEAT_JOB
21708 job_unref(varp->vval.v_job);
21709 varp->vval.v_job = NULL;
21710#endif
21711 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021712 case VAR_CHANNEL:
21713#ifdef FEAT_CHANNEL
21714 channel_unref(varp->vval.v_channel);
21715 varp->vval.v_channel = NULL;
21716#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021717 case VAR_UNKNOWN:
21718 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021720 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721 }
21722}
21723
21724/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021725 * Set the value of a variable to NULL without freeing items.
21726 */
21727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021728init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021729{
21730 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021731 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021732}
21733
21734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735 * Get the number value of a variable.
21736 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021737 * For incompatible types, return 0.
21738 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21739 * caller of incompatible types: it sets *denote to TRUE if "denote"
21740 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 */
21742 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021743get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021745 int error = FALSE;
21746
21747 return get_tv_number_chk(varp, &error); /* return 0L on error */
21748}
21749
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021750 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021751get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021752{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021753 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021755 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021757 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021758 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021759 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021760#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021761 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021762 break;
21763#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021764 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021765 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021766 break;
21767 case VAR_STRING:
21768 if (varp->vval.v_string != NULL)
21769 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021770 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021771 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021772 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021773 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021774 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021775 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021776 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021777 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021778 case VAR_SPECIAL:
21779 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21780 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021781 case VAR_JOB:
21782#ifdef FEAT_JOB
21783 EMSG(_("E910: Using a Job as a Number"));
21784 break;
21785#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021786 case VAR_CHANNEL:
21787#ifdef FEAT_CHANNEL
21788 EMSG(_("E913: Using a Channel as a Number"));
21789 break;
21790#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021791 case VAR_UNKNOWN:
21792 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021793 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021795 if (denote == NULL) /* useful for values that must be unsigned */
21796 n = -1;
21797 else
21798 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021799 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800}
21801
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021802#ifdef FEAT_FLOAT
21803 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021804get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021805{
21806 switch (varp->v_type)
21807 {
21808 case VAR_NUMBER:
21809 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021810 case VAR_FLOAT:
21811 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021812 case VAR_FUNC:
21813 EMSG(_("E891: Using a Funcref as a Float"));
21814 break;
21815 case VAR_STRING:
21816 EMSG(_("E892: Using a String as a Float"));
21817 break;
21818 case VAR_LIST:
21819 EMSG(_("E893: Using a List as a Float"));
21820 break;
21821 case VAR_DICT:
21822 EMSG(_("E894: Using a Dictionary as a Float"));
21823 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021824 case VAR_SPECIAL:
21825 EMSG(_("E907: Using a special value as a Float"));
21826 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021827 case VAR_JOB:
21828# ifdef FEAT_JOB
21829 EMSG(_("E911: Using a Job as a Float"));
21830 break;
21831# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021832 case VAR_CHANNEL:
21833# ifdef FEAT_CHANNEL
21834 EMSG(_("E914: Using a Channel as a Float"));
21835 break;
21836# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021837 case VAR_UNKNOWN:
21838 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021839 break;
21840 }
21841 return 0;
21842}
21843#endif
21844
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021846 * Get the lnum from the first argument.
21847 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021848 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 */
21850 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021851get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852{
Bram Moolenaar33570922005-01-25 22:26:29 +000021853 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 linenr_T lnum;
21855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021856 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 if (lnum == 0) /* no valid number, try using line() */
21858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021859 rettv.v_type = VAR_NUMBER;
21860 f_line(argvars, &rettv);
21861 lnum = rettv.vval.v_number;
21862 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 }
21864 return lnum;
21865}
21866
21867/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021868 * Get the lnum from the first argument.
21869 * Also accepts "$", then "buf" is used.
21870 * Returns 0 on error.
21871 */
21872 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021873get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021874{
21875 if (argvars[0].v_type == VAR_STRING
21876 && argvars[0].vval.v_string != NULL
21877 && argvars[0].vval.v_string[0] == '$'
21878 && buf != NULL)
21879 return buf->b_ml.ml_line_count;
21880 return get_tv_number_chk(&argvars[0], NULL);
21881}
21882
21883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 * Get the string value of a variable.
21885 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021886 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21887 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 * If the String variable has never been set, return an empty string.
21889 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021890 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21891 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 */
21893 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021894get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895{
21896 static char_u mybuf[NUMBUFLEN];
21897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021898 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899}
21900
21901 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021902get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021904 char_u *res = get_tv_string_buf_chk(varp, buf);
21905
21906 return res != NULL ? res : (char_u *)"";
21907}
21908
Bram Moolenaar7d647822014-04-05 21:28:56 +020021909/*
21910 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21911 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021912 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021913get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021914{
21915 static char_u mybuf[NUMBUFLEN];
21916
21917 return get_tv_string_buf_chk(varp, mybuf);
21918}
21919
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021920 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021921get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021922{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021923 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021925 case VAR_NUMBER:
21926 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21927 return buf;
21928 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021929 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021930 break;
21931 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021932 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021933 break;
21934 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021935 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021936 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021937 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021938#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021939 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021940 break;
21941#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021942 case VAR_STRING:
21943 if (varp->vval.v_string != NULL)
21944 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021945 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021946 case VAR_SPECIAL:
21947 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21948 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021949 case VAR_JOB:
21950#ifdef FEAT_JOB
21951 {
21952 job_T *job = varp->vval.v_job;
21953 char *status = job->jv_status == JOB_FAILED ? "fail"
21954 : job->jv_status == JOB_ENDED ? "dead"
21955 : "run";
21956# ifdef UNIX
21957 vim_snprintf((char *)buf, NUMBUFLEN,
21958 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021959# elif defined(WIN32)
21960 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021961 "process %ld %s",
21962 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021963 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021964# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021965 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021966 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21967# endif
21968 return buf;
21969 }
21970#endif
21971 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021972 case VAR_CHANNEL:
21973#ifdef FEAT_CHANNEL
21974 {
21975 channel_T *channel = varp->vval.v_channel;
21976 char *status = channel_status(channel);
21977
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021978 if (channel == NULL)
21979 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21980 else
21981 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021982 "channel %d %s", channel->ch_id, status);
21983 return buf;
21984 }
21985#endif
21986 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021987 case VAR_UNKNOWN:
21988 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021991 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992}
21993
21994/*
21995 * Find variable "name" in the list of variables.
21996 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021997 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021998 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021999 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022001 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022002find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022005 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006
Bram Moolenaara7043832005-01-21 11:56:39 +000022007 ht = find_var_ht(name, &varname);
22008 if (htp != NULL)
22009 *htp = ht;
22010 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022012 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013}
22014
22015/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022016 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022017 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022019 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022020find_var_in_ht(
22021 hashtab_T *ht,
22022 int htname,
22023 char_u *varname,
22024 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022025{
Bram Moolenaar33570922005-01-25 22:26:29 +000022026 hashitem_T *hi;
22027
22028 if (*varname == NUL)
22029 {
22030 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022031 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022032 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022033 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022034 case 'g': return &globvars_var;
22035 case 'v': return &vimvars_var;
22036 case 'b': return &curbuf->b_bufvar;
22037 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022038#ifdef FEAT_WINDOWS
22039 case 't': return &curtab->tp_winvar;
22040#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022041 case 'l': return current_funccal == NULL
22042 ? NULL : &current_funccal->l_vars_var;
22043 case 'a': return current_funccal == NULL
22044 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022045 }
22046 return NULL;
22047 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022048
22049 hi = hash_find(ht, varname);
22050 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022051 {
22052 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022053 * worked find the variable again. Don't auto-load a script if it was
22054 * loaded already, otherwise it would be loaded every time when
22055 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022056 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022057 {
22058 /* Note: script_autoload() may make "hi" invalid. It must either
22059 * be obtained again or not used. */
22060 if (!script_autoload(varname, FALSE) || aborting())
22061 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022062 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022063 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022064 if (HASHITEM_EMPTY(hi))
22065 return NULL;
22066 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022067 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022068}
22069
22070/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022071 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022072 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022073 * Set "varname" to the start of name without ':'.
22074 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022075 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022076find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022078 hashitem_T *hi;
22079
Bram Moolenaar73627d02015-08-11 15:46:09 +020022080 if (name[0] == NUL)
22081 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 if (name[1] != ':')
22083 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022084 /* The name must not start with a colon or #. */
22085 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 return NULL;
22087 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022088
22089 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022090 hi = hash_find(&compat_hashtab, name);
22091 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022092 return &compat_hashtab;
22093
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022095 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022096 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 }
22098 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022099 if (*name == 'g') /* global variable */
22100 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022101 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22102 */
22103 if (vim_strchr(name + 2, ':') != NULL
22104 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022105 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022107 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022109 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022110#ifdef FEAT_WINDOWS
22111 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022112 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022113#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022114 if (*name == 'v') /* v: variable */
22115 return &vimvarht;
22116 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022117 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022118 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022119 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 if (*name == 's' /* script variable */
22121 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22122 return &SCRIPT_VARS(current_SID);
22123 return NULL;
22124}
22125
22126/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022127 * Get function call environment based on bactrace debug level
22128 */
22129 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022130get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022131{
22132 int i;
22133 funccall_T *funccal;
22134 funccall_T *temp_funccal;
22135
22136 funccal = current_funccal;
22137 if (debug_backtrace_level > 0)
22138 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022139 for (i = 0; i < debug_backtrace_level; i++)
22140 {
22141 temp_funccal = funccal->caller;
22142 if (temp_funccal)
22143 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022144 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022145 /* backtrace level overflow. reset to max */
22146 debug_backtrace_level = i;
22147 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022148 }
22149 return funccal;
22150}
22151
22152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022154 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 * Returns NULL when it doesn't exist.
22156 */
22157 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022158get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159{
Bram Moolenaar33570922005-01-25 22:26:29 +000022160 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022161
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022162 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 if (v == NULL)
22164 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022165 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166}
22167
22168/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022169 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 * sourcing this script and when executing functions defined in the script.
22171 */
22172 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022173new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174{
Bram Moolenaara7043832005-01-21 11:56:39 +000022175 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022176 hashtab_T *ht;
22177 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022178
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22180 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022181 /* Re-allocating ga_data means that an ht_array pointing to
22182 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022183 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022184 for (i = 1; i <= ga_scripts.ga_len; ++i)
22185 {
22186 ht = &SCRIPT_VARS(i);
22187 if (ht->ht_mask == HT_INIT_SIZE - 1)
22188 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022189 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022190 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022191 }
22192
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 while (ga_scripts.ga_len < id)
22194 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022195 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022196 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022197 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022198 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 }
22200 }
22201}
22202
22203/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022204 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22205 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 */
22207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022208init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209{
Bram Moolenaar33570922005-01-25 22:26:29 +000022210 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022211 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022212 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022213 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022214 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022215 dict_var->di_tv.vval.v_dict = dict;
22216 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022217 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022218 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22219 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220}
22221
22222/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022223 * Unreference a dictionary initialized by init_var_dict().
22224 */
22225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022226unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022227{
22228 /* Now the dict needs to be freed if no one else is using it, go back to
22229 * normal reference counting. */
22230 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22231 dict_unref(dict);
22232}
22233
22234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022236 * Frees all allocated variables and the value they contain.
22237 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 */
22239 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022240vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022241{
22242 vars_clear_ext(ht, TRUE);
22243}
22244
22245/*
22246 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22247 */
22248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022249vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250{
Bram Moolenaara7043832005-01-21 11:56:39 +000022251 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022252 hashitem_T *hi;
22253 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022256 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022257 for (hi = ht->ht_array; todo > 0; ++hi)
22258 {
22259 if (!HASHITEM_EMPTY(hi))
22260 {
22261 --todo;
22262
Bram Moolenaar33570922005-01-25 22:26:29 +000022263 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022264 * ht_array might change then. hash_clear() takes care of it
22265 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022266 v = HI2DI(hi);
22267 if (free_val)
22268 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022269 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022270 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022271 }
22272 }
22273 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022274 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275}
22276
Bram Moolenaara7043832005-01-21 11:56:39 +000022277/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022278 * Delete a variable from hashtab "ht" at item "hi".
22279 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022282delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283{
Bram Moolenaar33570922005-01-25 22:26:29 +000022284 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022285
22286 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022287 clear_tv(&di->di_tv);
22288 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289}
22290
22291/*
22292 * List the value of one internal variable.
22293 */
22294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022295list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022297 char_u *tofree;
22298 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022299 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022300
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022301 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022302 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022303 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022304 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305}
22306
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022308list_one_var_a(
22309 char_u *prefix,
22310 char_u *name,
22311 int type,
22312 char_u *string,
22313 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314{
Bram Moolenaar31859182007-08-14 20:41:13 +000022315 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22316 msg_start();
22317 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318 if (name != NULL) /* "a:" vars don't have a name stored */
22319 msg_puts(name);
22320 msg_putchar(' ');
22321 msg_advance(22);
22322 if (type == VAR_NUMBER)
22323 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022324 else if (type == VAR_FUNC)
22325 msg_putchar('*');
22326 else if (type == VAR_LIST)
22327 {
22328 msg_putchar('[');
22329 if (*string == '[')
22330 ++string;
22331 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022332 else if (type == VAR_DICT)
22333 {
22334 msg_putchar('{');
22335 if (*string == '{')
22336 ++string;
22337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 else
22339 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022340
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022342
22343 if (type == VAR_FUNC)
22344 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022345 if (*first)
22346 {
22347 msg_clr_eos();
22348 *first = FALSE;
22349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350}
22351
22352/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022353 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 * If the variable already exists, the value is updated.
22355 * Otherwise the variable is created.
22356 */
22357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022358set_var(
22359 char_u *name,
22360 typval_T *tv,
22361 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362{
Bram Moolenaar33570922005-01-25 22:26:29 +000022363 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022365 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022367 ht = find_var_ht(name, &varname);
22368 if (ht == NULL || *varname == NUL)
22369 {
22370 EMSG2(_(e_illvar), name);
22371 return;
22372 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022373 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022374
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022375 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22376 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022377
Bram Moolenaar33570922005-01-25 22:26:29 +000022378 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022380 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022381 if (var_check_ro(v->di_flags, name, FALSE)
22382 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022383 return;
22384 if (v->di_tv.v_type != tv->v_type
22385 && !((v->di_tv.v_type == VAR_STRING
22386 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022387 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022388 || tv->v_type == VAR_NUMBER))
22389#ifdef FEAT_FLOAT
22390 && !((v->di_tv.v_type == VAR_NUMBER
22391 || v->di_tv.v_type == VAR_FLOAT)
22392 && (tv->v_type == VAR_NUMBER
22393 || tv->v_type == VAR_FLOAT))
22394#endif
22395 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022396 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022397 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022398 return;
22399 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022400
22401 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022402 * Handle setting internal v: variables separately where needed to
22403 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022404 */
22405 if (ht == &vimvarht)
22406 {
22407 if (v->di_tv.v_type == VAR_STRING)
22408 {
22409 vim_free(v->di_tv.vval.v_string);
22410 if (copy || tv->v_type != VAR_STRING)
22411 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22412 else
22413 {
22414 /* Take over the string to avoid an extra alloc/free. */
22415 v->di_tv.vval.v_string = tv->vval.v_string;
22416 tv->vval.v_string = NULL;
22417 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022418 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022419 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022420 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022421 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022422 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022423 if (STRCMP(varname, "searchforward") == 0)
22424 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022425#ifdef FEAT_SEARCH_EXTRA
22426 else if (STRCMP(varname, "hlsearch") == 0)
22427 {
22428 no_hlsearch = !v->di_tv.vval.v_number;
22429 redraw_all_later(SOME_VALID);
22430 }
22431#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022432 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022433 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022434 else if (v->di_tv.v_type != tv->v_type)
22435 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022436 }
22437
22438 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439 }
22440 else /* add a new variable */
22441 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022442 /* Can't add "v:" variable. */
22443 if (ht == &vimvarht)
22444 {
22445 EMSG2(_(e_illvar), name);
22446 return;
22447 }
22448
Bram Moolenaar92124a32005-06-17 22:03:40 +000022449 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022450 if (!valid_varname(varname))
22451 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022452
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022453 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22454 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022455 if (v == NULL)
22456 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022457 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022460 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 return;
22462 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022463 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022465
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022466 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022467 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022468 else
22469 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022470 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022471 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022472 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474}
22475
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022476/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022477 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022478 * Also give an error message.
22479 */
22480 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022481var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022482{
22483 if (flags & DI_FLAGS_RO)
22484 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022485 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022486 return TRUE;
22487 }
22488 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22489 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022490 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022491 return TRUE;
22492 }
22493 return FALSE;
22494}
22495
22496/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022497 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22498 * Also give an error message.
22499 */
22500 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022501var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022502{
22503 if (flags & DI_FLAGS_FIX)
22504 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022505 EMSG2(_("E795: Cannot delete variable %s"),
22506 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022507 return TRUE;
22508 }
22509 return FALSE;
22510}
22511
22512/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022513 * Check if a funcref is assigned to a valid variable name.
22514 * Return TRUE and give an error if not.
22515 */
22516 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022517var_check_func_name(
22518 char_u *name, /* points to start of variable name */
22519 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022520{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022521 /* Allow for w: b: s: and t:. */
22522 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022523 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22524 ? name[2] : name[0]))
22525 {
22526 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22527 name);
22528 return TRUE;
22529 }
22530 /* Don't allow hiding a function. When "v" is not NULL we might be
22531 * assigning another function to the same var, the type is checked
22532 * below. */
22533 if (new_var && function_exists(name))
22534 {
22535 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22536 name);
22537 return TRUE;
22538 }
22539 return FALSE;
22540}
22541
22542/*
22543 * Check if a variable name is valid.
22544 * Return FALSE and give an error if not.
22545 */
22546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022547valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022548{
22549 char_u *p;
22550
22551 for (p = varname; *p != NUL; ++p)
22552 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22553 && *p != AUTOLOAD_CHAR)
22554 {
22555 EMSG2(_(e_illvar), varname);
22556 return FALSE;
22557 }
22558 return TRUE;
22559}
22560
22561/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022562 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022563 * Also give an error message, using "name" or _("name") when use_gettext is
22564 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022565 */
22566 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022567tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022568{
22569 if (lock & VAR_LOCKED)
22570 {
22571 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022572 name == NULL ? (char_u *)_("Unknown")
22573 : use_gettext ? (char_u *)_(name)
22574 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022575 return TRUE;
22576 }
22577 if (lock & VAR_FIXED)
22578 {
22579 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022580 name == NULL ? (char_u *)_("Unknown")
22581 : use_gettext ? (char_u *)_(name)
22582 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022583 return TRUE;
22584 }
22585 return FALSE;
22586}
22587
22588/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022590 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022591 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022592 * It is OK for "from" and "to" to point to the same item. This is used to
22593 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022594 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022596copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022598 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022599 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022600 switch (from->v_type)
22601 {
22602 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022603 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022604 to->vval.v_number = from->vval.v_number;
22605 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022606 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022607#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022608 to->vval.v_float = from->vval.v_float;
22609 break;
22610#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022611 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022612#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022613 to->vval.v_job = from->vval.v_job;
22614 ++to->vval.v_job->jv_refcount;
22615 break;
22616#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022617 case VAR_CHANNEL:
22618#ifdef FEAT_CHANNEL
22619 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022620 if (to->vval.v_channel != NULL)
22621 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022622 break;
22623#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022624 case VAR_STRING:
22625 case VAR_FUNC:
22626 if (from->vval.v_string == NULL)
22627 to->vval.v_string = NULL;
22628 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022629 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022630 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022631 if (from->v_type == VAR_FUNC)
22632 func_ref(to->vval.v_string);
22633 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022634 break;
22635 case VAR_LIST:
22636 if (from->vval.v_list == NULL)
22637 to->vval.v_list = NULL;
22638 else
22639 {
22640 to->vval.v_list = from->vval.v_list;
22641 ++to->vval.v_list->lv_refcount;
22642 }
22643 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022644 case VAR_DICT:
22645 if (from->vval.v_dict == NULL)
22646 to->vval.v_dict = NULL;
22647 else
22648 {
22649 to->vval.v_dict = from->vval.v_dict;
22650 ++to->vval.v_dict->dv_refcount;
22651 }
22652 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022653 case VAR_UNKNOWN:
22654 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022655 break;
22656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657}
22658
22659/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022660 * Make a copy of an item.
22661 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022662 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22663 * reference to an already copied list/dict can be used.
22664 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022665 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022667item_copy(
22668 typval_T *from,
22669 typval_T *to,
22670 int deep,
22671 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022672{
22673 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022674 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022675
Bram Moolenaar33570922005-01-25 22:26:29 +000022676 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022677 {
22678 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022679 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022680 }
22681 ++recurse;
22682
22683 switch (from->v_type)
22684 {
22685 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022686 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022687 case VAR_STRING:
22688 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022689 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022690 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022691 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022692 copy_tv(from, to);
22693 break;
22694 case VAR_LIST:
22695 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022696 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022697 if (from->vval.v_list == NULL)
22698 to->vval.v_list = NULL;
22699 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22700 {
22701 /* use the copy made earlier */
22702 to->vval.v_list = from->vval.v_list->lv_copylist;
22703 ++to->vval.v_list->lv_refcount;
22704 }
22705 else
22706 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22707 if (to->vval.v_list == NULL)
22708 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022709 break;
22710 case VAR_DICT:
22711 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022712 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022713 if (from->vval.v_dict == NULL)
22714 to->vval.v_dict = NULL;
22715 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22716 {
22717 /* use the copy made earlier */
22718 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22719 ++to->vval.v_dict->dv_refcount;
22720 }
22721 else
22722 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22723 if (to->vval.v_dict == NULL)
22724 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022725 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022726 case VAR_UNKNOWN:
22727 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022728 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022729 }
22730 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022731 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022732}
22733
22734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 * ":echo expr1 ..." print each argument separated with a space, add a
22736 * newline at the end.
22737 * ":echon expr1 ..." print each argument plain.
22738 */
22739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022740ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741{
22742 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022743 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022744 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745 char_u *p;
22746 int needclr = TRUE;
22747 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022748 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749
22750 if (eap->skip)
22751 ++emsg_skip;
22752 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22753 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022754 /* If eval1() causes an error message the text from the command may
22755 * still need to be cleared. E.g., "echo 22,44". */
22756 need_clr_eos = needclr;
22757
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022759 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 {
22761 /*
22762 * Report the invalid expression unless the expression evaluation
22763 * has been cancelled due to an aborting error, an interrupt, or an
22764 * exception.
22765 */
22766 if (!aborting())
22767 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022768 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 break;
22770 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022771 need_clr_eos = FALSE;
22772
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773 if (!eap->skip)
22774 {
22775 if (atstart)
22776 {
22777 atstart = FALSE;
22778 /* Call msg_start() after eval1(), evaluating the expression
22779 * may cause a message to appear. */
22780 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022781 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022782 /* Mark the saved text as finishing the line, so that what
22783 * follows is displayed on a new line when scrolling back
22784 * at the more prompt. */
22785 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 }
22789 else if (eap->cmdidx == CMD_echo)
22790 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022791 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022792 if (p != NULL)
22793 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022795 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022797 if (*p != TAB && needclr)
22798 {
22799 /* remove any text still there from the command */
22800 msg_clr_eos();
22801 needclr = FALSE;
22802 }
22803 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 }
22805 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022806 {
22807#ifdef FEAT_MBYTE
22808 if (has_mbyte)
22809 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022810 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022811
22812 (void)msg_outtrans_len_attr(p, i, echo_attr);
22813 p += i - 1;
22814 }
22815 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022817 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022820 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022822 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823 arg = skipwhite(arg);
22824 }
22825 eap->nextcmd = check_nextcmd(arg);
22826
22827 if (eap->skip)
22828 --emsg_skip;
22829 else
22830 {
22831 /* remove text that may still be there from the command */
22832 if (needclr)
22833 msg_clr_eos();
22834 if (eap->cmdidx == CMD_echo)
22835 msg_end();
22836 }
22837}
22838
22839/*
22840 * ":echohl {name}".
22841 */
22842 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022843ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844{
22845 int id;
22846
22847 id = syn_name2id(eap->arg);
22848 if (id == 0)
22849 echo_attr = 0;
22850 else
22851 echo_attr = syn_id2attr(id);
22852}
22853
22854/*
22855 * ":execute expr1 ..." execute the result of an expression.
22856 * ":echomsg expr1 ..." Print a message
22857 * ":echoerr expr1 ..." Print an error
22858 * Each gets spaces around each argument and a newline at the end for
22859 * echo commands
22860 */
22861 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022862ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863{
22864 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022865 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866 int ret = OK;
22867 char_u *p;
22868 garray_T ga;
22869 int len;
22870 int save_did_emsg;
22871
22872 ga_init2(&ga, 1, 80);
22873
22874 if (eap->skip)
22875 ++emsg_skip;
22876 while (*arg != NUL && *arg != '|' && *arg != '\n')
22877 {
22878 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022879 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 {
22881 /*
22882 * Report the invalid expression unless the expression evaluation
22883 * has been cancelled due to an aborting error, an interrupt, or an
22884 * exception.
22885 */
22886 if (!aborting())
22887 EMSG2(_(e_invexpr2), p);
22888 ret = FAIL;
22889 break;
22890 }
22891
22892 if (!eap->skip)
22893 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022894 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 len = (int)STRLEN(p);
22896 if (ga_grow(&ga, len + 2) == FAIL)
22897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022898 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 ret = FAIL;
22900 break;
22901 }
22902 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 ga.ga_len += len;
22906 }
22907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 arg = skipwhite(arg);
22910 }
22911
22912 if (ret != FAIL && ga.ga_data != NULL)
22913 {
22914 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022917 out_flush();
22918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 else if (eap->cmdidx == CMD_echoerr)
22920 {
22921 /* We don't want to abort following commands, restore did_emsg. */
22922 save_did_emsg = did_emsg;
22923 EMSG((char_u *)ga.ga_data);
22924 if (!force_abort)
22925 did_emsg = save_did_emsg;
22926 }
22927 else if (eap->cmdidx == CMD_execute)
22928 do_cmdline((char_u *)ga.ga_data,
22929 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22930 }
22931
22932 ga_clear(&ga);
22933
22934 if (eap->skip)
22935 --emsg_skip;
22936
22937 eap->nextcmd = check_nextcmd(arg);
22938}
22939
22940/*
22941 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22942 * "arg" points to the "&" or '+' when called, to "option" when returning.
22943 * Returns NULL when no option name found. Otherwise pointer to the char
22944 * after the option name.
22945 */
22946 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022947find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948{
22949 char_u *p = *arg;
22950
22951 ++p;
22952 if (*p == 'g' && p[1] == ':')
22953 {
22954 *opt_flags = OPT_GLOBAL;
22955 p += 2;
22956 }
22957 else if (*p == 'l' && p[1] == ':')
22958 {
22959 *opt_flags = OPT_LOCAL;
22960 p += 2;
22961 }
22962 else
22963 *opt_flags = 0;
22964
22965 if (!ASCII_ISALPHA(*p))
22966 return NULL;
22967 *arg = p;
22968
22969 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22970 p += 4; /* termcap option */
22971 else
22972 while (ASCII_ISALPHA(*p))
22973 ++p;
22974 return p;
22975}
22976
22977/*
22978 * ":function"
22979 */
22980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022981ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982{
22983 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022984 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 int j;
22986 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022988 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 char_u *name = NULL;
22990 char_u *p;
22991 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022992 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 garray_T newargs;
22994 garray_T newlines;
22995 int varargs = FALSE;
22996 int mustend = FALSE;
22997 int flags = 0;
22998 ufunc_T *fp;
22999 int indent;
23000 int nesting;
23001 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023002 dictitem_T *v;
23003 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023004 static int func_nr = 0; /* number for nameless function */
23005 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023006 hashtab_T *ht;
23007 int todo;
23008 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023009 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010
23011 /*
23012 * ":function" without argument: list functions.
23013 */
23014 if (ends_excmd(*eap->arg))
23015 {
23016 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023017 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023018 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023019 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023020 {
23021 if (!HASHITEM_EMPTY(hi))
23022 {
23023 --todo;
23024 fp = HI2UF(hi);
23025 if (!isdigit(*fp->uf_name))
23026 list_func_head(fp, FALSE);
23027 }
23028 }
23029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 eap->nextcmd = check_nextcmd(eap->arg);
23031 return;
23032 }
23033
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023034 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023035 * ":function /pat": list functions matching pattern.
23036 */
23037 if (*eap->arg == '/')
23038 {
23039 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23040 if (!eap->skip)
23041 {
23042 regmatch_T regmatch;
23043
23044 c = *p;
23045 *p = NUL;
23046 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23047 *p = c;
23048 if (regmatch.regprog != NULL)
23049 {
23050 regmatch.rm_ic = p_ic;
23051
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023052 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023053 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23054 {
23055 if (!HASHITEM_EMPTY(hi))
23056 {
23057 --todo;
23058 fp = HI2UF(hi);
23059 if (!isdigit(*fp->uf_name)
23060 && vim_regexec(&regmatch, fp->uf_name, 0))
23061 list_func_head(fp, FALSE);
23062 }
23063 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023064 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023065 }
23066 }
23067 if (*p == '/')
23068 ++p;
23069 eap->nextcmd = check_nextcmd(p);
23070 return;
23071 }
23072
23073 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023074 * Get the function name. There are these situations:
23075 * func normal function name
23076 * "name" == func, "fudi.fd_dict" == NULL
23077 * dict.func new dictionary entry
23078 * "name" == NULL, "fudi.fd_dict" set,
23079 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23080 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023081 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023082 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23083 * dict.func existing dict entry that's not a Funcref
23084 * "name" == NULL, "fudi.fd_dict" set,
23085 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023086 * s:func script-local function name
23087 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023090 name = trans_function_name(&p, eap->skip, 0, &fudi);
23091 paren = (vim_strchr(p, '(') != NULL);
23092 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 {
23094 /*
23095 * Return on an invalid expression in braces, unless the expression
23096 * evaluation has been cancelled due to an aborting error, an
23097 * interrupt, or an exception.
23098 */
23099 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023100 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023101 if (!eap->skip && fudi.fd_newkey != NULL)
23102 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023103 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106 else
23107 eap->skip = TRUE;
23108 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023109
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 /* An error in a function call during evaluation of an expression in magic
23111 * braces should not cause the function not to be defined. */
23112 saved_did_emsg = did_emsg;
23113 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114
23115 /*
23116 * ":function func" with only function name: list function.
23117 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023118 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119 {
23120 if (!ends_excmd(*skipwhite(p)))
23121 {
23122 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023123 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 }
23125 eap->nextcmd = check_nextcmd(p);
23126 if (eap->nextcmd != NULL)
23127 *p = NUL;
23128 if (!eap->skip && !got_int)
23129 {
23130 fp = find_func(name);
23131 if (fp != NULL)
23132 {
23133 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023134 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023136 if (FUNCLINE(fp, j) == NULL)
23137 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 msg_putchar('\n');
23139 msg_outnum((long)(j + 1));
23140 if (j < 9)
23141 msg_putchar(' ');
23142 if (j < 99)
23143 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023144 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 out_flush(); /* show a line at a time */
23146 ui_breakcheck();
23147 }
23148 if (!got_int)
23149 {
23150 msg_putchar('\n');
23151 msg_puts((char_u *)" endfunction");
23152 }
23153 }
23154 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023155 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023157 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 }
23159
23160 /*
23161 * ":function name(arg1, arg2)" Define function.
23162 */
23163 p = skipwhite(p);
23164 if (*p != '(')
23165 {
23166 if (!eap->skip)
23167 {
23168 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023169 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 }
23171 /* attempt to continue by skipping some text */
23172 if (vim_strchr(p, '(') != NULL)
23173 p = vim_strchr(p, '(');
23174 }
23175 p = skipwhite(p + 1);
23176
23177 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23178 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23179
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023180 if (!eap->skip)
23181 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023182 /* Check the name of the function. Unless it's a dictionary function
23183 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023184 if (name != NULL)
23185 arg = name;
23186 else
23187 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023188 if (arg != NULL && (fudi.fd_di == NULL
23189 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023190 {
23191 if (*arg == K_SPECIAL)
23192 j = 3;
23193 else
23194 j = 0;
23195 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23196 : eval_isnamec(arg[j])))
23197 ++j;
23198 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023199 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023200 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023201 /* Disallow using the g: dict. */
23202 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23203 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023204 }
23205
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206 /*
23207 * Isolate the arguments: "arg1, arg2, ...)"
23208 */
23209 while (*p != ')')
23210 {
23211 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23212 {
23213 varargs = TRUE;
23214 p += 3;
23215 mustend = TRUE;
23216 }
23217 else
23218 {
23219 arg = p;
23220 while (ASCII_ISALNUM(*p) || *p == '_')
23221 ++p;
23222 if (arg == p || isdigit(*arg)
23223 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23224 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23225 {
23226 if (!eap->skip)
23227 EMSG2(_("E125: Illegal argument: %s"), arg);
23228 break;
23229 }
23230 if (ga_grow(&newargs, 1) == FAIL)
23231 goto erret;
23232 c = *p;
23233 *p = NUL;
23234 arg = vim_strsave(arg);
23235 if (arg == NULL)
23236 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023237
23238 /* Check for duplicate argument name. */
23239 for (i = 0; i < newargs.ga_len; ++i)
23240 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23241 {
23242 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023243 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023244 goto erret;
23245 }
23246
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23248 *p = c;
23249 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 if (*p == ',')
23251 ++p;
23252 else
23253 mustend = TRUE;
23254 }
23255 p = skipwhite(p);
23256 if (mustend && *p != ')')
23257 {
23258 if (!eap->skip)
23259 EMSG2(_(e_invarg2), eap->arg);
23260 break;
23261 }
23262 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023263 if (*p != ')')
23264 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265 ++p; /* skip the ')' */
23266
Bram Moolenaare9a41262005-01-15 22:18:47 +000023267 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 for (;;)
23269 {
23270 p = skipwhite(p);
23271 if (STRNCMP(p, "range", 5) == 0)
23272 {
23273 flags |= FC_RANGE;
23274 p += 5;
23275 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023276 else if (STRNCMP(p, "dict", 4) == 0)
23277 {
23278 flags |= FC_DICT;
23279 p += 4;
23280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 else if (STRNCMP(p, "abort", 5) == 0)
23282 {
23283 flags |= FC_ABORT;
23284 p += 5;
23285 }
23286 else
23287 break;
23288 }
23289
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023290 /* When there is a line break use what follows for the function body.
23291 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23292 if (*p == '\n')
23293 line_arg = p + 1;
23294 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 EMSG(_(e_trailing));
23296
23297 /*
23298 * Read the body of the function, until ":endfunction" is found.
23299 */
23300 if (KeyTyped)
23301 {
23302 /* Check if the function already exists, don't let the user type the
23303 * whole function before telling him it doesn't work! For a script we
23304 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023305 if (!eap->skip && !eap->forceit)
23306 {
23307 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23308 EMSG(_(e_funcdict));
23309 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023310 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023313 if (!eap->skip && did_emsg)
23314 goto erret;
23315
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 msg_putchar('\n'); /* don't overwrite the function name */
23317 cmdline_row = msg_row;
23318 }
23319
23320 indent = 2;
23321 nesting = 0;
23322 for (;;)
23323 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023324 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023325 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023326 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023327 saved_wait_return = FALSE;
23328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023329 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023330 sourcing_lnum_off = sourcing_lnum;
23331
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023332 if (line_arg != NULL)
23333 {
23334 /* Use eap->arg, split up in parts by line breaks. */
23335 theline = line_arg;
23336 p = vim_strchr(theline, '\n');
23337 if (p == NULL)
23338 line_arg += STRLEN(line_arg);
23339 else
23340 {
23341 *p = NUL;
23342 line_arg = p + 1;
23343 }
23344 }
23345 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 theline = getcmdline(':', 0L, indent);
23347 else
23348 theline = eap->getline(':', eap->cookie, indent);
23349 if (KeyTyped)
23350 lines_left = Rows - 1;
23351 if (theline == NULL)
23352 {
23353 EMSG(_("E126: Missing :endfunction"));
23354 goto erret;
23355 }
23356
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023357 /* Detect line continuation: sourcing_lnum increased more than one. */
23358 if (sourcing_lnum > sourcing_lnum_off + 1)
23359 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23360 else
23361 sourcing_lnum_off = 0;
23362
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 if (skip_until != NULL)
23364 {
23365 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23366 * don't check for ":endfunc". */
23367 if (STRCMP(theline, skip_until) == 0)
23368 {
23369 vim_free(skip_until);
23370 skip_until = NULL;
23371 }
23372 }
23373 else
23374 {
23375 /* skip ':' and blanks*/
23376 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23377 ;
23378
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023379 /* Check for "endfunction". */
23380 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023382 if (line_arg == NULL)
23383 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384 break;
23385 }
23386
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023387 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388 * at "end". */
23389 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23390 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023391 else if (STRNCMP(p, "if", 2) == 0
23392 || STRNCMP(p, "wh", 2) == 0
23393 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394 || STRNCMP(p, "try", 3) == 0)
23395 indent += 2;
23396
23397 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023398 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023400 if (*p == '!')
23401 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023402 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023403 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23404 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023406 ++nesting;
23407 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 }
23409 }
23410
23411 /* Check for ":append" or ":insert". */
23412 p = skip_range(p, NULL);
23413 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23414 || (p[0] == 'i'
23415 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23416 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23417 skip_until = vim_strsave((char_u *)".");
23418
23419 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23420 arg = skipwhite(skiptowhite(p));
23421 if (arg[0] == '<' && arg[1] =='<'
23422 && ((p[0] == 'p' && p[1] == 'y'
23423 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23424 || (p[0] == 'p' && p[1] == 'e'
23425 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23426 || (p[0] == 't' && p[1] == 'c'
23427 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023428 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23429 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23431 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023432 || (p[0] == 'm' && p[1] == 'z'
23433 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 ))
23435 {
23436 /* ":python <<" continues until a dot, like ":append" */
23437 p = skipwhite(arg + 2);
23438 if (*p == NUL)
23439 skip_until = vim_strsave((char_u *)".");
23440 else
23441 skip_until = vim_strsave(p);
23442 }
23443 }
23444
23445 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023446 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023447 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023448 if (line_arg == NULL)
23449 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023451 }
23452
23453 /* Copy the line to newly allocated memory. get_one_sourceline()
23454 * allocates 250 bytes per line, this saves 80% on average. The cost
23455 * is an extra alloc/free. */
23456 p = vim_strsave(theline);
23457 if (p != NULL)
23458 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023459 if (line_arg == NULL)
23460 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023461 theline = p;
23462 }
23463
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023464 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23465
23466 /* Add NULL lines for continuation lines, so that the line count is
23467 * equal to the index in the growarray. */
23468 while (sourcing_lnum_off-- > 0)
23469 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023470
23471 /* Check for end of eap->arg. */
23472 if (line_arg != NULL && *line_arg == NUL)
23473 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 }
23475
23476 /* Don't define the function when skipping commands or when an error was
23477 * detected. */
23478 if (eap->skip || did_emsg)
23479 goto erret;
23480
23481 /*
23482 * If there are no errors, add the function
23483 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023484 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023485 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023486 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023487 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023488 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023489 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023490 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023491 goto erret;
23492 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023493
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023494 fp = find_func(name);
23495 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023497 if (!eap->forceit)
23498 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023499 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023500 goto erret;
23501 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023502 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023503 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023504 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023505 name);
23506 goto erret;
23507 }
23508 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023509 ga_clear_strings(&(fp->uf_args));
23510 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023511 vim_free(name);
23512 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 }
23515 else
23516 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023517 char numbuf[20];
23518
23519 fp = NULL;
23520 if (fudi.fd_newkey == NULL && !eap->forceit)
23521 {
23522 EMSG(_(e_funcdict));
23523 goto erret;
23524 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023525 if (fudi.fd_di == NULL)
23526 {
23527 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023528 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023529 goto erret;
23530 }
23531 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023532 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023533 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023534
23535 /* Give the function a sequential number. Can only be used with a
23536 * Funcref! */
23537 vim_free(name);
23538 sprintf(numbuf, "%d", ++func_nr);
23539 name = vim_strsave((char_u *)numbuf);
23540 if (name == NULL)
23541 goto erret;
23542 }
23543
23544 if (fp == NULL)
23545 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023546 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023547 {
23548 int slen, plen;
23549 char_u *scriptname;
23550
23551 /* Check that the autoload name matches the script name. */
23552 j = FAIL;
23553 if (sourcing_name != NULL)
23554 {
23555 scriptname = autoload_name(name);
23556 if (scriptname != NULL)
23557 {
23558 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023559 plen = (int)STRLEN(p);
23560 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023561 if (slen > plen && fnamecmp(p,
23562 sourcing_name + slen - plen) == 0)
23563 j = OK;
23564 vim_free(scriptname);
23565 }
23566 }
23567 if (j == FAIL)
23568 {
23569 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23570 goto erret;
23571 }
23572 }
23573
23574 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 if (fp == NULL)
23576 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023577
23578 if (fudi.fd_dict != NULL)
23579 {
23580 if (fudi.fd_di == NULL)
23581 {
23582 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023583 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023584 if (fudi.fd_di == NULL)
23585 {
23586 vim_free(fp);
23587 goto erret;
23588 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023589 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23590 {
23591 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023592 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023593 goto erret;
23594 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023595 }
23596 else
23597 /* overwrite existing dict entry */
23598 clear_tv(&fudi.fd_di->di_tv);
23599 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023600 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023601 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023602 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023603
23604 /* behave like "dict" was used */
23605 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023606 }
23607
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023609 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023610 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23611 {
23612 vim_free(fp);
23613 goto erret;
23614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023616 fp->uf_args = newargs;
23617 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023618#ifdef FEAT_PROFILE
23619 fp->uf_tml_count = NULL;
23620 fp->uf_tml_total = NULL;
23621 fp->uf_tml_self = NULL;
23622 fp->uf_profiling = FALSE;
23623 if (prof_def_func())
23624 func_do_profile(fp);
23625#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023626 fp->uf_varargs = varargs;
23627 fp->uf_flags = flags;
23628 fp->uf_calls = 0;
23629 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023630 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631
23632erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 ga_clear_strings(&newargs);
23634 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023635ret_free:
23636 vim_free(skip_until);
23637 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023640 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641}
23642
23643/*
23644 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023645 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023647 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023648 * TFN_INT: internal function name OK
23649 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023650 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 * Advances "pp" to just after the function name (if no error).
23652 */
23653 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023654trans_function_name(
23655 char_u **pp,
23656 int skip, /* only find the end, don't evaluate */
23657 int flags,
23658 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023660 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661 char_u *start;
23662 char_u *end;
23663 int lead;
23664 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023666 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023667
23668 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023669 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023671
23672 /* Check for hard coded <SNR>: already translated function ID (from a user
23673 * command). */
23674 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23675 && (*pp)[2] == (int)KE_SNR)
23676 {
23677 *pp += 3;
23678 len = get_id_len(pp) + 3;
23679 return vim_strnsave(start, len);
23680 }
23681
23682 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23683 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023685 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023686 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023687
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023688 /* Note that TFN_ flags use the same values as GLV_ flags. */
23689 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023690 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023691 if (end == start)
23692 {
23693 if (!skip)
23694 EMSG(_("E129: Function name required"));
23695 goto theend;
23696 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023697 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023698 {
23699 /*
23700 * Report an invalid expression in braces, unless the expression
23701 * evaluation has been cancelled due to an aborting error, an
23702 * interrupt, or an exception.
23703 */
23704 if (!aborting())
23705 {
23706 if (end != NULL)
23707 EMSG2(_(e_invarg2), start);
23708 }
23709 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023710 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023711 goto theend;
23712 }
23713
23714 if (lv.ll_tv != NULL)
23715 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023716 if (fdp != NULL)
23717 {
23718 fdp->fd_dict = lv.ll_dict;
23719 fdp->fd_newkey = lv.ll_newkey;
23720 lv.ll_newkey = NULL;
23721 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023722 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023723 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23724 {
23725 name = vim_strsave(lv.ll_tv->vval.v_string);
23726 *pp = end;
23727 }
23728 else
23729 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023730 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23731 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023732 EMSG(_(e_funcref));
23733 else
23734 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023735 name = NULL;
23736 }
23737 goto theend;
23738 }
23739
23740 if (lv.ll_name == NULL)
23741 {
23742 /* Error found, but continue after the function name. */
23743 *pp = end;
23744 goto theend;
23745 }
23746
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023747 /* Check if the name is a Funcref. If so, use the value. */
23748 if (lv.ll_exp_name != NULL)
23749 {
23750 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023751 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023752 if (name == lv.ll_exp_name)
23753 name = NULL;
23754 }
23755 else
23756 {
23757 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023758 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023759 if (name == *pp)
23760 name = NULL;
23761 }
23762 if (name != NULL)
23763 {
23764 name = vim_strsave(name);
23765 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023766 if (STRNCMP(name, "<SNR>", 5) == 0)
23767 {
23768 /* Change "<SNR>" to the byte sequence. */
23769 name[0] = K_SPECIAL;
23770 name[1] = KS_EXTRA;
23771 name[2] = (int)KE_SNR;
23772 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23773 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023774 goto theend;
23775 }
23776
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023777 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023778 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023779 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023780 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23781 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23782 {
23783 /* When there was "s:" already or the name expanded to get a
23784 * leading "s:" then remove it. */
23785 lv.ll_name += 2;
23786 len -= 2;
23787 lead = 2;
23788 }
23789 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023790 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023791 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023792 /* skip over "s:" and "g:" */
23793 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023794 lv.ll_name += 2;
23795 len = (int)(end - lv.ll_name);
23796 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023797
23798 /*
23799 * Copy the function name to allocated memory.
23800 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23801 * Accept <SNR>123_name() outside a script.
23802 */
23803 if (skip)
23804 lead = 0; /* do nothing */
23805 else if (lead > 0)
23806 {
23807 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023808 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23809 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023810 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023811 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023812 if (current_SID <= 0)
23813 {
23814 EMSG(_(e_usingsid));
23815 goto theend;
23816 }
23817 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23818 lead += (int)STRLEN(sid_buf);
23819 }
23820 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023821 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023822 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023823 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023824 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023825 goto theend;
23826 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023827 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023828 {
23829 char_u *cp = vim_strchr(lv.ll_name, ':');
23830
23831 if (cp != NULL && cp < end)
23832 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023833 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023834 goto theend;
23835 }
23836 }
23837
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023838 name = alloc((unsigned)(len + lead + 1));
23839 if (name != NULL)
23840 {
23841 if (lead > 0)
23842 {
23843 name[0] = K_SPECIAL;
23844 name[1] = KS_EXTRA;
23845 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023846 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023847 STRCPY(name + 3, sid_buf);
23848 }
23849 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023850 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023851 }
23852 *pp = end;
23853
23854theend:
23855 clear_lval(&lv);
23856 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857}
23858
23859/*
23860 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23861 * Return 2 if "p" starts with "s:".
23862 * Return 0 otherwise.
23863 */
23864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023865eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023867 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23868 * the standard library function. */
23869 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23870 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871 return 5;
23872 if (p[0] == 's' && p[1] == ':')
23873 return 2;
23874 return 0;
23875}
23876
23877/*
23878 * Return TRUE if "p" starts with "<SID>" or "s:".
23879 * Only works if eval_fname_script() returned non-zero for "p"!
23880 */
23881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023882eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883{
23884 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23885}
23886
23887/*
23888 * List the head of the function: "name(arg1, arg2)".
23889 */
23890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023891list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892{
23893 int j;
23894
23895 msg_start();
23896 if (indent)
23897 MSG_PUTS(" ");
23898 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023899 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 {
23901 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023902 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 }
23904 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023905 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023907 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023908 {
23909 if (j)
23910 MSG_PUTS(", ");
23911 msg_puts(FUNCARG(fp, j));
23912 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023913 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023914 {
23915 if (j)
23916 MSG_PUTS(", ");
23917 MSG_PUTS("...");
23918 }
23919 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023920 if (fp->uf_flags & FC_ABORT)
23921 MSG_PUTS(" abort");
23922 if (fp->uf_flags & FC_RANGE)
23923 MSG_PUTS(" range");
23924 if (fp->uf_flags & FC_DICT)
23925 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023926 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023927 if (p_verbose > 0)
23928 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929}
23930
23931/*
23932 * Find a function by name, return pointer to it in ufuncs.
23933 * Return NULL for unknown function.
23934 */
23935 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023936find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023938 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023940 hi = hash_find(&func_hashtab, name);
23941 if (!HASHITEM_EMPTY(hi))
23942 return HI2UF(hi);
23943 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944}
23945
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023946#if defined(EXITFREE) || defined(PROTO)
23947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023948free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023949{
23950 hashitem_T *hi;
23951
23952 /* Need to start all over every time, because func_free() may change the
23953 * hash table. */
23954 while (func_hashtab.ht_used > 0)
23955 for (hi = func_hashtab.ht_array; ; ++hi)
23956 if (!HASHITEM_EMPTY(hi))
23957 {
23958 func_free(HI2UF(hi));
23959 break;
23960 }
23961}
23962#endif
23963
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023965translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023966{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023967 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023968 return find_internal_func(name) >= 0;
23969 return find_func(name) != NULL;
23970}
23971
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023972/*
23973 * Return TRUE if a function "name" exists.
23974 */
23975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023976function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023977{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023978 char_u *nm = name;
23979 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023980 int n = FALSE;
23981
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023982 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23983 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023984 nm = skipwhite(nm);
23985
23986 /* Only accept "funcname", "funcname ", "funcname (..." and
23987 * "funcname(...", not "funcname!...". */
23988 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023989 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023990 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023991 return n;
23992}
23993
Bram Moolenaara1544c02013-05-30 12:35:52 +020023994 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023995get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023996{
23997 char_u *nm = name;
23998 char_u *p;
23999
24000 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24001
24002 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024003 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024004 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024005
Bram Moolenaara1544c02013-05-30 12:35:52 +020024006 vim_free(p);
24007 return NULL;
24008}
24009
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024010/*
24011 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024012 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24013 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024014 */
24015 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024016builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024017{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024018 char_u *p;
24019
24020 if (!ASCII_ISLOWER(name[0]))
24021 return FALSE;
24022 p = vim_strchr(name, AUTOLOAD_CHAR);
24023 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024024}
24025
Bram Moolenaar05159a02005-02-26 23:04:13 +000024026#if defined(FEAT_PROFILE) || defined(PROTO)
24027/*
24028 * Start profiling function "fp".
24029 */
24030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024031func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024032{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024033 int len = fp->uf_lines.ga_len;
24034
24035 if (len == 0)
24036 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024037 fp->uf_tm_count = 0;
24038 profile_zero(&fp->uf_tm_self);
24039 profile_zero(&fp->uf_tm_total);
24040 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024041 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024042 if (fp->uf_tml_total == NULL)
24043 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024044 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024045 if (fp->uf_tml_self == NULL)
24046 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024047 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024048 fp->uf_tml_idx = -1;
24049 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24050 || fp->uf_tml_self == NULL)
24051 return; /* out of memory */
24052
24053 fp->uf_profiling = TRUE;
24054}
24055
24056/*
24057 * Dump the profiling results for all functions in file "fd".
24058 */
24059 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024060func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024061{
24062 hashitem_T *hi;
24063 int todo;
24064 ufunc_T *fp;
24065 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024066 ufunc_T **sorttab;
24067 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024068
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024069 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024070 if (todo == 0)
24071 return; /* nothing to dump */
24072
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024073 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024074
Bram Moolenaar05159a02005-02-26 23:04:13 +000024075 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24076 {
24077 if (!HASHITEM_EMPTY(hi))
24078 {
24079 --todo;
24080 fp = HI2UF(hi);
24081 if (fp->uf_profiling)
24082 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024083 if (sorttab != NULL)
24084 sorttab[st_len++] = fp;
24085
Bram Moolenaar05159a02005-02-26 23:04:13 +000024086 if (fp->uf_name[0] == K_SPECIAL)
24087 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24088 else
24089 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24090 if (fp->uf_tm_count == 1)
24091 fprintf(fd, "Called 1 time\n");
24092 else
24093 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24094 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24095 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24096 fprintf(fd, "\n");
24097 fprintf(fd, "count total (s) self (s)\n");
24098
24099 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24100 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024101 if (FUNCLINE(fp, i) == NULL)
24102 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024103 prof_func_line(fd, fp->uf_tml_count[i],
24104 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024105 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24106 }
24107 fprintf(fd, "\n");
24108 }
24109 }
24110 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024111
24112 if (sorttab != NULL && st_len > 0)
24113 {
24114 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24115 prof_total_cmp);
24116 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24117 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24118 prof_self_cmp);
24119 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24120 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024121
24122 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024123}
Bram Moolenaar73830342005-02-28 22:48:19 +000024124
24125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024126prof_sort_list(
24127 FILE *fd,
24128 ufunc_T **sorttab,
24129 int st_len,
24130 char *title,
24131 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024132{
24133 int i;
24134 ufunc_T *fp;
24135
24136 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24137 fprintf(fd, "count total (s) self (s) function\n");
24138 for (i = 0; i < 20 && i < st_len; ++i)
24139 {
24140 fp = sorttab[i];
24141 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24142 prefer_self);
24143 if (fp->uf_name[0] == K_SPECIAL)
24144 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24145 else
24146 fprintf(fd, " %s()\n", fp->uf_name);
24147 }
24148 fprintf(fd, "\n");
24149}
24150
24151/*
24152 * Print the count and times for one function or function line.
24153 */
24154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024155prof_func_line(
24156 FILE *fd,
24157 int count,
24158 proftime_T *total,
24159 proftime_T *self,
24160 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024161{
24162 if (count > 0)
24163 {
24164 fprintf(fd, "%5d ", count);
24165 if (prefer_self && profile_equal(total, self))
24166 fprintf(fd, " ");
24167 else
24168 fprintf(fd, "%s ", profile_msg(total));
24169 if (!prefer_self && profile_equal(total, self))
24170 fprintf(fd, " ");
24171 else
24172 fprintf(fd, "%s ", profile_msg(self));
24173 }
24174 else
24175 fprintf(fd, " ");
24176}
24177
24178/*
24179 * Compare function for total time sorting.
24180 */
24181 static int
24182#ifdef __BORLANDC__
24183_RTLENTRYF
24184#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024185prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024186{
24187 ufunc_T *p1, *p2;
24188
24189 p1 = *(ufunc_T **)s1;
24190 p2 = *(ufunc_T **)s2;
24191 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24192}
24193
24194/*
24195 * Compare function for self time sorting.
24196 */
24197 static int
24198#ifdef __BORLANDC__
24199_RTLENTRYF
24200#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024201prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024202{
24203 ufunc_T *p1, *p2;
24204
24205 p1 = *(ufunc_T **)s1;
24206 p2 = *(ufunc_T **)s2;
24207 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24208}
24209
Bram Moolenaar05159a02005-02-26 23:04:13 +000024210#endif
24211
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024212/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024213 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024214 * Return TRUE if a package was loaded.
24215 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024216 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024217script_autoload(
24218 char_u *name,
24219 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024220{
24221 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024222 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024223 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024224 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024225
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024226 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024227 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024228 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024229 return FALSE;
24230
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024231 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024232
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024233 /* Find the name in the list of previously loaded package names. Skip
24234 * "autoload/", it's always the same. */
24235 for (i = 0; i < ga_loaded.ga_len; ++i)
24236 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24237 break;
24238 if (!reload && i < ga_loaded.ga_len)
24239 ret = FALSE; /* was loaded already */
24240 else
24241 {
24242 /* Remember the name if it wasn't loaded already. */
24243 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24244 {
24245 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24246 tofree = NULL;
24247 }
24248
24249 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024250 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024251 ret = TRUE;
24252 }
24253
24254 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024255 return ret;
24256}
24257
24258/*
24259 * Return the autoload script name for a function or variable name.
24260 * Returns NULL when out of memory.
24261 */
24262 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024263autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024264{
24265 char_u *p;
24266 char_u *scriptname;
24267
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024268 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024269 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24270 if (scriptname == NULL)
24271 return FALSE;
24272 STRCPY(scriptname, "autoload/");
24273 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024274 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024275 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024276 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024277 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024278 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024279}
24280
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24282
24283/*
24284 * Function given to ExpandGeneric() to obtain the list of user defined
24285 * function names.
24286 */
24287 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024288get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024290 static long_u done;
24291 static hashitem_T *hi;
24292 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293
24294 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024296 done = 0;
24297 hi = func_hashtab.ht_array;
24298 }
24299 if (done < func_hashtab.ht_used)
24300 {
24301 if (done++ > 0)
24302 ++hi;
24303 while (HASHITEM_EMPTY(hi))
24304 ++hi;
24305 fp = HI2UF(hi);
24306
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024307 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024308 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024309
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024310 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24311 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312
24313 cat_func_name(IObuff, fp);
24314 if (xp->xp_context != EXPAND_USER_FUNC)
24315 {
24316 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024317 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318 STRCAT(IObuff, ")");
24319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024320 return IObuff;
24321 }
24322 return NULL;
24323}
24324
24325#endif /* FEAT_CMDL_COMPL */
24326
24327/*
24328 * Copy the function name of "fp" to buffer "buf".
24329 * "buf" must be able to hold the function name plus three bytes.
24330 * Takes care of script-local function names.
24331 */
24332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024333cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024334{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024335 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024336 {
24337 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024338 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339 }
24340 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024341 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024342}
24343
24344/*
24345 * ":delfunction {name}"
24346 */
24347 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024348ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024350 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024351 char_u *p;
24352 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024353 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354
24355 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024356 name = trans_function_name(&p, eap->skip, 0, &fudi);
24357 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024359 {
24360 if (fudi.fd_dict != NULL && !eap->skip)
24361 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 if (!ends_excmd(*skipwhite(p)))
24365 {
24366 vim_free(name);
24367 EMSG(_(e_trailing));
24368 return;
24369 }
24370 eap->nextcmd = check_nextcmd(p);
24371 if (eap->nextcmd != NULL)
24372 *p = NUL;
24373
24374 if (!eap->skip)
24375 fp = find_func(name);
24376 vim_free(name);
24377
24378 if (!eap->skip)
24379 {
24380 if (fp == NULL)
24381 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024382 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 return;
24384 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024385 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 {
24387 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24388 return;
24389 }
24390
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024391 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024393 /* Delete the dict item that refers to the function, it will
24394 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024395 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024396 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024397 else
24398 func_free(fp);
24399 }
24400}
24401
24402/*
24403 * Free a function and remove it from the list of functions.
24404 */
24405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024406func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024407{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024408 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024409
24410 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024411 ga_clear_strings(&(fp->uf_args));
24412 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024413#ifdef FEAT_PROFILE
24414 vim_free(fp->uf_tml_count);
24415 vim_free(fp->uf_tml_total);
24416 vim_free(fp->uf_tml_self);
24417#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024418
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024419 /* remove the function from the function hashtable */
24420 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24421 if (HASHITEM_EMPTY(hi))
24422 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024423 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024424 hash_remove(&func_hashtab, hi);
24425
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024426 vim_free(fp);
24427}
24428
24429/*
24430 * Unreference a Function: decrement the reference count and free it when it
24431 * becomes zero. Only for numbered functions.
24432 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024433 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024434func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024435{
24436 ufunc_T *fp;
24437
24438 if (name != NULL && isdigit(*name))
24439 {
24440 fp = find_func(name);
24441 if (fp == NULL)
24442 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024443 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024444 {
24445 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024446 * when "uf_calls" becomes zero. */
24447 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024448 func_free(fp);
24449 }
24450 }
24451}
24452
24453/*
24454 * Count a reference to a Function.
24455 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024456 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024457func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024458{
24459 ufunc_T *fp;
24460
24461 if (name != NULL && isdigit(*name))
24462 {
24463 fp = find_func(name);
24464 if (fp == NULL)
24465 EMSG2(_(e_intern2), "func_ref()");
24466 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024467 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024468 }
24469}
24470
24471/*
24472 * Call a user function.
24473 */
24474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024475call_user_func(
24476 ufunc_T *fp, /* pointer to function */
24477 int argcount, /* nr of args */
24478 typval_T *argvars, /* arguments */
24479 typval_T *rettv, /* return value */
24480 linenr_T firstline, /* first line of range */
24481 linenr_T lastline, /* last line of range */
24482 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483{
Bram Moolenaar33570922005-01-25 22:26:29 +000024484 char_u *save_sourcing_name;
24485 linenr_T save_sourcing_lnum;
24486 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024487 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024488 int save_did_emsg;
24489 static int depth = 0;
24490 dictitem_T *v;
24491 int fixvar_idx = 0; /* index in fixvar[] */
24492 int i;
24493 int ai;
24494 char_u numbuf[NUMBUFLEN];
24495 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024496 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024497#ifdef FEAT_PROFILE
24498 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024499 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024501
24502 /* If depth of calling is getting too high, don't execute the function */
24503 if (depth >= p_mfd)
24504 {
24505 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024506 rettv->v_type = VAR_NUMBER;
24507 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024508 return;
24509 }
24510 ++depth;
24511
24512 line_breakcheck(); /* check for CTRL-C hit */
24513
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024514 fc = (funccall_T *)alloc(sizeof(funccall_T));
24515 fc->caller = current_funccal;
24516 current_funccal = fc;
24517 fc->func = fp;
24518 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024519 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024520 fc->linenr = 0;
24521 fc->returned = FALSE;
24522 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024524 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24525 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024526
Bram Moolenaar33570922005-01-25 22:26:29 +000024527 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024528 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024529 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24530 * each argument variable and saves a lot of time.
24531 */
24532 /*
24533 * Init l: variables.
24534 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024535 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024536 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024537 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024538 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24539 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024540 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024541 name = v->di_key;
24542 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024543 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024544 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024545 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024546 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024547 v->di_tv.vval.v_dict = selfdict;
24548 ++selfdict->dv_refcount;
24549 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024550
Bram Moolenaar33570922005-01-25 22:26:29 +000024551 /*
24552 * Init a: variables.
24553 * Set a:0 to "argcount".
24554 * Set a:000 to a list with room for the "..." arguments.
24555 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024556 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024557 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024558 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024559 /* Use "name" to avoid a warning from some compiler that checks the
24560 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024561 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024562 name = v->di_key;
24563 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024564 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024565 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024566 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024567 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024568 v->di_tv.vval.v_list = &fc->l_varlist;
24569 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24570 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24571 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024572
24573 /*
24574 * Set a:firstline to "firstline" and a:lastline to "lastline".
24575 * Set a:name to named arguments.
24576 * Set a:N to the "..." arguments.
24577 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024578 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024579 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024580 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024581 (varnumber_T)lastline);
24582 for (i = 0; i < argcount; ++i)
24583 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024584 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024585 if (ai < 0)
24586 /* named argument a:name */
24587 name = FUNCARG(fp, i);
24588 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024589 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024590 /* "..." argument a:1, a:2, etc. */
24591 sprintf((char *)numbuf, "%d", ai + 1);
24592 name = numbuf;
24593 }
24594 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24595 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024596 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024597 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24598 }
24599 else
24600 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024601 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24602 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024603 if (v == NULL)
24604 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024605 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024606 }
24607 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024608 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024609
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024610 /* Note: the values are copied directly to avoid alloc/free.
24611 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024612 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024613 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024614
24615 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24616 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024617 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24618 fc->l_listitems[ai].li_tv = argvars[i];
24619 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024620 }
24621 }
24622
Bram Moolenaar071d4272004-06-13 20:20:40 +000024623 /* Don't redraw while executing the function. */
24624 ++RedrawingDisabled;
24625 save_sourcing_name = sourcing_name;
24626 save_sourcing_lnum = sourcing_lnum;
24627 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024628 /* need space for function name + ("function " + 3) or "[number]" */
24629 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24630 + STRLEN(fp->uf_name) + 20;
24631 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 if (sourcing_name != NULL)
24633 {
24634 if (save_sourcing_name != NULL
24635 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024636 sprintf((char *)sourcing_name, "%s[%d]..",
24637 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 else
24639 STRCPY(sourcing_name, "function ");
24640 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24641
24642 if (p_verbose >= 12)
24643 {
24644 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024645 verbose_enter_scroll();
24646
Bram Moolenaar555b2802005-05-19 21:08:39 +000024647 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648 if (p_verbose >= 14)
24649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024651 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024652 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024653 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024654
24655 msg_puts((char_u *)"(");
24656 for (i = 0; i < argcount; ++i)
24657 {
24658 if (i > 0)
24659 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024660 if (argvars[i].v_type == VAR_NUMBER)
24661 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662 else
24663 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024664 /* Do not want errors such as E724 here. */
24665 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024666 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024667 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024668 if (s != NULL)
24669 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024670 if (vim_strsize(s) > MSG_BUF_CLEN)
24671 {
24672 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24673 s = buf;
24674 }
24675 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024676 vim_free(tofree);
24677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024678 }
24679 }
24680 msg_puts((char_u *)")");
24681 }
24682 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024683
24684 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685 --no_wait_return;
24686 }
24687 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024688#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024689 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024690 {
24691 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24692 func_do_profile(fp);
24693 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024694 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024695 {
24696 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024697 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024698 profile_zero(&fp->uf_tm_children);
24699 }
24700 script_prof_save(&wait_start);
24701 }
24702#endif
24703
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024705 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706 save_did_emsg = did_emsg;
24707 did_emsg = FALSE;
24708
24709 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024710 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24712
24713 --RedrawingDisabled;
24714
24715 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024716 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024717 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024718 clear_tv(rettv);
24719 rettv->v_type = VAR_NUMBER;
24720 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024721 }
24722
Bram Moolenaar05159a02005-02-26 23:04:13 +000024723#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024724 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024725 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024726 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024727 profile_end(&call_start);
24728 profile_sub_wait(&wait_start, &call_start);
24729 profile_add(&fp->uf_tm_total, &call_start);
24730 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024731 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024732 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024733 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24734 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024735 }
24736 }
24737#endif
24738
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739 /* when being verbose, mention the return value */
24740 if (p_verbose >= 12)
24741 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024742 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024743 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024744
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024746 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024747 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024748 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024749 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024750 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024752 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024753 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024754 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024755 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024756
Bram Moolenaar555b2802005-05-19 21:08:39 +000024757 /* The value may be very long. Skip the middle part, so that we
24758 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024759 * truncate it at the end. Don't want errors such as E724 here. */
24760 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024761 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024762 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024763 if (s != NULL)
24764 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024765 if (vim_strsize(s) > MSG_BUF_CLEN)
24766 {
24767 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24768 s = buf;
24769 }
24770 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024771 vim_free(tofree);
24772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024773 }
24774 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024775
24776 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 --no_wait_return;
24778 }
24779
24780 vim_free(sourcing_name);
24781 sourcing_name = save_sourcing_name;
24782 sourcing_lnum = save_sourcing_lnum;
24783 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024784#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024785 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024786 script_prof_restore(&wait_start);
24787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788
24789 if (p_verbose >= 12 && sourcing_name != NULL)
24790 {
24791 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024792 verbose_enter_scroll();
24793
Bram Moolenaar555b2802005-05-19 21:08:39 +000024794 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024795 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024796
24797 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024798 --no_wait_return;
24799 }
24800
24801 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024802 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024803 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024804
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024805 /* If the a:000 list and the l: and a: dicts are not referenced we can
24806 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024807 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24808 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24809 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24810 {
24811 free_funccal(fc, FALSE);
24812 }
24813 else
24814 {
24815 hashitem_T *hi;
24816 listitem_T *li;
24817 int todo;
24818
24819 /* "fc" is still in use. This can happen when returning "a:000" or
24820 * assigning "l:" to a global variable.
24821 * Link "fc" in the list for garbage collection later. */
24822 fc->caller = previous_funccal;
24823 previous_funccal = fc;
24824
24825 /* Make a copy of the a: variables, since we didn't do that above. */
24826 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24827 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24828 {
24829 if (!HASHITEM_EMPTY(hi))
24830 {
24831 --todo;
24832 v = HI2DI(hi);
24833 copy_tv(&v->di_tv, &v->di_tv);
24834 }
24835 }
24836
24837 /* Make a copy of the a:000 items, since we didn't do that above. */
24838 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24839 copy_tv(&li->li_tv, &li->li_tv);
24840 }
24841}
24842
24843/*
24844 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024845 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024846 */
24847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024848can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024849{
24850 return (fc->l_varlist.lv_copyID != copyID
24851 && fc->l_vars.dv_copyID != copyID
24852 && fc->l_avars.dv_copyID != copyID);
24853}
24854
24855/*
24856 * Free "fc" and what it contains.
24857 */
24858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024859free_funccal(
24860 funccall_T *fc,
24861 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024862{
24863 listitem_T *li;
24864
24865 /* The a: variables typevals may not have been allocated, only free the
24866 * allocated variables. */
24867 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24868
24869 /* free all l: variables */
24870 vars_clear(&fc->l_vars.dv_hashtab);
24871
24872 /* Free the a:000 variables if they were allocated. */
24873 if (free_val)
24874 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24875 clear_tv(&li->li_tv);
24876
24877 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878}
24879
24880/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024881 * Add a number variable "name" to dict "dp" with value "nr".
24882 */
24883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024884add_nr_var(
24885 dict_T *dp,
24886 dictitem_T *v,
24887 char *name,
24888 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024889{
24890 STRCPY(v->di_key, name);
24891 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24892 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24893 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024894 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024895 v->di_tv.vval.v_number = nr;
24896}
24897
24898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899 * ":return [expr]"
24900 */
24901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024902ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903{
24904 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024905 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 int returning = FALSE;
24907
24908 if (current_funccal == NULL)
24909 {
24910 EMSG(_("E133: :return not inside a function"));
24911 return;
24912 }
24913
24914 if (eap->skip)
24915 ++emsg_skip;
24916
24917 eap->nextcmd = NULL;
24918 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024919 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024920 {
24921 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024922 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 }
24926 /* It's safer to return also on error. */
24927 else if (!eap->skip)
24928 {
24929 /*
24930 * Return unless the expression evaluation has been cancelled due to an
24931 * aborting error, an interrupt, or an exception.
24932 */
24933 if (!aborting())
24934 returning = do_return(eap, FALSE, TRUE, NULL);
24935 }
24936
24937 /* When skipping or the return gets pending, advance to the next command
24938 * in this line (!returning). Otherwise, ignore the rest of the line.
24939 * Following lines will be ignored by get_func_line(). */
24940 if (returning)
24941 eap->nextcmd = NULL;
24942 else if (eap->nextcmd == NULL) /* no argument */
24943 eap->nextcmd = check_nextcmd(arg);
24944
24945 if (eap->skip)
24946 --emsg_skip;
24947}
24948
24949/*
24950 * Return from a function. Possibly makes the return pending. Also called
24951 * for a pending return at the ":endtry" or after returning from an extra
24952 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024953 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024954 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955 * FALSE when the return gets pending.
24956 */
24957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024958do_return(
24959 exarg_T *eap,
24960 int reanimate,
24961 int is_cmd,
24962 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963{
24964 int idx;
24965 struct condstack *cstack = eap->cstack;
24966
24967 if (reanimate)
24968 /* Undo the return. */
24969 current_funccal->returned = FALSE;
24970
24971 /*
24972 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24973 * not in its finally clause (which then is to be executed next) is found.
24974 * In this case, make the ":return" pending for execution at the ":endtry".
24975 * Otherwise, return normally.
24976 */
24977 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24978 if (idx >= 0)
24979 {
24980 cstack->cs_pending[idx] = CSTP_RETURN;
24981
24982 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024983 /* A pending return again gets pending. "rettv" points to an
24984 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024986 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987 else
24988 {
24989 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024990 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024991 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024992 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024994 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024995 {
24996 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024997 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024998 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024999 else
25000 EMSG(_(e_outofmem));
25001 }
25002 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025003 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004
25005 if (reanimate)
25006 {
25007 /* The pending return value could be overwritten by a ":return"
25008 * without argument in a finally clause; reset the default
25009 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025010 current_funccal->rettv->v_type = VAR_NUMBER;
25011 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 }
25013 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025014 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015 }
25016 else
25017 {
25018 current_funccal->returned = TRUE;
25019
25020 /* If the return is carried out now, store the return value. For
25021 * a return immediately after reanimation, the value is already
25022 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025023 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025025 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025026 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025027 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025028 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025029 }
25030 }
25031
25032 return idx < 0;
25033}
25034
25035/*
25036 * Free the variable with a pending return value.
25037 */
25038 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025039discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040{
Bram Moolenaar33570922005-01-25 22:26:29 +000025041 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042}
25043
25044/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025045 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046 * is an allocated string. Used by report_pending() for verbose messages.
25047 */
25048 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025049get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025051 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025052 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025053 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025055 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025056 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025057 if (s == NULL)
25058 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025059
25060 STRCPY(IObuff, ":return ");
25061 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25062 if (STRLEN(s) + 8 >= IOSIZE)
25063 STRCPY(IObuff + IOSIZE - 4, "...");
25064 vim_free(tofree);
25065 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025066}
25067
25068/*
25069 * Get next function line.
25070 * Called by do_cmdline() to get the next line.
25071 * Returns allocated string, or NULL for end of function.
25072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025073 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025074get_func_line(
25075 int c UNUSED,
25076 void *cookie,
25077 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078{
Bram Moolenaar33570922005-01-25 22:26:29 +000025079 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025080 ufunc_T *fp = fcp->func;
25081 char_u *retval;
25082 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083
25084 /* If breakpoints have been added/deleted need to check for it. */
25085 if (fcp->dbg_tick != debug_tick)
25086 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025087 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088 sourcing_lnum);
25089 fcp->dbg_tick = debug_tick;
25090 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025091#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025092 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025093 func_line_end(cookie);
25094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095
Bram Moolenaar05159a02005-02-26 23:04:13 +000025096 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025097 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25098 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025099 retval = NULL;
25100 else
25101 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025102 /* Skip NULL lines (continuation lines). */
25103 while (fcp->linenr < gap->ga_len
25104 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25105 ++fcp->linenr;
25106 if (fcp->linenr >= gap->ga_len)
25107 retval = NULL;
25108 else
25109 {
25110 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25111 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025112#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025113 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025114 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025115#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117 }
25118
25119 /* Did we encounter a breakpoint? */
25120 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25121 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025122 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025123 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025124 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025125 sourcing_lnum);
25126 fcp->dbg_tick = debug_tick;
25127 }
25128
25129 return retval;
25130}
25131
Bram Moolenaar05159a02005-02-26 23:04:13 +000025132#if defined(FEAT_PROFILE) || defined(PROTO)
25133/*
25134 * Called when starting to read a function line.
25135 * "sourcing_lnum" must be correct!
25136 * When skipping lines it may not actually be executed, but we won't find out
25137 * until later and we need to store the time now.
25138 */
25139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025140func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025141{
25142 funccall_T *fcp = (funccall_T *)cookie;
25143 ufunc_T *fp = fcp->func;
25144
25145 if (fp->uf_profiling && sourcing_lnum >= 1
25146 && sourcing_lnum <= fp->uf_lines.ga_len)
25147 {
25148 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025149 /* Skip continuation lines. */
25150 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25151 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025152 fp->uf_tml_execed = FALSE;
25153 profile_start(&fp->uf_tml_start);
25154 profile_zero(&fp->uf_tml_children);
25155 profile_get_wait(&fp->uf_tml_wait);
25156 }
25157}
25158
25159/*
25160 * Called when actually executing a function line.
25161 */
25162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025163func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025164{
25165 funccall_T *fcp = (funccall_T *)cookie;
25166 ufunc_T *fp = fcp->func;
25167
25168 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25169 fp->uf_tml_execed = TRUE;
25170}
25171
25172/*
25173 * Called when done with a function line.
25174 */
25175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025176func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025177{
25178 funccall_T *fcp = (funccall_T *)cookie;
25179 ufunc_T *fp = fcp->func;
25180
25181 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25182 {
25183 if (fp->uf_tml_execed)
25184 {
25185 ++fp->uf_tml_count[fp->uf_tml_idx];
25186 profile_end(&fp->uf_tml_start);
25187 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025188 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025189 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25190 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025191 }
25192 fp->uf_tml_idx = -1;
25193 }
25194}
25195#endif
25196
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197/*
25198 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025199 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200 */
25201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025202func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203{
Bram Moolenaar33570922005-01-25 22:26:29 +000025204 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205
25206 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25207 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025208 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 || fcp->returned);
25210}
25211
25212/*
25213 * return TRUE if cookie indicates a function which "abort"s on errors.
25214 */
25215 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025216func_has_abort(
25217 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025218{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025219 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220}
25221
25222#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25223typedef enum
25224{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025225 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25226 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25227 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228} var_flavour_T;
25229
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025230static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231
25232 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025233var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234{
25235 char_u *p = varname;
25236
25237 if (ASCII_ISUPPER(*p))
25238 {
25239 while (*(++p))
25240 if (ASCII_ISLOWER(*p))
25241 return VAR_FLAVOUR_SESSION;
25242 return VAR_FLAVOUR_VIMINFO;
25243 }
25244 else
25245 return VAR_FLAVOUR_DEFAULT;
25246}
25247#endif
25248
25249#if defined(FEAT_VIMINFO) || defined(PROTO)
25250/*
25251 * Restore global vars that start with a capital from the viminfo file
25252 */
25253 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025254read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255{
25256 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025257 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025258 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025259 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260
25261 if (!writing && (find_viminfo_parameter('!') != NULL))
25262 {
25263 tab = vim_strchr(virp->vir_line + 1, '\t');
25264 if (tab != NULL)
25265 {
25266 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025267 switch (*tab)
25268 {
25269 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025270#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025271 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025272#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025273 case 'D': type = VAR_DICT; break;
25274 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025275 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277
25278 tab = vim_strchr(tab, '\t');
25279 if (tab != NULL)
25280 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025281 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025282 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025283 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025284 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025285#ifdef FEAT_FLOAT
25286 else if (type == VAR_FLOAT)
25287 (void)string2float(tab + 1, &tv.vval.v_float);
25288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025290 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025291 if (type == VAR_DICT || type == VAR_LIST)
25292 {
25293 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25294
25295 if (etv == NULL)
25296 /* Failed to parse back the dict or list, use it as a
25297 * string. */
25298 tv.v_type = VAR_STRING;
25299 else
25300 {
25301 vim_free(tv.vval.v_string);
25302 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025303 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025304 }
25305 }
25306
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025307 /* when in a function use global variables */
25308 save_funccal = current_funccal;
25309 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025310 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025311 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025312
25313 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025314 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025315 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25316 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317 }
25318 }
25319 }
25320
25321 return viminfo_readline(virp);
25322}
25323
25324/*
25325 * Write global vars that start with a capital to the viminfo file
25326 */
25327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025328write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329{
Bram Moolenaar33570922005-01-25 22:26:29 +000025330 hashitem_T *hi;
25331 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025332 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025333 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025334 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025335 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025336 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337
25338 if (find_viminfo_parameter('!') == NULL)
25339 return;
25340
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025341 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025342
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025343 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025344 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025346 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025347 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025348 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025349 this_var = HI2DI(hi);
25350 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025351 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025352 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025353 {
25354 case VAR_STRING: s = "STR"; break;
25355 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025356 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025357 case VAR_DICT: s = "DIC"; break;
25358 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025359 case VAR_SPECIAL: s = "XPL"; break;
25360
25361 case VAR_UNKNOWN:
25362 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025363 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025364 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025365 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025366 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025367 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025368 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025369 if (p != NULL)
25370 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025371 vim_free(tofree);
25372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373 }
25374 }
25375}
25376#endif
25377
25378#if defined(FEAT_SESSION) || defined(PROTO)
25379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025380store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025381{
Bram Moolenaar33570922005-01-25 22:26:29 +000025382 hashitem_T *hi;
25383 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025384 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385 char_u *p, *t;
25386
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025387 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025388 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025390 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025392 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025393 this_var = HI2DI(hi);
25394 if ((this_var->di_tv.v_type == VAR_NUMBER
25395 || this_var->di_tv.v_type == VAR_STRING)
25396 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025397 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025398 /* Escape special characters with a backslash. Turn a LF and
25399 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025400 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025401 (char_u *)"\\\"\n\r");
25402 if (p == NULL) /* out of memory */
25403 break;
25404 for (t = p; *t != NUL; ++t)
25405 if (*t == '\n')
25406 *t = 'n';
25407 else if (*t == '\r')
25408 *t = 'r';
25409 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025410 this_var->di_key,
25411 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25412 : ' ',
25413 p,
25414 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25415 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025416 || put_eol(fd) == FAIL)
25417 {
25418 vim_free(p);
25419 return FAIL;
25420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421 vim_free(p);
25422 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025423#ifdef FEAT_FLOAT
25424 else if (this_var->di_tv.v_type == VAR_FLOAT
25425 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25426 {
25427 float_T f = this_var->di_tv.vval.v_float;
25428 int sign = ' ';
25429
25430 if (f < 0)
25431 {
25432 f = -f;
25433 sign = '-';
25434 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025435 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025436 this_var->di_key, sign, f) < 0)
25437 || put_eol(fd) == FAIL)
25438 return FAIL;
25439 }
25440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441 }
25442 }
25443 return OK;
25444}
25445#endif
25446
Bram Moolenaar661b1822005-07-28 22:36:45 +000025447/*
25448 * Display script name where an item was last set.
25449 * Should only be invoked when 'verbose' is non-zero.
25450 */
25451 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025452last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025453{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025454 char_u *p;
25455
Bram Moolenaar661b1822005-07-28 22:36:45 +000025456 if (scriptID != 0)
25457 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025458 p = home_replace_save(NULL, get_scriptname(scriptID));
25459 if (p != NULL)
25460 {
25461 verbose_enter();
25462 MSG_PUTS(_("\n\tLast set from "));
25463 MSG_PUTS(p);
25464 vim_free(p);
25465 verbose_leave();
25466 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025467 }
25468}
25469
Bram Moolenaard812df62008-11-09 12:46:09 +000025470/*
25471 * List v:oldfiles in a nice way.
25472 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025473 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025474ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025475{
25476 list_T *l = vimvars[VV_OLDFILES].vv_list;
25477 listitem_T *li;
25478 int nr = 0;
25479
25480 if (l == NULL)
25481 msg((char_u *)_("No old files"));
25482 else
25483 {
25484 msg_start();
25485 msg_scroll = TRUE;
25486 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25487 {
25488 msg_outnum((long)++nr);
25489 MSG_PUTS(": ");
25490 msg_outtrans(get_tv_string(&li->li_tv));
25491 msg_putchar('\n');
25492 out_flush(); /* output one line at a time */
25493 ui_breakcheck();
25494 }
25495 /* Assume "got_int" was set to truncate the listing. */
25496 got_int = FALSE;
25497
25498#ifdef FEAT_BROWSE_CMD
25499 if (cmdmod.browse)
25500 {
25501 quit_more = FALSE;
25502 nr = prompt_for_number(FALSE);
25503 msg_starthere();
25504 if (nr > 0)
25505 {
25506 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25507 (long)nr);
25508
25509 if (p != NULL)
25510 {
25511 p = expand_env_save(p);
25512 eap->arg = p;
25513 eap->cmdidx = CMD_edit;
25514 cmdmod.browse = FALSE;
25515 do_exedit(eap, NULL);
25516 vim_free(p);
25517 }
25518 }
25519 }
25520#endif
25521 }
25522}
25523
Bram Moolenaar53744302015-07-17 17:38:22 +020025524/* reset v:option_new, v:option_old and v:option_type */
25525 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025526reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025527{
25528 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25529 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25530 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25531}
25532
25533
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534#endif /* FEAT_EVAL */
25535
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025537#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538
25539#ifdef WIN3264
25540/*
25541 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25542 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025543static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25544static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25545static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546
25547/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025548 * Get the short path (8.3) for the filename in "fnamep".
25549 * Only works for a valid file name.
25550 * When the path gets longer "fnamep" is changed and the allocated buffer
25551 * is put in "bufp".
25552 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25553 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025554 */
25555 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025556get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025558 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 char_u *newbuf;
25560
25561 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025562 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 if (l > len - 1)
25564 {
25565 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025566 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567 newbuf = vim_strnsave(*fnamep, l);
25568 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570
25571 vim_free(*bufp);
25572 *fnamep = *bufp = newbuf;
25573
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025574 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025575 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025576 }
25577
25578 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025579 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025580}
25581
25582/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025583 * Get the short path (8.3) for the filename in "fname". The converted
25584 * path is returned in "bufp".
25585 *
25586 * Some of the directories specified in "fname" may not exist. This function
25587 * will shorten the existing directories at the beginning of the path and then
25588 * append the remaining non-existing path.
25589 *
25590 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025591 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025592 * bufp - Pointer to an allocated buffer for the filename.
25593 * fnamelen - Length of the filename pointed to by fname
25594 *
25595 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596 */
25597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025598shortpath_for_invalid_fname(
25599 char_u **fname,
25600 char_u **bufp,
25601 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025603 char_u *short_fname, *save_fname, *pbuf_unused;
25604 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025605 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025606 int old_len, len;
25607 int new_len, sfx_len;
25608 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609
25610 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025611 old_len = *fnamelen;
25612 save_fname = vim_strnsave(*fname, old_len);
25613 pbuf_unused = NULL;
25614 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025615
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025616 endp = save_fname + old_len - 1; /* Find the end of the copy */
25617 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025619 /*
25620 * Try shortening the supplied path till it succeeds by removing one
25621 * directory at a time from the tail of the path.
25622 */
25623 len = 0;
25624 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025625 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025626 /* go back one path-separator */
25627 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25628 --endp;
25629 if (endp <= save_fname)
25630 break; /* processed the complete path */
25631
25632 /*
25633 * Replace the path separator with a NUL and try to shorten the
25634 * resulting path.
25635 */
25636 ch = *endp;
25637 *endp = 0;
25638 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025639 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025640 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25641 {
25642 retval = FAIL;
25643 goto theend;
25644 }
25645 *endp = ch; /* preserve the string */
25646
25647 if (len > 0)
25648 break; /* successfully shortened the path */
25649
25650 /* failed to shorten the path. Skip the path separator */
25651 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 }
25653
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025654 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025656 /*
25657 * Succeeded in shortening the path. Now concatenate the shortened
25658 * path with the remaining path at the tail.
25659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025661 /* Compute the length of the new path. */
25662 sfx_len = (int)(save_endp - endp) + 1;
25663 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025665 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025667 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025668 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025669 /* There is not enough space in the currently allocated string,
25670 * copy it to a buffer big enough. */
25671 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025672 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025673 {
25674 retval = FAIL;
25675 goto theend;
25676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025677 }
25678 else
25679 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025680 /* Transfer short_fname to the main buffer (it's big enough),
25681 * unless get_short_pathname() did its work in-place. */
25682 *fname = *bufp = save_fname;
25683 if (short_fname != save_fname)
25684 vim_strncpy(save_fname, short_fname, len);
25685 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025687
25688 /* concat the not-shortened part of the path */
25689 vim_strncpy(*fname + len, endp, sfx_len);
25690 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025691 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025692
25693theend:
25694 vim_free(pbuf_unused);
25695 vim_free(save_fname);
25696
25697 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025698}
25699
25700/*
25701 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025702 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703 */
25704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025705shortpath_for_partial(
25706 char_u **fnamep,
25707 char_u **bufp,
25708 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025709{
25710 int sepcount, len, tflen;
25711 char_u *p;
25712 char_u *pbuf, *tfname;
25713 int hasTilde;
25714
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025715 /* Count up the path separators from the RHS.. so we know which part
25716 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025717 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025718 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025719 if (vim_ispathsep(*p))
25720 ++sepcount;
25721
25722 /* Need full path first (use expand_env() to remove a "~/") */
25723 hasTilde = (**fnamep == '~');
25724 if (hasTilde)
25725 pbuf = tfname = expand_env_save(*fnamep);
25726 else
25727 pbuf = tfname = FullName_save(*fnamep, FALSE);
25728
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025729 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025730
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025731 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25732 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025733
25734 if (len == 0)
25735 {
25736 /* Don't have a valid filename, so shorten the rest of the
25737 * path if we can. This CAN give us invalid 8.3 filenames, but
25738 * there's not a lot of point in guessing what it might be.
25739 */
25740 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025741 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25742 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025743 }
25744
25745 /* Count the paths backward to find the beginning of the desired string. */
25746 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025747 {
25748#ifdef FEAT_MBYTE
25749 if (has_mbyte)
25750 p -= mb_head_off(tfname, p);
25751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025752 if (vim_ispathsep(*p))
25753 {
25754 if (sepcount == 0 || (hasTilde && sepcount == 1))
25755 break;
25756 else
25757 sepcount --;
25758 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025760 if (hasTilde)
25761 {
25762 --p;
25763 if (p >= tfname)
25764 *p = '~';
25765 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025766 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025767 }
25768 else
25769 ++p;
25770
25771 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25772 vim_free(*bufp);
25773 *fnamelen = (int)STRLEN(p);
25774 *bufp = pbuf;
25775 *fnamep = p;
25776
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025777 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778}
25779#endif /* WIN3264 */
25780
25781/*
25782 * Adjust a filename, according to a string of modifiers.
25783 * *fnamep must be NUL terminated when called. When returning, the length is
25784 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025785 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786 * When there is an error, *fnamep is set to NULL.
25787 */
25788 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025789modify_fname(
25790 char_u *src, /* string with modifiers */
25791 int *usedlen, /* characters after src that are used */
25792 char_u **fnamep, /* file name so far */
25793 char_u **bufp, /* buffer for allocated file name or NULL */
25794 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795{
25796 int valid = 0;
25797 char_u *tail;
25798 char_u *s, *p, *pbuf;
25799 char_u dirname[MAXPATHL];
25800 int c;
25801 int has_fullname = 0;
25802#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025803 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025804 int has_shortname = 0;
25805#endif
25806
25807repeat:
25808 /* ":p" - full path/file_name */
25809 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25810 {
25811 has_fullname = 1;
25812
25813 valid |= VALID_PATH;
25814 *usedlen += 2;
25815
25816 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25817 if ((*fnamep)[0] == '~'
25818#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25819 && ((*fnamep)[1] == '/'
25820# ifdef BACKSLASH_IN_FILENAME
25821 || (*fnamep)[1] == '\\'
25822# endif
25823 || (*fnamep)[1] == NUL)
25824
25825#endif
25826 )
25827 {
25828 *fnamep = expand_env_save(*fnamep);
25829 vim_free(*bufp); /* free any allocated file name */
25830 *bufp = *fnamep;
25831 if (*fnamep == NULL)
25832 return -1;
25833 }
25834
25835 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025836 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025837 {
25838 if (vim_ispathsep(*p)
25839 && p[1] == '.'
25840 && (p[2] == NUL
25841 || vim_ispathsep(p[2])
25842 || (p[2] == '.'
25843 && (p[3] == NUL || vim_ispathsep(p[3])))))
25844 break;
25845 }
25846
25847 /* FullName_save() is slow, don't use it when not needed. */
25848 if (*p != NUL || !vim_isAbsName(*fnamep))
25849 {
25850 *fnamep = FullName_save(*fnamep, *p != NUL);
25851 vim_free(*bufp); /* free any allocated file name */
25852 *bufp = *fnamep;
25853 if (*fnamep == NULL)
25854 return -1;
25855 }
25856
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025857#ifdef WIN3264
25858# if _WIN32_WINNT >= 0x0500
25859 if (vim_strchr(*fnamep, '~') != NULL)
25860 {
25861 /* Expand 8.3 filename to full path. Needed to make sure the same
25862 * file does not have two different names.
25863 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25864 p = alloc(_MAX_PATH + 1);
25865 if (p != NULL)
25866 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025867 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025868 {
25869 vim_free(*bufp);
25870 *bufp = *fnamep = p;
25871 }
25872 else
25873 vim_free(p);
25874 }
25875 }
25876# endif
25877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878 /* Append a path separator to a directory. */
25879 if (mch_isdir(*fnamep))
25880 {
25881 /* Make room for one or two extra characters. */
25882 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25883 vim_free(*bufp); /* free any allocated file name */
25884 *bufp = *fnamep;
25885 if (*fnamep == NULL)
25886 return -1;
25887 add_pathsep(*fnamep);
25888 }
25889 }
25890
25891 /* ":." - path relative to the current directory */
25892 /* ":~" - path relative to the home directory */
25893 /* ":8" - shortname path - postponed till after */
25894 while (src[*usedlen] == ':'
25895 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25896 {
25897 *usedlen += 2;
25898 if (c == '8')
25899 {
25900#ifdef WIN3264
25901 has_shortname = 1; /* Postpone this. */
25902#endif
25903 continue;
25904 }
25905 pbuf = NULL;
25906 /* Need full path first (use expand_env() to remove a "~/") */
25907 if (!has_fullname)
25908 {
25909 if (c == '.' && **fnamep == '~')
25910 p = pbuf = expand_env_save(*fnamep);
25911 else
25912 p = pbuf = FullName_save(*fnamep, FALSE);
25913 }
25914 else
25915 p = *fnamep;
25916
25917 has_fullname = 0;
25918
25919 if (p != NULL)
25920 {
25921 if (c == '.')
25922 {
25923 mch_dirname(dirname, MAXPATHL);
25924 s = shorten_fname(p, dirname);
25925 if (s != NULL)
25926 {
25927 *fnamep = s;
25928 if (pbuf != NULL)
25929 {
25930 vim_free(*bufp); /* free any allocated file name */
25931 *bufp = pbuf;
25932 pbuf = NULL;
25933 }
25934 }
25935 }
25936 else
25937 {
25938 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25939 /* Only replace it when it starts with '~' */
25940 if (*dirname == '~')
25941 {
25942 s = vim_strsave(dirname);
25943 if (s != NULL)
25944 {
25945 *fnamep = s;
25946 vim_free(*bufp);
25947 *bufp = s;
25948 }
25949 }
25950 }
25951 vim_free(pbuf);
25952 }
25953 }
25954
25955 tail = gettail(*fnamep);
25956 *fnamelen = (int)STRLEN(*fnamep);
25957
25958 /* ":h" - head, remove "/file_name", can be repeated */
25959 /* Don't remove the first "/" or "c:\" */
25960 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25961 {
25962 valid |= VALID_HEAD;
25963 *usedlen += 2;
25964 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025965 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025966 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025967 *fnamelen = (int)(tail - *fnamep);
25968#ifdef VMS
25969 if (*fnamelen > 0)
25970 *fnamelen += 1; /* the path separator is part of the path */
25971#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025972 if (*fnamelen == 0)
25973 {
25974 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25975 p = vim_strsave((char_u *)".");
25976 if (p == NULL)
25977 return -1;
25978 vim_free(*bufp);
25979 *bufp = *fnamep = tail = p;
25980 *fnamelen = 1;
25981 }
25982 else
25983 {
25984 while (tail > s && !after_pathsep(s, tail))
25985 mb_ptr_back(*fnamep, tail);
25986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987 }
25988
25989 /* ":8" - shortname */
25990 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25991 {
25992 *usedlen += 2;
25993#ifdef WIN3264
25994 has_shortname = 1;
25995#endif
25996 }
25997
25998#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025999 /*
26000 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026001 */
26002 if (has_shortname)
26003 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026004 /* Copy the string if it is shortened by :h and when it wasn't copied
26005 * yet, because we are going to change it in place. Avoids changing
26006 * the buffer name for "%:8". */
26007 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026008 {
26009 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026010 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026011 return -1;
26012 vim_free(*bufp);
26013 *bufp = *fnamep = p;
26014 }
26015
26016 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026017 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018 if (!has_fullname && !vim_isAbsName(*fnamep))
26019 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026020 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026021 return -1;
26022 }
26023 else
26024 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026025 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026
Bram Moolenaardc935552011-08-17 15:23:23 +020026027 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026028 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026029 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026030 return -1;
26031
26032 if (l == 0)
26033 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026034 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026036 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026037 return -1;
26038 }
26039 *fnamelen = l;
26040 }
26041 }
26042#endif /* WIN3264 */
26043
26044 /* ":t" - tail, just the basename */
26045 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26046 {
26047 *usedlen += 2;
26048 *fnamelen -= (int)(tail - *fnamep);
26049 *fnamep = tail;
26050 }
26051
26052 /* ":e" - extension, can be repeated */
26053 /* ":r" - root, without extension, can be repeated */
26054 while (src[*usedlen] == ':'
26055 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26056 {
26057 /* find a '.' in the tail:
26058 * - for second :e: before the current fname
26059 * - otherwise: The last '.'
26060 */
26061 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26062 s = *fnamep - 2;
26063 else
26064 s = *fnamep + *fnamelen - 1;
26065 for ( ; s > tail; --s)
26066 if (s[0] == '.')
26067 break;
26068 if (src[*usedlen + 1] == 'e') /* :e */
26069 {
26070 if (s > tail)
26071 {
26072 *fnamelen += (int)(*fnamep - (s + 1));
26073 *fnamep = s + 1;
26074#ifdef VMS
26075 /* cut version from the extension */
26076 s = *fnamep + *fnamelen - 1;
26077 for ( ; s > *fnamep; --s)
26078 if (s[0] == ';')
26079 break;
26080 if (s > *fnamep)
26081 *fnamelen = s - *fnamep;
26082#endif
26083 }
26084 else if (*fnamep <= tail)
26085 *fnamelen = 0;
26086 }
26087 else /* :r */
26088 {
26089 if (s > tail) /* remove one extension */
26090 *fnamelen = (int)(s - *fnamep);
26091 }
26092 *usedlen += 2;
26093 }
26094
26095 /* ":s?pat?foo?" - substitute */
26096 /* ":gs?pat?foo?" - global substitute */
26097 if (src[*usedlen] == ':'
26098 && (src[*usedlen + 1] == 's'
26099 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26100 {
26101 char_u *str;
26102 char_u *pat;
26103 char_u *sub;
26104 int sep;
26105 char_u *flags;
26106 int didit = FALSE;
26107
26108 flags = (char_u *)"";
26109 s = src + *usedlen + 2;
26110 if (src[*usedlen + 1] == 'g')
26111 {
26112 flags = (char_u *)"g";
26113 ++s;
26114 }
26115
26116 sep = *s++;
26117 if (sep)
26118 {
26119 /* find end of pattern */
26120 p = vim_strchr(s, sep);
26121 if (p != NULL)
26122 {
26123 pat = vim_strnsave(s, (int)(p - s));
26124 if (pat != NULL)
26125 {
26126 s = p + 1;
26127 /* find end of substitution */
26128 p = vim_strchr(s, sep);
26129 if (p != NULL)
26130 {
26131 sub = vim_strnsave(s, (int)(p - s));
26132 str = vim_strnsave(*fnamep, *fnamelen);
26133 if (sub != NULL && str != NULL)
26134 {
26135 *usedlen = (int)(p + 1 - src);
26136 s = do_string_sub(str, pat, sub, flags);
26137 if (s != NULL)
26138 {
26139 *fnamep = s;
26140 *fnamelen = (int)STRLEN(s);
26141 vim_free(*bufp);
26142 *bufp = s;
26143 didit = TRUE;
26144 }
26145 }
26146 vim_free(sub);
26147 vim_free(str);
26148 }
26149 vim_free(pat);
26150 }
26151 }
26152 /* after using ":s", repeat all the modifiers */
26153 if (didit)
26154 goto repeat;
26155 }
26156 }
26157
Bram Moolenaar26df0922014-02-23 23:39:13 +010026158 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26159 {
26160 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26161 if (p == NULL)
26162 return -1;
26163 vim_free(*bufp);
26164 *bufp = *fnamep = p;
26165 *fnamelen = (int)STRLEN(p);
26166 *usedlen += 2;
26167 }
26168
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169 return valid;
26170}
26171
26172/*
26173 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26174 * "flags" can be "g" to do a global substitute.
26175 * Returns an allocated string, NULL for error.
26176 */
26177 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026178do_string_sub(
26179 char_u *str,
26180 char_u *pat,
26181 char_u *sub,
26182 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026183{
26184 int sublen;
26185 regmatch_T regmatch;
26186 int i;
26187 int do_all;
26188 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026189 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190 garray_T ga;
26191 char_u *ret;
26192 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026193 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026194
26195 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26196 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026197 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198
26199 ga_init2(&ga, 1, 200);
26200
26201 do_all = (flags[0] == 'g');
26202
26203 regmatch.rm_ic = p_ic;
26204 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26205 if (regmatch.regprog != NULL)
26206 {
26207 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026208 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026209 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26210 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026211 /* Skip empty match except for first match. */
26212 if (regmatch.startp[0] == regmatch.endp[0])
26213 {
26214 if (zero_width == regmatch.startp[0])
26215 {
26216 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026217 i = MB_PTR2LEN(tail);
26218 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26219 (size_t)i);
26220 ga.ga_len += i;
26221 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026222 continue;
26223 }
26224 zero_width = regmatch.startp[0];
26225 }
26226
Bram Moolenaar071d4272004-06-13 20:20:40 +000026227 /*
26228 * Get some space for a temporary buffer to do the substitution
26229 * into. It will contain:
26230 * - The text up to where the match is.
26231 * - The substituted text.
26232 * - The text after the match.
26233 */
26234 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026235 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026236 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26237 {
26238 ga_clear(&ga);
26239 break;
26240 }
26241
26242 /* copy the text up to where the match is */
26243 i = (int)(regmatch.startp[0] - tail);
26244 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26245 /* add the substituted text */
26246 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26247 + ga.ga_len + i, TRUE, TRUE, FALSE);
26248 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026249 tail = regmatch.endp[0];
26250 if (*tail == NUL)
26251 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026252 if (!do_all)
26253 break;
26254 }
26255
26256 if (ga.ga_data != NULL)
26257 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26258
Bram Moolenaar473de612013-06-08 18:19:48 +020026259 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026260 }
26261
26262 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26263 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026264 if (p_cpo == empty_option)
26265 p_cpo = save_cpo;
26266 else
26267 /* Darn, evaluating {sub} expression changed the value. */
26268 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269
26270 return ret;
26271}
26272
26273#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */