blob: 31dff055451c48a2665435bcffeee2ee5d77c58a [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 Moolenaar40ea1da2016-02-19 22:33:35 +01009889 if (tv->v_type == VAR_UNKNOWN)
9890 return OK;
9891 if (tv->v_type != VAR_DICT)
9892 {
9893 EMSG(_(e_invarg));
9894 return FAIL;
9895 }
9896 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009897 if (dict == NULL)
9898 return OK;
9899
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009900 todo = (int)dict->dv_hashtab.ht_used;
9901 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
9902 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009903 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009904 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009905
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009906 if (STRCMP(hi->hi_key, "mode") == 0)
9907 {
9908 if (!(supported & JO_MODE))
9909 break;
9910 opt->jo_set |= JO_MODE;
9911 mode = get_tv_string(item);
9912 if (STRCMP(mode, "nl") == 0)
9913 opt->jo_mode = MODE_NL;
9914 else if (STRCMP(mode, "raw") == 0)
9915 opt->jo_mode = MODE_RAW;
9916 else if (STRCMP(mode, "js") == 0)
9917 opt->jo_mode = MODE_JS;
9918 else if (STRCMP(mode, "json") == 0)
9919 opt->jo_mode = MODE_JSON;
9920 else
9921 {
9922 EMSG2(_(e_invarg2), mode);
9923 return FAIL;
9924 }
9925 }
9926 else if (STRCMP(hi->hi_key, "callback") == 0)
9927 {
9928 if (!(supported & JO_CALLBACK))
9929 break;
9930 opt->jo_set |= JO_CALLBACK;
9931 opt->jo_callback = get_callback(item);
9932 if (opt->jo_callback == NULL)
9933 {
9934 EMSG2(_(e_invarg2), "callback");
9935 return FAIL;
9936 }
9937 }
9938 else if (STRCMP(hi->hi_key, "waittime") == 0)
9939 {
9940 if (!(supported & JO_WAITTIME))
9941 break;
9942 opt->jo_set |= JO_WAITTIME;
9943 opt->jo_waittime = get_tv_number(item);
9944 }
9945 else if (STRCMP(hi->hi_key, "timeout") == 0)
9946 {
9947 if (!(supported & JO_TIMEOUT))
9948 break;
9949 opt->jo_set |= JO_TIMEOUT;
9950 opt->jo_timeout = get_tv_number(item);
9951 }
9952 else
9953 break;
9954 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009955 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009956 if (todo > 0)
9957 {
9958 EMSG2(_(e_invarg2), hi->hi_key);
9959 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +01009960 }
9961
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009962 return OK;
9963}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009964#endif
9965
9966#ifdef FEAT_CHANNEL
9967/*
9968 * Get the channel from the argument.
9969 * Returns NULL if the handle is invalid.
9970 */
9971 static channel_T *
9972get_channel_arg(typval_T *tv)
9973{
9974 channel_T *channel;
9975
9976 if (tv->v_type != VAR_CHANNEL)
9977 {
9978 EMSG2(_(e_invarg2), get_tv_string(tv));
9979 return NULL;
9980 }
9981 channel = tv->vval.v_channel;
9982
9983 if (channel == NULL || !channel_is_open(channel))
9984 {
9985 EMSG(_("E906: not an open channel"));
9986 return NULL;
9987 }
9988 return channel;
9989}
9990
9991/*
9992 * "ch_close()" function
9993 */
9994 static void
9995f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9996{
9997 channel_T *channel = get_channel_arg(&argvars[0]);
9998
9999 if (channel != NULL)
10000 channel_close(channel);
10001}
10002
10003/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010004 * "ch_log()" function
10005 */
10006 static void
10007f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10008{
10009 char_u *msg = get_tv_string(&argvars[0]);
10010 channel_T *channel = NULL;
10011
10012 if (argvars[1].v_type != VAR_UNKNOWN)
10013 channel = get_channel_arg(&argvars[1]);
10014
10015 ch_log(channel, (char *)msg);
10016}
10017
10018/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010019 * "ch_logfile()" function
10020 */
10021 static void
10022f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10023{
10024 char_u *fname;
10025 char_u *opt = (char_u *)"";
10026 char_u buf[NUMBUFLEN];
10027 FILE *file = NULL;
10028
10029 fname = get_tv_string(&argvars[0]);
10030 if (argvars[1].v_type == VAR_STRING)
10031 opt = get_tv_string_buf(&argvars[1], buf);
10032 if (*fname != NUL)
10033 {
10034 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10035 if (file == NULL)
10036 {
10037 EMSG2(_(e_notopen), fname);
10038 return;
10039 }
10040 }
10041 ch_logfile(file);
10042}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010043
10044/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010045 * "ch_open()" function
10046 */
10047 static void
10048f_ch_open(typval_T *argvars, typval_T *rettv)
10049{
10050 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010051 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010052 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010053 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010054 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010055 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010056
10057 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010058 rettv->v_type = VAR_CHANNEL;
10059 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010060
10061 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010062 if (argvars[1].v_type != VAR_UNKNOWN
10063 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010064 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010065 EMSG(_(e_invarg));
10066 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010067 }
10068
10069 /* parse address */
10070 p = vim_strchr(address, ':');
10071 if (p == NULL)
10072 {
10073 EMSG2(_(e_invarg2), address);
10074 return;
10075 }
10076 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010077 port = strtol((char *)p, &rest, 10);
10078 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010079 {
10080 p[-1] = ':';
10081 EMSG2(_(e_invarg2), address);
10082 return;
10083 }
10084
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010085 /* parse options */
10086 opt.jo_mode = MODE_JSON;
10087 opt.jo_callback = NULL;
10088 opt.jo_waittime = 0;
10089 opt.jo_timeout = 2000;
10090 if (get_job_options(&argvars[1], &opt,
10091 JO_MODE + JO_CALLBACK + JO_WAITTIME + JO_TIMEOUT) == FAIL)
10092 return;
10093 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010094 {
10095 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010096 return;
10097 }
10098
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010099 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010100 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010101 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010102 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010103 opt.jo_set = JO_ALL;
10104 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010105 }
10106}
10107
10108/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010109 * "ch_readraw()" function
10110 */
10111 static void
10112f_ch_readraw(typval_T *argvars, typval_T *rettv)
10113{
Bram Moolenaar77073442016-02-13 23:23:53 +010010114 channel_T *channel;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010115
10116 /* return an empty string by default */
10117 rettv->v_type = VAR_STRING;
10118 rettv->vval.v_string = NULL;
10119
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010120 /* TODO: use timeout from the options */
10121
Bram Moolenaar77073442016-02-13 23:23:53 +010010122 channel = get_channel_arg(&argvars[0]);
10123 if (channel != NULL)
10124 rettv->vval.v_string = channel_read_block(channel);
10125}
10126
10127/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010128 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010129 * Returns the channel if the caller should read the response.
10130 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010131 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010132 static channel_T *
Bram Moolenaara07fec92016-02-05 21:04:08 +010010133send_common(typval_T *argvars, char_u *text, int id, char *fun)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010134{
Bram Moolenaar77073442016-02-13 23:23:53 +010010135 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010136 jobopt_T opt;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010137
Bram Moolenaar77073442016-02-13 23:23:53 +010010138 channel = get_channel_arg(&argvars[0]);
10139 if (channel == NULL)
10140 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010141
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010142 opt.jo_callback = NULL;
10143 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10144 return NULL;
10145
Bram Moolenaara07fec92016-02-05 21:04:08 +010010146 /* Set the callback. An empty callback means no callback and not reading
10147 * the response. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010148 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
10149 channel_set_req_callback(channel, opt.jo_callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010150
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010151 if (channel_send(channel, text, fun) == OK && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010152 return channel;
10153 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010154}
10155
10156/*
10157 * "ch_sendexpr()" function
10158 */
10159 static void
10160f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10161{
10162 char_u *text;
10163 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010164 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010165 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010166 ch_mode_T ch_mode;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010167
10168 /* return an empty string by default */
10169 rettv->v_type = VAR_STRING;
10170 rettv->vval.v_string = NULL;
10171
Bram Moolenaar77073442016-02-13 23:23:53 +010010172 channel = get_channel_arg(&argvars[0]);
10173 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010174 return;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010175
Bram Moolenaar77073442016-02-13 23:23:53 +010010176 ch_mode = channel_get_mode(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010177 if (ch_mode == MODE_RAW)
10178 {
10179 EMSG(_("E912: cannot use ch_sendexpr() with a raw channel"));
10180 return;
10181 }
10182
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010183 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010184 text = json_encode_nr_expr(id, &argvars[1],
10185 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010186 if (text == NULL)
10187 return;
10188
Bram Moolenaar77073442016-02-13 23:23:53 +010010189 channel = send_common(argvars, text, id, "sendexpr");
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010190 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010191 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010192 {
Bram Moolenaar77073442016-02-13 23:23:53 +010010193 if (channel_read_json_block(channel, id, &listtv) == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010194 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010195 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010196
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010197 /* Move the item from the list and then change the type to
10198 * avoid the value being freed. */
10199 *rettv = list->lv_last->li_tv;
10200 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010201 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010202 }
10203 }
10204}
10205
10206/*
10207 * "ch_sendraw()" function
10208 */
10209 static void
10210f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10211{
10212 char_u buf[NUMBUFLEN];
10213 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010214 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010215
10216 /* return an empty string by default */
10217 rettv->v_type = VAR_STRING;
10218 rettv->vval.v_string = NULL;
10219
10220 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar77073442016-02-13 23:23:53 +010010221 channel = send_common(argvars, text, 0, "sendraw");
10222 if (channel != NULL)
10223 rettv->vval.v_string = channel_read_block(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010224}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010225
10226/*
10227 * "ch_setoptions()" function
10228 */
10229 static void
10230f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10231{
10232 channel_T *channel;
10233 jobopt_T opt;
10234
10235 channel = get_channel_arg(&argvars[0]);
10236 if (channel == NULL)
10237 return;
10238 if (get_job_options(&argvars[1], &opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010239 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010240 channel_set_options(channel, &opt);
10241}
10242
10243/*
10244 * "ch_status()" function
10245 */
10246 static void
10247f_ch_status(typval_T *argvars, typval_T *rettv)
10248{
10249 /* return an empty string by default */
10250 rettv->v_type = VAR_STRING;
10251
10252 if (argvars[0].v_type != VAR_CHANNEL)
10253 {
10254 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10255 rettv->vval.v_string = NULL;
10256 }
10257 else
10258 rettv->vval.v_string = vim_strsave(
10259 (char_u *)channel_status(argvars[0].vval.v_channel));
10260}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010261#endif
10262
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010263/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010264 * "changenr()" function
10265 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010267f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010268{
10269 rettv->vval.v_number = curbuf->b_u_seq_cur;
10270}
10271
10272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010273 * "char2nr(string)" function
10274 */
10275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010276f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277{
10278#ifdef FEAT_MBYTE
10279 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010280 {
10281 int utf8 = 0;
10282
10283 if (argvars[1].v_type != VAR_UNKNOWN)
10284 utf8 = get_tv_number_chk(&argvars[1], NULL);
10285
10286 if (utf8)
10287 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10288 else
10289 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 else
10292#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010293 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294}
10295
10296/*
10297 * "cindent(lnum)" function
10298 */
10299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010300f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301{
10302#ifdef FEAT_CINDENT
10303 pos_T pos;
10304 linenr_T lnum;
10305
10306 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010307 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10309 {
10310 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010311 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 curwin->w_cursor = pos;
10313 }
10314 else
10315#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317}
10318
10319/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010320 * "clearmatches()" function
10321 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010323f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010324{
10325#ifdef FEAT_SEARCH_EXTRA
10326 clear_matches(curwin);
10327#endif
10328}
10329
10330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 * "col(string)" function
10332 */
10333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010334f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335{
10336 colnr_T col = 0;
10337 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010338 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010340 fp = var2fpos(&argvars[0], FALSE, &fnum);
10341 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 {
10343 if (fp->col == MAXCOL)
10344 {
10345 /* '> can be MAXCOL, get the length of the line then */
10346 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010347 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 else
10349 col = MAXCOL;
10350 }
10351 else
10352 {
10353 col = fp->col + 1;
10354#ifdef FEAT_VIRTUALEDIT
10355 /* col(".") when the cursor is on the NUL at the end of the line
10356 * because of "coladd" can be seen as an extra column. */
10357 if (virtual_active() && fp == &curwin->w_cursor)
10358 {
10359 char_u *p = ml_get_cursor();
10360
10361 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10362 curwin->w_virtcol - curwin->w_cursor.coladd))
10363 {
10364# ifdef FEAT_MBYTE
10365 int l;
10366
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010367 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010368 col += l;
10369# else
10370 if (*p != NUL && p[1] == NUL)
10371 ++col;
10372# endif
10373 }
10374 }
10375#endif
10376 }
10377 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010378 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379}
10380
Bram Moolenaar572cb562005-08-05 21:35:02 +000010381#if defined(FEAT_INS_EXPAND)
10382/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010383 * "complete()" function
10384 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010386f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010387{
10388 int startcol;
10389
10390 if ((State & INSERT) == 0)
10391 {
10392 EMSG(_("E785: complete() can only be used in Insert mode"));
10393 return;
10394 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010395
10396 /* Check for undo allowed here, because if something was already inserted
10397 * the line was already saved for undo and this check isn't done. */
10398 if (!undo_allowed())
10399 return;
10400
Bram Moolenaarade00832006-03-10 21:46:58 +000010401 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10402 {
10403 EMSG(_(e_invarg));
10404 return;
10405 }
10406
10407 startcol = get_tv_number_chk(&argvars[0], NULL);
10408 if (startcol <= 0)
10409 return;
10410
10411 set_completion(startcol - 1, argvars[1].vval.v_list);
10412}
10413
10414/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010415 * "complete_add()" function
10416 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010418f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010419{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010420 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010421}
10422
10423/*
10424 * "complete_check()" function
10425 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010427f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010428{
10429 int saved = RedrawingDisabled;
10430
10431 RedrawingDisabled = 0;
10432 ins_compl_check_keys(0);
10433 rettv->vval.v_number = compl_interrupted;
10434 RedrawingDisabled = saved;
10435}
10436#endif
10437
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438/*
10439 * "confirm(message, buttons[, default [, type]])" function
10440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010442f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443{
10444#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10445 char_u *message;
10446 char_u *buttons = NULL;
10447 char_u buf[NUMBUFLEN];
10448 char_u buf2[NUMBUFLEN];
10449 int def = 1;
10450 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010451 char_u *typestr;
10452 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010454 message = get_tv_string_chk(&argvars[0]);
10455 if (message == NULL)
10456 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010457 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010459 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10460 if (buttons == NULL)
10461 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010462 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010464 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010465 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010467 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10468 if (typestr == NULL)
10469 error = TRUE;
10470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010472 switch (TOUPPER_ASC(*typestr))
10473 {
10474 case 'E': type = VIM_ERROR; break;
10475 case 'Q': type = VIM_QUESTION; break;
10476 case 'I': type = VIM_INFO; break;
10477 case 'W': type = VIM_WARNING; break;
10478 case 'G': type = VIM_GENERIC; break;
10479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 }
10481 }
10482 }
10483 }
10484
10485 if (buttons == NULL || *buttons == NUL)
10486 buttons = (char_u *)_("&Ok");
10487
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010488 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010489 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010490 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491#endif
10492}
10493
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010494/*
10495 * "copy()" function
10496 */
10497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010498f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010499{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010500 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010501}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010503#ifdef FEAT_FLOAT
10504/*
10505 * "cos()" function
10506 */
10507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010508f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010509{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010510 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010511
10512 rettv->v_type = VAR_FLOAT;
10513 if (get_float_arg(argvars, &f) == OK)
10514 rettv->vval.v_float = cos(f);
10515 else
10516 rettv->vval.v_float = 0.0;
10517}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010518
10519/*
10520 * "cosh()" function
10521 */
10522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010523f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010524{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010525 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010526
10527 rettv->v_type = VAR_FLOAT;
10528 if (get_float_arg(argvars, &f) == OK)
10529 rettv->vval.v_float = cosh(f);
10530 else
10531 rettv->vval.v_float = 0.0;
10532}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010533#endif
10534
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010536 * "count()" function
10537 */
10538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010539f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010540{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010541 long n = 0;
10542 int ic = FALSE;
10543
Bram Moolenaare9a41262005-01-15 22:18:47 +000010544 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010545 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010546 listitem_T *li;
10547 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010548 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010549
Bram Moolenaare9a41262005-01-15 22:18:47 +000010550 if ((l = argvars[0].vval.v_list) != NULL)
10551 {
10552 li = l->lv_first;
10553 if (argvars[2].v_type != VAR_UNKNOWN)
10554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010555 int error = FALSE;
10556
10557 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010558 if (argvars[3].v_type != VAR_UNKNOWN)
10559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010560 idx = get_tv_number_chk(&argvars[3], &error);
10561 if (!error)
10562 {
10563 li = list_find(l, idx);
10564 if (li == NULL)
10565 EMSGN(_(e_listidx), idx);
10566 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010567 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 if (error)
10569 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 }
10571
10572 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010573 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 ++n;
10575 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010576 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010577 else if (argvars[0].v_type == VAR_DICT)
10578 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010579 int todo;
10580 dict_T *d;
10581 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010583 if ((d = argvars[0].vval.v_dict) != NULL)
10584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 int error = FALSE;
10586
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 if (argvars[2].v_type != VAR_UNKNOWN)
10588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010590 if (argvars[3].v_type != VAR_UNKNOWN)
10591 EMSG(_(e_invarg));
10592 }
10593
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010594 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010595 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010596 {
10597 if (!HASHITEM_EMPTY(hi))
10598 {
10599 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010600 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010601 ++n;
10602 }
10603 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010604 }
10605 }
10606 else
10607 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010608 rettv->vval.v_number = n;
10609}
10610
10611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10613 *
10614 * Checks the existence of a cscope connection.
10615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010617f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618{
10619#ifdef FEAT_CSCOPE
10620 int num = 0;
10621 char_u *dbpath = NULL;
10622 char_u *prepend = NULL;
10623 char_u buf[NUMBUFLEN];
10624
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010625 if (argvars[0].v_type != VAR_UNKNOWN
10626 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010628 num = (int)get_tv_number(&argvars[0]);
10629 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010630 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 }
10633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635#endif
10636}
10637
10638/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010639 * "cursor(lnum, col)" function, or
10640 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010642 * Moves the cursor to the specified line and column.
10643 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010646f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647{
10648 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010649#ifdef FEAT_VIRTUALEDIT
10650 long coladd = 0;
10651#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010652 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010654 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010655 if (argvars[1].v_type == VAR_UNKNOWN)
10656 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010657 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010658 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010659
Bram Moolenaar493c1782014-05-28 14:34:46 +020010660 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010661 {
10662 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010663 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010664 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010665 line = pos.lnum;
10666 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010667#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010668 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010669#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010670 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010671 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010672 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010673 set_curswant = FALSE;
10674 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010675 }
10676 else
10677 {
10678 line = get_tv_lnum(argvars);
10679 col = get_tv_number_chk(&argvars[1], NULL);
10680#ifdef FEAT_VIRTUALEDIT
10681 if (argvars[2].v_type != VAR_UNKNOWN)
10682 coladd = get_tv_number_chk(&argvars[2], NULL);
10683#endif
10684 }
10685 if (line < 0 || col < 0
10686#ifdef FEAT_VIRTUALEDIT
10687 || coladd < 0
10688#endif
10689 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010690 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691 if (line > 0)
10692 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 if (col > 0)
10694 curwin->w_cursor.col = col - 1;
10695#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010696 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697#endif
10698
10699 /* Make sure the cursor is in a valid position. */
10700 check_cursor();
10701#ifdef FEAT_MBYTE
10702 /* Correct cursor for multi-byte character. */
10703 if (has_mbyte)
10704 mb_adjust_cursor();
10705#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010706
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010707 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010708 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709}
10710
10711/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010712 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 */
10714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010715f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010717 int noref = 0;
10718
10719 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010720 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010721 if (noref < 0 || noref > 1)
10722 EMSG(_(e_invarg));
10723 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010724 {
10725 current_copyID += COPYID_INC;
10726 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728}
10729
10730/*
10731 * "delete()" function
10732 */
10733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010734f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010736 char_u nbuf[NUMBUFLEN];
10737 char_u *name;
10738 char_u *flags;
10739
10740 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010742 return;
10743
10744 name = get_tv_string(&argvars[0]);
10745 if (name == NULL || *name == NUL)
10746 {
10747 EMSG(_(e_invarg));
10748 return;
10749 }
10750
10751 if (argvars[1].v_type != VAR_UNKNOWN)
10752 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010754 flags = (char_u *)"";
10755
10756 if (*flags == NUL)
10757 /* delete a file */
10758 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10759 else if (STRCMP(flags, "d") == 0)
10760 /* delete an empty directory */
10761 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10762 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010763 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010764 rettv->vval.v_number = delete_recursive(name);
10765 else
10766 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767}
10768
10769/*
10770 * "did_filetype()" function
10771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010773f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774{
10775#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777#endif
10778}
10779
10780/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010781 * "diff_filler()" function
10782 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010784f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010785{
10786#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010788#endif
10789}
10790
10791/*
10792 * "diff_hlID()" function
10793 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010795f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010796{
10797#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010798 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010799 static linenr_T prev_lnum = 0;
10800 static int changedtick = 0;
10801 static int fnum = 0;
10802 static int change_start = 0;
10803 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010804 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010805 int filler_lines;
10806 int col;
10807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010808 if (lnum < 0) /* ignore type error in {lnum} arg */
10809 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010810 if (lnum != prev_lnum
10811 || changedtick != curbuf->b_changedtick
10812 || fnum != curbuf->b_fnum)
10813 {
10814 /* New line, buffer, change: need to get the values. */
10815 filler_lines = diff_check(curwin, lnum);
10816 if (filler_lines < 0)
10817 {
10818 if (filler_lines == -1)
10819 {
10820 change_start = MAXCOL;
10821 change_end = -1;
10822 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10823 hlID = HLF_ADD; /* added line */
10824 else
10825 hlID = HLF_CHD; /* changed line */
10826 }
10827 else
10828 hlID = HLF_ADD; /* added line */
10829 }
10830 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010831 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010832 prev_lnum = lnum;
10833 changedtick = curbuf->b_changedtick;
10834 fnum = curbuf->b_fnum;
10835 }
10836
10837 if (hlID == HLF_CHD || hlID == HLF_TXD)
10838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010839 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010840 if (col >= change_start && col <= change_end)
10841 hlID = HLF_TXD; /* changed text */
10842 else
10843 hlID = HLF_CHD; /* changed line */
10844 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010845 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010846#endif
10847}
10848
10849/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010850 * "disable_char_avail_for_testing({expr})" function
10851 */
10852 static void
10853f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10854{
10855 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10856}
10857
10858/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010859 * "empty({expr})" function
10860 */
10861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010862f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010863{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010864 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010865
10866 switch (argvars[0].v_type)
10867 {
10868 case VAR_STRING:
10869 case VAR_FUNC:
10870 n = argvars[0].vval.v_string == NULL
10871 || *argvars[0].vval.v_string == NUL;
10872 break;
10873 case VAR_NUMBER:
10874 n = argvars[0].vval.v_number == 0;
10875 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010876 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010877#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010878 n = argvars[0].vval.v_float == 0.0;
10879 break;
10880#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010881 case VAR_LIST:
10882 n = argvars[0].vval.v_list == NULL
10883 || argvars[0].vval.v_list->lv_first == NULL;
10884 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010885 case VAR_DICT:
10886 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010887 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010888 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010889 case VAR_SPECIAL:
10890 n = argvars[0].vval.v_number != VVAL_TRUE;
10891 break;
10892
Bram Moolenaar835dc632016-02-07 14:27:38 +010010893 case VAR_JOB:
10894#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010010895 n = argvars[0].vval.v_job == NULL
10896 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10897 break;
10898#endif
10899 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010010900#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010901 n = argvars[0].vval.v_channel == NULL
10902 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010903 break;
10904#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010905 case VAR_UNKNOWN:
10906 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10907 n = TRUE;
10908 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010909 }
10910
10911 rettv->vval.v_number = n;
10912}
10913
10914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 * "escape({string}, {chars})" function
10916 */
10917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010918f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919{
10920 char_u buf[NUMBUFLEN];
10921
Bram Moolenaar758711c2005-02-02 23:11:38 +000010922 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10923 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010924 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925}
10926
10927/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010928 * "eval()" function
10929 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010931f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010932{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010933 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010935 s = get_tv_string_chk(&argvars[0]);
10936 if (s != NULL)
10937 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010938
Bram Moolenaar615b9972015-01-14 17:15:05 +010010939 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10941 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010942 if (p != NULL && !aborting())
10943 EMSG2(_(e_invexpr2), p);
10944 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010945 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010946 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010947 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010948 else if (*s != NUL)
10949 EMSG(_(e_trailing));
10950}
10951
10952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 * "eventhandler()" function
10954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010956f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959}
10960
10961/*
10962 * "executable()" function
10963 */
10964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010965f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010967 char_u *name = get_tv_string(&argvars[0]);
10968
10969 /* Check in $PATH and also check directly if there is a directory name. */
10970 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10971 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010972}
10973
10974/*
10975 * "exepath()" function
10976 */
10977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010978f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010979{
10980 char_u *p = NULL;
10981
Bram Moolenaarb5971142015-03-21 17:32:19 +010010982 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010983 rettv->v_type = VAR_STRING;
10984 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985}
10986
10987/*
10988 * "exists()" function
10989 */
10990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010991f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992{
10993 char_u *p;
10994 char_u *name;
10995 int n = FALSE;
10996 int len = 0;
10997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 if (*p == '$') /* environment variable */
11000 {
11001 /* first try "normal" environment variables (fast) */
11002 if (mch_getenv(p + 1) != NULL)
11003 n = TRUE;
11004 else
11005 {
11006 /* try expanding things like $VIM and ${HOME} */
11007 p = expand_env_save(p);
11008 if (p != NULL && *p != '$')
11009 n = TRUE;
11010 vim_free(p);
11011 }
11012 }
11013 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011016 if (*skipwhite(p) != NUL)
11017 n = FALSE; /* trailing garbage */
11018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 else if (*p == '*') /* internal or user defined function */
11020 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011021 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 }
11023 else if (*p == ':')
11024 {
11025 n = cmd_exists(p + 1);
11026 }
11027 else if (*p == '#')
11028 {
11029#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011030 if (p[1] == '#')
11031 n = autocmd_supported(p + 2);
11032 else
11033 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034#endif
11035 }
11036 else /* internal variable */
11037 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011038 char_u *tofree;
11039 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011041 /* get_name_len() takes care of expanding curly braces */
11042 name = p;
11043 len = get_name_len(&p, &tofree, TRUE, FALSE);
11044 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011046 if (tofree != NULL)
11047 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011048 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011049 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011051 /* handle d.key, l[idx], f(expr) */
11052 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11053 if (n)
11054 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055 }
11056 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011057 if (*p != NUL)
11058 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011060 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061 }
11062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064}
11065
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011066#ifdef FEAT_FLOAT
11067/*
11068 * "exp()" function
11069 */
11070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011071f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011072{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011073 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011074
11075 rettv->v_type = VAR_FLOAT;
11076 if (get_float_arg(argvars, &f) == OK)
11077 rettv->vval.v_float = exp(f);
11078 else
11079 rettv->vval.v_float = 0.0;
11080}
11081#endif
11082
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083/*
11084 * "expand()" function
11085 */
11086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011087f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088{
11089 char_u *s;
11090 int len;
11091 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011092 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011094 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011095 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011097 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011098 if (argvars[1].v_type != VAR_UNKNOWN
11099 && argvars[2].v_type != VAR_UNKNOWN
11100 && get_tv_number_chk(&argvars[2], &error)
11101 && !error)
11102 {
11103 rettv->v_type = VAR_LIST;
11104 rettv->vval.v_list = NULL;
11105 }
11106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011107 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 if (*s == '%' || *s == '#' || *s == '<')
11109 {
11110 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011111 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011113 if (rettv->v_type == VAR_LIST)
11114 {
11115 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11116 list_append_string(rettv->vval.v_list, result, -1);
11117 else
11118 vim_free(result);
11119 }
11120 else
11121 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 }
11123 else
11124 {
11125 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011126 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011127 if (argvars[1].v_type != VAR_UNKNOWN
11128 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011129 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011130 if (!error)
11131 {
11132 ExpandInit(&xpc);
11133 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011134 if (p_wic)
11135 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011136 if (rettv->v_type == VAR_STRING)
11137 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11138 options, WILD_ALL);
11139 else if (rettv_list_alloc(rettv) != FAIL)
11140 {
11141 int i;
11142
11143 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11144 for (i = 0; i < xpc.xp_numfiles; i++)
11145 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11146 ExpandCleanup(&xpc);
11147 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 }
11149 else
11150 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151 }
11152}
11153
11154/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011155 * Go over all entries in "d2" and add them to "d1".
11156 * When "action" is "error" then a duplicate key is an error.
11157 * When "action" is "force" then a duplicate key is overwritten.
11158 * Otherwise duplicate keys are ignored ("action" is "keep").
11159 */
11160 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011161dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011162{
11163 dictitem_T *di1;
11164 hashitem_T *hi2;
11165 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011166 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011167
11168 todo = (int)d2->dv_hashtab.ht_used;
11169 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11170 {
11171 if (!HASHITEM_EMPTY(hi2))
11172 {
11173 --todo;
11174 di1 = dict_find(d1, hi2->hi_key, -1);
11175 if (d1->dv_scope != 0)
11176 {
11177 /* Disallow replacing a builtin function in l: and g:.
11178 * Check the key to be valid when adding to any
11179 * scope. */
11180 if (d1->dv_scope == VAR_DEF_SCOPE
11181 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11182 && var_check_func_name(hi2->hi_key,
11183 di1 == NULL))
11184 break;
11185 if (!valid_varname(hi2->hi_key))
11186 break;
11187 }
11188 if (di1 == NULL)
11189 {
11190 di1 = dictitem_copy(HI2DI(hi2));
11191 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11192 dictitem_free(di1);
11193 }
11194 else if (*action == 'e')
11195 {
11196 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11197 break;
11198 }
11199 else if (*action == 'f' && HI2DI(hi2) != di1)
11200 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011201 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11202 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011203 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011204 clear_tv(&di1->di_tv);
11205 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11206 }
11207 }
11208 }
11209}
11210
11211/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011212 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011213 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011214 */
11215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011216f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011217{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011218 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011219
Bram Moolenaare9a41262005-01-15 22:18:47 +000011220 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011221 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011222 list_T *l1, *l2;
11223 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011224 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011226
Bram Moolenaare9a41262005-01-15 22:18:47 +000011227 l1 = argvars[0].vval.v_list;
11228 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011229 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011230 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231 {
11232 if (argvars[2].v_type != VAR_UNKNOWN)
11233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011234 before = get_tv_number_chk(&argvars[2], &error);
11235 if (error)
11236 return; /* type error; errmsg already given */
11237
Bram Moolenaar758711c2005-02-02 23:11:38 +000011238 if (before == l1->lv_len)
11239 item = NULL;
11240 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011241 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011242 item = list_find(l1, before);
11243 if (item == NULL)
11244 {
11245 EMSGN(_(e_listidx), before);
11246 return;
11247 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011248 }
11249 }
11250 else
11251 item = NULL;
11252 list_extend(l1, l2, item);
11253
Bram Moolenaare9a41262005-01-15 22:18:47 +000011254 copy_tv(&argvars[0], rettv);
11255 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011257 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11258 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011259 dict_T *d1, *d2;
11260 char_u *action;
11261 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011262
11263 d1 = argvars[0].vval.v_dict;
11264 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011265 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011266 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011267 {
11268 /* Check the third argument. */
11269 if (argvars[2].v_type != VAR_UNKNOWN)
11270 {
11271 static char *(av[]) = {"keep", "force", "error"};
11272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011273 action = get_tv_string_chk(&argvars[2]);
11274 if (action == NULL)
11275 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011276 for (i = 0; i < 3; ++i)
11277 if (STRCMP(action, av[i]) == 0)
11278 break;
11279 if (i == 3)
11280 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011281 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011282 return;
11283 }
11284 }
11285 else
11286 action = (char_u *)"force";
11287
Bram Moolenaara9922d62013-05-30 13:01:18 +020011288 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011289
Bram Moolenaare9a41262005-01-15 22:18:47 +000011290 copy_tv(&argvars[0], rettv);
11291 }
11292 }
11293 else
11294 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011295}
11296
11297/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011298 * "feedkeys()" function
11299 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011301f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011302{
11303 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011304 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011305 char_u *keys, *flags;
11306 char_u nbuf[NUMBUFLEN];
11307 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011308 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011309 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011310
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011311 /* This is not allowed in the sandbox. If the commands would still be
11312 * executed in the sandbox it would be OK, but it probably happens later,
11313 * when "sandbox" is no longer set. */
11314 if (check_secure())
11315 return;
11316
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011317 keys = get_tv_string(&argvars[0]);
11318 if (*keys != NUL)
11319 {
11320 if (argvars[1].v_type != VAR_UNKNOWN)
11321 {
11322 flags = get_tv_string_buf(&argvars[1], nbuf);
11323 for ( ; *flags != NUL; ++flags)
11324 {
11325 switch (*flags)
11326 {
11327 case 'n': remap = FALSE; break;
11328 case 'm': remap = TRUE; break;
11329 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011330 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011331 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011332 }
11333 }
11334 }
11335
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011336 /* Need to escape K_SPECIAL and CSI before putting the string in the
11337 * typeahead buffer. */
11338 keys_esc = vim_strsave_escape_csi(keys);
11339 if (keys_esc != NULL)
11340 {
11341 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011342 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011343 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011344 if (vgetc_busy)
11345 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011346 if (execute)
11347 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011348 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011349 }
11350}
11351
11352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 * "filereadable()" function
11354 */
11355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011356f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011358 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 char_u *p;
11360 int n;
11361
Bram Moolenaarc236c162008-07-13 17:41:49 +000011362#ifndef O_NONBLOCK
11363# define O_NONBLOCK 0
11364#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011365 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011366 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11367 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 {
11369 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011370 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 }
11372 else
11373 n = FALSE;
11374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376}
11377
11378/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011379 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 * rights to write into.
11381 */
11382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011383f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011385 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386}
11387
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011388 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011389findfilendir(
11390 typval_T *argvars UNUSED,
11391 typval_T *rettv,
11392 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011393{
11394#ifdef FEAT_SEARCHPATH
11395 char_u *fname;
11396 char_u *fresult = NULL;
11397 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11398 char_u *p;
11399 char_u pathbuf[NUMBUFLEN];
11400 int count = 1;
11401 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011402 int error = FALSE;
11403#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011404
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011405 rettv->vval.v_string = NULL;
11406 rettv->v_type = VAR_STRING;
11407
11408#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011410
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011411 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011413 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11414 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011415 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 else
11417 {
11418 if (*p != NUL)
11419 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011422 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011423 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011424 }
11425
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011426 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11427 error = TRUE;
11428
11429 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431 do
11432 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011433 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011434 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 fresult = find_file_in_path_option(first ? fname : NULL,
11436 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011437 0, first, path,
11438 find_what,
11439 curbuf->b_ffname,
11440 find_what == FINDFILE_DIR
11441 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011442 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011443
11444 if (fresult != NULL && rettv->v_type == VAR_LIST)
11445 list_append_string(rettv->vval.v_list, fresult, -1);
11446
11447 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011449
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011450 if (rettv->v_type == VAR_STRING)
11451 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011452#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011453}
11454
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011455static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11456static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011457
11458/*
11459 * Implementation of map() and filter().
11460 */
11461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011462filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011463{
11464 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011465 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 listitem_T *li, *nli;
11467 list_T *l = NULL;
11468 dictitem_T *di;
11469 hashtab_T *ht;
11470 hashitem_T *hi;
11471 dict_T *d = NULL;
11472 typval_T save_val;
11473 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011474 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011475 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011476 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011477 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011478 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011479 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011480 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011481
Bram Moolenaare9a41262005-01-15 22:18:47 +000011482 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011483 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011484 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011485 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011486 return;
11487 }
11488 else if (argvars[0].v_type == VAR_DICT)
11489 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011490 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011491 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011492 return;
11493 }
11494 else
11495 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011496 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 return;
11498 }
11499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 expr = get_tv_string_buf_chk(&argvars[1], buf);
11501 /* On type errors, the preceding call has already displayed an error
11502 * message. Avoid a misleading error message for an empty string that
11503 * was not passed as argument. */
11504 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 prepare_vimvar(VV_VAL, &save_val);
11507 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011508
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011509 /* We reset "did_emsg" to be able to detect whether an error
11510 * occurred during evaluation of the expression. */
11511 save_did_emsg = did_emsg;
11512 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011513
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011514 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011515 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 vimvars[VV_KEY].vv_type = VAR_STRING;
11518
11519 ht = &d->dv_hashtab;
11520 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011521 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011522 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011524 if (!HASHITEM_EMPTY(hi))
11525 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011526 int r;
11527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011528 --todo;
11529 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011530 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011531 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11532 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011533 break;
11534 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011535 r = filter_map_one(&di->di_tv, expr, map, &rem);
11536 clear_tv(&vimvars[VV_KEY].vv_tv);
11537 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011538 break;
11539 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011540 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011541 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11542 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011543 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011544 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011546 }
11547 }
11548 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011549 }
11550 else
11551 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011552 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 for (li = l->lv_first; li != NULL; li = nli)
11555 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011556 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011557 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011558 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011559 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011560 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011561 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011562 break;
11563 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011565 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011566 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011567 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011568
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011569 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011570 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011571
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011572 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011573 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011574
11575 copy_tv(&argvars[0], rettv);
11576}
11577
11578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011579filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011580{
Bram Moolenaar33570922005-01-25 22:26:29 +000011581 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011582 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011583 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011584
Bram Moolenaar33570922005-01-25 22:26:29 +000011585 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011586 s = expr;
11587 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011588 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011589 if (*s != NUL) /* check for trailing chars after expr */
11590 {
11591 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011592 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011593 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011594 }
11595 if (map)
11596 {
11597 /* map(): replace the list item value */
11598 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011599 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011600 *tv = rettv;
11601 }
11602 else
11603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011604 int error = FALSE;
11605
Bram Moolenaare9a41262005-01-15 22:18:47 +000011606 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011607 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011608 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011609 /* On type error, nothing has been removed; return FAIL to stop the
11610 * loop. The error message was given by get_tv_number_chk(). */
11611 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011612 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011613 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011614 retval = OK;
11615theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011616 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011617 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011618}
11619
11620/*
11621 * "filter()" function
11622 */
11623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011624f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011625{
11626 filter_map(argvars, rettv, FALSE);
11627}
11628
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011629/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011630 * "finddir({fname}[, {path}[, {count}]])" function
11631 */
11632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011633f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011634{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011635 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011636}
11637
11638/*
11639 * "findfile({fname}[, {path}[, {count}]])" function
11640 */
11641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011642f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011643{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011644 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645}
11646
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011647#ifdef FEAT_FLOAT
11648/*
11649 * "float2nr({float})" function
11650 */
11651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011652f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011653{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011654 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011655
11656 if (get_float_arg(argvars, &f) == OK)
11657 {
11658 if (f < -0x7fffffff)
11659 rettv->vval.v_number = -0x7fffffff;
11660 else if (f > 0x7fffffff)
11661 rettv->vval.v_number = 0x7fffffff;
11662 else
11663 rettv->vval.v_number = (varnumber_T)f;
11664 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011665}
11666
11667/*
11668 * "floor({float})" function
11669 */
11670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011671f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011672{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011673 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011674
11675 rettv->v_type = VAR_FLOAT;
11676 if (get_float_arg(argvars, &f) == OK)
11677 rettv->vval.v_float = floor(f);
11678 else
11679 rettv->vval.v_float = 0.0;
11680}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011681
11682/*
11683 * "fmod()" function
11684 */
11685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011686f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011687{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011688 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011689
11690 rettv->v_type = VAR_FLOAT;
11691 if (get_float_arg(argvars, &fx) == OK
11692 && get_float_arg(&argvars[1], &fy) == OK)
11693 rettv->vval.v_float = fmod(fx, fy);
11694 else
11695 rettv->vval.v_float = 0.0;
11696}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011697#endif
11698
Bram Moolenaar0d660222005-01-07 21:51:51 +000011699/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011700 * "fnameescape({string})" function
11701 */
11702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011703f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011704{
11705 rettv->vval.v_string = vim_strsave_fnameescape(
11706 get_tv_string(&argvars[0]), FALSE);
11707 rettv->v_type = VAR_STRING;
11708}
11709
11710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 * "fnamemodify({fname}, {mods})" function
11712 */
11713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011714f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715{
11716 char_u *fname;
11717 char_u *mods;
11718 int usedlen = 0;
11719 int len;
11720 char_u *fbuf = NULL;
11721 char_u buf[NUMBUFLEN];
11722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011723 fname = get_tv_string_chk(&argvars[0]);
11724 mods = get_tv_string_buf_chk(&argvars[1], buf);
11725 if (fname == NULL || mods == NULL)
11726 fname = NULL;
11727 else
11728 {
11729 len = (int)STRLEN(fname);
11730 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011733 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 vim_free(fbuf);
11739}
11740
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011741static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742
11743/*
11744 * "foldclosed()" function
11745 */
11746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011747foldclosed_both(
11748 typval_T *argvars UNUSED,
11749 typval_T *rettv,
11750 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751{
11752#ifdef FEAT_FOLDING
11753 linenr_T lnum;
11754 linenr_T first, last;
11755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011756 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11758 {
11759 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11760 {
11761 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011764 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 return;
11766 }
11767 }
11768#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770}
11771
11772/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011773 * "foldclosed()" function
11774 */
11775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011776f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011777{
11778 foldclosed_both(argvars, rettv, FALSE);
11779}
11780
11781/*
11782 * "foldclosedend()" function
11783 */
11784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011785f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011786{
11787 foldclosed_both(argvars, rettv, TRUE);
11788}
11789
11790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 * "foldlevel()" function
11792 */
11793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011794f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795{
11796#ifdef FEAT_FOLDING
11797 linenr_T lnum;
11798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011799 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803}
11804
11805/*
11806 * "foldtext()" function
11807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011809f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810{
11811#ifdef FEAT_FOLDING
11812 linenr_T lnum;
11813 char_u *s;
11814 char_u *r;
11815 int len;
11816 char *txt;
11817#endif
11818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011819 rettv->v_type = VAR_STRING;
11820 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011822 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11823 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11824 <= curbuf->b_ml.ml_line_count
11825 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 {
11827 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011828 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11829 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 {
11831 if (!linewhite(lnum))
11832 break;
11833 ++lnum;
11834 }
11835
11836 /* Find interesting text in this line. */
11837 s = skipwhite(ml_get(lnum));
11838 /* skip C comment-start */
11839 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011840 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011842 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011844 {
11845 s = skipwhite(ml_get(lnum + 1));
11846 if (*s == '*')
11847 s = skipwhite(s + 1);
11848 }
11849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 txt = _("+-%s%3ld lines: ");
11851 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011852 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853 + 20 /* for %3ld */
11854 + STRLEN(s))); /* concatenated */
11855 if (r != NULL)
11856 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011857 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11858 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11859 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860 len = (int)STRLEN(r);
11861 STRCAT(r, s);
11862 /* remove 'foldmarker' and 'commentstring' */
11863 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 }
11866 }
11867#endif
11868}
11869
11870/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011871 * "foldtextresult(lnum)" function
11872 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011874f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011875{
11876#ifdef FEAT_FOLDING
11877 linenr_T lnum;
11878 char_u *text;
11879 char_u buf[51];
11880 foldinfo_T foldinfo;
11881 int fold_count;
11882#endif
11883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011884 rettv->v_type = VAR_STRING;
11885 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011886#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011887 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011888 /* treat illegal types and illegal string values for {lnum} the same */
11889 if (lnum < 0)
11890 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011891 fold_count = foldedCount(curwin, lnum, &foldinfo);
11892 if (fold_count > 0)
11893 {
11894 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11895 &foldinfo, buf);
11896 if (text == buf)
11897 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011899 }
11900#endif
11901}
11902
11903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011904 * "foreground()" function
11905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011907f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011908{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909#ifdef FEAT_GUI
11910 if (gui.in_use)
11911 gui_mch_set_foreground();
11912#else
11913# ifdef WIN32
11914 win32_set_foreground();
11915# endif
11916#endif
11917}
11918
11919/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011920 * "function()" function
11921 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011923f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011924{
11925 char_u *s;
11926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011928 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011929 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011930 /* Don't check an autoload name for existence here. */
11931 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011932 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011933 else
11934 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011935 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011936 {
11937 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011938 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011939
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011940 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11941 * also be called from another script. Using trans_function_name()
11942 * would also work, but some plugins depend on the name being
11943 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011944 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011945 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011946 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011947 if (rettv->vval.v_string != NULL)
11948 {
11949 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011950 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011951 }
11952 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011953 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011954 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011956 }
11957}
11958
11959/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011960 * "garbagecollect()" function
11961 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011963f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011964{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011965 /* This is postponed until we are back at the toplevel, because we may be
11966 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11967 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011968
11969 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11970 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011971}
11972
11973/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011974 * "get()" function
11975 */
11976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011977f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011978{
Bram Moolenaar33570922005-01-25 22:26:29 +000011979 listitem_T *li;
11980 list_T *l;
11981 dictitem_T *di;
11982 dict_T *d;
11983 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011984
Bram Moolenaare9a41262005-01-15 22:18:47 +000011985 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011986 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011987 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011989 int error = FALSE;
11990
11991 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11992 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011993 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011994 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011995 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011996 else if (argvars[0].v_type == VAR_DICT)
11997 {
11998 if ((d = argvars[0].vval.v_dict) != NULL)
11999 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012000 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012001 if (di != NULL)
12002 tv = &di->di_tv;
12003 }
12004 }
12005 else
12006 EMSG2(_(e_listdictarg), "get()");
12007
12008 if (tv == NULL)
12009 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012010 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012011 copy_tv(&argvars[2], rettv);
12012 }
12013 else
12014 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012015}
12016
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012017static 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 +000012018
12019/*
12020 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012021 * Return a range (from start to end) of lines in rettv from the specified
12022 * buffer.
12023 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012024 */
12025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012026get_buffer_lines(
12027 buf_T *buf,
12028 linenr_T start,
12029 linenr_T end,
12030 int retlist,
12031 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012032{
12033 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012034
Bram Moolenaar959a1432013-12-14 12:17:38 +010012035 rettv->v_type = VAR_STRING;
12036 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012037 if (retlist && rettv_list_alloc(rettv) == FAIL)
12038 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012039
12040 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12041 return;
12042
12043 if (!retlist)
12044 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012045 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12046 p = ml_get_buf(buf, start, FALSE);
12047 else
12048 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012049 rettv->vval.v_string = vim_strsave(p);
12050 }
12051 else
12052 {
12053 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012054 return;
12055
12056 if (start < 1)
12057 start = 1;
12058 if (end > buf->b_ml.ml_line_count)
12059 end = buf->b_ml.ml_line_count;
12060 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012061 if (list_append_string(rettv->vval.v_list,
12062 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012063 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012064 }
12065}
12066
12067/*
12068 * "getbufline()" function
12069 */
12070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012071f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012072{
12073 linenr_T lnum;
12074 linenr_T end;
12075 buf_T *buf;
12076
12077 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12078 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012079 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012080 --emsg_off;
12081
Bram Moolenaar661b1822005-07-28 22:36:45 +000012082 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012083 if (argvars[2].v_type == VAR_UNKNOWN)
12084 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012085 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012086 end = get_tv_lnum_buf(&argvars[2], buf);
12087
Bram Moolenaar342337a2005-07-21 21:11:17 +000012088 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012089}
12090
Bram Moolenaar0d660222005-01-07 21:51:51 +000012091/*
12092 * "getbufvar()" function
12093 */
12094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012095f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012096{
12097 buf_T *buf;
12098 buf_T *save_curbuf;
12099 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012100 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012101 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012103 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12104 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012105 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012106 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012107
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012108 rettv->v_type = VAR_STRING;
12109 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012110
12111 if (buf != NULL && varname != NULL)
12112 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012113 /* set curbuf to be our buf, temporarily */
12114 save_curbuf = curbuf;
12115 curbuf = buf;
12116
Bram Moolenaar0d660222005-01-07 21:51:51 +000012117 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012118 {
12119 if (get_option_tv(&varname, rettv, TRUE) == OK)
12120 done = TRUE;
12121 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012122 else if (STRCMP(varname, "changedtick") == 0)
12123 {
12124 rettv->v_type = VAR_NUMBER;
12125 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012126 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012127 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012128 else
12129 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012130 /* Look up the variable. */
12131 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12132 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12133 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012135 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012136 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012137 done = TRUE;
12138 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012139 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012140
12141 /* restore previous notion of curbuf */
12142 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143 }
12144
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012145 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12146 /* use the default value */
12147 copy_tv(&argvars[2], rettv);
12148
Bram Moolenaar0d660222005-01-07 21:51:51 +000012149 --emsg_off;
12150}
12151
12152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153 * "getchar()" function
12154 */
12155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012156f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157{
12158 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012159 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012161 /* Position the cursor. Needed after a message that ends in a space. */
12162 windgoto(msg_row, msg_col);
12163
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164 ++no_mapping;
12165 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012166 for (;;)
12167 {
12168 if (argvars[0].v_type == VAR_UNKNOWN)
12169 /* getchar(): blocking wait. */
12170 n = safe_vgetc();
12171 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12172 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012173 n = vpeekc_any();
12174 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012175 /* illegal argument or getchar(0) and no char avail: return zero */
12176 n = 0;
12177 else
12178 /* getchar(0) and char avail: return char */
12179 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012180
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012181 if (n == K_IGNORE)
12182 continue;
12183 break;
12184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185 --no_mapping;
12186 --allow_keys;
12187
Bram Moolenaar219b8702006-11-01 14:32:36 +000012188 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12189 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12190 vimvars[VV_MOUSE_COL].vv_nr = 0;
12191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 if (IS_SPECIAL(n) || mod_mask != 0)
12194 {
12195 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12196 int i = 0;
12197
12198 /* Turn a special key into three bytes, plus modifier. */
12199 if (mod_mask != 0)
12200 {
12201 temp[i++] = K_SPECIAL;
12202 temp[i++] = KS_MODIFIER;
12203 temp[i++] = mod_mask;
12204 }
12205 if (IS_SPECIAL(n))
12206 {
12207 temp[i++] = K_SPECIAL;
12208 temp[i++] = K_SECOND(n);
12209 temp[i++] = K_THIRD(n);
12210 }
12211#ifdef FEAT_MBYTE
12212 else if (has_mbyte)
12213 i += (*mb_char2bytes)(n, temp + i);
12214#endif
12215 else
12216 temp[i++] = n;
12217 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012218 rettv->v_type = VAR_STRING;
12219 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012220
12221#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012222 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012223 {
12224 int row = mouse_row;
12225 int col = mouse_col;
12226 win_T *win;
12227 linenr_T lnum;
12228# ifdef FEAT_WINDOWS
12229 win_T *wp;
12230# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012231 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012232
12233 if (row >= 0 && col >= 0)
12234 {
12235 /* Find the window at the mouse coordinates and compute the
12236 * text position. */
12237 win = mouse_find_win(&row, &col);
12238 (void)mouse_comp_pos(win, &row, &col, &lnum);
12239# ifdef FEAT_WINDOWS
12240 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012241 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012242# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012243 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012244 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12245 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12246 }
12247 }
12248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249 }
12250}
12251
12252/*
12253 * "getcharmod()" function
12254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012256f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259}
12260
12261/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012262 * "getcharsearch()" function
12263 */
12264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012265f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012266{
12267 if (rettv_dict_alloc(rettv) != FAIL)
12268 {
12269 dict_T *dict = rettv->vval.v_dict;
12270
12271 dict_add_nr_str(dict, "char", 0L, last_csearch());
12272 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12273 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12274 }
12275}
12276
12277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 * "getcmdline()" function
12279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012281f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283 rettv->v_type = VAR_STRING;
12284 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012285}
12286
12287/*
12288 * "getcmdpos()" function
12289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012291f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012293 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294}
12295
12296/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012297 * "getcmdtype()" function
12298 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012300f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012301{
12302 rettv->v_type = VAR_STRING;
12303 rettv->vval.v_string = alloc(2);
12304 if (rettv->vval.v_string != NULL)
12305 {
12306 rettv->vval.v_string[0] = get_cmdline_type();
12307 rettv->vval.v_string[1] = NUL;
12308 }
12309}
12310
12311/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012312 * "getcmdwintype()" function
12313 */
12314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012315f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012316{
12317 rettv->v_type = VAR_STRING;
12318 rettv->vval.v_string = NULL;
12319#ifdef FEAT_CMDWIN
12320 rettv->vval.v_string = alloc(2);
12321 if (rettv->vval.v_string != NULL)
12322 {
12323 rettv->vval.v_string[0] = cmdwin_type;
12324 rettv->vval.v_string[1] = NUL;
12325 }
12326#endif
12327}
12328
12329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 * "getcwd()" function
12331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012333f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012335 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012336 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012339 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012340
12341 wp = find_tabwin(&argvars[0], &argvars[1]);
12342 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012344 if (wp->w_localdir != NULL)
12345 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12346 else if(globaldir != NULL)
12347 rettv->vval.v_string = vim_strsave(globaldir);
12348 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012349 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012350 cwd = alloc(MAXPATHL);
12351 if (cwd != NULL)
12352 {
12353 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12354 rettv->vval.v_string = vim_strsave(cwd);
12355 vim_free(cwd);
12356 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012357 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012358#ifdef BACKSLASH_IN_FILENAME
12359 if (rettv->vval.v_string != NULL)
12360 slash_adjust(rettv->vval.v_string);
12361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 }
12363}
12364
12365/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012366 * "getfontname()" function
12367 */
12368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012369f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012370{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 rettv->v_type = VAR_STRING;
12372 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012373#ifdef FEAT_GUI
12374 if (gui.in_use)
12375 {
12376 GuiFont font;
12377 char_u *name = NULL;
12378
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012379 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012380 {
12381 /* Get the "Normal" font. Either the name saved by
12382 * hl_set_font_name() or from the font ID. */
12383 font = gui.norm_font;
12384 name = hl_get_font_name();
12385 }
12386 else
12387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012389 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12390 return;
12391 font = gui_mch_get_font(name, FALSE);
12392 if (font == NOFONT)
12393 return; /* Invalid font name, return empty string. */
12394 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012395 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012396 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012397 gui_mch_free_font(font);
12398 }
12399#endif
12400}
12401
12402/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012403 * "getfperm({fname})" function
12404 */
12405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012406f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012407{
12408 char_u *fname;
12409 struct stat st;
12410 char_u *perm = NULL;
12411 char_u flags[] = "rwx";
12412 int i;
12413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012414 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012416 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012417 if (mch_stat((char *)fname, &st) >= 0)
12418 {
12419 perm = vim_strsave((char_u *)"---------");
12420 if (perm != NULL)
12421 {
12422 for (i = 0; i < 9; i++)
12423 {
12424 if (st.st_mode & (1 << (8 - i)))
12425 perm[i] = flags[i % 3];
12426 }
12427 }
12428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012429 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012430}
12431
12432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 * "getfsize({fname})" function
12434 */
12435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012436f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437{
12438 char_u *fname;
12439 struct stat st;
12440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012443 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444
12445 if (mch_stat((char *)fname, &st) >= 0)
12446 {
12447 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012448 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012451 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012452
12453 /* non-perfect check for overflow */
12454 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12455 rettv->vval.v_number = -2;
12456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 }
12458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012459 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460}
12461
12462/*
12463 * "getftime({fname})" function
12464 */
12465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012466f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467{
12468 char_u *fname;
12469 struct stat st;
12470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012471 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472
12473 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012476 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477}
12478
12479/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012480 * "getftype({fname})" function
12481 */
12482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012483f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012484{
12485 char_u *fname;
12486 struct stat st;
12487 char_u *type = NULL;
12488 char *t;
12489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012492 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012493 if (mch_lstat((char *)fname, &st) >= 0)
12494 {
12495#ifdef S_ISREG
12496 if (S_ISREG(st.st_mode))
12497 t = "file";
12498 else if (S_ISDIR(st.st_mode))
12499 t = "dir";
12500# ifdef S_ISLNK
12501 else if (S_ISLNK(st.st_mode))
12502 t = "link";
12503# endif
12504# ifdef S_ISBLK
12505 else if (S_ISBLK(st.st_mode))
12506 t = "bdev";
12507# endif
12508# ifdef S_ISCHR
12509 else if (S_ISCHR(st.st_mode))
12510 t = "cdev";
12511# endif
12512# ifdef S_ISFIFO
12513 else if (S_ISFIFO(st.st_mode))
12514 t = "fifo";
12515# endif
12516# ifdef S_ISSOCK
12517 else if (S_ISSOCK(st.st_mode))
12518 t = "fifo";
12519# endif
12520 else
12521 t = "other";
12522#else
12523# ifdef S_IFMT
12524 switch (st.st_mode & S_IFMT)
12525 {
12526 case S_IFREG: t = "file"; break;
12527 case S_IFDIR: t = "dir"; break;
12528# ifdef S_IFLNK
12529 case S_IFLNK: t = "link"; break;
12530# endif
12531# ifdef S_IFBLK
12532 case S_IFBLK: t = "bdev"; break;
12533# endif
12534# ifdef S_IFCHR
12535 case S_IFCHR: t = "cdev"; break;
12536# endif
12537# ifdef S_IFIFO
12538 case S_IFIFO: t = "fifo"; break;
12539# endif
12540# ifdef S_IFSOCK
12541 case S_IFSOCK: t = "socket"; break;
12542# endif
12543 default: t = "other";
12544 }
12545# else
12546 if (mch_isdir(fname))
12547 t = "dir";
12548 else
12549 t = "file";
12550# endif
12551#endif
12552 type = vim_strsave((char_u *)t);
12553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012554 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012555}
12556
12557/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012558 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012559 */
12560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012561f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012562{
12563 linenr_T lnum;
12564 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012565 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566
12567 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012568 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012569 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012570 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012571 retlist = FALSE;
12572 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012573 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012574 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012575 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012576 retlist = TRUE;
12577 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012578
Bram Moolenaar342337a2005-07-21 21:11:17 +000012579 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012580}
12581
12582/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012583 * "getmatches()" function
12584 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012586f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012587{
12588#ifdef FEAT_SEARCH_EXTRA
12589 dict_T *dict;
12590 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012591 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012592
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012593 if (rettv_list_alloc(rettv) == OK)
12594 {
12595 while (cur != NULL)
12596 {
12597 dict = dict_alloc();
12598 if (dict == NULL)
12599 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012600 if (cur->match.regprog == NULL)
12601 {
12602 /* match added with matchaddpos() */
12603 for (i = 0; i < MAXPOSMATCH; ++i)
12604 {
12605 llpos_T *llpos;
12606 char buf[6];
12607 list_T *l;
12608
12609 llpos = &cur->pos.pos[i];
12610 if (llpos->lnum == 0)
12611 break;
12612 l = list_alloc();
12613 if (l == NULL)
12614 break;
12615 list_append_number(l, (varnumber_T)llpos->lnum);
12616 if (llpos->col > 0)
12617 {
12618 list_append_number(l, (varnumber_T)llpos->col);
12619 list_append_number(l, (varnumber_T)llpos->len);
12620 }
12621 sprintf(buf, "pos%d", i + 1);
12622 dict_add_list(dict, buf, l);
12623 }
12624 }
12625 else
12626 {
12627 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12628 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012629 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012630 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12631 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012632# ifdef FEAT_CONCEAL
12633 if (cur->conceal_char)
12634 {
12635 char_u buf[MB_MAXBYTES + 1];
12636
12637 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12638 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12639 }
12640# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012641 list_append_dict(rettv->vval.v_list, dict);
12642 cur = cur->next;
12643 }
12644 }
12645#endif
12646}
12647
12648/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012649 * "getpid()" function
12650 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012652f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012653{
12654 rettv->vval.v_number = mch_get_pid();
12655}
12656
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012657static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012658
12659/*
12660 * "getcurpos()" function
12661 */
12662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012663f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012664{
12665 getpos_both(argvars, rettv, TRUE);
12666}
12667
Bram Moolenaar18081e32008-02-20 19:11:07 +000012668/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012669 * "getpos(string)" function
12670 */
12671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012672f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012673{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012674 getpos_both(argvars, rettv, FALSE);
12675}
12676
12677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012678getpos_both(
12679 typval_T *argvars,
12680 typval_T *rettv,
12681 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012682{
Bram Moolenaara5525202006-03-02 22:52:09 +000012683 pos_T *fp;
12684 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012685 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012686
12687 if (rettv_list_alloc(rettv) == OK)
12688 {
12689 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012690 if (getcurpos)
12691 fp = &curwin->w_cursor;
12692 else
12693 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012694 if (fnum != -1)
12695 list_append_number(l, (varnumber_T)fnum);
12696 else
12697 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012698 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12699 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012700 list_append_number(l, (fp != NULL)
12701 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012702 : (varnumber_T)0);
12703 list_append_number(l,
12704#ifdef FEAT_VIRTUALEDIT
12705 (fp != NULL) ? (varnumber_T)fp->coladd :
12706#endif
12707 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012708 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012709 {
12710 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012711 list_append_number(l, curwin->w_curswant == MAXCOL ?
12712 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012713 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012714 }
12715 else
12716 rettv->vval.v_number = FALSE;
12717}
12718
12719/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012720 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012721 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012723f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012724{
12725#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012726 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012727#endif
12728
Bram Moolenaar2641f772005-03-25 21:58:17 +000012729#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012730 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012731 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012732 wp = NULL;
12733 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12734 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012735 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012736 if (wp == NULL)
12737 return;
12738 }
12739
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012740 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012741 }
12742#endif
12743}
12744
12745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 * "getreg()" function
12747 */
12748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012749f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750{
12751 char_u *strregname;
12752 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012753 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012754 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012755 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012757 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012759 strregname = get_tv_string_chk(&argvars[0]);
12760 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012761 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012763 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012764 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12765 return_list = get_tv_number_chk(&argvars[2], &error);
12766 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012769 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012770
12771 if (error)
12772 return;
12773
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 regname = (strregname == NULL ? '"' : *strregname);
12775 if (regname == 0)
12776 regname = '"';
12777
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012778 if (return_list)
12779 {
12780 rettv->v_type = VAR_LIST;
12781 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12782 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012783 if (rettv->vval.v_list != NULL)
12784 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012785 }
12786 else
12787 {
12788 rettv->v_type = VAR_STRING;
12789 rettv->vval.v_string = get_reg_contents(regname,
12790 arg2 ? GREG_EXPR_SRC : 0);
12791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792}
12793
12794/*
12795 * "getregtype()" function
12796 */
12797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012798f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799{
12800 char_u *strregname;
12801 int regname;
12802 char_u buf[NUMBUFLEN + 2];
12803 long reglen = 0;
12804
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012805 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012806 {
12807 strregname = get_tv_string_chk(&argvars[0]);
12808 if (strregname == NULL) /* type error; errmsg already given */
12809 {
12810 rettv->v_type = VAR_STRING;
12811 rettv->vval.v_string = NULL;
12812 return;
12813 }
12814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 else
12816 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012817 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818
12819 regname = (strregname == NULL ? '"' : *strregname);
12820 if (regname == 0)
12821 regname = '"';
12822
12823 buf[0] = NUL;
12824 buf[1] = NUL;
12825 switch (get_reg_type(regname, &reglen))
12826 {
12827 case MLINE: buf[0] = 'V'; break;
12828 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829 case MBLOCK:
12830 buf[0] = Ctrl_V;
12831 sprintf((char *)buf + 1, "%ld", reglen + 1);
12832 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 rettv->v_type = VAR_STRING;
12835 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836}
12837
12838/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012839 * "gettabvar()" function
12840 */
12841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012842f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012843{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012844 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012845 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012846 dictitem_T *v;
12847 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012848 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012849
12850 rettv->v_type = VAR_STRING;
12851 rettv->vval.v_string = NULL;
12852
12853 varname = get_tv_string_chk(&argvars[1]);
12854 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12855 if (tp != NULL && varname != NULL)
12856 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012857 /* Set tp to be our tabpage, temporarily. Also set the window to the
12858 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012859 if (switch_win(&oldcurwin, &oldtabpage,
12860 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012861 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012862 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012863 /* look up the variable */
12864 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12865 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12866 if (v != NULL)
12867 {
12868 copy_tv(&v->di_tv, rettv);
12869 done = TRUE;
12870 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012871 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012872
12873 /* restore previous notion of curwin */
12874 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012875 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012876
12877 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012878 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012879}
12880
12881/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012882 * "gettabwinvar()" function
12883 */
12884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012885f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012886{
12887 getwinvar(argvars, rettv, 1);
12888}
12889
12890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891 * "getwinposx()" function
12892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012894f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897#ifdef FEAT_GUI
12898 if (gui.in_use)
12899 {
12900 int x, y;
12901
12902 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 }
12905#endif
12906}
12907
12908/*
12909 * "getwinposy()" function
12910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012912f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915#ifdef FEAT_GUI
12916 if (gui.in_use)
12917 {
12918 int x, y;
12919
12920 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922 }
12923#endif
12924}
12925
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012926/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012927 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012928 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012929 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012930find_win_by_nr(
12931 typval_T *vp,
12932 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012933{
12934#ifdef FEAT_WINDOWS
12935 win_T *wp;
12936#endif
12937 int nr;
12938
12939 nr = get_tv_number_chk(vp, NULL);
12940
12941#ifdef FEAT_WINDOWS
12942 if (nr < 0)
12943 return NULL;
12944 if (nr == 0)
12945 return curwin;
12946
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012947 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12948 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012949 if (--nr <= 0)
12950 break;
12951 return wp;
12952#else
12953 if (nr == 0 || nr == 1)
12954 return curwin;
12955 return NULL;
12956#endif
12957}
12958
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012960 * Find window specified by "wvp" in tabpage "tvp".
12961 */
12962 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012963find_tabwin(
12964 typval_T *wvp, /* VAR_UNKNOWN for current window */
12965 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012966{
12967 win_T *wp = NULL;
12968 tabpage_T *tp = NULL;
12969 long n;
12970
12971 if (wvp->v_type != VAR_UNKNOWN)
12972 {
12973 if (tvp->v_type != VAR_UNKNOWN)
12974 {
12975 n = get_tv_number(tvp);
12976 if (n >= 0)
12977 tp = find_tabpage(n);
12978 }
12979 else
12980 tp = curtab;
12981
12982 if (tp != NULL)
12983 wp = find_win_by_nr(wvp, tp);
12984 }
12985 else
12986 wp = curwin;
12987
12988 return wp;
12989}
12990
12991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 * "getwinvar()" function
12993 */
12994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012995f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012997 getwinvar(argvars, rettv, 0);
12998}
12999
13000/*
13001 * getwinvar() and gettabwinvar()
13002 */
13003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013004getwinvar(
13005 typval_T *argvars,
13006 typval_T *rettv,
13007 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013008{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013009 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013011 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013012 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013013 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013014#ifdef FEAT_WINDOWS
13015 win_T *oldcurwin;
13016 tabpage_T *oldtabpage;
13017 int need_switch_win;
13018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013020#ifdef FEAT_WINDOWS
13021 if (off == 1)
13022 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13023 else
13024 tp = curtab;
13025#endif
13026 win = find_win_by_nr(&argvars[off], tp);
13027 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013028 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013030 rettv->v_type = VAR_STRING;
13031 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032
13033 if (win != NULL && varname != NULL)
13034 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013035#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013036 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013037 * otherwise the window is not valid. Only do this when needed,
13038 * autocommands get blocked. */
13039 need_switch_win = !(tp == curtab && win == curwin);
13040 if (!need_switch_win
13041 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13042#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013043 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013044 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013045 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013046 if (get_option_tv(&varname, rettv, 1) == OK)
13047 done = TRUE;
13048 }
13049 else
13050 {
13051 /* Look up the variable. */
13052 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13053 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13054 varname, FALSE);
13055 if (v != NULL)
13056 {
13057 copy_tv(&v->di_tv, rettv);
13058 done = TRUE;
13059 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013062
Bram Moolenaarba117c22015-09-29 16:53:22 +020013063#ifdef FEAT_WINDOWS
13064 if (need_switch_win)
13065 /* restore previous notion of curwin */
13066 restore_win(oldcurwin, oldtabpage, TRUE);
13067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 }
13069
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013070 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13071 /* use the default return value */
13072 copy_tv(&argvars[off + 2], rettv);
13073
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074 --emsg_off;
13075}
13076
13077/*
13078 * "glob()" function
13079 */
13080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013081f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013083 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013084 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013085 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013087 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013088 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013089 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013090 if (argvars[1].v_type != VAR_UNKNOWN)
13091 {
13092 if (get_tv_number_chk(&argvars[1], &error))
13093 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013095 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013096 if (get_tv_number_chk(&argvars[2], &error))
13097 {
13098 rettv->v_type = VAR_LIST;
13099 rettv->vval.v_list = NULL;
13100 }
13101 if (argvars[3].v_type != VAR_UNKNOWN
13102 && get_tv_number_chk(&argvars[3], &error))
13103 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013104 }
13105 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013106 if (!error)
13107 {
13108 ExpandInit(&xpc);
13109 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013110 if (p_wic)
13111 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013112 if (rettv->v_type == VAR_STRING)
13113 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013114 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013115 else if (rettv_list_alloc(rettv) != FAIL)
13116 {
13117 int i;
13118
13119 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13120 NULL, options, WILD_ALL_KEEP);
13121 for (i = 0; i < xpc.xp_numfiles; i++)
13122 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13123
13124 ExpandCleanup(&xpc);
13125 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013126 }
13127 else
13128 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129}
13130
13131/*
13132 * "globpath()" function
13133 */
13134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013135f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013137 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013139 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013140 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013141 garray_T ga;
13142 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013144 /* When the optional second argument is non-zero, don't remove matches
13145 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013146 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013147 if (argvars[2].v_type != VAR_UNKNOWN)
13148 {
13149 if (get_tv_number_chk(&argvars[2], &error))
13150 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013151 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013152 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013153 if (get_tv_number_chk(&argvars[3], &error))
13154 {
13155 rettv->v_type = VAR_LIST;
13156 rettv->vval.v_list = NULL;
13157 }
13158 if (argvars[4].v_type != VAR_UNKNOWN
13159 && get_tv_number_chk(&argvars[4], &error))
13160 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013161 }
13162 }
13163 if (file != NULL && !error)
13164 {
13165 ga_init2(&ga, (int)sizeof(char_u *), 10);
13166 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13167 if (rettv->v_type == VAR_STRING)
13168 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13169 else if (rettv_list_alloc(rettv) != FAIL)
13170 for (i = 0; i < ga.ga_len; ++i)
13171 list_append_string(rettv->vval.v_list,
13172 ((char_u **)(ga.ga_data))[i], -1);
13173 ga_clear_strings(&ga);
13174 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013175 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013176 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177}
13178
13179/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013180 * "glob2regpat()" function
13181 */
13182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013183f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013184{
13185 char_u *pat = get_tv_string_chk(&argvars[0]);
13186
13187 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013188 rettv->vval.v_string = (pat == NULL)
13189 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013190}
13191
13192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193 * "has()" function
13194 */
13195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013196f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197{
13198 int i;
13199 char_u *name;
13200 int n = FALSE;
13201 static char *(has_list[]) =
13202 {
13203#ifdef AMIGA
13204 "amiga",
13205# ifdef FEAT_ARP
13206 "arp",
13207# endif
13208#endif
13209#ifdef __BEOS__
13210 "beos",
13211#endif
13212#ifdef MSDOS
13213# ifdef DJGPP
13214 "dos32",
13215# else
13216 "dos16",
13217# endif
13218#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013219#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 "mac",
13221#endif
13222#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013223 "macunix", /* built with 'darwin' enabled */
13224#endif
13225#if defined(__APPLE__) && __APPLE__ == 1
13226 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228#ifdef __QNX__
13229 "qnx",
13230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231#ifdef UNIX
13232 "unix",
13233#endif
13234#ifdef VMS
13235 "vms",
13236#endif
13237#ifdef WIN16
13238 "win16",
13239#endif
13240#ifdef WIN32
13241 "win32",
13242#endif
13243#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13244 "win32unix",
13245#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013246#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 "win64",
13248#endif
13249#ifdef EBCDIC
13250 "ebcdic",
13251#endif
13252#ifndef CASE_INSENSITIVE_FILENAME
13253 "fname_case",
13254#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013255#ifdef HAVE_ACL
13256 "acl",
13257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258#ifdef FEAT_ARABIC
13259 "arabic",
13260#endif
13261#ifdef FEAT_AUTOCMD
13262 "autocmd",
13263#endif
13264#ifdef FEAT_BEVAL
13265 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013266# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13267 "balloon_multiline",
13268# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269#endif
13270#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13271 "builtin_terms",
13272# ifdef ALL_BUILTIN_TCAPS
13273 "all_builtin_terms",
13274# endif
13275#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013276#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13277 || defined(FEAT_GUI_W32) \
13278 || defined(FEAT_GUI_MOTIF))
13279 "browsefilter",
13280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281#ifdef FEAT_BYTEOFF
13282 "byte_offset",
13283#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013284#ifdef FEAT_CHANNEL
13285 "channel",
13286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287#ifdef FEAT_CINDENT
13288 "cindent",
13289#endif
13290#ifdef FEAT_CLIENTSERVER
13291 "clientserver",
13292#endif
13293#ifdef FEAT_CLIPBOARD
13294 "clipboard",
13295#endif
13296#ifdef FEAT_CMDL_COMPL
13297 "cmdline_compl",
13298#endif
13299#ifdef FEAT_CMDHIST
13300 "cmdline_hist",
13301#endif
13302#ifdef FEAT_COMMENTS
13303 "comments",
13304#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013305#ifdef FEAT_CONCEAL
13306 "conceal",
13307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308#ifdef FEAT_CRYPT
13309 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013310 "crypt-blowfish",
13311 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312#endif
13313#ifdef FEAT_CSCOPE
13314 "cscope",
13315#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013316#ifdef FEAT_CURSORBIND
13317 "cursorbind",
13318#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013319#ifdef CURSOR_SHAPE
13320 "cursorshape",
13321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322#ifdef DEBUG
13323 "debug",
13324#endif
13325#ifdef FEAT_CON_DIALOG
13326 "dialog_con",
13327#endif
13328#ifdef FEAT_GUI_DIALOG
13329 "dialog_gui",
13330#endif
13331#ifdef FEAT_DIFF
13332 "diff",
13333#endif
13334#ifdef FEAT_DIGRAPHS
13335 "digraphs",
13336#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013337#ifdef FEAT_DIRECTX
13338 "directx",
13339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340#ifdef FEAT_DND
13341 "dnd",
13342#endif
13343#ifdef FEAT_EMACS_TAGS
13344 "emacs_tags",
13345#endif
13346 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013347 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348#ifdef FEAT_SEARCH_EXTRA
13349 "extra_search",
13350#endif
13351#ifdef FEAT_FKMAP
13352 "farsi",
13353#endif
13354#ifdef FEAT_SEARCHPATH
13355 "file_in_path",
13356#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013357#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013358 "filterpipe",
13359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360#ifdef FEAT_FIND_ID
13361 "find_in_path",
13362#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013363#ifdef FEAT_FLOAT
13364 "float",
13365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366#ifdef FEAT_FOLDING
13367 "folding",
13368#endif
13369#ifdef FEAT_FOOTER
13370 "footer",
13371#endif
13372#if !defined(USE_SYSTEM) && defined(UNIX)
13373 "fork",
13374#endif
13375#ifdef FEAT_GETTEXT
13376 "gettext",
13377#endif
13378#ifdef FEAT_GUI
13379 "gui",
13380#endif
13381#ifdef FEAT_GUI_ATHENA
13382# ifdef FEAT_GUI_NEXTAW
13383 "gui_neXtaw",
13384# else
13385 "gui_athena",
13386# endif
13387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388#ifdef FEAT_GUI_GTK
13389 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013392#ifdef FEAT_GUI_GNOME
13393 "gui_gnome",
13394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395#ifdef FEAT_GUI_MAC
13396 "gui_mac",
13397#endif
13398#ifdef FEAT_GUI_MOTIF
13399 "gui_motif",
13400#endif
13401#ifdef FEAT_GUI_PHOTON
13402 "gui_photon",
13403#endif
13404#ifdef FEAT_GUI_W16
13405 "gui_win16",
13406#endif
13407#ifdef FEAT_GUI_W32
13408 "gui_win32",
13409#endif
13410#ifdef FEAT_HANGULIN
13411 "hangul_input",
13412#endif
13413#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13414 "iconv",
13415#endif
13416#ifdef FEAT_INS_EXPAND
13417 "insert_expand",
13418#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013419#ifdef FEAT_JOB
13420 "job",
13421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422#ifdef FEAT_JUMPLIST
13423 "jumplist",
13424#endif
13425#ifdef FEAT_KEYMAP
13426 "keymap",
13427#endif
13428#ifdef FEAT_LANGMAP
13429 "langmap",
13430#endif
13431#ifdef FEAT_LIBCALL
13432 "libcall",
13433#endif
13434#ifdef FEAT_LINEBREAK
13435 "linebreak",
13436#endif
13437#ifdef FEAT_LISP
13438 "lispindent",
13439#endif
13440#ifdef FEAT_LISTCMDS
13441 "listcmds",
13442#endif
13443#ifdef FEAT_LOCALMAP
13444 "localmap",
13445#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013446#ifdef FEAT_LUA
13447# ifndef DYNAMIC_LUA
13448 "lua",
13449# endif
13450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451#ifdef FEAT_MENU
13452 "menu",
13453#endif
13454#ifdef FEAT_SESSION
13455 "mksession",
13456#endif
13457#ifdef FEAT_MODIFY_FNAME
13458 "modify_fname",
13459#endif
13460#ifdef FEAT_MOUSE
13461 "mouse",
13462#endif
13463#ifdef FEAT_MOUSESHAPE
13464 "mouseshape",
13465#endif
13466#if defined(UNIX) || defined(VMS)
13467# ifdef FEAT_MOUSE_DEC
13468 "mouse_dec",
13469# endif
13470# ifdef FEAT_MOUSE_GPM
13471 "mouse_gpm",
13472# endif
13473# ifdef FEAT_MOUSE_JSB
13474 "mouse_jsbterm",
13475# endif
13476# ifdef FEAT_MOUSE_NET
13477 "mouse_netterm",
13478# endif
13479# ifdef FEAT_MOUSE_PTERM
13480 "mouse_pterm",
13481# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013482# ifdef FEAT_MOUSE_SGR
13483 "mouse_sgr",
13484# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013485# ifdef FEAT_SYSMOUSE
13486 "mouse_sysmouse",
13487# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013488# ifdef FEAT_MOUSE_URXVT
13489 "mouse_urxvt",
13490# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491# ifdef FEAT_MOUSE_XTERM
13492 "mouse_xterm",
13493# endif
13494#endif
13495#ifdef FEAT_MBYTE
13496 "multi_byte",
13497#endif
13498#ifdef FEAT_MBYTE_IME
13499 "multi_byte_ime",
13500#endif
13501#ifdef FEAT_MULTI_LANG
13502 "multi_lang",
13503#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013504#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013505#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013506 "mzscheme",
13507#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509#ifdef FEAT_OLE
13510 "ole",
13511#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512#ifdef FEAT_PATH_EXTRA
13513 "path_extra",
13514#endif
13515#ifdef FEAT_PERL
13516#ifndef DYNAMIC_PERL
13517 "perl",
13518#endif
13519#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013520#ifdef FEAT_PERSISTENT_UNDO
13521 "persistent_undo",
13522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523#ifdef FEAT_PYTHON
13524#ifndef DYNAMIC_PYTHON
13525 "python",
13526#endif
13527#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013528#ifdef FEAT_PYTHON3
13529#ifndef DYNAMIC_PYTHON3
13530 "python3",
13531#endif
13532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533#ifdef FEAT_POSTSCRIPT
13534 "postscript",
13535#endif
13536#ifdef FEAT_PRINTER
13537 "printer",
13538#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013539#ifdef FEAT_PROFILE
13540 "profile",
13541#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013542#ifdef FEAT_RELTIME
13543 "reltime",
13544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545#ifdef FEAT_QUICKFIX
13546 "quickfix",
13547#endif
13548#ifdef FEAT_RIGHTLEFT
13549 "rightleft",
13550#endif
13551#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13552 "ruby",
13553#endif
13554#ifdef FEAT_SCROLLBIND
13555 "scrollbind",
13556#endif
13557#ifdef FEAT_CMDL_INFO
13558 "showcmd",
13559 "cmdline_info",
13560#endif
13561#ifdef FEAT_SIGNS
13562 "signs",
13563#endif
13564#ifdef FEAT_SMARTINDENT
13565 "smartindent",
13566#endif
13567#ifdef FEAT_SNIFF
13568 "sniff",
13569#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013570#ifdef STARTUPTIME
13571 "startuptime",
13572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573#ifdef FEAT_STL_OPT
13574 "statusline",
13575#endif
13576#ifdef FEAT_SUN_WORKSHOP
13577 "sun_workshop",
13578#endif
13579#ifdef FEAT_NETBEANS_INTG
13580 "netbeans_intg",
13581#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013582#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013583 "spell",
13584#endif
13585#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 "syntax",
13587#endif
13588#if defined(USE_SYSTEM) || !defined(UNIX)
13589 "system",
13590#endif
13591#ifdef FEAT_TAG_BINS
13592 "tag_binary",
13593#endif
13594#ifdef FEAT_TAG_OLDSTATIC
13595 "tag_old_static",
13596#endif
13597#ifdef FEAT_TAG_ANYWHITE
13598 "tag_any_white",
13599#endif
13600#ifdef FEAT_TCL
13601# ifndef DYNAMIC_TCL
13602 "tcl",
13603# endif
13604#endif
13605#ifdef TERMINFO
13606 "terminfo",
13607#endif
13608#ifdef FEAT_TERMRESPONSE
13609 "termresponse",
13610#endif
13611#ifdef FEAT_TEXTOBJ
13612 "textobjects",
13613#endif
13614#ifdef HAVE_TGETENT
13615 "tgetent",
13616#endif
13617#ifdef FEAT_TITLE
13618 "title",
13619#endif
13620#ifdef FEAT_TOOLBAR
13621 "toolbar",
13622#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013623#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13624 "unnamedplus",
13625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626#ifdef FEAT_USR_CMDS
13627 "user-commands", /* was accidentally included in 5.4 */
13628 "user_commands",
13629#endif
13630#ifdef FEAT_VIMINFO
13631 "viminfo",
13632#endif
13633#ifdef FEAT_VERTSPLIT
13634 "vertsplit",
13635#endif
13636#ifdef FEAT_VIRTUALEDIT
13637 "virtualedit",
13638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640#ifdef FEAT_VISUALEXTRA
13641 "visualextra",
13642#endif
13643#ifdef FEAT_VREPLACE
13644 "vreplace",
13645#endif
13646#ifdef FEAT_WILDIGN
13647 "wildignore",
13648#endif
13649#ifdef FEAT_WILDMENU
13650 "wildmenu",
13651#endif
13652#ifdef FEAT_WINDOWS
13653 "windows",
13654#endif
13655#ifdef FEAT_WAK
13656 "winaltkeys",
13657#endif
13658#ifdef FEAT_WRITEBACKUP
13659 "writebackup",
13660#endif
13661#ifdef FEAT_XIM
13662 "xim",
13663#endif
13664#ifdef FEAT_XFONTSET
13665 "xfontset",
13666#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013667#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013668 "xpm",
13669 "xpm_w32", /* for backward compatibility */
13670#else
13671# if defined(HAVE_XPM)
13672 "xpm",
13673# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675#ifdef USE_XSMP
13676 "xsmp",
13677#endif
13678#ifdef USE_XSMP_INTERACT
13679 "xsmp_interact",
13680#endif
13681#ifdef FEAT_XCLIPBOARD
13682 "xterm_clipboard",
13683#endif
13684#ifdef FEAT_XTERM_SAVE
13685 "xterm_save",
13686#endif
13687#if defined(UNIX) && defined(FEAT_X11)
13688 "X11",
13689#endif
13690 NULL
13691 };
13692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 for (i = 0; has_list[i] != NULL; ++i)
13695 if (STRICMP(name, has_list[i]) == 0)
13696 {
13697 n = TRUE;
13698 break;
13699 }
13700
13701 if (n == FALSE)
13702 {
13703 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013704 {
13705 if (name[5] == '-'
13706 && STRLEN(name) > 11
13707 && vim_isdigit(name[6])
13708 && vim_isdigit(name[8])
13709 && vim_isdigit(name[10]))
13710 {
13711 int major = atoi((char *)name + 6);
13712 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013713
13714 /* Expect "patch-9.9.01234". */
13715 n = (major < VIM_VERSION_MAJOR
13716 || (major == VIM_VERSION_MAJOR
13717 && (minor < VIM_VERSION_MINOR
13718 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013719 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013720 }
13721 else
13722 n = has_patch(atoi((char *)name + 5));
13723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724 else if (STRICMP(name, "vim_starting") == 0)
13725 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013726#ifdef FEAT_MBYTE
13727 else if (STRICMP(name, "multi_byte_encoding") == 0)
13728 n = has_mbyte;
13729#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013730#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13731 else if (STRICMP(name, "balloon_multiline") == 0)
13732 n = multiline_balloon_available();
13733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734#ifdef DYNAMIC_TCL
13735 else if (STRICMP(name, "tcl") == 0)
13736 n = tcl_enabled(FALSE);
13737#endif
13738#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13739 else if (STRICMP(name, "iconv") == 0)
13740 n = iconv_enabled(FALSE);
13741#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013742#ifdef DYNAMIC_LUA
13743 else if (STRICMP(name, "lua") == 0)
13744 n = lua_enabled(FALSE);
13745#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013746#ifdef DYNAMIC_MZSCHEME
13747 else if (STRICMP(name, "mzscheme") == 0)
13748 n = mzscheme_enabled(FALSE);
13749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750#ifdef DYNAMIC_RUBY
13751 else if (STRICMP(name, "ruby") == 0)
13752 n = ruby_enabled(FALSE);
13753#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013754#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#ifdef DYNAMIC_PYTHON
13756 else if (STRICMP(name, "python") == 0)
13757 n = python_enabled(FALSE);
13758#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013759#endif
13760#ifdef FEAT_PYTHON3
13761#ifdef DYNAMIC_PYTHON3
13762 else if (STRICMP(name, "python3") == 0)
13763 n = python3_enabled(FALSE);
13764#endif
13765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766#ifdef DYNAMIC_PERL
13767 else if (STRICMP(name, "perl") == 0)
13768 n = perl_enabled(FALSE);
13769#endif
13770#ifdef FEAT_GUI
13771 else if (STRICMP(name, "gui_running") == 0)
13772 n = (gui.in_use || gui.starting);
13773# ifdef FEAT_GUI_W32
13774 else if (STRICMP(name, "gui_win32s") == 0)
13775 n = gui_is_win32s();
13776# endif
13777# ifdef FEAT_BROWSE
13778 else if (STRICMP(name, "browse") == 0)
13779 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13780# endif
13781#endif
13782#ifdef FEAT_SYN_HL
13783 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013784 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785#endif
13786#if defined(WIN3264)
13787 else if (STRICMP(name, "win95") == 0)
13788 n = mch_windows95();
13789#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013790#ifdef FEAT_NETBEANS_INTG
13791 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013792 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794 }
13795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013796 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797}
13798
13799/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013800 * "has_key()" function
13801 */
13802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013803f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013804{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013805 if (argvars[0].v_type != VAR_DICT)
13806 {
13807 EMSG(_(e_dictreq));
13808 return;
13809 }
13810 if (argvars[0].vval.v_dict == NULL)
13811 return;
13812
13813 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013814 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013815}
13816
13817/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013818 * "haslocaldir()" function
13819 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013821f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013822{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013823 win_T *wp = NULL;
13824
13825 wp = find_tabwin(&argvars[0], &argvars[1]);
13826 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013827}
13828
13829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 * "hasmapto()" function
13831 */
13832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013833f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834{
13835 char_u *name;
13836 char_u *mode;
13837 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013838 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013840 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013841 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 mode = (char_u *)"nvo";
13843 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013845 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013846 if (argvars[2].v_type != VAR_UNKNOWN)
13847 abbr = get_tv_number(&argvars[2]);
13848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849
Bram Moolenaar2c932302006-03-18 21:42:09 +000013850 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013851 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013853 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854}
13855
13856/*
13857 * "histadd()" function
13858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013860f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013861{
13862#ifdef FEAT_CMDHIST
13863 int histype;
13864 char_u *str;
13865 char_u buf[NUMBUFLEN];
13866#endif
13867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013868 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869 if (check_restricted() || check_secure())
13870 return;
13871#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013872 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13873 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 if (histype >= 0)
13875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013876 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877 if (*str != NUL)
13878 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013879 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013881 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882 return;
13883 }
13884 }
13885#endif
13886}
13887
13888/*
13889 * "histdel()" function
13890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013892f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893{
13894#ifdef FEAT_CMDHIST
13895 int n;
13896 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013899 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13900 if (str == NULL)
13901 n = 0;
13902 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013904 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013905 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013907 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 else
13910 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013911 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013912 get_tv_string_buf(&argvars[1], buf));
13913 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914#endif
13915}
13916
13917/*
13918 * "histget()" function
13919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013921f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922{
13923#ifdef FEAT_CMDHIST
13924 int type;
13925 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013926 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013928 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13929 if (str == NULL)
13930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013932 {
13933 type = get_histtype(str);
13934 if (argvars[1].v_type == VAR_UNKNOWN)
13935 idx = get_history_idx(type);
13936 else
13937 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13938 /* -1 on type error */
13939 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013942 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945}
13946
13947/*
13948 * "histnr()" function
13949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013951f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952{
13953 int i;
13954
13955#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 char_u *history = get_tv_string_chk(&argvars[0]);
13957
13958 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959 if (i >= HIST_CMD && i < HIST_COUNT)
13960 i = get_history_idx(i);
13961 else
13962#endif
13963 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965}
13966
13967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 * "highlightID(name)" function
13969 */
13970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013971f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013973 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974}
13975
13976/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013977 * "highlight_exists()" function
13978 */
13979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013980f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013981{
13982 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13983}
13984
13985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 * "hostname()" function
13987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013989f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990{
13991 char_u hostname[256];
13992
13993 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013994 rettv->v_type = VAR_STRING;
13995 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996}
13997
13998/*
13999 * iconv() function
14000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014002f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003{
14004#ifdef FEAT_MBYTE
14005 char_u buf1[NUMBUFLEN];
14006 char_u buf2[NUMBUFLEN];
14007 char_u *from, *to, *str;
14008 vimconv_T vimconv;
14009#endif
14010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014011 rettv->v_type = VAR_STRING;
14012 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013
14014#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014015 str = get_tv_string(&argvars[0]);
14016 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14017 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 vimconv.vc_type = CONV_NONE;
14019 convert_setup(&vimconv, from, to);
14020
14021 /* If the encodings are equal, no conversion needed. */
14022 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014025 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026
14027 convert_setup(&vimconv, NULL, NULL);
14028 vim_free(from);
14029 vim_free(to);
14030#endif
14031}
14032
14033/*
14034 * "indent()" function
14035 */
14036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014037f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038{
14039 linenr_T lnum;
14040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014045 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046}
14047
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014048/*
14049 * "index()" function
14050 */
14051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014052f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014053{
Bram Moolenaar33570922005-01-25 22:26:29 +000014054 list_T *l;
14055 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014056 long idx = 0;
14057 int ic = FALSE;
14058
14059 rettv->vval.v_number = -1;
14060 if (argvars[0].v_type != VAR_LIST)
14061 {
14062 EMSG(_(e_listreq));
14063 return;
14064 }
14065 l = argvars[0].vval.v_list;
14066 if (l != NULL)
14067 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014068 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014071 int error = FALSE;
14072
Bram Moolenaar758711c2005-02-02 23:11:38 +000014073 /* Start at specified item. Use the cached index that list_find()
14074 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014075 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014076 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014077 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014078 ic = get_tv_number_chk(&argvars[3], &error);
14079 if (error)
14080 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014081 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014082
Bram Moolenaar758711c2005-02-02 23:11:38 +000014083 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014084 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014085 {
14086 rettv->vval.v_number = idx;
14087 break;
14088 }
14089 }
14090}
14091
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092static int inputsecret_flag = 0;
14093
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014094static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014095
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014097 * This function is used by f_input() and f_inputdialog() functions. The third
14098 * argument to f_input() specifies the type of completion to use at the
14099 * prompt. The third argument to f_inputdialog() specifies the value to return
14100 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101 */
14102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014103get_user_input(
14104 typval_T *argvars,
14105 typval_T *rettv,
14106 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 char_u *p = NULL;
14110 int c;
14111 char_u buf[NUMBUFLEN];
14112 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014114 int xp_type = EXPAND_NOTHING;
14115 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014117 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014118 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119
14120#ifdef NO_CONSOLE_INPUT
14121 /* While starting up, there is no place to enter text. */
14122 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124#endif
14125
14126 cmd_silent = FALSE; /* Want to see the prompt. */
14127 if (prompt != NULL)
14128 {
14129 /* Only the part of the message after the last NL is considered as
14130 * prompt for the command line */
14131 p = vim_strrchr(prompt, '\n');
14132 if (p == NULL)
14133 p = prompt;
14134 else
14135 {
14136 ++p;
14137 c = *p;
14138 *p = NUL;
14139 msg_start();
14140 msg_clr_eos();
14141 msg_puts_attr(prompt, echo_attr);
14142 msg_didout = FALSE;
14143 msg_starthere();
14144 *p = c;
14145 }
14146 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 if (argvars[1].v_type != VAR_UNKNOWN)
14149 {
14150 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14151 if (defstr != NULL)
14152 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014154 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014155 {
14156 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014157 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014158 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014159
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014160 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014161 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014162
Bram Moolenaar4463f292005-09-25 22:20:24 +000014163 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14164 if (xp_name == NULL)
14165 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014166
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014167 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014168
Bram Moolenaar4463f292005-09-25 22:20:24 +000014169 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14170 &xp_arg) == FAIL)
14171 return;
14172 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014173 }
14174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014176 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014177 int save_ex_normal_busy = ex_normal_busy;
14178 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014180 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14181 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014182 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014183 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014184 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014185 && argvars[1].v_type != VAR_UNKNOWN
14186 && argvars[2].v_type != VAR_UNKNOWN)
14187 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14188 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014189
14190 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 /* since the user typed this, no need to wait for return */
14193 need_wait_return = FALSE;
14194 msg_didout = FALSE;
14195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 cmd_silent = cmd_silent_save;
14197}
14198
14199/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014200 * "input()" function
14201 * Also handles inputsecret() when inputsecret is set.
14202 */
14203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014204f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014205{
14206 get_user_input(argvars, rettv, FALSE);
14207}
14208
14209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210 * "inputdialog()" function
14211 */
14212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014213f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214{
14215#if defined(FEAT_GUI_TEXTDIALOG)
14216 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14217 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14218 {
14219 char_u *message;
14220 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014221 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014223 message = get_tv_string_chk(&argvars[0]);
14224 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014225 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014226 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227 else
14228 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014229 if (message != NULL && defstr != NULL
14230 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014231 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014232 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233 else
14234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 if (message != NULL && defstr != NULL
14236 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014237 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014238 rettv->vval.v_string = vim_strsave(
14239 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014243 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 }
14245 else
14246#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014247 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248}
14249
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014250/*
14251 * "inputlist()" function
14252 */
14253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014254f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014255{
14256 listitem_T *li;
14257 int selected;
14258 int mouse_used;
14259
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014260#ifdef NO_CONSOLE_INPUT
14261 /* While starting up, there is no place to enter text. */
14262 if (no_console_input())
14263 return;
14264#endif
14265 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14266 {
14267 EMSG2(_(e_listarg), "inputlist()");
14268 return;
14269 }
14270
14271 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014272 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014273 lines_left = Rows; /* avoid more prompt */
14274 msg_scroll = TRUE;
14275 msg_clr_eos();
14276
14277 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14278 {
14279 msg_puts(get_tv_string(&li->li_tv));
14280 msg_putchar('\n');
14281 }
14282
14283 /* Ask for choice. */
14284 selected = prompt_for_number(&mouse_used);
14285 if (mouse_used)
14286 selected -= lines_left;
14287
14288 rettv->vval.v_number = selected;
14289}
14290
14291
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14293
14294/*
14295 * "inputrestore()" function
14296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014298f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299{
14300 if (ga_userinput.ga_len > 0)
14301 {
14302 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14304 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014305 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306 }
14307 else if (p_verbose > 1)
14308 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014309 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014310 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311 }
14312}
14313
14314/*
14315 * "inputsave()" function
14316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014318f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014320 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 if (ga_grow(&ga_userinput, 1) == OK)
14322 {
14323 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14324 + ga_userinput.ga_len);
14325 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014326 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 }
14328 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014329 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330}
14331
14332/*
14333 * "inputsecret()" function
14334 */
14335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014336f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337{
14338 ++cmdline_star;
14339 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 --cmdline_star;
14342 --inputsecret_flag;
14343}
14344
14345/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014346 * "insert()" function
14347 */
14348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014349f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014350{
14351 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014352 listitem_T *item;
14353 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014355
14356 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014357 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014358 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014359 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014360 {
14361 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014362 before = get_tv_number_chk(&argvars[2], &error);
14363 if (error)
14364 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014365
Bram Moolenaar758711c2005-02-02 23:11:38 +000014366 if (before == l->lv_len)
14367 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014368 else
14369 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014370 item = list_find(l, before);
14371 if (item == NULL)
14372 {
14373 EMSGN(_(e_listidx), before);
14374 l = NULL;
14375 }
14376 }
14377 if (l != NULL)
14378 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014379 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014380 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014381 }
14382 }
14383}
14384
14385/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014386 * "invert(expr)" function
14387 */
14388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014389f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014390{
14391 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14392}
14393
14394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 * "isdirectory()" function
14396 */
14397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014398f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401}
14402
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014403/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014404 * "islocked()" function
14405 */
14406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014407f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014408{
14409 lval_T lv;
14410 char_u *end;
14411 dictitem_T *di;
14412
14413 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014414 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14415 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014416 if (end != NULL && lv.ll_name != NULL)
14417 {
14418 if (*end != NUL)
14419 EMSG(_(e_trailing));
14420 else
14421 {
14422 if (lv.ll_tv == NULL)
14423 {
14424 if (check_changedtick(lv.ll_name))
14425 rettv->vval.v_number = 1; /* always locked */
14426 else
14427 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014428 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014429 if (di != NULL)
14430 {
14431 /* Consider a variable locked when:
14432 * 1. the variable itself is locked
14433 * 2. the value of the variable is locked.
14434 * 3. the List or Dict value is locked.
14435 */
14436 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14437 || tv_islocked(&di->di_tv));
14438 }
14439 }
14440 }
14441 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014442 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014443 else if (lv.ll_newkey != NULL)
14444 EMSG2(_(e_dictkey), lv.ll_newkey);
14445 else if (lv.ll_list != NULL)
14446 /* List item. */
14447 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14448 else
14449 /* Dictionary item. */
14450 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14451 }
14452 }
14453
14454 clear_lval(&lv);
14455}
14456
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014457static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014458
14459/*
14460 * Turn a dict into a list:
14461 * "what" == 0: list of keys
14462 * "what" == 1: list of values
14463 * "what" == 2: list of items
14464 */
14465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014466dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014467{
Bram Moolenaar33570922005-01-25 22:26:29 +000014468 list_T *l2;
14469 dictitem_T *di;
14470 hashitem_T *hi;
14471 listitem_T *li;
14472 listitem_T *li2;
14473 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014474 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014475
Bram Moolenaar8c711452005-01-14 21:53:12 +000014476 if (argvars[0].v_type != VAR_DICT)
14477 {
14478 EMSG(_(e_dictreq));
14479 return;
14480 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014481 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014482 return;
14483
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014484 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014485 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014486
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014487 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014488 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014489 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014490 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014491 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014492 --todo;
14493 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014494
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014495 li = listitem_alloc();
14496 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014497 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014498 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014499
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014500 if (what == 0)
14501 {
14502 /* keys() */
14503 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014504 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014505 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14506 }
14507 else if (what == 1)
14508 {
14509 /* values() */
14510 copy_tv(&di->di_tv, &li->li_tv);
14511 }
14512 else
14513 {
14514 /* items() */
14515 l2 = list_alloc();
14516 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014517 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014518 li->li_tv.vval.v_list = l2;
14519 if (l2 == NULL)
14520 break;
14521 ++l2->lv_refcount;
14522
14523 li2 = listitem_alloc();
14524 if (li2 == NULL)
14525 break;
14526 list_append(l2, li2);
14527 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014528 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014529 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14530
14531 li2 = listitem_alloc();
14532 if (li2 == NULL)
14533 break;
14534 list_append(l2, li2);
14535 copy_tv(&di->di_tv, &li2->li_tv);
14536 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014537 }
14538 }
14539}
14540
14541/*
14542 * "items(dict)" function
14543 */
14544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014545f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014546{
14547 dict_list(argvars, rettv, 2);
14548}
14549
Bram Moolenaar835dc632016-02-07 14:27:38 +010014550#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014551
14552# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014553/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014554 * "job_getchannel()" function
14555 */
14556 static void
14557f_job_getchannel(typval_T *argvars, typval_T *rettv)
14558{
14559 if (argvars[0].v_type != VAR_JOB)
14560 EMSG(_(e_invarg));
14561 else
14562 {
14563 job_T *job = argvars[0].vval.v_job;
14564
Bram Moolenaar77073442016-02-13 23:23:53 +010014565 rettv->v_type = VAR_CHANNEL;
14566 rettv->vval.v_channel = job->jv_channel;
14567 if (job->jv_channel != NULL)
14568 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014569 }
14570}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014571# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014572
14573/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014574 * "job_start()" function
14575 */
14576 static void
14577f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14578{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014579 job_T *job;
14580 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014581#if defined(UNIX)
14582# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014583 char **argv = NULL;
14584 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014585#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014586 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014587#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014588 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014589
14590 rettv->v_type = VAR_JOB;
14591 job = job_alloc();
14592 rettv->vval.v_job = job;
14593 if (job == NULL)
14594 return;
14595
14596 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014597
14598 /* Default mode is NL. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014599 opt.jo_mode = MODE_NL;
14600 opt.jo_callback = NULL;
14601 if (get_job_options(&argvars[1], &opt, JO_MODE + JO_CALLBACK) == FAIL)
14602 return;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014603
Bram Moolenaar835dc632016-02-07 14:27:38 +010014604#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010014605 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014606#endif
14607
14608 if (argvars[0].v_type == VAR_STRING)
14609 {
14610 /* Command is a string. */
14611 cmd = argvars[0].vval.v_string;
14612#ifdef USE_ARGV
14613 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
14614 return;
14615 argv[argc] = NULL;
14616#endif
14617 }
14618 else if (argvars[0].v_type != VAR_LIST
14619 || argvars[0].vval.v_list == NULL
14620 || argvars[0].vval.v_list->lv_len < 1)
14621 {
14622 EMSG(_(e_invarg));
14623 return;
14624 }
14625 else
14626 {
14627 list_T *l = argvars[0].vval.v_list;
14628 listitem_T *li;
14629 char_u *s;
14630
14631#ifdef USE_ARGV
14632 /* Pass argv[] to mch_call_shell(). */
14633 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
14634 if (argv == NULL)
14635 return;
14636#endif
14637 for (li = l->lv_first; li != NULL; li = li->li_next)
14638 {
14639 s = get_tv_string_chk(&li->li_tv);
14640 if (s == NULL)
14641 goto theend;
14642#ifdef USE_ARGV
14643 argv[argc++] = (char *)s;
14644#else
14645 if (li != l->lv_first)
14646 {
14647 s = vim_strsave_shellescape(s, FALSE, TRUE);
14648 if (s == NULL)
14649 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010014650 ga_concat(&ga, s);
14651 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014652 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010014653 else
14654 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014655 if (li->li_next != NULL)
14656 ga_append(&ga, ' ');
14657#endif
14658 }
14659#ifdef USE_ARGV
14660 argv[argc] = NULL;
14661#else
14662 cmd = ga.ga_data;
14663#endif
14664 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014665
Bram Moolenaar835dc632016-02-07 14:27:38 +010014666#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014667# ifdef FEAT_CHANNEL
14668 if (ch_log_active())
14669 {
14670 garray_T ga;
14671 int i;
14672
14673 ga_init2(&ga, (int)sizeof(char), 200);
14674 for (i = 0; i < argc; ++i)
14675 {
14676 if (i > 0)
14677 ga_concat(&ga, (char_u *)" ");
14678 ga_concat(&ga, (char_u *)argv[i]);
14679 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010014680 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014681 ga_clear(&ga);
14682 }
14683# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014684 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014685#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014686# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010014687 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010014688# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014689 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014690#endif
14691
14692theend:
14693#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010014694 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014695#else
14696 vim_free(ga.ga_data);
14697#endif
14698}
14699
14700/*
14701 * "job_status()" function
14702 */
14703 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014704f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014705{
14706 char *result;
14707
14708 if (argvars[0].v_type != VAR_JOB)
14709 EMSG(_(e_invarg));
14710 else
14711 {
14712 job_T *job = argvars[0].vval.v_job;
14713
14714 if (job->jv_status == JOB_ENDED)
14715 /* No need to check, dead is dead. */
14716 result = "dead";
14717 else if (job->jv_status == JOB_FAILED)
14718 result = "fail";
14719 else
14720 result = mch_job_status(job);
14721 rettv->v_type = VAR_STRING;
14722 rettv->vval.v_string = vim_strsave((char_u *)result);
14723 }
14724}
14725
14726/*
14727 * "job_stop()" function
14728 */
14729 static void
14730f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14731{
14732 if (argvars[0].v_type != VAR_JOB)
14733 EMSG(_(e_invarg));
14734 else
14735 {
14736 char_u *arg;
14737
14738 if (argvars[1].v_type == VAR_UNKNOWN)
14739 arg = (char_u *)"";
14740 else
14741 {
14742 arg = get_tv_string_chk(&argvars[1]);
14743 if (arg == NULL)
14744 {
14745 EMSG(_(e_invarg));
14746 return;
14747 }
14748 }
14749 if (mch_stop_job(argvars[0].vval.v_job, arg) == FAIL)
14750 rettv->vval.v_number = 0;
14751 else
14752 rettv->vval.v_number = 1;
14753 }
14754}
14755#endif
14756
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014758 * "join()" function
14759 */
14760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014761f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014762{
14763 garray_T ga;
14764 char_u *sep;
14765
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014766 if (argvars[0].v_type != VAR_LIST)
14767 {
14768 EMSG(_(e_listreq));
14769 return;
14770 }
14771 if (argvars[0].vval.v_list == NULL)
14772 return;
14773 if (argvars[1].v_type == VAR_UNKNOWN)
14774 sep = (char_u *)" ";
14775 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014776 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014777
14778 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014779
14780 if (sep != NULL)
14781 {
14782 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014783 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014784 ga_append(&ga, NUL);
14785 rettv->vval.v_string = (char_u *)ga.ga_data;
14786 }
14787 else
14788 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014789}
14790
14791/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014792 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014793 */
14794 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014795f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014796{
14797 js_read_T reader;
14798
14799 reader.js_buf = get_tv_string(&argvars[0]);
14800 reader.js_fill = NULL;
14801 reader.js_used = 0;
14802 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14803 EMSG(_(e_invarg));
14804}
14805
14806/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014807 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014808 */
14809 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014810f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014811{
14812 rettv->v_type = VAR_STRING;
14813 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14814}
14815
14816/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014817 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014818 */
14819 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014820f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014821{
14822 js_read_T reader;
14823
14824 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014825 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014826 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014827 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014828 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014829}
14830
14831/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014832 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014833 */
14834 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014835f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014836{
14837 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014838 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014839}
14840
14841/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014842 * "keys()" function
14843 */
14844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014845f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014846{
14847 dict_list(argvars, rettv, 0);
14848}
14849
14850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014851 * "last_buffer_nr()" function.
14852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014854f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855{
14856 int n = 0;
14857 buf_T *buf;
14858
14859 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14860 if (n < buf->b_fnum)
14861 n = buf->b_fnum;
14862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014863 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014864}
14865
14866/*
14867 * "len()" function
14868 */
14869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014870f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014871{
14872 switch (argvars[0].v_type)
14873 {
14874 case VAR_STRING:
14875 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014876 rettv->vval.v_number = (varnumber_T)STRLEN(
14877 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014878 break;
14879 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014880 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014881 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014882 case VAR_DICT:
14883 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14884 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014885 case VAR_UNKNOWN:
14886 case VAR_SPECIAL:
14887 case VAR_FLOAT:
14888 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014889 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014890 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014891 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014892 break;
14893 }
14894}
14895
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014896static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014897
14898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014899libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014900{
14901#ifdef FEAT_LIBCALL
14902 char_u *string_in;
14903 char_u **string_result;
14904 int nr_result;
14905#endif
14906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014907 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014908 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014910
14911 if (check_restricted() || check_secure())
14912 return;
14913
14914#ifdef FEAT_LIBCALL
14915 /* The first two args must be strings, otherwise its meaningless */
14916 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14917 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014918 string_in = NULL;
14919 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014920 string_in = argvars[2].vval.v_string;
14921 if (type == VAR_NUMBER)
14922 string_result = NULL;
14923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014924 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014925 if (mch_libcall(argvars[0].vval.v_string,
14926 argvars[1].vval.v_string,
14927 string_in,
14928 argvars[2].vval.v_number,
14929 string_result,
14930 &nr_result) == OK
14931 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014932 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014933 }
14934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935}
14936
14937/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014938 * "libcall()" function
14939 */
14940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014941f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014942{
14943 libcall_common(argvars, rettv, VAR_STRING);
14944}
14945
14946/*
14947 * "libcallnr()" function
14948 */
14949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014950f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014951{
14952 libcall_common(argvars, rettv, VAR_NUMBER);
14953}
14954
14955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956 * "line(string)" function
14957 */
14958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014959f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960{
14961 linenr_T lnum = 0;
14962 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014963 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014964
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014965 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 if (fp != NULL)
14967 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014968 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969}
14970
14971/*
14972 * "line2byte(lnum)" function
14973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014975f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976{
14977#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014978 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979#else
14980 linenr_T lnum;
14981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014986 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14987 if (rettv->vval.v_number >= 0)
14988 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014989#endif
14990}
14991
14992/*
14993 * "lispindent(lnum)" function
14994 */
14995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014996f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997{
14998#ifdef FEAT_LISP
14999 pos_T pos;
15000 linenr_T lnum;
15001
15002 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015003 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015004 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15005 {
15006 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015007 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008 curwin->w_cursor = pos;
15009 }
15010 else
15011#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015012 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013}
15014
15015/*
15016 * "localtime()" function
15017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015019f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015021 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015022}
15023
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015024static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025
15026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015027get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028{
15029 char_u *keys;
15030 char_u *which;
15031 char_u buf[NUMBUFLEN];
15032 char_u *keys_buf = NULL;
15033 char_u *rhs;
15034 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015035 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015036 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015037 mapblock_T *mp;
15038 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039
15040 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015041 rettv->v_type = VAR_STRING;
15042 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015044 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 if (*keys == NUL)
15046 return;
15047
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015048 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015050 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015051 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015052 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015053 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015054 if (argvars[3].v_type != VAR_UNKNOWN)
15055 get_dict = get_tv_number(&argvars[3]);
15056 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 else
15059 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015060 if (which == NULL)
15061 return;
15062
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063 mode = get_map_mode(&which, 0);
15064
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015065 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015066 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015068
15069 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015071 /* Return a string. */
15072 if (rhs != NULL)
15073 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074
Bram Moolenaarbd743252010-10-20 21:23:33 +020015075 }
15076 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15077 {
15078 /* Return a dictionary. */
15079 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15080 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15081 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082
Bram Moolenaarbd743252010-10-20 21:23:33 +020015083 dict_add_nr_str(dict, "lhs", 0L, lhs);
15084 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15085 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15086 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15087 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15088 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15089 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015090 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015091 dict_add_nr_str(dict, "mode", 0L, mapmode);
15092
15093 vim_free(lhs);
15094 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 }
15096}
15097
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015098#ifdef FEAT_FLOAT
15099/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015100 * "log()" function
15101 */
15102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015103f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015104{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015105 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015106
15107 rettv->v_type = VAR_FLOAT;
15108 if (get_float_arg(argvars, &f) == OK)
15109 rettv->vval.v_float = log(f);
15110 else
15111 rettv->vval.v_float = 0.0;
15112}
15113
15114/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015115 * "log10()" function
15116 */
15117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015118f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015119{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015120 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015121
15122 rettv->v_type = VAR_FLOAT;
15123 if (get_float_arg(argvars, &f) == OK)
15124 rettv->vval.v_float = log10(f);
15125 else
15126 rettv->vval.v_float = 0.0;
15127}
15128#endif
15129
Bram Moolenaar1dced572012-04-05 16:54:08 +020015130#ifdef FEAT_LUA
15131/*
15132 * "luaeval()" function
15133 */
15134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015135f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015136{
15137 char_u *str;
15138 char_u buf[NUMBUFLEN];
15139
15140 str = get_tv_string_buf(&argvars[0], buf);
15141 do_luaeval(str, argvars + 1, rettv);
15142}
15143#endif
15144
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015146 * "map()" function
15147 */
15148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015149f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015150{
15151 filter_map(argvars, rettv, TRUE);
15152}
15153
15154/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015155 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156 */
15157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015158f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015159{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161}
15162
15163/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015164 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015165 */
15166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015167f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015168{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015169 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015170}
15171
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015172static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173
15174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015175find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015177 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015178 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015179 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180 char_u *pat;
15181 regmatch_T regmatch;
15182 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015183 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184 char_u *save_cpo;
15185 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015186 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015187 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015188 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015189 list_T *l = NULL;
15190 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015191 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015192 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193
15194 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15195 save_cpo = p_cpo;
15196 p_cpo = (char_u *)"";
15197
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015198 rettv->vval.v_number = -1;
15199 if (type == 3)
15200 {
15201 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015202 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015203 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015204 }
15205 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015207 rettv->v_type = VAR_STRING;
15208 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015211 if (argvars[0].v_type == VAR_LIST)
15212 {
15213 if ((l = argvars[0].vval.v_list) == NULL)
15214 goto theend;
15215 li = l->lv_first;
15216 }
15217 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015218 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015219 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015220 len = (long)STRLEN(str);
15221 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015223 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15224 if (pat == NULL)
15225 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015226
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015227 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229 int error = FALSE;
15230
15231 start = get_tv_number_chk(&argvars[2], &error);
15232 if (error)
15233 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015234 if (l != NULL)
15235 {
15236 li = list_find(l, start);
15237 if (li == NULL)
15238 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015239 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015240 }
15241 else
15242 {
15243 if (start < 0)
15244 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015245 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015246 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015247 /* When "count" argument is there ignore matches before "start",
15248 * otherwise skip part of the string. Differs when pattern is "^"
15249 * or "\<". */
15250 if (argvars[3].v_type != VAR_UNKNOWN)
15251 startcol = start;
15252 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015253 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015254 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015255 len -= start;
15256 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015257 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015258
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015259 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015260 nth = get_tv_number_chk(&argvars[3], &error);
15261 if (error)
15262 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 }
15264
15265 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15266 if (regmatch.regprog != NULL)
15267 {
15268 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015269
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015270 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015271 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015272 if (l != NULL)
15273 {
15274 if (li == NULL)
15275 {
15276 match = FALSE;
15277 break;
15278 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015279 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015280 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015281 if (str == NULL)
15282 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015283 }
15284
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015285 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015286
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015287 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015288 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015289 if (l == NULL && !match)
15290 break;
15291
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015292 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015293 if (l != NULL)
15294 {
15295 li = li->li_next;
15296 ++idx;
15297 }
15298 else
15299 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015300#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015301 startcol = (colnr_T)(regmatch.startp[0]
15302 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015303#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015304 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015305#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015306 if (startcol > (colnr_T)len
15307 || str + startcol <= regmatch.startp[0])
15308 {
15309 match = FALSE;
15310 break;
15311 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015312 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015313 }
15314
15315 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015317 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015318 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015319 int i;
15320
15321 /* return list with matched string and submatches */
15322 for (i = 0; i < NSUBEXP; ++i)
15323 {
15324 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015325 {
15326 if (list_append_string(rettv->vval.v_list,
15327 (char_u *)"", 0) == FAIL)
15328 break;
15329 }
15330 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015331 regmatch.startp[i],
15332 (int)(regmatch.endp[i] - regmatch.startp[i]))
15333 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015334 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015335 }
15336 }
15337 else if (type == 2)
15338 {
15339 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015340 if (l != NULL)
15341 copy_tv(&li->li_tv, rettv);
15342 else
15343 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015345 }
15346 else if (l != NULL)
15347 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348 else
15349 {
15350 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015351 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 (varnumber_T)(regmatch.startp[0] - str);
15353 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015354 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015355 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015356 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357 }
15358 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015359 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 }
15361
15362theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015363 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 p_cpo = save_cpo;
15365}
15366
15367/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015368 * "match()" function
15369 */
15370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015371f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015372{
15373 find_some_match(argvars, rettv, 1);
15374}
15375
15376/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015377 * "matchadd()" function
15378 */
15379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015380f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015381{
15382#ifdef FEAT_SEARCH_EXTRA
15383 char_u buf[NUMBUFLEN];
15384 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15385 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15386 int prio = 10; /* default priority */
15387 int id = -1;
15388 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015389 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015390
15391 rettv->vval.v_number = -1;
15392
15393 if (grp == NULL || pat == NULL)
15394 return;
15395 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015396 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015397 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015398 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015399 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015400 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015401 if (argvars[4].v_type != VAR_UNKNOWN)
15402 {
15403 if (argvars[4].v_type != VAR_DICT)
15404 {
15405 EMSG(_(e_dictreq));
15406 return;
15407 }
15408 if (dict_find(argvars[4].vval.v_dict,
15409 (char_u *)"conceal", -1) != NULL)
15410 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15411 (char_u *)"conceal", FALSE);
15412 }
15413 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015414 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015415 if (error == TRUE)
15416 return;
15417 if (id >= 1 && id <= 3)
15418 {
15419 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15420 return;
15421 }
15422
Bram Moolenaar6561d522015-07-21 15:48:27 +020015423 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15424 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015425#endif
15426}
15427
15428/*
15429 * "matchaddpos()" function
15430 */
15431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015432f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015433{
15434#ifdef FEAT_SEARCH_EXTRA
15435 char_u buf[NUMBUFLEN];
15436 char_u *group;
15437 int prio = 10;
15438 int id = -1;
15439 int error = FALSE;
15440 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015441 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015442
15443 rettv->vval.v_number = -1;
15444
15445 group = get_tv_string_buf_chk(&argvars[0], buf);
15446 if (group == NULL)
15447 return;
15448
15449 if (argvars[1].v_type != VAR_LIST)
15450 {
15451 EMSG2(_(e_listarg), "matchaddpos()");
15452 return;
15453 }
15454 l = argvars[1].vval.v_list;
15455 if (l == NULL)
15456 return;
15457
15458 if (argvars[2].v_type != VAR_UNKNOWN)
15459 {
15460 prio = get_tv_number_chk(&argvars[2], &error);
15461 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015462 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015463 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015464 if (argvars[4].v_type != VAR_UNKNOWN)
15465 {
15466 if (argvars[4].v_type != VAR_DICT)
15467 {
15468 EMSG(_(e_dictreq));
15469 return;
15470 }
15471 if (dict_find(argvars[4].vval.v_dict,
15472 (char_u *)"conceal", -1) != NULL)
15473 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15474 (char_u *)"conceal", FALSE);
15475 }
15476 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015477 }
15478 if (error == TRUE)
15479 return;
15480
15481 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15482 if (id == 1 || id == 2)
15483 {
15484 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15485 return;
15486 }
15487
Bram Moolenaar6561d522015-07-21 15:48:27 +020015488 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15489 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015490#endif
15491}
15492
15493/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015494 * "matcharg()" function
15495 */
15496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015497f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015498{
15499 if (rettv_list_alloc(rettv) == OK)
15500 {
15501#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015502 int id = get_tv_number(&argvars[0]);
15503 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015504
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015505 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015506 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015507 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15508 {
15509 list_append_string(rettv->vval.v_list,
15510 syn_id2name(m->hlg_id), -1);
15511 list_append_string(rettv->vval.v_list, m->pattern, -1);
15512 }
15513 else
15514 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015515 list_append_string(rettv->vval.v_list, NULL, -1);
15516 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015517 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015518 }
15519#endif
15520 }
15521}
15522
15523/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015524 * "matchdelete()" function
15525 */
15526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015527f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015528{
15529#ifdef FEAT_SEARCH_EXTRA
15530 rettv->vval.v_number = match_delete(curwin,
15531 (int)get_tv_number(&argvars[0]), TRUE);
15532#endif
15533}
15534
15535/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015536 * "matchend()" function
15537 */
15538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015539f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015540{
15541 find_some_match(argvars, rettv, 0);
15542}
15543
15544/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015545 * "matchlist()" function
15546 */
15547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015548f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015549{
15550 find_some_match(argvars, rettv, 3);
15551}
15552
15553/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015554 * "matchstr()" function
15555 */
15556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015557f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015558{
15559 find_some_match(argvars, rettv, 2);
15560}
15561
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015562static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015563
15564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015565max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015566{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015567 long n = 0;
15568 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015569 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015570
15571 if (argvars[0].v_type == VAR_LIST)
15572 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015573 list_T *l;
15574 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015575
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015576 l = argvars[0].vval.v_list;
15577 if (l != NULL)
15578 {
15579 li = l->lv_first;
15580 if (li != NULL)
15581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015582 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015583 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015584 {
15585 li = li->li_next;
15586 if (li == NULL)
15587 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015588 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015589 if (domax ? i > n : i < n)
15590 n = i;
15591 }
15592 }
15593 }
15594 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015595 else if (argvars[0].v_type == VAR_DICT)
15596 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015597 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015598 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015599 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015600 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015601
15602 d = argvars[0].vval.v_dict;
15603 if (d != NULL)
15604 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015605 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015606 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015607 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015608 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015609 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015610 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015611 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015612 if (first)
15613 {
15614 n = i;
15615 first = FALSE;
15616 }
15617 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015618 n = i;
15619 }
15620 }
15621 }
15622 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015623 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015624 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015625 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015626}
15627
15628/*
15629 * "max()" function
15630 */
15631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015632f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015633{
15634 max_min(argvars, rettv, TRUE);
15635}
15636
15637/*
15638 * "min()" function
15639 */
15640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015641f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015642{
15643 max_min(argvars, rettv, FALSE);
15644}
15645
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015646static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015647
15648/*
15649 * Create the directory in which "dir" is located, and higher levels when
15650 * needed.
15651 */
15652 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015653mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015654{
15655 char_u *p;
15656 char_u *updir;
15657 int r = FAIL;
15658
15659 /* Get end of directory name in "dir".
15660 * We're done when it's "/" or "c:/". */
15661 p = gettail_sep(dir);
15662 if (p <= get_past_head(dir))
15663 return OK;
15664
15665 /* If the directory exists we're done. Otherwise: create it.*/
15666 updir = vim_strnsave(dir, (int)(p - dir));
15667 if (updir == NULL)
15668 return FAIL;
15669 if (mch_isdir(updir))
15670 r = OK;
15671 else if (mkdir_recurse(updir, prot) == OK)
15672 r = vim_mkdir_emsg(updir, prot);
15673 vim_free(updir);
15674 return r;
15675}
15676
15677#ifdef vim_mkdir
15678/*
15679 * "mkdir()" function
15680 */
15681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015682f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015683{
15684 char_u *dir;
15685 char_u buf[NUMBUFLEN];
15686 int prot = 0755;
15687
15688 rettv->vval.v_number = FAIL;
15689 if (check_restricted() || check_secure())
15690 return;
15691
15692 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015693 if (*dir == NUL)
15694 rettv->vval.v_number = FAIL;
15695 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015696 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015697 if (*gettail(dir) == NUL)
15698 /* remove trailing slashes */
15699 *gettail_sep(dir) = NUL;
15700
15701 if (argvars[1].v_type != VAR_UNKNOWN)
15702 {
15703 if (argvars[2].v_type != VAR_UNKNOWN)
15704 prot = get_tv_number_chk(&argvars[2], NULL);
15705 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15706 mkdir_recurse(dir, prot);
15707 }
15708 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015709 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015710}
15711#endif
15712
Bram Moolenaar0d660222005-01-07 21:51:51 +000015713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714 * "mode()" function
15715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015717f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015719 char_u buf[3];
15720
15721 buf[1] = NUL;
15722 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 if (VIsual_active)
15725 {
15726 if (VIsual_select)
15727 buf[0] = VIsual_mode + 's' - 'v';
15728 else
15729 buf[0] = VIsual_mode;
15730 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015731 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015732 || State == CONFIRM)
15733 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015735 if (State == ASKMORE)
15736 buf[1] = 'm';
15737 else if (State == CONFIRM)
15738 buf[1] = '?';
15739 }
15740 else if (State == EXTERNCMD)
15741 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 else if (State & INSERT)
15743 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015744#ifdef FEAT_VREPLACE
15745 if (State & VREPLACE_FLAG)
15746 {
15747 buf[0] = 'R';
15748 buf[1] = 'v';
15749 }
15750 else
15751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 if (State & REPLACE_FLAG)
15753 buf[0] = 'R';
15754 else
15755 buf[0] = 'i';
15756 }
15757 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015759 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015760 if (exmode_active)
15761 buf[1] = 'v';
15762 }
15763 else if (exmode_active)
15764 {
15765 buf[0] = 'c';
15766 buf[1] = 'e';
15767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015770 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015771 if (finish_op)
15772 buf[1] = 'o';
15773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015775 /* Clear out the minor mode when the argument is not a non-zero number or
15776 * non-empty string. */
15777 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015778 buf[1] = NUL;
15779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015780 rettv->vval.v_string = vim_strsave(buf);
15781 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782}
15783
Bram Moolenaar429fa852013-04-15 12:27:36 +020015784#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015785/*
15786 * "mzeval()" function
15787 */
15788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015789f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015790{
15791 char_u *str;
15792 char_u buf[NUMBUFLEN];
15793
15794 str = get_tv_string_buf(&argvars[0], buf);
15795 do_mzeval(str, rettv);
15796}
Bram Moolenaar75676462013-01-30 14:55:42 +010015797
15798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015799mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015800{
15801 typval_T argvars[3];
15802
15803 argvars[0].v_type = VAR_STRING;
15804 argvars[0].vval.v_string = name;
15805 copy_tv(args, &argvars[1]);
15806 argvars[2].v_type = VAR_UNKNOWN;
15807 f_call(argvars, rettv);
15808 clear_tv(&argvars[1]);
15809}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015810#endif
15811
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015813 * "nextnonblank()" function
15814 */
15815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015816f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015817{
15818 linenr_T lnum;
15819
15820 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015822 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015823 {
15824 lnum = 0;
15825 break;
15826 }
15827 if (*skipwhite(ml_get(lnum)) != NUL)
15828 break;
15829 }
15830 rettv->vval.v_number = lnum;
15831}
15832
15833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834 * "nr2char()" function
15835 */
15836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015837f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838{
15839 char_u buf[NUMBUFLEN];
15840
15841#ifdef FEAT_MBYTE
15842 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015843 {
15844 int utf8 = 0;
15845
15846 if (argvars[1].v_type != VAR_UNKNOWN)
15847 utf8 = get_tv_number_chk(&argvars[1], NULL);
15848 if (utf8)
15849 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15850 else
15851 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853 else
15854#endif
15855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015856 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 buf[1] = NUL;
15858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015859 rettv->v_type = VAR_STRING;
15860 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015861}
15862
15863/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015864 * "or(expr, expr)" function
15865 */
15866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015867f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015868{
15869 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15870 | get_tv_number_chk(&argvars[1], NULL);
15871}
15872
15873/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015874 * "pathshorten()" function
15875 */
15876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015877f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015878{
15879 char_u *p;
15880
15881 rettv->v_type = VAR_STRING;
15882 p = get_tv_string_chk(&argvars[0]);
15883 if (p == NULL)
15884 rettv->vval.v_string = NULL;
15885 else
15886 {
15887 p = vim_strsave(p);
15888 rettv->vval.v_string = p;
15889 if (p != NULL)
15890 shorten_dir(p);
15891 }
15892}
15893
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015894#ifdef FEAT_PERL
15895/*
15896 * "perleval()" function
15897 */
15898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015899f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015900{
15901 char_u *str;
15902 char_u buf[NUMBUFLEN];
15903
15904 str = get_tv_string_buf(&argvars[0], buf);
15905 do_perleval(str, rettv);
15906}
15907#endif
15908
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015909#ifdef FEAT_FLOAT
15910/*
15911 * "pow()" function
15912 */
15913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015914f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015915{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015916 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015917
15918 rettv->v_type = VAR_FLOAT;
15919 if (get_float_arg(argvars, &fx) == OK
15920 && get_float_arg(&argvars[1], &fy) == OK)
15921 rettv->vval.v_float = pow(fx, fy);
15922 else
15923 rettv->vval.v_float = 0.0;
15924}
15925#endif
15926
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015927/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015928 * "prevnonblank()" function
15929 */
15930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015931f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015932{
15933 linenr_T lnum;
15934
15935 lnum = get_tv_lnum(argvars);
15936 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15937 lnum = 0;
15938 else
15939 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15940 --lnum;
15941 rettv->vval.v_number = lnum;
15942}
15943
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015944/* This dummy va_list is here because:
15945 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15946 * - locally in the function results in a "used before set" warning
15947 * - using va_start() to initialize it gives "function with fixed args" error */
15948static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015949
Bram Moolenaar8c711452005-01-14 21:53:12 +000015950/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015951 * "printf()" function
15952 */
15953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015954f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015955{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015956 char_u buf[NUMBUFLEN];
15957 int len;
15958 char_u *s;
15959 int saved_did_emsg = did_emsg;
15960 char *fmt;
15961
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015962 rettv->v_type = VAR_STRING;
15963 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015964
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015965 /* Get the required length, allocate the buffer and do it for real. */
15966 did_emsg = FALSE;
15967 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15968 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15969 if (!did_emsg)
15970 {
15971 s = alloc(len + 1);
15972 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015973 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015974 rettv->vval.v_string = s;
15975 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015976 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015977 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015978 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015979}
15980
15981/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015982 * "pumvisible()" function
15983 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015985f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015986{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015987#ifdef FEAT_INS_EXPAND
15988 if (pum_visible())
15989 rettv->vval.v_number = 1;
15990#endif
15991}
15992
Bram Moolenaardb913952012-06-29 12:54:53 +020015993#ifdef FEAT_PYTHON3
15994/*
15995 * "py3eval()" function
15996 */
15997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015998f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015999{
16000 char_u *str;
16001 char_u buf[NUMBUFLEN];
16002
16003 str = get_tv_string_buf(&argvars[0], buf);
16004 do_py3eval(str, rettv);
16005}
16006#endif
16007
16008#ifdef FEAT_PYTHON
16009/*
16010 * "pyeval()" function
16011 */
16012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016013f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016014{
16015 char_u *str;
16016 char_u buf[NUMBUFLEN];
16017
16018 str = get_tv_string_buf(&argvars[0], buf);
16019 do_pyeval(str, rettv);
16020}
16021#endif
16022
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016023/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016024 * "range()" function
16025 */
16026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016027f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016028{
16029 long start;
16030 long end;
16031 long stride = 1;
16032 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016033 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016035 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016036 if (argvars[1].v_type == VAR_UNKNOWN)
16037 {
16038 end = start - 1;
16039 start = 0;
16040 }
16041 else
16042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016043 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016044 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016045 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016046 }
16047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016048 if (error)
16049 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016050 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016051 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016052 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016053 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016054 else
16055 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016056 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016057 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016058 if (list_append_number(rettv->vval.v_list,
16059 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016060 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016061 }
16062}
16063
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016064/*
16065 * "readfile()" function
16066 */
16067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016068f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016069{
16070 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016071 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016072 char_u *fname;
16073 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016074 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16075 int io_size = sizeof(buf);
16076 int readlen; /* size of last fread() */
16077 char_u *prev = NULL; /* previously read bytes, if any */
16078 long prevlen = 0; /* length of data in prev */
16079 long prevsize = 0; /* size of prev buffer */
16080 long maxline = MAXLNUM;
16081 long cnt = 0;
16082 char_u *p; /* position in buf */
16083 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016084
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016085 if (argvars[1].v_type != VAR_UNKNOWN)
16086 {
16087 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16088 binary = TRUE;
16089 if (argvars[2].v_type != VAR_UNKNOWN)
16090 maxline = get_tv_number(&argvars[2]);
16091 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016092
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016093 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016094 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016095
16096 /* Always open the file in binary mode, library functions have a mind of
16097 * their own about CR-LF conversion. */
16098 fname = get_tv_string(&argvars[0]);
16099 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16100 {
16101 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16102 return;
16103 }
16104
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016105 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016106 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016107 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016108
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016109 /* This for loop processes what was read, but is also entered at end
16110 * of file so that either:
16111 * - an incomplete line gets written
16112 * - a "binary" file gets an empty line at the end if it ends in a
16113 * newline. */
16114 for (p = buf, start = buf;
16115 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16116 ++p)
16117 {
16118 if (*p == '\n' || readlen <= 0)
16119 {
16120 listitem_T *li;
16121 char_u *s = NULL;
16122 long_u len = p - start;
16123
16124 /* Finished a line. Remove CRs before NL. */
16125 if (readlen > 0 && !binary)
16126 {
16127 while (len > 0 && start[len - 1] == '\r')
16128 --len;
16129 /* removal may cross back to the "prev" string */
16130 if (len == 0)
16131 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16132 --prevlen;
16133 }
16134 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016135 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016136 else
16137 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016138 /* Change "prev" buffer to be the right size. This way
16139 * the bytes are only copied once, and very long lines are
16140 * allocated only once. */
16141 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016142 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016143 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016144 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016145 prev = NULL; /* the list will own the string */
16146 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016147 }
16148 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016149 if (s == NULL)
16150 {
16151 do_outofmem_msg((long_u) prevlen + len + 1);
16152 failed = TRUE;
16153 break;
16154 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016155
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016156 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016157 {
16158 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016159 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016160 break;
16161 }
16162 li->li_tv.v_type = VAR_STRING;
16163 li->li_tv.v_lock = 0;
16164 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016165 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016166
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016167 start = p + 1; /* step over newline */
16168 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016169 break;
16170 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016171 else if (*p == NUL)
16172 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016173#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016174 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16175 * when finding the BF and check the previous two bytes. */
16176 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016177 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016178 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16179 * + 1, these may be in the "prev" string. */
16180 char_u back1 = p >= buf + 1 ? p[-1]
16181 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16182 char_u back2 = p >= buf + 2 ? p[-2]
16183 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16184 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016185
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016186 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016187 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016188 char_u *dest = p - 2;
16189
16190 /* Usually a BOM is at the beginning of a file, and so at
16191 * the beginning of a line; then we can just step over it.
16192 */
16193 if (start == dest)
16194 start = p + 1;
16195 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016196 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016197 /* have to shuffle buf to close gap */
16198 int adjust_prevlen = 0;
16199
16200 if (dest < buf)
16201 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016202 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016203 dest = buf;
16204 }
16205 if (readlen > p - buf + 1)
16206 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16207 readlen -= 3 - adjust_prevlen;
16208 prevlen -= adjust_prevlen;
16209 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016210 }
16211 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016212 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016213#endif
16214 } /* for */
16215
16216 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16217 break;
16218 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016219 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016220 /* There's part of a line in buf, store it in "prev". */
16221 if (p - start + prevlen >= prevsize)
16222 {
16223 /* need bigger "prev" buffer */
16224 char_u *newprev;
16225
16226 /* A common use case is ordinary text files and "prev" gets a
16227 * fragment of a line, so the first allocation is made
16228 * small, to avoid repeatedly 'allocing' large and
16229 * 'reallocing' small. */
16230 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016231 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016232 else
16233 {
16234 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016235 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016236 prevsize = grow50pc > growmin ? grow50pc : growmin;
16237 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016238 newprev = prev == NULL ? alloc(prevsize)
16239 : vim_realloc(prev, prevsize);
16240 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016241 {
16242 do_outofmem_msg((long_u)prevsize);
16243 failed = TRUE;
16244 break;
16245 }
16246 prev = newprev;
16247 }
16248 /* Add the line part to end of "prev". */
16249 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016250 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016251 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016252 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016253
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016254 /*
16255 * For a negative line count use only the lines at the end of the file,
16256 * free the rest.
16257 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016258 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016259 while (cnt > -maxline)
16260 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016261 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016262 --cnt;
16263 }
16264
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016265 if (failed)
16266 {
16267 list_free(rettv->vval.v_list, TRUE);
16268 /* readfile doc says an empty list is returned on error */
16269 rettv->vval.v_list = list_alloc();
16270 }
16271
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016272 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016273 fclose(fd);
16274}
16275
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016276#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016277static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016278
16279/*
16280 * Convert a List to proftime_T.
16281 * Return FAIL when there is something wrong.
16282 */
16283 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016284list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016285{
16286 long n1, n2;
16287 int error = FALSE;
16288
16289 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16290 || arg->vval.v_list->lv_len != 2)
16291 return FAIL;
16292 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16293 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16294# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016295 tm->HighPart = n1;
16296 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016297# else
16298 tm->tv_sec = n1;
16299 tm->tv_usec = n2;
16300# endif
16301 return error ? FAIL : OK;
16302}
16303#endif /* FEAT_RELTIME */
16304
16305/*
16306 * "reltime()" function
16307 */
16308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016309f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016310{
16311#ifdef FEAT_RELTIME
16312 proftime_T res;
16313 proftime_T start;
16314
16315 if (argvars[0].v_type == VAR_UNKNOWN)
16316 {
16317 /* No arguments: get current time. */
16318 profile_start(&res);
16319 }
16320 else if (argvars[1].v_type == VAR_UNKNOWN)
16321 {
16322 if (list2proftime(&argvars[0], &res) == FAIL)
16323 return;
16324 profile_end(&res);
16325 }
16326 else
16327 {
16328 /* Two arguments: compute the difference. */
16329 if (list2proftime(&argvars[0], &start) == FAIL
16330 || list2proftime(&argvars[1], &res) == FAIL)
16331 return;
16332 profile_sub(&res, &start);
16333 }
16334
16335 if (rettv_list_alloc(rettv) == OK)
16336 {
16337 long n1, n2;
16338
16339# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016340 n1 = res.HighPart;
16341 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016342# else
16343 n1 = res.tv_sec;
16344 n2 = res.tv_usec;
16345# endif
16346 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16347 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16348 }
16349#endif
16350}
16351
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016352#ifdef FEAT_FLOAT
16353/*
16354 * "reltimefloat()" function
16355 */
16356 static void
16357f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16358{
16359# ifdef FEAT_RELTIME
16360 proftime_T tm;
16361# endif
16362
16363 rettv->v_type = VAR_FLOAT;
16364 rettv->vval.v_float = 0;
16365# ifdef FEAT_RELTIME
16366 if (list2proftime(&argvars[0], &tm) == OK)
16367 rettv->vval.v_float = profile_float(&tm);
16368# endif
16369}
16370#endif
16371
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016372/*
16373 * "reltimestr()" function
16374 */
16375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016376f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016377{
16378#ifdef FEAT_RELTIME
16379 proftime_T tm;
16380#endif
16381
16382 rettv->v_type = VAR_STRING;
16383 rettv->vval.v_string = NULL;
16384#ifdef FEAT_RELTIME
16385 if (list2proftime(&argvars[0], &tm) == OK)
16386 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16387#endif
16388}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016389
Bram Moolenaar0d660222005-01-07 21:51:51 +000016390#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016391static void make_connection(void);
16392static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016393
16394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016395make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016396{
16397 if (X_DISPLAY == NULL
16398# ifdef FEAT_GUI
16399 && !gui.in_use
16400# endif
16401 )
16402 {
16403 x_force_connect = TRUE;
16404 setup_term_clip();
16405 x_force_connect = FALSE;
16406 }
16407}
16408
16409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016410check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016411{
16412 make_connection();
16413 if (X_DISPLAY == NULL)
16414 {
16415 EMSG(_("E240: No connection to Vim server"));
16416 return FAIL;
16417 }
16418 return OK;
16419}
16420#endif
16421
16422#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016423static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424
16425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016426remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427{
16428 char_u *server_name;
16429 char_u *keys;
16430 char_u *r = NULL;
16431 char_u buf[NUMBUFLEN];
16432# ifdef WIN32
16433 HWND w;
16434# else
16435 Window w;
16436# endif
16437
16438 if (check_restricted() || check_secure())
16439 return;
16440
16441# ifdef FEAT_X11
16442 if (check_connection() == FAIL)
16443 return;
16444# endif
16445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016446 server_name = get_tv_string_chk(&argvars[0]);
16447 if (server_name == NULL)
16448 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016449 keys = get_tv_string_buf(&argvars[1], buf);
16450# ifdef WIN32
16451 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16452# else
16453 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16454 < 0)
16455# endif
16456 {
16457 if (r != NULL)
16458 EMSG(r); /* sending worked but evaluation failed */
16459 else
16460 EMSG2(_("E241: Unable to send to %s"), server_name);
16461 return;
16462 }
16463
16464 rettv->vval.v_string = r;
16465
16466 if (argvars[2].v_type != VAR_UNKNOWN)
16467 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016468 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016469 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016470 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016471
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016472 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016473 v.di_tv.v_type = VAR_STRING;
16474 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016475 idvar = get_tv_string_chk(&argvars[2]);
16476 if (idvar != NULL)
16477 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016478 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016479 }
16480}
16481#endif
16482
16483/*
16484 * "remote_expr()" function
16485 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016487f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016488{
16489 rettv->v_type = VAR_STRING;
16490 rettv->vval.v_string = NULL;
16491#ifdef FEAT_CLIENTSERVER
16492 remote_common(argvars, rettv, TRUE);
16493#endif
16494}
16495
16496/*
16497 * "remote_foreground()" function
16498 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016500f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016501{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016502#ifdef FEAT_CLIENTSERVER
16503# ifdef WIN32
16504 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016505 {
16506 char_u *server_name = get_tv_string_chk(&argvars[0]);
16507
16508 if (server_name != NULL)
16509 serverForeground(server_name);
16510 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016511# else
16512 /* Send a foreground() expression to the server. */
16513 argvars[1].v_type = VAR_STRING;
16514 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16515 argvars[2].v_type = VAR_UNKNOWN;
16516 remote_common(argvars, rettv, TRUE);
16517 vim_free(argvars[1].vval.v_string);
16518# endif
16519#endif
16520}
16521
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016523f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524{
16525#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016526 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016527 char_u *s = NULL;
16528# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016529 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016530# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532
16533 if (check_restricted() || check_secure())
16534 {
16535 rettv->vval.v_number = -1;
16536 return;
16537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016538 serverid = get_tv_string_chk(&argvars[0]);
16539 if (serverid == NULL)
16540 {
16541 rettv->vval.v_number = -1;
16542 return; /* type error; errmsg already given */
16543 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016544# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016545 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016546 if (n == 0)
16547 rettv->vval.v_number = -1;
16548 else
16549 {
16550 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16551 rettv->vval.v_number = (s != NULL);
16552 }
16553# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016554 if (check_connection() == FAIL)
16555 return;
16556
16557 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016558 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016559# endif
16560
16561 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016563 char_u *retvar;
16564
Bram Moolenaar33570922005-01-25 22:26:29 +000016565 v.di_tv.v_type = VAR_STRING;
16566 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016567 retvar = get_tv_string_chk(&argvars[1]);
16568 if (retvar != NULL)
16569 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016570 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016571 }
16572#else
16573 rettv->vval.v_number = -1;
16574#endif
16575}
16576
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016578f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016579{
16580 char_u *r = NULL;
16581
16582#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016583 char_u *serverid = get_tv_string_chk(&argvars[0]);
16584
16585 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016586 {
16587# ifdef WIN32
16588 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016589 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016590
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016591 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016592 if (n != 0)
16593 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16594 if (r == NULL)
16595# else
16596 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016597 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016598# endif
16599 EMSG(_("E277: Unable to read a server reply"));
16600 }
16601#endif
16602 rettv->v_type = VAR_STRING;
16603 rettv->vval.v_string = r;
16604}
16605
16606/*
16607 * "remote_send()" function
16608 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016610f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016611{
16612 rettv->v_type = VAR_STRING;
16613 rettv->vval.v_string = NULL;
16614#ifdef FEAT_CLIENTSERVER
16615 remote_common(argvars, rettv, FALSE);
16616#endif
16617}
16618
16619/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016620 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016621 */
16622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016623f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016624{
Bram Moolenaar33570922005-01-25 22:26:29 +000016625 list_T *l;
16626 listitem_T *item, *item2;
16627 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016628 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016629 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016630 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016631 dict_T *d;
16632 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016633 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016634
Bram Moolenaar8c711452005-01-14 21:53:12 +000016635 if (argvars[0].v_type == VAR_DICT)
16636 {
16637 if (argvars[2].v_type != VAR_UNKNOWN)
16638 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016639 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016640 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016642 key = get_tv_string_chk(&argvars[1]);
16643 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016644 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016645 di = dict_find(d, key, -1);
16646 if (di == NULL)
16647 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016648 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16649 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016650 {
16651 *rettv = di->di_tv;
16652 init_tv(&di->di_tv);
16653 dictitem_remove(d, di);
16654 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016656 }
16657 }
16658 else if (argvars[0].v_type != VAR_LIST)
16659 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016660 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016661 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016663 int error = FALSE;
16664
16665 idx = get_tv_number_chk(&argvars[1], &error);
16666 if (error)
16667 ; /* type error: do nothing, errmsg already given */
16668 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016669 EMSGN(_(e_listidx), idx);
16670 else
16671 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016672 if (argvars[2].v_type == VAR_UNKNOWN)
16673 {
16674 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016675 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016676 *rettv = item->li_tv;
16677 vim_free(item);
16678 }
16679 else
16680 {
16681 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016682 end = get_tv_number_chk(&argvars[2], &error);
16683 if (error)
16684 ; /* type error: do nothing */
16685 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016686 EMSGN(_(e_listidx), end);
16687 else
16688 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016689 int cnt = 0;
16690
16691 for (li = item; li != NULL; li = li->li_next)
16692 {
16693 ++cnt;
16694 if (li == item2)
16695 break;
16696 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016697 if (li == NULL) /* didn't find "item2" after "item" */
16698 EMSG(_(e_invrange));
16699 else
16700 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016701 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016702 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016703 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016704 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016705 l->lv_first = item;
16706 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016707 item->li_prev = NULL;
16708 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016709 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016710 }
16711 }
16712 }
16713 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016714 }
16715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716}
16717
16718/*
16719 * "rename({from}, {to})" function
16720 */
16721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016722f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723{
16724 char_u buf[NUMBUFLEN];
16725
16726 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016727 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016729 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16730 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731}
16732
16733/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016734 * "repeat()" function
16735 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016737f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016738{
16739 char_u *p;
16740 int n;
16741 int slen;
16742 int len;
16743 char_u *r;
16744 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016745
16746 n = get_tv_number(&argvars[1]);
16747 if (argvars[0].v_type == VAR_LIST)
16748 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016749 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016750 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016751 if (list_extend(rettv->vval.v_list,
16752 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016753 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016754 }
16755 else
16756 {
16757 p = get_tv_string(&argvars[0]);
16758 rettv->v_type = VAR_STRING;
16759 rettv->vval.v_string = NULL;
16760
16761 slen = (int)STRLEN(p);
16762 len = slen * n;
16763 if (len <= 0)
16764 return;
16765
16766 r = alloc(len + 1);
16767 if (r != NULL)
16768 {
16769 for (i = 0; i < n; i++)
16770 mch_memmove(r + i * slen, p, (size_t)slen);
16771 r[len] = NUL;
16772 }
16773
16774 rettv->vval.v_string = r;
16775 }
16776}
16777
16778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779 * "resolve()" function
16780 */
16781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016782f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783{
16784 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016785#ifdef HAVE_READLINK
16786 char_u *buf = NULL;
16787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016789 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790#ifdef FEAT_SHORTCUT
16791 {
16792 char_u *v = NULL;
16793
16794 v = mch_resolve_shortcut(p);
16795 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016796 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016798 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016799 }
16800#else
16801# ifdef HAVE_READLINK
16802 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 char_u *cpy;
16804 int len;
16805 char_u *remain = NULL;
16806 char_u *q;
16807 int is_relative_to_current = FALSE;
16808 int has_trailing_pathsep = FALSE;
16809 int limit = 100;
16810
16811 p = vim_strsave(p);
16812
16813 if (p[0] == '.' && (vim_ispathsep(p[1])
16814 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16815 is_relative_to_current = TRUE;
16816
16817 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016818 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016821 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823
16824 q = getnextcomp(p);
16825 if (*q != NUL)
16826 {
16827 /* Separate the first path component in "p", and keep the
16828 * remainder (beginning with the path separator). */
16829 remain = vim_strsave(q - 1);
16830 q[-1] = NUL;
16831 }
16832
Bram Moolenaard9462e32011-04-11 21:35:11 +020016833 buf = alloc(MAXPATHL + 1);
16834 if (buf == NULL)
16835 goto fail;
16836
Bram Moolenaar071d4272004-06-13 20:20:40 +000016837 for (;;)
16838 {
16839 for (;;)
16840 {
16841 len = readlink((char *)p, (char *)buf, MAXPATHL);
16842 if (len <= 0)
16843 break;
16844 buf[len] = NUL;
16845
16846 if (limit-- == 0)
16847 {
16848 vim_free(p);
16849 vim_free(remain);
16850 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016851 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 goto fail;
16853 }
16854
16855 /* Ensure that the result will have a trailing path separator
16856 * if the argument has one. */
16857 if (remain == NULL && has_trailing_pathsep)
16858 add_pathsep(buf);
16859
16860 /* Separate the first path component in the link value and
16861 * concatenate the remainders. */
16862 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16863 if (*q != NUL)
16864 {
16865 if (remain == NULL)
16866 remain = vim_strsave(q - 1);
16867 else
16868 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016869 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 if (cpy != NULL)
16871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016872 vim_free(remain);
16873 remain = cpy;
16874 }
16875 }
16876 q[-1] = NUL;
16877 }
16878
16879 q = gettail(p);
16880 if (q > p && *q == NUL)
16881 {
16882 /* Ignore trailing path separator. */
16883 q[-1] = NUL;
16884 q = gettail(p);
16885 }
16886 if (q > p && !mch_isFullName(buf))
16887 {
16888 /* symlink is relative to directory of argument */
16889 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16890 if (cpy != NULL)
16891 {
16892 STRCPY(cpy, p);
16893 STRCPY(gettail(cpy), buf);
16894 vim_free(p);
16895 p = cpy;
16896 }
16897 }
16898 else
16899 {
16900 vim_free(p);
16901 p = vim_strsave(buf);
16902 }
16903 }
16904
16905 if (remain == NULL)
16906 break;
16907
16908 /* Append the first path component of "remain" to "p". */
16909 q = getnextcomp(remain + 1);
16910 len = q - remain - (*q != NUL);
16911 cpy = vim_strnsave(p, STRLEN(p) + len);
16912 if (cpy != NULL)
16913 {
16914 STRNCAT(cpy, remain, len);
16915 vim_free(p);
16916 p = cpy;
16917 }
16918 /* Shorten "remain". */
16919 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016920 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921 else
16922 {
16923 vim_free(remain);
16924 remain = NULL;
16925 }
16926 }
16927
16928 /* If the result is a relative path name, make it explicitly relative to
16929 * the current directory if and only if the argument had this form. */
16930 if (!vim_ispathsep(*p))
16931 {
16932 if (is_relative_to_current
16933 && *p != NUL
16934 && !(p[0] == '.'
16935 && (p[1] == NUL
16936 || vim_ispathsep(p[1])
16937 || (p[1] == '.'
16938 && (p[2] == NUL
16939 || vim_ispathsep(p[2]))))))
16940 {
16941 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016942 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943 if (cpy != NULL)
16944 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945 vim_free(p);
16946 p = cpy;
16947 }
16948 }
16949 else if (!is_relative_to_current)
16950 {
16951 /* Strip leading "./". */
16952 q = p;
16953 while (q[0] == '.' && vim_ispathsep(q[1]))
16954 q += 2;
16955 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016956 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016957 }
16958 }
16959
16960 /* Ensure that the result will have no trailing path separator
16961 * if the argument had none. But keep "/" or "//". */
16962 if (!has_trailing_pathsep)
16963 {
16964 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016965 if (after_pathsep(p, q))
16966 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 }
16968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016969 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970 }
16971# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016972 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973# endif
16974#endif
16975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016976 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977
16978#ifdef HAVE_READLINK
16979fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016980 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016982 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983}
16984
16985/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987 */
16988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016989f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990{
Bram Moolenaar33570922005-01-25 22:26:29 +000016991 list_T *l;
16992 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994 if (argvars[0].v_type != VAR_LIST)
16995 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016996 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016997 && !tv_check_lock(l->lv_lock,
16998 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016999 {
17000 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017001 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017002 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017003 while (li != NULL)
17004 {
17005 ni = li->li_prev;
17006 list_append(l, li);
17007 li = ni;
17008 }
17009 rettv->vval.v_list = l;
17010 rettv->v_type = VAR_LIST;
17011 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017012 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014}
17015
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017016#define SP_NOMOVE 0x01 /* don't move cursor */
17017#define SP_REPEAT 0x02 /* repeat to find outer pair */
17018#define SP_RETCOUNT 0x04 /* return matchcount */
17019#define SP_SETPCMARK 0x08 /* set previous context mark */
17020#define SP_START 0x10 /* accept match at start position */
17021#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17022#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017023#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017024
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017025static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026
17027/*
17028 * Get flags for a search function.
17029 * Possibly sets "p_ws".
17030 * Returns BACKWARD, FORWARD or zero (for an error).
17031 */
17032 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017033get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034{
17035 int dir = FORWARD;
17036 char_u *flags;
17037 char_u nbuf[NUMBUFLEN];
17038 int mask;
17039
17040 if (varp->v_type != VAR_UNKNOWN)
17041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017042 flags = get_tv_string_buf_chk(varp, nbuf);
17043 if (flags == NULL)
17044 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017045 while (*flags != NUL)
17046 {
17047 switch (*flags)
17048 {
17049 case 'b': dir = BACKWARD; break;
17050 case 'w': p_ws = TRUE; break;
17051 case 'W': p_ws = FALSE; break;
17052 default: mask = 0;
17053 if (flagsp != NULL)
17054 switch (*flags)
17055 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017056 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017057 case 'e': mask = SP_END; break;
17058 case 'm': mask = SP_RETCOUNT; break;
17059 case 'n': mask = SP_NOMOVE; break;
17060 case 'p': mask = SP_SUBPAT; break;
17061 case 'r': mask = SP_REPEAT; break;
17062 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017063 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017064 }
17065 if (mask == 0)
17066 {
17067 EMSG2(_(e_invarg2), flags);
17068 dir = 0;
17069 }
17070 else
17071 *flagsp |= mask;
17072 }
17073 if (dir == 0)
17074 break;
17075 ++flags;
17076 }
17077 }
17078 return dir;
17079}
17080
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017082 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017084 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017085search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017087 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017088 char_u *pat;
17089 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017090 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017091 int save_p_ws = p_ws;
17092 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017093 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017094 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017095 proftime_T tm;
17096#ifdef FEAT_RELTIME
17097 long time_limit = 0;
17098#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017099 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017100 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017102 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017103 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017104 if (dir == 0)
17105 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017106 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017107 if (flags & SP_START)
17108 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017109 if (flags & SP_END)
17110 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017111 if (flags & SP_COLUMN)
17112 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017113
Bram Moolenaar76929292008-01-06 19:07:36 +000017114 /* Optional arguments: line number to stop searching and timeout. */
17115 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017116 {
17117 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17118 if (lnum_stop < 0)
17119 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017120#ifdef FEAT_RELTIME
17121 if (argvars[3].v_type != VAR_UNKNOWN)
17122 {
17123 time_limit = get_tv_number_chk(&argvars[3], NULL);
17124 if (time_limit < 0)
17125 goto theend;
17126 }
17127#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017128 }
17129
Bram Moolenaar76929292008-01-06 19:07:36 +000017130#ifdef FEAT_RELTIME
17131 /* Set the time limit, if there is one. */
17132 profile_setlimit(time_limit, &tm);
17133#endif
17134
Bram Moolenaar231334e2005-07-25 20:46:57 +000017135 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017136 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017137 * Check to make sure only those flags are set.
17138 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17139 * flags cannot be set. Check for that condition also.
17140 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017141 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017142 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017143 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017144 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017145 goto theend;
17146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017148 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017149 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017150 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017151 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017153 if (flags & SP_SUBPAT)
17154 retval = subpatnum;
17155 else
17156 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017157 if (flags & SP_SETPCMARK)
17158 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017160 if (match_pos != NULL)
17161 {
17162 /* Store the match cursor position */
17163 match_pos->lnum = pos.lnum;
17164 match_pos->col = pos.col + 1;
17165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 /* "/$" will put the cursor after the end of the line, may need to
17167 * correct that here */
17168 check_cursor();
17169 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017170
17171 /* If 'n' flag is used: restore cursor position. */
17172 if (flags & SP_NOMOVE)
17173 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017174 else
17175 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017176theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017178
17179 return retval;
17180}
17181
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017182#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017183
17184/*
17185 * round() is not in C90, use ceil() or floor() instead.
17186 */
17187 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017188vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017189{
17190 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17191}
17192
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017193/*
17194 * "round({float})" function
17195 */
17196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017197f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017198{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017199 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017200
17201 rettv->v_type = VAR_FLOAT;
17202 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017203 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017204 else
17205 rettv->vval.v_float = 0.0;
17206}
17207#endif
17208
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017209/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017210 * "screenattr()" function
17211 */
17212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017213f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017214{
17215 int row;
17216 int col;
17217 int c;
17218
17219 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17220 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17221 if (row < 0 || row >= screen_Rows
17222 || col < 0 || col >= screen_Columns)
17223 c = -1;
17224 else
17225 c = ScreenAttrs[LineOffset[row] + col];
17226 rettv->vval.v_number = c;
17227}
17228
17229/*
17230 * "screenchar()" function
17231 */
17232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017233f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017234{
17235 int row;
17236 int col;
17237 int off;
17238 int c;
17239
17240 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17241 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17242 if (row < 0 || row >= screen_Rows
17243 || col < 0 || col >= screen_Columns)
17244 c = -1;
17245 else
17246 {
17247 off = LineOffset[row] + col;
17248#ifdef FEAT_MBYTE
17249 if (enc_utf8 && ScreenLinesUC[off] != 0)
17250 c = ScreenLinesUC[off];
17251 else
17252#endif
17253 c = ScreenLines[off];
17254 }
17255 rettv->vval.v_number = c;
17256}
17257
17258/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017259 * "screencol()" function
17260 *
17261 * First column is 1 to be consistent with virtcol().
17262 */
17263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017264f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017265{
17266 rettv->vval.v_number = screen_screencol() + 1;
17267}
17268
17269/*
17270 * "screenrow()" function
17271 */
17272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017273f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017274{
17275 rettv->vval.v_number = screen_screenrow() + 1;
17276}
17277
17278/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017279 * "search()" function
17280 */
17281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017282f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017283{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017284 int flags = 0;
17285
17286 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287}
17288
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017290 * "searchdecl()" function
17291 */
17292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017293f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017294{
17295 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017296 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017297 int error = FALSE;
17298 char_u *name;
17299
17300 rettv->vval.v_number = 1; /* default: FAIL */
17301
17302 name = get_tv_string_chk(&argvars[0]);
17303 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017304 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017305 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017306 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17307 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17308 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017309 if (!error && name != NULL)
17310 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017311 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017312}
17313
17314/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017315 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017317 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017318searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319{
17320 char_u *spat, *mpat, *epat;
17321 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 int dir;
17324 int flags = 0;
17325 char_u nbuf1[NUMBUFLEN];
17326 char_u nbuf2[NUMBUFLEN];
17327 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017328 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017329 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017330 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017333 spat = get_tv_string_chk(&argvars[0]);
17334 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17335 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17336 if (spat == NULL || mpat == NULL || epat == NULL)
17337 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 /* Handle the optional fourth argument: flags */
17340 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017341 if (dir == 0)
17342 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017343
17344 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017345 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17346 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017347 if ((flags & (SP_END | SP_SUBPAT)) != 0
17348 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017349 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017350 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017351 goto theend;
17352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017354 /* Using 'r' implies 'W', otherwise it doesn't work. */
17355 if (flags & SP_REPEAT)
17356 p_ws = FALSE;
17357
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017358 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017359 if (argvars[3].v_type == VAR_UNKNOWN
17360 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361 skip = (char_u *)"";
17362 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017363 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017364 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017365 if (argvars[5].v_type != VAR_UNKNOWN)
17366 {
17367 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17368 if (lnum_stop < 0)
17369 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017370#ifdef FEAT_RELTIME
17371 if (argvars[6].v_type != VAR_UNKNOWN)
17372 {
17373 time_limit = get_tv_number_chk(&argvars[6], NULL);
17374 if (time_limit < 0)
17375 goto theend;
17376 }
17377#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017378 }
17379 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017380 if (skip == NULL)
17381 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017383 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017384 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017385
17386theend:
17387 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017388
17389 return retval;
17390}
17391
17392/*
17393 * "searchpair()" function
17394 */
17395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017396f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017397{
17398 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17399}
17400
17401/*
17402 * "searchpairpos()" function
17403 */
17404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017405f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017406{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017407 pos_T match_pos;
17408 int lnum = 0;
17409 int col = 0;
17410
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017411 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017412 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017413
17414 if (searchpair_cmn(argvars, &match_pos) > 0)
17415 {
17416 lnum = match_pos.lnum;
17417 col = match_pos.col;
17418 }
17419
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017420 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17421 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017422}
17423
17424/*
17425 * Search for a start/middle/end thing.
17426 * Used by searchpair(), see its documentation for the details.
17427 * Returns 0 or -1 for no match,
17428 */
17429 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017430do_searchpair(
17431 char_u *spat, /* start pattern */
17432 char_u *mpat, /* middle pattern */
17433 char_u *epat, /* end pattern */
17434 int dir, /* BACKWARD or FORWARD */
17435 char_u *skip, /* skip expression */
17436 int flags, /* SP_SETPCMARK and other SP_ values */
17437 pos_T *match_pos,
17438 linenr_T lnum_stop, /* stop at this line if not zero */
17439 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017440{
17441 char_u *save_cpo;
17442 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17443 long retval = 0;
17444 pos_T pos;
17445 pos_T firstpos;
17446 pos_T foundpos;
17447 pos_T save_cursor;
17448 pos_T save_pos;
17449 int n;
17450 int r;
17451 int nest = 1;
17452 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017453 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017454 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017455
17456 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17457 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017458 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017459
Bram Moolenaar76929292008-01-06 19:07:36 +000017460#ifdef FEAT_RELTIME
17461 /* Set the time limit, if there is one. */
17462 profile_setlimit(time_limit, &tm);
17463#endif
17464
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017465 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17466 * start/middle/end (pat3, for the top pair). */
17467 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17468 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17469 if (pat2 == NULL || pat3 == NULL)
17470 goto theend;
17471 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17472 if (*mpat == NUL)
17473 STRCPY(pat3, pat2);
17474 else
17475 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17476 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017477 if (flags & SP_START)
17478 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017479
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480 save_cursor = curwin->w_cursor;
17481 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017482 clearpos(&firstpos);
17483 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 pat = pat3;
17485 for (;;)
17486 {
17487 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017488 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17490 /* didn't find it or found the first match again: FAIL */
17491 break;
17492
17493 if (firstpos.lnum == 0)
17494 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017495 if (equalpos(pos, foundpos))
17496 {
17497 /* Found the same position again. Can happen with a pattern that
17498 * has "\zs" at the end and searching backwards. Advance one
17499 * character and try again. */
17500 if (dir == BACKWARD)
17501 decl(&pos);
17502 else
17503 incl(&pos);
17504 }
17505 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017507 /* clear the start flag to avoid getting stuck here */
17508 options &= ~SEARCH_START;
17509
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 /* If the skip pattern matches, ignore this match. */
17511 if (*skip != NUL)
17512 {
17513 save_pos = curwin->w_cursor;
17514 curwin->w_cursor = pos;
17515 r = eval_to_bool(skip, &err, NULL, FALSE);
17516 curwin->w_cursor = save_pos;
17517 if (err)
17518 {
17519 /* Evaluating {skip} caused an error, break here. */
17520 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017521 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522 break;
17523 }
17524 if (r)
17525 continue;
17526 }
17527
17528 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17529 {
17530 /* Found end when searching backwards or start when searching
17531 * forward: nested pair. */
17532 ++nest;
17533 pat = pat2; /* nested, don't search for middle */
17534 }
17535 else
17536 {
17537 /* Found end when searching forward or start when searching
17538 * backward: end of (nested) pair; or found middle in outer pair. */
17539 if (--nest == 1)
17540 pat = pat3; /* outer level, search for middle */
17541 }
17542
17543 if (nest == 0)
17544 {
17545 /* Found the match: return matchcount or line number. */
17546 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017547 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017549 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017550 if (flags & SP_SETPCMARK)
17551 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 curwin->w_cursor = pos;
17553 if (!(flags & SP_REPEAT))
17554 break;
17555 nest = 1; /* search for next unmatched */
17556 }
17557 }
17558
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017559 if (match_pos != NULL)
17560 {
17561 /* Store the match cursor position */
17562 match_pos->lnum = curwin->w_cursor.lnum;
17563 match_pos->col = curwin->w_cursor.col + 1;
17564 }
17565
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017567 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568 curwin->w_cursor = save_cursor;
17569
17570theend:
17571 vim_free(pat2);
17572 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017573 if (p_cpo == empty_option)
17574 p_cpo = save_cpo;
17575 else
17576 /* Darn, evaluating the {skip} expression changed the value. */
17577 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017578
17579 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580}
17581
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017582/*
17583 * "searchpos()" function
17584 */
17585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017586f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017587{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017588 pos_T match_pos;
17589 int lnum = 0;
17590 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017591 int n;
17592 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017593
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017594 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017595 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017596
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017597 n = search_cmn(argvars, &match_pos, &flags);
17598 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017599 {
17600 lnum = match_pos.lnum;
17601 col = match_pos.col;
17602 }
17603
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017604 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17605 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017606 if (flags & SP_SUBPAT)
17607 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017608}
17609
Bram Moolenaar0d660222005-01-07 21:51:51 +000017610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017611f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017612{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017613#ifdef FEAT_CLIENTSERVER
17614 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017615 char_u *server = get_tv_string_chk(&argvars[0]);
17616 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617
Bram Moolenaar0d660222005-01-07 21:51:51 +000017618 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017619 if (server == NULL || reply == NULL)
17620 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017621 if (check_restricted() || check_secure())
17622 return;
17623# ifdef FEAT_X11
17624 if (check_connection() == FAIL)
17625 return;
17626# endif
17627
17628 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017630 EMSG(_("E258: Unable to send to client"));
17631 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017633 rettv->vval.v_number = 0;
17634#else
17635 rettv->vval.v_number = -1;
17636#endif
17637}
17638
Bram Moolenaar0d660222005-01-07 21:51:51 +000017639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017640f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017641{
17642 char_u *r = NULL;
17643
17644#ifdef FEAT_CLIENTSERVER
17645# ifdef WIN32
17646 r = serverGetVimNames();
17647# else
17648 make_connection();
17649 if (X_DISPLAY != NULL)
17650 r = serverGetVimNames(X_DISPLAY);
17651# endif
17652#endif
17653 rettv->v_type = VAR_STRING;
17654 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655}
17656
17657/*
17658 * "setbufvar()" function
17659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017661f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662{
17663 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017666 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 char_u nbuf[NUMBUFLEN];
17668
17669 if (check_restricted() || check_secure())
17670 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017671 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17672 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017673 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 varp = &argvars[2];
17675
17676 if (buf != NULL && varname != NULL && varp != NULL)
17677 {
17678 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680
17681 if (*varname == '&')
17682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017683 long numval;
17684 char_u *strval;
17685 int error = FALSE;
17686
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017688 numval = get_tv_number_chk(varp, &error);
17689 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017690 if (!error && strval != NULL)
17691 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692 }
17693 else
17694 {
17695 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17696 if (bufvarname != NULL)
17697 {
17698 STRCPY(bufvarname, "b:");
17699 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017700 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 vim_free(bufvarname);
17702 }
17703 }
17704
17705 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708}
17709
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017711f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017712{
17713 dict_T *d;
17714 dictitem_T *di;
17715 char_u *csearch;
17716
17717 if (argvars[0].v_type != VAR_DICT)
17718 {
17719 EMSG(_(e_dictreq));
17720 return;
17721 }
17722
17723 if ((d = argvars[0].vval.v_dict) != NULL)
17724 {
17725 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17726 if (csearch != NULL)
17727 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017728#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017729 if (enc_utf8)
17730 {
17731 int pcc[MAX_MCO];
17732 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017733
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017734 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17735 }
17736 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017737#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017738 set_last_csearch(PTR2CHAR(csearch),
17739 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017740 }
17741
17742 di = dict_find(d, (char_u *)"forward", -1);
17743 if (di != NULL)
17744 set_csearch_direction(get_tv_number(&di->di_tv)
17745 ? FORWARD : BACKWARD);
17746
17747 di = dict_find(d, (char_u *)"until", -1);
17748 if (di != NULL)
17749 set_csearch_until(!!get_tv_number(&di->di_tv));
17750 }
17751}
17752
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753/*
17754 * "setcmdpos()" function
17755 */
17756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017757f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017759 int pos = (int)get_tv_number(&argvars[0]) - 1;
17760
17761 if (pos >= 0)
17762 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763}
17764
17765/*
17766 * "setline()" function
17767 */
17768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017769f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770{
17771 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017772 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017773 list_T *l = NULL;
17774 listitem_T *li = NULL;
17775 long added = 0;
17776 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017778 lnum = get_tv_lnum(&argvars[0]);
17779 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017781 l = argvars[1].vval.v_list;
17782 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017784 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017785 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017786
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017787 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017788 for (;;)
17789 {
17790 if (l != NULL)
17791 {
17792 /* list argument, get next string */
17793 if (li == NULL)
17794 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017795 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017796 li = li->li_next;
17797 }
17798
17799 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017800 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017801 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017802
17803 /* When coming here from Insert mode, sync undo, so that this can be
17804 * undone separately from what was previously inserted. */
17805 if (u_sync_once == 2)
17806 {
17807 u_sync_once = 1; /* notify that u_sync() was called */
17808 u_sync(TRUE);
17809 }
17810
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017811 if (lnum <= curbuf->b_ml.ml_line_count)
17812 {
17813 /* existing line, replace it */
17814 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17815 {
17816 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017817 if (lnum == curwin->w_cursor.lnum)
17818 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017819 rettv->vval.v_number = 0; /* OK */
17820 }
17821 }
17822 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17823 {
17824 /* lnum is one past the last line, append the line */
17825 ++added;
17826 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17827 rettv->vval.v_number = 0; /* OK */
17828 }
17829
17830 if (l == NULL) /* only one string argument */
17831 break;
17832 ++lnum;
17833 }
17834
17835 if (added > 0)
17836 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837}
17838
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017839static 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 +000017840
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017842 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017843 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017845set_qf_ll_list(
17846 win_T *wp UNUSED,
17847 typval_T *list_arg UNUSED,
17848 typval_T *action_arg UNUSED,
17849 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017850{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017851#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017852 char_u *act;
17853 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017854#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017855
Bram Moolenaar2641f772005-03-25 21:58:17 +000017856 rettv->vval.v_number = -1;
17857
17858#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017859 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017860 EMSG(_(e_listreq));
17861 else
17862 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017863 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017864
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017865 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017866 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017867 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017868 if (act == NULL)
17869 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017870 if (*act == 'a' || *act == 'r')
17871 action = *act;
17872 }
17873
Bram Moolenaar81484f42012-12-05 15:16:47 +010017874 if (l != NULL && set_errorlist(wp, l, action,
17875 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017876 rettv->vval.v_number = 0;
17877 }
17878#endif
17879}
17880
17881/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017882 * "setloclist()" function
17883 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017885f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017886{
17887 win_T *win;
17888
17889 rettv->vval.v_number = -1;
17890
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017891 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017892 if (win != NULL)
17893 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17894}
17895
17896/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017897 * "setmatches()" function
17898 */
17899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017900f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017901{
17902#ifdef FEAT_SEARCH_EXTRA
17903 list_T *l;
17904 listitem_T *li;
17905 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017906 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017907
17908 rettv->vval.v_number = -1;
17909 if (argvars[0].v_type != VAR_LIST)
17910 {
17911 EMSG(_(e_listreq));
17912 return;
17913 }
17914 if ((l = argvars[0].vval.v_list) != NULL)
17915 {
17916
17917 /* To some extent make sure that we are dealing with a list from
17918 * "getmatches()". */
17919 li = l->lv_first;
17920 while (li != NULL)
17921 {
17922 if (li->li_tv.v_type != VAR_DICT
17923 || (d = li->li_tv.vval.v_dict) == NULL)
17924 {
17925 EMSG(_(e_invarg));
17926 return;
17927 }
17928 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017929 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17930 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017931 && dict_find(d, (char_u *)"priority", -1) != NULL
17932 && dict_find(d, (char_u *)"id", -1) != NULL))
17933 {
17934 EMSG(_(e_invarg));
17935 return;
17936 }
17937 li = li->li_next;
17938 }
17939
17940 clear_matches(curwin);
17941 li = l->lv_first;
17942 while (li != NULL)
17943 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017944 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017945 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017946 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017947 char_u *group;
17948 int priority;
17949 int id;
17950 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017951
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017952 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017953 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17954 {
17955 if (s == NULL)
17956 {
17957 s = list_alloc();
17958 if (s == NULL)
17959 return;
17960 }
17961
17962 /* match from matchaddpos() */
17963 for (i = 1; i < 9; i++)
17964 {
17965 sprintf((char *)buf, (char *)"pos%d", i);
17966 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17967 {
17968 if (di->di_tv.v_type != VAR_LIST)
17969 return;
17970
17971 list_append_tv(s, &di->di_tv);
17972 s->lv_refcount++;
17973 }
17974 else
17975 break;
17976 }
17977 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017978
17979 group = get_dict_string(d, (char_u *)"group", FALSE);
17980 priority = (int)get_dict_number(d, (char_u *)"priority");
17981 id = (int)get_dict_number(d, (char_u *)"id");
17982 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17983 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17984 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017985 if (i == 0)
17986 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017987 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017988 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017989 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017990 }
17991 else
17992 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017993 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017994 list_unref(s);
17995 s = NULL;
17996 }
17997
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017998 li = li->li_next;
17999 }
18000 rettv->vval.v_number = 0;
18001 }
18002#endif
18003}
18004
18005/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018006 * "setpos()" function
18007 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018009f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018010{
18011 pos_T pos;
18012 int fnum;
18013 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018014 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018015
Bram Moolenaar08250432008-02-13 11:42:46 +000018016 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018017 name = get_tv_string_chk(argvars);
18018 if (name != NULL)
18019 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018020 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018021 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018022 if (--pos.col < 0)
18023 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018024 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018025 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018026 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018027 if (fnum == curbuf->b_fnum)
18028 {
18029 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018030 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018031 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018032 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018033 curwin->w_set_curswant = FALSE;
18034 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018035 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018036 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018037 }
18038 else
18039 EMSG(_(e_invarg));
18040 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018041 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18042 {
18043 /* set mark */
18044 if (setmark_pos(name[1], &pos, fnum) == OK)
18045 rettv->vval.v_number = 0;
18046 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018047 else
18048 EMSG(_(e_invarg));
18049 }
18050 }
18051}
18052
18053/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018054 * "setqflist()" function
18055 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018057f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018058{
18059 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18060}
18061
18062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 * "setreg()" function
18064 */
18065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018066f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067{
18068 int regname;
18069 char_u *strregname;
18070 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018071 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 int append;
18073 char_u yank_type;
18074 long block_len;
18075
18076 block_len = -1;
18077 yank_type = MAUTO;
18078 append = FALSE;
18079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018080 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018081 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018083 if (strregname == NULL)
18084 return; /* type error; errmsg already given */
18085 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 if (regname == 0 || regname == '@')
18087 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018089 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018091 stropt = get_tv_string_chk(&argvars[2]);
18092 if (stropt == NULL)
18093 return; /* type error */
18094 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 switch (*stropt)
18096 {
18097 case 'a': case 'A': /* append */
18098 append = TRUE;
18099 break;
18100 case 'v': case 'c': /* character-wise selection */
18101 yank_type = MCHAR;
18102 break;
18103 case 'V': case 'l': /* line-wise selection */
18104 yank_type = MLINE;
18105 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 case 'b': case Ctrl_V: /* block-wise selection */
18107 yank_type = MBLOCK;
18108 if (VIM_ISDIGIT(stropt[1]))
18109 {
18110 ++stropt;
18111 block_len = getdigits(&stropt) - 1;
18112 --stropt;
18113 }
18114 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 }
18116 }
18117
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018118 if (argvars[1].v_type == VAR_LIST)
18119 {
18120 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018121 char_u **allocval;
18122 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018123 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018124 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018125 int len = argvars[1].vval.v_list->lv_len;
18126 listitem_T *li;
18127
Bram Moolenaar7d647822014-04-05 21:28:56 +020018128 /* First half: use for pointers to result lines; second half: use for
18129 * pointers to allocated copies. */
18130 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018131 if (lstval == NULL)
18132 return;
18133 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018134 allocval = lstval + len + 2;
18135 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018136
18137 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18138 li = li->li_next)
18139 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018140 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018141 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018142 goto free_lstval;
18143 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018144 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018145 /* Need to make a copy, next get_tv_string_buf_chk() will
18146 * overwrite the string. */
18147 strval = vim_strsave(buf);
18148 if (strval == NULL)
18149 goto free_lstval;
18150 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018151 }
18152 *curval++ = strval;
18153 }
18154 *curval++ = NULL;
18155
18156 write_reg_contents_lst(regname, lstval, -1,
18157 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018158free_lstval:
18159 while (curallocval > allocval)
18160 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018161 vim_free(lstval);
18162 }
18163 else
18164 {
18165 strval = get_tv_string_chk(&argvars[1]);
18166 if (strval == NULL)
18167 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018168 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018170 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018171 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172}
18173
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018174/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018175 * "settabvar()" function
18176 */
18177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018178f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018179{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018180#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018181 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018182 tabpage_T *tp;
18183#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018184 char_u *varname, *tabvarname;
18185 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018186
18187 rettv->vval.v_number = 0;
18188
18189 if (check_restricted() || check_secure())
18190 return;
18191
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018192#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018193 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018194#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018195 varname = get_tv_string_chk(&argvars[1]);
18196 varp = &argvars[2];
18197
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018198 if (varname != NULL && varp != NULL
18199#ifdef FEAT_WINDOWS
18200 && tp != NULL
18201#endif
18202 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018203 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018204#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018205 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018206 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018207#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018208
18209 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18210 if (tabvarname != NULL)
18211 {
18212 STRCPY(tabvarname, "t:");
18213 STRCPY(tabvarname + 2, varname);
18214 set_var(tabvarname, varp, TRUE);
18215 vim_free(tabvarname);
18216 }
18217
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018218#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018219 /* Restore current tabpage */
18220 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018221 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018222#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018223 }
18224}
18225
18226/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018227 * "settabwinvar()" function
18228 */
18229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018230f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018231{
18232 setwinvar(argvars, rettv, 1);
18233}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234
18235/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018236 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018239f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018241 setwinvar(argvars, rettv, 0);
18242}
18243
18244/*
18245 * "setwinvar()" and "settabwinvar()" functions
18246 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018247
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018249setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018250{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251 win_T *win;
18252#ifdef FEAT_WINDOWS
18253 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018254 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018255 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256#endif
18257 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018258 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018260 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261
18262 if (check_restricted() || check_secure())
18263 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018264
18265#ifdef FEAT_WINDOWS
18266 if (off == 1)
18267 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18268 else
18269 tp = curtab;
18270#endif
18271 win = find_win_by_nr(&argvars[off], tp);
18272 varname = get_tv_string_chk(&argvars[off + 1]);
18273 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274
18275 if (win != NULL && varname != NULL && varp != NULL)
18276 {
18277#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018278 need_switch_win = !(tp == curtab && win == curwin);
18279 if (!need_switch_win
18280 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018283 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018285 long numval;
18286 char_u *strval;
18287 int error = FALSE;
18288
18289 ++varname;
18290 numval = get_tv_number_chk(varp, &error);
18291 strval = get_tv_string_buf_chk(varp, nbuf);
18292 if (!error && strval != NULL)
18293 set_option_value(varname, numval, strval, OPT_LOCAL);
18294 }
18295 else
18296 {
18297 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18298 if (winvarname != NULL)
18299 {
18300 STRCPY(winvarname, "w:");
18301 STRCPY(winvarname + 2, varname);
18302 set_var(winvarname, varp, TRUE);
18303 vim_free(winvarname);
18304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305 }
18306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018308 if (need_switch_win)
18309 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310#endif
18311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312}
18313
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018314#ifdef FEAT_CRYPT
18315/*
18316 * "sha256({string})" function
18317 */
18318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018319f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018320{
18321 char_u *p;
18322
18323 p = get_tv_string(&argvars[0]);
18324 rettv->vval.v_string = vim_strsave(
18325 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18326 rettv->v_type = VAR_STRING;
18327}
18328#endif /* FEAT_CRYPT */
18329
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018331 * "shellescape({string})" function
18332 */
18333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018334f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018335{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018336 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018337 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018338 rettv->v_type = VAR_STRING;
18339}
18340
18341/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018342 * shiftwidth() function
18343 */
18344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018345f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018346{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018347 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018348}
18349
18350/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018351 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 */
18353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018354f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018356 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357
Bram Moolenaar0d660222005-01-07 21:51:51 +000018358 p = get_tv_string(&argvars[0]);
18359 rettv->vval.v_string = vim_strsave(p);
18360 simplify_filename(rettv->vval.v_string); /* simplify in place */
18361 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362}
18363
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018364#ifdef FEAT_FLOAT
18365/*
18366 * "sin()" function
18367 */
18368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018369f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018370{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018371 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018372
18373 rettv->v_type = VAR_FLOAT;
18374 if (get_float_arg(argvars, &f) == OK)
18375 rettv->vval.v_float = sin(f);
18376 else
18377 rettv->vval.v_float = 0.0;
18378}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018379
18380/*
18381 * "sinh()" function
18382 */
18383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018384f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018385{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018386 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018387
18388 rettv->v_type = VAR_FLOAT;
18389 if (get_float_arg(argvars, &f) == OK)
18390 rettv->vval.v_float = sinh(f);
18391 else
18392 rettv->vval.v_float = 0.0;
18393}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018394#endif
18395
Bram Moolenaar0d660222005-01-07 21:51:51 +000018396static int
18397#ifdef __BORLANDC__
18398 _RTLENTRYF
18399#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018400 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018401static int
18402#ifdef __BORLANDC__
18403 _RTLENTRYF
18404#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018405 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018406
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018407/* struct used in the array that's given to qsort() */
18408typedef struct
18409{
18410 listitem_T *item;
18411 int idx;
18412} sortItem_T;
18413
Bram Moolenaar0d660222005-01-07 21:51:51 +000018414static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018415static int item_compare_numeric;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018416static int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018417#ifdef FEAT_FLOAT
18418static int item_compare_float;
18419#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018420static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018421static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018422static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018423static int item_compare_keep_zero;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018424static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018425#define ITEM_COMPARE_FAIL 999
18426
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018428 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018430 static int
18431#ifdef __BORLANDC__
18432_RTLENTRYF
18433#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018434item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018436 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018437 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018438 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018439 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018440 int res;
18441 char_u numbuf1[NUMBUFLEN];
18442 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018444 si1 = (sortItem_T *)s1;
18445 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018446 tv1 = &si1->item->li_tv;
18447 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018448
18449 if (item_compare_numbers)
18450 {
18451 long v1 = get_tv_number(tv1);
18452 long v2 = get_tv_number(tv2);
18453
18454 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18455 }
18456
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018457#ifdef FEAT_FLOAT
18458 if (item_compare_float)
18459 {
18460 float_T v1 = get_tv_float(tv1);
18461 float_T v2 = get_tv_float(tv2);
18462
18463 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18464 }
18465#endif
18466
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018467 /* tv2string() puts quotes around a string and allocates memory. Don't do
18468 * that for string variables. Use a single quote when comparing with a
18469 * non-string to do what the docs promise. */
18470 if (tv1->v_type == VAR_STRING)
18471 {
18472 if (tv2->v_type != VAR_STRING || item_compare_numeric)
18473 p1 = (char_u *)"'";
18474 else
18475 p1 = tv1->vval.v_string;
18476 }
18477 else
18478 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18479 if (tv2->v_type == VAR_STRING)
18480 {
18481 if (tv1->v_type != VAR_STRING || item_compare_numeric)
18482 p2 = (char_u *)"'";
18483 else
18484 p2 = tv2->vval.v_string;
18485 }
18486 else
18487 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018488 if (p1 == NULL)
18489 p1 = (char_u *)"";
18490 if (p2 == NULL)
18491 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020018492 if (!item_compare_numeric)
18493 {
18494 if (item_compare_ic)
18495 res = STRICMP(p1, p2);
18496 else
18497 res = STRCMP(p1, p2);
18498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018500 {
18501 double n1, n2;
18502 n1 = strtod((char *)p1, (char **)&p1);
18503 n2 = strtod((char *)p2, (char **)&p2);
18504 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18505 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018506
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018507 /* When the result would be zero, compare the item indexes. Makes the
18508 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018509 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018510 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018511
Bram Moolenaar0d660222005-01-07 21:51:51 +000018512 vim_free(tofree1);
18513 vim_free(tofree2);
18514 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515}
18516
18517 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018518#ifdef __BORLANDC__
18519_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018521item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018522{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018523 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018524 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018525 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018526 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018527 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018529 /* shortcut after failure in previous call; compare all items equal */
18530 if (item_compare_func_err)
18531 return 0;
18532
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018533 si1 = (sortItem_T *)s1;
18534 si2 = (sortItem_T *)s2;
18535
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018536 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018537 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018538 copy_tv(&si1->item->li_tv, &argv[0]);
18539 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018540
18541 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000018542 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018543 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
18544 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018545 clear_tv(&argv[0]);
18546 clear_tv(&argv[1]);
18547
18548 if (res == FAIL)
18549 res = ITEM_COMPARE_FAIL;
18550 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018551 res = get_tv_number_chk(&rettv, &item_compare_func_err);
18552 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018553 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018554 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018555
18556 /* When the result would be zero, compare the pointers themselves. Makes
18557 * the sort stable. */
18558 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018559 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018560
Bram Moolenaar0d660222005-01-07 21:51:51 +000018561 return res;
18562}
18563
18564/*
18565 * "sort({list})" function
18566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018568do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569{
Bram Moolenaar33570922005-01-25 22:26:29 +000018570 list_T *l;
18571 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018572 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018573 long len;
18574 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575
Bram Moolenaar0d660222005-01-07 21:51:51 +000018576 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018577 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578 else
18579 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018580 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018581 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018582 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18583 TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018584 return;
18585 rettv->vval.v_list = l;
18586 rettv->v_type = VAR_LIST;
18587 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588
Bram Moolenaar0d660222005-01-07 21:51:51 +000018589 len = list_len(l);
18590 if (len <= 1)
18591 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592
Bram Moolenaar0d660222005-01-07 21:51:51 +000018593 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018594 item_compare_numeric = FALSE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018595 item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018596#ifdef FEAT_FLOAT
18597 item_compare_float = FALSE;
18598#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000018599 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018600 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018601 if (argvars[1].v_type != VAR_UNKNOWN)
18602 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018603 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018604 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018605 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018606 else
18607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018608 int error = FALSE;
18609
18610 i = get_tv_number_chk(&argvars[1], &error);
18611 if (error)
18612 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018613 if (i == 1)
18614 item_compare_ic = TRUE;
18615 else
18616 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020018617 if (item_compare_func != NULL)
18618 {
18619 if (STRCMP(item_compare_func, "n") == 0)
18620 {
18621 item_compare_func = NULL;
18622 item_compare_numeric = TRUE;
18623 }
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018624 else if (STRCMP(item_compare_func, "N") == 0)
18625 {
18626 item_compare_func = NULL;
18627 item_compare_numbers = TRUE;
18628 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018629#ifdef FEAT_FLOAT
18630 else if (STRCMP(item_compare_func, "f") == 0)
18631 {
18632 item_compare_func = NULL;
18633 item_compare_float = TRUE;
18634 }
18635#endif
Bram Moolenaare8a34922014-06-25 17:31:09 +020018636 else if (STRCMP(item_compare_func, "i") == 0)
18637 {
18638 item_compare_func = NULL;
18639 item_compare_ic = TRUE;
18640 }
18641 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018642 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018643
18644 if (argvars[2].v_type != VAR_UNKNOWN)
18645 {
18646 /* optional third argument: {dict} */
18647 if (argvars[2].v_type != VAR_DICT)
18648 {
18649 EMSG(_(e_dictreq));
18650 return;
18651 }
18652 item_compare_selfdict = argvars[2].vval.v_dict;
18653 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655
Bram Moolenaar0d660222005-01-07 21:51:51 +000018656 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018657 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018658 if (ptrs == NULL)
18659 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660
Bram Moolenaar327aa022014-03-25 18:24:23 +010018661 i = 0;
18662 if (sort)
18663 {
18664 /* sort(): ptrs will be the list to sort */
18665 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018666 {
18667 ptrs[i].item = li;
18668 ptrs[i].idx = i;
18669 ++i;
18670 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018671
18672 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018673 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018674 /* test the compare function */
18675 if (item_compare_func != NULL
18676 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018677 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018678 EMSG(_("E702: Sort compare function failed"));
18679 else
18680 {
18681 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018682 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010018683 item_compare_func == NULL ? item_compare : item_compare2);
18684
18685 if (!item_compare_func_err)
18686 {
18687 /* Clear the List and append the items in sorted order. */
18688 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18689 l->lv_len = 0;
18690 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018691 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018692 }
18693 }
18694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018696 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018697 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018698
18699 /* f_uniq(): ptrs will be a stack of items to remove */
18700 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018701 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018702 item_compare_func_ptr = item_compare_func
18703 ? item_compare2 : item_compare;
18704
18705 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18706 li = li->li_next)
18707 {
18708 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18709 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018710 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018711 if (item_compare_func_err)
18712 {
18713 EMSG(_("E882: Uniq compare function failed"));
18714 break;
18715 }
18716 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018718 if (!item_compare_func_err)
18719 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018720 while (--i >= 0)
18721 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018722 li = ptrs[i].item->li_next;
18723 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018724 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018725 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018726 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018727 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018728 list_fix_watch(l, li);
18729 listitem_free(li);
18730 l->lv_len--;
18731 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018732 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018733 }
18734
18735 vim_free(ptrs);
18736 }
18737}
18738
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018739/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018740 * "sort({list})" function
18741 */
18742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018743f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018744{
18745 do_sort_uniq(argvars, rettv, TRUE);
18746}
18747
18748/*
18749 * "uniq({list})" function
18750 */
18751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018752f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018753{
18754 do_sort_uniq(argvars, rettv, FALSE);
18755}
18756
18757/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018758 * "soundfold({word})" function
18759 */
18760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018761f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018762{
18763 char_u *s;
18764
18765 rettv->v_type = VAR_STRING;
18766 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018767#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018768 rettv->vval.v_string = eval_soundfold(s);
18769#else
18770 rettv->vval.v_string = vim_strsave(s);
18771#endif
18772}
18773
18774/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018775 * "spellbadword()" function
18776 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018778f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018779{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018780 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018781 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018782 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018783
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018784 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018785 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018786
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018787#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018788 if (argvars[0].v_type == VAR_UNKNOWN)
18789 {
18790 /* Find the start and length of the badly spelled word. */
18791 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18792 if (len != 0)
18793 word = ml_get_cursor();
18794 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018795 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018796 {
18797 char_u *str = get_tv_string_chk(&argvars[0]);
18798 int capcol = -1;
18799
18800 if (str != NULL)
18801 {
18802 /* Check the argument for spelling. */
18803 while (*str != NUL)
18804 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018805 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018806 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018807 {
18808 word = str;
18809 break;
18810 }
18811 str += len;
18812 }
18813 }
18814 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018815#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018816
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018817 list_append_string(rettv->vval.v_list, word, len);
18818 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018819 attr == HLF_SPB ? "bad" :
18820 attr == HLF_SPR ? "rare" :
18821 attr == HLF_SPL ? "local" :
18822 attr == HLF_SPC ? "caps" :
18823 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018824}
18825
18826/*
18827 * "spellsuggest()" function
18828 */
18829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018830f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018831{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018832#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018833 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018834 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018835 int maxcount;
18836 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018837 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018838 listitem_T *li;
18839 int need_capital = FALSE;
18840#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018841
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018842 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018843 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018844
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018845#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018846 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018847 {
18848 str = get_tv_string(&argvars[0]);
18849 if (argvars[1].v_type != VAR_UNKNOWN)
18850 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018851 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018852 if (maxcount <= 0)
18853 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018854 if (argvars[2].v_type != VAR_UNKNOWN)
18855 {
18856 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18857 if (typeerr)
18858 return;
18859 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018860 }
18861 else
18862 maxcount = 25;
18863
Bram Moolenaar4770d092006-01-12 23:22:24 +000018864 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018865
18866 for (i = 0; i < ga.ga_len; ++i)
18867 {
18868 str = ((char_u **)ga.ga_data)[i];
18869
18870 li = listitem_alloc();
18871 if (li == NULL)
18872 vim_free(str);
18873 else
18874 {
18875 li->li_tv.v_type = VAR_STRING;
18876 li->li_tv.v_lock = 0;
18877 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018878 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018879 }
18880 }
18881 ga_clear(&ga);
18882 }
18883#endif
18884}
18885
Bram Moolenaar0d660222005-01-07 21:51:51 +000018886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018887f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018888{
18889 char_u *str;
18890 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018891 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018892 regmatch_T regmatch;
18893 char_u patbuf[NUMBUFLEN];
18894 char_u *save_cpo;
18895 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018896 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018897 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018898 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018899
18900 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18901 save_cpo = p_cpo;
18902 p_cpo = (char_u *)"";
18903
18904 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018905 if (argvars[1].v_type != VAR_UNKNOWN)
18906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018907 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18908 if (pat == NULL)
18909 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018910 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018911 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018912 }
18913 if (pat == NULL || *pat == NUL)
18914 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018915
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018916 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018918 if (typeerr)
18919 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920
Bram Moolenaar0d660222005-01-07 21:51:51 +000018921 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18922 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018924 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018925 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018926 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018927 if (*str == NUL)
18928 match = FALSE; /* empty item at the end */
18929 else
18930 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018931 if (match)
18932 end = regmatch.startp[0];
18933 else
18934 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018935 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18936 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018937 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018938 if (list_append_string(rettv->vval.v_list, str,
18939 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018940 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018941 }
18942 if (!match)
18943 break;
18944 /* Advance to just after the match. */
18945 if (regmatch.endp[0] > str)
18946 col = 0;
18947 else
18948 {
18949 /* Don't get stuck at the same match. */
18950#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018951 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018952#else
18953 col = 1;
18954#endif
18955 }
18956 str = regmatch.endp[0];
18957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958
Bram Moolenaar473de612013-06-08 18:19:48 +020018959 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961
Bram Moolenaar0d660222005-01-07 21:51:51 +000018962 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018963}
18964
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018965#ifdef FEAT_FLOAT
18966/*
18967 * "sqrt()" function
18968 */
18969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018970f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018971{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018972 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018973
18974 rettv->v_type = VAR_FLOAT;
18975 if (get_float_arg(argvars, &f) == OK)
18976 rettv->vval.v_float = sqrt(f);
18977 else
18978 rettv->vval.v_float = 0.0;
18979}
18980
18981/*
18982 * "str2float()" function
18983 */
18984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018985f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018986{
18987 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18988
18989 if (*p == '+')
18990 p = skipwhite(p + 1);
18991 (void)string2float(p, &rettv->vval.v_float);
18992 rettv->v_type = VAR_FLOAT;
18993}
18994#endif
18995
Bram Moolenaar2c932302006-03-18 21:42:09 +000018996/*
18997 * "str2nr()" function
18998 */
18999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019000f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019001{
19002 int base = 10;
19003 char_u *p;
19004 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019005 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019006
19007 if (argvars[1].v_type != VAR_UNKNOWN)
19008 {
19009 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019010 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019011 {
19012 EMSG(_(e_invarg));
19013 return;
19014 }
19015 }
19016
19017 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019018 if (*p == '+')
19019 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019020 switch (base)
19021 {
19022 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19023 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19024 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19025 default: what = 0;
19026 }
19027 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019028 rettv->vval.v_number = n;
19029}
19030
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031#ifdef HAVE_STRFTIME
19032/*
19033 * "strftime({format}[, {time}])" function
19034 */
19035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019036f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037{
19038 char_u result_buf[256];
19039 struct tm *curtime;
19040 time_t seconds;
19041 char_u *p;
19042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019043 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019045 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019046 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047 seconds = time(NULL);
19048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019049 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050 curtime = localtime(&seconds);
19051 /* MSVC returns NULL for an invalid value of seconds. */
19052 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019053 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 else
19055 {
19056# ifdef FEAT_MBYTE
19057 vimconv_T conv;
19058 char_u *enc;
19059
19060 conv.vc_type = CONV_NONE;
19061 enc = enc_locale();
19062 convert_setup(&conv, p_enc, enc);
19063 if (conv.vc_type != CONV_NONE)
19064 p = string_convert(&conv, p, NULL);
19065# endif
19066 if (p != NULL)
19067 (void)strftime((char *)result_buf, sizeof(result_buf),
19068 (char *)p, curtime);
19069 else
19070 result_buf[0] = NUL;
19071
19072# ifdef FEAT_MBYTE
19073 if (conv.vc_type != CONV_NONE)
19074 vim_free(p);
19075 convert_setup(&conv, enc, p_enc);
19076 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019077 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 else
19079# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019080 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019081
19082# ifdef FEAT_MBYTE
19083 /* Release conversion descriptors */
19084 convert_setup(&conv, NULL, NULL);
19085 vim_free(enc);
19086# endif
19087 }
19088}
19089#endif
19090
19091/*
19092 * "stridx()" function
19093 */
19094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019095f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019096{
19097 char_u buf[NUMBUFLEN];
19098 char_u *needle;
19099 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019100 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019101 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019102 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019104 needle = get_tv_string_chk(&argvars[1]);
19105 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019106 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019107 if (needle == NULL || haystack == NULL)
19108 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109
Bram Moolenaar33570922005-01-25 22:26:29 +000019110 if (argvars[2].v_type != VAR_UNKNOWN)
19111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019112 int error = FALSE;
19113
19114 start_idx = get_tv_number_chk(&argvars[2], &error);
19115 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019116 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019117 if (start_idx >= 0)
19118 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019119 }
19120
19121 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19122 if (pos != NULL)
19123 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124}
19125
19126/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019127 * "string()" function
19128 */
19129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019130f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019131{
19132 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019133 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019135 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019136 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019137 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019138 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019139 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140}
19141
19142/*
19143 * "strlen()" function
19144 */
19145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019146f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019148 rettv->vval.v_number = (varnumber_T)(STRLEN(
19149 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150}
19151
19152/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019153 * "strchars()" function
19154 */
19155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019156f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019157{
19158 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019159 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019160#ifdef FEAT_MBYTE
19161 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019162 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019163#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019164
19165 if (argvars[1].v_type != VAR_UNKNOWN)
19166 skipcc = get_tv_number_chk(&argvars[1], NULL);
19167 if (skipcc < 0 || skipcc > 1)
19168 EMSG(_(e_invarg));
19169 else
19170 {
19171#ifdef FEAT_MBYTE
19172 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19173 while (*s != NUL)
19174 {
19175 func_mb_ptr2char_adv(&s);
19176 ++len;
19177 }
19178 rettv->vval.v_number = len;
19179#else
19180 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19181#endif
19182 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019183}
19184
19185/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019186 * "strdisplaywidth()" function
19187 */
19188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019189f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019190{
19191 char_u *s = get_tv_string(&argvars[0]);
19192 int col = 0;
19193
19194 if (argvars[1].v_type != VAR_UNKNOWN)
19195 col = get_tv_number(&argvars[1]);
19196
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019197 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019198}
19199
19200/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019201 * "strwidth()" function
19202 */
19203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019204f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019205{
19206 char_u *s = get_tv_string(&argvars[0]);
19207
19208 rettv->vval.v_number = (varnumber_T)(
19209#ifdef FEAT_MBYTE
19210 mb_string2cells(s, -1)
19211#else
19212 STRLEN(s)
19213#endif
19214 );
19215}
19216
19217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 * "strpart()" function
19219 */
19220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019221f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222{
19223 char_u *p;
19224 int n;
19225 int len;
19226 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019227 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019229 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 slen = (int)STRLEN(p);
19231
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019232 n = get_tv_number_chk(&argvars[1], &error);
19233 if (error)
19234 len = 0;
19235 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019236 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237 else
19238 len = slen - n; /* default len: all bytes that are available. */
19239
19240 /*
19241 * Only return the overlap between the specified part and the actual
19242 * string.
19243 */
19244 if (n < 0)
19245 {
19246 len += n;
19247 n = 0;
19248 }
19249 else if (n > slen)
19250 n = slen;
19251 if (len < 0)
19252 len = 0;
19253 else if (n + len > slen)
19254 len = slen - n;
19255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019256 rettv->v_type = VAR_STRING;
19257 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258}
19259
19260/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019261 * "strridx()" function
19262 */
19263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019264f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019265{
19266 char_u buf[NUMBUFLEN];
19267 char_u *needle;
19268 char_u *haystack;
19269 char_u *rest;
19270 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019271 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019273 needle = get_tv_string_chk(&argvars[1]);
19274 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019275
19276 rettv->vval.v_number = -1;
19277 if (needle == NULL || haystack == NULL)
19278 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019279
19280 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019281 if (argvars[2].v_type != VAR_UNKNOWN)
19282 {
19283 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019284 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019285 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019286 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019287 }
19288 else
19289 end_idx = haystack_len;
19290
Bram Moolenaar0d660222005-01-07 21:51:51 +000019291 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019292 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019293 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019294 lastmatch = haystack + end_idx;
19295 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019296 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019297 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019298 for (rest = haystack; *rest != '\0'; ++rest)
19299 {
19300 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019301 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019302 break;
19303 lastmatch = rest;
19304 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019305 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019306
19307 if (lastmatch == NULL)
19308 rettv->vval.v_number = -1;
19309 else
19310 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19311}
19312
19313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 * "strtrans()" function
19315 */
19316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019317f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019319 rettv->v_type = VAR_STRING;
19320 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321}
19322
19323/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019324 * "submatch()" function
19325 */
19326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019327f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019328{
Bram Moolenaar41571762014-04-02 19:00:58 +020019329 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019330 int no;
19331 int retList = 0;
19332
19333 no = (int)get_tv_number_chk(&argvars[0], &error);
19334 if (error)
19335 return;
19336 error = FALSE;
19337 if (argvars[1].v_type != VAR_UNKNOWN)
19338 retList = get_tv_number_chk(&argvars[1], &error);
19339 if (error)
19340 return;
19341
19342 if (retList == 0)
19343 {
19344 rettv->v_type = VAR_STRING;
19345 rettv->vval.v_string = reg_submatch(no);
19346 }
19347 else
19348 {
19349 rettv->v_type = VAR_LIST;
19350 rettv->vval.v_list = reg_submatch_list(no);
19351 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019352}
19353
19354/*
19355 * "substitute()" function
19356 */
19357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019358f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019359{
19360 char_u patbuf[NUMBUFLEN];
19361 char_u subbuf[NUMBUFLEN];
19362 char_u flagsbuf[NUMBUFLEN];
19363
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019364 char_u *str = get_tv_string_chk(&argvars[0]);
19365 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19366 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19367 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19368
Bram Moolenaar0d660222005-01-07 21:51:51 +000019369 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019370 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19371 rettv->vval.v_string = NULL;
19372 else
19373 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019374}
19375
19376/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019377 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019380f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381{
19382 int id = 0;
19383#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019384 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 long col;
19386 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019387 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019389 lnum = get_tv_lnum(argvars); /* -1 on type error */
19390 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19391 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019393 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019394 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019395 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396#endif
19397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019398 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399}
19400
19401/*
19402 * "synIDattr(id, what [, mode])" function
19403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019405f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406{
19407 char_u *p = NULL;
19408#ifdef FEAT_SYN_HL
19409 int id;
19410 char_u *what;
19411 char_u *mode;
19412 char_u modebuf[NUMBUFLEN];
19413 int modec;
19414
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019415 id = get_tv_number(&argvars[0]);
19416 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019417 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019419 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019421 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422 modec = 0; /* replace invalid with current */
19423 }
19424 else
19425 {
19426#ifdef FEAT_GUI
19427 if (gui.in_use)
19428 modec = 'g';
19429 else
19430#endif
19431 if (t_colors > 1)
19432 modec = 'c';
19433 else
19434 modec = 't';
19435 }
19436
19437
19438 switch (TOLOWER_ASC(what[0]))
19439 {
19440 case 'b':
19441 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19442 p = highlight_color(id, what, modec);
19443 else /* bold */
19444 p = highlight_has_attr(id, HL_BOLD, modec);
19445 break;
19446
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019447 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 p = highlight_color(id, what, modec);
19449 break;
19450
19451 case 'i':
19452 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19453 p = highlight_has_attr(id, HL_INVERSE, modec);
19454 else /* italic */
19455 p = highlight_has_attr(id, HL_ITALIC, modec);
19456 break;
19457
19458 case 'n': /* name */
19459 p = get_highlight_name(NULL, id - 1);
19460 break;
19461
19462 case 'r': /* reverse */
19463 p = highlight_has_attr(id, HL_INVERSE, modec);
19464 break;
19465
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019466 case 's':
19467 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19468 p = highlight_color(id, what, modec);
19469 else /* standout */
19470 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 break;
19472
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019473 case 'u':
19474 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19475 /* underline */
19476 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19477 else
19478 /* undercurl */
19479 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 break;
19481 }
19482
19483 if (p != NULL)
19484 p = vim_strsave(p);
19485#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019486 rettv->v_type = VAR_STRING;
19487 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488}
19489
19490/*
19491 * "synIDtrans(id)" function
19492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019494f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495{
19496 int id;
19497
19498#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019499 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500
19501 if (id > 0)
19502 id = syn_get_final_id(id);
19503 else
19504#endif
19505 id = 0;
19506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019507 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508}
19509
19510/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019511 * "synconcealed(lnum, col)" function
19512 */
19513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019514f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019515{
19516#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19517 long lnum;
19518 long col;
19519 int syntax_flags = 0;
19520 int cchar;
19521 int matchid = 0;
19522 char_u str[NUMBUFLEN];
19523#endif
19524
19525 rettv->v_type = VAR_LIST;
19526 rettv->vval.v_list = NULL;
19527
19528#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19529 lnum = get_tv_lnum(argvars); /* -1 on type error */
19530 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19531
19532 vim_memset(str, NUL, sizeof(str));
19533
19534 if (rettv_list_alloc(rettv) != FAIL)
19535 {
19536 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19537 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19538 && curwin->w_p_cole > 0)
19539 {
19540 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19541 syntax_flags = get_syntax_info(&matchid);
19542
19543 /* get the conceal character */
19544 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19545 {
19546 cchar = syn_get_sub_char();
19547 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19548 cchar = lcs_conceal;
19549 if (cchar != NUL)
19550 {
19551# ifdef FEAT_MBYTE
19552 if (has_mbyte)
19553 (*mb_char2bytes)(cchar, str);
19554 else
19555# endif
19556 str[0] = cchar;
19557 }
19558 }
19559 }
19560
19561 list_append_number(rettv->vval.v_list,
19562 (syntax_flags & HL_CONCEAL) != 0);
19563 /* -1 to auto-determine strlen */
19564 list_append_string(rettv->vval.v_list, str, -1);
19565 list_append_number(rettv->vval.v_list, matchid);
19566 }
19567#endif
19568}
19569
19570/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019571 * "synstack(lnum, col)" function
19572 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019574f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019575{
19576#ifdef FEAT_SYN_HL
19577 long lnum;
19578 long col;
19579 int i;
19580 int id;
19581#endif
19582
19583 rettv->v_type = VAR_LIST;
19584 rettv->vval.v_list = NULL;
19585
19586#ifdef FEAT_SYN_HL
19587 lnum = get_tv_lnum(argvars); /* -1 on type error */
19588 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19589
19590 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019591 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019592 && rettv_list_alloc(rettv) != FAIL)
19593 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019594 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019595 for (i = 0; ; ++i)
19596 {
19597 id = syn_get_stack_item(i);
19598 if (id < 0)
19599 break;
19600 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19601 break;
19602 }
19603 }
19604#endif
19605}
19606
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019608get_cmd_output_as_rettv(
19609 typval_T *argvars,
19610 typval_T *rettv,
19611 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019613 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019615 char_u *infile = NULL;
19616 char_u buf[NUMBUFLEN];
19617 int err = FALSE;
19618 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019619 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019620 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019622 rettv->v_type = VAR_STRING;
19623 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019624 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019625 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019626
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019627 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019628 {
19629 /*
19630 * Write the string to a temp file, to be used for input of the shell
19631 * command.
19632 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019633 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019634 {
19635 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019636 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019637 }
19638
19639 fd = mch_fopen((char *)infile, WRITEBIN);
19640 if (fd == NULL)
19641 {
19642 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019643 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019644 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019645 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019646 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019647 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19648 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019649 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019650 else
19651 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019652 size_t len;
19653
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019654 p = get_tv_string_buf_chk(&argvars[1], buf);
19655 if (p == NULL)
19656 {
19657 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019658 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019659 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019660 len = STRLEN(p);
19661 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019662 err = TRUE;
19663 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019664 if (fclose(fd) != 0)
19665 err = TRUE;
19666 if (err)
19667 {
19668 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019669 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019670 }
19671 }
19672
Bram Moolenaar52a72462014-08-29 15:53:52 +020019673 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19674 * echoes typeahead, that messes up the display. */
19675 if (!msg_silent)
19676 flags += SHELL_COOKED;
19677
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019678 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019680 int len;
19681 listitem_T *li;
19682 char_u *s = NULL;
19683 char_u *start;
19684 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019685 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686
Bram Moolenaar52a72462014-08-29 15:53:52 +020019687 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019688 if (res == NULL)
19689 goto errret;
19690
19691 list = list_alloc();
19692 if (list == NULL)
19693 goto errret;
19694
19695 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019697 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019698 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019699 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019700 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019701
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019702 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019703 if (s == NULL)
19704 goto errret;
19705
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019706 for (p = s; start < end; ++p, ++start)
19707 *p = *start == NUL ? NL : *start;
19708 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019709
19710 li = listitem_alloc();
19711 if (li == NULL)
19712 {
19713 vim_free(s);
19714 goto errret;
19715 }
19716 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019717 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019718 li->li_tv.vval.v_string = s;
19719 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019721
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019722 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019723 rettv->v_type = VAR_LIST;
19724 rettv->vval.v_list = list;
19725 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019727 else
19728 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019729 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019730#ifdef USE_CR
19731 /* translate <CR> into <NL> */
19732 if (res != NULL)
19733 {
19734 char_u *s;
19735
19736 for (s = res; *s; ++s)
19737 {
19738 if (*s == CAR)
19739 *s = NL;
19740 }
19741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019742#else
19743# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019744 /* translate <CR><NL> into <NL> */
19745 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019747 char_u *s, *d;
19748
19749 d = res;
19750 for (s = res; *s; ++s)
19751 {
19752 if (s[0] == CAR && s[1] == NL)
19753 ++s;
19754 *d++ = *s;
19755 }
19756 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758# endif
19759#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019760 rettv->vval.v_string = res;
19761 res = NULL;
19762 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019763
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019764errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019765 if (infile != NULL)
19766 {
19767 mch_remove(infile);
19768 vim_free(infile);
19769 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019770 if (res != NULL)
19771 vim_free(res);
19772 if (list != NULL)
19773 list_free(list, TRUE);
19774}
19775
19776/*
19777 * "system()" function
19778 */
19779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019780f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019781{
19782 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19783}
19784
19785/*
19786 * "systemlist()" function
19787 */
19788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019789f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019790{
19791 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792}
19793
19794/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019795 * "tabpagebuflist()" function
19796 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019798f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019799{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019800#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019801 tabpage_T *tp;
19802 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019803
19804 if (argvars[0].v_type == VAR_UNKNOWN)
19805 wp = firstwin;
19806 else
19807 {
19808 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19809 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019810 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019811 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019812 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019813 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019814 for (; wp != NULL; wp = wp->w_next)
19815 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019816 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019817 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019818 }
19819#endif
19820}
19821
19822
19823/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019824 * "tabpagenr()" function
19825 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019827f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019828{
19829 int nr = 1;
19830#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019831 char_u *arg;
19832
19833 if (argvars[0].v_type != VAR_UNKNOWN)
19834 {
19835 arg = get_tv_string_chk(&argvars[0]);
19836 nr = 0;
19837 if (arg != NULL)
19838 {
19839 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019840 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019841 else
19842 EMSG2(_(e_invexpr2), arg);
19843 }
19844 }
19845 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019846 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019847#endif
19848 rettv->vval.v_number = nr;
19849}
19850
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019851
19852#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019853static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019854
19855/*
19856 * Common code for tabpagewinnr() and winnr().
19857 */
19858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019859get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019860{
19861 win_T *twin;
19862 int nr = 1;
19863 win_T *wp;
19864 char_u *arg;
19865
19866 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19867 if (argvar->v_type != VAR_UNKNOWN)
19868 {
19869 arg = get_tv_string_chk(argvar);
19870 if (arg == NULL)
19871 nr = 0; /* type error; errmsg already given */
19872 else if (STRCMP(arg, "$") == 0)
19873 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19874 else if (STRCMP(arg, "#") == 0)
19875 {
19876 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19877 if (twin == NULL)
19878 nr = 0;
19879 }
19880 else
19881 {
19882 EMSG2(_(e_invexpr2), arg);
19883 nr = 0;
19884 }
19885 }
19886
19887 if (nr > 0)
19888 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19889 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019890 {
19891 if (wp == NULL)
19892 {
19893 /* didn't find it in this tabpage */
19894 nr = 0;
19895 break;
19896 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019897 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019898 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019899 return nr;
19900}
19901#endif
19902
19903/*
19904 * "tabpagewinnr()" function
19905 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019907f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019908{
19909 int nr = 1;
19910#ifdef FEAT_WINDOWS
19911 tabpage_T *tp;
19912
19913 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19914 if (tp == NULL)
19915 nr = 0;
19916 else
19917 nr = get_winnr(tp, &argvars[1]);
19918#endif
19919 rettv->vval.v_number = nr;
19920}
19921
19922
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019923/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019924 * "tagfiles()" function
19925 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019927f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019928{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019929 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019930 tagname_T tn;
19931 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019932
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019933 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019934 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019935 fname = alloc(MAXPATHL);
19936 if (fname == NULL)
19937 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019938
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019939 for (first = TRUE; ; first = FALSE)
19940 if (get_tagfname(&tn, first, fname) == FAIL
19941 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019942 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019943 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019944 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019945}
19946
19947/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019948 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019949 */
19950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019951f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019952{
19953 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019954
19955 tag_pattern = get_tv_string(&argvars[0]);
19956
19957 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019958 if (*tag_pattern == NUL)
19959 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019960
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019961 if (rettv_list_alloc(rettv) == OK)
19962 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019963}
19964
19965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 * "tempname()" function
19967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019969f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970{
19971 static int x = 'A';
19972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019973 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019974 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975
19976 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19977 * names. Skip 'I' and 'O', they are used for shell redirection. */
19978 do
19979 {
19980 if (x == 'Z')
19981 x = '0';
19982 else if (x == '9')
19983 x = 'A';
19984 else
19985 {
19986#ifdef EBCDIC
19987 if (x == 'I')
19988 x = 'J';
19989 else if (x == 'R')
19990 x = 'S';
19991 else
19992#endif
19993 ++x;
19994 }
19995 } while (x == 'I' || x == 'O');
19996}
19997
19998/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019999 * "test(list)" function: Just checking the walls...
20000 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020002f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020003{
20004 /* Used for unit testing. Change the code below to your liking. */
20005#if 0
20006 listitem_T *li;
20007 list_T *l;
20008 char_u *bad, *good;
20009
20010 if (argvars[0].v_type != VAR_LIST)
20011 return;
20012 l = argvars[0].vval.v_list;
20013 if (l == NULL)
20014 return;
20015 li = l->lv_first;
20016 if (li == NULL)
20017 return;
20018 bad = get_tv_string(&li->li_tv);
20019 li = li->li_next;
20020 if (li == NULL)
20021 return;
20022 good = get_tv_string(&li->li_tv);
20023 rettv->vval.v_number = test_edit_score(bad, good);
20024#endif
20025}
20026
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020027#ifdef FEAT_FLOAT
20028/*
20029 * "tan()" function
20030 */
20031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020032f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020033{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020034 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020035
20036 rettv->v_type = VAR_FLOAT;
20037 if (get_float_arg(argvars, &f) == OK)
20038 rettv->vval.v_float = tan(f);
20039 else
20040 rettv->vval.v_float = 0.0;
20041}
20042
20043/*
20044 * "tanh()" function
20045 */
20046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020047f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020048{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020049 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020050
20051 rettv->v_type = VAR_FLOAT;
20052 if (get_float_arg(argvars, &f) == OK)
20053 rettv->vval.v_float = tanh(f);
20054 else
20055 rettv->vval.v_float = 0.0;
20056}
20057#endif
20058
Bram Moolenaard52d9742005-08-21 22:20:28 +000020059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 * "tolower(string)" function
20061 */
20062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020063f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064{
20065 char_u *p;
20066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020067 p = vim_strsave(get_tv_string(&argvars[0]));
20068 rettv->v_type = VAR_STRING;
20069 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070
20071 if (p != NULL)
20072 while (*p != NUL)
20073 {
20074#ifdef FEAT_MBYTE
20075 int l;
20076
20077 if (enc_utf8)
20078 {
20079 int c, lc;
20080
20081 c = utf_ptr2char(p);
20082 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020083 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 /* TODO: reallocate string when byte count changes. */
20085 if (utf_char2len(lc) == l)
20086 utf_char2bytes(lc, p);
20087 p += l;
20088 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020089 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090 p += l; /* skip multi-byte character */
20091 else
20092#endif
20093 {
20094 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20095 ++p;
20096 }
20097 }
20098}
20099
20100/*
20101 * "toupper(string)" function
20102 */
20103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020104f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020107 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108}
20109
20110/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020111 * "tr(string, fromstr, tostr)" function
20112 */
20113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020114f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020115{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020116 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020117 char_u *fromstr;
20118 char_u *tostr;
20119 char_u *p;
20120#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020121 int inlen;
20122 int fromlen;
20123 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020124 int idx;
20125 char_u *cpstr;
20126 int cplen;
20127 int first = TRUE;
20128#endif
20129 char_u buf[NUMBUFLEN];
20130 char_u buf2[NUMBUFLEN];
20131 garray_T ga;
20132
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020133 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020134 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20135 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020136
20137 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020138 rettv->v_type = VAR_STRING;
20139 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020140 if (fromstr == NULL || tostr == NULL)
20141 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020142 ga_init2(&ga, (int)sizeof(char), 80);
20143
20144#ifdef FEAT_MBYTE
20145 if (!has_mbyte)
20146#endif
20147 /* not multi-byte: fromstr and tostr must be the same length */
20148 if (STRLEN(fromstr) != STRLEN(tostr))
20149 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020150#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020151error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020152#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020153 EMSG2(_(e_invarg2), fromstr);
20154 ga_clear(&ga);
20155 return;
20156 }
20157
20158 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020159 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020160 {
20161#ifdef FEAT_MBYTE
20162 if (has_mbyte)
20163 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020164 inlen = (*mb_ptr2len)(in_str);
20165 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020166 cplen = inlen;
20167 idx = 0;
20168 for (p = fromstr; *p != NUL; p += fromlen)
20169 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020170 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020171 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020172 {
20173 for (p = tostr; *p != NUL; p += tolen)
20174 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020175 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020176 if (idx-- == 0)
20177 {
20178 cplen = tolen;
20179 cpstr = p;
20180 break;
20181 }
20182 }
20183 if (*p == NUL) /* tostr is shorter than fromstr */
20184 goto error;
20185 break;
20186 }
20187 ++idx;
20188 }
20189
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020190 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020191 {
20192 /* Check that fromstr and tostr have the same number of
20193 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020194 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020195 first = FALSE;
20196 for (p = tostr; *p != NUL; p += tolen)
20197 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020198 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020199 --idx;
20200 }
20201 if (idx != 0)
20202 goto error;
20203 }
20204
Bram Moolenaarcde88542015-08-11 19:14:00 +020020205 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020206 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020207 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020208
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020209 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020210 }
20211 else
20212#endif
20213 {
20214 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020215 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020216 if (p != NULL)
20217 ga_append(&ga, tostr[p - fromstr]);
20218 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020219 ga_append(&ga, *in_str);
20220 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020221 }
20222 }
20223
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020224 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020225 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020226 ga_append(&ga, NUL);
20227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020228 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020229}
20230
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020231#ifdef FEAT_FLOAT
20232/*
20233 * "trunc({float})" function
20234 */
20235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020236f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020237{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020238 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020239
20240 rettv->v_type = VAR_FLOAT;
20241 if (get_float_arg(argvars, &f) == OK)
20242 /* trunc() is not in C90, use floor() or ceil() instead. */
20243 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20244 else
20245 rettv->vval.v_float = 0.0;
20246}
20247#endif
20248
Bram Moolenaar8299df92004-07-10 09:47:34 +000020249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 * "type(expr)" function
20251 */
20252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020253f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020255 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020256
20257 switch (argvars[0].v_type)
20258 {
20259 case VAR_NUMBER: n = 0; break;
20260 case VAR_STRING: n = 1; break;
20261 case VAR_FUNC: n = 2; break;
20262 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020263 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020264 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020265 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020266 if (argvars[0].vval.v_number == VVAL_FALSE
20267 || argvars[0].vval.v_number == VVAL_TRUE)
20268 n = 6;
20269 else
20270 n = 7;
20271 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020272 case VAR_JOB: n = 8; break;
20273 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020274 case VAR_UNKNOWN:
20275 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20276 n = -1;
20277 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020278 }
20279 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280}
20281
20282/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020283 * "undofile(name)" function
20284 */
20285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020286f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020287{
20288 rettv->v_type = VAR_STRING;
20289#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020290 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020291 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020292
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020293 if (*fname == NUL)
20294 {
20295 /* If there is no file name there will be no undo file. */
20296 rettv->vval.v_string = NULL;
20297 }
20298 else
20299 {
20300 char_u *ffname = FullName_save(fname, FALSE);
20301
20302 if (ffname != NULL)
20303 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20304 vim_free(ffname);
20305 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020306 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020307#else
20308 rettv->vval.v_string = NULL;
20309#endif
20310}
20311
20312/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020313 * "undotree()" function
20314 */
20315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020316f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020317{
20318 if (rettv_dict_alloc(rettv) == OK)
20319 {
20320 dict_T *dict = rettv->vval.v_dict;
20321 list_T *list;
20322
Bram Moolenaar730cde92010-06-27 05:18:54 +020020323 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020324 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020325 dict_add_nr_str(dict, "save_last",
20326 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020327 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20328 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020329 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020330
20331 list = list_alloc();
20332 if (list != NULL)
20333 {
20334 u_eval_tree(curbuf->b_u_oldhead, list);
20335 dict_add_list(dict, "entries", list);
20336 }
20337 }
20338}
20339
20340/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020341 * "values(dict)" function
20342 */
20343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020344f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020345{
20346 dict_list(argvars, rettv, 1);
20347}
20348
20349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 * "virtcol(string)" function
20351 */
20352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020353f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354{
20355 colnr_T vcol = 0;
20356 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020357 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020359 fp = var2fpos(&argvars[0], FALSE, &fnum);
20360 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20361 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 {
20363 getvvcol(curwin, fp, NULL, NULL, &vcol);
20364 ++vcol;
20365 }
20366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020367 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368}
20369
20370/*
20371 * "visualmode()" function
20372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020374f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 char_u str[2];
20377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020378 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379 str[0] = curbuf->b_visual_mode_eval;
20380 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020381 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382
20383 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020384 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386}
20387
20388/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020389 * "wildmenumode()" function
20390 */
20391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020392f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020393{
20394#ifdef FEAT_WILDMENU
20395 if (wild_menu_showing)
20396 rettv->vval.v_number = 1;
20397#endif
20398}
20399
20400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020401 * "winbufnr(nr)" function
20402 */
20403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020404f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405{
20406 win_T *wp;
20407
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020408 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020410 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020412 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020413}
20414
20415/*
20416 * "wincol()" function
20417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020419f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420{
20421 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020422 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423}
20424
20425/*
20426 * "winheight(nr)" function
20427 */
20428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020429f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430{
20431 win_T *wp;
20432
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020433 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020435 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020437 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438}
20439
20440/*
20441 * "winline()" function
20442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020444f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020445{
20446 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020447 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448}
20449
20450/*
20451 * "winnr()" function
20452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020454f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455{
20456 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020457
Bram Moolenaar071d4272004-06-13 20:20:40 +000020458#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020459 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020461 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462}
20463
20464/*
20465 * "winrestcmd()" function
20466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020468f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469{
20470#ifdef FEAT_WINDOWS
20471 win_T *wp;
20472 int winnr = 1;
20473 garray_T ga;
20474 char_u buf[50];
20475
20476 ga_init2(&ga, (int)sizeof(char), 70);
20477 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20478 {
20479 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20480 ga_concat(&ga, buf);
20481# ifdef FEAT_VERTSPLIT
20482 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20483 ga_concat(&ga, buf);
20484# endif
20485 ++winnr;
20486 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020487 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020489 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020491 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020493 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494}
20495
20496/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020497 * "winrestview()" function
20498 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020500f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020501{
20502 dict_T *dict;
20503
20504 if (argvars[0].v_type != VAR_DICT
20505 || (dict = argvars[0].vval.v_dict) == NULL)
20506 EMSG(_(e_invarg));
20507 else
20508 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020509 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20510 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20511 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20512 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020513#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020514 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20515 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020516#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020517 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20518 {
20519 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20520 curwin->w_set_curswant = FALSE;
20521 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020522
Bram Moolenaar82c25852014-05-28 16:47:16 +020020523 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20524 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020525#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020526 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20527 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020528#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020529 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20530 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20531 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20532 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020533
20534 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020535 win_new_height(curwin, curwin->w_height);
20536# ifdef FEAT_VERTSPLIT
20537 win_new_width(curwin, W_WIDTH(curwin));
20538# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020539 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020540
Bram Moolenaarb851a962014-10-31 15:45:52 +010020541 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020542 curwin->w_topline = 1;
20543 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20544 curwin->w_topline = curbuf->b_ml.ml_line_count;
20545#ifdef FEAT_DIFF
20546 check_topfill(curwin, TRUE);
20547#endif
20548 }
20549}
20550
20551/*
20552 * "winsaveview()" function
20553 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020555f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020556{
20557 dict_T *dict;
20558
Bram Moolenaara800b422010-06-27 01:15:55 +020020559 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020560 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020561 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020562
20563 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20564 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20565#ifdef FEAT_VIRTUALEDIT
20566 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20567#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020568 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020569 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20570
20571 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20572#ifdef FEAT_DIFF
20573 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20574#endif
20575 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20576 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20577}
20578
20579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 * "winwidth(nr)" function
20581 */
20582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020583f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584{
20585 win_T *wp;
20586
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020587 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 else
20591#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020592 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020594 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020595#endif
20596}
20597
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020599 * "wordcount()" function
20600 */
20601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020602f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020603{
20604 if (rettv_dict_alloc(rettv) == FAIL)
20605 return;
20606 cursor_pos_info(rettv->vval.v_dict);
20607}
20608
20609/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020610 * Write list of strings to file
20611 */
20612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020613write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020614{
20615 listitem_T *li;
20616 int c;
20617 int ret = OK;
20618 char_u *s;
20619
20620 for (li = list->lv_first; li != NULL; li = li->li_next)
20621 {
20622 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20623 {
20624 if (*s == '\n')
20625 c = putc(NUL, fd);
20626 else
20627 c = putc(*s, fd);
20628 if (c == EOF)
20629 {
20630 ret = FAIL;
20631 break;
20632 }
20633 }
20634 if (!binary || li->li_next != NULL)
20635 if (putc('\n', fd) == EOF)
20636 {
20637 ret = FAIL;
20638 break;
20639 }
20640 if (ret == FAIL)
20641 {
20642 EMSG(_(e_write));
20643 break;
20644 }
20645 }
20646 return ret;
20647}
20648
20649/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020650 * "writefile()" function
20651 */
20652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020653f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020654{
20655 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020656 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020657 char_u *fname;
20658 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020659 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020660
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020661 if (check_restricted() || check_secure())
20662 return;
20663
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020664 if (argvars[0].v_type != VAR_LIST)
20665 {
20666 EMSG2(_(e_listarg), "writefile()");
20667 return;
20668 }
20669 if (argvars[0].vval.v_list == NULL)
20670 return;
20671
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020672 if (argvars[2].v_type != VAR_UNKNOWN)
20673 {
20674 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20675 binary = TRUE;
20676 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20677 append = TRUE;
20678 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020679
20680 /* Always open the file in binary mode, library functions have a mind of
20681 * their own about CR-LF conversion. */
20682 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020683 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20684 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020685 {
20686 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20687 ret = -1;
20688 }
20689 else
20690 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020691 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20692 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020693 fclose(fd);
20694 }
20695
20696 rettv->vval.v_number = ret;
20697}
20698
20699/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020700 * "xor(expr, expr)" function
20701 */
20702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020703f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020704{
20705 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20706 ^ get_tv_number_chk(&argvars[1], NULL);
20707}
20708
20709
20710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020712 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713 */
20714 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020715var2fpos(
20716 typval_T *varp,
20717 int dollar_lnum, /* TRUE when $ is last line */
20718 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020720 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020721 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020722 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723
Bram Moolenaara5525202006-03-02 22:52:09 +000020724 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020725 if (varp->v_type == VAR_LIST)
20726 {
20727 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020728 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020729 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020730 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020731
20732 l = varp->vval.v_list;
20733 if (l == NULL)
20734 return NULL;
20735
20736 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020737 pos.lnum = list_find_nr(l, 0L, &error);
20738 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020739 return NULL; /* invalid line number */
20740
20741 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020742 pos.col = list_find_nr(l, 1L, &error);
20743 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020744 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020745 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020746
20747 /* We accept "$" for the column number: last column. */
20748 li = list_find(l, 1L);
20749 if (li != NULL && li->li_tv.v_type == VAR_STRING
20750 && li->li_tv.vval.v_string != NULL
20751 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20752 pos.col = len + 1;
20753
Bram Moolenaara5525202006-03-02 22:52:09 +000020754 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020755 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020756 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020757 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020758
Bram Moolenaara5525202006-03-02 22:52:09 +000020759#ifdef FEAT_VIRTUALEDIT
20760 /* Get the virtual offset. Defaults to zero. */
20761 pos.coladd = list_find_nr(l, 2L, &error);
20762 if (error)
20763 pos.coladd = 0;
20764#endif
20765
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020766 return &pos;
20767 }
20768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020769 name = get_tv_string_chk(varp);
20770 if (name == NULL)
20771 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020772 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020774 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20775 {
20776 if (VIsual_active)
20777 return &VIsual;
20778 return &curwin->w_cursor;
20779 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020780 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020782 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20784 return NULL;
20785 return pp;
20786 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020787
20788#ifdef FEAT_VIRTUALEDIT
20789 pos.coladd = 0;
20790#endif
20791
Bram Moolenaar477933c2007-07-17 14:32:23 +000020792 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020793 {
20794 pos.col = 0;
20795 if (name[1] == '0') /* "w0": first visible line */
20796 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020797 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020798 pos.lnum = curwin->w_topline;
20799 return &pos;
20800 }
20801 else if (name[1] == '$') /* "w$": last visible line */
20802 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020803 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020804 pos.lnum = curwin->w_botline - 1;
20805 return &pos;
20806 }
20807 }
20808 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020809 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020810 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 {
20812 pos.lnum = curbuf->b_ml.ml_line_count;
20813 pos.col = 0;
20814 }
20815 else
20816 {
20817 pos.lnum = curwin->w_cursor.lnum;
20818 pos.col = (colnr_T)STRLEN(ml_get_curline());
20819 }
20820 return &pos;
20821 }
20822 return NULL;
20823}
20824
20825/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020826 * Convert list in "arg" into a position and optional file number.
20827 * When "fnump" is NULL there is no file number, only 3 items.
20828 * Note that the column is passed on as-is, the caller may want to decrement
20829 * it to use 1 for the first column.
20830 * Return FAIL when conversion is not possible, doesn't check the position for
20831 * validity.
20832 */
20833 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020834list2fpos(
20835 typval_T *arg,
20836 pos_T *posp,
20837 int *fnump,
20838 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020839{
20840 list_T *l = arg->vval.v_list;
20841 long i = 0;
20842 long n;
20843
Bram Moolenaar493c1782014-05-28 14:34:46 +020020844 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20845 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020846 if (arg->v_type != VAR_LIST
20847 || l == NULL
20848 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020849 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020850 return FAIL;
20851
20852 if (fnump != NULL)
20853 {
20854 n = list_find_nr(l, i++, NULL); /* fnum */
20855 if (n < 0)
20856 return FAIL;
20857 if (n == 0)
20858 n = curbuf->b_fnum; /* current buffer */
20859 *fnump = n;
20860 }
20861
20862 n = list_find_nr(l, i++, NULL); /* lnum */
20863 if (n < 0)
20864 return FAIL;
20865 posp->lnum = n;
20866
20867 n = list_find_nr(l, i++, NULL); /* col */
20868 if (n < 0)
20869 return FAIL;
20870 posp->col = n;
20871
20872#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020873 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020874 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020875 posp->coladd = 0;
20876 else
20877 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020878#endif
20879
Bram Moolenaar493c1782014-05-28 14:34:46 +020020880 if (curswantp != NULL)
20881 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20882
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020883 return OK;
20884}
20885
20886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 * Get the length of an environment variable name.
20888 * Advance "arg" to the first character after the name.
20889 * Return 0 for error.
20890 */
20891 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020892get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893{
20894 char_u *p;
20895 int len;
20896
20897 for (p = *arg; vim_isIDc(*p); ++p)
20898 ;
20899 if (p == *arg) /* no name found */
20900 return 0;
20901
20902 len = (int)(p - *arg);
20903 *arg = p;
20904 return len;
20905}
20906
20907/*
20908 * Get the length of the name of a function or internal variable.
20909 * "arg" is advanced to the first non-white character after the name.
20910 * Return 0 if something is wrong.
20911 */
20912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020913get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914{
20915 char_u *p;
20916 int len;
20917
20918 /* Find the end of the name. */
20919 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020920 {
20921 if (*p == ':')
20922 {
20923 /* "s:" is start of "s:var", but "n:" is not and can be used in
20924 * slice "[n:]". Also "xx:" is not a namespace. */
20925 len = (int)(p - *arg);
20926 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20927 || len > 1)
20928 break;
20929 }
20930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 if (p == *arg) /* no name found */
20932 return 0;
20933
20934 len = (int)(p - *arg);
20935 *arg = skipwhite(p);
20936
20937 return len;
20938}
20939
20940/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020941 * Get the length of the name of a variable or function.
20942 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020944 * Return -1 if curly braces expansion failed.
20945 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 * If the name contains 'magic' {}'s, expand them and return the
20947 * expanded name in an allocated string via 'alias' - caller must free.
20948 */
20949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020950get_name_len(
20951 char_u **arg,
20952 char_u **alias,
20953 int evaluate,
20954 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955{
20956 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 char_u *p;
20958 char_u *expr_start;
20959 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960
20961 *alias = NULL; /* default to no alias */
20962
20963 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20964 && (*arg)[2] == (int)KE_SNR)
20965 {
20966 /* hard coded <SNR>, already translated */
20967 *arg += 3;
20968 return get_id_len(arg) + 3;
20969 }
20970 len = eval_fname_script(*arg);
20971 if (len > 0)
20972 {
20973 /* literal "<SID>", "s:" or "<SNR>" */
20974 *arg += len;
20975 }
20976
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020978 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020980 p = find_name_end(*arg, &expr_start, &expr_end,
20981 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 if (expr_start != NULL)
20983 {
20984 char_u *temp_string;
20985
20986 if (!evaluate)
20987 {
20988 len += (int)(p - *arg);
20989 *arg = skipwhite(p);
20990 return len;
20991 }
20992
20993 /*
20994 * Include any <SID> etc in the expanded string:
20995 * Thus the -len here.
20996 */
20997 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20998 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020999 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 *alias = temp_string;
21001 *arg = skipwhite(p);
21002 return (int)STRLEN(temp_string);
21003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004
21005 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021006 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 EMSG2(_(e_invexpr2), *arg);
21008
21009 return len;
21010}
21011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021012/*
21013 * Find the end of a variable or function name, taking care of magic braces.
21014 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21015 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021016 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021017 * Return a pointer to just after the name. Equal to "arg" if there is no
21018 * valid name.
21019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021021find_name_end(
21022 char_u *arg,
21023 char_u **expr_start,
21024 char_u **expr_end,
21025 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021027 int mb_nest = 0;
21028 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021030 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021034 *expr_start = NULL;
21035 *expr_end = NULL;
21036 }
21037
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021038 /* Quick check for valid starting character. */
21039 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21040 return arg;
21041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021042 for (p = arg; *p != NUL
21043 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021044 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021045 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021046 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021047 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021048 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021049 if (*p == '\'')
21050 {
21051 /* skip over 'string' to avoid counting [ and ] inside it. */
21052 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21053 ;
21054 if (*p == NUL)
21055 break;
21056 }
21057 else if (*p == '"')
21058 {
21059 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21060 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21061 if (*p == '\\' && p[1] != NUL)
21062 ++p;
21063 if (*p == NUL)
21064 break;
21065 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021066 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21067 {
21068 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021069 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021070 len = (int)(p - arg);
21071 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021072 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021073 break;
21074 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021076 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021078 if (*p == '[')
21079 ++br_nest;
21080 else if (*p == ']')
21081 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021084 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021086 if (*p == '{')
21087 {
21088 mb_nest++;
21089 if (expr_start != NULL && *expr_start == NULL)
21090 *expr_start = p;
21091 }
21092 else if (*p == '}')
21093 {
21094 mb_nest--;
21095 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21096 *expr_end = p;
21097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 }
21100
21101 return p;
21102}
21103
21104/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021105 * Expands out the 'magic' {}'s in a variable/function name.
21106 * Note that this can call itself recursively, to deal with
21107 * constructs like foo{bar}{baz}{bam}
21108 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21109 * "in_start" ^
21110 * "expr_start" ^
21111 * "expr_end" ^
21112 * "in_end" ^
21113 *
21114 * Returns a new allocated string, which the caller must free.
21115 * Returns NULL for failure.
21116 */
21117 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021118make_expanded_name(
21119 char_u *in_start,
21120 char_u *expr_start,
21121 char_u *expr_end,
21122 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021123{
21124 char_u c1;
21125 char_u *retval = NULL;
21126 char_u *temp_result;
21127 char_u *nextcmd = NULL;
21128
21129 if (expr_end == NULL || in_end == NULL)
21130 return NULL;
21131 *expr_start = NUL;
21132 *expr_end = NUL;
21133 c1 = *in_end;
21134 *in_end = NUL;
21135
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021136 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021137 if (temp_result != NULL && nextcmd == NULL)
21138 {
21139 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21140 + (in_end - expr_end) + 1));
21141 if (retval != NULL)
21142 {
21143 STRCPY(retval, in_start);
21144 STRCAT(retval, temp_result);
21145 STRCAT(retval, expr_end + 1);
21146 }
21147 }
21148 vim_free(temp_result);
21149
21150 *in_end = c1; /* put char back for error messages */
21151 *expr_start = '{';
21152 *expr_end = '}';
21153
21154 if (retval != NULL)
21155 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021156 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021157 if (expr_start != NULL)
21158 {
21159 /* Further expansion! */
21160 temp_result = make_expanded_name(retval, expr_start,
21161 expr_end, temp_result);
21162 vim_free(retval);
21163 retval = temp_result;
21164 }
21165 }
21166
21167 return retval;
21168}
21169
21170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021172 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 */
21174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021175eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021177 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21178}
21179
21180/*
21181 * Return TRUE if character "c" can be used as the first character in a
21182 * variable or function name (excluding '{' and '}').
21183 */
21184 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021185eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021186{
21187 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188}
21189
21190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 * Set number v: variable to "val".
21192 */
21193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021194set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021196 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197}
21198
21199/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021200 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 */
21202 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021203get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021205 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206}
21207
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021208/*
21209 * Get string v: variable value. Uses a static buffer, can only be used once.
21210 */
21211 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021212get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021213{
21214 return get_tv_string(&vimvars[idx].vv_tv);
21215}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021216
Bram Moolenaar071d4272004-06-13 20:20:40 +000021217/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021218 * Get List v: variable value. Caller must take care of reference count when
21219 * needed.
21220 */
21221 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021222get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021223{
21224 return vimvars[idx].vv_list;
21225}
21226
21227/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021228 * Set v:char to character "c".
21229 */
21230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021231set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021232{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021233 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021234
21235#ifdef FEAT_MBYTE
21236 if (has_mbyte)
21237 buf[(*mb_char2bytes)(c, buf)] = NUL;
21238 else
21239#endif
21240 {
21241 buf[0] = c;
21242 buf[1] = NUL;
21243 }
21244 set_vim_var_string(VV_CHAR, buf, -1);
21245}
21246
21247/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021248 * Set v:count to "count" and v:count1 to "count1".
21249 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 */
21251 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021252set_vcount(
21253 long count,
21254 long count1,
21255 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021257 if (set_prevcount)
21258 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021259 vimvars[VV_COUNT].vv_nr = count;
21260 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261}
21262
21263/*
21264 * Set string v: variable to a copy of "val".
21265 */
21266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021267set_vim_var_string(
21268 int idx,
21269 char_u *val,
21270 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271{
Bram Moolenaara542c682016-01-31 16:28:04 +010021272 clear_tv(&vimvars[idx].vv_di.di_tv);
21273 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021275 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021277 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021279 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280}
21281
21282/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021283 * Set List v: variable to "val".
21284 */
21285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021286set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021287{
Bram Moolenaara542c682016-01-31 16:28:04 +010021288 clear_tv(&vimvars[idx].vv_di.di_tv);
21289 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021290 vimvars[idx].vv_list = val;
21291 if (val != NULL)
21292 ++val->lv_refcount;
21293}
21294
21295/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021296 * Set Dictionary v: variable to "val".
21297 */
21298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021299set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021300{
21301 int todo;
21302 hashitem_T *hi;
21303
Bram Moolenaara542c682016-01-31 16:28:04 +010021304 clear_tv(&vimvars[idx].vv_di.di_tv);
21305 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021306 vimvars[idx].vv_dict = val;
21307 if (val != NULL)
21308 {
21309 ++val->dv_refcount;
21310
21311 /* Set readonly */
21312 todo = (int)val->dv_hashtab.ht_used;
21313 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21314 {
21315 if (HASHITEM_EMPTY(hi))
21316 continue;
21317 --todo;
21318 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21319 }
21320 }
21321}
21322
21323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 * Set v:register if needed.
21325 */
21326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021327set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328{
21329 char_u regname;
21330
21331 if (c == 0 || c == ' ')
21332 regname = '"';
21333 else
21334 regname = c;
21335 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021336 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 set_vim_var_string(VV_REG, &regname, 1);
21338}
21339
21340/*
21341 * Get or set v:exception. If "oldval" == NULL, return the current value.
21342 * Otherwise, restore the value to "oldval" and return NULL.
21343 * Must always be called in pairs to save and restore v:exception! Does not
21344 * take care of memory allocations.
21345 */
21346 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021347v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348{
21349 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021350 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351
Bram Moolenaare9a41262005-01-15 22:18:47 +000021352 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353 return NULL;
21354}
21355
21356/*
21357 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21358 * Otherwise, restore the value to "oldval" and return NULL.
21359 * Must always be called in pairs to save and restore v:throwpoint! Does not
21360 * take care of memory allocations.
21361 */
21362 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021363v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364{
21365 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021366 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367
Bram Moolenaare9a41262005-01-15 22:18:47 +000021368 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 return NULL;
21370}
21371
21372#if defined(FEAT_AUTOCMD) || defined(PROTO)
21373/*
21374 * Set v:cmdarg.
21375 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21376 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21377 * Must always be called in pairs!
21378 */
21379 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021380set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381{
21382 char_u *oldval;
21383 char_u *newval;
21384 unsigned len;
21385
Bram Moolenaare9a41262005-01-15 22:18:47 +000021386 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021387 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021389 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021390 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021391 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392 }
21393
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021394 if (eap->force_bin == FORCE_BIN)
21395 len = 6;
21396 else if (eap->force_bin == FORCE_NOBIN)
21397 len = 8;
21398 else
21399 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021400
21401 if (eap->read_edit)
21402 len += 7;
21403
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021404 if (eap->force_ff != 0)
21405 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21406# ifdef FEAT_MBYTE
21407 if (eap->force_enc != 0)
21408 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021409 if (eap->bad_char != 0)
21410 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021411# endif
21412
21413 newval = alloc(len + 1);
21414 if (newval == NULL)
21415 return NULL;
21416
21417 if (eap->force_bin == FORCE_BIN)
21418 sprintf((char *)newval, " ++bin");
21419 else if (eap->force_bin == FORCE_NOBIN)
21420 sprintf((char *)newval, " ++nobin");
21421 else
21422 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021423
21424 if (eap->read_edit)
21425 STRCAT(newval, " ++edit");
21426
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021427 if (eap->force_ff != 0)
21428 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21429 eap->cmd + eap->force_ff);
21430# ifdef FEAT_MBYTE
21431 if (eap->force_enc != 0)
21432 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21433 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021434 if (eap->bad_char == BAD_KEEP)
21435 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21436 else if (eap->bad_char == BAD_DROP)
21437 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21438 else if (eap->bad_char != 0)
21439 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021440# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021441 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021442 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443}
21444#endif
21445
21446/*
21447 * Get the value of internal variable "name".
21448 * Return OK or FAIL.
21449 */
21450 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021451get_var_tv(
21452 char_u *name,
21453 int len, /* length of "name" */
21454 typval_T *rettv, /* NULL when only checking existence */
21455 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21456 int verbose, /* may give error message */
21457 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458{
21459 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021460 typval_T *tv = NULL;
21461 typval_T atv;
21462 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464
21465 /* truncate the name, so that we can use strcmp() */
21466 cc = name[len];
21467 name[len] = NUL;
21468
21469 /*
21470 * Check for "b:changedtick".
21471 */
21472 if (STRCMP(name, "b:changedtick") == 0)
21473 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021474 atv.v_type = VAR_NUMBER;
21475 atv.vval.v_number = curbuf->b_changedtick;
21476 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 }
21478
21479 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 * Check for user-defined variables.
21481 */
21482 else
21483 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021484 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021486 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021487 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021488 if (dip != NULL)
21489 *dip = v;
21490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 }
21492
Bram Moolenaare9a41262005-01-15 22:18:47 +000021493 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021495 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021496 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497 ret = FAIL;
21498 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021499 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021500 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501
21502 name[len] = cc;
21503
21504 return ret;
21505}
21506
21507/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021508 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21509 * Also handle function call with Funcref variable: func(expr)
21510 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21511 */
21512 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021513handle_subscript(
21514 char_u **arg,
21515 typval_T *rettv,
21516 int evaluate, /* do more than finding the end */
21517 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021518{
21519 int ret = OK;
21520 dict_T *selfdict = NULL;
21521 char_u *s;
21522 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021523 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021524
21525 while (ret == OK
21526 && (**arg == '['
21527 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021528 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021529 && !vim_iswhite(*(*arg - 1)))
21530 {
21531 if (**arg == '(')
21532 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021533 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021534 if (evaluate)
21535 {
21536 functv = *rettv;
21537 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021538
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021539 /* Invoke the function. Recursive! */
21540 s = functv.vval.v_string;
21541 }
21542 else
21543 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021544 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021545 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21546 &len, evaluate, selfdict);
21547
21548 /* Clear the funcref afterwards, so that deleting it while
21549 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021550 if (evaluate)
21551 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021552
21553 /* Stop the expression evaluation when immediately aborting on
21554 * error, or when an interrupt occurred or an exception was thrown
21555 * but not caught. */
21556 if (aborting())
21557 {
21558 if (ret == OK)
21559 clear_tv(rettv);
21560 ret = FAIL;
21561 }
21562 dict_unref(selfdict);
21563 selfdict = NULL;
21564 }
21565 else /* **arg == '[' || **arg == '.' */
21566 {
21567 dict_unref(selfdict);
21568 if (rettv->v_type == VAR_DICT)
21569 {
21570 selfdict = rettv->vval.v_dict;
21571 if (selfdict != NULL)
21572 ++selfdict->dv_refcount;
21573 }
21574 else
21575 selfdict = NULL;
21576 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21577 {
21578 clear_tv(rettv);
21579 ret = FAIL;
21580 }
21581 }
21582 }
21583 dict_unref(selfdict);
21584 return ret;
21585}
21586
21587/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021588 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021589 * value).
21590 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021591 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021592alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021593{
Bram Moolenaar33570922005-01-25 22:26:29 +000021594 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021595}
21596
21597/*
21598 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599 * The string "s" must have been allocated, it is consumed.
21600 * Return NULL for out of memory, the variable otherwise.
21601 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021602 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021603alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604{
Bram Moolenaar33570922005-01-25 22:26:29 +000021605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021607 rettv = alloc_tv();
21608 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021610 rettv->v_type = VAR_STRING;
21611 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 }
21613 else
21614 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021615 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616}
21617
21618/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021619 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021622free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623{
21624 if (varp != NULL)
21625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021626 switch (varp->v_type)
21627 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021628 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021629 func_unref(varp->vval.v_string);
21630 /*FALLTHROUGH*/
21631 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021632 vim_free(varp->vval.v_string);
21633 break;
21634 case VAR_LIST:
21635 list_unref(varp->vval.v_list);
21636 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021637 case VAR_DICT:
21638 dict_unref(varp->vval.v_dict);
21639 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021640 case VAR_JOB:
21641#ifdef FEAT_JOB
21642 job_unref(varp->vval.v_job);
21643 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021644#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021645 case VAR_CHANNEL:
21646#ifdef FEAT_CHANNEL
21647 channel_unref(varp->vval.v_channel);
21648 break;
21649#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021650 case VAR_NUMBER:
21651 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021652 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021653 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021654 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 vim_free(varp);
21657 }
21658}
21659
21660/*
21661 * Free the memory for a variable value and set the value to NULL or 0.
21662 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021664clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665{
21666 if (varp != NULL)
21667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021668 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021670 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021671 func_unref(varp->vval.v_string);
21672 /*FALLTHROUGH*/
21673 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021674 vim_free(varp->vval.v_string);
21675 varp->vval.v_string = NULL;
21676 break;
21677 case VAR_LIST:
21678 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021679 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021680 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021681 case VAR_DICT:
21682 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021683 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021684 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021685 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021686 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021687 varp->vval.v_number = 0;
21688 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021689 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021690#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021691 varp->vval.v_float = 0.0;
21692 break;
21693#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021694 case VAR_JOB:
21695#ifdef FEAT_JOB
21696 job_unref(varp->vval.v_job);
21697 varp->vval.v_job = NULL;
21698#endif
21699 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021700 case VAR_CHANNEL:
21701#ifdef FEAT_CHANNEL
21702 channel_unref(varp->vval.v_channel);
21703 varp->vval.v_channel = NULL;
21704#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021705 case VAR_UNKNOWN:
21706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021708 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 }
21710}
21711
21712/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021713 * Set the value of a variable to NULL without freeing items.
21714 */
21715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021716init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021717{
21718 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021719 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021720}
21721
21722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723 * Get the number value of a variable.
21724 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021725 * For incompatible types, return 0.
21726 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21727 * caller of incompatible types: it sets *denote to TRUE if "denote"
21728 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 */
21730 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021731get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021733 int error = FALSE;
21734
21735 return get_tv_number_chk(varp, &error); /* return 0L on error */
21736}
21737
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021738 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021739get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021740{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021741 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021743 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021745 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021746 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021747 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021748#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021749 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021750 break;
21751#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021752 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021753 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021754 break;
21755 case VAR_STRING:
21756 if (varp->vval.v_string != NULL)
21757 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021758 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021759 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021760 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021761 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021762 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021763 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021764 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021765 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021766 case VAR_SPECIAL:
21767 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21768 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021769 case VAR_JOB:
21770#ifdef FEAT_JOB
21771 EMSG(_("E910: Using a Job as a Number"));
21772 break;
21773#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021774 case VAR_CHANNEL:
21775#ifdef FEAT_CHANNEL
21776 EMSG(_("E913: Using a Channel as a Number"));
21777 break;
21778#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021779 case VAR_UNKNOWN:
21780 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021781 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021783 if (denote == NULL) /* useful for values that must be unsigned */
21784 n = -1;
21785 else
21786 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021787 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021788}
21789
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021790#ifdef FEAT_FLOAT
21791 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021792get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021793{
21794 switch (varp->v_type)
21795 {
21796 case VAR_NUMBER:
21797 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021798 case VAR_FLOAT:
21799 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021800 case VAR_FUNC:
21801 EMSG(_("E891: Using a Funcref as a Float"));
21802 break;
21803 case VAR_STRING:
21804 EMSG(_("E892: Using a String as a Float"));
21805 break;
21806 case VAR_LIST:
21807 EMSG(_("E893: Using a List as a Float"));
21808 break;
21809 case VAR_DICT:
21810 EMSG(_("E894: Using a Dictionary as a Float"));
21811 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021812 case VAR_SPECIAL:
21813 EMSG(_("E907: Using a special value as a Float"));
21814 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021815 case VAR_JOB:
21816# ifdef FEAT_JOB
21817 EMSG(_("E911: Using a Job as a Float"));
21818 break;
21819# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021820 case VAR_CHANNEL:
21821# ifdef FEAT_CHANNEL
21822 EMSG(_("E914: Using a Channel as a Float"));
21823 break;
21824# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021825 case VAR_UNKNOWN:
21826 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021827 break;
21828 }
21829 return 0;
21830}
21831#endif
21832
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021834 * Get the lnum from the first argument.
21835 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021836 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 */
21838 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021839get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840{
Bram Moolenaar33570922005-01-25 22:26:29 +000021841 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021842 linenr_T lnum;
21843
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021844 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 if (lnum == 0) /* no valid number, try using line() */
21846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021847 rettv.v_type = VAR_NUMBER;
21848 f_line(argvars, &rettv);
21849 lnum = rettv.vval.v_number;
21850 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 }
21852 return lnum;
21853}
21854
21855/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021856 * Get the lnum from the first argument.
21857 * Also accepts "$", then "buf" is used.
21858 * Returns 0 on error.
21859 */
21860 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021861get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021862{
21863 if (argvars[0].v_type == VAR_STRING
21864 && argvars[0].vval.v_string != NULL
21865 && argvars[0].vval.v_string[0] == '$'
21866 && buf != NULL)
21867 return buf->b_ml.ml_line_count;
21868 return get_tv_number_chk(&argvars[0], NULL);
21869}
21870
21871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 * Get the string value of a variable.
21873 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021874 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21875 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876 * If the String variable has never been set, return an empty string.
21877 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021878 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21879 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 */
21881 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021882get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883{
21884 static char_u mybuf[NUMBUFLEN];
21885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021886 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887}
21888
21889 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021890get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021892 char_u *res = get_tv_string_buf_chk(varp, buf);
21893
21894 return res != NULL ? res : (char_u *)"";
21895}
21896
Bram Moolenaar7d647822014-04-05 21:28:56 +020021897/*
21898 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21899 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021900 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021901get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021902{
21903 static char_u mybuf[NUMBUFLEN];
21904
21905 return get_tv_string_buf_chk(varp, mybuf);
21906}
21907
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021908 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021909get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021910{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021911 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021913 case VAR_NUMBER:
21914 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21915 return buf;
21916 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021917 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021918 break;
21919 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021920 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021921 break;
21922 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021923 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021924 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021925 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021926#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021927 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021928 break;
21929#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021930 case VAR_STRING:
21931 if (varp->vval.v_string != NULL)
21932 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021933 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021934 case VAR_SPECIAL:
21935 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21936 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021937 case VAR_JOB:
21938#ifdef FEAT_JOB
21939 {
21940 job_T *job = varp->vval.v_job;
21941 char *status = job->jv_status == JOB_FAILED ? "fail"
21942 : job->jv_status == JOB_ENDED ? "dead"
21943 : "run";
21944# ifdef UNIX
21945 vim_snprintf((char *)buf, NUMBUFLEN,
21946 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021947# elif defined(WIN32)
21948 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021949 "process %ld %s",
21950 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021951 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021952# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021953 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021954 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21955# endif
21956 return buf;
21957 }
21958#endif
21959 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021960 case VAR_CHANNEL:
21961#ifdef FEAT_CHANNEL
21962 {
21963 channel_T *channel = varp->vval.v_channel;
21964 char *status = channel_status(channel);
21965
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021966 if (channel == NULL)
21967 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21968 else
21969 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021970 "channel %d %s", channel->ch_id, status);
21971 return buf;
21972 }
21973#endif
21974 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021975 case VAR_UNKNOWN:
21976 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021977 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021979 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980}
21981
21982/*
21983 * Find variable "name" in the list of variables.
21984 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021985 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021986 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021987 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021989 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021990find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021993 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994
Bram Moolenaara7043832005-01-21 11:56:39 +000021995 ht = find_var_ht(name, &varname);
21996 if (htp != NULL)
21997 *htp = ht;
21998 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022000 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001}
22002
22003/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022004 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022005 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022007 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022008find_var_in_ht(
22009 hashtab_T *ht,
22010 int htname,
22011 char_u *varname,
22012 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022013{
Bram Moolenaar33570922005-01-25 22:26:29 +000022014 hashitem_T *hi;
22015
22016 if (*varname == NUL)
22017 {
22018 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022019 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022020 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022021 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022022 case 'g': return &globvars_var;
22023 case 'v': return &vimvars_var;
22024 case 'b': return &curbuf->b_bufvar;
22025 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022026#ifdef FEAT_WINDOWS
22027 case 't': return &curtab->tp_winvar;
22028#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022029 case 'l': return current_funccal == NULL
22030 ? NULL : &current_funccal->l_vars_var;
22031 case 'a': return current_funccal == NULL
22032 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022033 }
22034 return NULL;
22035 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022036
22037 hi = hash_find(ht, varname);
22038 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022039 {
22040 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022041 * worked find the variable again. Don't auto-load a script if it was
22042 * loaded already, otherwise it would be loaded every time when
22043 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022044 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022045 {
22046 /* Note: script_autoload() may make "hi" invalid. It must either
22047 * be obtained again or not used. */
22048 if (!script_autoload(varname, FALSE) || aborting())
22049 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022050 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022051 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022052 if (HASHITEM_EMPTY(hi))
22053 return NULL;
22054 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022055 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022056}
22057
22058/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022059 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022060 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022061 * Set "varname" to the start of name without ':'.
22062 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022063 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022064find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022066 hashitem_T *hi;
22067
Bram Moolenaar73627d02015-08-11 15:46:09 +020022068 if (name[0] == NUL)
22069 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 if (name[1] != ':')
22071 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022072 /* The name must not start with a colon or #. */
22073 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074 return NULL;
22075 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022076
22077 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022078 hi = hash_find(&compat_hashtab, name);
22079 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022080 return &compat_hashtab;
22081
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022083 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022084 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 }
22086 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022087 if (*name == 'g') /* global variable */
22088 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022089 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22090 */
22091 if (vim_strchr(name + 2, ':') != NULL
22092 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022093 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022095 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022097 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022098#ifdef FEAT_WINDOWS
22099 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022100 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022101#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022102 if (*name == 'v') /* v: variable */
22103 return &vimvarht;
22104 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022105 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022106 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022107 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 if (*name == 's' /* script variable */
22109 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22110 return &SCRIPT_VARS(current_SID);
22111 return NULL;
22112}
22113
22114/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022115 * Get function call environment based on bactrace debug level
22116 */
22117 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022118get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022119{
22120 int i;
22121 funccall_T *funccal;
22122 funccall_T *temp_funccal;
22123
22124 funccal = current_funccal;
22125 if (debug_backtrace_level > 0)
22126 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022127 for (i = 0; i < debug_backtrace_level; i++)
22128 {
22129 temp_funccal = funccal->caller;
22130 if (temp_funccal)
22131 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022132 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022133 /* backtrace level overflow. reset to max */
22134 debug_backtrace_level = i;
22135 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022136 }
22137 return funccal;
22138}
22139
22140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022142 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 * Returns NULL when it doesn't exist.
22144 */
22145 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022146get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147{
Bram Moolenaar33570922005-01-25 22:26:29 +000022148 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022150 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022151 if (v == NULL)
22152 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022153 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154}
22155
22156/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022157 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 * sourcing this script and when executing functions defined in the script.
22159 */
22160 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022161new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162{
Bram Moolenaara7043832005-01-21 11:56:39 +000022163 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022164 hashtab_T *ht;
22165 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022166
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22168 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022169 /* Re-allocating ga_data means that an ht_array pointing to
22170 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022171 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022172 for (i = 1; i <= ga_scripts.ga_len; ++i)
22173 {
22174 ht = &SCRIPT_VARS(i);
22175 if (ht->ht_mask == HT_INIT_SIZE - 1)
22176 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022177 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022178 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022179 }
22180
Bram Moolenaar071d4272004-06-13 20:20:40 +000022181 while (ga_scripts.ga_len < id)
22182 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022183 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022184 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022185 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022186 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 }
22188 }
22189}
22190
22191/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022192 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22193 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 */
22195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022196init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197{
Bram Moolenaar33570922005-01-25 22:26:29 +000022198 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022199 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022200 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022201 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022202 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022203 dict_var->di_tv.vval.v_dict = dict;
22204 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022205 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022206 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22207 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208}
22209
22210/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022211 * Unreference a dictionary initialized by init_var_dict().
22212 */
22213 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022214unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022215{
22216 /* Now the dict needs to be freed if no one else is using it, go back to
22217 * normal reference counting. */
22218 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22219 dict_unref(dict);
22220}
22221
22222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022224 * Frees all allocated variables and the value they contain.
22225 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 */
22227 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022228vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022229{
22230 vars_clear_ext(ht, TRUE);
22231}
22232
22233/*
22234 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22235 */
22236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022237vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238{
Bram Moolenaara7043832005-01-21 11:56:39 +000022239 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022240 hashitem_T *hi;
22241 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242
Bram Moolenaar33570922005-01-25 22:26:29 +000022243 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022244 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022245 for (hi = ht->ht_array; todo > 0; ++hi)
22246 {
22247 if (!HASHITEM_EMPTY(hi))
22248 {
22249 --todo;
22250
Bram Moolenaar33570922005-01-25 22:26:29 +000022251 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022252 * ht_array might change then. hash_clear() takes care of it
22253 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022254 v = HI2DI(hi);
22255 if (free_val)
22256 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022257 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022258 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022259 }
22260 }
22261 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022262 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263}
22264
Bram Moolenaara7043832005-01-21 11:56:39 +000022265/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022266 * Delete a variable from hashtab "ht" at item "hi".
22267 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022270delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271{
Bram Moolenaar33570922005-01-25 22:26:29 +000022272 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022273
22274 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022275 clear_tv(&di->di_tv);
22276 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277}
22278
22279/*
22280 * List the value of one internal variable.
22281 */
22282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022283list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022285 char_u *tofree;
22286 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022287 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022288
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022289 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022290 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022291 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022292 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293}
22294
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022296list_one_var_a(
22297 char_u *prefix,
22298 char_u *name,
22299 int type,
22300 char_u *string,
22301 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302{
Bram Moolenaar31859182007-08-14 20:41:13 +000022303 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22304 msg_start();
22305 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306 if (name != NULL) /* "a:" vars don't have a name stored */
22307 msg_puts(name);
22308 msg_putchar(' ');
22309 msg_advance(22);
22310 if (type == VAR_NUMBER)
22311 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022312 else if (type == VAR_FUNC)
22313 msg_putchar('*');
22314 else if (type == VAR_LIST)
22315 {
22316 msg_putchar('[');
22317 if (*string == '[')
22318 ++string;
22319 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022320 else if (type == VAR_DICT)
22321 {
22322 msg_putchar('{');
22323 if (*string == '{')
22324 ++string;
22325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 else
22327 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022328
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022330
22331 if (type == VAR_FUNC)
22332 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022333 if (*first)
22334 {
22335 msg_clr_eos();
22336 *first = FALSE;
22337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338}
22339
22340/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022341 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 * If the variable already exists, the value is updated.
22343 * Otherwise the variable is created.
22344 */
22345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022346set_var(
22347 char_u *name,
22348 typval_T *tv,
22349 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350{
Bram Moolenaar33570922005-01-25 22:26:29 +000022351 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022353 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022355 ht = find_var_ht(name, &varname);
22356 if (ht == NULL || *varname == NUL)
22357 {
22358 EMSG2(_(e_illvar), name);
22359 return;
22360 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022361 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022362
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022363 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22364 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022365
Bram Moolenaar33570922005-01-25 22:26:29 +000022366 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022368 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022369 if (var_check_ro(v->di_flags, name, FALSE)
22370 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022371 return;
22372 if (v->di_tv.v_type != tv->v_type
22373 && !((v->di_tv.v_type == VAR_STRING
22374 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022375 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022376 || tv->v_type == VAR_NUMBER))
22377#ifdef FEAT_FLOAT
22378 && !((v->di_tv.v_type == VAR_NUMBER
22379 || v->di_tv.v_type == VAR_FLOAT)
22380 && (tv->v_type == VAR_NUMBER
22381 || tv->v_type == VAR_FLOAT))
22382#endif
22383 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022384 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022385 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022386 return;
22387 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022388
22389 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022390 * Handle setting internal v: variables separately where needed to
22391 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022392 */
22393 if (ht == &vimvarht)
22394 {
22395 if (v->di_tv.v_type == VAR_STRING)
22396 {
22397 vim_free(v->di_tv.vval.v_string);
22398 if (copy || tv->v_type != VAR_STRING)
22399 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22400 else
22401 {
22402 /* Take over the string to avoid an extra alloc/free. */
22403 v->di_tv.vval.v_string = tv->vval.v_string;
22404 tv->vval.v_string = NULL;
22405 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022406 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022407 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022408 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022409 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022410 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022411 if (STRCMP(varname, "searchforward") == 0)
22412 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022413#ifdef FEAT_SEARCH_EXTRA
22414 else if (STRCMP(varname, "hlsearch") == 0)
22415 {
22416 no_hlsearch = !v->di_tv.vval.v_number;
22417 redraw_all_later(SOME_VALID);
22418 }
22419#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022420 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022421 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022422 else if (v->di_tv.v_type != tv->v_type)
22423 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022424 }
22425
22426 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 }
22428 else /* add a new variable */
22429 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022430 /* Can't add "v:" variable. */
22431 if (ht == &vimvarht)
22432 {
22433 EMSG2(_(e_illvar), name);
22434 return;
22435 }
22436
Bram Moolenaar92124a32005-06-17 22:03:40 +000022437 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022438 if (!valid_varname(varname))
22439 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022440
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022441 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22442 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022443 if (v == NULL)
22444 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022445 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022446 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022448 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 return;
22450 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022451 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022453
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022454 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022455 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022456 else
22457 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022459 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022460 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462}
22463
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022464/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022465 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022466 * Also give an error message.
22467 */
22468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022469var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022470{
22471 if (flags & DI_FLAGS_RO)
22472 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022473 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022474 return TRUE;
22475 }
22476 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22477 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022478 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022479 return TRUE;
22480 }
22481 return FALSE;
22482}
22483
22484/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022485 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22486 * Also give an error message.
22487 */
22488 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022489var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022490{
22491 if (flags & DI_FLAGS_FIX)
22492 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022493 EMSG2(_("E795: Cannot delete variable %s"),
22494 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022495 return TRUE;
22496 }
22497 return FALSE;
22498}
22499
22500/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022501 * Check if a funcref is assigned to a valid variable name.
22502 * Return TRUE and give an error if not.
22503 */
22504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022505var_check_func_name(
22506 char_u *name, /* points to start of variable name */
22507 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022508{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022509 /* Allow for w: b: s: and t:. */
22510 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022511 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22512 ? name[2] : name[0]))
22513 {
22514 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22515 name);
22516 return TRUE;
22517 }
22518 /* Don't allow hiding a function. When "v" is not NULL we might be
22519 * assigning another function to the same var, the type is checked
22520 * below. */
22521 if (new_var && function_exists(name))
22522 {
22523 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22524 name);
22525 return TRUE;
22526 }
22527 return FALSE;
22528}
22529
22530/*
22531 * Check if a variable name is valid.
22532 * Return FALSE and give an error if not.
22533 */
22534 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022535valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022536{
22537 char_u *p;
22538
22539 for (p = varname; *p != NUL; ++p)
22540 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22541 && *p != AUTOLOAD_CHAR)
22542 {
22543 EMSG2(_(e_illvar), varname);
22544 return FALSE;
22545 }
22546 return TRUE;
22547}
22548
22549/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022550 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022551 * Also give an error message, using "name" or _("name") when use_gettext is
22552 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022553 */
22554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022555tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022556{
22557 if (lock & VAR_LOCKED)
22558 {
22559 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022560 name == NULL ? (char_u *)_("Unknown")
22561 : use_gettext ? (char_u *)_(name)
22562 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022563 return TRUE;
22564 }
22565 if (lock & VAR_FIXED)
22566 {
22567 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022568 name == NULL ? (char_u *)_("Unknown")
22569 : use_gettext ? (char_u *)_(name)
22570 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022571 return TRUE;
22572 }
22573 return FALSE;
22574}
22575
22576/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022577 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022578 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022579 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022580 * It is OK for "from" and "to" to point to the same item. This is used to
22581 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022582 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022584copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022586 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022587 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022588 switch (from->v_type)
22589 {
22590 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022591 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022592 to->vval.v_number = from->vval.v_number;
22593 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022594 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022595#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022596 to->vval.v_float = from->vval.v_float;
22597 break;
22598#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022599 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010022600#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010022601 to->vval.v_job = from->vval.v_job;
22602 ++to->vval.v_job->jv_refcount;
22603 break;
22604#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022605 case VAR_CHANNEL:
22606#ifdef FEAT_CHANNEL
22607 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022608 if (to->vval.v_channel != NULL)
22609 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022610 break;
22611#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022612 case VAR_STRING:
22613 case VAR_FUNC:
22614 if (from->vval.v_string == NULL)
22615 to->vval.v_string = NULL;
22616 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022618 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022619 if (from->v_type == VAR_FUNC)
22620 func_ref(to->vval.v_string);
22621 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022622 break;
22623 case VAR_LIST:
22624 if (from->vval.v_list == NULL)
22625 to->vval.v_list = NULL;
22626 else
22627 {
22628 to->vval.v_list = from->vval.v_list;
22629 ++to->vval.v_list->lv_refcount;
22630 }
22631 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022632 case VAR_DICT:
22633 if (from->vval.v_dict == NULL)
22634 to->vval.v_dict = NULL;
22635 else
22636 {
22637 to->vval.v_dict = from->vval.v_dict;
22638 ++to->vval.v_dict->dv_refcount;
22639 }
22640 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022641 case VAR_UNKNOWN:
22642 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022643 break;
22644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645}
22646
22647/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022648 * Make a copy of an item.
22649 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022650 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22651 * reference to an already copied list/dict can be used.
22652 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022653 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022655item_copy(
22656 typval_T *from,
22657 typval_T *to,
22658 int deep,
22659 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022660{
22661 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022662 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022663
Bram Moolenaar33570922005-01-25 22:26:29 +000022664 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022665 {
22666 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022667 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022668 }
22669 ++recurse;
22670
22671 switch (from->v_type)
22672 {
22673 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022674 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022675 case VAR_STRING:
22676 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022677 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022678 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022679 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022680 copy_tv(from, to);
22681 break;
22682 case VAR_LIST:
22683 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022684 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022685 if (from->vval.v_list == NULL)
22686 to->vval.v_list = NULL;
22687 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22688 {
22689 /* use the copy made earlier */
22690 to->vval.v_list = from->vval.v_list->lv_copylist;
22691 ++to->vval.v_list->lv_refcount;
22692 }
22693 else
22694 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22695 if (to->vval.v_list == NULL)
22696 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022697 break;
22698 case VAR_DICT:
22699 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022700 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022701 if (from->vval.v_dict == NULL)
22702 to->vval.v_dict = NULL;
22703 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22704 {
22705 /* use the copy made earlier */
22706 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22707 ++to->vval.v_dict->dv_refcount;
22708 }
22709 else
22710 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22711 if (to->vval.v_dict == NULL)
22712 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022713 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022714 case VAR_UNKNOWN:
22715 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022716 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022717 }
22718 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022719 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022720}
22721
22722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 * ":echo expr1 ..." print each argument separated with a space, add a
22724 * newline at the end.
22725 * ":echon expr1 ..." print each argument plain.
22726 */
22727 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022728ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729{
22730 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022731 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022732 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 char_u *p;
22734 int needclr = TRUE;
22735 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022736 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737
22738 if (eap->skip)
22739 ++emsg_skip;
22740 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22741 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022742 /* If eval1() causes an error message the text from the command may
22743 * still need to be cleared. E.g., "echo 22,44". */
22744 need_clr_eos = needclr;
22745
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022747 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 {
22749 /*
22750 * Report the invalid expression unless the expression evaluation
22751 * has been cancelled due to an aborting error, an interrupt, or an
22752 * exception.
22753 */
22754 if (!aborting())
22755 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022756 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022757 break;
22758 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022759 need_clr_eos = FALSE;
22760
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 if (!eap->skip)
22762 {
22763 if (atstart)
22764 {
22765 atstart = FALSE;
22766 /* Call msg_start() after eval1(), evaluating the expression
22767 * may cause a message to appear. */
22768 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022769 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022770 /* Mark the saved text as finishing the line, so that what
22771 * follows is displayed on a new line when scrolling back
22772 * at the more prompt. */
22773 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 }
22777 else if (eap->cmdidx == CMD_echo)
22778 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022779 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022780 if (p != NULL)
22781 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022783 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022785 if (*p != TAB && needclr)
22786 {
22787 /* remove any text still there from the command */
22788 msg_clr_eos();
22789 needclr = FALSE;
22790 }
22791 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 }
22793 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022794 {
22795#ifdef FEAT_MBYTE
22796 if (has_mbyte)
22797 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022798 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022799
22800 (void)msg_outtrans_len_attr(p, i, echo_attr);
22801 p += i - 1;
22802 }
22803 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022805 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022808 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022810 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 arg = skipwhite(arg);
22812 }
22813 eap->nextcmd = check_nextcmd(arg);
22814
22815 if (eap->skip)
22816 --emsg_skip;
22817 else
22818 {
22819 /* remove text that may still be there from the command */
22820 if (needclr)
22821 msg_clr_eos();
22822 if (eap->cmdidx == CMD_echo)
22823 msg_end();
22824 }
22825}
22826
22827/*
22828 * ":echohl {name}".
22829 */
22830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022831ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832{
22833 int id;
22834
22835 id = syn_name2id(eap->arg);
22836 if (id == 0)
22837 echo_attr = 0;
22838 else
22839 echo_attr = syn_id2attr(id);
22840}
22841
22842/*
22843 * ":execute expr1 ..." execute the result of an expression.
22844 * ":echomsg expr1 ..." Print a message
22845 * ":echoerr expr1 ..." Print an error
22846 * Each gets spaces around each argument and a newline at the end for
22847 * echo commands
22848 */
22849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022850ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851{
22852 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022853 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 int ret = OK;
22855 char_u *p;
22856 garray_T ga;
22857 int len;
22858 int save_did_emsg;
22859
22860 ga_init2(&ga, 1, 80);
22861
22862 if (eap->skip)
22863 ++emsg_skip;
22864 while (*arg != NUL && *arg != '|' && *arg != '\n')
22865 {
22866 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022867 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 {
22869 /*
22870 * Report the invalid expression unless the expression evaluation
22871 * has been cancelled due to an aborting error, an interrupt, or an
22872 * exception.
22873 */
22874 if (!aborting())
22875 EMSG2(_(e_invexpr2), p);
22876 ret = FAIL;
22877 break;
22878 }
22879
22880 if (!eap->skip)
22881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022882 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883 len = (int)STRLEN(p);
22884 if (ga_grow(&ga, len + 2) == FAIL)
22885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022886 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 ret = FAIL;
22888 break;
22889 }
22890 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 ga.ga_len += len;
22894 }
22895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 arg = skipwhite(arg);
22898 }
22899
22900 if (ret != FAIL && ga.ga_data != NULL)
22901 {
22902 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022905 out_flush();
22906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 else if (eap->cmdidx == CMD_echoerr)
22908 {
22909 /* We don't want to abort following commands, restore did_emsg. */
22910 save_did_emsg = did_emsg;
22911 EMSG((char_u *)ga.ga_data);
22912 if (!force_abort)
22913 did_emsg = save_did_emsg;
22914 }
22915 else if (eap->cmdidx == CMD_execute)
22916 do_cmdline((char_u *)ga.ga_data,
22917 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22918 }
22919
22920 ga_clear(&ga);
22921
22922 if (eap->skip)
22923 --emsg_skip;
22924
22925 eap->nextcmd = check_nextcmd(arg);
22926}
22927
22928/*
22929 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22930 * "arg" points to the "&" or '+' when called, to "option" when returning.
22931 * Returns NULL when no option name found. Otherwise pointer to the char
22932 * after the option name.
22933 */
22934 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022935find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936{
22937 char_u *p = *arg;
22938
22939 ++p;
22940 if (*p == 'g' && p[1] == ':')
22941 {
22942 *opt_flags = OPT_GLOBAL;
22943 p += 2;
22944 }
22945 else if (*p == 'l' && p[1] == ':')
22946 {
22947 *opt_flags = OPT_LOCAL;
22948 p += 2;
22949 }
22950 else
22951 *opt_flags = 0;
22952
22953 if (!ASCII_ISALPHA(*p))
22954 return NULL;
22955 *arg = p;
22956
22957 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22958 p += 4; /* termcap option */
22959 else
22960 while (ASCII_ISALPHA(*p))
22961 ++p;
22962 return p;
22963}
22964
22965/*
22966 * ":function"
22967 */
22968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022969ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970{
22971 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022972 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 int j;
22974 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022976 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 char_u *name = NULL;
22978 char_u *p;
22979 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022980 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 garray_T newargs;
22982 garray_T newlines;
22983 int varargs = FALSE;
22984 int mustend = FALSE;
22985 int flags = 0;
22986 ufunc_T *fp;
22987 int indent;
22988 int nesting;
22989 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022990 dictitem_T *v;
22991 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022992 static int func_nr = 0; /* number for nameless function */
22993 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022994 hashtab_T *ht;
22995 int todo;
22996 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022997 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998
22999 /*
23000 * ":function" without argument: list functions.
23001 */
23002 if (ends_excmd(*eap->arg))
23003 {
23004 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023005 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023006 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023007 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023008 {
23009 if (!HASHITEM_EMPTY(hi))
23010 {
23011 --todo;
23012 fp = HI2UF(hi);
23013 if (!isdigit(*fp->uf_name))
23014 list_func_head(fp, FALSE);
23015 }
23016 }
23017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 eap->nextcmd = check_nextcmd(eap->arg);
23019 return;
23020 }
23021
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023022 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023023 * ":function /pat": list functions matching pattern.
23024 */
23025 if (*eap->arg == '/')
23026 {
23027 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23028 if (!eap->skip)
23029 {
23030 regmatch_T regmatch;
23031
23032 c = *p;
23033 *p = NUL;
23034 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23035 *p = c;
23036 if (regmatch.regprog != NULL)
23037 {
23038 regmatch.rm_ic = p_ic;
23039
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023040 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023041 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23042 {
23043 if (!HASHITEM_EMPTY(hi))
23044 {
23045 --todo;
23046 fp = HI2UF(hi);
23047 if (!isdigit(*fp->uf_name)
23048 && vim_regexec(&regmatch, fp->uf_name, 0))
23049 list_func_head(fp, FALSE);
23050 }
23051 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023052 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023053 }
23054 }
23055 if (*p == '/')
23056 ++p;
23057 eap->nextcmd = check_nextcmd(p);
23058 return;
23059 }
23060
23061 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023062 * Get the function name. There are these situations:
23063 * func normal function name
23064 * "name" == func, "fudi.fd_dict" == NULL
23065 * dict.func new dictionary entry
23066 * "name" == NULL, "fudi.fd_dict" set,
23067 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23068 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023069 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023070 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23071 * dict.func existing dict entry that's not a Funcref
23072 * "name" == NULL, "fudi.fd_dict" set,
23073 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023074 * s:func script-local function name
23075 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023078 name = trans_function_name(&p, eap->skip, 0, &fudi);
23079 paren = (vim_strchr(p, '(') != NULL);
23080 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 {
23082 /*
23083 * Return on an invalid expression in braces, unless the expression
23084 * evaluation has been cancelled due to an aborting error, an
23085 * interrupt, or an exception.
23086 */
23087 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023088 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023089 if (!eap->skip && fudi.fd_newkey != NULL)
23090 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023091 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 else
23095 eap->skip = TRUE;
23096 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023097
Bram Moolenaar071d4272004-06-13 20:20:40 +000023098 /* An error in a function call during evaluation of an expression in magic
23099 * braces should not cause the function not to be defined. */
23100 saved_did_emsg = did_emsg;
23101 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102
23103 /*
23104 * ":function func" with only function name: list function.
23105 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023106 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023107 {
23108 if (!ends_excmd(*skipwhite(p)))
23109 {
23110 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023111 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 }
23113 eap->nextcmd = check_nextcmd(p);
23114 if (eap->nextcmd != NULL)
23115 *p = NUL;
23116 if (!eap->skip && !got_int)
23117 {
23118 fp = find_func(name);
23119 if (fp != NULL)
23120 {
23121 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023122 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023124 if (FUNCLINE(fp, j) == NULL)
23125 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 msg_putchar('\n');
23127 msg_outnum((long)(j + 1));
23128 if (j < 9)
23129 msg_putchar(' ');
23130 if (j < 99)
23131 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023132 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133 out_flush(); /* show a line at a time */
23134 ui_breakcheck();
23135 }
23136 if (!got_int)
23137 {
23138 msg_putchar('\n');
23139 msg_puts((char_u *)" endfunction");
23140 }
23141 }
23142 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023143 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023145 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146 }
23147
23148 /*
23149 * ":function name(arg1, arg2)" Define function.
23150 */
23151 p = skipwhite(p);
23152 if (*p != '(')
23153 {
23154 if (!eap->skip)
23155 {
23156 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023157 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 }
23159 /* attempt to continue by skipping some text */
23160 if (vim_strchr(p, '(') != NULL)
23161 p = vim_strchr(p, '(');
23162 }
23163 p = skipwhite(p + 1);
23164
23165 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23166 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23167
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023168 if (!eap->skip)
23169 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023170 /* Check the name of the function. Unless it's a dictionary function
23171 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023172 if (name != NULL)
23173 arg = name;
23174 else
23175 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023176 if (arg != NULL && (fudi.fd_di == NULL
23177 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023178 {
23179 if (*arg == K_SPECIAL)
23180 j = 3;
23181 else
23182 j = 0;
23183 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23184 : eval_isnamec(arg[j])))
23185 ++j;
23186 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023187 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023188 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023189 /* Disallow using the g: dict. */
23190 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23191 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023192 }
23193
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 /*
23195 * Isolate the arguments: "arg1, arg2, ...)"
23196 */
23197 while (*p != ')')
23198 {
23199 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23200 {
23201 varargs = TRUE;
23202 p += 3;
23203 mustend = TRUE;
23204 }
23205 else
23206 {
23207 arg = p;
23208 while (ASCII_ISALNUM(*p) || *p == '_')
23209 ++p;
23210 if (arg == p || isdigit(*arg)
23211 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23212 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23213 {
23214 if (!eap->skip)
23215 EMSG2(_("E125: Illegal argument: %s"), arg);
23216 break;
23217 }
23218 if (ga_grow(&newargs, 1) == FAIL)
23219 goto erret;
23220 c = *p;
23221 *p = NUL;
23222 arg = vim_strsave(arg);
23223 if (arg == NULL)
23224 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023225
23226 /* Check for duplicate argument name. */
23227 for (i = 0; i < newargs.ga_len; ++i)
23228 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23229 {
23230 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023231 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023232 goto erret;
23233 }
23234
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23236 *p = c;
23237 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 if (*p == ',')
23239 ++p;
23240 else
23241 mustend = TRUE;
23242 }
23243 p = skipwhite(p);
23244 if (mustend && *p != ')')
23245 {
23246 if (!eap->skip)
23247 EMSG2(_(e_invarg2), eap->arg);
23248 break;
23249 }
23250 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023251 if (*p != ')')
23252 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253 ++p; /* skip the ')' */
23254
Bram Moolenaare9a41262005-01-15 22:18:47 +000023255 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 for (;;)
23257 {
23258 p = skipwhite(p);
23259 if (STRNCMP(p, "range", 5) == 0)
23260 {
23261 flags |= FC_RANGE;
23262 p += 5;
23263 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023264 else if (STRNCMP(p, "dict", 4) == 0)
23265 {
23266 flags |= FC_DICT;
23267 p += 4;
23268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 else if (STRNCMP(p, "abort", 5) == 0)
23270 {
23271 flags |= FC_ABORT;
23272 p += 5;
23273 }
23274 else
23275 break;
23276 }
23277
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023278 /* When there is a line break use what follows for the function body.
23279 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23280 if (*p == '\n')
23281 line_arg = p + 1;
23282 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 EMSG(_(e_trailing));
23284
23285 /*
23286 * Read the body of the function, until ":endfunction" is found.
23287 */
23288 if (KeyTyped)
23289 {
23290 /* Check if the function already exists, don't let the user type the
23291 * whole function before telling him it doesn't work! For a script we
23292 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023293 if (!eap->skip && !eap->forceit)
23294 {
23295 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23296 EMSG(_(e_funcdict));
23297 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023298 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023301 if (!eap->skip && did_emsg)
23302 goto erret;
23303
Bram Moolenaar071d4272004-06-13 20:20:40 +000023304 msg_putchar('\n'); /* don't overwrite the function name */
23305 cmdline_row = msg_row;
23306 }
23307
23308 indent = 2;
23309 nesting = 0;
23310 for (;;)
23311 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023312 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023313 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023314 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023315 saved_wait_return = FALSE;
23316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023318 sourcing_lnum_off = sourcing_lnum;
23319
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023320 if (line_arg != NULL)
23321 {
23322 /* Use eap->arg, split up in parts by line breaks. */
23323 theline = line_arg;
23324 p = vim_strchr(theline, '\n');
23325 if (p == NULL)
23326 line_arg += STRLEN(line_arg);
23327 else
23328 {
23329 *p = NUL;
23330 line_arg = p + 1;
23331 }
23332 }
23333 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023334 theline = getcmdline(':', 0L, indent);
23335 else
23336 theline = eap->getline(':', eap->cookie, indent);
23337 if (KeyTyped)
23338 lines_left = Rows - 1;
23339 if (theline == NULL)
23340 {
23341 EMSG(_("E126: Missing :endfunction"));
23342 goto erret;
23343 }
23344
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023345 /* Detect line continuation: sourcing_lnum increased more than one. */
23346 if (sourcing_lnum > sourcing_lnum_off + 1)
23347 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23348 else
23349 sourcing_lnum_off = 0;
23350
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 if (skip_until != NULL)
23352 {
23353 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23354 * don't check for ":endfunc". */
23355 if (STRCMP(theline, skip_until) == 0)
23356 {
23357 vim_free(skip_until);
23358 skip_until = NULL;
23359 }
23360 }
23361 else
23362 {
23363 /* skip ':' and blanks*/
23364 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23365 ;
23366
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023367 /* Check for "endfunction". */
23368 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023370 if (line_arg == NULL)
23371 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372 break;
23373 }
23374
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023375 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 * at "end". */
23377 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23378 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023379 else if (STRNCMP(p, "if", 2) == 0
23380 || STRNCMP(p, "wh", 2) == 0
23381 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 || STRNCMP(p, "try", 3) == 0)
23383 indent += 2;
23384
23385 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023386 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023388 if (*p == '!')
23389 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023391 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23392 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023394 ++nesting;
23395 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 }
23397 }
23398
23399 /* Check for ":append" or ":insert". */
23400 p = skip_range(p, NULL);
23401 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23402 || (p[0] == 'i'
23403 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23404 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23405 skip_until = vim_strsave((char_u *)".");
23406
23407 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23408 arg = skipwhite(skiptowhite(p));
23409 if (arg[0] == '<' && arg[1] =='<'
23410 && ((p[0] == 'p' && p[1] == 'y'
23411 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23412 || (p[0] == 'p' && p[1] == 'e'
23413 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23414 || (p[0] == 't' && p[1] == 'c'
23415 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023416 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23417 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23419 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023420 || (p[0] == 'm' && p[1] == 'z'
23421 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422 ))
23423 {
23424 /* ":python <<" continues until a dot, like ":append" */
23425 p = skipwhite(arg + 2);
23426 if (*p == NUL)
23427 skip_until = vim_strsave((char_u *)".");
23428 else
23429 skip_until = vim_strsave(p);
23430 }
23431 }
23432
23433 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023434 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023435 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023436 if (line_arg == NULL)
23437 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023439 }
23440
23441 /* Copy the line to newly allocated memory. get_one_sourceline()
23442 * allocates 250 bytes per line, this saves 80% on average. The cost
23443 * is an extra alloc/free. */
23444 p = vim_strsave(theline);
23445 if (p != NULL)
23446 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023447 if (line_arg == NULL)
23448 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023449 theline = p;
23450 }
23451
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023452 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23453
23454 /* Add NULL lines for continuation lines, so that the line count is
23455 * equal to the index in the growarray. */
23456 while (sourcing_lnum_off-- > 0)
23457 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023458
23459 /* Check for end of eap->arg. */
23460 if (line_arg != NULL && *line_arg == NUL)
23461 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462 }
23463
23464 /* Don't define the function when skipping commands or when an error was
23465 * detected. */
23466 if (eap->skip || did_emsg)
23467 goto erret;
23468
23469 /*
23470 * If there are no errors, add the function
23471 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023472 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023473 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023474 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023475 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023476 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023477 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023478 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023479 goto erret;
23480 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023481
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023482 fp = find_func(name);
23483 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023485 if (!eap->forceit)
23486 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023487 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023488 goto erret;
23489 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023490 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023491 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023492 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023493 name);
23494 goto erret;
23495 }
23496 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023497 ga_clear_strings(&(fp->uf_args));
23498 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023499 vim_free(name);
23500 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023502 }
23503 else
23504 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023505 char numbuf[20];
23506
23507 fp = NULL;
23508 if (fudi.fd_newkey == NULL && !eap->forceit)
23509 {
23510 EMSG(_(e_funcdict));
23511 goto erret;
23512 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023513 if (fudi.fd_di == NULL)
23514 {
23515 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023516 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023517 goto erret;
23518 }
23519 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023520 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023521 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023522
23523 /* Give the function a sequential number. Can only be used with a
23524 * Funcref! */
23525 vim_free(name);
23526 sprintf(numbuf, "%d", ++func_nr);
23527 name = vim_strsave((char_u *)numbuf);
23528 if (name == NULL)
23529 goto erret;
23530 }
23531
23532 if (fp == NULL)
23533 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023534 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023535 {
23536 int slen, plen;
23537 char_u *scriptname;
23538
23539 /* Check that the autoload name matches the script name. */
23540 j = FAIL;
23541 if (sourcing_name != NULL)
23542 {
23543 scriptname = autoload_name(name);
23544 if (scriptname != NULL)
23545 {
23546 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023547 plen = (int)STRLEN(p);
23548 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023549 if (slen > plen && fnamecmp(p,
23550 sourcing_name + slen - plen) == 0)
23551 j = OK;
23552 vim_free(scriptname);
23553 }
23554 }
23555 if (j == FAIL)
23556 {
23557 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23558 goto erret;
23559 }
23560 }
23561
23562 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563 if (fp == NULL)
23564 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023565
23566 if (fudi.fd_dict != NULL)
23567 {
23568 if (fudi.fd_di == NULL)
23569 {
23570 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023571 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023572 if (fudi.fd_di == NULL)
23573 {
23574 vim_free(fp);
23575 goto erret;
23576 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023577 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23578 {
23579 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023580 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023581 goto erret;
23582 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023583 }
23584 else
23585 /* overwrite existing dict entry */
23586 clear_tv(&fudi.fd_di->di_tv);
23587 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023588 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023589 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023590 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023591
23592 /* behave like "dict" was used */
23593 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023594 }
23595
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023597 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023598 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23599 {
23600 vim_free(fp);
23601 goto erret;
23602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023604 fp->uf_args = newargs;
23605 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023606#ifdef FEAT_PROFILE
23607 fp->uf_tml_count = NULL;
23608 fp->uf_tml_total = NULL;
23609 fp->uf_tml_self = NULL;
23610 fp->uf_profiling = FALSE;
23611 if (prof_def_func())
23612 func_do_profile(fp);
23613#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023614 fp->uf_varargs = varargs;
23615 fp->uf_flags = flags;
23616 fp->uf_calls = 0;
23617 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023618 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023619
23620erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 ga_clear_strings(&newargs);
23622 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023623ret_free:
23624 vim_free(skip_until);
23625 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023628 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629}
23630
23631/*
23632 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023633 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023635 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023636 * TFN_INT: internal function name OK
23637 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023638 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 * Advances "pp" to just after the function name (if no error).
23640 */
23641 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023642trans_function_name(
23643 char_u **pp,
23644 int skip, /* only find the end, don't evaluate */
23645 int flags,
23646 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023648 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 char_u *start;
23650 char_u *end;
23651 int lead;
23652 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023654 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023655
23656 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023657 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023659
23660 /* Check for hard coded <SNR>: already translated function ID (from a user
23661 * command). */
23662 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23663 && (*pp)[2] == (int)KE_SNR)
23664 {
23665 *pp += 3;
23666 len = get_id_len(pp) + 3;
23667 return vim_strnsave(start, len);
23668 }
23669
23670 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23671 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023673 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023674 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023675
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023676 /* Note that TFN_ flags use the same values as GLV_ flags. */
23677 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023678 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023679 if (end == start)
23680 {
23681 if (!skip)
23682 EMSG(_("E129: Function name required"));
23683 goto theend;
23684 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023685 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023686 {
23687 /*
23688 * Report an invalid expression in braces, unless the expression
23689 * evaluation has been cancelled due to an aborting error, an
23690 * interrupt, or an exception.
23691 */
23692 if (!aborting())
23693 {
23694 if (end != NULL)
23695 EMSG2(_(e_invarg2), start);
23696 }
23697 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023698 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023699 goto theend;
23700 }
23701
23702 if (lv.ll_tv != NULL)
23703 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023704 if (fdp != NULL)
23705 {
23706 fdp->fd_dict = lv.ll_dict;
23707 fdp->fd_newkey = lv.ll_newkey;
23708 lv.ll_newkey = NULL;
23709 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023710 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023711 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23712 {
23713 name = vim_strsave(lv.ll_tv->vval.v_string);
23714 *pp = end;
23715 }
23716 else
23717 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023718 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23719 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023720 EMSG(_(e_funcref));
23721 else
23722 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023723 name = NULL;
23724 }
23725 goto theend;
23726 }
23727
23728 if (lv.ll_name == NULL)
23729 {
23730 /* Error found, but continue after the function name. */
23731 *pp = end;
23732 goto theend;
23733 }
23734
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023735 /* Check if the name is a Funcref. If so, use the value. */
23736 if (lv.ll_exp_name != NULL)
23737 {
23738 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023739 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023740 if (name == lv.ll_exp_name)
23741 name = NULL;
23742 }
23743 else
23744 {
23745 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023746 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023747 if (name == *pp)
23748 name = NULL;
23749 }
23750 if (name != NULL)
23751 {
23752 name = vim_strsave(name);
23753 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023754 if (STRNCMP(name, "<SNR>", 5) == 0)
23755 {
23756 /* Change "<SNR>" to the byte sequence. */
23757 name[0] = K_SPECIAL;
23758 name[1] = KS_EXTRA;
23759 name[2] = (int)KE_SNR;
23760 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23761 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023762 goto theend;
23763 }
23764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023765 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023766 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023767 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023768 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23769 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23770 {
23771 /* When there was "s:" already or the name expanded to get a
23772 * leading "s:" then remove it. */
23773 lv.ll_name += 2;
23774 len -= 2;
23775 lead = 2;
23776 }
23777 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023778 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023779 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023780 /* skip over "s:" and "g:" */
23781 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023782 lv.ll_name += 2;
23783 len = (int)(end - lv.ll_name);
23784 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023785
23786 /*
23787 * Copy the function name to allocated memory.
23788 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23789 * Accept <SNR>123_name() outside a script.
23790 */
23791 if (skip)
23792 lead = 0; /* do nothing */
23793 else if (lead > 0)
23794 {
23795 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023796 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23797 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023798 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023799 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023800 if (current_SID <= 0)
23801 {
23802 EMSG(_(e_usingsid));
23803 goto theend;
23804 }
23805 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23806 lead += (int)STRLEN(sid_buf);
23807 }
23808 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023809 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023810 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023811 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023812 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023813 goto theend;
23814 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023815 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023816 {
23817 char_u *cp = vim_strchr(lv.ll_name, ':');
23818
23819 if (cp != NULL && cp < end)
23820 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023821 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023822 goto theend;
23823 }
23824 }
23825
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023826 name = alloc((unsigned)(len + lead + 1));
23827 if (name != NULL)
23828 {
23829 if (lead > 0)
23830 {
23831 name[0] = K_SPECIAL;
23832 name[1] = KS_EXTRA;
23833 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023834 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023835 STRCPY(name + 3, sid_buf);
23836 }
23837 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023838 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023839 }
23840 *pp = end;
23841
23842theend:
23843 clear_lval(&lv);
23844 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845}
23846
23847/*
23848 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23849 * Return 2 if "p" starts with "s:".
23850 * Return 0 otherwise.
23851 */
23852 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023853eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023855 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23856 * the standard library function. */
23857 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23858 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859 return 5;
23860 if (p[0] == 's' && p[1] == ':')
23861 return 2;
23862 return 0;
23863}
23864
23865/*
23866 * Return TRUE if "p" starts with "<SID>" or "s:".
23867 * Only works if eval_fname_script() returned non-zero for "p"!
23868 */
23869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023870eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871{
23872 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23873}
23874
23875/*
23876 * List the head of the function: "name(arg1, arg2)".
23877 */
23878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023879list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880{
23881 int j;
23882
23883 msg_start();
23884 if (indent)
23885 MSG_PUTS(" ");
23886 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023887 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 {
23889 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023890 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 }
23892 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023893 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023895 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 {
23897 if (j)
23898 MSG_PUTS(", ");
23899 msg_puts(FUNCARG(fp, j));
23900 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023901 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 {
23903 if (j)
23904 MSG_PUTS(", ");
23905 MSG_PUTS("...");
23906 }
23907 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023908 if (fp->uf_flags & FC_ABORT)
23909 MSG_PUTS(" abort");
23910 if (fp->uf_flags & FC_RANGE)
23911 MSG_PUTS(" range");
23912 if (fp->uf_flags & FC_DICT)
23913 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023914 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023915 if (p_verbose > 0)
23916 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917}
23918
23919/*
23920 * Find a function by name, return pointer to it in ufuncs.
23921 * Return NULL for unknown function.
23922 */
23923 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023924find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023926 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023928 hi = hash_find(&func_hashtab, name);
23929 if (!HASHITEM_EMPTY(hi))
23930 return HI2UF(hi);
23931 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023932}
23933
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023934#if defined(EXITFREE) || defined(PROTO)
23935 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023936free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023937{
23938 hashitem_T *hi;
23939
23940 /* Need to start all over every time, because func_free() may change the
23941 * hash table. */
23942 while (func_hashtab.ht_used > 0)
23943 for (hi = func_hashtab.ht_array; ; ++hi)
23944 if (!HASHITEM_EMPTY(hi))
23945 {
23946 func_free(HI2UF(hi));
23947 break;
23948 }
23949}
23950#endif
23951
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023953translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023954{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023955 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023956 return find_internal_func(name) >= 0;
23957 return find_func(name) != NULL;
23958}
23959
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023960/*
23961 * Return TRUE if a function "name" exists.
23962 */
23963 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023964function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023965{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023966 char_u *nm = name;
23967 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023968 int n = FALSE;
23969
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023970 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23971 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023972 nm = skipwhite(nm);
23973
23974 /* Only accept "funcname", "funcname ", "funcname (..." and
23975 * "funcname(...", not "funcname!...". */
23976 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023977 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023978 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023979 return n;
23980}
23981
Bram Moolenaara1544c02013-05-30 12:35:52 +020023982 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023983get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023984{
23985 char_u *nm = name;
23986 char_u *p;
23987
23988 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23989
23990 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023991 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023992 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023993
Bram Moolenaara1544c02013-05-30 12:35:52 +020023994 vim_free(p);
23995 return NULL;
23996}
23997
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023998/*
23999 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024000 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24001 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024002 */
24003 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024004builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024005{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024006 char_u *p;
24007
24008 if (!ASCII_ISLOWER(name[0]))
24009 return FALSE;
24010 p = vim_strchr(name, AUTOLOAD_CHAR);
24011 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024012}
24013
Bram Moolenaar05159a02005-02-26 23:04:13 +000024014#if defined(FEAT_PROFILE) || defined(PROTO)
24015/*
24016 * Start profiling function "fp".
24017 */
24018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024019func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024020{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024021 int len = fp->uf_lines.ga_len;
24022
24023 if (len == 0)
24024 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024025 fp->uf_tm_count = 0;
24026 profile_zero(&fp->uf_tm_self);
24027 profile_zero(&fp->uf_tm_total);
24028 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024029 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024030 if (fp->uf_tml_total == NULL)
24031 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024032 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024033 if (fp->uf_tml_self == NULL)
24034 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024035 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024036 fp->uf_tml_idx = -1;
24037 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24038 || fp->uf_tml_self == NULL)
24039 return; /* out of memory */
24040
24041 fp->uf_profiling = TRUE;
24042}
24043
24044/*
24045 * Dump the profiling results for all functions in file "fd".
24046 */
24047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024048func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024049{
24050 hashitem_T *hi;
24051 int todo;
24052 ufunc_T *fp;
24053 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024054 ufunc_T **sorttab;
24055 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024056
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024057 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024058 if (todo == 0)
24059 return; /* nothing to dump */
24060
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024061 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024062
Bram Moolenaar05159a02005-02-26 23:04:13 +000024063 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24064 {
24065 if (!HASHITEM_EMPTY(hi))
24066 {
24067 --todo;
24068 fp = HI2UF(hi);
24069 if (fp->uf_profiling)
24070 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024071 if (sorttab != NULL)
24072 sorttab[st_len++] = fp;
24073
Bram Moolenaar05159a02005-02-26 23:04:13 +000024074 if (fp->uf_name[0] == K_SPECIAL)
24075 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24076 else
24077 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24078 if (fp->uf_tm_count == 1)
24079 fprintf(fd, "Called 1 time\n");
24080 else
24081 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24082 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24083 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24084 fprintf(fd, "\n");
24085 fprintf(fd, "count total (s) self (s)\n");
24086
24087 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24088 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024089 if (FUNCLINE(fp, i) == NULL)
24090 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024091 prof_func_line(fd, fp->uf_tml_count[i],
24092 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024093 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24094 }
24095 fprintf(fd, "\n");
24096 }
24097 }
24098 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024099
24100 if (sorttab != NULL && st_len > 0)
24101 {
24102 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24103 prof_total_cmp);
24104 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24105 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24106 prof_self_cmp);
24107 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24108 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024109
24110 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024111}
Bram Moolenaar73830342005-02-28 22:48:19 +000024112
24113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024114prof_sort_list(
24115 FILE *fd,
24116 ufunc_T **sorttab,
24117 int st_len,
24118 char *title,
24119 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024120{
24121 int i;
24122 ufunc_T *fp;
24123
24124 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24125 fprintf(fd, "count total (s) self (s) function\n");
24126 for (i = 0; i < 20 && i < st_len; ++i)
24127 {
24128 fp = sorttab[i];
24129 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24130 prefer_self);
24131 if (fp->uf_name[0] == K_SPECIAL)
24132 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24133 else
24134 fprintf(fd, " %s()\n", fp->uf_name);
24135 }
24136 fprintf(fd, "\n");
24137}
24138
24139/*
24140 * Print the count and times for one function or function line.
24141 */
24142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024143prof_func_line(
24144 FILE *fd,
24145 int count,
24146 proftime_T *total,
24147 proftime_T *self,
24148 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024149{
24150 if (count > 0)
24151 {
24152 fprintf(fd, "%5d ", count);
24153 if (prefer_self && profile_equal(total, self))
24154 fprintf(fd, " ");
24155 else
24156 fprintf(fd, "%s ", profile_msg(total));
24157 if (!prefer_self && profile_equal(total, self))
24158 fprintf(fd, " ");
24159 else
24160 fprintf(fd, "%s ", profile_msg(self));
24161 }
24162 else
24163 fprintf(fd, " ");
24164}
24165
24166/*
24167 * Compare function for total time sorting.
24168 */
24169 static int
24170#ifdef __BORLANDC__
24171_RTLENTRYF
24172#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024173prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024174{
24175 ufunc_T *p1, *p2;
24176
24177 p1 = *(ufunc_T **)s1;
24178 p2 = *(ufunc_T **)s2;
24179 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24180}
24181
24182/*
24183 * Compare function for self time sorting.
24184 */
24185 static int
24186#ifdef __BORLANDC__
24187_RTLENTRYF
24188#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024189prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024190{
24191 ufunc_T *p1, *p2;
24192
24193 p1 = *(ufunc_T **)s1;
24194 p2 = *(ufunc_T **)s2;
24195 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24196}
24197
Bram Moolenaar05159a02005-02-26 23:04:13 +000024198#endif
24199
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024200/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024201 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024202 * Return TRUE if a package was loaded.
24203 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024204 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024205script_autoload(
24206 char_u *name,
24207 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024208{
24209 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024210 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024211 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024212 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024213
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024214 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024215 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024216 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024217 return FALSE;
24218
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024219 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024220
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024221 /* Find the name in the list of previously loaded package names. Skip
24222 * "autoload/", it's always the same. */
24223 for (i = 0; i < ga_loaded.ga_len; ++i)
24224 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24225 break;
24226 if (!reload && i < ga_loaded.ga_len)
24227 ret = FALSE; /* was loaded already */
24228 else
24229 {
24230 /* Remember the name if it wasn't loaded already. */
24231 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24232 {
24233 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24234 tofree = NULL;
24235 }
24236
24237 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024238 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024239 ret = TRUE;
24240 }
24241
24242 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024243 return ret;
24244}
24245
24246/*
24247 * Return the autoload script name for a function or variable name.
24248 * Returns NULL when out of memory.
24249 */
24250 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024251autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024252{
24253 char_u *p;
24254 char_u *scriptname;
24255
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024256 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024257 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24258 if (scriptname == NULL)
24259 return FALSE;
24260 STRCPY(scriptname, "autoload/");
24261 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024262 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024263 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024264 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024265 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024266 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024267}
24268
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24270
24271/*
24272 * Function given to ExpandGeneric() to obtain the list of user defined
24273 * function names.
24274 */
24275 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024276get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024278 static long_u done;
24279 static hashitem_T *hi;
24280 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024281
24282 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024284 done = 0;
24285 hi = func_hashtab.ht_array;
24286 }
24287 if (done < func_hashtab.ht_used)
24288 {
24289 if (done++ > 0)
24290 ++hi;
24291 while (HASHITEM_EMPTY(hi))
24292 ++hi;
24293 fp = HI2UF(hi);
24294
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024295 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024296 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024297
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024298 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24299 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300
24301 cat_func_name(IObuff, fp);
24302 if (xp->xp_context != EXPAND_USER_FUNC)
24303 {
24304 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024305 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306 STRCAT(IObuff, ")");
24307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 return IObuff;
24309 }
24310 return NULL;
24311}
24312
24313#endif /* FEAT_CMDL_COMPL */
24314
24315/*
24316 * Copy the function name of "fp" to buffer "buf".
24317 * "buf" must be able to hold the function name plus three bytes.
24318 * Takes care of script-local function names.
24319 */
24320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024321cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024322{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024323 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324 {
24325 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024326 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024327 }
24328 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024329 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330}
24331
24332/*
24333 * ":delfunction {name}"
24334 */
24335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024336ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024337{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024338 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024339 char_u *p;
24340 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024341 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024342
24343 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024344 name = trans_function_name(&p, eap->skip, 0, &fudi);
24345 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024347 {
24348 if (fudi.fd_dict != NULL && !eap->skip)
24349 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 if (!ends_excmd(*skipwhite(p)))
24353 {
24354 vim_free(name);
24355 EMSG(_(e_trailing));
24356 return;
24357 }
24358 eap->nextcmd = check_nextcmd(p);
24359 if (eap->nextcmd != NULL)
24360 *p = NUL;
24361
24362 if (!eap->skip)
24363 fp = find_func(name);
24364 vim_free(name);
24365
24366 if (!eap->skip)
24367 {
24368 if (fp == NULL)
24369 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024370 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371 return;
24372 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024373 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374 {
24375 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24376 return;
24377 }
24378
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024379 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024381 /* Delete the dict item that refers to the function, it will
24382 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024383 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024385 else
24386 func_free(fp);
24387 }
24388}
24389
24390/*
24391 * Free a function and remove it from the list of functions.
24392 */
24393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024394func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024395{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024396 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024397
24398 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024399 ga_clear_strings(&(fp->uf_args));
24400 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024401#ifdef FEAT_PROFILE
24402 vim_free(fp->uf_tml_count);
24403 vim_free(fp->uf_tml_total);
24404 vim_free(fp->uf_tml_self);
24405#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024406
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024407 /* remove the function from the function hashtable */
24408 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24409 if (HASHITEM_EMPTY(hi))
24410 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024411 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024412 hash_remove(&func_hashtab, hi);
24413
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024414 vim_free(fp);
24415}
24416
24417/*
24418 * Unreference a Function: decrement the reference count and free it when it
24419 * becomes zero. Only for numbered functions.
24420 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024422func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024423{
24424 ufunc_T *fp;
24425
24426 if (name != NULL && isdigit(*name))
24427 {
24428 fp = find_func(name);
24429 if (fp == NULL)
24430 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024431 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024432 {
24433 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024434 * when "uf_calls" becomes zero. */
24435 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024436 func_free(fp);
24437 }
24438 }
24439}
24440
24441/*
24442 * Count a reference to a Function.
24443 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024445func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024446{
24447 ufunc_T *fp;
24448
24449 if (name != NULL && isdigit(*name))
24450 {
24451 fp = find_func(name);
24452 if (fp == NULL)
24453 EMSG2(_(e_intern2), "func_ref()");
24454 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024455 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024456 }
24457}
24458
24459/*
24460 * Call a user function.
24461 */
24462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024463call_user_func(
24464 ufunc_T *fp, /* pointer to function */
24465 int argcount, /* nr of args */
24466 typval_T *argvars, /* arguments */
24467 typval_T *rettv, /* return value */
24468 linenr_T firstline, /* first line of range */
24469 linenr_T lastline, /* last line of range */
24470 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024471{
Bram Moolenaar33570922005-01-25 22:26:29 +000024472 char_u *save_sourcing_name;
24473 linenr_T save_sourcing_lnum;
24474 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024475 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024476 int save_did_emsg;
24477 static int depth = 0;
24478 dictitem_T *v;
24479 int fixvar_idx = 0; /* index in fixvar[] */
24480 int i;
24481 int ai;
24482 char_u numbuf[NUMBUFLEN];
24483 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024484 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024485#ifdef FEAT_PROFILE
24486 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024487 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489
24490 /* If depth of calling is getting too high, don't execute the function */
24491 if (depth >= p_mfd)
24492 {
24493 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024494 rettv->v_type = VAR_NUMBER;
24495 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024496 return;
24497 }
24498 ++depth;
24499
24500 line_breakcheck(); /* check for CTRL-C hit */
24501
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024502 fc = (funccall_T *)alloc(sizeof(funccall_T));
24503 fc->caller = current_funccal;
24504 current_funccal = fc;
24505 fc->func = fp;
24506 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024507 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024508 fc->linenr = 0;
24509 fc->returned = FALSE;
24510 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024512 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24513 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514
Bram Moolenaar33570922005-01-25 22:26:29 +000024515 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024516 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024517 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24518 * each argument variable and saves a lot of time.
24519 */
24520 /*
24521 * Init l: variables.
24522 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024523 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024524 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024525 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024526 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24527 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024528 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024529 name = v->di_key;
24530 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024531 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024532 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024533 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024534 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024535 v->di_tv.vval.v_dict = selfdict;
24536 ++selfdict->dv_refcount;
24537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024538
Bram Moolenaar33570922005-01-25 22:26:29 +000024539 /*
24540 * Init a: variables.
24541 * Set a:0 to "argcount".
24542 * Set a:000 to a list with room for the "..." arguments.
24543 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024544 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024545 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024546 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024547 /* Use "name" to avoid a warning from some compiler that checks the
24548 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024549 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024550 name = v->di_key;
24551 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024552 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024553 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024554 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024555 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024556 v->di_tv.vval.v_list = &fc->l_varlist;
24557 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24558 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24559 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024560
24561 /*
24562 * Set a:firstline to "firstline" and a:lastline to "lastline".
24563 * Set a:name to named arguments.
24564 * Set a:N to the "..." arguments.
24565 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024566 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024567 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024568 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024569 (varnumber_T)lastline);
24570 for (i = 0; i < argcount; ++i)
24571 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024572 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024573 if (ai < 0)
24574 /* named argument a:name */
24575 name = FUNCARG(fp, i);
24576 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024578 /* "..." argument a:1, a:2, etc. */
24579 sprintf((char *)numbuf, "%d", ai + 1);
24580 name = numbuf;
24581 }
24582 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24583 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024584 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024585 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24586 }
24587 else
24588 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024589 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24590 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024591 if (v == NULL)
24592 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024593 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024594 }
24595 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024596 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024597
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024598 /* Note: the values are copied directly to avoid alloc/free.
24599 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024600 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024601 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024602
24603 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24604 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024605 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24606 fc->l_listitems[ai].li_tv = argvars[i];
24607 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024608 }
24609 }
24610
Bram Moolenaar071d4272004-06-13 20:20:40 +000024611 /* Don't redraw while executing the function. */
24612 ++RedrawingDisabled;
24613 save_sourcing_name = sourcing_name;
24614 save_sourcing_lnum = sourcing_lnum;
24615 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024616 /* need space for function name + ("function " + 3) or "[number]" */
24617 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24618 + STRLEN(fp->uf_name) + 20;
24619 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 if (sourcing_name != NULL)
24621 {
24622 if (save_sourcing_name != NULL
24623 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024624 sprintf((char *)sourcing_name, "%s[%d]..",
24625 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 else
24627 STRCPY(sourcing_name, "function ");
24628 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24629
24630 if (p_verbose >= 12)
24631 {
24632 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024633 verbose_enter_scroll();
24634
Bram Moolenaar555b2802005-05-19 21:08:39 +000024635 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 if (p_verbose >= 14)
24637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024639 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024640 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024641 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642
24643 msg_puts((char_u *)"(");
24644 for (i = 0; i < argcount; ++i)
24645 {
24646 if (i > 0)
24647 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024648 if (argvars[i].v_type == VAR_NUMBER)
24649 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 else
24651 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024652 /* Do not want errors such as E724 here. */
24653 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024654 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024655 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024656 if (s != NULL)
24657 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024658 if (vim_strsize(s) > MSG_BUF_CLEN)
24659 {
24660 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24661 s = buf;
24662 }
24663 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024664 vim_free(tofree);
24665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024666 }
24667 }
24668 msg_puts((char_u *)")");
24669 }
24670 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024671
24672 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 --no_wait_return;
24674 }
24675 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024676#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024677 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024678 {
24679 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24680 func_do_profile(fp);
24681 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024682 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024683 {
24684 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024685 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024686 profile_zero(&fp->uf_tm_children);
24687 }
24688 script_prof_save(&wait_start);
24689 }
24690#endif
24691
Bram Moolenaar071d4272004-06-13 20:20:40 +000024692 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024693 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694 save_did_emsg = did_emsg;
24695 did_emsg = FALSE;
24696
24697 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024698 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24700
24701 --RedrawingDisabled;
24702
24703 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024704 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024706 clear_tv(rettv);
24707 rettv->v_type = VAR_NUMBER;
24708 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709 }
24710
Bram Moolenaar05159a02005-02-26 23:04:13 +000024711#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024712 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024713 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024714 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024715 profile_end(&call_start);
24716 profile_sub_wait(&wait_start, &call_start);
24717 profile_add(&fp->uf_tm_total, &call_start);
24718 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024719 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024720 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024721 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24722 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024723 }
24724 }
24725#endif
24726
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 /* when being verbose, mention the return value */
24728 if (p_verbose >= 12)
24729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024730 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024731 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024732
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024734 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024735 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024736 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024737 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024740 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024741 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024742 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024743 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024744
Bram Moolenaar555b2802005-05-19 21:08:39 +000024745 /* The value may be very long. Skip the middle part, so that we
24746 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024747 * truncate it at the end. Don't want errors such as E724 here. */
24748 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024749 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024750 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024751 if (s != NULL)
24752 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024753 if (vim_strsize(s) > MSG_BUF_CLEN)
24754 {
24755 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24756 s = buf;
24757 }
24758 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024759 vim_free(tofree);
24760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024761 }
24762 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024763
24764 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024765 --no_wait_return;
24766 }
24767
24768 vim_free(sourcing_name);
24769 sourcing_name = save_sourcing_name;
24770 sourcing_lnum = save_sourcing_lnum;
24771 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024772#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024773 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024774 script_prof_restore(&wait_start);
24775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024776
24777 if (p_verbose >= 12 && sourcing_name != NULL)
24778 {
24779 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024780 verbose_enter_scroll();
24781
Bram Moolenaar555b2802005-05-19 21:08:39 +000024782 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024784
24785 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786 --no_wait_return;
24787 }
24788
24789 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024790 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024792
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024793 /* If the a:000 list and the l: and a: dicts are not referenced we can
24794 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024795 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24796 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24797 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24798 {
24799 free_funccal(fc, FALSE);
24800 }
24801 else
24802 {
24803 hashitem_T *hi;
24804 listitem_T *li;
24805 int todo;
24806
24807 /* "fc" is still in use. This can happen when returning "a:000" or
24808 * assigning "l:" to a global variable.
24809 * Link "fc" in the list for garbage collection later. */
24810 fc->caller = previous_funccal;
24811 previous_funccal = fc;
24812
24813 /* Make a copy of the a: variables, since we didn't do that above. */
24814 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24815 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24816 {
24817 if (!HASHITEM_EMPTY(hi))
24818 {
24819 --todo;
24820 v = HI2DI(hi);
24821 copy_tv(&v->di_tv, &v->di_tv);
24822 }
24823 }
24824
24825 /* Make a copy of the a:000 items, since we didn't do that above. */
24826 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24827 copy_tv(&li->li_tv, &li->li_tv);
24828 }
24829}
24830
24831/*
24832 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024833 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024834 */
24835 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024836can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024837{
24838 return (fc->l_varlist.lv_copyID != copyID
24839 && fc->l_vars.dv_copyID != copyID
24840 && fc->l_avars.dv_copyID != copyID);
24841}
24842
24843/*
24844 * Free "fc" and what it contains.
24845 */
24846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024847free_funccal(
24848 funccall_T *fc,
24849 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024850{
24851 listitem_T *li;
24852
24853 /* The a: variables typevals may not have been allocated, only free the
24854 * allocated variables. */
24855 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24856
24857 /* free all l: variables */
24858 vars_clear(&fc->l_vars.dv_hashtab);
24859
24860 /* Free the a:000 variables if they were allocated. */
24861 if (free_val)
24862 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24863 clear_tv(&li->li_tv);
24864
24865 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866}
24867
24868/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024869 * Add a number variable "name" to dict "dp" with value "nr".
24870 */
24871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024872add_nr_var(
24873 dict_T *dp,
24874 dictitem_T *v,
24875 char *name,
24876 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024877{
24878 STRCPY(v->di_key, name);
24879 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24880 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24881 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024882 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024883 v->di_tv.vval.v_number = nr;
24884}
24885
24886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887 * ":return [expr]"
24888 */
24889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024890ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024891{
24892 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024893 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 int returning = FALSE;
24895
24896 if (current_funccal == NULL)
24897 {
24898 EMSG(_("E133: :return not inside a function"));
24899 return;
24900 }
24901
24902 if (eap->skip)
24903 ++emsg_skip;
24904
24905 eap->nextcmd = NULL;
24906 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024907 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 {
24909 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024910 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024912 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913 }
24914 /* It's safer to return also on error. */
24915 else if (!eap->skip)
24916 {
24917 /*
24918 * Return unless the expression evaluation has been cancelled due to an
24919 * aborting error, an interrupt, or an exception.
24920 */
24921 if (!aborting())
24922 returning = do_return(eap, FALSE, TRUE, NULL);
24923 }
24924
24925 /* When skipping or the return gets pending, advance to the next command
24926 * in this line (!returning). Otherwise, ignore the rest of the line.
24927 * Following lines will be ignored by get_func_line(). */
24928 if (returning)
24929 eap->nextcmd = NULL;
24930 else if (eap->nextcmd == NULL) /* no argument */
24931 eap->nextcmd = check_nextcmd(arg);
24932
24933 if (eap->skip)
24934 --emsg_skip;
24935}
24936
24937/*
24938 * Return from a function. Possibly makes the return pending. Also called
24939 * for a pending return at the ":endtry" or after returning from an extra
24940 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024941 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024942 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943 * FALSE when the return gets pending.
24944 */
24945 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024946do_return(
24947 exarg_T *eap,
24948 int reanimate,
24949 int is_cmd,
24950 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024951{
24952 int idx;
24953 struct condstack *cstack = eap->cstack;
24954
24955 if (reanimate)
24956 /* Undo the return. */
24957 current_funccal->returned = FALSE;
24958
24959 /*
24960 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24961 * not in its finally clause (which then is to be executed next) is found.
24962 * In this case, make the ":return" pending for execution at the ":endtry".
24963 * Otherwise, return normally.
24964 */
24965 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24966 if (idx >= 0)
24967 {
24968 cstack->cs_pending[idx] = CSTP_RETURN;
24969
24970 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024971 /* A pending return again gets pending. "rettv" points to an
24972 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024973 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024974 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975 else
24976 {
24977 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024978 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024980 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024982 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024983 {
24984 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024985 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024986 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987 else
24988 EMSG(_(e_outofmem));
24989 }
24990 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024991 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992
24993 if (reanimate)
24994 {
24995 /* The pending return value could be overwritten by a ":return"
24996 * without argument in a finally clause; reset the default
24997 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024998 current_funccal->rettv->v_type = VAR_NUMBER;
24999 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000 }
25001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025002 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025003 }
25004 else
25005 {
25006 current_funccal->returned = TRUE;
25007
25008 /* If the return is carried out now, store the return value. For
25009 * a return immediately after reanimation, the value is already
25010 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025011 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025013 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025014 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025016 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025017 }
25018 }
25019
25020 return idx < 0;
25021}
25022
25023/*
25024 * Free the variable with a pending return value.
25025 */
25026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025027discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028{
Bram Moolenaar33570922005-01-25 22:26:29 +000025029 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025030}
25031
25032/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025033 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025034 * is an allocated string. Used by report_pending() for verbose messages.
25035 */
25036 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025037get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025038{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025039 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025040 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025041 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025043 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025044 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025045 if (s == NULL)
25046 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025047
25048 STRCPY(IObuff, ":return ");
25049 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25050 if (STRLEN(s) + 8 >= IOSIZE)
25051 STRCPY(IObuff + IOSIZE - 4, "...");
25052 vim_free(tofree);
25053 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054}
25055
25056/*
25057 * Get next function line.
25058 * Called by do_cmdline() to get the next line.
25059 * Returns allocated string, or NULL for end of function.
25060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025062get_func_line(
25063 int c UNUSED,
25064 void *cookie,
25065 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025066{
Bram Moolenaar33570922005-01-25 22:26:29 +000025067 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025068 ufunc_T *fp = fcp->func;
25069 char_u *retval;
25070 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071
25072 /* If breakpoints have been added/deleted need to check for it. */
25073 if (fcp->dbg_tick != debug_tick)
25074 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025075 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 sourcing_lnum);
25077 fcp->dbg_tick = debug_tick;
25078 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025079#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025080 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025081 func_line_end(cookie);
25082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083
Bram Moolenaar05159a02005-02-26 23:04:13 +000025084 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025085 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25086 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025087 retval = NULL;
25088 else
25089 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025090 /* Skip NULL lines (continuation lines). */
25091 while (fcp->linenr < gap->ga_len
25092 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25093 ++fcp->linenr;
25094 if (fcp->linenr >= gap->ga_len)
25095 retval = NULL;
25096 else
25097 {
25098 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25099 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025100#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025101 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025102 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025103#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025105 }
25106
25107 /* Did we encounter a breakpoint? */
25108 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25109 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025110 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025112 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025113 sourcing_lnum);
25114 fcp->dbg_tick = debug_tick;
25115 }
25116
25117 return retval;
25118}
25119
Bram Moolenaar05159a02005-02-26 23:04:13 +000025120#if defined(FEAT_PROFILE) || defined(PROTO)
25121/*
25122 * Called when starting to read a function line.
25123 * "sourcing_lnum" must be correct!
25124 * When skipping lines it may not actually be executed, but we won't find out
25125 * until later and we need to store the time now.
25126 */
25127 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025128func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025129{
25130 funccall_T *fcp = (funccall_T *)cookie;
25131 ufunc_T *fp = fcp->func;
25132
25133 if (fp->uf_profiling && sourcing_lnum >= 1
25134 && sourcing_lnum <= fp->uf_lines.ga_len)
25135 {
25136 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025137 /* Skip continuation lines. */
25138 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25139 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025140 fp->uf_tml_execed = FALSE;
25141 profile_start(&fp->uf_tml_start);
25142 profile_zero(&fp->uf_tml_children);
25143 profile_get_wait(&fp->uf_tml_wait);
25144 }
25145}
25146
25147/*
25148 * Called when actually executing a function line.
25149 */
25150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025151func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025152{
25153 funccall_T *fcp = (funccall_T *)cookie;
25154 ufunc_T *fp = fcp->func;
25155
25156 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25157 fp->uf_tml_execed = TRUE;
25158}
25159
25160/*
25161 * Called when done with a function line.
25162 */
25163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025164func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025165{
25166 funccall_T *fcp = (funccall_T *)cookie;
25167 ufunc_T *fp = fcp->func;
25168
25169 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25170 {
25171 if (fp->uf_tml_execed)
25172 {
25173 ++fp->uf_tml_count[fp->uf_tml_idx];
25174 profile_end(&fp->uf_tml_start);
25175 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025176 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025177 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25178 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025179 }
25180 fp->uf_tml_idx = -1;
25181 }
25182}
25183#endif
25184
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185/*
25186 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025187 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025188 */
25189 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025190func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025191{
Bram Moolenaar33570922005-01-25 22:26:29 +000025192 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193
25194 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25195 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025196 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 || fcp->returned);
25198}
25199
25200/*
25201 * return TRUE if cookie indicates a function which "abort"s on errors.
25202 */
25203 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025204func_has_abort(
25205 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025207 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025208}
25209
25210#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25211typedef enum
25212{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025213 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25214 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25215 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025216} var_flavour_T;
25217
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025218static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219
25220 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025221var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025222{
25223 char_u *p = varname;
25224
25225 if (ASCII_ISUPPER(*p))
25226 {
25227 while (*(++p))
25228 if (ASCII_ISLOWER(*p))
25229 return VAR_FLAVOUR_SESSION;
25230 return VAR_FLAVOUR_VIMINFO;
25231 }
25232 else
25233 return VAR_FLAVOUR_DEFAULT;
25234}
25235#endif
25236
25237#if defined(FEAT_VIMINFO) || defined(PROTO)
25238/*
25239 * Restore global vars that start with a capital from the viminfo file
25240 */
25241 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025242read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243{
25244 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025245 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025246 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025247 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248
25249 if (!writing && (find_viminfo_parameter('!') != NULL))
25250 {
25251 tab = vim_strchr(virp->vir_line + 1, '\t');
25252 if (tab != NULL)
25253 {
25254 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025255 switch (*tab)
25256 {
25257 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025258#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025259 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025260#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025261 case 'D': type = VAR_DICT; break;
25262 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025263 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265
25266 tab = vim_strchr(tab, '\t');
25267 if (tab != NULL)
25268 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025269 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025270 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025271 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025273#ifdef FEAT_FLOAT
25274 else if (type == VAR_FLOAT)
25275 (void)string2float(tab + 1, &tv.vval.v_float);
25276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025278 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025279 if (type == VAR_DICT || type == VAR_LIST)
25280 {
25281 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25282
25283 if (etv == NULL)
25284 /* Failed to parse back the dict or list, use it as a
25285 * string. */
25286 tv.v_type = VAR_STRING;
25287 else
25288 {
25289 vim_free(tv.vval.v_string);
25290 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025291 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025292 }
25293 }
25294
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025295 /* when in a function use global variables */
25296 save_funccal = current_funccal;
25297 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025298 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025299 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025300
25301 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025302 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025303 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25304 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025305 }
25306 }
25307 }
25308
25309 return viminfo_readline(virp);
25310}
25311
25312/*
25313 * Write global vars that start with a capital to the viminfo file
25314 */
25315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025316write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317{
Bram Moolenaar33570922005-01-25 22:26:29 +000025318 hashitem_T *hi;
25319 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025320 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025321 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025322 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025323 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025324 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025325
25326 if (find_viminfo_parameter('!') == NULL)
25327 return;
25328
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025329 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025330
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025331 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025332 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025334 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025336 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025337 this_var = HI2DI(hi);
25338 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025339 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025340 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025341 {
25342 case VAR_STRING: s = "STR"; break;
25343 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025344 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025345 case VAR_DICT: s = "DIC"; break;
25346 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025347 case VAR_SPECIAL: s = "XPL"; break;
25348
25349 case VAR_UNKNOWN:
25350 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025351 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025352 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025353 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025354 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025355 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025356 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025357 if (p != NULL)
25358 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025359 vim_free(tofree);
25360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 }
25362 }
25363}
25364#endif
25365
25366#if defined(FEAT_SESSION) || defined(PROTO)
25367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025368store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369{
Bram Moolenaar33570922005-01-25 22:26:29 +000025370 hashitem_T *hi;
25371 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025372 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373 char_u *p, *t;
25374
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025375 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025376 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025378 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025380 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025381 this_var = HI2DI(hi);
25382 if ((this_var->di_tv.v_type == VAR_NUMBER
25383 || this_var->di_tv.v_type == VAR_STRING)
25384 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025385 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025386 /* Escape special characters with a backslash. Turn a LF and
25387 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025388 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025389 (char_u *)"\\\"\n\r");
25390 if (p == NULL) /* out of memory */
25391 break;
25392 for (t = p; *t != NUL; ++t)
25393 if (*t == '\n')
25394 *t = 'n';
25395 else if (*t == '\r')
25396 *t = 'r';
25397 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025398 this_var->di_key,
25399 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25400 : ' ',
25401 p,
25402 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25403 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025404 || put_eol(fd) == FAIL)
25405 {
25406 vim_free(p);
25407 return FAIL;
25408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025409 vim_free(p);
25410 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025411#ifdef FEAT_FLOAT
25412 else if (this_var->di_tv.v_type == VAR_FLOAT
25413 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25414 {
25415 float_T f = this_var->di_tv.vval.v_float;
25416 int sign = ' ';
25417
25418 if (f < 0)
25419 {
25420 f = -f;
25421 sign = '-';
25422 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025423 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025424 this_var->di_key, sign, f) < 0)
25425 || put_eol(fd) == FAIL)
25426 return FAIL;
25427 }
25428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429 }
25430 }
25431 return OK;
25432}
25433#endif
25434
Bram Moolenaar661b1822005-07-28 22:36:45 +000025435/*
25436 * Display script name where an item was last set.
25437 * Should only be invoked when 'verbose' is non-zero.
25438 */
25439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025440last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025441{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025442 char_u *p;
25443
Bram Moolenaar661b1822005-07-28 22:36:45 +000025444 if (scriptID != 0)
25445 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025446 p = home_replace_save(NULL, get_scriptname(scriptID));
25447 if (p != NULL)
25448 {
25449 verbose_enter();
25450 MSG_PUTS(_("\n\tLast set from "));
25451 MSG_PUTS(p);
25452 vim_free(p);
25453 verbose_leave();
25454 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025455 }
25456}
25457
Bram Moolenaard812df62008-11-09 12:46:09 +000025458/*
25459 * List v:oldfiles in a nice way.
25460 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025461 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025462ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025463{
25464 list_T *l = vimvars[VV_OLDFILES].vv_list;
25465 listitem_T *li;
25466 int nr = 0;
25467
25468 if (l == NULL)
25469 msg((char_u *)_("No old files"));
25470 else
25471 {
25472 msg_start();
25473 msg_scroll = TRUE;
25474 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25475 {
25476 msg_outnum((long)++nr);
25477 MSG_PUTS(": ");
25478 msg_outtrans(get_tv_string(&li->li_tv));
25479 msg_putchar('\n');
25480 out_flush(); /* output one line at a time */
25481 ui_breakcheck();
25482 }
25483 /* Assume "got_int" was set to truncate the listing. */
25484 got_int = FALSE;
25485
25486#ifdef FEAT_BROWSE_CMD
25487 if (cmdmod.browse)
25488 {
25489 quit_more = FALSE;
25490 nr = prompt_for_number(FALSE);
25491 msg_starthere();
25492 if (nr > 0)
25493 {
25494 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25495 (long)nr);
25496
25497 if (p != NULL)
25498 {
25499 p = expand_env_save(p);
25500 eap->arg = p;
25501 eap->cmdidx = CMD_edit;
25502 cmdmod.browse = FALSE;
25503 do_exedit(eap, NULL);
25504 vim_free(p);
25505 }
25506 }
25507 }
25508#endif
25509 }
25510}
25511
Bram Moolenaar53744302015-07-17 17:38:22 +020025512/* reset v:option_new, v:option_old and v:option_type */
25513 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025514reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025515{
25516 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25517 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25518 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25519}
25520
25521
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522#endif /* FEAT_EVAL */
25523
Bram Moolenaar071d4272004-06-13 20:20:40 +000025524
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025525#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025526
25527#ifdef WIN3264
25528/*
25529 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25530 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025531static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25532static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25533static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534
25535/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025536 * Get the short path (8.3) for the filename in "fnamep".
25537 * Only works for a valid file name.
25538 * When the path gets longer "fnamep" is changed and the allocated buffer
25539 * is put in "bufp".
25540 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25541 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542 */
25543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025544get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025546 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025547 char_u *newbuf;
25548
25549 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025550 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551 if (l > len - 1)
25552 {
25553 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025554 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025555 newbuf = vim_strnsave(*fnamep, l);
25556 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025557 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558
25559 vim_free(*bufp);
25560 *fnamep = *bufp = newbuf;
25561
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025562 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025563 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025564 }
25565
25566 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025567 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568}
25569
25570/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025571 * Get the short path (8.3) for the filename in "fname". The converted
25572 * path is returned in "bufp".
25573 *
25574 * Some of the directories specified in "fname" may not exist. This function
25575 * will shorten the existing directories at the beginning of the path and then
25576 * append the remaining non-existing path.
25577 *
25578 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025579 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025580 * bufp - Pointer to an allocated buffer for the filename.
25581 * fnamelen - Length of the filename pointed to by fname
25582 *
25583 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584 */
25585 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025586shortpath_for_invalid_fname(
25587 char_u **fname,
25588 char_u **bufp,
25589 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025591 char_u *short_fname, *save_fname, *pbuf_unused;
25592 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025593 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025594 int old_len, len;
25595 int new_len, sfx_len;
25596 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025597
25598 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025599 old_len = *fnamelen;
25600 save_fname = vim_strnsave(*fname, old_len);
25601 pbuf_unused = NULL;
25602 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025603
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025604 endp = save_fname + old_len - 1; /* Find the end of the copy */
25605 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025607 /*
25608 * Try shortening the supplied path till it succeeds by removing one
25609 * directory at a time from the tail of the path.
25610 */
25611 len = 0;
25612 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025613 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025614 /* go back one path-separator */
25615 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25616 --endp;
25617 if (endp <= save_fname)
25618 break; /* processed the complete path */
25619
25620 /*
25621 * Replace the path separator with a NUL and try to shorten the
25622 * resulting path.
25623 */
25624 ch = *endp;
25625 *endp = 0;
25626 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025627 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025628 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25629 {
25630 retval = FAIL;
25631 goto theend;
25632 }
25633 *endp = ch; /* preserve the string */
25634
25635 if (len > 0)
25636 break; /* successfully shortened the path */
25637
25638 /* failed to shorten the path. Skip the path separator */
25639 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640 }
25641
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025642 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025643 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025644 /*
25645 * Succeeded in shortening the path. Now concatenate the shortened
25646 * path with the remaining path at the tail.
25647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025649 /* Compute the length of the new path. */
25650 sfx_len = (int)(save_endp - endp) + 1;
25651 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025653 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025655 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025656 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025657 /* There is not enough space in the currently allocated string,
25658 * copy it to a buffer big enough. */
25659 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025661 {
25662 retval = FAIL;
25663 goto theend;
25664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025665 }
25666 else
25667 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025668 /* Transfer short_fname to the main buffer (it's big enough),
25669 * unless get_short_pathname() did its work in-place. */
25670 *fname = *bufp = save_fname;
25671 if (short_fname != save_fname)
25672 vim_strncpy(save_fname, short_fname, len);
25673 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025674 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025675
25676 /* concat the not-shortened part of the path */
25677 vim_strncpy(*fname + len, endp, sfx_len);
25678 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025679 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025680
25681theend:
25682 vim_free(pbuf_unused);
25683 vim_free(save_fname);
25684
25685 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686}
25687
25688/*
25689 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025690 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025691 */
25692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025693shortpath_for_partial(
25694 char_u **fnamep,
25695 char_u **bufp,
25696 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025697{
25698 int sepcount, len, tflen;
25699 char_u *p;
25700 char_u *pbuf, *tfname;
25701 int hasTilde;
25702
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025703 /* Count up the path separators from the RHS.. so we know which part
25704 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025705 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025706 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025707 if (vim_ispathsep(*p))
25708 ++sepcount;
25709
25710 /* Need full path first (use expand_env() to remove a "~/") */
25711 hasTilde = (**fnamep == '~');
25712 if (hasTilde)
25713 pbuf = tfname = expand_env_save(*fnamep);
25714 else
25715 pbuf = tfname = FullName_save(*fnamep, FALSE);
25716
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025717 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025718
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025719 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25720 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025721
25722 if (len == 0)
25723 {
25724 /* Don't have a valid filename, so shorten the rest of the
25725 * path if we can. This CAN give us invalid 8.3 filenames, but
25726 * there's not a lot of point in guessing what it might be.
25727 */
25728 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025729 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25730 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731 }
25732
25733 /* Count the paths backward to find the beginning of the desired string. */
25734 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025735 {
25736#ifdef FEAT_MBYTE
25737 if (has_mbyte)
25738 p -= mb_head_off(tfname, p);
25739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740 if (vim_ispathsep(*p))
25741 {
25742 if (sepcount == 0 || (hasTilde && sepcount == 1))
25743 break;
25744 else
25745 sepcount --;
25746 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025748 if (hasTilde)
25749 {
25750 --p;
25751 if (p >= tfname)
25752 *p = '~';
25753 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025754 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755 }
25756 else
25757 ++p;
25758
25759 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25760 vim_free(*bufp);
25761 *fnamelen = (int)STRLEN(p);
25762 *bufp = pbuf;
25763 *fnamep = p;
25764
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025765 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766}
25767#endif /* WIN3264 */
25768
25769/*
25770 * Adjust a filename, according to a string of modifiers.
25771 * *fnamep must be NUL terminated when called. When returning, the length is
25772 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025773 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 * When there is an error, *fnamep is set to NULL.
25775 */
25776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025777modify_fname(
25778 char_u *src, /* string with modifiers */
25779 int *usedlen, /* characters after src that are used */
25780 char_u **fnamep, /* file name so far */
25781 char_u **bufp, /* buffer for allocated file name or NULL */
25782 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783{
25784 int valid = 0;
25785 char_u *tail;
25786 char_u *s, *p, *pbuf;
25787 char_u dirname[MAXPATHL];
25788 int c;
25789 int has_fullname = 0;
25790#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025791 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025792 int has_shortname = 0;
25793#endif
25794
25795repeat:
25796 /* ":p" - full path/file_name */
25797 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25798 {
25799 has_fullname = 1;
25800
25801 valid |= VALID_PATH;
25802 *usedlen += 2;
25803
25804 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25805 if ((*fnamep)[0] == '~'
25806#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25807 && ((*fnamep)[1] == '/'
25808# ifdef BACKSLASH_IN_FILENAME
25809 || (*fnamep)[1] == '\\'
25810# endif
25811 || (*fnamep)[1] == NUL)
25812
25813#endif
25814 )
25815 {
25816 *fnamep = expand_env_save(*fnamep);
25817 vim_free(*bufp); /* free any allocated file name */
25818 *bufp = *fnamep;
25819 if (*fnamep == NULL)
25820 return -1;
25821 }
25822
25823 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025824 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825 {
25826 if (vim_ispathsep(*p)
25827 && p[1] == '.'
25828 && (p[2] == NUL
25829 || vim_ispathsep(p[2])
25830 || (p[2] == '.'
25831 && (p[3] == NUL || vim_ispathsep(p[3])))))
25832 break;
25833 }
25834
25835 /* FullName_save() is slow, don't use it when not needed. */
25836 if (*p != NUL || !vim_isAbsName(*fnamep))
25837 {
25838 *fnamep = FullName_save(*fnamep, *p != NUL);
25839 vim_free(*bufp); /* free any allocated file name */
25840 *bufp = *fnamep;
25841 if (*fnamep == NULL)
25842 return -1;
25843 }
25844
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025845#ifdef WIN3264
25846# if _WIN32_WINNT >= 0x0500
25847 if (vim_strchr(*fnamep, '~') != NULL)
25848 {
25849 /* Expand 8.3 filename to full path. Needed to make sure the same
25850 * file does not have two different names.
25851 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25852 p = alloc(_MAX_PATH + 1);
25853 if (p != NULL)
25854 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025855 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025856 {
25857 vim_free(*bufp);
25858 *bufp = *fnamep = p;
25859 }
25860 else
25861 vim_free(p);
25862 }
25863 }
25864# endif
25865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866 /* Append a path separator to a directory. */
25867 if (mch_isdir(*fnamep))
25868 {
25869 /* Make room for one or two extra characters. */
25870 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25871 vim_free(*bufp); /* free any allocated file name */
25872 *bufp = *fnamep;
25873 if (*fnamep == NULL)
25874 return -1;
25875 add_pathsep(*fnamep);
25876 }
25877 }
25878
25879 /* ":." - path relative to the current directory */
25880 /* ":~" - path relative to the home directory */
25881 /* ":8" - shortname path - postponed till after */
25882 while (src[*usedlen] == ':'
25883 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25884 {
25885 *usedlen += 2;
25886 if (c == '8')
25887 {
25888#ifdef WIN3264
25889 has_shortname = 1; /* Postpone this. */
25890#endif
25891 continue;
25892 }
25893 pbuf = NULL;
25894 /* Need full path first (use expand_env() to remove a "~/") */
25895 if (!has_fullname)
25896 {
25897 if (c == '.' && **fnamep == '~')
25898 p = pbuf = expand_env_save(*fnamep);
25899 else
25900 p = pbuf = FullName_save(*fnamep, FALSE);
25901 }
25902 else
25903 p = *fnamep;
25904
25905 has_fullname = 0;
25906
25907 if (p != NULL)
25908 {
25909 if (c == '.')
25910 {
25911 mch_dirname(dirname, MAXPATHL);
25912 s = shorten_fname(p, dirname);
25913 if (s != NULL)
25914 {
25915 *fnamep = s;
25916 if (pbuf != NULL)
25917 {
25918 vim_free(*bufp); /* free any allocated file name */
25919 *bufp = pbuf;
25920 pbuf = NULL;
25921 }
25922 }
25923 }
25924 else
25925 {
25926 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25927 /* Only replace it when it starts with '~' */
25928 if (*dirname == '~')
25929 {
25930 s = vim_strsave(dirname);
25931 if (s != NULL)
25932 {
25933 *fnamep = s;
25934 vim_free(*bufp);
25935 *bufp = s;
25936 }
25937 }
25938 }
25939 vim_free(pbuf);
25940 }
25941 }
25942
25943 tail = gettail(*fnamep);
25944 *fnamelen = (int)STRLEN(*fnamep);
25945
25946 /* ":h" - head, remove "/file_name", can be repeated */
25947 /* Don't remove the first "/" or "c:\" */
25948 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25949 {
25950 valid |= VALID_HEAD;
25951 *usedlen += 2;
25952 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025953 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025954 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025955 *fnamelen = (int)(tail - *fnamep);
25956#ifdef VMS
25957 if (*fnamelen > 0)
25958 *fnamelen += 1; /* the path separator is part of the path */
25959#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025960 if (*fnamelen == 0)
25961 {
25962 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25963 p = vim_strsave((char_u *)".");
25964 if (p == NULL)
25965 return -1;
25966 vim_free(*bufp);
25967 *bufp = *fnamep = tail = p;
25968 *fnamelen = 1;
25969 }
25970 else
25971 {
25972 while (tail > s && !after_pathsep(s, tail))
25973 mb_ptr_back(*fnamep, tail);
25974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025975 }
25976
25977 /* ":8" - shortname */
25978 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25979 {
25980 *usedlen += 2;
25981#ifdef WIN3264
25982 has_shortname = 1;
25983#endif
25984 }
25985
25986#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025987 /*
25988 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989 */
25990 if (has_shortname)
25991 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025992 /* Copy the string if it is shortened by :h and when it wasn't copied
25993 * yet, because we are going to change it in place. Avoids changing
25994 * the buffer name for "%:8". */
25995 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996 {
25997 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025998 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 return -1;
26000 vim_free(*bufp);
26001 *bufp = *fnamep = p;
26002 }
26003
26004 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026005 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026006 if (!has_fullname && !vim_isAbsName(*fnamep))
26007 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026008 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026009 return -1;
26010 }
26011 else
26012 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026013 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014
Bram Moolenaardc935552011-08-17 15:23:23 +020026015 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026016 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026017 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018 return -1;
26019
26020 if (l == 0)
26021 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026022 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026024 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025 return -1;
26026 }
26027 *fnamelen = l;
26028 }
26029 }
26030#endif /* WIN3264 */
26031
26032 /* ":t" - tail, just the basename */
26033 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26034 {
26035 *usedlen += 2;
26036 *fnamelen -= (int)(tail - *fnamep);
26037 *fnamep = tail;
26038 }
26039
26040 /* ":e" - extension, can be repeated */
26041 /* ":r" - root, without extension, can be repeated */
26042 while (src[*usedlen] == ':'
26043 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26044 {
26045 /* find a '.' in the tail:
26046 * - for second :e: before the current fname
26047 * - otherwise: The last '.'
26048 */
26049 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26050 s = *fnamep - 2;
26051 else
26052 s = *fnamep + *fnamelen - 1;
26053 for ( ; s > tail; --s)
26054 if (s[0] == '.')
26055 break;
26056 if (src[*usedlen + 1] == 'e') /* :e */
26057 {
26058 if (s > tail)
26059 {
26060 *fnamelen += (int)(*fnamep - (s + 1));
26061 *fnamep = s + 1;
26062#ifdef VMS
26063 /* cut version from the extension */
26064 s = *fnamep + *fnamelen - 1;
26065 for ( ; s > *fnamep; --s)
26066 if (s[0] == ';')
26067 break;
26068 if (s > *fnamep)
26069 *fnamelen = s - *fnamep;
26070#endif
26071 }
26072 else if (*fnamep <= tail)
26073 *fnamelen = 0;
26074 }
26075 else /* :r */
26076 {
26077 if (s > tail) /* remove one extension */
26078 *fnamelen = (int)(s - *fnamep);
26079 }
26080 *usedlen += 2;
26081 }
26082
26083 /* ":s?pat?foo?" - substitute */
26084 /* ":gs?pat?foo?" - global substitute */
26085 if (src[*usedlen] == ':'
26086 && (src[*usedlen + 1] == 's'
26087 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26088 {
26089 char_u *str;
26090 char_u *pat;
26091 char_u *sub;
26092 int sep;
26093 char_u *flags;
26094 int didit = FALSE;
26095
26096 flags = (char_u *)"";
26097 s = src + *usedlen + 2;
26098 if (src[*usedlen + 1] == 'g')
26099 {
26100 flags = (char_u *)"g";
26101 ++s;
26102 }
26103
26104 sep = *s++;
26105 if (sep)
26106 {
26107 /* find end of pattern */
26108 p = vim_strchr(s, sep);
26109 if (p != NULL)
26110 {
26111 pat = vim_strnsave(s, (int)(p - s));
26112 if (pat != NULL)
26113 {
26114 s = p + 1;
26115 /* find end of substitution */
26116 p = vim_strchr(s, sep);
26117 if (p != NULL)
26118 {
26119 sub = vim_strnsave(s, (int)(p - s));
26120 str = vim_strnsave(*fnamep, *fnamelen);
26121 if (sub != NULL && str != NULL)
26122 {
26123 *usedlen = (int)(p + 1 - src);
26124 s = do_string_sub(str, pat, sub, flags);
26125 if (s != NULL)
26126 {
26127 *fnamep = s;
26128 *fnamelen = (int)STRLEN(s);
26129 vim_free(*bufp);
26130 *bufp = s;
26131 didit = TRUE;
26132 }
26133 }
26134 vim_free(sub);
26135 vim_free(str);
26136 }
26137 vim_free(pat);
26138 }
26139 }
26140 /* after using ":s", repeat all the modifiers */
26141 if (didit)
26142 goto repeat;
26143 }
26144 }
26145
Bram Moolenaar26df0922014-02-23 23:39:13 +010026146 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26147 {
26148 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26149 if (p == NULL)
26150 return -1;
26151 vim_free(*bufp);
26152 *bufp = *fnamep = p;
26153 *fnamelen = (int)STRLEN(p);
26154 *usedlen += 2;
26155 }
26156
Bram Moolenaar071d4272004-06-13 20:20:40 +000026157 return valid;
26158}
26159
26160/*
26161 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26162 * "flags" can be "g" to do a global substitute.
26163 * Returns an allocated string, NULL for error.
26164 */
26165 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026166do_string_sub(
26167 char_u *str,
26168 char_u *pat,
26169 char_u *sub,
26170 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171{
26172 int sublen;
26173 regmatch_T regmatch;
26174 int i;
26175 int do_all;
26176 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026177 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026178 garray_T ga;
26179 char_u *ret;
26180 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026181 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026182
26183 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26184 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026185 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026186
26187 ga_init2(&ga, 1, 200);
26188
26189 do_all = (flags[0] == 'g');
26190
26191 regmatch.rm_ic = p_ic;
26192 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26193 if (regmatch.regprog != NULL)
26194 {
26195 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026196 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026197 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26198 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026199 /* Skip empty match except for first match. */
26200 if (regmatch.startp[0] == regmatch.endp[0])
26201 {
26202 if (zero_width == regmatch.startp[0])
26203 {
26204 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026205 i = MB_PTR2LEN(tail);
26206 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26207 (size_t)i);
26208 ga.ga_len += i;
26209 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026210 continue;
26211 }
26212 zero_width = regmatch.startp[0];
26213 }
26214
Bram Moolenaar071d4272004-06-13 20:20:40 +000026215 /*
26216 * Get some space for a temporary buffer to do the substitution
26217 * into. It will contain:
26218 * - The text up to where the match is.
26219 * - The substituted text.
26220 * - The text after the match.
26221 */
26222 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026223 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026224 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26225 {
26226 ga_clear(&ga);
26227 break;
26228 }
26229
26230 /* copy the text up to where the match is */
26231 i = (int)(regmatch.startp[0] - tail);
26232 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26233 /* add the substituted text */
26234 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26235 + ga.ga_len + i, TRUE, TRUE, FALSE);
26236 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026237 tail = regmatch.endp[0];
26238 if (*tail == NUL)
26239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026240 if (!do_all)
26241 break;
26242 }
26243
26244 if (ga.ga_data != NULL)
26245 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26246
Bram Moolenaar473de612013-06-08 18:19:48 +020026247 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026248 }
26249
26250 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26251 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026252 if (p_cpo == empty_option)
26253 p_cpo = save_cpo;
26254 else
26255 /* Darn, evaluating {sub} expression changed the value. */
26256 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026257
26258 return ret;
26259}
26260
26261#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */