blob: a49d54bd056bd39271286901ddc94d9e011acaee [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 Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
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 Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000355 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000356 {VV_NAME("swapname", VAR_STRING), VV_RO},
357 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000358 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200359 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000360 {VV_NAME("mouse_win", VAR_NUMBER), 0},
361 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
362 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000363 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100365 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200367 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200368 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200369 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200370 {VV_NAME("option_new", VAR_STRING), VV_RO},
371 {VV_NAME("option_old", VAR_STRING), VV_RO},
372 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100373 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100374 {VV_NAME("false", VAR_SPECIAL), VV_RO},
375 {VV_NAME("true", VAR_SPECIAL), VV_RO},
376 {VV_NAME("null", VAR_SPECIAL), VV_RO},
377 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100378 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200379 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000380};
381
382/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_type vv_di.di_tv.v_type
384#define vv_nr vv_di.di_tv.vval.v_number
385#define vv_float vv_di.di_tv.vval.v_float
386#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000387#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200388#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000389#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000390
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200391static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000392#define vimvarht vimvardict.dv_hashtab
393
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100394static void prepare_vimvar(int idx, typval_T *save_tv);
395static void restore_vimvar(int idx, typval_T *save_tv);
396static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
397static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
398static char_u *skip_var_one(char_u *arg);
399static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
400static void list_glob_vars(int *first);
401static void list_buf_vars(int *first);
402static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100404static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_vim_vars(int *first);
407static void list_script_vars(int *first);
408static void list_func_vars(int *first);
409static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
410static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
411static int check_changedtick(char_u *arg);
412static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
413static void clear_lval(lval_T *lp);
414static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
415static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
416static void list_fix_watch(list_T *l, listitem_T *item);
417static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
418static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
419static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
420static void item_lock(typval_T *tv, int deep, int lock);
421static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100423static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
424static int eval1(char_u **arg, typval_T *rettv, int evaluate);
425static int eval2(char_u **arg, typval_T *rettv, int evaluate);
426static int eval3(char_u **arg, typval_T *rettv, int evaluate);
427static int eval4(char_u **arg, typval_T *rettv, int evaluate);
428static int eval5(char_u **arg, typval_T *rettv, int evaluate);
429static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
430static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000431
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100432static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
433static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
434static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
435static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200437static void list_free_contents(list_T *l);
438static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static long list_len(list_T *l);
440static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
441static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
442static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
443static long list_find_nr(list_T *l, long idx, int *errorp);
444static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100445static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
446static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
447static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200448static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
449static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap);
450static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100451static int free_unref_items(int copyID);
452static dictitem_T *dictitem_copy(dictitem_T *org);
453static void dictitem_remove(dict_T *dict, dictitem_T *item);
454static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
455static long dict_len(dict_T *d);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200456static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200458static char_u *echo_string_core(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID, int echo_style, int restore_copyID, int dict_val);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100460static char_u *string_quote(char_u *str, int function);
461static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
462static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100463static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
464static 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, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void emsg_funcname(char *ermsg, char_u *name);
466static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000467
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200468static void dict_free_contents(dict_T *d);
469static void dict_free_dict(dict_T *d);
470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100472static void f_abs(typval_T *argvars, typval_T *rettv);
473static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100475static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100476static void f_and(typval_T *argvars, typval_T *rettv);
477static void f_append(typval_T *argvars, typval_T *rettv);
478static void f_argc(typval_T *argvars, typval_T *rettv);
479static void f_argidx(typval_T *argvars, typval_T *rettv);
480static void f_arglistid(typval_T *argvars, typval_T *rettv);
481static void f_argv(typval_T *argvars, typval_T *rettv);
482static void f_assert_equal(typval_T *argvars, typval_T *rettv);
483static void f_assert_exception(typval_T *argvars, typval_T *rettv);
484static void f_assert_fails(typval_T *argvars, typval_T *rettv);
485static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200486static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200487static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
488static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_asin(typval_T *argvars, typval_T *rettv);
492static void f_atan(typval_T *argvars, typval_T *rettv);
493static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100495static void f_browse(typval_T *argvars, typval_T *rettv);
496static void f_browsedir(typval_T *argvars, typval_T *rettv);
497static void f_bufexists(typval_T *argvars, typval_T *rettv);
498static void f_buflisted(typval_T *argvars, typval_T *rettv);
499static void f_bufloaded(typval_T *argvars, typval_T *rettv);
500static void f_bufname(typval_T *argvars, typval_T *rettv);
501static void f_bufnr(typval_T *argvars, typval_T *rettv);
502static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
503static void f_byte2line(typval_T *argvars, typval_T *rettv);
504static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
505static void f_byteidx(typval_T *argvars, typval_T *rettv);
506static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
507static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100509static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000510#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100513static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
514static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100515static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100516static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100517static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100518static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100519static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
520static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100521static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100523static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
524static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100525static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100526static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100527#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_changenr(typval_T *argvars, typval_T *rettv);
529static void f_char2nr(typval_T *argvars, typval_T *rettv);
530static void f_cindent(typval_T *argvars, typval_T *rettv);
531static void f_clearmatches(typval_T *argvars, typval_T *rettv);
532static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000533#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100534static void f_complete(typval_T *argvars, typval_T *rettv);
535static void f_complete_add(typval_T *argvars, typval_T *rettv);
536static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000537#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100538static void f_confirm(typval_T *argvars, typval_T *rettv);
539static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_cos(typval_T *argvars, typval_T *rettv);
542static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_count(typval_T *argvars, typval_T *rettv);
545static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
546static void f_cursor(typval_T *argsvars, typval_T *rettv);
547static void f_deepcopy(typval_T *argvars, typval_T *rettv);
548static void f_delete(typval_T *argvars, typval_T *rettv);
549static void f_did_filetype(typval_T *argvars, typval_T *rettv);
550static void f_diff_filler(typval_T *argvars, typval_T *rettv);
551static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
552static void f_empty(typval_T *argvars, typval_T *rettv);
553static void f_escape(typval_T *argvars, typval_T *rettv);
554static void f_eval(typval_T *argvars, typval_T *rettv);
555static void f_eventhandler(typval_T *argvars, typval_T *rettv);
556static void f_executable(typval_T *argvars, typval_T *rettv);
557static void f_exepath(typval_T *argvars, typval_T *rettv);
558static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200559#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_expand(typval_T *argvars, typval_T *rettv);
563static void f_extend(typval_T *argvars, typval_T *rettv);
564static void f_feedkeys(typval_T *argvars, typval_T *rettv);
565static void f_filereadable(typval_T *argvars, typval_T *rettv);
566static void f_filewritable(typval_T *argvars, typval_T *rettv);
567static void f_filter(typval_T *argvars, typval_T *rettv);
568static void f_finddir(typval_T *argvars, typval_T *rettv);
569static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000570#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100571static void f_float2nr(typval_T *argvars, typval_T *rettv);
572static void f_floor(typval_T *argvars, typval_T *rettv);
573static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000574#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100575static void f_fnameescape(typval_T *argvars, typval_T *rettv);
576static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
577static void f_foldclosed(typval_T *argvars, typval_T *rettv);
578static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
579static void f_foldlevel(typval_T *argvars, typval_T *rettv);
580static void f_foldtext(typval_T *argvars, typval_T *rettv);
581static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
582static void f_foreground(typval_T *argvars, typval_T *rettv);
583static void f_function(typval_T *argvars, typval_T *rettv);
584static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
585static void f_get(typval_T *argvars, typval_T *rettv);
586static void f_getbufline(typval_T *argvars, typval_T *rettv);
587static void f_getbufvar(typval_T *argvars, typval_T *rettv);
588static void f_getchar(typval_T *argvars, typval_T *rettv);
589static void f_getcharmod(typval_T *argvars, typval_T *rettv);
590static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
591static void f_getcmdline(typval_T *argvars, typval_T *rettv);
592static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
593static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
594static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
595static void f_getcwd(typval_T *argvars, typval_T *rettv);
596static void f_getfontname(typval_T *argvars, typval_T *rettv);
597static void f_getfperm(typval_T *argvars, typval_T *rettv);
598static void f_getfsize(typval_T *argvars, typval_T *rettv);
599static void f_getftime(typval_T *argvars, typval_T *rettv);
600static void f_getftype(typval_T *argvars, typval_T *rettv);
601static void f_getline(typval_T *argvars, typval_T *rettv);
602static void f_getmatches(typval_T *argvars, typval_T *rettv);
603static void f_getpid(typval_T *argvars, typval_T *rettv);
604static void f_getcurpos(typval_T *argvars, typval_T *rettv);
605static void f_getpos(typval_T *argvars, typval_T *rettv);
606static void f_getqflist(typval_T *argvars, typval_T *rettv);
607static void f_getreg(typval_T *argvars, typval_T *rettv);
608static void f_getregtype(typval_T *argvars, typval_T *rettv);
609static void f_gettabvar(typval_T *argvars, typval_T *rettv);
610static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
611static void f_getwinposx(typval_T *argvars, typval_T *rettv);
612static void f_getwinposy(typval_T *argvars, typval_T *rettv);
613static void f_getwinvar(typval_T *argvars, typval_T *rettv);
614static void f_glob(typval_T *argvars, typval_T *rettv);
615static void f_globpath(typval_T *argvars, typval_T *rettv);
616static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
617static void f_has(typval_T *argvars, typval_T *rettv);
618static void f_has_key(typval_T *argvars, typval_T *rettv);
619static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
620static void f_hasmapto(typval_T *argvars, typval_T *rettv);
621static void f_histadd(typval_T *argvars, typval_T *rettv);
622static void f_histdel(typval_T *argvars, typval_T *rettv);
623static void f_histget(typval_T *argvars, typval_T *rettv);
624static void f_histnr(typval_T *argvars, typval_T *rettv);
625static void f_hlID(typval_T *argvars, typval_T *rettv);
626static void f_hlexists(typval_T *argvars, typval_T *rettv);
627static void f_hostname(typval_T *argvars, typval_T *rettv);
628static void f_iconv(typval_T *argvars, typval_T *rettv);
629static void f_indent(typval_T *argvars, typval_T *rettv);
630static void f_index(typval_T *argvars, typval_T *rettv);
631static void f_input(typval_T *argvars, typval_T *rettv);
632static void f_inputdialog(typval_T *argvars, typval_T *rettv);
633static void f_inputlist(typval_T *argvars, typval_T *rettv);
634static void f_inputrestore(typval_T *argvars, typval_T *rettv);
635static void f_inputsave(typval_T *argvars, typval_T *rettv);
636static void f_inputsecret(typval_T *argvars, typval_T *rettv);
637static void f_insert(typval_T *argvars, typval_T *rettv);
638static void f_invert(typval_T *argvars, typval_T *rettv);
639static void f_isdirectory(typval_T *argvars, typval_T *rettv);
640static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100641#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
642static void f_isnan(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100645#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100646static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100647static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100648static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100649static void f_job_start(typval_T *argvars, typval_T *rettv);
650static void f_job_stop(typval_T *argvars, typval_T *rettv);
651static void f_job_status(typval_T *argvars, typval_T *rettv);
652#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100653static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100654static void f_js_decode(typval_T *argvars, typval_T *rettv);
655static void f_js_encode(typval_T *argvars, typval_T *rettv);
656static void f_json_decode(typval_T *argvars, typval_T *rettv);
657static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_keys(typval_T *argvars, typval_T *rettv);
659static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
660static void f_len(typval_T *argvars, typval_T *rettv);
661static void f_libcall(typval_T *argvars, typval_T *rettv);
662static void f_libcallnr(typval_T *argvars, typval_T *rettv);
663static void f_line(typval_T *argvars, typval_T *rettv);
664static void f_line2byte(typval_T *argvars, typval_T *rettv);
665static void f_lispindent(typval_T *argvars, typval_T *rettv);
666static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000667#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100668static void f_log(typval_T *argvars, typval_T *rettv);
669static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200671#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200673#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_map(typval_T *argvars, typval_T *rettv);
675static void f_maparg(typval_T *argvars, typval_T *rettv);
676static void f_mapcheck(typval_T *argvars, typval_T *rettv);
677static void f_match(typval_T *argvars, typval_T *rettv);
678static void f_matchadd(typval_T *argvars, typval_T *rettv);
679static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
680static void f_matcharg(typval_T *argvars, typval_T *rettv);
681static void f_matchdelete(typval_T *argvars, typval_T *rettv);
682static void f_matchend(typval_T *argvars, typval_T *rettv);
683static void f_matchlist(typval_T *argvars, typval_T *rettv);
684static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200685static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_max(typval_T *argvars, typval_T *rettv);
687static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000688#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100692#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
696static void f_nr2char(typval_T *argvars, typval_T *rettv);
697static void f_or(typval_T *argvars, typval_T *rettv);
698static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100699#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100701#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
706static void f_printf(typval_T *argvars, typval_T *rettv);
707static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200708#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200710#endif
711#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200713#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100714static void f_range(typval_T *argvars, typval_T *rettv);
715static void f_readfile(typval_T *argvars, typval_T *rettv);
716static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100717#ifdef FEAT_FLOAT
718static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
719#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_reltimestr(typval_T *argvars, typval_T *rettv);
721static void f_remote_expr(typval_T *argvars, typval_T *rettv);
722static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
723static void f_remote_peek(typval_T *argvars, typval_T *rettv);
724static void f_remote_read(typval_T *argvars, typval_T *rettv);
725static void f_remote_send(typval_T *argvars, typval_T *rettv);
726static void f_remove(typval_T *argvars, typval_T *rettv);
727static void f_rename(typval_T *argvars, typval_T *rettv);
728static void f_repeat(typval_T *argvars, typval_T *rettv);
729static void f_resolve(typval_T *argvars, typval_T *rettv);
730static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000731#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100732static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_screenattr(typval_T *argvars, typval_T *rettv);
735static void f_screenchar(typval_T *argvars, typval_T *rettv);
736static void f_screencol(typval_T *argvars, typval_T *rettv);
737static void f_screenrow(typval_T *argvars, typval_T *rettv);
738static void f_search(typval_T *argvars, typval_T *rettv);
739static void f_searchdecl(typval_T *argvars, typval_T *rettv);
740static void f_searchpair(typval_T *argvars, typval_T *rettv);
741static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
742static void f_searchpos(typval_T *argvars, typval_T *rettv);
743static void f_server2client(typval_T *argvars, typval_T *rettv);
744static void f_serverlist(typval_T *argvars, typval_T *rettv);
745static void f_setbufvar(typval_T *argvars, typval_T *rettv);
746static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
747static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100748static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_setline(typval_T *argvars, typval_T *rettv);
750static void f_setloclist(typval_T *argvars, typval_T *rettv);
751static void f_setmatches(typval_T *argvars, typval_T *rettv);
752static void f_setpos(typval_T *argvars, typval_T *rettv);
753static void f_setqflist(typval_T *argvars, typval_T *rettv);
754static void f_setreg(typval_T *argvars, typval_T *rettv);
755static void f_settabvar(typval_T *argvars, typval_T *rettv);
756static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
757static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100758#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100760#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_shellescape(typval_T *argvars, typval_T *rettv);
762static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
763static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_sin(typval_T *argvars, typval_T *rettv);
766static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_sort(typval_T *argvars, typval_T *rettv);
769static void f_soundfold(typval_T *argvars, typval_T *rettv);
770static void f_spellbadword(typval_T *argvars, typval_T *rettv);
771static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
772static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000773#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100774static void f_sqrt(typval_T *argvars, typval_T *rettv);
775static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000776#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_str2nr(typval_T *argvars, typval_T *rettv);
778static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000779#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200782static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100783static void f_stridx(typval_T *argvars, typval_T *rettv);
784static void f_string(typval_T *argvars, typval_T *rettv);
785static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200786static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_strpart(typval_T *argvars, typval_T *rettv);
788static void f_strridx(typval_T *argvars, typval_T *rettv);
789static void f_strtrans(typval_T *argvars, typval_T *rettv);
790static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
791static void f_strwidth(typval_T *argvars, typval_T *rettv);
792static void f_submatch(typval_T *argvars, typval_T *rettv);
793static void f_substitute(typval_T *argvars, typval_T *rettv);
794static void f_synID(typval_T *argvars, typval_T *rettv);
795static void f_synIDattr(typval_T *argvars, typval_T *rettv);
796static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
797static void f_synstack(typval_T *argvars, typval_T *rettv);
798static void f_synconcealed(typval_T *argvars, typval_T *rettv);
799static void f_system(typval_T *argvars, typval_T *rettv);
800static void f_systemlist(typval_T *argvars, typval_T *rettv);
801static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
802static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
803static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
804static void f_taglist(typval_T *argvars, typval_T *rettv);
805static void f_tagfiles(typval_T *argvars, typval_T *rettv);
806static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200807static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
808static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200809static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
810#ifdef FEAT_JOB_CHANNEL
811static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
812#endif
813static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
814#ifdef FEAT_JOB_CHANNEL
815static void f_test_null_job(typval_T *argvars, typval_T *rettv);
816#endif
817static void f_test_null_list(typval_T *argvars, typval_T *rettv);
818static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
819static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200820#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static void f_tan(typval_T *argvars, typval_T *rettv);
822static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200823#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100824#ifdef FEAT_TIMERS
825static void f_timer_start(typval_T *argvars, typval_T *rettv);
826static void f_timer_stop(typval_T *argvars, typval_T *rettv);
827#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100828static void f_tolower(typval_T *argvars, typval_T *rettv);
829static void f_toupper(typval_T *argvars, typval_T *rettv);
830static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000831#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000833#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100834static void f_type(typval_T *argvars, typval_T *rettv);
835static void f_undofile(typval_T *argvars, typval_T *rettv);
836static void f_undotree(typval_T *argvars, typval_T *rettv);
837static void f_uniq(typval_T *argvars, typval_T *rettv);
838static void f_values(typval_T *argvars, typval_T *rettv);
839static void f_virtcol(typval_T *argvars, typval_T *rettv);
840static void f_visualmode(typval_T *argvars, typval_T *rettv);
841static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100842static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100843static void f_win_getid(typval_T *argvars, typval_T *rettv);
844static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
845static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
846static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static void f_winbufnr(typval_T *argvars, typval_T *rettv);
848static void f_wincol(typval_T *argvars, typval_T *rettv);
849static void f_winheight(typval_T *argvars, typval_T *rettv);
850static void f_winline(typval_T *argvars, typval_T *rettv);
851static void f_winnr(typval_T *argvars, typval_T *rettv);
852static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
853static void f_winrestview(typval_T *argvars, typval_T *rettv);
854static void f_winsaveview(typval_T *argvars, typval_T *rettv);
855static void f_winwidth(typval_T *argvars, typval_T *rettv);
856static void f_writefile(typval_T *argvars, typval_T *rettv);
857static void f_wordcount(typval_T *argvars, typval_T *rettv);
858static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100860static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
861static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
862static int get_env_len(char_u **arg);
863static int get_id_len(char_u **arg);
864static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
865static 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 +0000866#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
867#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
868 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
870static int eval_isnamec(int c);
871static int eval_isnamec1(int c);
872static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
873static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static typval_T *alloc_string_tv(char_u *string);
875static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100876#ifdef FEAT_FLOAT
877static float_T get_tv_float(typval_T *varp);
878#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879static linenr_T get_tv_lnum(typval_T *argvars);
880static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
882static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
883static hashtab_T *find_var_ht(char_u *name, char_u **varname);
884static funccall_T *get_funccal(void);
885static void vars_clear_ext(hashtab_T *ht, int free_val);
886static void delete_var(hashtab_T *ht, hashitem_T *hi);
887static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
888static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
889static void set_var(char_u *name, typval_T *varp, int copy);
890static int var_check_ro(int flags, char_u *name, int use_gettext);
891static int var_check_fixed(int flags, char_u *name, int use_gettext);
892static int var_check_func_name(char_u *name, int new_var);
893static int valid_varname(char_u *varname);
894static int tv_check_lock(int lock, char_u *name, int use_gettext);
895static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
896static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100897static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100898static int eval_fname_script(char_u *p);
899static int eval_fname_sid(char_u *p);
900static void list_func_head(ufunc_T *fp, int indent);
901static ufunc_T *find_func(char_u *name);
902static int function_exists(char_u *name);
903static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100905static void func_do_profile(ufunc_T *fp);
906static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
907static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000908static int
909# ifdef __BORLANDC__
910 _RTLENTRYF
911# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100912 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000913static int
914# ifdef __BORLANDC__
915 _RTLENTRYF
916# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100917 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000918#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100919static int script_autoload(char_u *name, int reload);
920static char_u *autoload_name(char_u *name);
921static void cat_func_name(char_u *buf, ufunc_T *fp);
922static void func_free(ufunc_T *fp);
923static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
924static int can_free_funccal(funccall_T *fc, int copyID) ;
925static void free_funccal(funccall_T *fc, int free_val);
926static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
927static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
928static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
929static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
930static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
931static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
932static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
933static int write_list(FILE *fd, list_T *list, int binary);
934static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000935
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200936
937#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100938static int compare_func_name(const void *s1, const void *s2);
939static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200940#endif
941
Bram Moolenaar33570922005-01-25 22:26:29 +0000942/*
943 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000944 */
945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000947{
Bram Moolenaar33570922005-01-25 22:26:29 +0000948 int i;
949 struct vimvar *p;
950
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200951 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
952 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200953 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000954 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000956
957 for (i = 0; i < VV_LEN; ++i)
958 {
959 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200960 if (STRLEN(p->vv_name) > 16)
961 {
962 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
963 getout(1);
964 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 STRCPY(p->vv_di.di_key, p->vv_name);
966 if (p->vv_flags & VV_RO)
967 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
968 else if (p->vv_flags & VV_RO_SBX)
969 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
970 else
971 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000972
973 /* add to v: scope dict, unless the value is not always available */
974 if (p->vv_type != VAR_UNKNOWN)
975 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000976 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000977 /* add to compat scope dict */
978 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100980 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
981
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000982 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100983 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200984 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100985 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100986
987 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
988 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
989 set_vim_var_nr(VV_NONE, VVAL_NONE);
990 set_vim_var_nr(VV_NULL, VVAL_NULL);
991
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200992 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200993
994#ifdef EBCDIC
995 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100996 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200997 */
998 sortFunctions();
999#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001000}
1001
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001002#if defined(EXITFREE) || defined(PROTO)
1003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001004eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001005{
1006 int i;
1007 struct vimvar *p;
1008
1009 for (i = 0; i < VV_LEN; ++i)
1010 {
1011 p = &vimvars[i];
1012 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001013 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001014 vim_free(p->vv_str);
1015 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001016 }
1017 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1018 {
1019 list_unref(p->vv_list);
1020 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001021 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001022 }
1023 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001024 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001025 hash_clear(&compat_hashtab);
1026
Bram Moolenaard9fba312005-06-26 22:34:35 +00001027 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001028# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001029 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001030# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001031
1032 /* global variables */
1033 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001034
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001035 /* autoloaded script names */
1036 ga_clear_strings(&ga_loaded);
1037
Bram Moolenaarcca74132013-09-25 21:00:28 +02001038 /* Script-local variables. First clear all the variables and in a second
1039 * loop free the scriptvar_T, because a variable in one script might hold
1040 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001041 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001042 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001043 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001044 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001045 ga_clear(&ga_scripts);
1046
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001047 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001048 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001049
1050 /* functions */
1051 free_all_functions();
1052 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001053}
1054#endif
1055
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 * Return the name of the executed function.
1058 */
1059 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001060func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063}
1064
1065/*
1066 * Return the address holding the next breakpoint line for a funccall cookie.
1067 */
1068 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001069func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaar33570922005-01-25 22:26:29 +00001071 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072}
1073
1074/*
1075 * Return the address holding the debug tick for a funccall cookie.
1076 */
1077 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001078func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar33570922005-01-25 22:26:29 +00001080 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081}
1082
1083/*
1084 * Return the nesting level for a funccall cookie.
1085 */
1086 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001087func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
Bram Moolenaar33570922005-01-25 22:26:29 +00001089 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090}
1091
1092/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001093funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001095/* pointer to list of previously used funccal, still around because some
1096 * item in it is still being used. */
1097funccall_T *previous_funccal = NULL;
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099/*
1100 * Return TRUE when a function was ended by a ":return" command.
1101 */
1102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001103current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 return current_funccal->returned;
1106}
1107
1108
1109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 * Set an internal variable to a string value. Creates the variable if it does
1111 * not already exist.
1112 */
1113 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001114set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001116 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001117 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119 val = vim_strsave(value);
1120 if (val != NULL)
1121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001123 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001125 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 }
1129}
1130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133static char_u *redir_endp = NULL;
1134static char_u *redir_varname = NULL;
1135
1136/*
1137 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001138 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 * Returns OK if successfully completed the setup. FAIL otherwise.
1140 */
1141 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001142var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143{
1144 int save_emsg;
1145 int err;
1146 typval_T tv;
1147
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001148 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001149 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 {
1151 EMSG(_(e_invarg));
1152 return FAIL;
1153 }
1154
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 redir_varname = vim_strsave(name);
1157 if (redir_varname == NULL)
1158 return FAIL;
1159
1160 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1161 if (redir_lval == NULL)
1162 {
1163 var_redir_stop();
1164 return FAIL;
1165 }
1166
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 /* The output is stored in growarray "redir_ga" until redirection ends. */
1168 ga_init2(&redir_ga, (int)sizeof(char), 500);
1169
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001171 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001172 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1174 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001175 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 if (redir_endp != NULL && *redir_endp != NUL)
1177 /* Trailing characters are present after the variable name */
1178 EMSG(_(e_trailing));
1179 else
1180 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001181 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 var_redir_stop();
1183 return FAIL;
1184 }
1185
1186 /* check if we can write to the variable: set it to or append an empty
1187 * string */
1188 save_emsg = did_emsg;
1189 did_emsg = FALSE;
1190 tv.v_type = VAR_STRING;
1191 tv.vval.v_string = (char_u *)"";
1192 if (append)
1193 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1194 else
1195 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001196 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001198 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199 if (err)
1200 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 var_redir_stop();
1203 return FAIL;
1204 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205
1206 return OK;
1207}
1208
1209/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001210 * Append "value[value_len]" to the variable set by var_redir_start().
1211 * The actual appending is postponed until redirection ends, because the value
1212 * appended may in fact be the string we write to, changing it may cause freed
1213 * memory to be used:
1214 * :redir => foo
1215 * :let foo
1216 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217 */
1218 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001219var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001221 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001222
1223 if (redir_lval == NULL)
1224 return;
1225
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001227 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001229 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001230
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001231 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232 {
1233 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001234 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001235 }
1236 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001238}
1239
1240/*
1241 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001242 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001243 */
1244 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001245var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001246{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001247 typval_T tv;
1248
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001249 if (redir_lval != NULL)
1250 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001251 /* If there was no error: assign the text to the variable. */
1252 if (redir_endp != NULL)
1253 {
1254 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1255 tv.v_type = VAR_STRING;
1256 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001257 /* Call get_lval() again, if it's inside a Dict or List it may
1258 * have changed. */
1259 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001260 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001261 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1262 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1263 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001264 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001265
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001266 /* free the collected output */
1267 vim_free(redir_ga.ga_data);
1268 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001269
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001270 vim_free(redir_lval);
1271 redir_lval = NULL;
1272 }
1273 vim_free(redir_varname);
1274 redir_varname = NULL;
1275}
1276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277# if defined(FEAT_MBYTE) || defined(PROTO)
1278 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001279eval_charconvert(
1280 char_u *enc_from,
1281 char_u *enc_to,
1282 char_u *fname_from,
1283 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 int err = FALSE;
1286
1287 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1288 set_vim_var_string(VV_CC_TO, enc_to, -1);
1289 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1290 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1291 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1292 err = TRUE;
1293 set_vim_var_string(VV_CC_FROM, NULL, -1);
1294 set_vim_var_string(VV_CC_TO, NULL, -1);
1295 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1296 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1297
1298 if (err)
1299 return FAIL;
1300 return OK;
1301}
1302# endif
1303
1304# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1305 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001306eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 int err = FALSE;
1309
1310 set_vim_var_string(VV_FNAME_IN, fname, -1);
1311 set_vim_var_string(VV_CMDARG, args, -1);
1312 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1313 err = TRUE;
1314 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1315 set_vim_var_string(VV_CMDARG, NULL, -1);
1316
1317 if (err)
1318 {
1319 mch_remove(fname);
1320 return FAIL;
1321 }
1322 return OK;
1323}
1324# endif
1325
1326# if defined(FEAT_DIFF) || defined(PROTO)
1327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001328eval_diff(
1329 char_u *origfile,
1330 char_u *newfile,
1331 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 int err = FALSE;
1334
1335 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1336 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1337 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1338 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1339 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1340 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1341 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1342}
1343
1344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001345eval_patch(
1346 char_u *origfile,
1347 char_u *difffile,
1348 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349{
1350 int err;
1351
1352 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1353 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1354 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1355 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1356 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1357 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1358 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1359}
1360# endif
1361
1362/*
1363 * Top level evaluation function, returning a boolean.
1364 * Sets "error" to TRUE if there was an error.
1365 * Return TRUE or FALSE.
1366 */
1367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001368eval_to_bool(
1369 char_u *arg,
1370 int *error,
1371 char_u **nextcmd,
1372 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
Bram Moolenaar33570922005-01-25 22:26:29 +00001374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 int retval = FALSE;
1376
1377 if (skip)
1378 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 else
1382 {
1383 *error = FALSE;
1384 if (!skip)
1385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001386 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001387 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 }
1390 if (skip)
1391 --emsg_skip;
1392
1393 return retval;
1394}
1395
1396/*
1397 * Top level evaluation function, returning a string. If "skip" is TRUE,
1398 * only parsing to "nextcmd" is done, without reporting errors. Return
1399 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1400 */
1401 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402eval_to_string_skip(
1403 char_u *arg,
1404 char_u **nextcmd,
1405 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 char_u *retval;
1409
1410 if (skip)
1411 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 retval = NULL;
1414 else
1415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 retval = vim_strsave(get_tv_string(&tv));
1417 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 if (skip)
1420 --emsg_skip;
1421
1422 return retval;
1423}
1424
1425/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001426 * Skip over an expression at "*pp".
1427 * Return FAIL for an error, OK otherwise.
1428 */
1429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001430skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001431{
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001433
1434 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001435 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001436}
1437
1438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001440 * When "convert" is TRUE convert a List into a sequence of lines and convert
1441 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 * Return pointer to allocated memory, or NULL for failure.
1443 */
1444 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001445eval_to_string(
1446 char_u *arg,
1447 char_u **nextcmd,
1448 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
Bram Moolenaar33570922005-01-25 22:26:29 +00001450 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001452 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001453#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001454 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001457 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 retval = NULL;
1459 else
1460 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001461 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001462 {
1463 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001464 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001465 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001466 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001467 if (tv.vval.v_list->lv_len > 0)
1468 ga_append(&ga, NL);
1469 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 ga_append(&ga, NUL);
1471 retval = (char_u *)ga.ga_data;
1472 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001473#ifdef FEAT_FLOAT
1474 else if (convert && tv.v_type == VAR_FLOAT)
1475 {
1476 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1477 retval = vim_strsave(numbuf);
1478 }
1479#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001480 else
1481 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484
1485 return retval;
1486}
1487
1488/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001489 * Call eval_to_string() without using current local variables and using
1490 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 */
1492 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493eval_to_string_safe(
1494 char_u *arg,
1495 char_u **nextcmd,
1496 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 char_u *retval;
1499 void *save_funccalp;
1500
1501 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001502 if (use_sandbox)
1503 ++sandbox;
1504 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001505 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001506 if (use_sandbox)
1507 --sandbox;
1508 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 restore_funccal(save_funccalp);
1510 return retval;
1511}
1512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513/*
1514 * Top level evaluation function, returning a number.
1515 * Evaluates "expr" silently.
1516 * Returns -1 for an error.
1517 */
1518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
Bram Moolenaar33570922005-01-25 22:26:29 +00001521 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001523 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
1525 ++emsg_off;
1526
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 retval = -1;
1529 else
1530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001531 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 --emsg_off;
1535
1536 return retval;
1537}
1538
Bram Moolenaara40058a2005-07-11 22:42:07 +00001539/*
1540 * Prepare v: variable "idx" to be used.
1541 * Save the current typeval in "save_tv".
1542 * When not used yet add the variable to the v: hashtable.
1543 */
1544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001545prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001546{
1547 *save_tv = vimvars[idx].vv_tv;
1548 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1549 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1550}
1551
1552/*
1553 * Restore v: variable "idx" to typeval "save_tv".
1554 * When no longer defined, remove the variable from the v: hashtable.
1555 */
1556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001557restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001558{
1559 hashitem_T *hi;
1560
Bram Moolenaara40058a2005-07-11 22:42:07 +00001561 vimvars[idx].vv_tv = *save_tv;
1562 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1563 {
1564 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1565 if (HASHITEM_EMPTY(hi))
1566 EMSG2(_(e_intern2), "restore_vimvar()");
1567 else
1568 hash_remove(&vimvarht, hi);
1569 }
1570}
1571
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001572#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001573/*
1574 * Evaluate an expression to a list with suggestions.
1575 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001576 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001577 */
1578 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580{
1581 typval_T save_val;
1582 typval_T rettv;
1583 list_T *list = NULL;
1584 char_u *p = skipwhite(expr);
1585
1586 /* Set "v:val" to the bad word. */
1587 prepare_vimvar(VV_VAL, &save_val);
1588 vimvars[VV_VAL].vv_type = VAR_STRING;
1589 vimvars[VV_VAL].vv_str = badword;
1590 if (p_verbose == 0)
1591 ++emsg_off;
1592
1593 if (eval1(&p, &rettv, TRUE) == OK)
1594 {
1595 if (rettv.v_type != VAR_LIST)
1596 clear_tv(&rettv);
1597 else
1598 list = rettv.vval.v_list;
1599 }
1600
1601 if (p_verbose == 0)
1602 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001603 restore_vimvar(VV_VAL, &save_val);
1604
1605 return list;
1606}
1607
1608/*
1609 * "list" is supposed to contain two items: a word and a number. Return the
1610 * word in "pp" and the number as the return value.
1611 * Return -1 if anything isn't right.
1612 * Used to get the good word and score from the eval_spell_expr() result.
1613 */
1614 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001615get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001616{
1617 listitem_T *li;
1618
1619 li = list->lv_first;
1620 if (li == NULL)
1621 return -1;
1622 *pp = get_tv_string(&li->li_tv);
1623
1624 li = li->li_next;
1625 if (li == NULL)
1626 return -1;
1627 return get_tv_number(&li->li_tv);
1628}
1629#endif
1630
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001631/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001632 * Top level evaluation function.
1633 * Returns an allocated typval_T with the result.
1634 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001635 */
1636 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001637eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001638{
1639 typval_T *tv;
1640
1641 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001642 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001643 {
1644 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001645 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001646 }
1647
1648 return tv;
1649}
1650
1651
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001654 * Uses argv[argc] for the function arguments. Only Number and String
1655 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001658 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659call_vim_function(
1660 char_u *func,
1661 int argc,
1662 char_u **argv,
1663 int safe, /* use the sandbox */
1664 int str_arg_only, /* all arguments are strings */
1665 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
Bram Moolenaar33570922005-01-25 22:26:29 +00001667 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 long n;
1669 int len;
1670 int i;
1671 int doesrange;
1672 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001675 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679 for (i = 0; i < argc; i++)
1680 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001681 /* Pass a NULL or empty argument as an empty string */
1682 if (argv[i] == NULL || *argv[i] == NUL)
1683 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001684 argvars[i].v_type = VAR_STRING;
1685 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001686 continue;
1687 }
1688
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001689 if (str_arg_only)
1690 len = 0;
1691 else
1692 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001693 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (len != 0 && len == (int)STRLEN(argv[i]))
1695 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001696 argvars[i].v_type = VAR_NUMBER;
1697 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699 else
1700 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001701 argvars[i].v_type = VAR_STRING;
1702 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 }
1704 }
1705
1706 if (safe)
1707 {
1708 save_funccalp = save_funccal();
1709 ++sandbox;
1710 }
1711
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1713 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001715 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (safe)
1717 {
1718 --sandbox;
1719 restore_funccal(save_funccalp);
1720 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 vim_free(argvars);
1722
1723 if (ret == FAIL)
1724 clear_tv(rettv);
1725
1726 return ret;
1727}
1728
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001729/*
1730 * Call vimL function "func" and return the result as a number.
1731 * Returns -1 when calling the function fails.
1732 * Uses argv[argc] for the function arguments.
1733 */
1734 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001735call_func_retnr(
1736 char_u *func,
1737 int argc,
1738 char_u **argv,
1739 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001740{
1741 typval_T rettv;
1742 long retval;
1743
1744 /* All arguments are passed as strings, no conversion to number. */
1745 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1746 return -1;
1747
1748 retval = get_tv_number_chk(&rettv, NULL);
1749 clear_tv(&rettv);
1750 return retval;
1751}
1752
1753#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1754 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1755
Bram Moolenaar4f688582007-07-24 12:34:30 +00001756# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001758 * Call vimL function "func" and return the result as a string.
1759 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760 * Uses argv[argc] for the function arguments.
1761 */
1762 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763call_func_retstr(
1764 char_u *func,
1765 int argc,
1766 char_u **argv,
1767 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001768{
1769 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001770 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001771
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001772 /* All arguments are passed as strings, no conversion to number. */
1773 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001774 return NULL;
1775
1776 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001777 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 return retval;
1779}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001780# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001781
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001782/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001783 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001784 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001785 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001786 */
1787 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788call_func_retlist(
1789 char_u *func,
1790 int argc,
1791 char_u **argv,
1792 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001793{
1794 typval_T rettv;
1795
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001796 /* All arguments are passed as strings, no conversion to number. */
1797 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001798 return NULL;
1799
1800 if (rettv.v_type != VAR_LIST)
1801 {
1802 clear_tv(&rettv);
1803 return NULL;
1804 }
1805
1806 return rettv.vval.v_list;
1807}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808#endif
1809
1810/*
1811 * Save the current function call pointer, and set it to NULL.
1812 * Used when executing autocommands and for ":source".
1813 */
1814 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001815save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001817 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 current_funccal = NULL;
1820 return (void *)fc;
1821}
1822
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001826 funccall_T *fc = (funccall_T *)vfc;
1827
1828 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829}
1830
Bram Moolenaar05159a02005-02-26 23:04:13 +00001831#if defined(FEAT_PROFILE) || defined(PROTO)
1832/*
1833 * Prepare profiling for entering a child or something else that is not
1834 * counted for the script/function itself.
1835 * Should always be called in pair with prof_child_exit().
1836 */
1837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838prof_child_enter(
1839 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001840{
1841 funccall_T *fc = current_funccal;
1842
1843 if (fc != NULL && fc->func->uf_profiling)
1844 profile_start(&fc->prof_child);
1845 script_prof_save(tm);
1846}
1847
1848/*
1849 * Take care of time spent in a child.
1850 * Should always be called after prof_child_enter().
1851 */
1852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001853prof_child_exit(
1854 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001855{
1856 funccall_T *fc = current_funccal;
1857
1858 if (fc != NULL && fc->func->uf_profiling)
1859 {
1860 profile_end(&fc->prof_child);
1861 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1862 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1863 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1864 }
1865 script_prof_restore(tm);
1866}
1867#endif
1868
1869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#ifdef FEAT_FOLDING
1871/*
1872 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1873 * it in "*cp". Doesn't give error messages.
1874 */
1875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001876eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
Bram Moolenaar33570922005-01-25 22:26:29 +00001878 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 int retval;
1880 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001881 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1882 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001885 if (use_sandbox)
1886 ++sandbox;
1887 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 retval = 0;
1891 else
1892 {
1893 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 if (tv.v_type == VAR_NUMBER)
1895 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001896 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 retval = 0;
1898 else
1899 {
1900 /* If the result is a string, check if there is a non-digit before
1901 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (!VIM_ISDIGIT(*s) && *s != '-')
1904 *cp = *s++;
1905 retval = atol((char *)s);
1906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001910 if (use_sandbox)
1911 --sandbox;
1912 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
1914 return retval;
1915}
1916#endif
1917
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 * ":let" list all variable values
1920 * ":let var1 var2" list variable values
1921 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 * ":let var += expr" assignment command.
1923 * ":let var -= expr" assignment command.
1924 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 */
1927 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001928ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929{
1930 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 int var_count = 0;
1935 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001937 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001938 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
Bram Moolenaardb552d602006-03-23 22:59:57 +00001940 argend = skip_var_list(arg, &var_count, &semicolon);
1941 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001943 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1944 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001945 expr = skipwhite(argend);
1946 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1947 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001949 /*
1950 * ":let" without "=": list variables
1951 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 if (*arg == '[')
1953 EMSG(_(e_invarg));
1954 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001956 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001960 list_glob_vars(&first);
1961 list_buf_vars(&first);
1962 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001963#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001964 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001965#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001966 list_script_vars(&first);
1967 list_func_vars(&first);
1968 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 eap->nextcmd = check_nextcmd(arg);
1971 }
1972 else
1973 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 op[0] = '=';
1975 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001976 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001978 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1979 op[0] = *expr; /* +=, -= or .= */
1980 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001982 else
1983 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (eap->skip)
1986 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (eap->skip)
1989 {
1990 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001991 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 --emsg_skip;
1993 }
1994 else if (i != FAIL)
1995 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 }
2001}
2002
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003/*
2004 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2005 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002006 * When "nextchars" is not NULL it points to a string with characters that
2007 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2008 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 * Returns OK or FAIL;
2010 */
2011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002012ex_let_vars(
2013 char_u *arg_start,
2014 typval_T *tv,
2015 int copy, /* copy values from "tv", don't move */
2016 int semicolon, /* from skip_var_list() */
2017 int var_count, /* from skip_var_list() */
2018 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019{
2020 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002021 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002023 listitem_T *item;
2024 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025
2026 if (*arg != '[')
2027 {
2028 /*
2029 * ":let var = expr" or ":for var in list"
2030 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002031 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 return FAIL;
2033 return OK;
2034 }
2035
2036 /*
2037 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2038 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002039 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 {
2041 EMSG(_(e_listreq));
2042 return FAIL;
2043 }
2044
2045 i = list_len(l);
2046 if (semicolon == 0 && var_count < i)
2047 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002048 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002049 return FAIL;
2050 }
2051 if (var_count - semicolon > i)
2052 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002053 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 return FAIL;
2055 }
2056
2057 item = l->lv_first;
2058 while (*arg != ']')
2059 {
2060 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002061 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 item = item->li_next;
2063 if (arg == NULL)
2064 return FAIL;
2065
2066 arg = skipwhite(arg);
2067 if (*arg == ';')
2068 {
2069 /* Put the rest of the list (may be empty) in the var after ';'.
2070 * Create a new list for this. */
2071 l = list_alloc();
2072 if (l == NULL)
2073 return FAIL;
2074 while (item != NULL)
2075 {
2076 list_append_tv(l, &item->li_tv);
2077 item = item->li_next;
2078 }
2079
2080 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002081 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002082 ltv.vval.v_list = l;
2083 l->lv_refcount = 1;
2084
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002085 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2086 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087 clear_tv(&ltv);
2088 if (arg == NULL)
2089 return FAIL;
2090 break;
2091 }
2092 else if (*arg != ',' && *arg != ']')
2093 {
2094 EMSG2(_(e_intern2), "ex_let_vars()");
2095 return FAIL;
2096 }
2097 }
2098
2099 return OK;
2100}
2101
2102/*
2103 * Skip over assignable variable "var" or list of variables "[var, var]".
2104 * Used for ":let varvar = expr" and ":for varvar in expr".
2105 * For "[var, var]" increment "*var_count" for each variable.
2106 * for "[var, var; var]" set "semicolon".
2107 * Return NULL for an error.
2108 */
2109 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002110skip_var_list(
2111 char_u *arg,
2112 int *var_count,
2113 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002114{
2115 char_u *p, *s;
2116
2117 if (*arg == '[')
2118 {
2119 /* "[var, var]": find the matching ']'. */
2120 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002121 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002122 {
2123 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2124 s = skip_var_one(p);
2125 if (s == p)
2126 {
2127 EMSG2(_(e_invarg2), p);
2128 return NULL;
2129 }
2130 ++*var_count;
2131
2132 p = skipwhite(s);
2133 if (*p == ']')
2134 break;
2135 else if (*p == ';')
2136 {
2137 if (*semicolon == 1)
2138 {
2139 EMSG(_("Double ; in list of variables"));
2140 return NULL;
2141 }
2142 *semicolon = 1;
2143 }
2144 else if (*p != ',')
2145 {
2146 EMSG2(_(e_invarg2), p);
2147 return NULL;
2148 }
2149 }
2150 return p + 1;
2151 }
2152 else
2153 return skip_var_one(arg);
2154}
2155
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002156/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002157 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002158 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002159 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002160 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002162{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002163 if (*arg == '@' && arg[1] != NUL)
2164 return arg + 2;
2165 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2166 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002167}
2168
Bram Moolenaara7043832005-01-21 11:56:39 +00002169/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002170 * List variables for hashtab "ht" with prefix "prefix".
2171 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174list_hashtable_vars(
2175 hashtab_T *ht,
2176 char_u *prefix,
2177 int empty,
2178 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002179{
Bram Moolenaar33570922005-01-25 22:26:29 +00002180 hashitem_T *hi;
2181 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002182 int todo;
2183
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002184 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002185 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2186 {
2187 if (!HASHITEM_EMPTY(hi))
2188 {
2189 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002190 di = HI2DI(hi);
2191 if (empty || di->di_tv.v_type != VAR_STRING
2192 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002194 }
2195 }
2196}
2197
2198/*
2199 * List global variables.
2200 */
2201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002203{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002205}
2206
2207/*
2208 * List buffer variables.
2209 */
2210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002211list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002212{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 char_u numbuf[NUMBUFLEN];
2214
Bram Moolenaar429fa852013-04-15 12:27:36 +02002215 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217
2218 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2220 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002221}
2222
2223/*
2224 * List window variables.
2225 */
2226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002228{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002229 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002231}
2232
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002233#ifdef FEAT_WINDOWS
2234/*
2235 * List tab page variables.
2236 */
2237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002239{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002240 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002242}
2243#endif
2244
Bram Moolenaara7043832005-01-21 11:56:39 +00002245/*
2246 * List Vim variables.
2247 */
2248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002249list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252}
2253
2254/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002255 * List script-local variables, if there is a script.
2256 */
2257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002258list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002259{
2260 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002261 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2262 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002263}
2264
2265/*
2266 * List function variables, if there is a function.
2267 */
2268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002269list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002270{
2271 if (current_funccal != NULL)
2272 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002274}
2275
2276/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 * List variables in "arg".
2278 */
2279 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002280list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281{
2282 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 char_u *name_start;
2286 char_u *arg_subsc;
2287 char_u *tofree;
2288 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289
2290 while (!ends_excmd(*arg) && !got_int)
2291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002294 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2296 {
2297 emsg_severe = TRUE;
2298 EMSG(_(e_trailing));
2299 break;
2300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 /* get_name_len() takes care of expanding curly braces */
2305 name_start = name = arg;
2306 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2307 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309 /* This is mainly to keep test 49 working: when expanding
2310 * curly braces fails overrule the exception error message. */
2311 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 emsg_severe = TRUE;
2314 EMSG2(_(e_invarg2), arg);
2315 break;
2316 }
2317 error = TRUE;
2318 }
2319 else
2320 {
2321 if (tofree != NULL)
2322 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002323 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 else
2326 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002327 /* handle d.key, l[idx], f(expr) */
2328 arg_subsc = arg;
2329 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002330 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002332 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002334 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002337 case 'g': list_glob_vars(first); break;
2338 case 'b': list_buf_vars(first); break;
2339 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002340#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002341 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002342#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002343 case 'v': list_vim_vars(first); break;
2344 case 's': list_script_vars(first); break;
2345 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002346 default:
2347 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002349 }
2350 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002351 {
2352 char_u numbuf[NUMBUFLEN];
2353 char_u *tf;
2354 int c;
2355 char_u *s;
2356
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002357 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002358 c = *arg;
2359 *arg = NUL;
2360 list_one_var_a((char_u *)"",
2361 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002362 tv.v_type,
2363 s == NULL ? (char_u *)"" : s,
2364 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002365 *arg = c;
2366 vim_free(tf);
2367 }
2368 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 }
2371 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002372
2373 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002375
2376 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 }
2378
2379 return arg;
2380}
2381
2382/*
2383 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2384 * Returns a pointer to the char just after the var name.
2385 * Returns NULL if there is an error.
2386 */
2387 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002388ex_let_one(
2389 char_u *arg, /* points to variable name */
2390 typval_T *tv, /* value to assign to variable */
2391 int copy, /* copy value from "tv" */
2392 char_u *endchars, /* valid chars after variable name or NULL */
2393 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394{
2395 int c1;
2396 char_u *name;
2397 char_u *p;
2398 char_u *arg_end = NULL;
2399 int len;
2400 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402
2403 /*
2404 * ":let $VAR = expr": Set environment variable.
2405 */
2406 if (*arg == '$')
2407 {
2408 /* Find the end of the name. */
2409 ++arg;
2410 name = arg;
2411 len = get_env_len(&arg);
2412 if (len == 0)
2413 EMSG2(_(e_invarg2), name - 1);
2414 else
2415 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 if (op != NULL && (*op == '+' || *op == '-'))
2417 EMSG2(_(e_letwrong), op);
2418 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002419 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002421 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 {
2423 c1 = name[len];
2424 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 p = get_tv_string_chk(tv);
2426 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 {
2428 int mustfree = FALSE;
2429 char_u *s = vim_getenv(name, &mustfree);
2430
2431 if (s != NULL)
2432 {
2433 p = tofree = concat_str(s, p);
2434 if (mustfree)
2435 vim_free(s);
2436 }
2437 }
2438 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 if (STRICMP(name, "HOME") == 0)
2442 init_homedir();
2443 else if (didset_vim && STRICMP(name, "VIM") == 0)
2444 didset_vim = FALSE;
2445 else if (didset_vimruntime
2446 && STRICMP(name, "VIMRUNTIME") == 0)
2447 didset_vimruntime = FALSE;
2448 arg_end = arg;
2449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 }
2453 }
2454 }
2455
2456 /*
2457 * ":let &option = expr": Set option value.
2458 * ":let &l:option = expr": Set local option value.
2459 * ":let &g:option = expr": Set global option value.
2460 */
2461 else if (*arg == '&')
2462 {
2463 /* Find the end of the name. */
2464 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002465 if (p == NULL || (endchars != NULL
2466 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 EMSG(_(e_letunexp));
2468 else
2469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 long n;
2471 int opt_type;
2472 long numval;
2473 char_u *stringval = NULL;
2474 char_u *s;
2475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 c1 = *p;
2477 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478
2479 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002480 s = get_tv_string_chk(tv); /* != NULL if number or string */
2481 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 {
2483 opt_type = get_option_value(arg, &numval,
2484 &stringval, opt_flags);
2485 if ((opt_type == 1 && *op == '.')
2486 || (opt_type == 0 && *op != '.'))
2487 EMSG2(_(e_letwrong), op);
2488 else
2489 {
2490 if (opt_type == 1) /* number */
2491 {
2492 if (*op == '+')
2493 n = numval + n;
2494 else
2495 n = numval - n;
2496 }
2497 else if (opt_type == 0 && stringval != NULL) /* string */
2498 {
2499 s = concat_str(stringval, s);
2500 vim_free(stringval);
2501 stringval = s;
2502 }
2503 }
2504 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 if (s != NULL)
2506 {
2507 set_option_value(arg, n, s, opt_flags);
2508 arg_end = p;
2509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 }
2513 }
2514
2515 /*
2516 * ":let @r = expr": Set register contents.
2517 */
2518 else if (*arg == '@')
2519 {
2520 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002521 if (op != NULL && (*op == '+' || *op == '-'))
2522 EMSG2(_(e_letwrong), op);
2523 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002524 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 EMSG(_(e_letunexp));
2526 else
2527 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002528 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 char_u *s;
2530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002531 p = get_tv_string_chk(tv);
2532 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002534 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 if (s != NULL)
2536 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002537 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002538 vim_free(s);
2539 }
2540 }
2541 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002542 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002543 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002544 arg_end = arg + 1;
2545 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002546 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 }
2548 }
2549
2550 /*
2551 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002553 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002558 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2562 EMSG(_(e_letunexp));
2563 else
2564 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 arg_end = p;
2567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 }
2571
2572 else
2573 EMSG2(_(e_invarg2), arg);
2574
2575 return arg_end;
2576}
2577
2578/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2580 */
2581 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002582check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583{
2584 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2585 {
2586 EMSG2(_(e_readonlyvar), arg);
2587 return TRUE;
2588 }
2589 return FALSE;
2590}
2591
2592/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 * Get an lval: variable, Dict item or List item that can be assigned a value
2594 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2595 * "name.key", "name.key[expr]" etc.
2596 * Indexing only works if "name" is an existing List or Dictionary.
2597 * "name" points to the start of the name.
2598 * If "rettv" is not NULL it points to the value to be assigned.
2599 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2600 * wrong; must end in space or cmd separator.
2601 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002602 * flags:
2603 * GLV_QUIET: do not give error messages
2604 * GLV_NO_AUTOLOAD: do not use script autoloading
2605 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002607 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 */
2610 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002611get_lval(
2612 char_u *name,
2613 typval_T *rettv,
2614 lval_T *lp,
2615 int unlet,
2616 int skip,
2617 int flags, /* GLV_ values */
2618 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 char_u *expr_start, *expr_end;
2622 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002623 dictitem_T *v;
2624 typval_T var1;
2625 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002630 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002631 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002634 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635
2636 if (skip)
2637 {
2638 /* When skipping just find the end of the name. */
2639 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002640 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 }
2642
2643 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002644 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (expr_start != NULL)
2646 {
2647 /* Don't expand the name when we already know there is an error. */
2648 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2649 && *p != '[' && *p != '.')
2650 {
2651 EMSG(_(e_trailing));
2652 return NULL;
2653 }
2654
2655 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2656 if (lp->ll_exp_name == NULL)
2657 {
2658 /* Report an invalid expression in braces, unless the
2659 * expression evaluation has been cancelled due to an
2660 * aborting error, an interrupt, or an exception. */
2661 if (!aborting() && !quiet)
2662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002663 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 EMSG2(_(e_invarg2), name);
2665 return NULL;
2666 }
2667 }
2668 lp->ll_name = lp->ll_exp_name;
2669 }
2670 else
2671 lp->ll_name = name;
2672
2673 /* Without [idx] or .key we are done. */
2674 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2675 return p;
2676
2677 cc = *p;
2678 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002679 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (v == NULL && !quiet)
2681 EMSG2(_(e_undefvar), lp->ll_name);
2682 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002683 if (v == NULL)
2684 return NULL;
2685
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 /*
2687 * Loop until no more [idx] or .key is following.
2688 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002689 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2693 && !(lp->ll_tv->v_type == VAR_DICT
2694 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!quiet)
2697 EMSG(_("E689: Can only index a List or Dictionary"));
2698 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!quiet)
2703 EMSG(_("E708: [:] must come last"));
2704 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 len = -1;
2708 if (*p == '.')
2709 {
2710 key = p + 1;
2711 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2712 ;
2713 if (len == 0)
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (!quiet)
2716 EMSG(_(e_emptykey));
2717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 p = key + len;
2720 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002721 else
2722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (*p == ':')
2726 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727 else
2728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 empty1 = FALSE;
2730 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002732 if (get_tv_string_chk(&var1) == NULL)
2733 {
2734 /* not a number or string */
2735 clear_tv(&var1);
2736 return NULL;
2737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 }
2739
2740 /* Optionally get the second index [ :expr]. */
2741 if (*p == ':')
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747 if (!empty1)
2748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (rettv != NULL && (rettv->v_type != VAR_LIST
2752 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (!quiet)
2755 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (!empty1)
2757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760 p = skipwhite(p + 1);
2761 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 else
2764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2767 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (!empty1)
2769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002772 if (get_tv_string_chk(&var2) == NULL)
2773 {
2774 /* not a number or string */
2775 if (!empty1)
2776 clear_tv(&var1);
2777 clear_tv(&var2);
2778 return NULL;
2779 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002785
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (!quiet)
2789 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 if (!empty1)
2791 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 }
2796
2797 /* Skip to past ']'. */
2798 ++p;
2799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 {
2803 if (len == -1)
2804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002806 key = get_tv_string_chk(&var1); /* is number or string */
2807 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 lp->ll_list = NULL;
2814 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002815 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002816
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002817 /* When assigning to a scope dictionary check that a function and
2818 * variable name is valid (only variable name unless it is l: or
2819 * g: dictionary). Disallow overwriting a builtin function. */
2820 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002821 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002822 int prevval;
2823 int wrong;
2824
2825 if (len != -1)
2826 {
2827 prevval = key[len];
2828 key[len] = NUL;
2829 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002830 else
2831 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002832 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2833 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002834 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002835 || !valid_varname(key);
2836 if (len != -1)
2837 key[len] = prevval;
2838 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002839 return NULL;
2840 }
2841
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002844 /* Can't add "v:" variable. */
2845 if (lp->ll_dict == &vimvardict)
2846 {
2847 EMSG2(_(e_illvar), name);
2848 return NULL;
2849 }
2850
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002851 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002855 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 if (len == -1)
2857 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 }
2860 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 if (len == -1)
2865 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 p = NULL;
2868 break;
2869 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002870 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002871 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002872 return NULL;
2873
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 if (len == -1)
2875 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 }
2878 else
2879 {
2880 /*
2881 * Get the number and item for the only or first index of the List.
2882 */
2883 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 else
2886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002887 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 clear_tv(&var1);
2889 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002890 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_list = lp->ll_tv->vval.v_list;
2892 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2893 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002895 if (lp->ll_n1 < 0)
2896 {
2897 lp->ll_n1 = 0;
2898 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2899 }
2900 }
2901 if (lp->ll_li == NULL)
2902 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002904 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002905 if (!quiet)
2906 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002908 }
2909
2910 /*
2911 * May need to find the item or absolute index for the second
2912 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 * When no index given: "lp->ll_empty2" is TRUE.
2914 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002915 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002918 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002919 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002924 {
2925 if (!quiet)
2926 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002930 }
2931
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2933 if (lp->ll_n1 < 0)
2934 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2935 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002936 {
2937 if (!quiet)
2938 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002940 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002941 }
2942
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 }
2946
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 return p;
2948}
2949
2950/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 */
2953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002954clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955{
2956 vim_free(lp->ll_exp_name);
2957 vim_free(lp->ll_newkey);
2958}
2959
2960/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 */
2965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002966set_var_lval(
2967 lval_T *lp,
2968 char_u *endp,
2969 typval_T *rettv,
2970 int copy,
2971 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972{
2973 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002974 listitem_T *ri;
2975 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976
2977 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002980 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 cc = *endp;
2982 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 if (op != NULL && *op != '=')
2984 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002985 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002987 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002988 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002989 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002990 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002991 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002992 if ((di == NULL
2993 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2994 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2995 FALSE)))
2996 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 set_var(lp->ll_name, &tv, FALSE);
2998 clear_tv(&tv);
2999 }
3000 }
3001 else
3002 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003004 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003006 else if (tv_check_lock(lp->ll_newkey == NULL
3007 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003008 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003009 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 else if (lp->ll_range)
3011 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003012 listitem_T *ll_li = lp->ll_li;
3013 int ll_n1 = lp->ll_n1;
3014
3015 /*
3016 * Check whether any of the list items is locked
3017 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003018 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003019 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003020 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003021 return;
3022 ri = ri->li_next;
3023 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3024 break;
3025 ll_li = ll_li->li_next;
3026 ++ll_n1;
3027 }
3028
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 /*
3030 * Assign the List values to the list items.
3031 */
3032 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003033 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 if (op != NULL && *op != '=')
3035 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3036 else
3037 {
3038 clear_tv(&lp->ll_li->li_tv);
3039 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 ri = ri->li_next;
3042 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3043 break;
3044 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003045 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003047 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003048 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 ri = NULL;
3050 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003052 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 lp->ll_li = lp->ll_li->li_next;
3054 ++lp->ll_n1;
3055 }
3056 if (ri != NULL)
3057 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 else if (lp->ll_empty2
3059 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 : lp->ll_n1 != lp->ll_n2)
3061 EMSG(_("E711: List value has not enough items"));
3062 }
3063 else
3064 {
3065 /*
3066 * Assign to a List or Dictionary item.
3067 */
3068 if (lp->ll_newkey != NULL)
3069 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 if (op != NULL && *op != '=')
3071 {
3072 EMSG2(_(e_letwrong), op);
3073 return;
3074 }
3075
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003076 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003077 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003078 if (di == NULL)
3079 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003080 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3081 {
3082 vim_free(di);
3083 return;
3084 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003085 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003086 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 else if (op != NULL && *op != '=')
3088 {
3089 tv_op(lp->ll_tv, rettv, op);
3090 return;
3091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003092 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003093 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003094
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003095 /*
3096 * Assign the value to the variable or list item.
3097 */
3098 if (copy)
3099 copy_tv(rettv, lp->ll_tv);
3100 else
3101 {
3102 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003103 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003104 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003105 }
3106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003107}
3108
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3111 * Returns OK or FAIL.
3112 */
3113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003114tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003115{
3116 long n;
3117 char_u numbuf[NUMBUFLEN];
3118 char_u *s;
3119
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003120 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3121 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3122 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 {
3124 switch (tv1->v_type)
3125 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003126 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 case VAR_DICT:
3128 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003129 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003130 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003131 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003132 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 break;
3134
3135 case VAR_LIST:
3136 if (*op != '+' || tv2->v_type != VAR_LIST)
3137 break;
3138 /* List += List */
3139 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3140 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3141 return OK;
3142
3143 case VAR_NUMBER:
3144 case VAR_STRING:
3145 if (tv2->v_type == VAR_LIST)
3146 break;
3147 if (*op == '+' || *op == '-')
3148 {
3149 /* nr += nr or nr -= nr*/
3150 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151#ifdef FEAT_FLOAT
3152 if (tv2->v_type == VAR_FLOAT)
3153 {
3154 float_T f = n;
3155
3156 if (*op == '+')
3157 f += tv2->vval.v_float;
3158 else
3159 f -= tv2->vval.v_float;
3160 clear_tv(tv1);
3161 tv1->v_type = VAR_FLOAT;
3162 tv1->vval.v_float = f;
3163 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003164 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165#endif
3166 {
3167 if (*op == '+')
3168 n += get_tv_number(tv2);
3169 else
3170 n -= get_tv_number(tv2);
3171 clear_tv(tv1);
3172 tv1->v_type = VAR_NUMBER;
3173 tv1->vval.v_number = n;
3174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003175 }
3176 else
3177 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178 if (tv2->v_type == VAR_FLOAT)
3179 break;
3180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 /* str .= str */
3182 s = get_tv_string(tv1);
3183 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3184 clear_tv(tv1);
3185 tv1->v_type = VAR_STRING;
3186 tv1->vval.v_string = s;
3187 }
3188 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003190 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003191#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192 {
3193 float_T f;
3194
3195 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3196 && tv2->v_type != VAR_NUMBER
3197 && tv2->v_type != VAR_STRING))
3198 break;
3199 if (tv2->v_type == VAR_FLOAT)
3200 f = tv2->vval.v_float;
3201 else
3202 f = get_tv_number(tv2);
3203 if (*op == '+')
3204 tv1->vval.v_float += f;
3205 else
3206 tv1->vval.v_float -= f;
3207 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003208#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003209 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003210 }
3211 }
3212
3213 EMSG2(_(e_letwrong), op);
3214 return FAIL;
3215}
3216
3217/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 * Add a watcher to a list.
3219 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003221list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222{
3223 lw->lw_next = l->lv_watch;
3224 l->lv_watch = lw;
3225}
3226
3227/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003228 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 * No warning when it isn't found...
3230 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 lwp = &l->lv_watch;
3237 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3238 {
3239 if (lw == lwrem)
3240 {
3241 *lwp = lw->lw_next;
3242 break;
3243 }
3244 lwp = &lw->lw_next;
3245 }
3246}
3247
3248/*
3249 * Just before removing an item from a list: advance watchers to the next
3250 * item.
3251 */
3252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003253list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254{
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256
3257 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3258 if (lw->lw_item == item)
3259 lw->lw_item = item->li_next;
3260}
3261
3262/*
3263 * Evaluate the expression used in a ":for var in expr" command.
3264 * "arg" points to "var".
3265 * Set "*errp" to TRUE for an error, FALSE otherwise;
3266 * Return a pointer that holds the info. Null when there is an error.
3267 */
3268 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003269eval_for_line(
3270 char_u *arg,
3271 int *errp,
3272 char_u **nextcmdp,
3273 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274{
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 typval_T tv;
3278 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279
3280 *errp = TRUE; /* default: there is an error */
3281
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 if (fi == NULL)
3284 return NULL;
3285
3286 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3287 if (expr == NULL)
3288 return fi;
3289
3290 expr = skipwhite(expr);
3291 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3292 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003293 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 return fi;
3295 }
3296
3297 if (skip)
3298 ++emsg_skip;
3299 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3300 {
3301 *errp = FALSE;
3302 if (!skip)
3303 {
3304 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003305 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003306 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003308 clear_tv(&tv);
3309 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003310 else if (l == NULL)
3311 {
3312 /* a null list is like an empty list: do nothing */
3313 clear_tv(&tv);
3314 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 else
3316 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003317 /* No need to increment the refcount, it's already set for the
3318 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 fi->fi_list = l;
3320 list_add_watch(l, &fi->fi_lw);
3321 fi->fi_lw.lw_item = l->lv_first;
3322 }
3323 }
3324 }
3325 if (skip)
3326 --emsg_skip;
3327
3328 return fi;
3329}
3330
3331/*
3332 * Use the first item in a ":for" list. Advance to the next.
3333 * Assign the values to the variable (list). "arg" points to the first one.
3334 * Return TRUE when a valid item was found, FALSE when at end of list or
3335 * something wrong.
3336 */
3337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003338next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003340 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003342 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343
3344 item = fi->fi_lw.lw_item;
3345 if (item == NULL)
3346 result = FALSE;
3347 else
3348 {
3349 fi->fi_lw.lw_item = item->li_next;
3350 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3351 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3352 }
3353 return result;
3354}
3355
3356/*
3357 * Free the structure used to store info used by ":for".
3358 */
3359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003360free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361{
Bram Moolenaar33570922005-01-25 22:26:29 +00003362 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003363
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003364 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003365 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003366 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003367 list_unref(fi->fi_list);
3368 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003369 vim_free(fi);
3370}
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3373
3374 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003375set_context_for_expression(
3376 expand_T *xp,
3377 char_u *arg,
3378 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 int got_eq = FALSE;
3381 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003382 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 if (cmdidx == CMD_let)
3385 {
3386 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003387 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003388 {
3389 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003390 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003391 {
3392 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003394 if (vim_iswhite(*p))
3395 break;
3396 }
3397 return;
3398 }
3399 }
3400 else
3401 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3402 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 while ((xp->xp_pattern = vim_strpbrk(arg,
3404 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3405 {
3406 c = *xp->xp_pattern;
3407 if (c == '&')
3408 {
3409 c = xp->xp_pattern[1];
3410 if (c == '&')
3411 {
3412 ++xp->xp_pattern;
3413 xp->xp_context = cmdidx != CMD_let || got_eq
3414 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3415 }
3416 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003419 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3420 xp->xp_pattern += 2;
3421
3422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
3424 else if (c == '$')
3425 {
3426 /* environment variable */
3427 xp->xp_context = EXPAND_ENV_VARS;
3428 }
3429 else if (c == '=')
3430 {
3431 got_eq = TRUE;
3432 xp->xp_context = EXPAND_EXPRESSION;
3433 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003434 else if (c == '#'
3435 && xp->xp_context == EXPAND_EXPRESSION)
3436 {
3437 /* Autoload function/variable contains '#'. */
3438 break;
3439 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003440 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 && xp->xp_context == EXPAND_FUNCTIONS
3442 && vim_strchr(xp->xp_pattern, '(') == NULL)
3443 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003444 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 break;
3446 }
3447 else if (cmdidx != CMD_let || got_eq)
3448 {
3449 if (c == '"') /* string */
3450 {
3451 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3452 if (c == '\\' && xp->xp_pattern[1] != NUL)
3453 ++xp->xp_pattern;
3454 xp->xp_context = EXPAND_NOTHING;
3455 }
3456 else if (c == '\'') /* literal string */
3457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003458 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3460 /* skip */ ;
3461 xp->xp_context = EXPAND_NOTHING;
3462 }
3463 else if (c == '|')
3464 {
3465 if (xp->xp_pattern[1] == '|')
3466 {
3467 ++xp->xp_pattern;
3468 xp->xp_context = EXPAND_EXPRESSION;
3469 }
3470 else
3471 xp->xp_context = EXPAND_COMMANDS;
3472 }
3473 else
3474 xp->xp_context = EXPAND_EXPRESSION;
3475 }
3476 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003477 /* Doesn't look like something valid, expand as an expression
3478 * anyway. */
3479 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 arg = xp->xp_pattern;
3481 if (*arg != NUL)
3482 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3483 /* skip */ ;
3484 }
3485 xp->xp_pattern = arg;
3486}
3487
3488#endif /* FEAT_CMDL_COMPL */
3489
3490/*
3491 * ":1,25call func(arg1, arg2)" function call.
3492 */
3493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 char_u *arg = eap->arg;
3497 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003499 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 linenr_T lnum;
3503 int doesrange;
3504 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003505 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003506 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003508 if (eap->skip)
3509 {
3510 /* trans_function_name() doesn't work well when skipping, use eval0()
3511 * instead to skip to any following command, e.g. for:
3512 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003513 ++emsg_skip;
3514 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3515 clear_tv(&rettv);
3516 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003517 return;
3518 }
3519
Bram Moolenaar65639032016-03-16 21:40:30 +01003520 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003521 if (fudi.fd_newkey != NULL)
3522 {
3523 /* Still need to give an error message for missing key. */
3524 EMSG2(_(e_dictkey), fudi.fd_newkey);
3525 vim_free(fudi.fd_newkey);
3526 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003527 if (tofree == NULL)
3528 return;
3529
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003530 /* Increase refcount on dictionary, it could get deleted when evaluating
3531 * the arguments. */
3532 if (fudi.fd_dict != NULL)
3533 ++fudi.fd_dict->dv_refcount;
3534
Bram Moolenaar65639032016-03-16 21:40:30 +01003535 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3536 * contents. For VAR_PARTIAL get its partial, unless we already have one
3537 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003538 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003539 name = deref_func_name(tofree, &len,
3540 partial != NULL ? NULL : &partial, FALSE);
3541
Bram Moolenaar532c7802005-01-27 14:44:31 +00003542 /* Skip white space to allow ":call func ()". Not good, but required for
3543 * backward compatibility. */
3544 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
3547 if (*startarg != '(')
3548 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003549 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 goto end;
3551 }
3552
3553 /*
3554 * When skipping, evaluate the function once, to find the end of the
3555 * arguments.
3556 * When the function takes a range, this is discovered after the first
3557 * call, and the loop is broken.
3558 */
3559 if (eap->skip)
3560 {
3561 ++emsg_skip;
3562 lnum = eap->line2; /* do it once, also with an invalid range */
3563 }
3564 else
3565 lnum = eap->line1;
3566 for ( ; lnum <= eap->line2; ++lnum)
3567 {
3568 if (!eap->skip && eap->addr_count > 0)
3569 {
3570 curwin->w_cursor.lnum = lnum;
3571 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003572#ifdef FEAT_VIRTUALEDIT
3573 curwin->w_cursor.coladd = 0;
3574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
3576 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003577 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003578 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003579 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 failed = TRUE;
3582 break;
3583 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003584
3585 /* Handle a function returning a Funcref, Dictionary or List. */
3586 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3587 {
3588 failed = TRUE;
3589 break;
3590 }
3591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003592 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 if (doesrange || eap->skip)
3594 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003597 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003598 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003599 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (aborting())
3601 break;
3602 }
3603 if (eap->skip)
3604 --emsg_skip;
3605
3606 if (!failed)
3607 {
3608 /* Check for trailing illegal characters and a following command. */
3609 if (!ends_excmd(*arg))
3610 {
3611 emsg_severe = TRUE;
3612 EMSG(_(e_trailing));
3613 }
3614 else
3615 eap->nextcmd = check_nextcmd(arg);
3616 }
3617
3618end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003619 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003620 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621}
3622
3623/*
3624 * ":unlet[!] var1 ... " command.
3625 */
3626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003627ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003629 ex_unletlock(eap, eap->arg, 0);
3630}
3631
3632/*
3633 * ":lockvar" and ":unlockvar" commands
3634 */
3635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003636ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003639 int deep = 2;
3640
3641 if (eap->forceit)
3642 deep = -1;
3643 else if (vim_isdigit(*arg))
3644 {
3645 deep = getdigits(&arg);
3646 arg = skipwhite(arg);
3647 }
3648
3649 ex_unletlock(eap, arg, deep);
3650}
3651
3652/*
3653 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3654 */
3655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003656ex_unletlock(
3657 exarg_T *eap,
3658 char_u *argstart,
3659 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660{
3661 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 do
3667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003669 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003670 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 if (lv.ll_name == NULL)
3672 error = TRUE; /* error but continue parsing */
3673 if (name_end == NULL || (!vim_iswhite(*name_end)
3674 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 if (name_end != NULL)
3677 {
3678 emsg_severe = TRUE;
3679 EMSG(_(e_trailing));
3680 }
3681 if (!(eap->skip || error))
3682 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 break;
3684 }
3685
3686 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003687 {
3688 if (eap->cmdidx == CMD_unlet)
3689 {
3690 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3691 error = TRUE;
3692 }
3693 else
3694 {
3695 if (do_lock_var(&lv, name_end, deep,
3696 eap->cmdidx == CMD_lockvar) == FAIL)
3697 error = TRUE;
3698 }
3699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 if (!eap->skip)
3702 clear_lval(&lv);
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 arg = skipwhite(name_end);
3705 } while (!ends_excmd(*arg));
3706
3707 eap->nextcmd = check_nextcmd(arg);
3708}
3709
Bram Moolenaar8c711452005-01-14 21:53:12 +00003710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003711do_unlet_var(
3712 lval_T *lp,
3713 char_u *name_end,
3714 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003715{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 int ret = OK;
3717 int cc;
3718
3719 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003720 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 cc = *name_end;
3722 *name_end = NUL;
3723
3724 /* Normal name or expanded name. */
3725 if (check_changedtick(lp->ll_name))
3726 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003728 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003730 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003731 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003732 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003733 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003734 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003735 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003736 else if (lp->ll_range)
3737 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003739 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003740 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003741
3742 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3743 {
3744 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003745 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003746 return FAIL;
3747 ll_li = li;
3748 ++ll_n1;
3749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003750
3751 /* Delete a range of List items. */
3752 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3753 {
3754 li = lp->ll_li->li_next;
3755 listitem_remove(lp->ll_list, lp->ll_li);
3756 lp->ll_li = li;
3757 ++lp->ll_n1;
3758 }
3759 }
3760 else
3761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003762 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003763 /* unlet a List item. */
3764 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003765 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003766 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003767 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003768 }
3769
3770 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003771}
3772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773/*
3774 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 */
3777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003778do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 hashtab_T *ht;
3781 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003783 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003784 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 ht = find_var_ht(name, &varname);
3787 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003789 if (ht == &globvarht)
3790 d = &globvardict;
3791 else if (current_funccal != NULL
3792 && ht == &current_funccal->l_vars.dv_hashtab)
3793 d = &current_funccal->l_vars;
3794 else if (ht == &compat_hashtab)
3795 d = &vimvardict;
3796 else
3797 {
3798 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3799 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3800 }
3801 if (d == NULL)
3802 {
3803 EMSG2(_(e_intern2), "do_unlet()");
3804 return FAIL;
3805 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003806 hi = hash_find(ht, varname);
3807 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003808 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003809 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003810 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003811 || var_check_ro(di->di_flags, name, FALSE)
3812 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003813 return FAIL;
3814
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815 delete_var(ht, hi);
3816 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003819 if (forceit)
3820 return OK;
3821 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 return FAIL;
3823}
3824
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003825/*
3826 * Lock or unlock variable indicated by "lp".
3827 * "deep" is the levels to go (-1 for unlimited);
3828 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3829 */
3830 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003831do_lock_var(
3832 lval_T *lp,
3833 char_u *name_end,
3834 int deep,
3835 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003836{
3837 int ret = OK;
3838 int cc;
3839 dictitem_T *di;
3840
3841 if (deep == 0) /* nothing to do */
3842 return OK;
3843
3844 if (lp->ll_tv == NULL)
3845 {
3846 cc = *name_end;
3847 *name_end = NUL;
3848
3849 /* Normal name or expanded name. */
3850 if (check_changedtick(lp->ll_name))
3851 ret = FAIL;
3852 else
3853 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003854 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003855 if (di == NULL)
3856 ret = FAIL;
3857 else
3858 {
3859 if (lock)
3860 di->di_flags |= DI_FLAGS_LOCK;
3861 else
3862 di->di_flags &= ~DI_FLAGS_LOCK;
3863 item_lock(&di->di_tv, deep, lock);
3864 }
3865 }
3866 *name_end = cc;
3867 }
3868 else if (lp->ll_range)
3869 {
3870 listitem_T *li = lp->ll_li;
3871
3872 /* (un)lock a range of List items. */
3873 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3874 {
3875 item_lock(&li->li_tv, deep, lock);
3876 li = li->li_next;
3877 ++lp->ll_n1;
3878 }
3879 }
3880 else if (lp->ll_list != NULL)
3881 /* (un)lock a List item. */
3882 item_lock(&lp->ll_li->li_tv, deep, lock);
3883 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003884 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003885 item_lock(&lp->ll_di->di_tv, deep, lock);
3886
3887 return ret;
3888}
3889
3890/*
3891 * Lock or unlock an item. "deep" is nr of levels to go.
3892 */
3893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003894item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003895{
3896 static int recurse = 0;
3897 list_T *l;
3898 listitem_T *li;
3899 dict_T *d;
3900 hashitem_T *hi;
3901 int todo;
3902
3903 if (recurse >= DICT_MAXNEST)
3904 {
3905 EMSG(_("E743: variable nested too deep for (un)lock"));
3906 return;
3907 }
3908 if (deep == 0)
3909 return;
3910 ++recurse;
3911
3912 /* lock/unlock the item itself */
3913 if (lock)
3914 tv->v_lock |= VAR_LOCKED;
3915 else
3916 tv->v_lock &= ~VAR_LOCKED;
3917
3918 switch (tv->v_type)
3919 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003920 case VAR_UNKNOWN:
3921 case VAR_NUMBER:
3922 case VAR_STRING:
3923 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003924 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003925 case VAR_FLOAT:
3926 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003927 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003928 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003929 break;
3930
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003931 case VAR_LIST:
3932 if ((l = tv->vval.v_list) != NULL)
3933 {
3934 if (lock)
3935 l->lv_lock |= VAR_LOCKED;
3936 else
3937 l->lv_lock &= ~VAR_LOCKED;
3938 if (deep < 0 || deep > 1)
3939 /* recursive: lock/unlock the items the List contains */
3940 for (li = l->lv_first; li != NULL; li = li->li_next)
3941 item_lock(&li->li_tv, deep - 1, lock);
3942 }
3943 break;
3944 case VAR_DICT:
3945 if ((d = tv->vval.v_dict) != NULL)
3946 {
3947 if (lock)
3948 d->dv_lock |= VAR_LOCKED;
3949 else
3950 d->dv_lock &= ~VAR_LOCKED;
3951 if (deep < 0 || deep > 1)
3952 {
3953 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003954 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003955 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3956 {
3957 if (!HASHITEM_EMPTY(hi))
3958 {
3959 --todo;
3960 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3961 }
3962 }
3963 }
3964 }
3965 }
3966 --recurse;
3967}
3968
Bram Moolenaara40058a2005-07-11 22:42:07 +00003969/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003970 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3971 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003972 */
3973 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003974tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003975{
3976 return (tv->v_lock & VAR_LOCKED)
3977 || (tv->v_type == VAR_LIST
3978 && tv->vval.v_list != NULL
3979 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3980 || (tv->v_type == VAR_DICT
3981 && tv->vval.v_dict != NULL
3982 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3983}
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3986/*
3987 * Delete all "menutrans_" variables.
3988 */
3989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003990del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003993 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
Bram Moolenaar33570922005-01-25 22:26:29 +00003995 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003996 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003998 {
3999 if (!HASHITEM_EMPTY(hi))
4000 {
4001 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4003 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004004 }
4005 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007}
4008#endif
4009
4010#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4011
4012/*
4013 * Local string buffer for the next two functions to store a variable name
4014 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4015 * get_user_var_name().
4016 */
4017
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004018static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
4020static char_u *varnamebuf = NULL;
4021static int varnamebuflen = 0;
4022
4023/*
4024 * Function to concatenate a prefix and a variable name.
4025 */
4026 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004027cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
4029 int len;
4030
4031 len = (int)STRLEN(name) + 3;
4032 if (len > varnamebuflen)
4033 {
4034 vim_free(varnamebuf);
4035 len += 10; /* some additional space */
4036 varnamebuf = alloc(len);
4037 if (varnamebuf == NULL)
4038 {
4039 varnamebuflen = 0;
4040 return NULL;
4041 }
4042 varnamebuflen = len;
4043 }
4044 *varnamebuf = prefix;
4045 varnamebuf[1] = ':';
4046 STRCPY(varnamebuf + 2, name);
4047 return varnamebuf;
4048}
4049
4050/*
4051 * Function given to ExpandGeneric() to obtain the list of user defined
4052 * (global/buffer/window/built-in) variable names.
4053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004055get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004057 static long_u gdone;
4058 static long_u bdone;
4059 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060#ifdef FEAT_WINDOWS
4061 static long_u tdone;
4062#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 static int vidx;
4064 static hashitem_T *hi;
4065 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004068 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004069 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070#ifdef FEAT_WINDOWS
4071 tdone = 0;
4072#endif
4073 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004074
4075 /* Global variables */
4076 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004078 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004080 else
4081 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004082 while (HASHITEM_EMPTY(hi))
4083 ++hi;
4084 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4085 return cat_prefix_varname('g', hi->hi_key);
4086 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004088
4089 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004090 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004091 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004093 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004094 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004095 else
4096 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004097 while (HASHITEM_EMPTY(hi))
4098 ++hi;
4099 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004101 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004103 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 return (char_u *)"b:changedtick";
4105 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004106
4107 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004108 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004109 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004111 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004112 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004113 else
4114 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004115 while (HASHITEM_EMPTY(hi))
4116 ++hi;
4117 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004119
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004120#ifdef FEAT_WINDOWS
4121 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004122 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004123 if (tdone < ht->ht_used)
4124 {
4125 if (tdone++ == 0)
4126 hi = ht->ht_array;
4127 else
4128 ++hi;
4129 while (HASHITEM_EMPTY(hi))
4130 ++hi;
4131 return cat_prefix_varname('t', hi->hi_key);
4132 }
4133#endif
4134
Bram Moolenaar33570922005-01-25 22:26:29 +00004135 /* v: variables */
4136 if (vidx < VV_LEN)
4137 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 vim_free(varnamebuf);
4140 varnamebuf = NULL;
4141 varnamebuflen = 0;
4142 return NULL;
4143}
4144
4145#endif /* FEAT_CMDL_COMPL */
4146
4147/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004148 * Return TRUE if "pat" matches "text".
4149 * Does not use 'cpo' and always uses 'magic'.
4150 */
4151 static int
4152pattern_match(char_u *pat, char_u *text, int ic)
4153{
4154 int matches = FALSE;
4155 char_u *save_cpo;
4156 regmatch_T regmatch;
4157
4158 /* avoid 'l' flag in 'cpoptions' */
4159 save_cpo = p_cpo;
4160 p_cpo = (char_u *)"";
4161 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4162 if (regmatch.regprog != NULL)
4163 {
4164 regmatch.rm_ic = ic;
4165 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4166 vim_regfree(regmatch.regprog);
4167 }
4168 p_cpo = save_cpo;
4169 return matches;
4170}
4171
4172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 * types for expressions.
4174 */
4175typedef enum
4176{
4177 TYPE_UNKNOWN = 0
4178 , TYPE_EQUAL /* == */
4179 , TYPE_NEQUAL /* != */
4180 , TYPE_GREATER /* > */
4181 , TYPE_GEQUAL /* >= */
4182 , TYPE_SMALLER /* < */
4183 , TYPE_SEQUAL /* <= */
4184 , TYPE_MATCH /* =~ */
4185 , TYPE_NOMATCH /* !~ */
4186} exptype_T;
4187
4188/*
4189 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4192 */
4193
4194/*
4195 * Handle zero level expression.
4196 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004198 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 * Return OK or FAIL.
4200 */
4201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004202eval0(
4203 char_u *arg,
4204 typval_T *rettv,
4205 char_u **nextcmd,
4206 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 int ret;
4209 char_u *p;
4210
4211 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if (ret == FAIL || !ends_excmd(*p))
4214 {
4215 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 /*
4218 * Report the invalid expression unless the expression evaluation has
4219 * been cancelled due to an aborting error, an interrupt, or an
4220 * exception.
4221 */
4222 if (!aborting())
4223 EMSG2(_(e_invexpr2), arg);
4224 ret = FAIL;
4225 }
4226 if (nextcmd != NULL)
4227 *nextcmd = check_nextcmd(p);
4228
4229 return ret;
4230}
4231
4232/*
4233 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004234 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 *
4236 * "arg" must point to the first non-white of the expression.
4237 * "arg" is advanced to the next non-white after the recognized expression.
4238 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004239 * Note: "rettv.v_lock" is not set.
4240 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * Return OK or FAIL.
4242 */
4243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004244eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004247 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 /*
4250 * Get the first variable.
4251 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
4254
4255 if ((*arg)[0] == '?')
4256 {
4257 result = FALSE;
4258 if (evaluate)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 int error = FALSE;
4261
4262 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (error)
4266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268
4269 /*
4270 * Get the second variable.
4271 */
4272 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 return FAIL;
4275
4276 /*
4277 * Check for the ":".
4278 */
4279 if ((*arg)[0] != ':')
4280 {
4281 EMSG(_("E109: Missing ':' after '?'"));
4282 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 return FAIL;
4285 }
4286
4287 /*
4288 * Get the third variable.
4289 */
4290 *arg = skipwhite(*arg + 1);
4291 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4292 {
4293 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 return FAIL;
4296 }
4297 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300
4301 return OK;
4302}
4303
4304/*
4305 * Handle first level expression:
4306 * expr2 || expr2 || expr2 logical OR
4307 *
4308 * "arg" must point to the first non-white of the expression.
4309 * "arg" is advanced to the next non-white after the recognized expression.
4310 *
4311 * Return OK or FAIL.
4312 */
4313 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004314eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315{
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 long result;
4318 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
4321 /*
4322 * Get the first variable.
4323 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
4326
4327 /*
4328 * Repeat until there is no following "||".
4329 */
4330 first = TRUE;
4331 result = FALSE;
4332 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4333 {
4334 if (evaluate && first)
4335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004336 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (error)
4340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 first = FALSE;
4342 }
4343
4344 /*
4345 * Get the second variable.
4346 */
4347 *arg = skipwhite(*arg + 2);
4348 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4349 return FAIL;
4350
4351 /*
4352 * Compute the result.
4353 */
4354 if (evaluate && !result)
4355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004356 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004359 if (error)
4360 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 if (evaluate)
4363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 rettv->v_type = VAR_NUMBER;
4365 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 }
4368
4369 return OK;
4370}
4371
4372/*
4373 * Handle second level expression:
4374 * expr3 && expr3 && expr3 logical AND
4375 *
4376 * "arg" must point to the first non-white of the expression.
4377 * "arg" is advanced to the next non-white after the recognized expression.
4378 *
4379 * Return OK or FAIL.
4380 */
4381 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004382eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
Bram Moolenaar33570922005-01-25 22:26:29 +00004384 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 long result;
4386 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 /*
4390 * Get the first variable.
4391 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 return FAIL;
4394
4395 /*
4396 * Repeat until there is no following "&&".
4397 */
4398 first = TRUE;
4399 result = TRUE;
4400 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4401 {
4402 if (evaluate && first)
4403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407 if (error)
4408 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 first = FALSE;
4410 }
4411
4412 /*
4413 * Get the second variable.
4414 */
4415 *arg = skipwhite(*arg + 2);
4416 if (eval4(arg, &var2, evaluate && result) == FAIL)
4417 return FAIL;
4418
4419 /*
4420 * Compute the result.
4421 */
4422 if (evaluate && result)
4423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004424 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004427 if (error)
4428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
4430 if (evaluate)
4431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 rettv->v_type = VAR_NUMBER;
4433 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435 }
4436
4437 return OK;
4438}
4439
4440/*
4441 * Handle third level expression:
4442 * var1 == var2
4443 * var1 =~ var2
4444 * var1 != var2
4445 * var1 !~ var2
4446 * var1 > var2
4447 * var1 >= var2
4448 * var1 < var2
4449 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004450 * var1 is var2
4451 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 *
4453 * "arg" must point to the first non-white of the expression.
4454 * "arg" is advanced to the next non-white after the recognized expression.
4455 *
4456 * Return OK or FAIL.
4457 */
4458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004459eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460{
Bram Moolenaar33570922005-01-25 22:26:29 +00004461 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 char_u *p;
4463 int i;
4464 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 int len = 2;
4467 long n1, n2;
4468 char_u *s1, *s2;
4469 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 /*
4473 * Get the first variable.
4474 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 return FAIL;
4477
4478 p = *arg;
4479 switch (p[0])
4480 {
4481 case '=': if (p[1] == '=')
4482 type = TYPE_EQUAL;
4483 else if (p[1] == '~')
4484 type = TYPE_MATCH;
4485 break;
4486 case '!': if (p[1] == '=')
4487 type = TYPE_NEQUAL;
4488 else if (p[1] == '~')
4489 type = TYPE_NOMATCH;
4490 break;
4491 case '>': if (p[1] != '=')
4492 {
4493 type = TYPE_GREATER;
4494 len = 1;
4495 }
4496 else
4497 type = TYPE_GEQUAL;
4498 break;
4499 case '<': if (p[1] != '=')
4500 {
4501 type = TYPE_SMALLER;
4502 len = 1;
4503 }
4504 else
4505 type = TYPE_SEQUAL;
4506 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004507 case 'i': if (p[1] == 's')
4508 {
4509 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4510 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004511 i = p[len];
4512 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 {
4514 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4515 type_is = TRUE;
4516 }
4517 }
4518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520
4521 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004522 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 */
4524 if (type != TYPE_UNKNOWN)
4525 {
4526 /* extra question mark appended: ignore case */
4527 if (p[len] == '?')
4528 {
4529 ic = TRUE;
4530 ++len;
4531 }
4532 /* extra '#' appended: match case */
4533 else if (p[len] == '#')
4534 {
4535 ic = FALSE;
4536 ++len;
4537 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004538 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 else
4540 ic = p_ic;
4541
4542 /*
4543 * Get the second variable.
4544 */
4545 *arg = skipwhite(p + len);
4546 if (eval5(arg, &var2, evaluate) == FAIL)
4547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004548 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 return FAIL;
4550 }
4551
4552 if (evaluate)
4553 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004554 if (type_is && rettv->v_type != var2.v_type)
4555 {
4556 /* For "is" a different type always means FALSE, for "notis"
4557 * it means TRUE. */
4558 n1 = (type == TYPE_NEQUAL);
4559 }
4560 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4561 {
4562 if (type_is)
4563 {
4564 n1 = (rettv->v_type == var2.v_type
4565 && rettv->vval.v_list == var2.vval.v_list);
4566 if (type == TYPE_NEQUAL)
4567 n1 = !n1;
4568 }
4569 else if (rettv->v_type != var2.v_type
4570 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4571 {
4572 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004573 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004574 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004575 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004576 clear_tv(rettv);
4577 clear_tv(&var2);
4578 return FAIL;
4579 }
4580 else
4581 {
4582 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004583 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4584 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004585 if (type == TYPE_NEQUAL)
4586 n1 = !n1;
4587 }
4588 }
4589
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004590 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4591 {
4592 if (type_is)
4593 {
4594 n1 = (rettv->v_type == var2.v_type
4595 && rettv->vval.v_dict == var2.vval.v_dict);
4596 if (type == TYPE_NEQUAL)
4597 n1 = !n1;
4598 }
4599 else if (rettv->v_type != var2.v_type
4600 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4601 {
4602 if (rettv->v_type != var2.v_type)
4603 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4604 else
4605 EMSG(_("E736: Invalid operation for Dictionary"));
4606 clear_tv(rettv);
4607 clear_tv(&var2);
4608 return FAIL;
4609 }
4610 else
4611 {
4612 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004613 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4614 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004615 if (type == TYPE_NEQUAL)
4616 n1 = !n1;
4617 }
4618 }
4619
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004620 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4621 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004622 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004623 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004624 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004625 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004626 clear_tv(rettv);
4627 clear_tv(&var2);
4628 return FAIL;
4629 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004630 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004631 if (type == TYPE_NEQUAL)
4632 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004633 }
4634
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004635#ifdef FEAT_FLOAT
4636 /*
4637 * If one of the two variables is a float, compare as a float.
4638 * When using "=~" or "!~", always compare as string.
4639 */
4640 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4641 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4642 {
4643 float_T f1, f2;
4644
4645 if (rettv->v_type == VAR_FLOAT)
4646 f1 = rettv->vval.v_float;
4647 else
4648 f1 = get_tv_number(rettv);
4649 if (var2.v_type == VAR_FLOAT)
4650 f2 = var2.vval.v_float;
4651 else
4652 f2 = get_tv_number(&var2);
4653 n1 = FALSE;
4654 switch (type)
4655 {
4656 case TYPE_EQUAL: n1 = (f1 == f2); break;
4657 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4658 case TYPE_GREATER: n1 = (f1 > f2); break;
4659 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4660 case TYPE_SMALLER: n1 = (f1 < f2); break;
4661 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4662 case TYPE_UNKNOWN:
4663 case TYPE_MATCH:
4664 case TYPE_NOMATCH: break; /* avoid gcc warning */
4665 }
4666 }
4667#endif
4668
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 /*
4670 * If one of the two variables is a number, compare as a number.
4671 * When using "=~" or "!~", always compare as string.
4672 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004673 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 n1 = get_tv_number(rettv);
4677 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 switch (type)
4679 {
4680 case TYPE_EQUAL: n1 = (n1 == n2); break;
4681 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4682 case TYPE_GREATER: n1 = (n1 > n2); break;
4683 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4684 case TYPE_SMALLER: n1 = (n1 < n2); break;
4685 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4686 case TYPE_UNKNOWN:
4687 case TYPE_MATCH:
4688 case TYPE_NOMATCH: break; /* avoid gcc warning */
4689 }
4690 }
4691 else
4692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 s1 = get_tv_string_buf(rettv, buf1);
4694 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4696 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4697 else
4698 i = 0;
4699 n1 = FALSE;
4700 switch (type)
4701 {
4702 case TYPE_EQUAL: n1 = (i == 0); break;
4703 case TYPE_NEQUAL: n1 = (i != 0); break;
4704 case TYPE_GREATER: n1 = (i > 0); break;
4705 case TYPE_GEQUAL: n1 = (i >= 0); break;
4706 case TYPE_SMALLER: n1 = (i < 0); break;
4707 case TYPE_SEQUAL: n1 = (i <= 0); break;
4708
4709 case TYPE_MATCH:
4710 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004711 n1 = pattern_match(s2, s1, ic);
4712 if (type == TYPE_NOMATCH)
4713 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 break;
4715
4716 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4717 }
4718 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 clear_tv(rettv);
4720 clear_tv(&var2);
4721 rettv->v_type = VAR_NUMBER;
4722 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 }
4724 }
4725
4726 return OK;
4727}
4728
4729/*
4730 * Handle fourth level expression:
4731 * + number addition
4732 * - number subtraction
4733 * . string concatenation
4734 *
4735 * "arg" must point to the first non-white of the expression.
4736 * "arg" is advanced to the next non-white after the recognized expression.
4737 *
4738 * Return OK or FAIL.
4739 */
4740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004741eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
Bram Moolenaar33570922005-01-25 22:26:29 +00004743 typval_T var2;
4744 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 int op;
4746 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004747#ifdef FEAT_FLOAT
4748 float_T f1 = 0, f2 = 0;
4749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 char_u *s1, *s2;
4751 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4752 char_u *p;
4753
4754 /*
4755 * Get the first variable.
4756 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004757 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 return FAIL;
4759
4760 /*
4761 * Repeat computing, until no '+', '-' or '.' is following.
4762 */
4763 for (;;)
4764 {
4765 op = **arg;
4766 if (op != '+' && op != '-' && op != '.')
4767 break;
4768
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004769 if ((op != '+' || rettv->v_type != VAR_LIST)
4770#ifdef FEAT_FLOAT
4771 && (op == '.' || rettv->v_type != VAR_FLOAT)
4772#endif
4773 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004774 {
4775 /* For "list + ...", an illegal use of the first operand as
4776 * a number cannot be determined before evaluating the 2nd
4777 * operand: if this is also a list, all is ok.
4778 * For "something . ...", "something - ..." or "non-list + ...",
4779 * we know that the first operand needs to be a string or number
4780 * without evaluating the 2nd operand. So check before to avoid
4781 * side effects after an error. */
4782 if (evaluate && get_tv_string_chk(rettv) == NULL)
4783 {
4784 clear_tv(rettv);
4785 return FAIL;
4786 }
4787 }
4788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 /*
4790 * Get the second variable.
4791 */
4792 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004793 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004795 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 return FAIL;
4797 }
4798
4799 if (evaluate)
4800 {
4801 /*
4802 * Compute the result.
4803 */
4804 if (op == '.')
4805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004806 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4807 s2 = get_tv_string_buf_chk(&var2, buf2);
4808 if (s2 == NULL) /* type error ? */
4809 {
4810 clear_tv(rettv);
4811 clear_tv(&var2);
4812 return FAIL;
4813 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004814 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 clear_tv(rettv);
4816 rettv->v_type = VAR_STRING;
4817 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004819 else if (op == '+' && rettv->v_type == VAR_LIST
4820 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004821 {
4822 /* concatenate Lists */
4823 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4824 &var3) == FAIL)
4825 {
4826 clear_tv(rettv);
4827 clear_tv(&var2);
4828 return FAIL;
4829 }
4830 clear_tv(rettv);
4831 *rettv = var3;
4832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 else
4834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 int error = FALSE;
4836
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#ifdef FEAT_FLOAT
4838 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840 f1 = rettv->vval.v_float;
4841 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004843 else
4844#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846 n1 = get_tv_number_chk(rettv, &error);
4847 if (error)
4848 {
4849 /* This can only happen for "list + non-list". For
4850 * "non-list + ..." or "something - ...", we returned
4851 * before evaluating the 2nd operand. */
4852 clear_tv(rettv);
4853 return FAIL;
4854 }
4855#ifdef FEAT_FLOAT
4856 if (var2.v_type == VAR_FLOAT)
4857 f1 = n1;
4858#endif
4859 }
4860#ifdef FEAT_FLOAT
4861 if (var2.v_type == VAR_FLOAT)
4862 {
4863 f2 = var2.vval.v_float;
4864 n2 = 0;
4865 }
4866 else
4867#endif
4868 {
4869 n2 = get_tv_number_chk(&var2, &error);
4870 if (error)
4871 {
4872 clear_tv(rettv);
4873 clear_tv(&var2);
4874 return FAIL;
4875 }
4876#ifdef FEAT_FLOAT
4877 if (rettv->v_type == VAR_FLOAT)
4878 f2 = n2;
4879#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882
4883#ifdef FEAT_FLOAT
4884 /* If there is a float on either side the result is a float. */
4885 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4886 {
4887 if (op == '+')
4888 f1 = f1 + f2;
4889 else
4890 f1 = f1 - f2;
4891 rettv->v_type = VAR_FLOAT;
4892 rettv->vval.v_float = f1;
4893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895#endif
4896 {
4897 if (op == '+')
4898 n1 = n1 + n2;
4899 else
4900 n1 = n1 - n2;
4901 rettv->v_type = VAR_NUMBER;
4902 rettv->vval.v_number = n1;
4903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004905 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 }
4908 return OK;
4909}
4910
4911/*
4912 * Handle fifth level expression:
4913 * * number multiplication
4914 * / number division
4915 * % number modulo
4916 *
4917 * "arg" must point to the first non-white of the expression.
4918 * "arg" is advanced to the next non-white after the recognized expression.
4919 *
4920 * Return OK or FAIL.
4921 */
4922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004923eval6(
4924 char_u **arg,
4925 typval_T *rettv,
4926 int evaluate,
4927 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
Bram Moolenaar33570922005-01-25 22:26:29 +00004929 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 int op;
4931 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932#ifdef FEAT_FLOAT
4933 int use_float = FALSE;
4934 float_T f1 = 0, f2;
4935#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004936 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 /*
4939 * Get the first variable.
4940 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004941 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 return FAIL;
4943
4944 /*
4945 * Repeat computing, until no '*', '/' or '%' is following.
4946 */
4947 for (;;)
4948 {
4949 op = **arg;
4950 if (op != '*' && op != '/' && op != '%')
4951 break;
4952
4953 if (evaluate)
4954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (rettv->v_type == VAR_FLOAT)
4957 {
4958 f1 = rettv->vval.v_float;
4959 use_float = TRUE;
4960 n1 = 0;
4961 }
4962 else
4963#endif
4964 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004965 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004966 if (error)
4967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 }
4969 else
4970 n1 = 0;
4971
4972 /*
4973 * Get the second variable.
4974 */
4975 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004976 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 return FAIL;
4978
4979 if (evaluate)
4980 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981#ifdef FEAT_FLOAT
4982 if (var2.v_type == VAR_FLOAT)
4983 {
4984 if (!use_float)
4985 {
4986 f1 = n1;
4987 use_float = TRUE;
4988 }
4989 f2 = var2.vval.v_float;
4990 n2 = 0;
4991 }
4992 else
4993#endif
4994 {
4995 n2 = get_tv_number_chk(&var2, &error);
4996 clear_tv(&var2);
4997 if (error)
4998 return FAIL;
4999#ifdef FEAT_FLOAT
5000 if (use_float)
5001 f2 = n2;
5002#endif
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
5005 /*
5006 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005007 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009#ifdef FEAT_FLOAT
5010 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 if (op == '*')
5013 f1 = f1 * f2;
5014 else if (op == '/')
5015 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005016# ifdef VMS
5017 /* VMS crashes on divide by zero, work around it */
5018 if (f2 == 0.0)
5019 {
5020 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005021 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005022 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005023 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005024 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005025 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005026 }
5027 else
5028 f1 = f1 / f2;
5029# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005030 /* We rely on the floating point library to handle divide
5031 * by zero to result in "inf" and not a crash. */
5032 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005033# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005036 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005037 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005038 return FAIL;
5039 }
5040 rettv->v_type = VAR_FLOAT;
5041 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
5043 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005046 if (op == '*')
5047 n1 = n1 * n2;
5048 else if (op == '/')
5049 {
5050 if (n2 == 0) /* give an error message? */
5051 {
5052 if (n1 == 0)
5053 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5054 else if (n1 < 0)
5055 n1 = -0x7fffffffL;
5056 else
5057 n1 = 0x7fffffffL;
5058 }
5059 else
5060 n1 = n1 / n2;
5061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005063 {
5064 if (n2 == 0) /* give an error message? */
5065 n1 = 0;
5066 else
5067 n1 = n1 % n2;
5068 }
5069 rettv->v_type = VAR_NUMBER;
5070 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 }
5074
5075 return OK;
5076}
5077
5078/*
5079 * Handle sixth level expression:
5080 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005081 * "string" string constant
5082 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 * &option-name option value
5084 * @r register contents
5085 * identifier variable value
5086 * function() function call
5087 * $VAR environment variable
5088 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005089 * [expr, expr] List
5090 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 *
5092 * Also handle:
5093 * ! in front logical NOT
5094 * - in front unary minus
5095 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 * trailing [] subscript in String or List
5097 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 *
5099 * "arg" must point to the first non-white of the expression.
5100 * "arg" is advanced to the next non-white after the recognized expression.
5101 *
5102 * Return OK or FAIL.
5103 */
5104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005105eval7(
5106 char_u **arg,
5107 typval_T *rettv,
5108 int evaluate,
5109 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 long n;
5112 int len;
5113 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 char_u *start_leader, *end_leader;
5115 int ret = OK;
5116 char_u *alias;
5117
5118 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005119 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124 /*
5125 * Skip '!' and '-' characters. They are handled later.
5126 */
5127 start_leader = *arg;
5128 while (**arg == '!' || **arg == '-' || **arg == '+')
5129 *arg = skipwhite(*arg + 1);
5130 end_leader = *arg;
5131
5132 switch (**arg)
5133 {
5134 /*
5135 * Number constant.
5136 */
5137 case '0':
5138 case '1':
5139 case '2':
5140 case '3':
5141 case '4':
5142 case '5':
5143 case '6':
5144 case '7':
5145 case '8':
5146 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 {
5148#ifdef FEAT_FLOAT
5149 char_u *p = skipdigits(*arg + 1);
5150 int get_float = FALSE;
5151
5152 /* We accept a float when the format matches
5153 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005154 * strict to avoid backwards compatibility problems.
5155 * Don't look for a float after the "." operator, so that
5156 * ":let vers = 1.2.3" doesn't fail. */
5157 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005159 get_float = TRUE;
5160 p = skipdigits(p + 2);
5161 if (*p == 'e' || *p == 'E')
5162 {
5163 ++p;
5164 if (*p == '-' || *p == '+')
5165 ++p;
5166 if (!vim_isdigit(*p))
5167 get_float = FALSE;
5168 else
5169 p = skipdigits(p + 1);
5170 }
5171 if (ASCII_ISALPHA(*p) || *p == '.')
5172 get_float = FALSE;
5173 }
5174 if (get_float)
5175 {
5176 float_T f;
5177
5178 *arg += string2float(*arg, &f);
5179 if (evaluate)
5180 {
5181 rettv->v_type = VAR_FLOAT;
5182 rettv->vval.v_float = f;
5183 }
5184 }
5185 else
5186#endif
5187 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005188 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005189 *arg += len;
5190 if (evaluate)
5191 {
5192 rettv->v_type = VAR_NUMBER;
5193 rettv->vval.v_number = n;
5194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199 /*
5200 * String constant: "string".
5201 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 break;
5204
5205 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209 break;
5210
5211 /*
5212 * List: [expr, expr]
5213 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216
5217 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005218 * Dictionary: {key: val, key: val}
5219 */
5220 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5221 break;
5222
5223 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005224 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005226 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 break;
5228
5229 /*
5230 * Environment variable: $VAR.
5231 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005232 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 break;
5234
5235 /*
5236 * Register contents: @r.
5237 */
5238 case '@': ++*arg;
5239 if (evaluate)
5240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005241 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005242 rettv->vval.v_string = get_reg_contents(**arg,
5243 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245 if (**arg != NUL)
5246 ++*arg;
5247 break;
5248
5249 /*
5250 * nested expression: (expression).
5251 */
5252 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005253 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 if (**arg == ')')
5255 ++*arg;
5256 else if (ret == OK)
5257 {
5258 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005259 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 ret = FAIL;
5261 }
5262 break;
5263
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 break;
5266 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267
5268 if (ret == NOTDONE)
5269 {
5270 /*
5271 * Must be a variable or function name.
5272 * Can also be a curly-braces kind of name: {expr}.
5273 */
5274 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005275 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 if (alias != NULL)
5277 s = alias;
5278
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005279 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 ret = FAIL;
5281 else
5282 {
5283 if (**arg == '(') /* recursive! */
5284 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005285 partial_T *partial;
5286
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 /* If "s" is the name of a variable of type VAR_FUNC
5288 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005289 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290
5291 /* Invoke the function. */
5292 ret = get_func_tv(s, len, rettv, arg,
5293 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005294 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005295
5296 /* If evaluate is FALSE rettv->v_type was not set in
5297 * get_func_tv, but it's needed in handle_subscript() to parse
5298 * what follows. So set it here. */
5299 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5300 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005301 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005302 rettv->v_type = VAR_FUNC;
5303 }
5304
Bram Moolenaar8c711452005-01-14 21:53:12 +00005305 /* Stop the expression evaluation when immediately
5306 * aborting on error, or when an interrupt occurred or
5307 * an exception was thrown but not caught. */
5308 if (aborting())
5309 {
5310 if (ret == OK)
5311 clear_tv(rettv);
5312 ret = FAIL;
5313 }
5314 }
5315 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005316 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005317 else
5318 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005320 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005321 }
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 *arg = skipwhite(*arg);
5324
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005325 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5326 * expr(expr). */
5327 if (ret == OK)
5328 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 /*
5331 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5332 */
5333 if (ret == OK && evaluate && end_leader > start_leader)
5334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005336 int val = 0;
5337#ifdef FEAT_FLOAT
5338 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005339
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005340 if (rettv->v_type == VAR_FLOAT)
5341 f = rettv->vval.v_float;
5342 else
5343#endif
5344 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005345 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005347 clear_tv(rettv);
5348 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005350 else
5351 {
5352 while (end_leader > start_leader)
5353 {
5354 --end_leader;
5355 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005356 {
5357#ifdef FEAT_FLOAT
5358 if (rettv->v_type == VAR_FLOAT)
5359 f = !f;
5360 else
5361#endif
5362 val = !val;
5363 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005364 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005365 {
5366#ifdef FEAT_FLOAT
5367 if (rettv->v_type == VAR_FLOAT)
5368 f = -f;
5369 else
5370#endif
5371 val = -val;
5372 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005373 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005374#ifdef FEAT_FLOAT
5375 if (rettv->v_type == VAR_FLOAT)
5376 {
5377 clear_tv(rettv);
5378 rettv->vval.v_float = f;
5379 }
5380 else
5381#endif
5382 {
5383 clear_tv(rettv);
5384 rettv->v_type = VAR_NUMBER;
5385 rettv->vval.v_number = val;
5386 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389
5390 return ret;
5391}
5392
5393/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005394 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5395 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5397 */
5398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005399eval_index(
5400 char_u **arg,
5401 typval_T *rettv,
5402 int evaluate,
5403 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404{
5405 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005406 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005408 long len = -1;
5409 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005411 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005412
Bram Moolenaara03f2332016-02-06 18:09:59 +01005413 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005415 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005416 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005417 if (verbose)
5418 EMSG(_("E695: Cannot index a Funcref"));
5419 return FAIL;
5420 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005421#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005422 if (verbose)
5423 EMSG(_(e_float_as_string));
5424 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005425#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005426 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005427 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005428 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005429 if (verbose)
5430 EMSG(_("E909: Cannot index a special variable"));
5431 return FAIL;
5432 case VAR_UNKNOWN:
5433 if (evaluate)
5434 return FAIL;
5435 /* FALLTHROUGH */
5436
5437 case VAR_STRING:
5438 case VAR_NUMBER:
5439 case VAR_LIST:
5440 case VAR_DICT:
5441 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005442 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005443
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005444 init_tv(&var1);
5445 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005448 /*
5449 * dict.name
5450 */
5451 key = *arg + 1;
5452 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5453 ;
5454 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460 /*
5461 * something[idx]
5462 *
5463 * Get the (first) variable from inside the [].
5464 */
5465 *arg = skipwhite(*arg + 1);
5466 if (**arg == ':')
5467 empty1 = TRUE;
5468 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5469 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005470 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5471 {
5472 /* not a number or string */
5473 clear_tv(&var1);
5474 return FAIL;
5475 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476
5477 /*
5478 * Get the second variable from inside the [:].
5479 */
5480 if (**arg == ':')
5481 {
5482 range = TRUE;
5483 *arg = skipwhite(*arg + 1);
5484 if (**arg == ']')
5485 empty2 = TRUE;
5486 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5487 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005488 if (!empty1)
5489 clear_tv(&var1);
5490 return FAIL;
5491 }
5492 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5493 {
5494 /* not a number or string */
5495 if (!empty1)
5496 clear_tv(&var1);
5497 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 return FAIL;
5499 }
5500 }
5501
5502 /* Check for the ']'. */
5503 if (**arg != ']')
5504 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005505 if (verbose)
5506 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507 clear_tv(&var1);
5508 if (range)
5509 clear_tv(&var2);
5510 return FAIL;
5511 }
5512 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 }
5514
5515 if (evaluate)
5516 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517 n1 = 0;
5518 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520 n1 = get_tv_number(&var1);
5521 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 }
5523 if (range)
5524 {
5525 if (empty2)
5526 n2 = -1;
5527 else
5528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 n2 = get_tv_number(&var2);
5530 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 }
5532 }
5533
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005534 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005536 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005537 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005538 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005539 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005540 case VAR_SPECIAL:
5541 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005542 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005543 break; /* not evaluating, skipping over subscript */
5544
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 case VAR_NUMBER:
5546 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005547 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548 len = (long)STRLEN(s);
5549 if (range)
5550 {
5551 /* The resulting variable is a substring. If the indexes
5552 * are out of range the result is empty. */
5553 if (n1 < 0)
5554 {
5555 n1 = len + n1;
5556 if (n1 < 0)
5557 n1 = 0;
5558 }
5559 if (n2 < 0)
5560 n2 = len + n2;
5561 else if (n2 >= len)
5562 n2 = len;
5563 if (n1 >= len || n2 < 0 || n1 > n2)
5564 s = NULL;
5565 else
5566 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5567 }
5568 else
5569 {
5570 /* The resulting variable is a string of a single
5571 * character. If the index is too big or negative the
5572 * result is empty. */
5573 if (n1 >= len || n1 < 0)
5574 s = NULL;
5575 else
5576 s = vim_strnsave(s + n1, 1);
5577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578 clear_tv(rettv);
5579 rettv->v_type = VAR_STRING;
5580 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 break;
5582
5583 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 if (n1 < 0)
5586 n1 = len + n1;
5587 if (!empty1 && (n1 < 0 || n1 >= len))
5588 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005589 /* For a range we allow invalid values and return an empty
5590 * list. A list index out of range is an error. */
5591 if (!range)
5592 {
5593 if (verbose)
5594 EMSGN(_(e_listidx), n1);
5595 return FAIL;
5596 }
5597 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 }
5599 if (range)
5600 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005601 list_T *l;
5602 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603
5604 if (n2 < 0)
5605 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005606 else if (n2 >= len)
5607 n2 = len - 1;
5608 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005609 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 l = list_alloc();
5611 if (l == NULL)
5612 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005614 n1 <= n2; ++n1)
5615 {
5616 if (list_append_tv(l, &item->li_tv) == FAIL)
5617 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005618 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 return FAIL;
5620 }
5621 item = item->li_next;
5622 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 clear_tv(rettv);
5624 rettv->v_type = VAR_LIST;
5625 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005626 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 }
5628 else
5629 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005630 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005631 clear_tv(rettv);
5632 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 }
5634 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635
5636 case VAR_DICT:
5637 if (range)
5638 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005639 if (verbose)
5640 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 if (len == -1)
5642 clear_tv(&var1);
5643 return FAIL;
5644 }
5645 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005646 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647
5648 if (len == -1)
5649 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005650 key = get_tv_string_chk(&var1);
5651 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 clear_tv(&var1);
5654 return FAIL;
5655 }
5656 }
5657
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005658 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005660 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005661 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005662 if (len == -1)
5663 clear_tv(&var1);
5664 if (item == NULL)
5665 return FAIL;
5666
5667 copy_tv(&item->di_tv, &var1);
5668 clear_tv(rettv);
5669 *rettv = var1;
5670 }
5671 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005672 }
5673 }
5674
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 return OK;
5676}
5677
5678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 * Get an option value.
5680 * "arg" points to the '&' or '+' before the option name.
5681 * "arg" is advanced to character after the option name.
5682 * Return OK or FAIL.
5683 */
5684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005685get_option_tv(
5686 char_u **arg,
5687 typval_T *rettv, /* when NULL, only check if option exists */
5688 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 char_u *option_end;
5691 long numval;
5692 char_u *stringval;
5693 int opt_type;
5694 int c;
5695 int working = (**arg == '+'); /* has("+option") */
5696 int ret = OK;
5697 int opt_flags;
5698
5699 /*
5700 * Isolate the option name and find its value.
5701 */
5702 option_end = find_option_end(arg, &opt_flags);
5703 if (option_end == NULL)
5704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 EMSG2(_("E112: Option name missing: %s"), *arg);
5707 return FAIL;
5708 }
5709
5710 if (!evaluate)
5711 {
5712 *arg = option_end;
5713 return OK;
5714 }
5715
5716 c = *option_end;
5717 *option_end = NUL;
5718 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005719 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
5721 if (opt_type == -3) /* invalid name */
5722 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005723 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 EMSG2(_("E113: Unknown option: %s"), *arg);
5725 ret = FAIL;
5726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005727 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
5729 if (opt_type == -2) /* hidden string option */
5730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005731 rettv->v_type = VAR_STRING;
5732 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734 else if (opt_type == -1) /* hidden number option */
5735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005736 rettv->v_type = VAR_NUMBER;
5737 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739 else if (opt_type == 1) /* number option */
5740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005741 rettv->v_type = VAR_NUMBER;
5742 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 }
5744 else /* string option */
5745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005746 rettv->v_type = VAR_STRING;
5747 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749 }
5750 else if (working && (opt_type == -2 || opt_type == -1))
5751 ret = FAIL;
5752
5753 *option_end = c; /* put back for error messages */
5754 *arg = option_end;
5755
5756 return ret;
5757}
5758
5759/*
5760 * Allocate a variable for a string constant.
5761 * Return OK or FAIL.
5762 */
5763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005764get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766 char_u *p;
5767 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 int extra = 0;
5769
5770 /*
5771 * Find the end of the string, skipping backslashed characters.
5772 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 {
5775 if (*p == '\\' && p[1] != NUL)
5776 {
5777 ++p;
5778 /* A "\<x>" form occupies at least 4 characters, and produces up
5779 * to 6 characters: reserve space for 2 extra */
5780 if (*p == '<')
5781 extra += 2;
5782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
5784
5785 if (*p != '"')
5786 {
5787 EMSG2(_("E114: Missing quote: %s"), *arg);
5788 return FAIL;
5789 }
5790
5791 /* If only parsing, set *arg and return here */
5792 if (!evaluate)
5793 {
5794 *arg = p + 1;
5795 return OK;
5796 }
5797
5798 /*
5799 * Copy the string into allocated memory, handling backslashed
5800 * characters.
5801 */
5802 name = alloc((unsigned)(p - *arg + extra));
5803 if (name == NULL)
5804 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 rettv->v_type = VAR_STRING;
5806 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {
5810 if (*p == '\\')
5811 {
5812 switch (*++p)
5813 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 case 'b': *name++ = BS; ++p; break;
5815 case 'e': *name++ = ESC; ++p; break;
5816 case 'f': *name++ = FF; ++p; break;
5817 case 'n': *name++ = NL; ++p; break;
5818 case 'r': *name++ = CAR; ++p; break;
5819 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
5821 case 'X': /* hex: "\x1", "\x12" */
5822 case 'x':
5823 case 'u': /* Unicode: "\u0023" */
5824 case 'U':
5825 if (vim_isxdigit(p[1]))
5826 {
5827 int n, nr;
5828 int c = toupper(*p);
5829
5830 if (c == 'X')
5831 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005832 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005834 else
5835 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 nr = 0;
5837 while (--n >= 0 && vim_isxdigit(p[1]))
5838 {
5839 ++p;
5840 nr = (nr << 4) + hex2nr(*p);
5841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843#ifdef FEAT_MBYTE
5844 /* For "\u" store the number according to
5845 * 'encoding'. */
5846 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 else
5849#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 break;
5853
5854 /* octal: "\1", "\12", "\123" */
5855 case '0':
5856 case '1':
5857 case '2':
5858 case '3':
5859 case '4':
5860 case '5':
5861 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 case '7': *name = *p++ - '0';
5863 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 *name = (*name << 3) + *p++ - '0';
5866 if (*p >= '0' && *p <= '7')
5867 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 break;
5871
5872 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 if (extra != 0)
5875 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 break;
5878 }
5879 /* FALLTHROUGH */
5880
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 break;
5883 }
5884 }
5885 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 *arg = p + 1;
5891
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 return OK;
5893}
5894
5895/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 * Return OK or FAIL.
5898 */
5899 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005900get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901{
5902 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 int reduce = 0;
5905
5906 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 */
5909 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5910 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914 break;
5915 ++reduce;
5916 ++p;
5917 }
5918 }
5919
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 return FAIL;
5924 }
5925
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 if (!evaluate)
5928 {
5929 *arg = p + 1;
5930 return OK;
5931 }
5932
5933 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005935 */
5936 str = alloc((unsigned)((p - *arg) - reduce));
5937 if (str == NULL)
5938 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 rettv->v_type = VAR_STRING;
5940 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005941
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005943 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005944 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005947 break;
5948 ++p;
5949 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005950 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005951 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005952 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005953 *arg = p + 1;
5954
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005955 return OK;
5956}
5957
Bram Moolenaarddecc252016-04-06 22:59:37 +02005958 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005959partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005960{
5961 int i;
5962
5963 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005964 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005965 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005966 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005967 func_unref(pt->pt_name);
5968 vim_free(pt->pt_name);
5969 vim_free(pt);
5970}
5971
5972/*
5973 * Unreference a closure: decrement the reference count and free it when it
5974 * becomes zero.
5975 */
5976 void
5977partial_unref(partial_T *pt)
5978{
5979 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005980 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005981}
5982
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005983/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 * Allocate a variable for a List and fill it from "*arg".
5985 * Return OK or FAIL.
5986 */
5987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005988get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989{
Bram Moolenaar33570922005-01-25 22:26:29 +00005990 list_T *l = NULL;
5991 typval_T tv;
5992 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993
5994 if (evaluate)
5995 {
5996 l = list_alloc();
5997 if (l == NULL)
5998 return FAIL;
5999 }
6000
6001 *arg = skipwhite(*arg + 1);
6002 while (**arg != ']' && **arg != NUL)
6003 {
6004 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6005 goto failret;
6006 if (evaluate)
6007 {
6008 item = listitem_alloc();
6009 if (item != NULL)
6010 {
6011 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006012 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 list_append(l, item);
6014 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006015 else
6016 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017 }
6018
6019 if (**arg == ']')
6020 break;
6021 if (**arg != ',')
6022 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006023 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 goto failret;
6025 }
6026 *arg = skipwhite(*arg + 1);
6027 }
6028
6029 if (**arg != ']')
6030 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006031 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032failret:
6033 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006034 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 return FAIL;
6036 }
6037
6038 *arg = skipwhite(*arg + 1);
6039 if (evaluate)
6040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006041 rettv->v_type = VAR_LIST;
6042 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 ++l->lv_refcount;
6044 }
6045
6046 return OK;
6047}
6048
6049/*
6050 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006051 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006053 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006054list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006056 list_T *l;
6057
6058 l = (list_T *)alloc_clear(sizeof(list_T));
6059 if (l != NULL)
6060 {
6061 /* Prepend the list to the list of lists for garbage collection. */
6062 if (first_list != NULL)
6063 first_list->lv_used_prev = l;
6064 l->lv_used_prev = NULL;
6065 l->lv_used_next = first_list;
6066 first_list = l;
6067 }
6068 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069}
6070
6071/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006072 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006073 * Returns OK or FAIL.
6074 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006075 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006076rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006077{
6078 list_T *l = list_alloc();
6079
6080 if (l == NULL)
6081 return FAIL;
6082
6083 rettv->vval.v_list = l;
6084 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006085 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006086 ++l->lv_refcount;
6087 return OK;
6088}
6089
6090/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 * Unreference a list: decrement the reference count and free it when it
6092 * becomes zero.
6093 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006097 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006098 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099}
6100
6101/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006102 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103 * Ignores the reference count.
6104 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006105 static void
6106list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006107{
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006109
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006110 for (item = l->lv_first; item != NULL; item = l->lv_first)
6111 {
6112 /* Remove the item before deleting it. */
6113 l->lv_first = item->li_next;
6114 clear_tv(&item->li_tv);
6115 vim_free(item);
6116 }
6117}
6118
6119 static void
6120list_free_list(list_T *l)
6121{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006122 /* Remove the list from the list of lists for garbage collection. */
6123 if (l->lv_used_prev == NULL)
6124 first_list = l->lv_used_next;
6125 else
6126 l->lv_used_prev->lv_used_next = l->lv_used_next;
6127 if (l->lv_used_next != NULL)
6128 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6129
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130 vim_free(l);
6131}
6132
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006133 void
6134list_free(list_T *l)
6135{
6136 if (!in_free_unref_items)
6137 {
6138 list_free_contents(l);
6139 list_free_list(l);
6140 }
6141}
6142
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143/*
6144 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006145 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006147 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006148listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149{
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151}
6152
6153/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006154 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006155 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006159 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006160 vim_free(item);
6161}
6162
6163/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006164 * Remove a list item from a List and free it. Also clears the value.
6165 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006168{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006169 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006170 listitem_free(item);
6171}
6172
6173/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 * Get the number of items in a list.
6175 */
6176 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006177list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179 if (l == NULL)
6180 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006181 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182}
6183
6184/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185 * Return TRUE when two lists have exactly the same values.
6186 */
6187 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006188list_equal(
6189 list_T *l1,
6190 list_T *l2,
6191 int ic, /* ignore case for strings */
6192 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193{
Bram Moolenaar33570922005-01-25 22:26:29 +00006194 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006195
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006196 if (l1 == NULL || l2 == NULL)
6197 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006198 if (l1 == l2)
6199 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006200 if (list_len(l1) != list_len(l2))
6201 return FALSE;
6202
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006203 for (item1 = l1->lv_first, item2 = l2->lv_first;
6204 item1 != NULL && item2 != NULL;
6205 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006207 return FALSE;
6208 return item1 == NULL && item2 == NULL;
6209}
6210
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006211/*
6212 * Return the dictitem that an entry in a hashtable points to.
6213 */
6214 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006215dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006216{
6217 return HI2DI(hi);
6218}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006219
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006220/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006221 * Return TRUE when two dictionaries have exactly the same key/values.
6222 */
6223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006224dict_equal(
6225 dict_T *d1,
6226 dict_T *d2,
6227 int ic, /* ignore case for strings */
6228 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006229{
Bram Moolenaar33570922005-01-25 22:26:29 +00006230 hashitem_T *hi;
6231 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006232 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006233
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006234 if (d1 == NULL && d2 == NULL)
6235 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006236 if (d1 == NULL || d2 == NULL)
6237 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006238 if (d1 == d2)
6239 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006240 if (dict_len(d1) != dict_len(d2))
6241 return FALSE;
6242
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006243 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006244 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006245 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006246 if (!HASHITEM_EMPTY(hi))
6247 {
6248 item2 = dict_find(d2, hi->hi_key, -1);
6249 if (item2 == NULL)
6250 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006251 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006252 return FALSE;
6253 --todo;
6254 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006255 }
6256 return TRUE;
6257}
6258
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006259static int tv_equal_recurse_limit;
6260
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006261/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006262 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006263 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006264 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006265 */
6266 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006267tv_equal(
6268 typval_T *tv1,
6269 typval_T *tv2,
6270 int ic, /* ignore case */
6271 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006272{
6273 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006274 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006275 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006276 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006277
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006278 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6279 if ((tv1->v_type == VAR_FUNC
6280 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6281 && (tv2->v_type == VAR_FUNC
6282 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6283 {
6284 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6285 : tv1->vval.v_partial->pt_name;
6286 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6287 : tv2->vval.v_partial->pt_name;
6288 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6289 }
6290
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006291 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006292 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006293
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006294 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006295 * recursiveness to a limit. We guess they are equal then.
6296 * A fixed limit has the problem of still taking an awful long time.
6297 * Reduce the limit every time running into it. That should work fine for
6298 * deeply linked structures that are not recursively linked and catch
6299 * recursiveness quickly. */
6300 if (!recursive)
6301 tv_equal_recurse_limit = 1000;
6302 if (recursive_cnt >= tv_equal_recurse_limit)
6303 {
6304 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006305 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006306 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006307
6308 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006309 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006310 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006311 ++recursive_cnt;
6312 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6313 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006314 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006315
6316 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006317 ++recursive_cnt;
6318 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6319 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006320 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006321
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006322 case VAR_NUMBER:
6323 return tv1->vval.v_number == tv2->vval.v_number;
6324
6325 case VAR_STRING:
6326 s1 = get_tv_string_buf(tv1, buf1);
6327 s2 = get_tv_string_buf(tv2, buf2);
6328 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006329
6330 case VAR_SPECIAL:
6331 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006332
6333 case VAR_FLOAT:
6334#ifdef FEAT_FLOAT
6335 return tv1->vval.v_float == tv2->vval.v_float;
6336#endif
6337 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006338#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006339 return tv1->vval.v_job == tv2->vval.v_job;
6340#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006341 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006342#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006343 return tv1->vval.v_channel == tv2->vval.v_channel;
6344#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006345 case VAR_FUNC:
6346 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006347 case VAR_UNKNOWN:
6348 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006349 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006350
Bram Moolenaara03f2332016-02-06 18:09:59 +01006351 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6352 * does not equal anything, not even itself. */
6353 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006354}
6355
6356/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006357 * Locate item with index "n" in list "l" and return it.
6358 * A negative index is counted from the end; -1 is the last item.
6359 * Returns NULL when "n" is out of range.
6360 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006361 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006362list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363{
Bram Moolenaar33570922005-01-25 22:26:29 +00006364 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006365 long idx;
6366
6367 if (l == NULL)
6368 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006369
6370 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006372 n = l->lv_len + n;
6373
6374 /* Check for index out of range. */
6375 if (n < 0 || n >= l->lv_len)
6376 return NULL;
6377
6378 /* When there is a cached index may start search from there. */
6379 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006381 if (n < l->lv_idx / 2)
6382 {
6383 /* closest to the start of the list */
6384 item = l->lv_first;
6385 idx = 0;
6386 }
6387 else if (n > (l->lv_idx + l->lv_len) / 2)
6388 {
6389 /* closest to the end of the list */
6390 item = l->lv_last;
6391 idx = l->lv_len - 1;
6392 }
6393 else
6394 {
6395 /* closest to the cached index */
6396 item = l->lv_idx_item;
6397 idx = l->lv_idx;
6398 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 }
6400 else
6401 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006402 if (n < l->lv_len / 2)
6403 {
6404 /* closest to the start of the list */
6405 item = l->lv_first;
6406 idx = 0;
6407 }
6408 else
6409 {
6410 /* closest to the end of the list */
6411 item = l->lv_last;
6412 idx = l->lv_len - 1;
6413 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006414 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006415
6416 while (n > idx)
6417 {
6418 /* search forward */
6419 item = item->li_next;
6420 ++idx;
6421 }
6422 while (n < idx)
6423 {
6424 /* search backward */
6425 item = item->li_prev;
6426 --idx;
6427 }
6428
6429 /* cache the used index */
6430 l->lv_idx = idx;
6431 l->lv_idx_item = item;
6432
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 return item;
6434}
6435
6436/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006437 * Get list item "l[idx]" as a number.
6438 */
6439 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440list_find_nr(
6441 list_T *l,
6442 long idx,
6443 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006444{
6445 listitem_T *li;
6446
6447 li = list_find(l, idx);
6448 if (li == NULL)
6449 {
6450 if (errorp != NULL)
6451 *errorp = TRUE;
6452 return -1L;
6453 }
6454 return get_tv_number_chk(&li->li_tv, errorp);
6455}
6456
6457/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006458 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6459 */
6460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006461list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006462{
6463 listitem_T *li;
6464
6465 li = list_find(l, idx - 1);
6466 if (li == NULL)
6467 {
6468 EMSGN(_(e_listidx), idx);
6469 return NULL;
6470 }
6471 return get_tv_string(&li->li_tv);
6472}
6473
6474/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006475 * Locate "item" list "l" and return its index.
6476 * Returns -1 when "item" is not in the list.
6477 */
6478 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006479list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006480{
6481 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006482 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006483
6484 if (l == NULL)
6485 return -1;
6486 idx = 0;
6487 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6488 ++idx;
6489 if (li == NULL)
6490 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006491 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006492}
6493
6494/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495 * Append item "item" to the end of list "l".
6496 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006498list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499{
6500 if (l->lv_last == NULL)
6501 {
6502 /* empty list */
6503 l->lv_first = item;
6504 l->lv_last = item;
6505 item->li_prev = NULL;
6506 }
6507 else
6508 {
6509 l->lv_last->li_next = item;
6510 item->li_prev = l->lv_last;
6511 l->lv_last = item;
6512 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006513 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006514 item->li_next = NULL;
6515}
6516
6517/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006522list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006523{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006524 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006525
Bram Moolenaar05159a02005-02-26 23:04:13 +00006526 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006528 copy_tv(tv, &li->li_tv);
6529 list_append(l, li);
6530 return OK;
6531}
6532
6533/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006534 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006535 * Return FAIL when out of memory.
6536 */
6537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006539{
6540 listitem_T *li = listitem_alloc();
6541
6542 if (li == NULL)
6543 return FAIL;
6544 li->li_tv.v_type = VAR_DICT;
6545 li->li_tv.v_lock = 0;
6546 li->li_tv.vval.v_dict = dict;
6547 list_append(list, li);
6548 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 return OK;
6550}
6551
6552/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006553 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006554 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006555 * Returns FAIL when out of memory.
6556 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006557 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006558list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006559{
6560 listitem_T *li = listitem_alloc();
6561
6562 if (li == NULL)
6563 return FAIL;
6564 list_append(l, li);
6565 li->li_tv.v_type = VAR_STRING;
6566 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006567 if (str == NULL)
6568 li->li_tv.vval.v_string = NULL;
6569 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006570 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006571 return FAIL;
6572 return OK;
6573}
6574
6575/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006576 * Append "n" to list "l".
6577 * Returns FAIL when out of memory.
6578 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006579 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006580list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006581{
6582 listitem_T *li;
6583
6584 li = listitem_alloc();
6585 if (li == NULL)
6586 return FAIL;
6587 li->li_tv.v_type = VAR_NUMBER;
6588 li->li_tv.v_lock = 0;
6589 li->li_tv.vval.v_number = n;
6590 list_append(l, li);
6591 return OK;
6592}
6593
6594/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006595 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006596 * If "item" is NULL append at the end.
6597 * Return FAIL when out of memory.
6598 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006599 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006600list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006601{
Bram Moolenaar33570922005-01-25 22:26:29 +00006602 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006603
6604 if (ni == NULL)
6605 return FAIL;
6606 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006607 list_insert(l, ni, item);
6608 return OK;
6609}
6610
6611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006612list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006613{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006614 if (item == NULL)
6615 /* Append new item at end of list. */
6616 list_append(l, ni);
6617 else
6618 {
6619 /* Insert new item before existing item. */
6620 ni->li_prev = item->li_prev;
6621 ni->li_next = item;
6622 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006623 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006625 ++l->lv_idx;
6626 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006627 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006628 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006630 l->lv_idx_item = NULL;
6631 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006632 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006633 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635}
6636
6637/*
6638 * Extend "l1" with "l2".
6639 * If "bef" is NULL append at the end, otherwise insert before this item.
6640 * Returns FAIL when out of memory.
6641 */
6642 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006643list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644{
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006646 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006648 /* We also quit the loop when we have inserted the original item count of
6649 * the list, avoid a hang when we extend a list with itself. */
6650 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6652 return FAIL;
6653 return OK;
6654}
6655
6656/*
6657 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6658 * Return FAIL when out of memory.
6659 */
6660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006661list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006662{
Bram Moolenaar33570922005-01-25 22:26:29 +00006663 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006664
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006665 if (l1 == NULL || l2 == NULL)
6666 return FAIL;
6667
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006668 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006669 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006670 if (l == NULL)
6671 return FAIL;
6672 tv->v_type = VAR_LIST;
6673 tv->vval.v_list = l;
6674
6675 /* append all items from the second list */
6676 return list_extend(l, l2, NULL);
6677}
6678
6679/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006680 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006682 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683 * Returns NULL when out of memory.
6684 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006685 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006686list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687{
Bram Moolenaar33570922005-01-25 22:26:29 +00006688 list_T *copy;
6689 listitem_T *item;
6690 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006691
6692 if (orig == NULL)
6693 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694
6695 copy = list_alloc();
6696 if (copy != NULL)
6697 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006698 if (copyID != 0)
6699 {
6700 /* Do this before adding the items, because one of the items may
6701 * refer back to this list. */
6702 orig->lv_copyID = copyID;
6703 orig->lv_copylist = copy;
6704 }
6705 for (item = orig->lv_first; item != NULL && !got_int;
6706 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707 {
6708 ni = listitem_alloc();
6709 if (ni == NULL)
6710 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006711 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 {
6713 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6714 {
6715 vim_free(ni);
6716 break;
6717 }
6718 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006720 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006721 list_append(copy, ni);
6722 }
6723 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006724 if (item != NULL)
6725 {
6726 list_unref(copy);
6727 copy = NULL;
6728 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006729 }
6730
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006731 return copy;
6732}
6733
6734/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006735 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006736 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006737 * This used to be called list_remove, but that conflicts with a Sun header
6738 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006739 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006740 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006741vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742{
Bram Moolenaar33570922005-01-25 22:26:29 +00006743 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006745 /* notify watchers */
6746 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006747 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006748 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006749 list_fix_watch(l, ip);
6750 if (ip == item2)
6751 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006752 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006753
6754 if (item2->li_next == NULL)
6755 l->lv_last = item->li_prev;
6756 else
6757 item2->li_next->li_prev = item->li_prev;
6758 if (item->li_prev == NULL)
6759 l->lv_first = item2->li_next;
6760 else
6761 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006762 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006763}
6764
6765/*
6766 * Return an allocated string with the string representation of a list.
6767 * May return NULL.
6768 */
6769 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006770list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006771{
6772 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006773
6774 if (tv->vval.v_list == NULL)
6775 return NULL;
6776 ga_init2(&ga, (int)sizeof(char), 80);
6777 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006778 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6779 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006780 {
6781 vim_free(ga.ga_data);
6782 return NULL;
6783 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006784 ga_append(&ga, ']');
6785 ga_append(&ga, NUL);
6786 return (char_u *)ga.ga_data;
6787}
6788
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006789typedef struct join_S {
6790 char_u *s;
6791 char_u *tofree;
6792} join_T;
6793
6794 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006795list_join_inner(
6796 garray_T *gap, /* to store the result in */
6797 list_T *l,
6798 char_u *sep,
6799 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006800 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006801 int copyID,
6802 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006803{
6804 int i;
6805 join_T *p;
6806 int len;
6807 int sumlen = 0;
6808 int first = TRUE;
6809 char_u *tofree;
6810 char_u numbuf[NUMBUFLEN];
6811 listitem_T *item;
6812 char_u *s;
6813
6814 /* Stringify each item in the list. */
6815 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6816 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006817 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6818 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006819 if (s == NULL)
6820 return FAIL;
6821
6822 len = (int)STRLEN(s);
6823 sumlen += len;
6824
Bram Moolenaarcde88542015-08-11 19:14:00 +02006825 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006826 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6827 if (tofree != NULL || s != numbuf)
6828 {
6829 p->s = s;
6830 p->tofree = tofree;
6831 }
6832 else
6833 {
6834 p->s = vim_strnsave(s, len);
6835 p->tofree = p->s;
6836 }
6837
6838 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006839 if (did_echo_string_emsg) /* recursion error, bail out */
6840 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006841 }
6842
6843 /* Allocate result buffer with its total size, avoid re-allocation and
6844 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6845 if (join_gap->ga_len >= 2)
6846 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6847 if (ga_grow(gap, sumlen + 2) == FAIL)
6848 return FAIL;
6849
6850 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6851 {
6852 if (first)
6853 first = FALSE;
6854 else
6855 ga_concat(gap, sep);
6856 p = ((join_T *)join_gap->ga_data) + i;
6857
6858 if (p->s != NULL)
6859 ga_concat(gap, p->s);
6860 line_breakcheck();
6861 }
6862
6863 return OK;
6864}
6865
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006866/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006867 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006868 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006869 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006870 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006872list_join(
6873 garray_T *gap,
6874 list_T *l,
6875 char_u *sep,
6876 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006877 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006878 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006879{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006880 garray_T join_ga;
6881 int retval;
6882 join_T *p;
6883 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006884
Bram Moolenaard39a7512015-04-16 22:51:22 +02006885 if (l->lv_len < 1)
6886 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006887 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006888 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6889 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006890
6891 /* Dispose each item in join_ga. */
6892 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006893 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006894 p = (join_T *)join_ga.ga_data;
6895 for (i = 0; i < join_ga.ga_len; ++i)
6896 {
6897 vim_free(p->tofree);
6898 ++p;
6899 }
6900 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006901 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006902
6903 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006904}
6905
6906/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006907 * Return the next (unique) copy ID.
6908 * Used for serializing nested structures.
6909 */
6910 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006911get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006912{
6913 current_copyID += COPYID_INC;
6914 return current_copyID;
6915}
6916
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006917/* Used by get_func_tv() */
6918static garray_T funcargs = GA_EMPTY;
6919
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006920/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 * Garbage collection for lists and dictionaries.
6922 *
6923 * We use reference counts to be able to free most items right away when they
6924 * are no longer used. But for composite items it's possible that it becomes
6925 * unused while the reference count is > 0: When there is a recursive
6926 * reference. Example:
6927 * :let l = [1, 2, 3]
6928 * :let d = {9: l}
6929 * :let l[1] = d
6930 *
6931 * Since this is quite unusual we handle this with garbage collection: every
6932 * once in a while find out which lists and dicts are not referenced from any
6933 * variable.
6934 *
6935 * Here is a good reference text about garbage collection (refers to Python
6936 * but it applies to all reference-counting mechanisms):
6937 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006938 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939
6940/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02006942 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006944 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006946garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006947{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006950 buf_T *buf;
6951 win_T *wp;
6952 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006953 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006954 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006956#ifdef FEAT_WINDOWS
6957 tabpage_T *tp;
6958#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006960 if (!testing)
6961 {
6962 /* Only do this once. */
6963 want_garbage_collect = FALSE;
6964 may_garbage_collect = FALSE;
6965 garbage_collect_at_exit = FALSE;
6966 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006967
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968 /* We advance by two because we add one for items referenced through
6969 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006970 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972 /*
6973 * 1. Go through all accessible variables and mark all lists and dicts
6974 * with copyID.
6975 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976
6977 /* Don't free variables in the previous_funccal list unless they are only
6978 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006979 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6981 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006982 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6983 NULL);
6984 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6985 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006986 }
6987
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 /* script-local variables */
6989 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006990 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991
6992 /* buffer-local variables */
6993 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006994 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6995 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996
6997 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006998 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006999 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7000 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007001#ifdef FEAT_AUTOCMD
7002 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007003 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7004 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007005#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007007#ifdef FEAT_WINDOWS
7008 /* tabpage-local variables */
7009 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007010 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7011 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007012#endif
7013
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016
7017 /* function-local variables */
7018 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7019 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7021 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 }
7023
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007024 /* function call arguments, if v:testing is set. */
7025 for (i = 0; i < funcargs.ga_len; ++i)
7026 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7027 copyID, NULL, NULL);
7028
Bram Moolenaard812df62008-11-09 12:46:09 +00007029 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007031
Bram Moolenaar1dced572012-04-05 16:54:08 +02007032#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007033 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007034#endif
7035
Bram Moolenaardb913952012-06-29 12:54:53 +02007036#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007038#endif
7039
7040#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007042#endif
7043
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007044#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007045 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007046 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007047#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007048#ifdef FEAT_NETBEANS_INTG
7049 abort = abort || set_ref_in_nb_channel(copyID);
7050#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007051
Bram Moolenaare3188e22016-05-31 21:13:04 +02007052#ifdef FEAT_TIMERS
7053 abort = abort || set_ref_in_timer(copyID);
7054#endif
7055
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007056 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007057 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 /*
7059 * 2. Free lists and dictionaries that are not referenced.
7060 */
7061 did_free = free_unref_items(copyID);
7062
7063 /*
7064 * 3. Check if any funccal can be freed now.
7065 */
7066 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007067 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 if (can_free_funccal(*pfc, copyID))
7069 {
7070 fc = *pfc;
7071 *pfc = fc->caller;
7072 free_funccal(fc, TRUE);
7073 did_free = TRUE;
7074 did_free_funccal = TRUE;
7075 }
7076 else
7077 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007078 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 if (did_free_funccal)
7080 /* When a funccal was freed some more items might be garbage
7081 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007082 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007083 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 else if (p_verbose > 0)
7085 {
7086 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7087 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007088
7089 return did_free;
7090}
7091
7092/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007093 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007094 */
7095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007096free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007097{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007098 dict_T *dd, *dd_next;
7099 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007100 int did_free = FALSE;
7101
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007102 /* Let all "free" functions know that we are here. This means no
7103 * dictionaries, lists, channels or jobs are to be freed, because we will
7104 * do that here. */
7105 in_free_unref_items = TRUE;
7106
7107 /*
7108 * PASS 1: free the contents of the items. We don't free the items
7109 * themselves yet, so that it is possible to decrement refcount counters
7110 */
7111
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007113 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007115 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007116 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007118 /* Free the Dictionary and ordinary items it contains, but don't
7119 * recurse into Lists and Dictionaries, they will be in the list
7120 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007121 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007122 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007123 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124
7125 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007126 * Go through the list of lists and free items without the copyID.
7127 * But don't free a list that has a watcher (used in a for loop), these
7128 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007129 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007130 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7131 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7132 && ll->lv_watch == NULL)
7133 {
7134 /* Free the List and ordinary items it contains, but don't recurse
7135 * into Lists and Dictionaries, they will be in the list of dicts
7136 * or list of lists. */
7137 list_free_contents(ll);
7138 did_free = TRUE;
7139 }
7140
7141#ifdef FEAT_JOB_CHANNEL
7142 /* Go through the list of jobs and free items without the copyID. This
7143 * must happen before doing channels, because jobs refer to channels, but
7144 * the reference from the channel to the job isn't tracked. */
7145 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7146
7147 /* Go through the list of channels and free items without the copyID. */
7148 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7149#endif
7150
7151 /*
7152 * PASS 2: free the items themselves.
7153 */
7154 for (dd = first_dict; dd != NULL; dd = dd_next)
7155 {
7156 dd_next = dd->dv_used_next;
7157 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7158 dict_free_dict(dd);
7159 }
7160
7161 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007162 {
7163 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007164 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7165 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007166 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007167 /* Free the List and ordinary items it contains, but don't recurse
7168 * into Lists and Dictionaries, they will be in the list of dicts
7169 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007170 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007171 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007172 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007173
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007174#ifdef FEAT_JOB_CHANNEL
7175 /* Go through the list of jobs and free items without the copyID. This
7176 * must happen before doing channels, because jobs refer to channels, but
7177 * the reference from the channel to the job isn't tracked. */
7178 free_unused_jobs(copyID, COPYID_MASK);
7179
7180 /* Go through the list of channels and free items without the copyID. */
7181 free_unused_channels(copyID, COPYID_MASK);
7182#endif
7183
7184 in_free_unref_items = FALSE;
7185
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007186 return did_free;
7187}
7188
7189/*
7190 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007191 * "list_stack" is used to add lists to be marked. Can be NULL.
7192 *
7193 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007194 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007195 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007196set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007197{
7198 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007199 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007201 hashtab_T *cur_ht;
7202 ht_stack_T *ht_stack = NULL;
7203 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007204
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007205 cur_ht = ht;
7206 for (;;)
7207 {
7208 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007209 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007210 /* Mark each item in the hashtab. If the item contains a hashtab
7211 * it is added to ht_stack, if it contains a list it is added to
7212 * list_stack. */
7213 todo = (int)cur_ht->ht_used;
7214 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7215 if (!HASHITEM_EMPTY(hi))
7216 {
7217 --todo;
7218 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7219 &ht_stack, list_stack);
7220 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007221 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007222
7223 if (ht_stack == NULL)
7224 break;
7225
7226 /* take an item from the stack */
7227 cur_ht = ht_stack->ht;
7228 tempitem = ht_stack;
7229 ht_stack = ht_stack->prev;
7230 free(tempitem);
7231 }
7232
7233 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007234}
7235
7236/*
7237 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007238 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7239 *
7240 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007241 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007242 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007243set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007244{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007245 listitem_T *li;
7246 int abort = FALSE;
7247 list_T *cur_l;
7248 list_stack_T *list_stack = NULL;
7249 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007250
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007251 cur_l = l;
7252 for (;;)
7253 {
7254 if (!abort)
7255 /* Mark each item in the list. If the item contains a hashtab
7256 * it is added to ht_stack, if it contains a list it is added to
7257 * list_stack. */
7258 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7259 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7260 ht_stack, &list_stack);
7261 if (list_stack == NULL)
7262 break;
7263
7264 /* take an item from the stack */
7265 cur_l = list_stack->list;
7266 tempitem = list_stack;
7267 list_stack = list_stack->prev;
7268 free(tempitem);
7269 }
7270
7271 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007272}
7273
7274/*
7275 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007276 * "list_stack" is used to add lists to be marked. Can be NULL.
7277 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7278 *
7279 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007280 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007281 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007282set_ref_in_item(
7283 typval_T *tv,
7284 int copyID,
7285 ht_stack_T **ht_stack,
7286 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007287{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007288 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007289
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007290 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007291 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007292 dict_T *dd = tv->vval.v_dict;
7293
Bram Moolenaara03f2332016-02-06 18:09:59 +01007294 if (dd != NULL && dd->dv_copyID != copyID)
7295 {
7296 /* Didn't see this dict yet. */
7297 dd->dv_copyID = copyID;
7298 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007299 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007300 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7301 }
7302 else
7303 {
7304 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7305 if (newitem == NULL)
7306 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007307 else
7308 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007309 newitem->ht = &dd->dv_hashtab;
7310 newitem->prev = *ht_stack;
7311 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007312 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007313 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007314 }
7315 }
7316 else if (tv->v_type == VAR_LIST)
7317 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007318 list_T *ll = tv->vval.v_list;
7319
Bram Moolenaara03f2332016-02-06 18:09:59 +01007320 if (ll != NULL && ll->lv_copyID != copyID)
7321 {
7322 /* Didn't see this list yet. */
7323 ll->lv_copyID = copyID;
7324 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007325 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007326 abort = set_ref_in_list(ll, copyID, ht_stack);
7327 }
7328 else
7329 {
7330 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007331 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007332 if (newitem == NULL)
7333 abort = TRUE;
7334 else
7335 {
7336 newitem->list = ll;
7337 newitem->prev = *list_stack;
7338 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007339 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007340 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007341 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007342 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007343 else if (tv->v_type == VAR_PARTIAL)
7344 {
7345 partial_T *pt = tv->vval.v_partial;
7346 int i;
7347
7348 /* A partial does not have a copyID, because it cannot contain itself.
7349 */
7350 if (pt != NULL)
7351 {
7352 if (pt->pt_dict != NULL)
7353 {
7354 typval_T dtv;
7355
7356 dtv.v_type = VAR_DICT;
7357 dtv.vval.v_dict = pt->pt_dict;
7358 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7359 }
7360
7361 for (i = 0; i < pt->pt_argc; ++i)
7362 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7363 ht_stack, list_stack);
7364 }
7365 }
7366#ifdef FEAT_JOB_CHANNEL
7367 else if (tv->v_type == VAR_JOB)
7368 {
7369 job_T *job = tv->vval.v_job;
7370 typval_T dtv;
7371
7372 if (job != NULL && job->jv_copyID != copyID)
7373 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007374 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007375 if (job->jv_channel != NULL)
7376 {
7377 dtv.v_type = VAR_CHANNEL;
7378 dtv.vval.v_channel = job->jv_channel;
7379 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7380 }
7381 if (job->jv_exit_partial != NULL)
7382 {
7383 dtv.v_type = VAR_PARTIAL;
7384 dtv.vval.v_partial = job->jv_exit_partial;
7385 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7386 }
7387 }
7388 }
7389 else if (tv->v_type == VAR_CHANNEL)
7390 {
7391 channel_T *ch =tv->vval.v_channel;
7392 int part;
7393 typval_T dtv;
7394 jsonq_T *jq;
7395 cbq_T *cq;
7396
7397 if (ch != NULL && ch->ch_copyID != copyID)
7398 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007399 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007400 for (part = PART_SOCK; part <= PART_IN; ++part)
7401 {
7402 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7403 jq = jq->jq_next)
7404 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7405 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7406 cq = cq->cq_next)
7407 if (cq->cq_partial != NULL)
7408 {
7409 dtv.v_type = VAR_PARTIAL;
7410 dtv.vval.v_partial = cq->cq_partial;
7411 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7412 }
7413 if (ch->ch_part[part].ch_partial != NULL)
7414 {
7415 dtv.v_type = VAR_PARTIAL;
7416 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7417 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7418 }
7419 }
7420 if (ch->ch_partial != NULL)
7421 {
7422 dtv.v_type = VAR_PARTIAL;
7423 dtv.vval.v_partial = ch->ch_partial;
7424 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7425 }
7426 if (ch->ch_close_partial != NULL)
7427 {
7428 dtv.v_type = VAR_PARTIAL;
7429 dtv.vval.v_partial = ch->ch_close_partial;
7430 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7431 }
7432 }
7433 }
7434#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007435 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007436}
7437
7438/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439 * Allocate an empty header for a dictionary.
7440 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007441 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007442dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007443{
Bram Moolenaar33570922005-01-25 22:26:29 +00007444 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007445
Bram Moolenaar33570922005-01-25 22:26:29 +00007446 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007447 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007448 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007449 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007450 if (first_dict != NULL)
7451 first_dict->dv_used_prev = d;
7452 d->dv_used_next = first_dict;
7453 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007454 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007455
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007457 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007458 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007459 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007460 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007461 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007462 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463}
7464
7465/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007466 * Allocate an empty dict for a return value.
7467 * Returns OK or FAIL.
7468 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007470rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007471{
7472 dict_T *d = dict_alloc();
7473
7474 if (d == NULL)
7475 return FAIL;
7476
7477 rettv->vval.v_dict = d;
7478 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007479 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007480 ++d->dv_refcount;
7481 return OK;
7482}
7483
7484
7485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 * Unreference a Dictionary: decrement the reference count and free it when it
7487 * becomes zero.
7488 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007489 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007490dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007492 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007493 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494}
7495
7496/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007497 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 * Ignores the reference count.
7499 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007500 static void
7501dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007504 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007505 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007507 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007508 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007509 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007510 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007512 if (!HASHITEM_EMPTY(hi))
7513 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007514 /* Remove the item before deleting it, just in case there is
7515 * something recursive causing trouble. */
7516 di = HI2DI(hi);
7517 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007518 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007519 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520 --todo;
7521 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007524}
7525
7526 static void
7527dict_free_dict(dict_T *d)
7528{
7529 /* Remove the dict from the list of dicts for garbage collection. */
7530 if (d->dv_used_prev == NULL)
7531 first_dict = d->dv_used_next;
7532 else
7533 d->dv_used_prev->dv_used_next = d->dv_used_next;
7534 if (d->dv_used_next != NULL)
7535 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536 vim_free(d);
7537}
7538
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007539 void
7540dict_free(dict_T *d)
7541{
7542 if (!in_free_unref_items)
7543 {
7544 dict_free_contents(d);
7545 dict_free_dict(d);
7546 }
7547}
7548
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549/*
7550 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551 * The "key" is copied to the new item.
7552 * Note that the value of the item "di_tv" still needs to be initialized!
7553 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007555 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557{
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007559
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007560 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007562 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007564 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007565 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567}
7568
7569/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007570 * Make a copy of a Dictionary item.
7571 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007573dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574{
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007576
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007577 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7578 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007579 if (di != NULL)
7580 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007581 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007582 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 copy_tv(&org->di_tv, &di->di_tv);
7584 }
7585 return di;
7586}
7587
7588/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007589 * Remove item "item" from Dictionary "dict" and free it.
7590 */
7591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007592dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007593{
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007595
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007597 if (HASHITEM_EMPTY(hi))
7598 EMSG2(_(e_intern2), "dictitem_remove()");
7599 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007600 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007601 dictitem_free(item);
7602}
7603
7604/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 * Free a dict item. Also clears the value.
7606 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007608dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007610 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007611 if (item->di_flags & DI_FLAGS_ALLOC)
7612 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613}
7614
7615/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007616 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7617 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007618 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 * Returns NULL when out of memory.
7620 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007621 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007622dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007623{
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 dict_T *copy;
7625 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007626 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007628
7629 if (orig == NULL)
7630 return NULL;
7631
7632 copy = dict_alloc();
7633 if (copy != NULL)
7634 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007635 if (copyID != 0)
7636 {
7637 orig->dv_copyID = copyID;
7638 orig->dv_copydict = copy;
7639 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007640 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007641 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007643 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007644 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007645 --todo;
7646
7647 di = dictitem_alloc(hi->hi_key);
7648 if (di == NULL)
7649 break;
7650 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007651 {
7652 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7653 copyID) == FAIL)
7654 {
7655 vim_free(di);
7656 break;
7657 }
7658 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 else
7660 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7661 if (dict_add(copy, di) == FAIL)
7662 {
7663 dictitem_free(di);
7664 break;
7665 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007668
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007670 if (todo > 0)
7671 {
7672 dict_unref(copy);
7673 copy = NULL;
7674 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007675 }
7676
7677 return copy;
7678}
7679
7680/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007682 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007684 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007685dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686{
Bram Moolenaar33570922005-01-25 22:26:29 +00007687 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688}
7689
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007691 * Add a number or string entry to dictionary "d".
7692 * When "str" is NULL use number "nr", otherwise use "str".
7693 * Returns FAIL when out of memory and when key already exists.
7694 */
7695 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007696dict_add_nr_str(
7697 dict_T *d,
7698 char *key,
7699 long nr,
7700 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007701{
7702 dictitem_T *item;
7703
7704 item = dictitem_alloc((char_u *)key);
7705 if (item == NULL)
7706 return FAIL;
7707 item->di_tv.v_lock = 0;
7708 if (str == NULL)
7709 {
7710 item->di_tv.v_type = VAR_NUMBER;
7711 item->di_tv.vval.v_number = nr;
7712 }
7713 else
7714 {
7715 item->di_tv.v_type = VAR_STRING;
7716 item->di_tv.vval.v_string = vim_strsave(str);
7717 }
7718 if (dict_add(d, item) == FAIL)
7719 {
7720 dictitem_free(item);
7721 return FAIL;
7722 }
7723 return OK;
7724}
7725
7726/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007727 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007728 * Returns FAIL when out of memory and when key already exists.
7729 */
7730 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007731dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007732{
7733 dictitem_T *item;
7734
7735 item = dictitem_alloc((char_u *)key);
7736 if (item == NULL)
7737 return FAIL;
7738 item->di_tv.v_lock = 0;
7739 item->di_tv.v_type = VAR_LIST;
7740 item->di_tv.vval.v_list = list;
7741 if (dict_add(d, item) == FAIL)
7742 {
7743 dictitem_free(item);
7744 return FAIL;
7745 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007746 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007747 return OK;
7748}
7749
7750/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007751 * Get the number of items in a Dictionary.
7752 */
7753 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007754dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007755{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007756 if (d == NULL)
7757 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007758 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007759}
7760
7761/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 * Find item "key[len]" in Dictionary "d".
7763 * If "len" is negative use strlen(key).
7764 * Returns NULL when not found.
7765 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007766 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007767dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007768{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007769#define AKEYLEN 200
7770 char_u buf[AKEYLEN];
7771 char_u *akey;
7772 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007774
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007775 if (d == NULL)
7776 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007777 if (len < 0)
7778 akey = key;
7779 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007780 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007781 tofree = akey = vim_strnsave(key, len);
7782 if (akey == NULL)
7783 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007784 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007785 else
7786 {
7787 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007788 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007789 akey = buf;
7790 }
7791
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007793 vim_free(tofree);
7794 if (HASHITEM_EMPTY(hi))
7795 return NULL;
7796 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007797}
7798
7799/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007800 * Get a string item from a dictionary.
7801 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007802 * Returns NULL if the entry doesn't exist or out of memory.
7803 */
7804 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007805get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007806{
7807 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007808 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007809
7810 di = dict_find(d, key, -1);
7811 if (di == NULL)
7812 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007813 s = get_tv_string(&di->di_tv);
7814 if (save && s != NULL)
7815 s = vim_strsave(s);
7816 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007817}
7818
7819/*
7820 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007821 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007822 */
7823 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007824get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007825{
7826 dictitem_T *di;
7827
7828 di = dict_find(d, key, -1);
7829 if (di == NULL)
7830 return 0;
7831 return get_tv_number(&di->di_tv);
7832}
7833
7834/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007835 * Return an allocated string with the string representation of a Dictionary.
7836 * May return NULL.
7837 */
7838 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007839dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007840{
7841 garray_T ga;
7842 int first = TRUE;
7843 char_u *tofree;
7844 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007847 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007848 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007849
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007850 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007851 return NULL;
7852 ga_init2(&ga, (int)sizeof(char), 80);
7853 ga_append(&ga, '{');
7854
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007855 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007856 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007858 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007859 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007860 --todo;
7861
7862 if (first)
7863 first = FALSE;
7864 else
7865 ga_concat(&ga, (char_u *)", ");
7866
7867 tofree = string_quote(hi->hi_key, FALSE);
7868 if (tofree != NULL)
7869 {
7870 ga_concat(&ga, tofree);
7871 vim_free(tofree);
7872 }
7873 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007874 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7875 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007876 if (s != NULL)
7877 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007878 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007879 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007880 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007881 line_breakcheck();
7882
Bram Moolenaar8c711452005-01-14 21:53:12 +00007883 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007884 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007885 if (todo > 0)
7886 {
7887 vim_free(ga.ga_data);
7888 return NULL;
7889 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007890
7891 ga_append(&ga, '}');
7892 ga_append(&ga, NUL);
7893 return (char_u *)ga.ga_data;
7894}
7895
7896/*
7897 * Allocate a variable for a Dictionary and fill it from "*arg".
7898 * Return OK or FAIL. Returns NOTDONE for {expr}.
7899 */
7900 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007901get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007902{
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 dict_T *d = NULL;
7904 typval_T tvkey;
7905 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007906 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007907 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007908 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007909 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007910
7911 /*
7912 * First check if it's not a curly-braces thing: {expr}.
7913 * Must do this without evaluating, otherwise a function may be called
7914 * twice. Unfortunately this means we need to call eval1() twice for the
7915 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007917 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007918 if (*start != '}')
7919 {
7920 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7921 return FAIL;
7922 if (*start == '}')
7923 return NOTDONE;
7924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007925
7926 if (evaluate)
7927 {
7928 d = dict_alloc();
7929 if (d == NULL)
7930 return FAIL;
7931 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007932 tvkey.v_type = VAR_UNKNOWN;
7933 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934
7935 *arg = skipwhite(*arg + 1);
7936 while (**arg != '}' && **arg != NUL)
7937 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007938 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007939 goto failret;
7940 if (**arg != ':')
7941 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007942 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007943 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944 goto failret;
7945 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007946 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007948 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007949 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007950 {
7951 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007952 clear_tv(&tvkey);
7953 goto failret;
7954 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956
7957 *arg = skipwhite(*arg + 1);
7958 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7959 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007960 if (evaluate)
7961 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007962 goto failret;
7963 }
7964 if (evaluate)
7965 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007966 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 if (item != NULL)
7968 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007969 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007970 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 clear_tv(&tv);
7972 goto failret;
7973 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007974 item = dictitem_alloc(key);
7975 clear_tv(&tvkey);
7976 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007979 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007980 if (dict_add(d, item) == FAIL)
7981 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 }
7983 }
7984
7985 if (**arg == '}')
7986 break;
7987 if (**arg != ',')
7988 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007989 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007990 goto failret;
7991 }
7992 *arg = skipwhite(*arg + 1);
7993 }
7994
7995 if (**arg != '}')
7996 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007997 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007998failret:
7999 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008000 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008001 return FAIL;
8002 }
8003
8004 *arg = skipwhite(*arg + 1);
8005 if (evaluate)
8006 {
8007 rettv->v_type = VAR_DICT;
8008 rettv->vval.v_dict = d;
8009 ++d->dv_refcount;
8010 }
8011
8012 return OK;
8013}
8014
Bram Moolenaar17a13432016-01-24 14:22:10 +01008015 static char *
8016get_var_special_name(int nr)
8017{
8018 switch (nr)
8019 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008020 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008021 case VVAL_TRUE: return "v:true";
8022 case VVAL_NONE: return "v:none";
8023 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008024 }
8025 EMSG2(_(e_intern2), "get_var_special_name()");
8026 return "42";
8027}
8028
Bram Moolenaar8c711452005-01-14 21:53:12 +00008029/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008030 * Return a string with the string representation of a variable.
8031 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008032 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008033 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008034 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8035 * "string()", otherwise does not put quotes around strings, as ":echo"
8036 * displays values.
8037 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8038 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008039 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008040 */
8041 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008042echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008043 typval_T *tv,
8044 char_u **tofree,
8045 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008046 int copyID,
8047 int echo_style,
8048 int restore_copyID,
8049 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008050{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008051 static int recurse = 0;
8052 char_u *r = NULL;
8053
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008055 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008056 if (!did_echo_string_emsg)
8057 {
8058 /* Only give this message once for a recursive call to avoid
8059 * flooding the user with errors. And stop iterating over lists
8060 * and dicts. */
8061 did_echo_string_emsg = TRUE;
8062 EMSG(_("E724: variable nested too deep for displaying"));
8063 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008064 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008065 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008066 }
8067 ++recurse;
8068
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008069 switch (tv->v_type)
8070 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008071 case VAR_STRING:
8072 if (echo_style && !dict_val)
8073 {
8074 *tofree = NULL;
8075 r = get_tv_string_buf(tv, numbuf);
8076 }
8077 else
8078 {
8079 *tofree = string_quote(tv->vval.v_string, FALSE);
8080 r = *tofree;
8081 }
8082 break;
8083
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008084 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008085 if (echo_style)
8086 {
8087 *tofree = NULL;
8088 r = tv->vval.v_string;
8089 }
8090 else
8091 {
8092 *tofree = string_quote(tv->vval.v_string, TRUE);
8093 r = *tofree;
8094 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008095 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008096
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008097 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008098 {
8099 partial_T *pt = tv->vval.v_partial;
8100 char_u *fname = string_quote(pt == NULL ? NULL
8101 : pt->pt_name, FALSE);
8102 garray_T ga;
8103 int i;
8104 char_u *tf;
8105
8106 ga_init2(&ga, 1, 100);
8107 ga_concat(&ga, (char_u *)"function(");
8108 if (fname != NULL)
8109 {
8110 ga_concat(&ga, fname);
8111 vim_free(fname);
8112 }
8113 if (pt != NULL && pt->pt_argc > 0)
8114 {
8115 ga_concat(&ga, (char_u *)", [");
8116 for (i = 0; i < pt->pt_argc; ++i)
8117 {
8118 if (i > 0)
8119 ga_concat(&ga, (char_u *)", ");
8120 ga_concat(&ga,
8121 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8122 vim_free(tf);
8123 }
8124 ga_concat(&ga, (char_u *)"]");
8125 }
8126 if (pt != NULL && pt->pt_dict != NULL)
8127 {
8128 typval_T dtv;
8129
8130 ga_concat(&ga, (char_u *)", ");
8131 dtv.v_type = VAR_DICT;
8132 dtv.vval.v_dict = pt->pt_dict;
8133 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8134 vim_free(tf);
8135 }
8136 ga_concat(&ga, (char_u *)")");
8137
8138 *tofree = ga.ga_data;
8139 r = *tofree;
8140 break;
8141 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008142
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008143 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008144 if (tv->vval.v_list == NULL)
8145 {
8146 *tofree = NULL;
8147 r = NULL;
8148 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008149 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8150 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008151 {
8152 *tofree = NULL;
8153 r = (char_u *)"[...]";
8154 }
8155 else
8156 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008157 int old_copyID = tv->vval.v_list->lv_copyID;
8158
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008159 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008160 *tofree = list2string(tv, copyID, restore_copyID);
8161 if (restore_copyID)
8162 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008163 r = *tofree;
8164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008165 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008166
Bram Moolenaar8c711452005-01-14 21:53:12 +00008167 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008168 if (tv->vval.v_dict == NULL)
8169 {
8170 *tofree = NULL;
8171 r = NULL;
8172 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008173 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8174 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008175 {
8176 *tofree = NULL;
8177 r = (char_u *)"{...}";
8178 }
8179 else
8180 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008181 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008182 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008183 *tofree = dict2string(tv, copyID, restore_copyID);
8184 if (restore_copyID)
8185 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008186 r = *tofree;
8187 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008188 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008189
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008190 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008191 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008192 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008193 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 *tofree = NULL;
8195 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008196 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008197
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008198 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008199#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008200 *tofree = NULL;
8201 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8202 r = numbuf;
8203 break;
8204#endif
8205
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008206 case VAR_SPECIAL:
8207 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008208 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008209 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008210 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008211
Bram Moolenaar8502c702014-06-17 12:51:16 +02008212 if (--recurse == 0)
8213 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008214 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008215}
8216
8217/*
8218 * Return a string with the string representation of a variable.
8219 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8220 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008221 * Does not put quotes around strings, as ":echo" displays values.
8222 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8223 * May return NULL.
8224 */
8225 static char_u *
8226echo_string(
8227 typval_T *tv,
8228 char_u **tofree,
8229 char_u *numbuf,
8230 int copyID)
8231{
8232 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8233}
8234
8235/*
8236 * Return a string with the string representation of a variable.
8237 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8238 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008239 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008240 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008242 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008243tv2string(
8244 typval_T *tv,
8245 char_u **tofree,
8246 char_u *numbuf,
8247 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008248{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008249 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008250}
8251
8252/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008253 * Return string "str" in ' quotes, doubling ' characters.
8254 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008255 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008256 */
8257 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008258string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008259{
Bram Moolenaar33570922005-01-25 22:26:29 +00008260 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008261 char_u *p, *r, *s;
8262
Bram Moolenaar33570922005-01-25 22:26:29 +00008263 len = (function ? 13 : 3);
8264 if (str != NULL)
8265 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008266 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008267 for (p = str; *p != NUL; mb_ptr_adv(p))
8268 if (*p == '\'')
8269 ++len;
8270 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008271 s = r = alloc(len);
8272 if (r != NULL)
8273 {
8274 if (function)
8275 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008276 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008277 r += 10;
8278 }
8279 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008280 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008281 if (str != NULL)
8282 for (p = str; *p != NUL; )
8283 {
8284 if (*p == '\'')
8285 *r++ = '\'';
8286 MB_COPY_CHAR(p, r);
8287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008288 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008289 if (function)
8290 *r++ = ')';
8291 *r++ = NUL;
8292 }
8293 return s;
8294}
8295
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008296#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008297/*
8298 * Convert the string "text" to a floating point number.
8299 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8300 * this always uses a decimal point.
8301 * Returns the length of the text that was consumed.
8302 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008303 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008304string2float(
8305 char_u *text,
8306 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008307{
8308 char *s = (char *)text;
8309 float_T f;
8310
8311 f = strtod(s, &s);
8312 *value = f;
8313 return (int)((char_u *)s - text);
8314}
8315#endif
8316
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 * Get the value of an environment variable.
8319 * "arg" is pointing to the '$'. It is advanced to after the name.
8320 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008321 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 */
8323 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008324get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325{
8326 char_u *string = NULL;
8327 int len;
8328 int cc;
8329 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008330 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331
8332 ++*arg;
8333 name = *arg;
8334 len = get_env_len(arg);
8335 if (evaluate)
8336 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008337 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008338 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008339
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008340 cc = name[len];
8341 name[len] = NUL;
8342 /* first try vim_getenv(), fast for normal environment vars */
8343 string = vim_getenv(name, &mustfree);
8344 if (string != NULL && *string != NUL)
8345 {
8346 if (!mustfree)
8347 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008349 else
8350 {
8351 if (mustfree)
8352 vim_free(string);
8353
8354 /* next try expanding things like $VIM and ${HOME} */
8355 string = expand_env_save(name - 1);
8356 if (string != NULL && *string == '$')
8357 {
8358 vim_free(string);
8359 string = NULL;
8360 }
8361 }
8362 name[len] = cc;
8363
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008364 rettv->v_type = VAR_STRING;
8365 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 }
8367
8368 return OK;
8369}
8370
8371/*
8372 * Array with names and number of arguments of all internal functions
8373 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8374 */
8375static struct fst
8376{
8377 char *f_name; /* function name */
8378 char f_min_argc; /* minimal number of arguments */
8379 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008380 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008381 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382} functions[] =
8383{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008384#ifdef FEAT_FLOAT
8385 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008386 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008387#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008388 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008389 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"append", 2, 2, f_append},
8391 {"argc", 0, 0, f_argc},
8392 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008393 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008394 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008395#ifdef FEAT_FLOAT
8396 {"asin", 1, 1, f_asin}, /* WJMc */
8397#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008398 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008399 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008400 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008401 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008402 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008403 {"assert_notequal", 2, 3, f_assert_notequal},
8404 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008405 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008406#ifdef FEAT_FLOAT
8407 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008408 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008411 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"bufexists", 1, 1, f_bufexists},
8413 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8414 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8415 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8416 {"buflisted", 1, 1, f_buflisted},
8417 {"bufloaded", 1, 1, f_bufloaded},
8418 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008419 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"bufwinnr", 1, 1, f_bufwinnr},
8421 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008422 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008423 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008424 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008425#ifdef FEAT_FLOAT
8426 {"ceil", 1, 1, f_ceil},
8427#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008428#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008429 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008430 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8431 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008432 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008433 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008434 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008435 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008436 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008437 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008438 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008439 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008440 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8441 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008442 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008443 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008444#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008445 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008446 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008448 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008450#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008451 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008452 {"complete_add", 1, 1, f_complete_add},
8453 {"complete_check", 0, 0, f_complete_check},
8454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008456 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008457#ifdef FEAT_FLOAT
8458 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008459 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008460#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008461 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008463 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008464 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008465 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008467 {"diff_filler", 1, 1, f_diff_filler},
8468 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008469 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008471 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 {"eventhandler", 0, 0, f_eventhandler},
8473 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008474 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008476#ifdef FEAT_FLOAT
8477 {"exp", 1, 1, f_exp},
8478#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008479 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008480 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008481 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8483 {"filereadable", 1, 1, f_filereadable},
8484 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008485 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008486 {"finddir", 1, 3, f_finddir},
8487 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008488#ifdef FEAT_FLOAT
8489 {"float2nr", 1, 1, f_float2nr},
8490 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008491 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008492#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008493 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {"fnamemodify", 2, 2, f_fnamemodify},
8495 {"foldclosed", 1, 1, f_foldclosed},
8496 {"foldclosedend", 1, 1, f_foldclosedend},
8497 {"foldlevel", 1, 1, f_foldlevel},
8498 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008499 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008501 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008502 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008503 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008504 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008505 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"getchar", 0, 1, f_getchar},
8507 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008508 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {"getcmdline", 0, 0, f_getcmdline},
8510 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008511 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008512 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008513 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008514 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008515 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008516 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {"getfsize", 1, 1, f_getfsize},
8518 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008519 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008520 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008521 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008522 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008523 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008524 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008525 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008526 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008528 {"gettabvar", 2, 3, f_gettabvar},
8529 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 {"getwinposx", 0, 0, f_getwinposx},
8531 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008532 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008533 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008534 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008535 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008537 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008538 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008539 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8541 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8542 {"histadd", 2, 2, f_histadd},
8543 {"histdel", 1, 2, f_histdel},
8544 {"histget", 1, 2, f_histget},
8545 {"histnr", 1, 1, f_histnr},
8546 {"hlID", 1, 1, f_hlID},
8547 {"hlexists", 1, 1, f_hlexists},
8548 {"hostname", 0, 0, f_hostname},
8549 {"iconv", 3, 3, f_iconv},
8550 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008551 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008552 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008554 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 {"inputrestore", 0, 0, f_inputrestore},
8556 {"inputsave", 0, 0, f_inputsave},
8557 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008558 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008559 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008561 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008562#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8563 {"isnan", 1, 1, f_isnan},
8564#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008565 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008566#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008567 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008568 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008569 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008570 {"job_start", 1, 2, f_job_start},
8571 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008572 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008573#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008574 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008575 {"js_decode", 1, 1, f_js_decode},
8576 {"js_encode", 1, 1, f_js_encode},
8577 {"json_decode", 1, 1, f_json_decode},
8578 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008579 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008581 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 {"libcall", 3, 3, f_libcall},
8583 {"libcallnr", 3, 3, f_libcallnr},
8584 {"line", 1, 1, f_line},
8585 {"line2byte", 1, 1, f_line2byte},
8586 {"lispindent", 1, 1, f_lispindent},
8587 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008588#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008589 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008590 {"log10", 1, 1, f_log10},
8591#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008592#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008593 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008594#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008595 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008596 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008597 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008598 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008599 {"matchadd", 2, 5, f_matchadd},
8600 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008601 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008602 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008603 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008604 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008605 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008606 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008607 {"max", 1, 1, f_max},
8608 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008609#ifdef vim_mkdir
8610 {"mkdir", 1, 3, f_mkdir},
8611#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008612 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008613#ifdef FEAT_MZSCHEME
8614 {"mzeval", 1, 1, f_mzeval},
8615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008617 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008618 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008619 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008620#ifdef FEAT_PERL
8621 {"perleval", 1, 1, f_perleval},
8622#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008623#ifdef FEAT_FLOAT
8624 {"pow", 2, 2, f_pow},
8625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008627 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008628 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008629#ifdef FEAT_PYTHON3
8630 {"py3eval", 1, 1, f_py3eval},
8631#endif
8632#ifdef FEAT_PYTHON
8633 {"pyeval", 1, 1, f_pyeval},
8634#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008635 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008636 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008637 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008638#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008639 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008640#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008641 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 {"remote_expr", 2, 3, f_remote_expr},
8643 {"remote_foreground", 1, 1, f_remote_foreground},
8644 {"remote_peek", 1, 2, f_remote_peek},
8645 {"remote_read", 1, 1, f_remote_read},
8646 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008647 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008649 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008651 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008652#ifdef FEAT_FLOAT
8653 {"round", 1, 1, f_round},
8654#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008655 {"screenattr", 2, 2, f_screenattr},
8656 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008657 {"screencol", 0, 0, f_screencol},
8658 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008659 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008660 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008661 {"searchpair", 3, 7, f_searchpair},
8662 {"searchpairpos", 3, 7, f_searchpairpos},
8663 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 {"server2client", 2, 2, f_server2client},
8665 {"serverlist", 0, 0, f_serverlist},
8666 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008667 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008669 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008671 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008672 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008673 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008674 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008676 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008677 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008679#ifdef FEAT_CRYPT
8680 {"sha256", 1, 1, f_sha256},
8681#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008682 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008683 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008685#ifdef FEAT_FLOAT
8686 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008687 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008688#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008689 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008690 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008691 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008692 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008693 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008694#ifdef FEAT_FLOAT
8695 {"sqrt", 1, 1, f_sqrt},
8696 {"str2float", 1, 1, f_str2float},
8697#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008698 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008699 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008700 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008701 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702#ifdef HAVE_STRFTIME
8703 {"strftime", 1, 2, f_strftime},
8704#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008705 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {"strlen", 1, 1, f_strlen},
8709 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008710 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008712 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008713 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 {"substitute", 4, 4, f_substitute},
8715 {"synID", 3, 3, f_synID},
8716 {"synIDattr", 2, 3, f_synIDattr},
8717 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008718 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008719 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008720 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008721 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008722 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008723 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008724 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008725 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008726 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008727#ifdef FEAT_FLOAT
8728 {"tan", 1, 1, f_tan},
8729 {"tanh", 1, 1, f_tanh},
8730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008732 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8733 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008734 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8735#ifdef FEAT_JOB_CHANNEL
8736 {"test_null_channel", 0, 0, f_test_null_channel},
8737#endif
8738 {"test_null_dict", 0, 0, f_test_null_dict},
8739#ifdef FEAT_JOB_CHANNEL
8740 {"test_null_job", 0, 0, f_test_null_job},
8741#endif
8742 {"test_null_list", 0, 0, f_test_null_list},
8743 {"test_null_partial", 0, 0, f_test_null_partial},
8744 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008745#ifdef FEAT_TIMERS
8746 {"timer_start", 2, 3, f_timer_start},
8747 {"timer_stop", 1, 1, f_timer_stop},
8748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {"tolower", 1, 1, f_tolower},
8750 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008751 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008752#ifdef FEAT_FLOAT
8753 {"trunc", 1, 1, f_trunc},
8754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008756 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008757 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008758 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008759 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 {"virtcol", 1, 1, f_virtcol},
8761 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008762 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008763 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008764 {"win_getid", 0, 2, f_win_getid},
8765 {"win_gotoid", 1, 1, f_win_gotoid},
8766 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8767 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 {"winbufnr", 1, 1, f_winbufnr},
8769 {"wincol", 0, 0, f_wincol},
8770 {"winheight", 1, 1, f_winheight},
8771 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008772 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008774 {"winrestview", 1, 1, f_winrestview},
8775 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008777 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008778 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008779 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780};
8781
8782#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8783
8784/*
8785 * Function given to ExpandGeneric() to obtain the list of internal
8786 * or user defined function names.
8787 */
8788 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008789get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
8791 static int intidx = -1;
8792 char_u *name;
8793
8794 if (idx == 0)
8795 intidx = -1;
8796 if (intidx < 0)
8797 {
8798 name = get_user_func_name(xp, idx);
8799 if (name != NULL)
8800 return name;
8801 }
8802 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8803 {
8804 STRCPY(IObuff, functions[intidx].f_name);
8805 STRCAT(IObuff, "(");
8806 if (functions[intidx].f_max_argc == 0)
8807 STRCAT(IObuff, ")");
8808 return IObuff;
8809 }
8810
8811 return NULL;
8812}
8813
8814/*
8815 * Function given to ExpandGeneric() to obtain the list of internal or
8816 * user defined variable or function names.
8817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008819get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 static int intidx = -1;
8822 char_u *name;
8823
8824 if (idx == 0)
8825 intidx = -1;
8826 if (intidx < 0)
8827 {
8828 name = get_function_name(xp, idx);
8829 if (name != NULL)
8830 return name;
8831 }
8832 return get_user_var_name(xp, ++intidx);
8833}
8834
8835#endif /* FEAT_CMDL_COMPL */
8836
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008837#if defined(EBCDIC) || defined(PROTO)
8838/*
8839 * Compare struct fst by function name.
8840 */
8841 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008842compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008843{
8844 struct fst *p1 = (struct fst *)s1;
8845 struct fst *p2 = (struct fst *)s2;
8846
8847 return STRCMP(p1->f_name, p2->f_name);
8848}
8849
8850/*
8851 * Sort the function table by function name.
8852 * The sorting of the table above is ASCII dependant.
8853 * On machines using EBCDIC we have to sort it.
8854 */
8855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008856sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008857{
8858 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8859
8860 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8861}
8862#endif
8863
8864
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865/*
8866 * Find internal function in table above.
8867 * Return index, or -1 if not found
8868 */
8869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008870find_internal_func(
8871 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
8873 int first = 0;
8874 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8875 int cmp;
8876 int x;
8877
8878 /*
8879 * Find the function name in the table. Binary search.
8880 */
8881 while (first <= last)
8882 {
8883 x = first + ((unsigned)(last - first) >> 1);
8884 cmp = STRCMP(name, functions[x].f_name);
8885 if (cmp < 0)
8886 last = x - 1;
8887 else if (cmp > 0)
8888 first = x + 1;
8889 else
8890 return x;
8891 }
8892 return -1;
8893}
8894
8895/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008896 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8897 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008898 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8899 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008900 */
8901 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008902deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008903{
Bram Moolenaar33570922005-01-25 22:26:29 +00008904 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008905 int cc;
8906
Bram Moolenaar65639032016-03-16 21:40:30 +01008907 if (partialp != NULL)
8908 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008909
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008910 cc = name[*lenp];
8911 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008912 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008913 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008914 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008915 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008916 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008917 {
8918 *lenp = 0;
8919 return (char_u *)""; /* just in case */
8920 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008921 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008922 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008923 }
8924
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008925 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8926 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008927 partial_T *pt = v->di_tv.vval.v_partial;
8928
8929 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008930 {
8931 *lenp = 0;
8932 return (char_u *)""; /* just in case */
8933 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008934 if (partialp != NULL)
8935 *partialp = pt;
8936 *lenp = (int)STRLEN(pt->pt_name);
8937 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008938 }
8939
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008940 return name;
8941}
8942
8943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 * Allocate a variable for the result of a function.
8945 * Return OK or FAIL.
8946 */
8947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008948get_func_tv(
8949 char_u *name, /* name of the function */
8950 int len, /* length of "name" */
8951 typval_T *rettv,
8952 char_u **arg, /* argument, pointing to the '(' */
8953 linenr_T firstline, /* first line of range */
8954 linenr_T lastline, /* last line of range */
8955 int *doesrange, /* return: function handled range */
8956 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008957 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008958 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959{
8960 char_u *argp;
8961 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008962 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 int argcount = 0; /* number of arguments found */
8964
8965 /*
8966 * Get the arguments.
8967 */
8968 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008969 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 {
8971 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8972 if (*argp == ')' || *argp == ',' || *argp == NUL)
8973 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8975 {
8976 ret = FAIL;
8977 break;
8978 }
8979 ++argcount;
8980 if (*argp != ',')
8981 break;
8982 }
8983 if (*argp == ')')
8984 ++argp;
8985 else
8986 ret = FAIL;
8987
8988 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008989 {
8990 int i = 0;
8991
8992 if (get_vim_var_nr(VV_TESTING))
8993 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008994 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008995 * what variables are used on the call stack. */
8996 if (funcargs.ga_itemsize == 0)
8997 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8998 for (i = 0; i < argcount; ++i)
8999 if (ga_grow(&funcargs, 1) == OK)
9000 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9001 &argvars[i];
9002 }
9003
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009005 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009006
9007 funcargs.ga_len -= i;
9008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 {
9011 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009012 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009014 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016
9017 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
9020 *arg = skipwhite(argp);
9021 return ret;
9022}
9023
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009024#define ERROR_UNKNOWN 0
9025#define ERROR_TOOMANY 1
9026#define ERROR_TOOFEW 2
9027#define ERROR_SCRIPT 3
9028#define ERROR_DICT 4
9029#define ERROR_NONE 5
9030#define ERROR_OTHER 6
9031#define FLEN_FIXED 40
9032
9033/*
9034 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9035 * Change <SNR>123_name() to K_SNR 123_name().
9036 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9037 * (slow).
9038 */
9039 static char_u *
9040fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9041{
9042 int llen;
9043 char_u *fname;
9044 int i;
9045
9046 llen = eval_fname_script(name);
9047 if (llen > 0)
9048 {
9049 fname_buf[0] = K_SPECIAL;
9050 fname_buf[1] = KS_EXTRA;
9051 fname_buf[2] = (int)KE_SNR;
9052 i = 3;
9053 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9054 {
9055 if (current_SID <= 0)
9056 *error = ERROR_SCRIPT;
9057 else
9058 {
9059 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9060 i = (int)STRLEN(fname_buf);
9061 }
9062 }
9063 if (i + STRLEN(name + llen) < FLEN_FIXED)
9064 {
9065 STRCPY(fname_buf + i, name + llen);
9066 fname = fname_buf;
9067 }
9068 else
9069 {
9070 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9071 if (fname == NULL)
9072 *error = ERROR_OTHER;
9073 else
9074 {
9075 *tofree = fname;
9076 mch_memmove(fname, fname_buf, (size_t)i);
9077 STRCPY(fname + i, name + llen);
9078 }
9079 }
9080 }
9081 else
9082 fname = name;
9083 return fname;
9084}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
9086/*
9087 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009088 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009089 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009092call_func(
9093 char_u *funcname, /* name of the function */
9094 int len, /* length of "name" */
9095 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009096 int argcount_in, /* number of "argvars" */
9097 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009098 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009099 linenr_T firstline, /* first line of range */
9100 linenr_T lastline, /* last line of range */
9101 int *doesrange, /* return: function handled range */
9102 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009103 partial_T *partial, /* optional, can be NULL */
9104 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105{
9106 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 int error = ERROR_NONE;
9108 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009111 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009113 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009114 int argcount = argcount_in;
9115 typval_T *argvars = argvars_in;
9116 dict_T *selfdict = selfdict_in;
9117 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9118 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009119
9120 /* Make a copy of the name, if it comes from a funcref variable it could
9121 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009122 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009123 if (name == NULL)
9124 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009126 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127
9128 *doesrange = FALSE;
9129
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009130 if (partial != NULL)
9131 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009132 /* When the function has a partial with a dict and there is a dict
9133 * argument, use the dict argument. That is backwards compatible.
9134 * When the dict was bound explicitly use the one from the partial. */
9135 if (partial->pt_dict != NULL
9136 && (selfdict_in == NULL || !partial->pt_auto))
9137 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009138 if (error == ERROR_NONE && partial->pt_argc > 0)
9139 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009140 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9141 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9142 for (i = 0; i < argcount_in; ++i)
9143 argv[i + argv_clear] = argvars_in[i];
9144 argvars = argv;
9145 argcount = partial->pt_argc + argcount_in;
9146 }
9147 }
9148
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149
9150 /* execute the function if no errors detected and executing */
9151 if (evaluate && error == ERROR_NONE)
9152 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009153 char_u *rfname = fname;
9154
9155 /* Ignore "g:" before a function name. */
9156 if (fname[0] == 'g' && fname[1] == ':')
9157 rfname = fname + 2;
9158
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009159 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9160 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 error = ERROR_UNKNOWN;
9162
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009163 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 {
9165 /*
9166 * User defined function.
9167 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009168 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009169
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009171 /* Trigger FuncUndefined event, may load the function. */
9172 if (fp == NULL
9173 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009174 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009175 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009177 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009178 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 }
9180#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009181 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009182 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009183 {
9184 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009185 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009186 }
9187
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 if (fp != NULL)
9189 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009190 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009192 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009194 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009196 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009197 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 else
9199 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009200 int did_save_redo = FALSE;
9201
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 /*
9203 * Call the user function.
9204 * Save and restore search patterns, script variables and
9205 * redo buffer.
9206 */
9207 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009208#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009209 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009210#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009211 {
9212 saveRedobuff();
9213 did_save_redo = TRUE;
9214 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009215 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009217 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009218 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9219 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9220 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009221 /* Function was unreferenced while being used, free it
9222 * now. */
9223 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009224 if (did_save_redo)
9225 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 restore_search_patterns();
9227 error = ERROR_NONE;
9228 }
9229 }
9230 }
9231 else
9232 {
9233 /*
9234 * Find the function name in the table, call its implementation.
9235 */
9236 i = find_internal_func(fname);
9237 if (i >= 0)
9238 {
9239 if (argcount < functions[i].f_min_argc)
9240 error = ERROR_TOOFEW;
9241 else if (argcount > functions[i].f_max_argc)
9242 error = ERROR_TOOMANY;
9243 else
9244 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009245 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009246 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 error = ERROR_NONE;
9248 }
9249 }
9250 }
9251 /*
9252 * The function call (or "FuncUndefined" autocommand sequence) might
9253 * have been aborted by an error, an interrupt, or an explicitly thrown
9254 * exception that has not been caught so far. This situation can be
9255 * tested for by calling aborting(). For an error in an internal
9256 * function or for the "E132" error in call_user_func(), however, the
9257 * throw point at which the "force_abort" flag (temporarily reset by
9258 * emsg()) is normally updated has not been reached yet. We need to
9259 * update that flag first to make aborting() reliable.
9260 */
9261 update_force_abort();
9262 }
9263 if (error == ERROR_NONE)
9264 ret = OK;
9265
9266 /*
9267 * Report an error unless the argument evaluation or function call has been
9268 * cancelled due to an aborting error, an interrupt, or an exception.
9269 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009270 if (!aborting())
9271 {
9272 switch (error)
9273 {
9274 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009275 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009276 break;
9277 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009278 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009279 break;
9280 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009281 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009282 name);
9283 break;
9284 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009285 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009286 name);
9287 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009288 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009289 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009290 name);
9291 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009292 }
9293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009295 while (argv_clear > 0)
9296 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009297 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009298 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299
9300 return ret;
9301}
9302
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009303/*
9304 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009305 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009306 */
9307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009308emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009309{
9310 char_u *p;
9311
9312 if (*name == K_SPECIAL)
9313 p = concat_str((char_u *)"<SNR>", name + 3);
9314 else
9315 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009316 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009317 if (p != name)
9318 vim_free(p);
9319}
9320
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009321/*
9322 * Return TRUE for a non-zero Number and a non-empty String.
9323 */
9324 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009325non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009326{
9327 return ((argvars[0].v_type == VAR_NUMBER
9328 && argvars[0].vval.v_number != 0)
9329 || (argvars[0].v_type == VAR_STRING
9330 && argvars[0].vval.v_string != NULL
9331 && *argvars[0].vval.v_string != NUL));
9332}
9333
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334/*********************************************
9335 * Implementation of the built-in functions
9336 */
9337
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009338#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009339static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009340
9341/*
9342 * Get the float value of "argvars[0]" into "f".
9343 * Returns FAIL when the argument is not a Number or Float.
9344 */
9345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009346get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009347{
9348 if (argvars[0].v_type == VAR_FLOAT)
9349 {
9350 *f = argvars[0].vval.v_float;
9351 return OK;
9352 }
9353 if (argvars[0].v_type == VAR_NUMBER)
9354 {
9355 *f = (float_T)argvars[0].vval.v_number;
9356 return OK;
9357 }
9358 EMSG(_("E808: Number or Float required"));
9359 return FAIL;
9360}
9361
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009362/*
9363 * "abs(expr)" function
9364 */
9365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009366f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009367{
9368 if (argvars[0].v_type == VAR_FLOAT)
9369 {
9370 rettv->v_type = VAR_FLOAT;
9371 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9372 }
9373 else
9374 {
9375 varnumber_T n;
9376 int error = FALSE;
9377
9378 n = get_tv_number_chk(&argvars[0], &error);
9379 if (error)
9380 rettv->vval.v_number = -1;
9381 else if (n > 0)
9382 rettv->vval.v_number = n;
9383 else
9384 rettv->vval.v_number = -n;
9385 }
9386}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009387
9388/*
9389 * "acos()" function
9390 */
9391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009392f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009393{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009394 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009395
9396 rettv->v_type = VAR_FLOAT;
9397 if (get_float_arg(argvars, &f) == OK)
9398 rettv->vval.v_float = acos(f);
9399 else
9400 rettv->vval.v_float = 0.0;
9401}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009402#endif
9403
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009405 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 */
9407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009408f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
Bram Moolenaar33570922005-01-25 22:26:29 +00009410 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009413 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009415 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009416 && !tv_check_lock(l->lv_lock,
9417 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009418 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009420 }
9421 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009422 EMSG(_(e_listreq));
9423}
9424
9425/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009426 * "and(expr, expr)" function
9427 */
9428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009429f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009430{
9431 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9432 & get_tv_number_chk(&argvars[1], NULL);
9433}
9434
9435/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009436 * "append(lnum, string/list)" function
9437 */
9438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009439f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009440{
9441 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009442 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009443 list_T *l = NULL;
9444 listitem_T *li = NULL;
9445 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009446 long added = 0;
9447
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009448 /* When coming here from Insert mode, sync undo, so that this can be
9449 * undone separately from what was previously inserted. */
9450 if (u_sync_once == 2)
9451 {
9452 u_sync_once = 1; /* notify that u_sync() was called */
9453 u_sync(TRUE);
9454 }
9455
Bram Moolenaar0d660222005-01-07 21:51:51 +00009456 lnum = get_tv_lnum(argvars);
9457 if (lnum >= 0
9458 && lnum <= curbuf->b_ml.ml_line_count
9459 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009460 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009461 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009462 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009463 l = argvars[1].vval.v_list;
9464 if (l == NULL)
9465 return;
9466 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009467 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009468 for (;;)
9469 {
9470 if (l == NULL)
9471 tv = &argvars[1]; /* append a string */
9472 else if (li == NULL)
9473 break; /* end of list */
9474 else
9475 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 line = get_tv_string_chk(tv);
9477 if (line == NULL) /* type error */
9478 {
9479 rettv->vval.v_number = 1; /* Failed */
9480 break;
9481 }
9482 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009483 ++added;
9484 if (l == NULL)
9485 break;
9486 li = li->li_next;
9487 }
9488
9489 appended_lines_mark(lnum, added);
9490 if (curwin->w_cursor.lnum > lnum)
9491 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009493 else
9494 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495}
9496
9497/*
9498 * "argc()" function
9499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504}
9505
9506/*
9507 * "argidx()" function
9508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009510f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513}
9514
9515/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009516 * "arglistid()" function
9517 */
9518 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009519f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009520{
9521 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009522
9523 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009524 wp = find_tabwin(&argvars[0], &argvars[1]);
9525 if (wp != NULL)
9526 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009527}
9528
9529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 * "argv(nr)" function
9531 */
9532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009533f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535 int idx;
9536
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009537 if (argvars[0].v_type != VAR_UNKNOWN)
9538 {
9539 idx = get_tv_number_chk(&argvars[0], NULL);
9540 if (idx >= 0 && idx < ARGCOUNT)
9541 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9542 else
9543 rettv->vval.v_string = NULL;
9544 rettv->v_type = VAR_STRING;
9545 }
9546 else if (rettv_list_alloc(rettv) == OK)
9547 for (idx = 0; idx < ARGCOUNT; ++idx)
9548 list_append_string(rettv->vval.v_list,
9549 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550}
9551
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009552typedef enum
9553{
9554 ASSERT_EQUAL,
9555 ASSERT_NOTEQUAL,
9556 ASSERT_MATCH,
9557 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009558 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009559} assert_type_T;
9560
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009561static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009562static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009563static void assert_error(garray_T *gap);
9564static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009565
9566/*
9567 * Prepare "gap" for an assert error and add the sourcing position.
9568 */
9569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009570prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009571{
9572 char buf[NUMBUFLEN];
9573
9574 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009575 if (sourcing_name != NULL)
9576 {
9577 ga_concat(gap, sourcing_name);
9578 if (sourcing_lnum > 0)
9579 ga_concat(gap, (char_u *)" ");
9580 }
9581 if (sourcing_lnum > 0)
9582 {
9583 sprintf(buf, "line %ld", (long)sourcing_lnum);
9584 ga_concat(gap, (char_u *)buf);
9585 }
9586 if (sourcing_name != NULL || sourcing_lnum > 0)
9587 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009588}
9589
9590/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009591 * Append "str" to "gap", escaping unprintable characters.
9592 * Changes NL to \n, CR to \r, etc.
9593 */
9594 static void
9595ga_concat_esc(garray_T *gap, char_u *str)
9596{
9597 char_u *p;
9598 char_u buf[NUMBUFLEN];
9599
Bram Moolenaarf1551962016-03-15 12:55:58 +01009600 if (str == NULL)
9601 {
9602 ga_concat(gap, (char_u *)"NULL");
9603 return;
9604 }
9605
Bram Moolenaar23689172016-02-15 22:37:37 +01009606 for (p = str; *p != NUL; ++p)
9607 switch (*p)
9608 {
9609 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9610 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9611 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9612 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9613 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9614 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9615 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9616 default:
9617 if (*p < ' ')
9618 {
9619 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9620 ga_concat(gap, buf);
9621 }
9622 else
9623 ga_append(gap, *p);
9624 break;
9625 }
9626}
9627
9628/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009629 * Fill "gap" with information about an assert error.
9630 */
9631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632fill_assert_error(
9633 garray_T *gap,
9634 typval_T *opt_msg_tv,
9635 char_u *exp_str,
9636 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009637 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009638 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009639{
9640 char_u numbuf[NUMBUFLEN];
9641 char_u *tofree;
9642
9643 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9644 {
9645 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9646 vim_free(tofree);
9647 }
9648 else
9649 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009650 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009651 ga_concat(gap, (char_u *)"Pattern ");
9652 else
9653 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009654 if (exp_str == NULL)
9655 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009656 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009657 vim_free(tofree);
9658 }
9659 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009660 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009661 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009662 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009663 else if (atype == ASSERT_NOTMATCH)
9664 ga_concat(gap, (char_u *)" does match ");
9665 else if (atype == ASSERT_NOTEQUAL)
9666 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009667 else
9668 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009669 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009670 vim_free(tofree);
9671 }
9672}
Bram Moolenaar43345542015-11-29 17:35:35 +01009673
9674/*
9675 * Add an assert error to v:errors.
9676 */
9677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009678assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009679{
9680 struct vimvar *vp = &vimvars[VV_ERRORS];
9681
9682 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9683 /* Make sure v:errors is a list. */
9684 set_vim_var_list(VV_ERRORS, list_alloc());
9685 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9686}
9687
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009688 static void
9689assert_equal_common(typval_T *argvars, assert_type_T atype)
9690{
9691 garray_T ga;
9692
9693 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9694 != (atype == ASSERT_EQUAL))
9695 {
9696 prepare_assert_error(&ga);
9697 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9698 atype);
9699 assert_error(&ga);
9700 ga_clear(&ga);
9701 }
9702}
9703
Bram Moolenaar43345542015-11-29 17:35:35 +01009704/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009705 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009706 */
9707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009708f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009709{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009710 assert_equal_common(argvars, ASSERT_EQUAL);
9711}
Bram Moolenaar43345542015-11-29 17:35:35 +01009712
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009713/*
9714 * "assert_notequal(expected, actual[, msg])" function
9715 */
9716 static void
9717f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9718{
9719 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009720}
9721
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009722/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009723 * "assert_exception(string[, msg])" function
9724 */
9725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009726f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009727{
9728 garray_T ga;
9729 char *error;
9730
9731 error = (char *)get_tv_string_chk(&argvars[0]);
9732 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9733 {
9734 prepare_assert_error(&ga);
9735 ga_concat(&ga, (char_u *)"v:exception is not set");
9736 assert_error(&ga);
9737 ga_clear(&ga);
9738 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009739 else if (error != NULL
9740 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009741 {
9742 prepare_assert_error(&ga);
9743 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009744 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009745 assert_error(&ga);
9746 ga_clear(&ga);
9747 }
9748}
9749
9750/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009751 * "assert_fails(cmd [, error])" function
9752 */
9753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009754f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009755{
9756 char_u *cmd = get_tv_string_chk(&argvars[0]);
9757 garray_T ga;
9758
9759 called_emsg = FALSE;
9760 suppress_errthrow = TRUE;
9761 emsg_silent = TRUE;
9762 do_cmdline_cmd(cmd);
9763 if (!called_emsg)
9764 {
9765 prepare_assert_error(&ga);
9766 ga_concat(&ga, (char_u *)"command did not fail: ");
9767 ga_concat(&ga, cmd);
9768 assert_error(&ga);
9769 ga_clear(&ga);
9770 }
9771 else if (argvars[1].v_type != VAR_UNKNOWN)
9772 {
9773 char_u buf[NUMBUFLEN];
9774 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9775
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009776 if (error == NULL
9777 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009778 {
9779 prepare_assert_error(&ga);
9780 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009781 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009782 assert_error(&ga);
9783 ga_clear(&ga);
9784 }
9785 }
9786
9787 called_emsg = FALSE;
9788 suppress_errthrow = FALSE;
9789 emsg_silent = FALSE;
9790 emsg_on_display = FALSE;
9791 set_vim_var_string(VV_ERRMSG, NULL, 0);
9792}
9793
9794/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009795 * Common for assert_true() and assert_false().
9796 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009798assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009799{
9800 int error = FALSE;
9801 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009802
Bram Moolenaar37127922016-02-06 20:29:28 +01009803 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009804 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009805 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009806 if (argvars[0].v_type != VAR_NUMBER
9807 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9808 || error)
9809 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009810 prepare_assert_error(&ga);
9811 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009812 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009813 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009814 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009815 ga_clear(&ga);
9816 }
9817}
9818
9819/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009820 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009821 */
9822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009823f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009824{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009825 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009826}
9827
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009828 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009829assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009830{
9831 garray_T ga;
9832 char_u buf1[NUMBUFLEN];
9833 char_u buf2[NUMBUFLEN];
9834 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9835 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9836
Bram Moolenaar72188e92016-03-28 22:48:29 +02009837 if (pat == NULL || text == NULL)
9838 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009839 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009840 {
9841 prepare_assert_error(&ga);
9842 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009843 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009844 assert_error(&ga);
9845 ga_clear(&ga);
9846 }
9847}
9848
9849/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009850 * "assert_match(pattern, actual[, msg])" function
9851 */
9852 static void
9853f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9854{
9855 assert_match_common(argvars, ASSERT_MATCH);
9856}
9857
9858/*
9859 * "assert_notmatch(pattern, actual[, msg])" function
9860 */
9861 static void
9862f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9863{
9864 assert_match_common(argvars, ASSERT_NOTMATCH);
9865}
9866
9867/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009868 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009869 */
9870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009871f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009872{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009873 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009874}
9875
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009876#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009877/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009878 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009879 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009881f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009882{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009883 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009884
9885 rettv->v_type = VAR_FLOAT;
9886 if (get_float_arg(argvars, &f) == OK)
9887 rettv->vval.v_float = asin(f);
9888 else
9889 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009890}
9891
9892/*
9893 * "atan()" function
9894 */
9895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009896f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009897{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009898 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009899
9900 rettv->v_type = VAR_FLOAT;
9901 if (get_float_arg(argvars, &f) == OK)
9902 rettv->vval.v_float = atan(f);
9903 else
9904 rettv->vval.v_float = 0.0;
9905}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009906
9907/*
9908 * "atan2()" function
9909 */
9910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009911f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009912{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009913 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009914
9915 rettv->v_type = VAR_FLOAT;
9916 if (get_float_arg(argvars, &fx) == OK
9917 && get_float_arg(&argvars[1], &fy) == OK)
9918 rettv->vval.v_float = atan2(fx, fy);
9919 else
9920 rettv->vval.v_float = 0.0;
9921}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009922#endif
9923
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924/*
9925 * "browse(save, title, initdir, default)" function
9926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009928f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929{
9930#ifdef FEAT_BROWSE
9931 int save;
9932 char_u *title;
9933 char_u *initdir;
9934 char_u *defname;
9935 char_u buf[NUMBUFLEN];
9936 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009937 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009939 save = get_tv_number_chk(&argvars[0], &error);
9940 title = get_tv_string_chk(&argvars[1]);
9941 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9942 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009944 if (error || title == NULL || initdir == NULL || defname == NULL)
9945 rettv->vval.v_string = NULL;
9946 else
9947 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009948 do_browse(save ? BROWSE_SAVE : 0,
9949 title, defname, NULL, initdir, NULL, curbuf);
9950#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009951 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009952#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009954}
9955
9956/*
9957 * "browsedir(title, initdir)" function
9958 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009960f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009961{
9962#ifdef FEAT_BROWSE
9963 char_u *title;
9964 char_u *initdir;
9965 char_u buf[NUMBUFLEN];
9966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009967 title = get_tv_string_chk(&argvars[0]);
9968 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009970 if (title == NULL || initdir == NULL)
9971 rettv->vval.v_string = NULL;
9972 else
9973 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009974 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979}
9980
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009981static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009982
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983/*
9984 * Find a buffer by number or exact name.
9985 */
9986 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009987find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
9989 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009991 if (avar->v_type == VAR_NUMBER)
9992 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009993 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009995 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009996 if (buf == NULL)
9997 {
9998 /* No full path name match, try a match with a URL or a "nofile"
9999 * buffer, these don't use the full path. */
10000 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10001 if (buf->b_fname != NULL
10002 && (path_with_url(buf->b_fname)
10003#ifdef FEAT_QUICKFIX
10004 || bt_nofile(buf)
10005#endif
10006 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010007 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010008 break;
10009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 }
10011 return buf;
10012}
10013
10014/*
10015 * "bufexists(expr)" function
10016 */
10017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010018f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021}
10022
10023/*
10024 * "buflisted(expr)" function
10025 */
10026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010027f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
10029 buf_T *buf;
10030
10031 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033}
10034
10035/*
10036 * "bufloaded(expr)" function
10037 */
10038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010039f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040{
10041 buf_T *buf;
10042
10043 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010044 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045}
10046
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010047 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010048buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010050 int save_magic;
10051 char_u *save_cpo;
10052 buf_T *buf;
10053
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10055 save_magic = p_magic;
10056 p_magic = TRUE;
10057 save_cpo = p_cpo;
10058 p_cpo = (char_u *)"";
10059
10060 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010061 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062
10063 p_magic = save_magic;
10064 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010065 return buf;
10066}
10067
10068/*
10069 * Get buffer by number or pattern.
10070 */
10071 static buf_T *
10072get_buf_tv(typval_T *tv, int curtab_only)
10073{
10074 char_u *name = tv->vval.v_string;
10075 buf_T *buf;
10076
10077 if (tv->v_type == VAR_NUMBER)
10078 return buflist_findnr((int)tv->vval.v_number);
10079 if (tv->v_type != VAR_STRING)
10080 return NULL;
10081 if (name == NULL || *name == NUL)
10082 return curbuf;
10083 if (name[0] == '$' && name[1] == NUL)
10084 return lastbuf;
10085
10086 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
10088 /* If not found, try expanding the name, like done for bufexists(). */
10089 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091
10092 return buf;
10093}
10094
10095/*
10096 * "bufname(expr)" function
10097 */
10098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010099f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101 buf_T *buf;
10102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010103 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010105 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 --emsg_off;
10112}
10113
10114/*
10115 * "bufnr(expr)" function
10116 */
10117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010118f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
10120 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010121 int error = FALSE;
10122 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010124 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010126 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010127 --emsg_off;
10128
10129 /* If the buffer isn't found and the second argument is not zero create a
10130 * new buffer. */
10131 if (buf == NULL
10132 && argvars[1].v_type != VAR_UNKNOWN
10133 && get_tv_number_chk(&argvars[1], &error) != 0
10134 && !error
10135 && (name = get_tv_string_chk(&argvars[0])) != NULL
10136 && !error)
10137 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10138
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010142 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143}
10144
10145/*
10146 * "bufwinnr(nr)" function
10147 */
10148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010149f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150{
10151#ifdef FEAT_WINDOWS
10152 win_T *wp;
10153 int winnr = 0;
10154#endif
10155 buf_T *buf;
10156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010157 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010159 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160#ifdef FEAT_WINDOWS
10161 for (wp = firstwin; wp; wp = wp->w_next)
10162 {
10163 ++winnr;
10164 if (wp->w_buffer == buf)
10165 break;
10166 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010167 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010169 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170#endif
10171 --emsg_off;
10172}
10173
10174/*
10175 * "byte2line(byte)" function
10176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010178f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182#else
10183 long boff = 0;
10184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 (linenr_T)0, &boff);
10191#endif
10192}
10193
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010195byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010196{
10197#ifdef FEAT_MBYTE
10198 char_u *t;
10199#endif
10200 char_u *str;
10201 long idx;
10202
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010203 str = get_tv_string_chk(&argvars[0]);
10204 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010206 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010207 return;
10208
10209#ifdef FEAT_MBYTE
10210 t = str;
10211 for ( ; idx > 0; idx--)
10212 {
10213 if (*t == NUL) /* EOL reached */
10214 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010215 if (enc_utf8 && comp)
10216 t += utf_ptr2len(t);
10217 else
10218 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010219 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010220 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010221#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010222 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010223 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010224#endif
10225}
10226
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010227/*
10228 * "byteidx()" function
10229 */
10230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010231f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010232{
10233 byteidx(argvars, rettv, FALSE);
10234}
10235
10236/*
10237 * "byteidxcomp()" function
10238 */
10239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010240f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010241{
10242 byteidx(argvars, rettv, TRUE);
10243}
10244
Bram Moolenaardb913952012-06-29 12:54:53 +020010245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010246func_call(
10247 char_u *name,
10248 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010249 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010250 dict_T *selfdict,
10251 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010252{
10253 listitem_T *item;
10254 typval_T argv[MAX_FUNC_ARGS + 1];
10255 int argc = 0;
10256 int dummy;
10257 int r = 0;
10258
10259 for (item = args->vval.v_list->lv_first; item != NULL;
10260 item = item->li_next)
10261 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010262 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010263 {
10264 EMSG(_("E699: Too many arguments"));
10265 break;
10266 }
10267 /* Make a copy of each argument. This is needed to be able to set
10268 * v_lock to VAR_FIXED in the copy without changing the original list.
10269 */
10270 copy_tv(&item->li_tv, &argv[argc++]);
10271 }
10272
10273 if (item == NULL)
10274 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10275 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010276 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010277
10278 /* Free the arguments. */
10279 while (argc > 0)
10280 clear_tv(&argv[--argc]);
10281
10282 return r;
10283}
10284
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010285/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010286 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010287 */
10288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010289f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010290{
10291 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010292 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010294
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010295 if (argvars[1].v_type != VAR_LIST)
10296 {
10297 EMSG(_(e_listreq));
10298 return;
10299 }
10300 if (argvars[1].vval.v_list == NULL)
10301 return;
10302
10303 if (argvars[0].v_type == VAR_FUNC)
10304 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010305 else if (argvars[0].v_type == VAR_PARTIAL)
10306 {
10307 partial = argvars[0].vval.v_partial;
10308 func = partial->pt_name;
10309 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010310 else
10311 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010312 if (*func == NUL)
10313 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010314
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 if (argvars[2].v_type != VAR_UNKNOWN)
10316 {
10317 if (argvars[2].v_type != VAR_DICT)
10318 {
10319 EMSG(_(e_dictreq));
10320 return;
10321 }
10322 selfdict = argvars[2].vval.v_dict;
10323 }
10324
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010325 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010326}
10327
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010328#ifdef FEAT_FLOAT
10329/*
10330 * "ceil({float})" function
10331 */
10332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010333f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010334{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010335 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010336
10337 rettv->v_type = VAR_FLOAT;
10338 if (get_float_arg(argvars, &f) == OK)
10339 rettv->vval.v_float = ceil(f);
10340 else
10341 rettv->vval.v_float = 0.0;
10342}
10343#endif
10344
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010345#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010346/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010347 * "ch_close()" function
10348 */
10349 static void
10350f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10351{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010352 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010353
10354 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010355 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010356 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010357 channel_clear(channel);
10358 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010359}
10360
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010361/*
10362 * "ch_getbufnr()" function
10363 */
10364 static void
10365f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10366{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010367 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010368
10369 rettv->vval.v_number = -1;
10370 if (channel != NULL)
10371 {
10372 char_u *what = get_tv_string(&argvars[1]);
10373 int part;
10374
10375 if (STRCMP(what, "err") == 0)
10376 part = PART_ERR;
10377 else if (STRCMP(what, "out") == 0)
10378 part = PART_OUT;
10379 else if (STRCMP(what, "in") == 0)
10380 part = PART_IN;
10381 else
10382 part = PART_SOCK;
10383 if (channel->ch_part[part].ch_buffer != NULL)
10384 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10385 }
10386}
10387
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010388/*
10389 * "ch_getjob()" function
10390 */
10391 static void
10392f_ch_getjob(typval_T *argvars, typval_T *rettv)
10393{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010394 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010395
10396 if (channel != NULL)
10397 {
10398 rettv->v_type = VAR_JOB;
10399 rettv->vval.v_job = channel->ch_job;
10400 if (channel->ch_job != NULL)
10401 ++channel->ch_job->jv_refcount;
10402 }
10403}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010404
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010405/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010406 * "ch_info()" function
10407 */
10408 static void
10409f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10410{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010411 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010412
10413 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10414 channel_info(channel, rettv->vval.v_dict);
10415}
10416
10417/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010418 * "ch_log()" function
10419 */
10420 static void
10421f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10422{
10423 char_u *msg = get_tv_string(&argvars[0]);
10424 channel_T *channel = NULL;
10425
10426 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010427 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010428
10429 ch_log(channel, (char *)msg);
10430}
10431
10432/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010433 * "ch_logfile()" function
10434 */
10435 static void
10436f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10437{
10438 char_u *fname;
10439 char_u *opt = (char_u *)"";
10440 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010441
10442 fname = get_tv_string(&argvars[0]);
10443 if (argvars[1].v_type == VAR_STRING)
10444 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010445 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010446}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010447
10448/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010449 * "ch_open()" function
10450 */
10451 static void
10452f_ch_open(typval_T *argvars, typval_T *rettv)
10453{
Bram Moolenaar77073442016-02-13 23:23:53 +010010454 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010455 if (check_restricted() || check_secure())
10456 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010457 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010458}
10459
10460/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010461 * "ch_read()" function
10462 */
10463 static void
10464f_ch_read(typval_T *argvars, typval_T *rettv)
10465{
10466 common_channel_read(argvars, rettv, FALSE);
10467}
10468
10469/*
10470 * "ch_readraw()" function
10471 */
10472 static void
10473f_ch_readraw(typval_T *argvars, typval_T *rettv)
10474{
10475 common_channel_read(argvars, rettv, TRUE);
10476}
10477
10478/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010479 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010480 */
10481 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010482f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10483{
10484 ch_expr_common(argvars, rettv, TRUE);
10485}
10486
10487/*
10488 * "ch_sendexpr()" function
10489 */
10490 static void
10491f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10492{
10493 ch_expr_common(argvars, rettv, FALSE);
10494}
10495
10496/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010497 * "ch_evalraw()" function
10498 */
10499 static void
10500f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10501{
10502 ch_raw_common(argvars, rettv, TRUE);
10503}
10504
10505/*
10506 * "ch_sendraw()" function
10507 */
10508 static void
10509f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10510{
10511 ch_raw_common(argvars, rettv, FALSE);
10512}
10513
10514/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010515 * "ch_setoptions()" function
10516 */
10517 static void
10518f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10519{
10520 channel_T *channel;
10521 jobopt_T opt;
10522
Bram Moolenaar437905c2016-04-26 19:01:05 +020010523 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010524 if (channel == NULL)
10525 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010526 clear_job_options(&opt);
10527 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010528 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10529 channel_set_options(channel, &opt);
10530 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010531}
10532
10533/*
10534 * "ch_status()" function
10535 */
10536 static void
10537f_ch_status(typval_T *argvars, typval_T *rettv)
10538{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010539 channel_T *channel;
10540
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010541 /* return an empty string by default */
10542 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010543 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010544
Bram Moolenaar437905c2016-04-26 19:01:05 +020010545 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010546 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010547}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010548#endif
10549
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010550/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010551 * "changenr()" function
10552 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010554f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010555{
10556 rettv->vval.v_number = curbuf->b_u_seq_cur;
10557}
10558
10559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 * "char2nr(string)" function
10561 */
10562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010563f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564{
10565#ifdef FEAT_MBYTE
10566 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010567 {
10568 int utf8 = 0;
10569
10570 if (argvars[1].v_type != VAR_UNKNOWN)
10571 utf8 = get_tv_number_chk(&argvars[1], NULL);
10572
10573 if (utf8)
10574 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10575 else
10576 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 else
10579#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581}
10582
10583/*
10584 * "cindent(lnum)" function
10585 */
10586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010587f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588{
10589#ifdef FEAT_CINDENT
10590 pos_T pos;
10591 linenr_T lnum;
10592
10593 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010594 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10596 {
10597 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 curwin->w_cursor = pos;
10600 }
10601 else
10602#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604}
10605
10606/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010607 * "clearmatches()" function
10608 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010610f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010611{
10612#ifdef FEAT_SEARCH_EXTRA
10613 clear_matches(curwin);
10614#endif
10615}
10616
10617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 * "col(string)" function
10619 */
10620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010621f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
10623 colnr_T col = 0;
10624 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010625 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010627 fp = var2fpos(&argvars[0], FALSE, &fnum);
10628 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 {
10630 if (fp->col == MAXCOL)
10631 {
10632 /* '> can be MAXCOL, get the length of the line then */
10633 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010634 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 else
10636 col = MAXCOL;
10637 }
10638 else
10639 {
10640 col = fp->col + 1;
10641#ifdef FEAT_VIRTUALEDIT
10642 /* col(".") when the cursor is on the NUL at the end of the line
10643 * because of "coladd" can be seen as an extra column. */
10644 if (virtual_active() && fp == &curwin->w_cursor)
10645 {
10646 char_u *p = ml_get_cursor();
10647
10648 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10649 curwin->w_virtcol - curwin->w_cursor.coladd))
10650 {
10651# ifdef FEAT_MBYTE
10652 int l;
10653
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010654 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 col += l;
10656# else
10657 if (*p != NUL && p[1] == NUL)
10658 ++col;
10659# endif
10660 }
10661 }
10662#endif
10663 }
10664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666}
10667
Bram Moolenaar572cb562005-08-05 21:35:02 +000010668#if defined(FEAT_INS_EXPAND)
10669/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010670 * "complete()" function
10671 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010673f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010674{
10675 int startcol;
10676
10677 if ((State & INSERT) == 0)
10678 {
10679 EMSG(_("E785: complete() can only be used in Insert mode"));
10680 return;
10681 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010682
10683 /* Check for undo allowed here, because if something was already inserted
10684 * the line was already saved for undo and this check isn't done. */
10685 if (!undo_allowed())
10686 return;
10687
Bram Moolenaarade00832006-03-10 21:46:58 +000010688 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10689 {
10690 EMSG(_(e_invarg));
10691 return;
10692 }
10693
10694 startcol = get_tv_number_chk(&argvars[0], NULL);
10695 if (startcol <= 0)
10696 return;
10697
10698 set_completion(startcol - 1, argvars[1].vval.v_list);
10699}
10700
10701/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010702 * "complete_add()" function
10703 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010705f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010706{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010707 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010708}
10709
10710/*
10711 * "complete_check()" function
10712 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010714f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010715{
10716 int saved = RedrawingDisabled;
10717
10718 RedrawingDisabled = 0;
10719 ins_compl_check_keys(0);
10720 rettv->vval.v_number = compl_interrupted;
10721 RedrawingDisabled = saved;
10722}
10723#endif
10724
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725/*
10726 * "confirm(message, buttons[, default [, type]])" function
10727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010729f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730{
10731#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10732 char_u *message;
10733 char_u *buttons = NULL;
10734 char_u buf[NUMBUFLEN];
10735 char_u buf2[NUMBUFLEN];
10736 int def = 1;
10737 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010738 char_u *typestr;
10739 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 message = get_tv_string_chk(&argvars[0]);
10742 if (message == NULL)
10743 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010744 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010746 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10747 if (buttons == NULL)
10748 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010749 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010751 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010752 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010754 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10755 if (typestr == NULL)
10756 error = TRUE;
10757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 switch (TOUPPER_ASC(*typestr))
10760 {
10761 case 'E': type = VIM_ERROR; break;
10762 case 'Q': type = VIM_QUESTION; break;
10763 case 'I': type = VIM_INFO; break;
10764 case 'W': type = VIM_WARNING; break;
10765 case 'G': type = VIM_GENERIC; break;
10766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767 }
10768 }
10769 }
10770 }
10771
10772 if (buttons == NULL || *buttons == NUL)
10773 buttons = (char_u *)_("&Ok");
10774
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010775 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010776 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010777 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778#endif
10779}
10780
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010781/*
10782 * "copy()" function
10783 */
10784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010785f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010786{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010787 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010788}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010790#ifdef FEAT_FLOAT
10791/*
10792 * "cos()" function
10793 */
10794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010795f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010796{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010797 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010798
10799 rettv->v_type = VAR_FLOAT;
10800 if (get_float_arg(argvars, &f) == OK)
10801 rettv->vval.v_float = cos(f);
10802 else
10803 rettv->vval.v_float = 0.0;
10804}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010805
10806/*
10807 * "cosh()" function
10808 */
10809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010810f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010811{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010812 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010813
10814 rettv->v_type = VAR_FLOAT;
10815 if (get_float_arg(argvars, &f) == OK)
10816 rettv->vval.v_float = cosh(f);
10817 else
10818 rettv->vval.v_float = 0.0;
10819}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010820#endif
10821
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010823 * "count()" function
10824 */
10825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010826f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010827{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010828 long n = 0;
10829 int ic = FALSE;
10830
Bram Moolenaare9a41262005-01-15 22:18:47 +000010831 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010832 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010833 listitem_T *li;
10834 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010836
Bram Moolenaare9a41262005-01-15 22:18:47 +000010837 if ((l = argvars[0].vval.v_list) != NULL)
10838 {
10839 li = l->lv_first;
10840 if (argvars[2].v_type != VAR_UNKNOWN)
10841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 int error = FALSE;
10843
10844 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 if (argvars[3].v_type != VAR_UNKNOWN)
10846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010847 idx = get_tv_number_chk(&argvars[3], &error);
10848 if (!error)
10849 {
10850 li = list_find(l, idx);
10851 if (li == NULL)
10852 EMSGN(_(e_listidx), idx);
10853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010854 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010855 if (error)
10856 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010857 }
10858
10859 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010860 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010861 ++n;
10862 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010864 else if (argvars[0].v_type == VAR_DICT)
10865 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010866 int todo;
10867 dict_T *d;
10868 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010869
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010870 if ((d = argvars[0].vval.v_dict) != NULL)
10871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 int error = FALSE;
10873
Bram Moolenaare9a41262005-01-15 22:18:47 +000010874 if (argvars[2].v_type != VAR_UNKNOWN)
10875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010876 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010877 if (argvars[3].v_type != VAR_UNKNOWN)
10878 EMSG(_(e_invarg));
10879 }
10880
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010881 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010882 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010883 {
10884 if (!HASHITEM_EMPTY(hi))
10885 {
10886 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010887 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010888 ++n;
10889 }
10890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010891 }
10892 }
10893 else
10894 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010895 rettv->vval.v_number = n;
10896}
10897
10898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10900 *
10901 * Checks the existence of a cscope connection.
10902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010904f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905{
10906#ifdef FEAT_CSCOPE
10907 int num = 0;
10908 char_u *dbpath = NULL;
10909 char_u *prepend = NULL;
10910 char_u buf[NUMBUFLEN];
10911
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010912 if (argvars[0].v_type != VAR_UNKNOWN
10913 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915 num = (int)get_tv_number(&argvars[0]);
10916 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010917 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 }
10920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010921 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922#endif
10923}
10924
10925/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010926 * "cursor(lnum, col)" function, or
10927 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010929 * Moves the cursor to the specified line and column.
10930 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010933f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934{
10935 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010936#ifdef FEAT_VIRTUALEDIT
10937 long coladd = 0;
10938#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010939 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010941 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010942 if (argvars[1].v_type == VAR_UNKNOWN)
10943 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010944 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010945 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010946
Bram Moolenaar493c1782014-05-28 14:34:46 +020010947 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010948 {
10949 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010950 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010951 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010952 line = pos.lnum;
10953 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010954#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010955 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010956#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010957 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010958 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010959 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010960 set_curswant = FALSE;
10961 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010962 }
10963 else
10964 {
10965 line = get_tv_lnum(argvars);
10966 col = get_tv_number_chk(&argvars[1], NULL);
10967#ifdef FEAT_VIRTUALEDIT
10968 if (argvars[2].v_type != VAR_UNKNOWN)
10969 coladd = get_tv_number_chk(&argvars[2], NULL);
10970#endif
10971 }
10972 if (line < 0 || col < 0
10973#ifdef FEAT_VIRTUALEDIT
10974 || coladd < 0
10975#endif
10976 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010977 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 if (line > 0)
10979 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 if (col > 0)
10981 curwin->w_cursor.col = col - 1;
10982#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010983 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984#endif
10985
10986 /* Make sure the cursor is in a valid position. */
10987 check_cursor();
10988#ifdef FEAT_MBYTE
10989 /* Correct cursor for multi-byte character. */
10990 if (has_mbyte)
10991 mb_adjust_cursor();
10992#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010993
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010994 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010995 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996}
10997
10998/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010999 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000 */
11001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011002f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011004 int noref = 0;
11005
11006 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011007 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011008 if (noref < 0 || noref > 1)
11009 EMSG(_(e_invarg));
11010 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011011 {
11012 current_copyID += COPYID_INC;
11013 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015}
11016
11017/*
11018 * "delete()" function
11019 */
11020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011021f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011023 char_u nbuf[NUMBUFLEN];
11024 char_u *name;
11025 char_u *flags;
11026
11027 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011029 return;
11030
11031 name = get_tv_string(&argvars[0]);
11032 if (name == NULL || *name == NUL)
11033 {
11034 EMSG(_(e_invarg));
11035 return;
11036 }
11037
11038 if (argvars[1].v_type != VAR_UNKNOWN)
11039 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011041 flags = (char_u *)"";
11042
11043 if (*flags == NUL)
11044 /* delete a file */
11045 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11046 else if (STRCMP(flags, "d") == 0)
11047 /* delete an empty directory */
11048 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11049 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011050 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011051 rettv->vval.v_number = delete_recursive(name);
11052 else
11053 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054}
11055
11056/*
11057 * "did_filetype()" function
11058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011060f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011061{
11062#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064#endif
11065}
11066
11067/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011068 * "diff_filler()" function
11069 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011071f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011072{
11073#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011075#endif
11076}
11077
11078/*
11079 * "diff_hlID()" function
11080 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011082f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011083{
11084#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011085 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011086 static linenr_T prev_lnum = 0;
11087 static int changedtick = 0;
11088 static int fnum = 0;
11089 static int change_start = 0;
11090 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011091 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011092 int filler_lines;
11093 int col;
11094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 if (lnum < 0) /* ignore type error in {lnum} arg */
11096 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011097 if (lnum != prev_lnum
11098 || changedtick != curbuf->b_changedtick
11099 || fnum != curbuf->b_fnum)
11100 {
11101 /* New line, buffer, change: need to get the values. */
11102 filler_lines = diff_check(curwin, lnum);
11103 if (filler_lines < 0)
11104 {
11105 if (filler_lines == -1)
11106 {
11107 change_start = MAXCOL;
11108 change_end = -1;
11109 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11110 hlID = HLF_ADD; /* added line */
11111 else
11112 hlID = HLF_CHD; /* changed line */
11113 }
11114 else
11115 hlID = HLF_ADD; /* added line */
11116 }
11117 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011118 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011119 prev_lnum = lnum;
11120 changedtick = curbuf->b_changedtick;
11121 fnum = curbuf->b_fnum;
11122 }
11123
11124 if (hlID == HLF_CHD || hlID == HLF_TXD)
11125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011127 if (col >= change_start && col <= change_end)
11128 hlID = HLF_TXD; /* changed text */
11129 else
11130 hlID = HLF_CHD; /* changed line */
11131 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011132 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011133#endif
11134}
11135
11136/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011137 * "empty({expr})" function
11138 */
11139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011140f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011141{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011142 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011143
11144 switch (argvars[0].v_type)
11145 {
11146 case VAR_STRING:
11147 case VAR_FUNC:
11148 n = argvars[0].vval.v_string == NULL
11149 || *argvars[0].vval.v_string == NUL;
11150 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011151 case VAR_PARTIAL:
11152 n = FALSE;
11153 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011154 case VAR_NUMBER:
11155 n = argvars[0].vval.v_number == 0;
11156 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011157 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011158#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011159 n = argvars[0].vval.v_float == 0.0;
11160 break;
11161#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011162 case VAR_LIST:
11163 n = argvars[0].vval.v_list == NULL
11164 || argvars[0].vval.v_list->lv_first == NULL;
11165 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011166 case VAR_DICT:
11167 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011168 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011169 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011170 case VAR_SPECIAL:
11171 n = argvars[0].vval.v_number != VVAL_TRUE;
11172 break;
11173
Bram Moolenaar835dc632016-02-07 14:27:38 +010011174 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011175#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011176 n = argvars[0].vval.v_job == NULL
11177 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11178 break;
11179#endif
11180 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011181#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011182 n = argvars[0].vval.v_channel == NULL
11183 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011184 break;
11185#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011186 case VAR_UNKNOWN:
11187 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11188 n = TRUE;
11189 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011190 }
11191
11192 rettv->vval.v_number = n;
11193}
11194
11195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 * "escape({string}, {chars})" function
11197 */
11198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011199f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200{
11201 char_u buf[NUMBUFLEN];
11202
Bram Moolenaar758711c2005-02-02 23:11:38 +000011203 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11204 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011206}
11207
11208/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011209 * "eval()" function
11210 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011212f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011213{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011214 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 s = get_tv_string_chk(&argvars[0]);
11217 if (s != NULL)
11218 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011219
Bram Moolenaar615b9972015-01-14 17:15:05 +010011220 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011221 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11222 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011223 if (p != NULL && !aborting())
11224 EMSG2(_(e_invexpr2), p);
11225 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011227 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011229 else if (*s != NUL)
11230 EMSG(_(e_trailing));
11231}
11232
11233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011234 * "eventhandler()" function
11235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011237f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240}
11241
11242/*
11243 * "executable()" function
11244 */
11245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011246f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011248 char_u *name = get_tv_string(&argvars[0]);
11249
11250 /* Check in $PATH and also check directly if there is a directory name. */
11251 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11252 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011253}
11254
11255/*
11256 * "exepath()" function
11257 */
11258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011259f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011260{
11261 char_u *p = NULL;
11262
Bram Moolenaarb5971142015-03-21 17:32:19 +010011263 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011264 rettv->v_type = VAR_STRING;
11265 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266}
11267
11268/*
11269 * "exists()" function
11270 */
11271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011272f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273{
11274 char_u *p;
11275 char_u *name;
11276 int n = FALSE;
11277 int len = 0;
11278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280 if (*p == '$') /* environment variable */
11281 {
11282 /* first try "normal" environment variables (fast) */
11283 if (mch_getenv(p + 1) != NULL)
11284 n = TRUE;
11285 else
11286 {
11287 /* try expanding things like $VIM and ${HOME} */
11288 p = expand_env_save(p);
11289 if (p != NULL && *p != '$')
11290 n = TRUE;
11291 vim_free(p);
11292 }
11293 }
11294 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011297 if (*skipwhite(p) != NUL)
11298 n = FALSE; /* trailing garbage */
11299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 else if (*p == '*') /* internal or user defined function */
11301 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011302 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303 }
11304 else if (*p == ':')
11305 {
11306 n = cmd_exists(p + 1);
11307 }
11308 else if (*p == '#')
11309 {
11310#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011311 if (p[1] == '#')
11312 n = autocmd_supported(p + 2);
11313 else
11314 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315#endif
11316 }
11317 else /* internal variable */
11318 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011319 char_u *tofree;
11320 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011322 /* get_name_len() takes care of expanding curly braces */
11323 name = p;
11324 len = get_name_len(&p, &tofree, TRUE, FALSE);
11325 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011327 if (tofree != NULL)
11328 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011329 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011330 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011332 /* handle d.key, l[idx], f(expr) */
11333 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11334 if (n)
11335 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 }
11337 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011338 if (*p != NUL)
11339 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011341 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 }
11343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011344 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345}
11346
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011347#ifdef FEAT_FLOAT
11348/*
11349 * "exp()" function
11350 */
11351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011352f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011353{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011354 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011355
11356 rettv->v_type = VAR_FLOAT;
11357 if (get_float_arg(argvars, &f) == OK)
11358 rettv->vval.v_float = exp(f);
11359 else
11360 rettv->vval.v_float = 0.0;
11361}
11362#endif
11363
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364/*
11365 * "expand()" function
11366 */
11367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011368f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369{
11370 char_u *s;
11371 int len;
11372 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011373 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011376 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011379 if (argvars[1].v_type != VAR_UNKNOWN
11380 && argvars[2].v_type != VAR_UNKNOWN
11381 && get_tv_number_chk(&argvars[2], &error)
11382 && !error)
11383 {
11384 rettv->v_type = VAR_LIST;
11385 rettv->vval.v_list = NULL;
11386 }
11387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 if (*s == '%' || *s == '#' || *s == '<')
11390 {
11391 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011392 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011394 if (rettv->v_type == VAR_LIST)
11395 {
11396 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11397 list_append_string(rettv->vval.v_list, result, -1);
11398 else
11399 vim_free(result);
11400 }
11401 else
11402 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 }
11404 else
11405 {
11406 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011407 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011408 if (argvars[1].v_type != VAR_UNKNOWN
11409 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011410 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 if (!error)
11412 {
11413 ExpandInit(&xpc);
11414 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011415 if (p_wic)
11416 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011417 if (rettv->v_type == VAR_STRING)
11418 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11419 options, WILD_ALL);
11420 else if (rettv_list_alloc(rettv) != FAIL)
11421 {
11422 int i;
11423
11424 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11425 for (i = 0; i < xpc.xp_numfiles; i++)
11426 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11427 ExpandCleanup(&xpc);
11428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011429 }
11430 else
11431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 }
11433}
11434
11435/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011436 * Go over all entries in "d2" and add them to "d1".
11437 * When "action" is "error" then a duplicate key is an error.
11438 * When "action" is "force" then a duplicate key is overwritten.
11439 * Otherwise duplicate keys are ignored ("action" is "keep").
11440 */
11441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011442dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011443{
11444 dictitem_T *di1;
11445 hashitem_T *hi2;
11446 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011447 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011448
11449 todo = (int)d2->dv_hashtab.ht_used;
11450 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11451 {
11452 if (!HASHITEM_EMPTY(hi2))
11453 {
11454 --todo;
11455 di1 = dict_find(d1, hi2->hi_key, -1);
11456 if (d1->dv_scope != 0)
11457 {
11458 /* Disallow replacing a builtin function in l: and g:.
11459 * Check the key to be valid when adding to any
11460 * scope. */
11461 if (d1->dv_scope == VAR_DEF_SCOPE
11462 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11463 && var_check_func_name(hi2->hi_key,
11464 di1 == NULL))
11465 break;
11466 if (!valid_varname(hi2->hi_key))
11467 break;
11468 }
11469 if (di1 == NULL)
11470 {
11471 di1 = dictitem_copy(HI2DI(hi2));
11472 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11473 dictitem_free(di1);
11474 }
11475 else if (*action == 'e')
11476 {
11477 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11478 break;
11479 }
11480 else if (*action == 'f' && HI2DI(hi2) != di1)
11481 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011482 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11483 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011484 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011485 clear_tv(&di1->di_tv);
11486 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11487 }
11488 }
11489 }
11490}
11491
11492/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011493 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011494 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011495 */
11496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011497f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011498{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011499 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011500
Bram Moolenaare9a41262005-01-15 22:18:47 +000011501 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011502 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011503 list_T *l1, *l2;
11504 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011505 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011507
Bram Moolenaare9a41262005-01-15 22:18:47 +000011508 l1 = argvars[0].vval.v_list;
11509 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011510 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011511 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 {
11513 if (argvars[2].v_type != VAR_UNKNOWN)
11514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011515 before = get_tv_number_chk(&argvars[2], &error);
11516 if (error)
11517 return; /* type error; errmsg already given */
11518
Bram Moolenaar758711c2005-02-02 23:11:38 +000011519 if (before == l1->lv_len)
11520 item = NULL;
11521 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011522 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011523 item = list_find(l1, before);
11524 if (item == NULL)
11525 {
11526 EMSGN(_(e_listidx), before);
11527 return;
11528 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011529 }
11530 }
11531 else
11532 item = NULL;
11533 list_extend(l1, l2, item);
11534
Bram Moolenaare9a41262005-01-15 22:18:47 +000011535 copy_tv(&argvars[0], rettv);
11536 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011537 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011538 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11539 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011540 dict_T *d1, *d2;
11541 char_u *action;
11542 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011543
11544 d1 = argvars[0].vval.v_dict;
11545 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011546 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011547 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548 {
11549 /* Check the third argument. */
11550 if (argvars[2].v_type != VAR_UNKNOWN)
11551 {
11552 static char *(av[]) = {"keep", "force", "error"};
11553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 action = get_tv_string_chk(&argvars[2]);
11555 if (action == NULL)
11556 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011557 for (i = 0; i < 3; ++i)
11558 if (STRCMP(action, av[i]) == 0)
11559 break;
11560 if (i == 3)
11561 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011562 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 return;
11564 }
11565 }
11566 else
11567 action = (char_u *)"force";
11568
Bram Moolenaara9922d62013-05-30 13:01:18 +020011569 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011570
Bram Moolenaare9a41262005-01-15 22:18:47 +000011571 copy_tv(&argvars[0], rettv);
11572 }
11573 }
11574 else
11575 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011576}
11577
11578/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011579 * "feedkeys()" function
11580 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011582f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011583{
11584 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011585 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011586 char_u *keys, *flags;
11587 char_u nbuf[NUMBUFLEN];
11588 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011589 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011590 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011591 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011592
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011593 /* This is not allowed in the sandbox. If the commands would still be
11594 * executed in the sandbox it would be OK, but it probably happens later,
11595 * when "sandbox" is no longer set. */
11596 if (check_secure())
11597 return;
11598
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011599 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011600
11601 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011602 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011603 flags = get_tv_string_buf(&argvars[1], nbuf);
11604 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011605 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011606 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011607 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011608 case 'n': remap = FALSE; break;
11609 case 'm': remap = TRUE; break;
11610 case 't': typed = TRUE; break;
11611 case 'i': insert = TRUE; break;
11612 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011613 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011614 }
11615 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011616 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011617
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011618 if (*keys != NUL || execute)
11619 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011620 /* Need to escape K_SPECIAL and CSI before putting the string in the
11621 * typeahead buffer. */
11622 keys_esc = vim_strsave_escape_csi(keys);
11623 if (keys_esc != NULL)
11624 {
11625 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011626 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011627 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011628 if (vgetc_busy)
11629 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011630 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011631 {
11632 int save_msg_scroll = msg_scroll;
11633
11634 /* Avoid a 1 second delay when the keys start Insert mode. */
11635 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011636
Bram Moolenaar245c4102016-04-20 17:37:41 +020011637 if (!dangerous)
11638 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011639 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011640 if (!dangerous)
11641 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011642 msg_scroll |= save_msg_scroll;
11643 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011644 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011645 }
11646}
11647
11648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011649 * "filereadable()" function
11650 */
11651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011652f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011654 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 char_u *p;
11656 int n;
11657
Bram Moolenaarc236c162008-07-13 17:41:49 +000011658#ifndef O_NONBLOCK
11659# define O_NONBLOCK 0
11660#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011662 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11663 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 {
11665 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011666 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 }
11668 else
11669 n = FALSE;
11670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672}
11673
11674/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011675 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 * rights to write into.
11677 */
11678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011679f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011681 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682}
11683
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011684 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011685findfilendir(
11686 typval_T *argvars UNUSED,
11687 typval_T *rettv,
11688 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011689{
11690#ifdef FEAT_SEARCHPATH
11691 char_u *fname;
11692 char_u *fresult = NULL;
11693 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11694 char_u *p;
11695 char_u pathbuf[NUMBUFLEN];
11696 int count = 1;
11697 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011698 int error = FALSE;
11699#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011700
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011701 rettv->vval.v_string = NULL;
11702 rettv->v_type = VAR_STRING;
11703
11704#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011707 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11710 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011711 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011712 else
11713 {
11714 if (*p != NUL)
11715 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011718 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011719 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011720 }
11721
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011722 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11723 error = TRUE;
11724
11725 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011727 do
11728 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011729 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011730 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011731 fresult = find_file_in_path_option(first ? fname : NULL,
11732 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011733 0, first, path,
11734 find_what,
11735 curbuf->b_ffname,
11736 find_what == FINDFILE_DIR
11737 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011738 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011739
11740 if (fresult != NULL && rettv->v_type == VAR_LIST)
11741 list_append_string(rettv->vval.v_list, fresult, -1);
11742
11743 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011745
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011746 if (rettv->v_type == VAR_STRING)
11747 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011748#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011749}
11750
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011751static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11752static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011753
11754/*
11755 * Implementation of map() and filter().
11756 */
11757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011758filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011759{
11760 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011761 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011762 listitem_T *li, *nli;
11763 list_T *l = NULL;
11764 dictitem_T *di;
11765 hashtab_T *ht;
11766 hashitem_T *hi;
11767 dict_T *d = NULL;
11768 typval_T save_val;
11769 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011770 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011771 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011772 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011773 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011774 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011775 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011776 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011777
Bram Moolenaare9a41262005-01-15 22:18:47 +000011778 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011779 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011780 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011781 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011782 return;
11783 }
11784 else if (argvars[0].v_type == VAR_DICT)
11785 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011786 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011787 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011788 return;
11789 }
11790 else
11791 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011792 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011793 return;
11794 }
11795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011796 expr = get_tv_string_buf_chk(&argvars[1], buf);
11797 /* On type errors, the preceding call has already displayed an error
11798 * message. Avoid a misleading error message for an empty string that
11799 * was not passed as argument. */
11800 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 prepare_vimvar(VV_VAL, &save_val);
11803 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011804
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011805 /* We reset "did_emsg" to be able to detect whether an error
11806 * occurred during evaluation of the expression. */
11807 save_did_emsg = did_emsg;
11808 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011809
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011810 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011811 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 vimvars[VV_KEY].vv_type = VAR_STRING;
11814
11815 ht = &d->dv_hashtab;
11816 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011817 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011818 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 if (!HASHITEM_EMPTY(hi))
11821 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011822 int r;
11823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824 --todo;
11825 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011826 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011827 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11828 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011829 break;
11830 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011831 r = filter_map_one(&di->di_tv, expr, map, &rem);
11832 clear_tv(&vimvars[VV_KEY].vv_tv);
11833 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011834 break;
11835 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011836 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011837 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11838 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011839 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011840 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011841 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011842 }
11843 }
11844 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011845 }
11846 else
11847 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011848 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850 for (li = l->lv_first; li != NULL; li = nli)
11851 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011852 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011853 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011854 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011855 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011856 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011857 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011858 break;
11859 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011860 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011861 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011862 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011863 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011864
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011865 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011867
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011868 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011869 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870
11871 copy_tv(&argvars[0], rettv);
11872}
11873
11874 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011875filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011876{
Bram Moolenaar33570922005-01-25 22:26:29 +000011877 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011878 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011879 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011880
Bram Moolenaar33570922005-01-25 22:26:29 +000011881 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011882 s = expr;
11883 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011884 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011885 if (*s != NUL) /* check for trailing chars after expr */
11886 {
11887 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011888 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011889 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011890 }
11891 if (map)
11892 {
11893 /* map(): replace the list item value */
11894 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011895 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011896 *tv = rettv;
11897 }
11898 else
11899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011900 int error = FALSE;
11901
Bram Moolenaare9a41262005-01-15 22:18:47 +000011902 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011904 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011905 /* On type error, nothing has been removed; return FAIL to stop the
11906 * loop. The error message was given by get_tv_number_chk(). */
11907 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011908 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011909 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011910 retval = OK;
11911theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011912 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011913 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011914}
11915
11916/*
11917 * "filter()" function
11918 */
11919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011920f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011921{
11922 filter_map(argvars, rettv, FALSE);
11923}
11924
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011925/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011926 * "finddir({fname}[, {path}[, {count}]])" function
11927 */
11928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011929f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011930{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011931 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011932}
11933
11934/*
11935 * "findfile({fname}[, {path}[, {count}]])" function
11936 */
11937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011938f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011939{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011940 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011941}
11942
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011943#ifdef FEAT_FLOAT
11944/*
11945 * "float2nr({float})" function
11946 */
11947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011948f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011949{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011950 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011951
11952 if (get_float_arg(argvars, &f) == OK)
11953 {
11954 if (f < -0x7fffffff)
11955 rettv->vval.v_number = -0x7fffffff;
11956 else if (f > 0x7fffffff)
11957 rettv->vval.v_number = 0x7fffffff;
11958 else
11959 rettv->vval.v_number = (varnumber_T)f;
11960 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011961}
11962
11963/*
11964 * "floor({float})" function
11965 */
11966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011967f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011968{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011969 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011970
11971 rettv->v_type = VAR_FLOAT;
11972 if (get_float_arg(argvars, &f) == OK)
11973 rettv->vval.v_float = floor(f);
11974 else
11975 rettv->vval.v_float = 0.0;
11976}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011977
11978/*
11979 * "fmod()" function
11980 */
11981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011982f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011983{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011984 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011985
11986 rettv->v_type = VAR_FLOAT;
11987 if (get_float_arg(argvars, &fx) == OK
11988 && get_float_arg(&argvars[1], &fy) == OK)
11989 rettv->vval.v_float = fmod(fx, fy);
11990 else
11991 rettv->vval.v_float = 0.0;
11992}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011993#endif
11994
Bram Moolenaar0d660222005-01-07 21:51:51 +000011995/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011996 * "fnameescape({string})" function
11997 */
11998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011999f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012000{
12001 rettv->vval.v_string = vim_strsave_fnameescape(
12002 get_tv_string(&argvars[0]), FALSE);
12003 rettv->v_type = VAR_STRING;
12004}
12005
12006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 * "fnamemodify({fname}, {mods})" function
12008 */
12009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012010f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011{
12012 char_u *fname;
12013 char_u *mods;
12014 int usedlen = 0;
12015 int len;
12016 char_u *fbuf = NULL;
12017 char_u buf[NUMBUFLEN];
12018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012019 fname = get_tv_string_chk(&argvars[0]);
12020 mods = get_tv_string_buf_chk(&argvars[1], buf);
12021 if (fname == NULL || mods == NULL)
12022 fname = NULL;
12023 else
12024 {
12025 len = (int)STRLEN(fname);
12026 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012029 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012031 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012033 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 vim_free(fbuf);
12035}
12036
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012037static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038
12039/*
12040 * "foldclosed()" function
12041 */
12042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012043foldclosed_both(
12044 typval_T *argvars UNUSED,
12045 typval_T *rettv,
12046 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012047{
12048#ifdef FEAT_FOLDING
12049 linenr_T lnum;
12050 linenr_T first, last;
12051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12054 {
12055 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12056 {
12057 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 return;
12062 }
12063 }
12064#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066}
12067
12068/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012069 * "foldclosed()" function
12070 */
12071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012072f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073{
12074 foldclosed_both(argvars, rettv, FALSE);
12075}
12076
12077/*
12078 * "foldclosedend()" function
12079 */
12080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012081f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012082{
12083 foldclosed_both(argvars, rettv, TRUE);
12084}
12085
12086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012087 * "foldlevel()" function
12088 */
12089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012090f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091{
12092#ifdef FEAT_FOLDING
12093 linenr_T lnum;
12094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012095 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012097 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012099}
12100
12101/*
12102 * "foldtext()" function
12103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012105f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106{
12107#ifdef FEAT_FOLDING
12108 linenr_T lnum;
12109 char_u *s;
12110 char_u *r;
12111 int len;
12112 char *txt;
12113#endif
12114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012115 rettv->v_type = VAR_STRING;
12116 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012118 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12119 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12120 <= curbuf->b_ml.ml_line_count
12121 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 {
12123 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012124 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12125 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 {
12127 if (!linewhite(lnum))
12128 break;
12129 ++lnum;
12130 }
12131
12132 /* Find interesting text in this line. */
12133 s = skipwhite(ml_get(lnum));
12134 /* skip C comment-start */
12135 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012138 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012139 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012140 {
12141 s = skipwhite(ml_get(lnum + 1));
12142 if (*s == '*')
12143 s = skipwhite(s + 1);
12144 }
12145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 txt = _("+-%s%3ld lines: ");
12147 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012148 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149 + 20 /* for %3ld */
12150 + STRLEN(s))); /* concatenated */
12151 if (r != NULL)
12152 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012153 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12154 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12155 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156 len = (int)STRLEN(r);
12157 STRCAT(r, s);
12158 /* remove 'foldmarker' and 'commentstring' */
12159 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012160 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161 }
12162 }
12163#endif
12164}
12165
12166/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012167 * "foldtextresult(lnum)" function
12168 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012170f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012171{
12172#ifdef FEAT_FOLDING
12173 linenr_T lnum;
12174 char_u *text;
12175 char_u buf[51];
12176 foldinfo_T foldinfo;
12177 int fold_count;
12178#endif
12179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 rettv->v_type = VAR_STRING;
12181 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012182#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012183 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012184 /* treat illegal types and illegal string values for {lnum} the same */
12185 if (lnum < 0)
12186 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012187 fold_count = foldedCount(curwin, lnum, &foldinfo);
12188 if (fold_count > 0)
12189 {
12190 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12191 &foldinfo, buf);
12192 if (text == buf)
12193 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012195 }
12196#endif
12197}
12198
12199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 * "foreground()" function
12201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012203f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205#ifdef FEAT_GUI
12206 if (gui.in_use)
12207 gui_mch_set_foreground();
12208#else
12209# ifdef WIN32
12210 win32_set_foreground();
12211# endif
12212#endif
12213}
12214
12215/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012216 * "function()" function
12217 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012219f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012220{
12221 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012222 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012223 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012224 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012225
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012226 if (argvars[0].v_type == VAR_FUNC)
12227 {
12228 /* function(MyFunc, [arg], dict) */
12229 s = argvars[0].vval.v_string;
12230 }
12231 else if (argvars[0].v_type == VAR_PARTIAL
12232 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012233 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012234 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012235 arg_pt = argvars[0].vval.v_partial;
12236 s = arg_pt->pt_name;
12237 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012238 else
12239 {
12240 /* function('MyFunc', [arg], dict) */
12241 s = get_tv_string(&argvars[0]);
12242 use_string = TRUE;
12243 }
12244
12245 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012246 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012247 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012248 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12249 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012250 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012251 else
12252 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012253 int dict_idx = 0;
12254 int arg_idx = 0;
12255 list_T *list = NULL;
12256
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012257 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012258 {
12259 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012260 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012261
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012262 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12263 * also be called from another script. Using trans_function_name()
12264 * would also work, but some plugins depend on the name being
12265 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012266 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012267 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12268 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012269 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012270 STRCPY(name, sid_buf);
12271 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012272 }
12273 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012274 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012275 name = vim_strsave(s);
12276
12277 if (argvars[1].v_type != VAR_UNKNOWN)
12278 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012279 if (argvars[2].v_type != VAR_UNKNOWN)
12280 {
12281 /* function(name, [args], dict) */
12282 arg_idx = 1;
12283 dict_idx = 2;
12284 }
12285 else if (argvars[1].v_type == VAR_DICT)
12286 /* function(name, dict) */
12287 dict_idx = 1;
12288 else
12289 /* function(name, [args]) */
12290 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012291 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012292 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012293 if (argvars[dict_idx].v_type != VAR_DICT)
12294 {
12295 EMSG(_("E922: expected a dict"));
12296 vim_free(name);
12297 return;
12298 }
12299 if (argvars[dict_idx].vval.v_dict == NULL)
12300 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012301 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012302 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012303 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012304 if (argvars[arg_idx].v_type != VAR_LIST)
12305 {
12306 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12307 vim_free(name);
12308 return;
12309 }
12310 list = argvars[arg_idx].vval.v_list;
12311 if (list == NULL || list->lv_len == 0)
12312 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012313 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012314 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012315 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012316 {
12317 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012318
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012319 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012320 if (pt == NULL)
12321 vim_free(name);
12322 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012323 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012324 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012325 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012326 listitem_T *li;
12327 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012328 int arg_len = 0;
12329 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012330
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012331 if (arg_pt != NULL)
12332 arg_len = arg_pt->pt_argc;
12333 if (list != NULL)
12334 lv_len = list->lv_len;
12335 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012336 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012337 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012338 if (pt->pt_argv == NULL)
12339 {
12340 vim_free(pt);
12341 vim_free(name);
12342 return;
12343 }
12344 else
12345 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012346 for (i = 0; i < arg_len; i++)
12347 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12348 if (lv_len > 0)
12349 for (li = list->lv_first; li != NULL;
12350 li = li->li_next)
12351 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012352 }
12353 }
12354
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012355 /* For "function(dict.func, [], dict)" and "func" is a partial
12356 * use "dict". That is backwards compatible. */
12357 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012358 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012359 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012360 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12361 ++pt->pt_dict->dv_refcount;
12362 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012363 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012364 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012365 /* If the dict was bound automatically the result is also
12366 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012367 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012368 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012369 if (pt->pt_dict != NULL)
12370 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012371 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012372
12373 pt->pt_refcount = 1;
12374 pt->pt_name = name;
12375 func_ref(pt->pt_name);
12376 }
12377 rettv->v_type = VAR_PARTIAL;
12378 rettv->vval.v_partial = pt;
12379 }
12380 else
12381 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012382 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012383 rettv->v_type = VAR_FUNC;
12384 rettv->vval.v_string = name;
12385 func_ref(name);
12386 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012387 }
12388}
12389
12390/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012391 * "garbagecollect()" function
12392 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012394f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012395{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012396 /* This is postponed until we are back at the toplevel, because we may be
12397 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12398 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012399
12400 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12401 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012402}
12403
12404/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012405 * "get()" function
12406 */
12407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012408f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012409{
Bram Moolenaar33570922005-01-25 22:26:29 +000012410 listitem_T *li;
12411 list_T *l;
12412 dictitem_T *di;
12413 dict_T *d;
12414 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012415
Bram Moolenaare9a41262005-01-15 22:18:47 +000012416 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012417 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012418 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012420 int error = FALSE;
12421
12422 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12423 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012424 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012425 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012427 else if (argvars[0].v_type == VAR_DICT)
12428 {
12429 if ((d = argvars[0].vval.v_dict) != NULL)
12430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012431 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012432 if (di != NULL)
12433 tv = &di->di_tv;
12434 }
12435 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012436 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012437 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012438 partial_T *pt;
12439 partial_T fref_pt;
12440
12441 if (argvars[0].v_type == VAR_PARTIAL)
12442 pt = argvars[0].vval.v_partial;
12443 else
12444 {
12445 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12446 fref_pt.pt_name = argvars[0].vval.v_string;
12447 pt = &fref_pt;
12448 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012449
12450 if (pt != NULL)
12451 {
12452 char_u *what = get_tv_string(&argvars[1]);
12453
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012454 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012455 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012456 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012457 if (pt->pt_name == NULL)
12458 rettv->vval.v_string = NULL;
12459 else
12460 rettv->vval.v_string = vim_strsave(pt->pt_name);
12461 }
12462 else if (STRCMP(what, "dict") == 0)
12463 {
12464 rettv->v_type = VAR_DICT;
12465 rettv->vval.v_dict = pt->pt_dict;
12466 if (pt->pt_dict != NULL)
12467 ++pt->pt_dict->dv_refcount;
12468 }
12469 else if (STRCMP(what, "args") == 0)
12470 {
12471 rettv->v_type = VAR_LIST;
12472 if (rettv_list_alloc(rettv) == OK)
12473 {
12474 int i;
12475
12476 for (i = 0; i < pt->pt_argc; ++i)
12477 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12478 }
12479 }
12480 else
12481 EMSG2(_(e_invarg2), what);
12482 return;
12483 }
12484 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012485 else
12486 EMSG2(_(e_listdictarg), "get()");
12487
12488 if (tv == NULL)
12489 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012490 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012491 copy_tv(&argvars[2], rettv);
12492 }
12493 else
12494 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495}
12496
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012497static 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 +000012498
12499/*
12500 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012501 * Return a range (from start to end) of lines in rettv from the specified
12502 * buffer.
12503 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012504 */
12505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012506get_buffer_lines(
12507 buf_T *buf,
12508 linenr_T start,
12509 linenr_T end,
12510 int retlist,
12511 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012512{
12513 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012514
Bram Moolenaar959a1432013-12-14 12:17:38 +010012515 rettv->v_type = VAR_STRING;
12516 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012517 if (retlist && rettv_list_alloc(rettv) == FAIL)
12518 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012519
12520 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12521 return;
12522
12523 if (!retlist)
12524 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012525 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12526 p = ml_get_buf(buf, start, FALSE);
12527 else
12528 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012529 rettv->vval.v_string = vim_strsave(p);
12530 }
12531 else
12532 {
12533 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012534 return;
12535
12536 if (start < 1)
12537 start = 1;
12538 if (end > buf->b_ml.ml_line_count)
12539 end = buf->b_ml.ml_line_count;
12540 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012541 if (list_append_string(rettv->vval.v_list,
12542 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012543 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012544 }
12545}
12546
12547/*
12548 * "getbufline()" function
12549 */
12550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012551f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012552{
12553 linenr_T lnum;
12554 linenr_T end;
12555 buf_T *buf;
12556
12557 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12558 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012559 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012560 --emsg_off;
12561
Bram Moolenaar661b1822005-07-28 22:36:45 +000012562 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012563 if (argvars[2].v_type == VAR_UNKNOWN)
12564 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012565 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012566 end = get_tv_lnum_buf(&argvars[2], buf);
12567
Bram Moolenaar342337a2005-07-21 21:11:17 +000012568 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012569}
12570
Bram Moolenaar0d660222005-01-07 21:51:51 +000012571/*
12572 * "getbufvar()" function
12573 */
12574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012575f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012576{
12577 buf_T *buf;
12578 buf_T *save_curbuf;
12579 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012580 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012581 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012583 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12584 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012585 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012586 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012588 rettv->v_type = VAR_STRING;
12589 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012590
12591 if (buf != NULL && varname != NULL)
12592 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012593 /* set curbuf to be our buf, temporarily */
12594 save_curbuf = curbuf;
12595 curbuf = buf;
12596
Bram Moolenaar0d660222005-01-07 21:51:51 +000012597 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012598 {
12599 if (get_option_tv(&varname, rettv, TRUE) == OK)
12600 done = TRUE;
12601 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012602 else if (STRCMP(varname, "changedtick") == 0)
12603 {
12604 rettv->v_type = VAR_NUMBER;
12605 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012606 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012607 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012608 else
12609 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012610 /* Look up the variable. */
12611 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12612 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12613 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012614 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012615 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012616 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012617 done = TRUE;
12618 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012620
12621 /* restore previous notion of curbuf */
12622 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012623 }
12624
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012625 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12626 /* use the default value */
12627 copy_tv(&argvars[2], rettv);
12628
Bram Moolenaar0d660222005-01-07 21:51:51 +000012629 --emsg_off;
12630}
12631
12632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 * "getchar()" function
12634 */
12635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012636f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637{
12638 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012639 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012641 /* Position the cursor. Needed after a message that ends in a space. */
12642 windgoto(msg_row, msg_col);
12643
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644 ++no_mapping;
12645 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012646 for (;;)
12647 {
12648 if (argvars[0].v_type == VAR_UNKNOWN)
12649 /* getchar(): blocking wait. */
12650 n = safe_vgetc();
12651 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12652 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012653 n = vpeekc_any();
12654 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012655 /* illegal argument or getchar(0) and no char avail: return zero */
12656 n = 0;
12657 else
12658 /* getchar(0) and char avail: return char */
12659 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012660
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012661 if (n == K_IGNORE)
12662 continue;
12663 break;
12664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 --no_mapping;
12666 --allow_keys;
12667
Bram Moolenaar219b8702006-11-01 14:32:36 +000012668 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12669 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12670 vimvars[VV_MOUSE_COL].vv_nr = 0;
12671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012672 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673 if (IS_SPECIAL(n) || mod_mask != 0)
12674 {
12675 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12676 int i = 0;
12677
12678 /* Turn a special key into three bytes, plus modifier. */
12679 if (mod_mask != 0)
12680 {
12681 temp[i++] = K_SPECIAL;
12682 temp[i++] = KS_MODIFIER;
12683 temp[i++] = mod_mask;
12684 }
12685 if (IS_SPECIAL(n))
12686 {
12687 temp[i++] = K_SPECIAL;
12688 temp[i++] = K_SECOND(n);
12689 temp[i++] = K_THIRD(n);
12690 }
12691#ifdef FEAT_MBYTE
12692 else if (has_mbyte)
12693 i += (*mb_char2bytes)(n, temp + i);
12694#endif
12695 else
12696 temp[i++] = n;
12697 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012698 rettv->v_type = VAR_STRING;
12699 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012700
12701#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012702 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012703 {
12704 int row = mouse_row;
12705 int col = mouse_col;
12706 win_T *win;
12707 linenr_T lnum;
12708# ifdef FEAT_WINDOWS
12709 win_T *wp;
12710# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012711 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012712
12713 if (row >= 0 && col >= 0)
12714 {
12715 /* Find the window at the mouse coordinates and compute the
12716 * text position. */
12717 win = mouse_find_win(&row, &col);
12718 (void)mouse_comp_pos(win, &row, &col, &lnum);
12719# ifdef FEAT_WINDOWS
12720 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012721 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012722# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012723 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012724 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12725 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12726 }
12727 }
12728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 }
12730}
12731
12732/*
12733 * "getcharmod()" function
12734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012736f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739}
12740
12741/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012742 * "getcharsearch()" function
12743 */
12744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012745f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012746{
12747 if (rettv_dict_alloc(rettv) != FAIL)
12748 {
12749 dict_T *dict = rettv->vval.v_dict;
12750
12751 dict_add_nr_str(dict, "char", 0L, last_csearch());
12752 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12753 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12754 }
12755}
12756
12757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 * "getcmdline()" function
12759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012761f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 rettv->v_type = VAR_STRING;
12764 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765}
12766
12767/*
12768 * "getcmdpos()" function
12769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012771f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774}
12775
12776/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012777 * "getcmdtype()" function
12778 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012780f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012781{
12782 rettv->v_type = VAR_STRING;
12783 rettv->vval.v_string = alloc(2);
12784 if (rettv->vval.v_string != NULL)
12785 {
12786 rettv->vval.v_string[0] = get_cmdline_type();
12787 rettv->vval.v_string[1] = NUL;
12788 }
12789}
12790
12791/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012792 * "getcmdwintype()" function
12793 */
12794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012795f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012796{
12797 rettv->v_type = VAR_STRING;
12798 rettv->vval.v_string = NULL;
12799#ifdef FEAT_CMDWIN
12800 rettv->vval.v_string = alloc(2);
12801 if (rettv->vval.v_string != NULL)
12802 {
12803 rettv->vval.v_string[0] = cmdwin_type;
12804 rettv->vval.v_string[1] = NUL;
12805 }
12806#endif
12807}
12808
12809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 * "getcwd()" function
12811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012813f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012815 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012816 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012819 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012820
12821 wp = find_tabwin(&argvars[0], &argvars[1]);
12822 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012824 if (wp->w_localdir != NULL)
12825 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12826 else if(globaldir != NULL)
12827 rettv->vval.v_string = vim_strsave(globaldir);
12828 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012829 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012830 cwd = alloc(MAXPATHL);
12831 if (cwd != NULL)
12832 {
12833 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12834 rettv->vval.v_string = vim_strsave(cwd);
12835 vim_free(cwd);
12836 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012837 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012838#ifdef BACKSLASH_IN_FILENAME
12839 if (rettv->vval.v_string != NULL)
12840 slash_adjust(rettv->vval.v_string);
12841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 }
12843}
12844
12845/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012846 * "getfontname()" function
12847 */
12848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012849f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012850{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 rettv->v_type = VAR_STRING;
12852 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012853#ifdef FEAT_GUI
12854 if (gui.in_use)
12855 {
12856 GuiFont font;
12857 char_u *name = NULL;
12858
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012859 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012860 {
12861 /* Get the "Normal" font. Either the name saved by
12862 * hl_set_font_name() or from the font ID. */
12863 font = gui.norm_font;
12864 name = hl_get_font_name();
12865 }
12866 else
12867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012869 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12870 return;
12871 font = gui_mch_get_font(name, FALSE);
12872 if (font == NOFONT)
12873 return; /* Invalid font name, return empty string. */
12874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012876 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012877 gui_mch_free_font(font);
12878 }
12879#endif
12880}
12881
12882/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012883 * "getfperm({fname})" function
12884 */
12885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012886f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012887{
12888 char_u *fname;
12889 struct stat st;
12890 char_u *perm = NULL;
12891 char_u flags[] = "rwx";
12892 int i;
12893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012896 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012897 if (mch_stat((char *)fname, &st) >= 0)
12898 {
12899 perm = vim_strsave((char_u *)"---------");
12900 if (perm != NULL)
12901 {
12902 for (i = 0; i < 9; i++)
12903 {
12904 if (st.st_mode & (1 << (8 - i)))
12905 perm[i] = flags[i % 3];
12906 }
12907 }
12908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012910}
12911
12912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 * "getfsize({fname})" function
12914 */
12915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012916f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917{
12918 char_u *fname;
12919 struct stat st;
12920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012923 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924
12925 if (mch_stat((char *)fname, &st) >= 0)
12926 {
12927 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012931 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012932
12933 /* non-perfect check for overflow */
12934 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12935 rettv->vval.v_number = -2;
12936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 }
12938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940}
12941
12942/*
12943 * "getftime({fname})" function
12944 */
12945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012946f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947{
12948 char_u *fname;
12949 struct stat st;
12950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952
12953 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012955 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012956 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957}
12958
12959/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012960 * "getftype({fname})" function
12961 */
12962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012963f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012964{
12965 char_u *fname;
12966 struct stat st;
12967 char_u *type = NULL;
12968 char *t;
12969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012970 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012972 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012973 if (mch_lstat((char *)fname, &st) >= 0)
12974 {
12975#ifdef S_ISREG
12976 if (S_ISREG(st.st_mode))
12977 t = "file";
12978 else if (S_ISDIR(st.st_mode))
12979 t = "dir";
12980# ifdef S_ISLNK
12981 else if (S_ISLNK(st.st_mode))
12982 t = "link";
12983# endif
12984# ifdef S_ISBLK
12985 else if (S_ISBLK(st.st_mode))
12986 t = "bdev";
12987# endif
12988# ifdef S_ISCHR
12989 else if (S_ISCHR(st.st_mode))
12990 t = "cdev";
12991# endif
12992# ifdef S_ISFIFO
12993 else if (S_ISFIFO(st.st_mode))
12994 t = "fifo";
12995# endif
12996# ifdef S_ISSOCK
12997 else if (S_ISSOCK(st.st_mode))
12998 t = "fifo";
12999# endif
13000 else
13001 t = "other";
13002#else
13003# ifdef S_IFMT
13004 switch (st.st_mode & S_IFMT)
13005 {
13006 case S_IFREG: t = "file"; break;
13007 case S_IFDIR: t = "dir"; break;
13008# ifdef S_IFLNK
13009 case S_IFLNK: t = "link"; break;
13010# endif
13011# ifdef S_IFBLK
13012 case S_IFBLK: t = "bdev"; break;
13013# endif
13014# ifdef S_IFCHR
13015 case S_IFCHR: t = "cdev"; break;
13016# endif
13017# ifdef S_IFIFO
13018 case S_IFIFO: t = "fifo"; break;
13019# endif
13020# ifdef S_IFSOCK
13021 case S_IFSOCK: t = "socket"; break;
13022# endif
13023 default: t = "other";
13024 }
13025# else
13026 if (mch_isdir(fname))
13027 t = "dir";
13028 else
13029 t = "file";
13030# endif
13031#endif
13032 type = vim_strsave((char_u *)t);
13033 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013034 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013035}
13036
13037/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013038 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013039 */
13040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013041f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013042{
13043 linenr_T lnum;
13044 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013045 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013046
13047 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013048 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013049 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013050 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013051 retlist = FALSE;
13052 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013053 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013054 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013055 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013056 retlist = TRUE;
13057 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013058
Bram Moolenaar342337a2005-07-21 21:11:17 +000013059 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013060}
13061
13062/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013063 * "getmatches()" function
13064 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013066f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013067{
13068#ifdef FEAT_SEARCH_EXTRA
13069 dict_T *dict;
13070 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013071 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013072
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013073 if (rettv_list_alloc(rettv) == OK)
13074 {
13075 while (cur != NULL)
13076 {
13077 dict = dict_alloc();
13078 if (dict == NULL)
13079 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013080 if (cur->match.regprog == NULL)
13081 {
13082 /* match added with matchaddpos() */
13083 for (i = 0; i < MAXPOSMATCH; ++i)
13084 {
13085 llpos_T *llpos;
13086 char buf[6];
13087 list_T *l;
13088
13089 llpos = &cur->pos.pos[i];
13090 if (llpos->lnum == 0)
13091 break;
13092 l = list_alloc();
13093 if (l == NULL)
13094 break;
13095 list_append_number(l, (varnumber_T)llpos->lnum);
13096 if (llpos->col > 0)
13097 {
13098 list_append_number(l, (varnumber_T)llpos->col);
13099 list_append_number(l, (varnumber_T)llpos->len);
13100 }
13101 sprintf(buf, "pos%d", i + 1);
13102 dict_add_list(dict, buf, l);
13103 }
13104 }
13105 else
13106 {
13107 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13108 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013109 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013110 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13111 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013112# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013113 if (cur->conceal_char)
13114 {
13115 char_u buf[MB_MAXBYTES + 1];
13116
13117 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13118 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13119 }
13120# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013121 list_append_dict(rettv->vval.v_list, dict);
13122 cur = cur->next;
13123 }
13124 }
13125#endif
13126}
13127
13128/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013129 * "getpid()" function
13130 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013132f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013133{
13134 rettv->vval.v_number = mch_get_pid();
13135}
13136
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013138getpos_both(
13139 typval_T *argvars,
13140 typval_T *rettv,
13141 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013142{
Bram Moolenaara5525202006-03-02 22:52:09 +000013143 pos_T *fp;
13144 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013145 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013146
13147 if (rettv_list_alloc(rettv) == OK)
13148 {
13149 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013150 if (getcurpos)
13151 fp = &curwin->w_cursor;
13152 else
13153 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013154 if (fnum != -1)
13155 list_append_number(l, (varnumber_T)fnum);
13156 else
13157 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013158 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13159 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013160 list_append_number(l, (fp != NULL)
13161 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013162 : (varnumber_T)0);
13163 list_append_number(l,
13164#ifdef FEAT_VIRTUALEDIT
13165 (fp != NULL) ? (varnumber_T)fp->coladd :
13166#endif
13167 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013168 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013169 {
13170 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013171 list_append_number(l, curwin->w_curswant == MAXCOL ?
13172 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013173 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013174 }
13175 else
13176 rettv->vval.v_number = FALSE;
13177}
13178
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013179
13180/*
13181 * "getcurpos()" function
13182 */
13183 static void
13184f_getcurpos(typval_T *argvars, typval_T *rettv)
13185{
13186 getpos_both(argvars, rettv, TRUE);
13187}
13188
13189/*
13190 * "getpos(string)" function
13191 */
13192 static void
13193f_getpos(typval_T *argvars, typval_T *rettv)
13194{
13195 getpos_both(argvars, rettv, FALSE);
13196}
13197
Bram Moolenaara5525202006-03-02 22:52:09 +000013198/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013199 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013200 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013202f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013203{
13204#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013205 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013206#endif
13207
Bram Moolenaar2641f772005-03-25 21:58:17 +000013208#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013209 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013210 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013211 wp = NULL;
13212 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13213 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013214 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013215 if (wp == NULL)
13216 return;
13217 }
13218
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013219 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013220 }
13221#endif
13222}
13223
13224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 * "getreg()" function
13226 */
13227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013228f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229{
13230 char_u *strregname;
13231 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013232 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013233 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013234 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013236 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013238 strregname = get_tv_string_chk(&argvars[0]);
13239 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013240 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013242 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013243 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13244 return_list = get_tv_number_chk(&argvars[2], &error);
13245 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013248 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013249
13250 if (error)
13251 return;
13252
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 regname = (strregname == NULL ? '"' : *strregname);
13254 if (regname == 0)
13255 regname = '"';
13256
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013257 if (return_list)
13258 {
13259 rettv->v_type = VAR_LIST;
13260 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13261 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013262 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013263 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013264 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013265 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013266 }
13267 else
13268 {
13269 rettv->v_type = VAR_STRING;
13270 rettv->vval.v_string = get_reg_contents(regname,
13271 arg2 ? GREG_EXPR_SRC : 0);
13272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273}
13274
13275/*
13276 * "getregtype()" function
13277 */
13278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013279f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280{
13281 char_u *strregname;
13282 int regname;
13283 char_u buf[NUMBUFLEN + 2];
13284 long reglen = 0;
13285
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013286 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 {
13288 strregname = get_tv_string_chk(&argvars[0]);
13289 if (strregname == NULL) /* type error; errmsg already given */
13290 {
13291 rettv->v_type = VAR_STRING;
13292 rettv->vval.v_string = NULL;
13293 return;
13294 }
13295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 else
13297 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013298 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299
13300 regname = (strregname == NULL ? '"' : *strregname);
13301 if (regname == 0)
13302 regname = '"';
13303
13304 buf[0] = NUL;
13305 buf[1] = NUL;
13306 switch (get_reg_type(regname, &reglen))
13307 {
13308 case MLINE: buf[0] = 'V'; break;
13309 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310 case MBLOCK:
13311 buf[0] = Ctrl_V;
13312 sprintf((char *)buf + 1, "%ld", reglen + 1);
13313 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013314 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315 rettv->v_type = VAR_STRING;
13316 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317}
13318
13319/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013320 * "gettabvar()" function
13321 */
13322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013323f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013324{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013325 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013326 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013327 dictitem_T *v;
13328 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013329 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013330
13331 rettv->v_type = VAR_STRING;
13332 rettv->vval.v_string = NULL;
13333
13334 varname = get_tv_string_chk(&argvars[1]);
13335 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13336 if (tp != NULL && varname != NULL)
13337 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013338 /* Set tp to be our tabpage, temporarily. Also set the window to the
13339 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013340 if (switch_win(&oldcurwin, &oldtabpage,
13341 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013342 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013343 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013344 /* look up the variable */
13345 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13346 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13347 if (v != NULL)
13348 {
13349 copy_tv(&v->di_tv, rettv);
13350 done = TRUE;
13351 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013352 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013353
13354 /* restore previous notion of curwin */
13355 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013356 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013357
13358 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013359 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013360}
13361
13362/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013363 * "gettabwinvar()" function
13364 */
13365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013366f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013367{
13368 getwinvar(argvars, rettv, 1);
13369}
13370
13371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 * "getwinposx()" function
13373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013375f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378#ifdef FEAT_GUI
13379 if (gui.in_use)
13380 {
13381 int x, y;
13382
13383 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013384 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 }
13386#endif
13387}
13388
13389/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013390 * "win_findbuf()" function
13391 */
13392 static void
13393f_win_findbuf(typval_T *argvars, typval_T *rettv)
13394{
13395 if (rettv_list_alloc(rettv) != FAIL)
13396 win_findbuf(argvars, rettv->vval.v_list);
13397}
13398
13399/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013400 * "win_getid()" function
13401 */
13402 static void
13403f_win_getid(typval_T *argvars, typval_T *rettv)
13404{
13405 rettv->vval.v_number = win_getid(argvars);
13406}
13407
13408/*
13409 * "win_gotoid()" function
13410 */
13411 static void
13412f_win_gotoid(typval_T *argvars, typval_T *rettv)
13413{
13414 rettv->vval.v_number = win_gotoid(argvars);
13415}
13416
13417/*
13418 * "win_id2tabwin()" function
13419 */
13420 static void
13421f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13422{
13423 if (rettv_list_alloc(rettv) != FAIL)
13424 win_id2tabwin(argvars, rettv->vval.v_list);
13425}
13426
13427/*
13428 * "win_id2win()" function
13429 */
13430 static void
13431f_win_id2win(typval_T *argvars, typval_T *rettv)
13432{
13433 rettv->vval.v_number = win_id2win(argvars);
13434}
13435
13436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 * "getwinposy()" function
13438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013440f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013442 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443#ifdef FEAT_GUI
13444 if (gui.in_use)
13445 {
13446 int x, y;
13447
13448 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450 }
13451#endif
13452}
13453
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013454/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013455 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013456 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013457 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013458find_win_by_nr(
13459 typval_T *vp,
13460 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013461{
13462#ifdef FEAT_WINDOWS
13463 win_T *wp;
13464#endif
13465 int nr;
13466
13467 nr = get_tv_number_chk(vp, NULL);
13468
13469#ifdef FEAT_WINDOWS
13470 if (nr < 0)
13471 return NULL;
13472 if (nr == 0)
13473 return curwin;
13474
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013475 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13476 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013477 if (--nr <= 0)
13478 break;
13479 return wp;
13480#else
13481 if (nr == 0 || nr == 1)
13482 return curwin;
13483 return NULL;
13484#endif
13485}
13486
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013488 * Find window specified by "wvp" in tabpage "tvp".
13489 */
13490 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013491find_tabwin(
13492 typval_T *wvp, /* VAR_UNKNOWN for current window */
13493 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013494{
13495 win_T *wp = NULL;
13496 tabpage_T *tp = NULL;
13497 long n;
13498
13499 if (wvp->v_type != VAR_UNKNOWN)
13500 {
13501 if (tvp->v_type != VAR_UNKNOWN)
13502 {
13503 n = get_tv_number(tvp);
13504 if (n >= 0)
13505 tp = find_tabpage(n);
13506 }
13507 else
13508 tp = curtab;
13509
13510 if (tp != NULL)
13511 wp = find_win_by_nr(wvp, tp);
13512 }
13513 else
13514 wp = curwin;
13515
13516 return wp;
13517}
13518
13519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 * "getwinvar()" function
13521 */
13522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013523f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013525 getwinvar(argvars, rettv, 0);
13526}
13527
13528/*
13529 * getwinvar() and gettabwinvar()
13530 */
13531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013532getwinvar(
13533 typval_T *argvars,
13534 typval_T *rettv,
13535 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013536{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013537 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013539 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013540 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013541 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013542#ifdef FEAT_WINDOWS
13543 win_T *oldcurwin;
13544 tabpage_T *oldtabpage;
13545 int need_switch_win;
13546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013548#ifdef FEAT_WINDOWS
13549 if (off == 1)
13550 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13551 else
13552 tp = curtab;
13553#endif
13554 win = find_win_by_nr(&argvars[off], tp);
13555 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013556 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013558 rettv->v_type = VAR_STRING;
13559 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560
13561 if (win != NULL && varname != NULL)
13562 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013563#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013564 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013565 * otherwise the window is not valid. Only do this when needed,
13566 * autocommands get blocked. */
13567 need_switch_win = !(tp == curtab && win == curwin);
13568 if (!need_switch_win
13569 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13570#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013571 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013572 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013573 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013574 if (get_option_tv(&varname, rettv, 1) == OK)
13575 done = TRUE;
13576 }
13577 else
13578 {
13579 /* Look up the variable. */
13580 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13581 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13582 varname, FALSE);
13583 if (v != NULL)
13584 {
13585 copy_tv(&v->di_tv, rettv);
13586 done = TRUE;
13587 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013590
Bram Moolenaarba117c22015-09-29 16:53:22 +020013591#ifdef FEAT_WINDOWS
13592 if (need_switch_win)
13593 /* restore previous notion of curwin */
13594 restore_win(oldcurwin, oldtabpage, TRUE);
13595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596 }
13597
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013598 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13599 /* use the default return value */
13600 copy_tv(&argvars[off + 2], rettv);
13601
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 --emsg_off;
13603}
13604
13605/*
13606 * "glob()" function
13607 */
13608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013609f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013611 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013613 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013615 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013616 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013618 if (argvars[1].v_type != VAR_UNKNOWN)
13619 {
13620 if (get_tv_number_chk(&argvars[1], &error))
13621 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013622 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013623 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013624 if (get_tv_number_chk(&argvars[2], &error))
13625 {
13626 rettv->v_type = VAR_LIST;
13627 rettv->vval.v_list = NULL;
13628 }
13629 if (argvars[3].v_type != VAR_UNKNOWN
13630 && get_tv_number_chk(&argvars[3], &error))
13631 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013632 }
13633 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013634 if (!error)
13635 {
13636 ExpandInit(&xpc);
13637 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013638 if (p_wic)
13639 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013640 if (rettv->v_type == VAR_STRING)
13641 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013642 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013643 else if (rettv_list_alloc(rettv) != FAIL)
13644 {
13645 int i;
13646
13647 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13648 NULL, options, WILD_ALL_KEEP);
13649 for (i = 0; i < xpc.xp_numfiles; i++)
13650 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13651
13652 ExpandCleanup(&xpc);
13653 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013654 }
13655 else
13656 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657}
13658
13659/*
13660 * "globpath()" function
13661 */
13662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013663f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013665 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013668 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013669 garray_T ga;
13670 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013672 /* When the optional second argument is non-zero, don't remove matches
13673 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013675 if (argvars[2].v_type != VAR_UNKNOWN)
13676 {
13677 if (get_tv_number_chk(&argvars[2], &error))
13678 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013679 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013680 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013681 if (get_tv_number_chk(&argvars[3], &error))
13682 {
13683 rettv->v_type = VAR_LIST;
13684 rettv->vval.v_list = NULL;
13685 }
13686 if (argvars[4].v_type != VAR_UNKNOWN
13687 && get_tv_number_chk(&argvars[4], &error))
13688 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013689 }
13690 }
13691 if (file != NULL && !error)
13692 {
13693 ga_init2(&ga, (int)sizeof(char_u *), 10);
13694 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13695 if (rettv->v_type == VAR_STRING)
13696 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13697 else if (rettv_list_alloc(rettv) != FAIL)
13698 for (i = 0; i < ga.ga_len; ++i)
13699 list_append_string(rettv->vval.v_list,
13700 ((char_u **)(ga.ga_data))[i], -1);
13701 ga_clear_strings(&ga);
13702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013703 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013704 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013705}
13706
13707/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013708 * "glob2regpat()" function
13709 */
13710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013711f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013712{
13713 char_u *pat = get_tv_string_chk(&argvars[0]);
13714
13715 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013716 rettv->vval.v_string = (pat == NULL)
13717 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013718}
13719
13720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 * "has()" function
13722 */
13723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013724f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013725{
13726 int i;
13727 char_u *name;
13728 int n = FALSE;
13729 static char *(has_list[]) =
13730 {
13731#ifdef AMIGA
13732 "amiga",
13733# ifdef FEAT_ARP
13734 "arp",
13735# endif
13736#endif
13737#ifdef __BEOS__
13738 "beos",
13739#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013740#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 "mac",
13742#endif
13743#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013744 "macunix", /* built with 'darwin' enabled */
13745#endif
13746#if defined(__APPLE__) && __APPLE__ == 1
13747 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#ifdef __QNX__
13750 "qnx",
13751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752#ifdef UNIX
13753 "unix",
13754#endif
13755#ifdef VMS
13756 "vms",
13757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758#ifdef WIN32
13759 "win32",
13760#endif
13761#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13762 "win32unix",
13763#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013764#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 "win64",
13766#endif
13767#ifdef EBCDIC
13768 "ebcdic",
13769#endif
13770#ifndef CASE_INSENSITIVE_FILENAME
13771 "fname_case",
13772#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013773#ifdef HAVE_ACL
13774 "acl",
13775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013776#ifdef FEAT_ARABIC
13777 "arabic",
13778#endif
13779#ifdef FEAT_AUTOCMD
13780 "autocmd",
13781#endif
13782#ifdef FEAT_BEVAL
13783 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013784# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13785 "balloon_multiline",
13786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787#endif
13788#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13789 "builtin_terms",
13790# ifdef ALL_BUILTIN_TCAPS
13791 "all_builtin_terms",
13792# endif
13793#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013794#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13795 || defined(FEAT_GUI_W32) \
13796 || defined(FEAT_GUI_MOTIF))
13797 "browsefilter",
13798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799#ifdef FEAT_BYTEOFF
13800 "byte_offset",
13801#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013802#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013803 "channel",
13804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805#ifdef FEAT_CINDENT
13806 "cindent",
13807#endif
13808#ifdef FEAT_CLIENTSERVER
13809 "clientserver",
13810#endif
13811#ifdef FEAT_CLIPBOARD
13812 "clipboard",
13813#endif
13814#ifdef FEAT_CMDL_COMPL
13815 "cmdline_compl",
13816#endif
13817#ifdef FEAT_CMDHIST
13818 "cmdline_hist",
13819#endif
13820#ifdef FEAT_COMMENTS
13821 "comments",
13822#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013823#ifdef FEAT_CONCEAL
13824 "conceal",
13825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826#ifdef FEAT_CRYPT
13827 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013828 "crypt-blowfish",
13829 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830#endif
13831#ifdef FEAT_CSCOPE
13832 "cscope",
13833#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013834#ifdef FEAT_CURSORBIND
13835 "cursorbind",
13836#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013837#ifdef CURSOR_SHAPE
13838 "cursorshape",
13839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840#ifdef DEBUG
13841 "debug",
13842#endif
13843#ifdef FEAT_CON_DIALOG
13844 "dialog_con",
13845#endif
13846#ifdef FEAT_GUI_DIALOG
13847 "dialog_gui",
13848#endif
13849#ifdef FEAT_DIFF
13850 "diff",
13851#endif
13852#ifdef FEAT_DIGRAPHS
13853 "digraphs",
13854#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013855#ifdef FEAT_DIRECTX
13856 "directx",
13857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858#ifdef FEAT_DND
13859 "dnd",
13860#endif
13861#ifdef FEAT_EMACS_TAGS
13862 "emacs_tags",
13863#endif
13864 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013865 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866#ifdef FEAT_SEARCH_EXTRA
13867 "extra_search",
13868#endif
13869#ifdef FEAT_FKMAP
13870 "farsi",
13871#endif
13872#ifdef FEAT_SEARCHPATH
13873 "file_in_path",
13874#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013875#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013876 "filterpipe",
13877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878#ifdef FEAT_FIND_ID
13879 "find_in_path",
13880#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013881#ifdef FEAT_FLOAT
13882 "float",
13883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884#ifdef FEAT_FOLDING
13885 "folding",
13886#endif
13887#ifdef FEAT_FOOTER
13888 "footer",
13889#endif
13890#if !defined(USE_SYSTEM) && defined(UNIX)
13891 "fork",
13892#endif
13893#ifdef FEAT_GETTEXT
13894 "gettext",
13895#endif
13896#ifdef FEAT_GUI
13897 "gui",
13898#endif
13899#ifdef FEAT_GUI_ATHENA
13900# ifdef FEAT_GUI_NEXTAW
13901 "gui_neXtaw",
13902# else
13903 "gui_athena",
13904# endif
13905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013906#ifdef FEAT_GUI_GTK
13907 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013908# ifdef USE_GTK3
13909 "gui_gtk3",
13910# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013914#ifdef FEAT_GUI_GNOME
13915 "gui_gnome",
13916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013917#ifdef FEAT_GUI_MAC
13918 "gui_mac",
13919#endif
13920#ifdef FEAT_GUI_MOTIF
13921 "gui_motif",
13922#endif
13923#ifdef FEAT_GUI_PHOTON
13924 "gui_photon",
13925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926#ifdef FEAT_GUI_W32
13927 "gui_win32",
13928#endif
13929#ifdef FEAT_HANGULIN
13930 "hangul_input",
13931#endif
13932#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13933 "iconv",
13934#endif
13935#ifdef FEAT_INS_EXPAND
13936 "insert_expand",
13937#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013938#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013939 "job",
13940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941#ifdef FEAT_JUMPLIST
13942 "jumplist",
13943#endif
13944#ifdef FEAT_KEYMAP
13945 "keymap",
13946#endif
13947#ifdef FEAT_LANGMAP
13948 "langmap",
13949#endif
13950#ifdef FEAT_LIBCALL
13951 "libcall",
13952#endif
13953#ifdef FEAT_LINEBREAK
13954 "linebreak",
13955#endif
13956#ifdef FEAT_LISP
13957 "lispindent",
13958#endif
13959#ifdef FEAT_LISTCMDS
13960 "listcmds",
13961#endif
13962#ifdef FEAT_LOCALMAP
13963 "localmap",
13964#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013965#ifdef FEAT_LUA
13966# ifndef DYNAMIC_LUA
13967 "lua",
13968# endif
13969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970#ifdef FEAT_MENU
13971 "menu",
13972#endif
13973#ifdef FEAT_SESSION
13974 "mksession",
13975#endif
13976#ifdef FEAT_MODIFY_FNAME
13977 "modify_fname",
13978#endif
13979#ifdef FEAT_MOUSE
13980 "mouse",
13981#endif
13982#ifdef FEAT_MOUSESHAPE
13983 "mouseshape",
13984#endif
13985#if defined(UNIX) || defined(VMS)
13986# ifdef FEAT_MOUSE_DEC
13987 "mouse_dec",
13988# endif
13989# ifdef FEAT_MOUSE_GPM
13990 "mouse_gpm",
13991# endif
13992# ifdef FEAT_MOUSE_JSB
13993 "mouse_jsbterm",
13994# endif
13995# ifdef FEAT_MOUSE_NET
13996 "mouse_netterm",
13997# endif
13998# ifdef FEAT_MOUSE_PTERM
13999 "mouse_pterm",
14000# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014001# ifdef FEAT_MOUSE_SGR
14002 "mouse_sgr",
14003# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014004# ifdef FEAT_SYSMOUSE
14005 "mouse_sysmouse",
14006# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014007# ifdef FEAT_MOUSE_URXVT
14008 "mouse_urxvt",
14009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010# ifdef FEAT_MOUSE_XTERM
14011 "mouse_xterm",
14012# endif
14013#endif
14014#ifdef FEAT_MBYTE
14015 "multi_byte",
14016#endif
14017#ifdef FEAT_MBYTE_IME
14018 "multi_byte_ime",
14019#endif
14020#ifdef FEAT_MULTI_LANG
14021 "multi_lang",
14022#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014023#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014024#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014025 "mzscheme",
14026#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028#ifdef FEAT_OLE
14029 "ole",
14030#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014031 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032#ifdef FEAT_PATH_EXTRA
14033 "path_extra",
14034#endif
14035#ifdef FEAT_PERL
14036#ifndef DYNAMIC_PERL
14037 "perl",
14038#endif
14039#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014040#ifdef FEAT_PERSISTENT_UNDO
14041 "persistent_undo",
14042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043#ifdef FEAT_PYTHON
14044#ifndef DYNAMIC_PYTHON
14045 "python",
14046#endif
14047#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014048#ifdef FEAT_PYTHON3
14049#ifndef DYNAMIC_PYTHON3
14050 "python3",
14051#endif
14052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053#ifdef FEAT_POSTSCRIPT
14054 "postscript",
14055#endif
14056#ifdef FEAT_PRINTER
14057 "printer",
14058#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014059#ifdef FEAT_PROFILE
14060 "profile",
14061#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014062#ifdef FEAT_RELTIME
14063 "reltime",
14064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065#ifdef FEAT_QUICKFIX
14066 "quickfix",
14067#endif
14068#ifdef FEAT_RIGHTLEFT
14069 "rightleft",
14070#endif
14071#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14072 "ruby",
14073#endif
14074#ifdef FEAT_SCROLLBIND
14075 "scrollbind",
14076#endif
14077#ifdef FEAT_CMDL_INFO
14078 "showcmd",
14079 "cmdline_info",
14080#endif
14081#ifdef FEAT_SIGNS
14082 "signs",
14083#endif
14084#ifdef FEAT_SMARTINDENT
14085 "smartindent",
14086#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014087#ifdef STARTUPTIME
14088 "startuptime",
14089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090#ifdef FEAT_STL_OPT
14091 "statusline",
14092#endif
14093#ifdef FEAT_SUN_WORKSHOP
14094 "sun_workshop",
14095#endif
14096#ifdef FEAT_NETBEANS_INTG
14097 "netbeans_intg",
14098#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014099#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014100 "spell",
14101#endif
14102#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 "syntax",
14104#endif
14105#if defined(USE_SYSTEM) || !defined(UNIX)
14106 "system",
14107#endif
14108#ifdef FEAT_TAG_BINS
14109 "tag_binary",
14110#endif
14111#ifdef FEAT_TAG_OLDSTATIC
14112 "tag_old_static",
14113#endif
14114#ifdef FEAT_TAG_ANYWHITE
14115 "tag_any_white",
14116#endif
14117#ifdef FEAT_TCL
14118# ifndef DYNAMIC_TCL
14119 "tcl",
14120# endif
14121#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014122#ifdef FEAT_TERMGUICOLORS
14123 "termguicolors",
14124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125#ifdef TERMINFO
14126 "terminfo",
14127#endif
14128#ifdef FEAT_TERMRESPONSE
14129 "termresponse",
14130#endif
14131#ifdef FEAT_TEXTOBJ
14132 "textobjects",
14133#endif
14134#ifdef HAVE_TGETENT
14135 "tgetent",
14136#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014137#ifdef FEAT_TIMERS
14138 "timers",
14139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140#ifdef FEAT_TITLE
14141 "title",
14142#endif
14143#ifdef FEAT_TOOLBAR
14144 "toolbar",
14145#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014146#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14147 "unnamedplus",
14148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149#ifdef FEAT_USR_CMDS
14150 "user-commands", /* was accidentally included in 5.4 */
14151 "user_commands",
14152#endif
14153#ifdef FEAT_VIMINFO
14154 "viminfo",
14155#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014156#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 "vertsplit",
14158#endif
14159#ifdef FEAT_VIRTUALEDIT
14160 "virtualedit",
14161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163#ifdef FEAT_VISUALEXTRA
14164 "visualextra",
14165#endif
14166#ifdef FEAT_VREPLACE
14167 "vreplace",
14168#endif
14169#ifdef FEAT_WILDIGN
14170 "wildignore",
14171#endif
14172#ifdef FEAT_WILDMENU
14173 "wildmenu",
14174#endif
14175#ifdef FEAT_WINDOWS
14176 "windows",
14177#endif
14178#ifdef FEAT_WAK
14179 "winaltkeys",
14180#endif
14181#ifdef FEAT_WRITEBACKUP
14182 "writebackup",
14183#endif
14184#ifdef FEAT_XIM
14185 "xim",
14186#endif
14187#ifdef FEAT_XFONTSET
14188 "xfontset",
14189#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014190#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014191 "xpm",
14192 "xpm_w32", /* for backward compatibility */
14193#else
14194# if defined(HAVE_XPM)
14195 "xpm",
14196# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198#ifdef USE_XSMP
14199 "xsmp",
14200#endif
14201#ifdef USE_XSMP_INTERACT
14202 "xsmp_interact",
14203#endif
14204#ifdef FEAT_XCLIPBOARD
14205 "xterm_clipboard",
14206#endif
14207#ifdef FEAT_XTERM_SAVE
14208 "xterm_save",
14209#endif
14210#if defined(UNIX) && defined(FEAT_X11)
14211 "X11",
14212#endif
14213 NULL
14214 };
14215
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014216 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 for (i = 0; has_list[i] != NULL; ++i)
14218 if (STRICMP(name, has_list[i]) == 0)
14219 {
14220 n = TRUE;
14221 break;
14222 }
14223
14224 if (n == FALSE)
14225 {
14226 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014227 {
14228 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014229 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014230 && vim_isdigit(name[6])
14231 && vim_isdigit(name[8])
14232 && vim_isdigit(name[10]))
14233 {
14234 int major = atoi((char *)name + 6);
14235 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014236
14237 /* Expect "patch-9.9.01234". */
14238 n = (major < VIM_VERSION_MAJOR
14239 || (major == VIM_VERSION_MAJOR
14240 && (minor < VIM_VERSION_MINOR
14241 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014242 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014243 }
14244 else
14245 n = has_patch(atoi((char *)name + 5));
14246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 else if (STRICMP(name, "vim_starting") == 0)
14248 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014249#ifdef FEAT_MBYTE
14250 else if (STRICMP(name, "multi_byte_encoding") == 0)
14251 n = has_mbyte;
14252#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014253#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14254 else if (STRICMP(name, "balloon_multiline") == 0)
14255 n = multiline_balloon_available();
14256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257#ifdef DYNAMIC_TCL
14258 else if (STRICMP(name, "tcl") == 0)
14259 n = tcl_enabled(FALSE);
14260#endif
14261#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14262 else if (STRICMP(name, "iconv") == 0)
14263 n = iconv_enabled(FALSE);
14264#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014265#ifdef DYNAMIC_LUA
14266 else if (STRICMP(name, "lua") == 0)
14267 n = lua_enabled(FALSE);
14268#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014269#ifdef DYNAMIC_MZSCHEME
14270 else if (STRICMP(name, "mzscheme") == 0)
14271 n = mzscheme_enabled(FALSE);
14272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273#ifdef DYNAMIC_RUBY
14274 else if (STRICMP(name, "ruby") == 0)
14275 n = ruby_enabled(FALSE);
14276#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014277#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278#ifdef DYNAMIC_PYTHON
14279 else if (STRICMP(name, "python") == 0)
14280 n = python_enabled(FALSE);
14281#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014282#endif
14283#ifdef FEAT_PYTHON3
14284#ifdef DYNAMIC_PYTHON3
14285 else if (STRICMP(name, "python3") == 0)
14286 n = python3_enabled(FALSE);
14287#endif
14288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289#ifdef DYNAMIC_PERL
14290 else if (STRICMP(name, "perl") == 0)
14291 n = perl_enabled(FALSE);
14292#endif
14293#ifdef FEAT_GUI
14294 else if (STRICMP(name, "gui_running") == 0)
14295 n = (gui.in_use || gui.starting);
14296# ifdef FEAT_GUI_W32
14297 else if (STRICMP(name, "gui_win32s") == 0)
14298 n = gui_is_win32s();
14299# endif
14300# ifdef FEAT_BROWSE
14301 else if (STRICMP(name, "browse") == 0)
14302 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14303# endif
14304#endif
14305#ifdef FEAT_SYN_HL
14306 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014307 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308#endif
14309#if defined(WIN3264)
14310 else if (STRICMP(name, "win95") == 0)
14311 n = mch_windows95();
14312#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014313#ifdef FEAT_NETBEANS_INTG
14314 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014315 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 }
14318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014319 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320}
14321
14322/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014323 * "has_key()" function
14324 */
14325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014326f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014327{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014328 if (argvars[0].v_type != VAR_DICT)
14329 {
14330 EMSG(_(e_dictreq));
14331 return;
14332 }
14333 if (argvars[0].vval.v_dict == NULL)
14334 return;
14335
14336 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014337 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014338}
14339
14340/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014341 * "haslocaldir()" function
14342 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014344f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014345{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014346 win_T *wp = NULL;
14347
14348 wp = find_tabwin(&argvars[0], &argvars[1]);
14349 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014350}
14351
14352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 * "hasmapto()" function
14354 */
14355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014356f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357{
14358 char_u *name;
14359 char_u *mode;
14360 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014361 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014364 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 mode = (char_u *)"nvo";
14366 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014369 if (argvars[2].v_type != VAR_UNKNOWN)
14370 abbr = get_tv_number(&argvars[2]);
14371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372
Bram Moolenaar2c932302006-03-18 21:42:09 +000014373 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377}
14378
14379/*
14380 * "histadd()" function
14381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014383f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384{
14385#ifdef FEAT_CMDHIST
14386 int histype;
14387 char_u *str;
14388 char_u buf[NUMBUFLEN];
14389#endif
14390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 if (check_restricted() || check_secure())
14393 return;
14394#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014395 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14396 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 if (histype >= 0)
14398 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400 if (*str != NUL)
14401 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014402 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014404 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 return;
14406 }
14407 }
14408#endif
14409}
14410
14411/*
14412 * "histdel()" function
14413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014415f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416{
14417#ifdef FEAT_CMDHIST
14418 int n;
14419 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14423 if (str == NULL)
14424 n = 0;
14425 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014427 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014428 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014430 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014431 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 else
14433 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014434 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014435 get_tv_string_buf(&argvars[1], buf));
14436 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437#endif
14438}
14439
14440/*
14441 * "histget()" function
14442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014444f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445{
14446#ifdef FEAT_CMDHIST
14447 int type;
14448 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014449 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014451 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14452 if (str == NULL)
14453 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014455 {
14456 type = get_histtype(str);
14457 if (argvars[1].v_type == VAR_UNKNOWN)
14458 idx = get_history_idx(type);
14459 else
14460 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14461 /* -1 on type error */
14462 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468}
14469
14470/*
14471 * "histnr()" function
14472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014474f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475{
14476 int i;
14477
14478#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014479 char_u *history = get_tv_string_chk(&argvars[0]);
14480
14481 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 if (i >= HIST_CMD && i < HIST_COUNT)
14483 i = get_history_idx(i);
14484 else
14485#endif
14486 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014487 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488}
14489
14490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 * "highlightID(name)" function
14492 */
14493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014494f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497}
14498
14499/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014500 * "highlight_exists()" function
14501 */
14502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014503f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014504{
14505 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14506}
14507
14508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 * "hostname()" function
14510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014512f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513{
14514 char_u hostname[256];
14515
14516 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517 rettv->v_type = VAR_STRING;
14518 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519}
14520
14521/*
14522 * iconv() function
14523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014525f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526{
14527#ifdef FEAT_MBYTE
14528 char_u buf1[NUMBUFLEN];
14529 char_u buf2[NUMBUFLEN];
14530 char_u *from, *to, *str;
14531 vimconv_T vimconv;
14532#endif
14533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534 rettv->v_type = VAR_STRING;
14535 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536
14537#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014538 str = get_tv_string(&argvars[0]);
14539 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14540 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541 vimconv.vc_type = CONV_NONE;
14542 convert_setup(&vimconv, from, to);
14543
14544 /* If the encodings are equal, no conversion needed. */
14545 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014546 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014548 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549
14550 convert_setup(&vimconv, NULL, NULL);
14551 vim_free(from);
14552 vim_free(to);
14553#endif
14554}
14555
14556/*
14557 * "indent()" function
14558 */
14559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014560f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561{
14562 linenr_T lnum;
14563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014564 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014568 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569}
14570
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014571/*
14572 * "index()" function
14573 */
14574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014575f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014576{
Bram Moolenaar33570922005-01-25 22:26:29 +000014577 list_T *l;
14578 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014579 long idx = 0;
14580 int ic = FALSE;
14581
14582 rettv->vval.v_number = -1;
14583 if (argvars[0].v_type != VAR_LIST)
14584 {
14585 EMSG(_(e_listreq));
14586 return;
14587 }
14588 l = argvars[0].vval.v_list;
14589 if (l != NULL)
14590 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014591 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014592 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014594 int error = FALSE;
14595
Bram Moolenaar758711c2005-02-02 23:11:38 +000014596 /* Start at specified item. Use the cached index that list_find()
14597 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014599 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014600 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014601 ic = get_tv_number_chk(&argvars[3], &error);
14602 if (error)
14603 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014604 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014605
Bram Moolenaar758711c2005-02-02 23:11:38 +000014606 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014607 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014608 {
14609 rettv->vval.v_number = idx;
14610 break;
14611 }
14612 }
14613}
14614
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615static int inputsecret_flag = 0;
14616
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014617static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014618
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014620 * This function is used by f_input() and f_inputdialog() functions. The third
14621 * argument to f_input() specifies the type of completion to use at the
14622 * prompt. The third argument to f_inputdialog() specifies the value to return
14623 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 */
14625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014626get_user_input(
14627 typval_T *argvars,
14628 typval_T *rettv,
14629 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014631 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632 char_u *p = NULL;
14633 int c;
14634 char_u buf[NUMBUFLEN];
14635 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014637 int xp_type = EXPAND_NOTHING;
14638 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014640 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014641 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642
14643#ifdef NO_CONSOLE_INPUT
14644 /* While starting up, there is no place to enter text. */
14645 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647#endif
14648
14649 cmd_silent = FALSE; /* Want to see the prompt. */
14650 if (prompt != NULL)
14651 {
14652 /* Only the part of the message after the last NL is considered as
14653 * prompt for the command line */
14654 p = vim_strrchr(prompt, '\n');
14655 if (p == NULL)
14656 p = prompt;
14657 else
14658 {
14659 ++p;
14660 c = *p;
14661 *p = NUL;
14662 msg_start();
14663 msg_clr_eos();
14664 msg_puts_attr(prompt, echo_attr);
14665 msg_didout = FALSE;
14666 msg_starthere();
14667 *p = c;
14668 }
14669 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 if (argvars[1].v_type != VAR_UNKNOWN)
14672 {
14673 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14674 if (defstr != NULL)
14675 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014677 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014678 {
14679 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014680 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014681 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014682
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014683 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014684 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014685
Bram Moolenaar4463f292005-09-25 22:20:24 +000014686 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14687 if (xp_name == NULL)
14688 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014689
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014690 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014691
Bram Moolenaar4463f292005-09-25 22:20:24 +000014692 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14693 &xp_arg) == FAIL)
14694 return;
14695 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014696 }
14697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014698 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014699 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014700 int save_ex_normal_busy = ex_normal_busy;
14701 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014703 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14704 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014705 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014706 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014707 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014708 && argvars[1].v_type != VAR_UNKNOWN
14709 && argvars[2].v_type != VAR_UNKNOWN)
14710 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14711 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014712
14713 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014715 /* since the user typed this, no need to wait for return */
14716 need_wait_return = FALSE;
14717 msg_didout = FALSE;
14718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719 cmd_silent = cmd_silent_save;
14720}
14721
14722/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014723 * "input()" function
14724 * Also handles inputsecret() when inputsecret is set.
14725 */
14726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014727f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014728{
14729 get_user_input(argvars, rettv, FALSE);
14730}
14731
14732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014733 * "inputdialog()" function
14734 */
14735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014736f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737{
14738#if defined(FEAT_GUI_TEXTDIALOG)
14739 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14740 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14741 {
14742 char_u *message;
14743 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014746 message = get_tv_string_chk(&argvars[0]);
14747 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014748 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014749 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 else
14751 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014752 if (message != NULL && defstr != NULL
14753 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014754 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 else
14757 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014758 if (message != NULL && defstr != NULL
14759 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014760 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014761 rettv->vval.v_string = vim_strsave(
14762 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014766 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 }
14768 else
14769#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014770 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771}
14772
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014773/*
14774 * "inputlist()" function
14775 */
14776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014777f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014778{
14779 listitem_T *li;
14780 int selected;
14781 int mouse_used;
14782
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014783#ifdef NO_CONSOLE_INPUT
14784 /* While starting up, there is no place to enter text. */
14785 if (no_console_input())
14786 return;
14787#endif
14788 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14789 {
14790 EMSG2(_(e_listarg), "inputlist()");
14791 return;
14792 }
14793
14794 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014795 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014796 lines_left = Rows; /* avoid more prompt */
14797 msg_scroll = TRUE;
14798 msg_clr_eos();
14799
14800 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14801 {
14802 msg_puts(get_tv_string(&li->li_tv));
14803 msg_putchar('\n');
14804 }
14805
14806 /* Ask for choice. */
14807 selected = prompt_for_number(&mouse_used);
14808 if (mouse_used)
14809 selected -= lines_left;
14810
14811 rettv->vval.v_number = selected;
14812}
14813
14814
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14816
14817/*
14818 * "inputrestore()" function
14819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014821f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822{
14823 if (ga_userinput.ga_len > 0)
14824 {
14825 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14827 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014828 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829 }
14830 else if (p_verbose > 1)
14831 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014832 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 }
14835}
14836
14837/*
14838 * "inputsave()" function
14839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014841f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014843 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 if (ga_grow(&ga_userinput, 1) == OK)
14845 {
14846 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14847 + ga_userinput.ga_len);
14848 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014849 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850 }
14851 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014852 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853}
14854
14855/*
14856 * "inputsecret()" function
14857 */
14858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014859f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860{
14861 ++cmdline_star;
14862 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014863 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 --cmdline_star;
14865 --inputsecret_flag;
14866}
14867
14868/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014869 * "insert()" function
14870 */
14871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014872f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014873{
14874 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014875 listitem_T *item;
14876 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014877 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014878
14879 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014880 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014881 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014882 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014883 {
14884 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014885 before = get_tv_number_chk(&argvars[2], &error);
14886 if (error)
14887 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014888
Bram Moolenaar758711c2005-02-02 23:11:38 +000014889 if (before == l->lv_len)
14890 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014891 else
14892 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014893 item = list_find(l, before);
14894 if (item == NULL)
14895 {
14896 EMSGN(_(e_listidx), before);
14897 l = NULL;
14898 }
14899 }
14900 if (l != NULL)
14901 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014902 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014903 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014904 }
14905 }
14906}
14907
14908/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014909 * "invert(expr)" function
14910 */
14911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014912f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014913{
14914 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14915}
14916
14917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918 * "isdirectory()" function
14919 */
14920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014921f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924}
14925
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014926/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014927 * "islocked()" function
14928 */
14929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014930f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014931{
14932 lval_T lv;
14933 char_u *end;
14934 dictitem_T *di;
14935
14936 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014937 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14938 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014939 if (end != NULL && lv.ll_name != NULL)
14940 {
14941 if (*end != NUL)
14942 EMSG(_(e_trailing));
14943 else
14944 {
14945 if (lv.ll_tv == NULL)
14946 {
14947 if (check_changedtick(lv.ll_name))
14948 rettv->vval.v_number = 1; /* always locked */
14949 else
14950 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014951 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014952 if (di != NULL)
14953 {
14954 /* Consider a variable locked when:
14955 * 1. the variable itself is locked
14956 * 2. the value of the variable is locked.
14957 * 3. the List or Dict value is locked.
14958 */
14959 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14960 || tv_islocked(&di->di_tv));
14961 }
14962 }
14963 }
14964 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014965 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014966 else if (lv.ll_newkey != NULL)
14967 EMSG2(_(e_dictkey), lv.ll_newkey);
14968 else if (lv.ll_list != NULL)
14969 /* List item. */
14970 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14971 else
14972 /* Dictionary item. */
14973 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14974 }
14975 }
14976
14977 clear_lval(&lv);
14978}
14979
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014980#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14981/*
14982 * "isnan()" function
14983 */
14984 static void
14985f_isnan(typval_T *argvars, typval_T *rettv)
14986{
14987 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14988 && isnan(argvars[0].vval.v_float);
14989}
14990#endif
14991
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014992static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014993
14994/*
14995 * Turn a dict into a list:
14996 * "what" == 0: list of keys
14997 * "what" == 1: list of values
14998 * "what" == 2: list of items
14999 */
15000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015001dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015002{
Bram Moolenaar33570922005-01-25 22:26:29 +000015003 list_T *l2;
15004 dictitem_T *di;
15005 hashitem_T *hi;
15006 listitem_T *li;
15007 listitem_T *li2;
15008 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015009 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015010
Bram Moolenaar8c711452005-01-14 21:53:12 +000015011 if (argvars[0].v_type != VAR_DICT)
15012 {
15013 EMSG(_(e_dictreq));
15014 return;
15015 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015016 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015017 return;
15018
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015019 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015020 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015021
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015022 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015023 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015024 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015025 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015026 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015027 --todo;
15028 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015029
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015030 li = listitem_alloc();
15031 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015032 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015033 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015034
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015035 if (what == 0)
15036 {
15037 /* keys() */
15038 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015039 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015040 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15041 }
15042 else if (what == 1)
15043 {
15044 /* values() */
15045 copy_tv(&di->di_tv, &li->li_tv);
15046 }
15047 else
15048 {
15049 /* items() */
15050 l2 = list_alloc();
15051 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015052 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015053 li->li_tv.vval.v_list = l2;
15054 if (l2 == NULL)
15055 break;
15056 ++l2->lv_refcount;
15057
15058 li2 = listitem_alloc();
15059 if (li2 == NULL)
15060 break;
15061 list_append(l2, li2);
15062 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015063 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015064 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15065
15066 li2 = listitem_alloc();
15067 if (li2 == NULL)
15068 break;
15069 list_append(l2, li2);
15070 copy_tv(&di->di_tv, &li2->li_tv);
15071 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015072 }
15073 }
15074}
15075
15076/*
15077 * "items(dict)" function
15078 */
15079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015080f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015081{
15082 dict_list(argvars, rettv, 2);
15083}
15084
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015085#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015086/*
15087 * Get the job from the argument.
15088 * Returns NULL if the job is invalid.
15089 */
15090 static job_T *
15091get_job_arg(typval_T *tv)
15092{
15093 job_T *job;
15094
15095 if (tv->v_type != VAR_JOB)
15096 {
15097 EMSG2(_(e_invarg2), get_tv_string(tv));
15098 return NULL;
15099 }
15100 job = tv->vval.v_job;
15101
15102 if (job == NULL)
15103 EMSG(_("E916: not a valid job"));
15104 return job;
15105}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015106
Bram Moolenaar835dc632016-02-07 14:27:38 +010015107/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015108 * "job_getchannel()" function
15109 */
15110 static void
15111f_job_getchannel(typval_T *argvars, typval_T *rettv)
15112{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015113 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015114
Bram Moolenaar65edff82016-02-21 16:40:11 +010015115 if (job != NULL)
15116 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015117 rettv->v_type = VAR_CHANNEL;
15118 rettv->vval.v_channel = job->jv_channel;
15119 if (job->jv_channel != NULL)
15120 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015121 }
15122}
15123
15124/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015125 * "job_info()" function
15126 */
15127 static void
15128f_job_info(typval_T *argvars, typval_T *rettv)
15129{
15130 job_T *job = get_job_arg(&argvars[0]);
15131
15132 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15133 job_info(job, rettv->vval.v_dict);
15134}
15135
15136/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015137 * "job_setoptions()" function
15138 */
15139 static void
15140f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15141{
15142 job_T *job = get_job_arg(&argvars[0]);
15143 jobopt_T opt;
15144
15145 if (job == NULL)
15146 return;
15147 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015148 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15149 job_set_options(job, &opt);
15150 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015151}
15152
15153/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015154 * "job_start()" function
15155 */
15156 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015157f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015158{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015159 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015160 if (check_restricted() || check_secure())
15161 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015162 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015163}
15164
15165/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015166 * "job_status()" function
15167 */
15168 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015169f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015170{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015171 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015172
Bram Moolenaar65edff82016-02-21 16:40:11 +010015173 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015174 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015175 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015176 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015177 }
15178}
15179
15180/*
15181 * "job_stop()" function
15182 */
15183 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015184f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015185{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015186 job_T *job = get_job_arg(&argvars[0]);
15187
15188 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015189 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015190}
15191#endif
15192
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015194 * "join()" function
15195 */
15196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015197f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015198{
15199 garray_T ga;
15200 char_u *sep;
15201
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015202 if (argvars[0].v_type != VAR_LIST)
15203 {
15204 EMSG(_(e_listreq));
15205 return;
15206 }
15207 if (argvars[0].vval.v_list == NULL)
15208 return;
15209 if (argvars[1].v_type == VAR_UNKNOWN)
15210 sep = (char_u *)" ";
15211 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015212 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015213
15214 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015215
15216 if (sep != NULL)
15217 {
15218 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015219 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015220 ga_append(&ga, NUL);
15221 rettv->vval.v_string = (char_u *)ga.ga_data;
15222 }
15223 else
15224 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015225}
15226
15227/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015228 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015229 */
15230 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015231f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015232{
15233 js_read_T reader;
15234
15235 reader.js_buf = get_tv_string(&argvars[0]);
15236 reader.js_fill = NULL;
15237 reader.js_used = 0;
15238 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15239 EMSG(_(e_invarg));
15240}
15241
15242/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015243 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015244 */
15245 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015246f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015247{
15248 rettv->v_type = VAR_STRING;
15249 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15250}
15251
15252/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015253 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015254 */
15255 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015256f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015257{
15258 js_read_T reader;
15259
15260 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015261 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015262 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015263 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015264 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015265}
15266
15267/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015268 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015269 */
15270 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015271f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015272{
15273 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015274 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015275}
15276
15277/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015278 * "keys()" function
15279 */
15280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015281f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015282{
15283 dict_list(argvars, rettv, 0);
15284}
15285
15286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287 * "last_buffer_nr()" function.
15288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015290f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291{
15292 int n = 0;
15293 buf_T *buf;
15294
15295 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15296 if (n < buf->b_fnum)
15297 n = buf->b_fnum;
15298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015299 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015300}
15301
15302/*
15303 * "len()" function
15304 */
15305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015306f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015307{
15308 switch (argvars[0].v_type)
15309 {
15310 case VAR_STRING:
15311 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015312 rettv->vval.v_number = (varnumber_T)STRLEN(
15313 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015314 break;
15315 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015316 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015317 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015318 case VAR_DICT:
15319 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15320 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015321 case VAR_UNKNOWN:
15322 case VAR_SPECIAL:
15323 case VAR_FLOAT:
15324 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015325 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015326 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015327 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015328 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015329 break;
15330 }
15331}
15332
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015333static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015334
15335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015336libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015337{
15338#ifdef FEAT_LIBCALL
15339 char_u *string_in;
15340 char_u **string_result;
15341 int nr_result;
15342#endif
15343
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015344 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015345 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015346 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015347
15348 if (check_restricted() || check_secure())
15349 return;
15350
15351#ifdef FEAT_LIBCALL
15352 /* The first two args must be strings, otherwise its meaningless */
15353 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15354 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015355 string_in = NULL;
15356 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015357 string_in = argvars[2].vval.v_string;
15358 if (type == VAR_NUMBER)
15359 string_result = NULL;
15360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015361 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015362 if (mch_libcall(argvars[0].vval.v_string,
15363 argvars[1].vval.v_string,
15364 string_in,
15365 argvars[2].vval.v_number,
15366 string_result,
15367 &nr_result) == OK
15368 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015369 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015370 }
15371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372}
15373
15374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015375 * "libcall()" function
15376 */
15377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015378f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015379{
15380 libcall_common(argvars, rettv, VAR_STRING);
15381}
15382
15383/*
15384 * "libcallnr()" function
15385 */
15386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015387f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015388{
15389 libcall_common(argvars, rettv, VAR_NUMBER);
15390}
15391
15392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 * "line(string)" function
15394 */
15395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015396f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397{
15398 linenr_T lnum = 0;
15399 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015400 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015402 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 if (fp != NULL)
15404 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406}
15407
15408/*
15409 * "line2byte(lnum)" function
15410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015412f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413{
15414#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416#else
15417 linenr_T lnum;
15418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015421 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15424 if (rettv->vval.v_number >= 0)
15425 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426#endif
15427}
15428
15429/*
15430 * "lispindent(lnum)" function
15431 */
15432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015433f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434{
15435#ifdef FEAT_LISP
15436 pos_T pos;
15437 linenr_T lnum;
15438
15439 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015440 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15442 {
15443 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015444 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 curwin->w_cursor = pos;
15446 }
15447 else
15448#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450}
15451
15452/*
15453 * "localtime()" function
15454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015456f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015458 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459}
15460
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015461static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015462
15463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015464get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015465{
15466 char_u *keys;
15467 char_u *which;
15468 char_u buf[NUMBUFLEN];
15469 char_u *keys_buf = NULL;
15470 char_u *rhs;
15471 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015472 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015473 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015474 mapblock_T *mp;
15475 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476
15477 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478 rettv->v_type = VAR_STRING;
15479 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015481 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 if (*keys == NUL)
15483 return;
15484
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015485 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015487 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015489 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015490 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015491 if (argvars[3].v_type != VAR_UNKNOWN)
15492 get_dict = get_tv_number(&argvars[3]);
15493 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 else
15496 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015497 if (which == NULL)
15498 return;
15499
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500 mode = get_map_mode(&which, 0);
15501
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015502 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015503 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015505
15506 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015508 /* Return a string. */
15509 if (rhs != NULL)
15510 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511
Bram Moolenaarbd743252010-10-20 21:23:33 +020015512 }
15513 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15514 {
15515 /* Return a dictionary. */
15516 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15517 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15518 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519
Bram Moolenaarbd743252010-10-20 21:23:33 +020015520 dict_add_nr_str(dict, "lhs", 0L, lhs);
15521 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15522 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15523 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15524 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15525 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15526 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015527 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015528 dict_add_nr_str(dict, "mode", 0L, mapmode);
15529
15530 vim_free(lhs);
15531 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 }
15533}
15534
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015535#ifdef FEAT_FLOAT
15536/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015537 * "log()" function
15538 */
15539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015540f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015541{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015542 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015543
15544 rettv->v_type = VAR_FLOAT;
15545 if (get_float_arg(argvars, &f) == OK)
15546 rettv->vval.v_float = log(f);
15547 else
15548 rettv->vval.v_float = 0.0;
15549}
15550
15551/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015552 * "log10()" function
15553 */
15554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015555f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015556{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015557 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015558
15559 rettv->v_type = VAR_FLOAT;
15560 if (get_float_arg(argvars, &f) == OK)
15561 rettv->vval.v_float = log10(f);
15562 else
15563 rettv->vval.v_float = 0.0;
15564}
15565#endif
15566
Bram Moolenaar1dced572012-04-05 16:54:08 +020015567#ifdef FEAT_LUA
15568/*
15569 * "luaeval()" function
15570 */
15571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015572f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015573{
15574 char_u *str;
15575 char_u buf[NUMBUFLEN];
15576
15577 str = get_tv_string_buf(&argvars[0], buf);
15578 do_luaeval(str, argvars + 1, rettv);
15579}
15580#endif
15581
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015583 * "map()" function
15584 */
15585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015586f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015587{
15588 filter_map(argvars, rettv, TRUE);
15589}
15590
15591/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015592 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 */
15594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015595f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015597 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598}
15599
15600/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015601 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 */
15603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015604f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607}
15608
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015609static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610
15611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015612find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015614 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015615 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015616 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 char_u *pat;
15618 regmatch_T regmatch;
15619 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015620 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 char_u *save_cpo;
15622 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015623 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015624 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015625 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015626 list_T *l = NULL;
15627 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015628 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015629 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630
15631 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15632 save_cpo = p_cpo;
15633 p_cpo = (char_u *)"";
15634
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015635 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015636 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015637 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015638 /* type 3: return empty list when there are no matches.
15639 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015640 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015641 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015642 if (type == 4
15643 && (list_append_string(rettv->vval.v_list,
15644 (char_u *)"", 0) == FAIL
15645 || list_append_number(rettv->vval.v_list,
15646 (varnumber_T)-1) == FAIL
15647 || list_append_number(rettv->vval.v_list,
15648 (varnumber_T)-1) == FAIL
15649 || list_append_number(rettv->vval.v_list,
15650 (varnumber_T)-1) == FAIL))
15651 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015652 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015653 rettv->vval.v_list = NULL;
15654 goto theend;
15655 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015656 }
15657 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015659 rettv->v_type = VAR_STRING;
15660 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015663 if (argvars[0].v_type == VAR_LIST)
15664 {
15665 if ((l = argvars[0].vval.v_list) == NULL)
15666 goto theend;
15667 li = l->lv_first;
15668 }
15669 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015670 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015671 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015672 len = (long)STRLEN(str);
15673 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015675 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15676 if (pat == NULL)
15677 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015678
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015679 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015681 int error = FALSE;
15682
15683 start = get_tv_number_chk(&argvars[2], &error);
15684 if (error)
15685 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015686 if (l != NULL)
15687 {
15688 li = list_find(l, start);
15689 if (li == NULL)
15690 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015691 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015692 }
15693 else
15694 {
15695 if (start < 0)
15696 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015697 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015698 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015699 /* When "count" argument is there ignore matches before "start",
15700 * otherwise skip part of the string. Differs when pattern is "^"
15701 * or "\<". */
15702 if (argvars[3].v_type != VAR_UNKNOWN)
15703 startcol = start;
15704 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015705 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015706 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015707 len -= start;
15708 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015709 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015710
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015711 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015712 nth = get_tv_number_chk(&argvars[3], &error);
15713 if (error)
15714 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 }
15716
15717 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15718 if (regmatch.regprog != NULL)
15719 {
15720 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015721
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015722 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015723 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015724 if (l != NULL)
15725 {
15726 if (li == NULL)
15727 {
15728 match = FALSE;
15729 break;
15730 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015731 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015732 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015733 if (str == NULL)
15734 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015735 }
15736
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015737 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015738
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015739 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015740 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015741 if (l == NULL && !match)
15742 break;
15743
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015744 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015745 if (l != NULL)
15746 {
15747 li = li->li_next;
15748 ++idx;
15749 }
15750 else
15751 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015752#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015753 startcol = (colnr_T)(regmatch.startp[0]
15754 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015755#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015756 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015757#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015758 if (startcol > (colnr_T)len
15759 || str + startcol <= regmatch.startp[0])
15760 {
15761 match = FALSE;
15762 break;
15763 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015764 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015765 }
15766
15767 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015769 if (type == 4)
15770 {
15771 listitem_T *li1 = rettv->vval.v_list->lv_first;
15772 listitem_T *li2 = li1->li_next;
15773 listitem_T *li3 = li2->li_next;
15774 listitem_T *li4 = li3->li_next;
15775
15776 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15777 (int)(regmatch.endp[0] - regmatch.startp[0]));
15778 li3->li_tv.vval.v_number =
15779 (varnumber_T)(regmatch.startp[0] - expr);
15780 li4->li_tv.vval.v_number =
15781 (varnumber_T)(regmatch.endp[0] - expr);
15782 if (l != NULL)
15783 li2->li_tv.vval.v_number = (varnumber_T)idx;
15784 }
15785 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015786 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015787 int i;
15788
15789 /* return list with matched string and submatches */
15790 for (i = 0; i < NSUBEXP; ++i)
15791 {
15792 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015793 {
15794 if (list_append_string(rettv->vval.v_list,
15795 (char_u *)"", 0) == FAIL)
15796 break;
15797 }
15798 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015799 regmatch.startp[i],
15800 (int)(regmatch.endp[i] - regmatch.startp[i]))
15801 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015802 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015803 }
15804 }
15805 else if (type == 2)
15806 {
15807 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015808 if (l != NULL)
15809 copy_tv(&li->li_tv, rettv);
15810 else
15811 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015813 }
15814 else if (l != NULL)
15815 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 else
15817 {
15818 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015819 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 (varnumber_T)(regmatch.startp[0] - str);
15821 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015822 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015824 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825 }
15826 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015827 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 }
15829
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015830 if (type == 4 && l == NULL)
15831 /* matchstrpos() without a list: drop the second item. */
15832 listitem_remove(rettv->vval.v_list,
15833 rettv->vval.v_list->lv_first->li_next);
15834
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015836 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837 p_cpo = save_cpo;
15838}
15839
15840/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015841 * "match()" function
15842 */
15843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015844f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015845{
15846 find_some_match(argvars, rettv, 1);
15847}
15848
15849/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015850 * "matchadd()" function
15851 */
15852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015853f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015854{
15855#ifdef FEAT_SEARCH_EXTRA
15856 char_u buf[NUMBUFLEN];
15857 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15858 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15859 int prio = 10; /* default priority */
15860 int id = -1;
15861 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015862 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015863
15864 rettv->vval.v_number = -1;
15865
15866 if (grp == NULL || pat == NULL)
15867 return;
15868 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015869 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015870 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015871 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015872 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015873 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015874 if (argvars[4].v_type != VAR_UNKNOWN)
15875 {
15876 if (argvars[4].v_type != VAR_DICT)
15877 {
15878 EMSG(_(e_dictreq));
15879 return;
15880 }
15881 if (dict_find(argvars[4].vval.v_dict,
15882 (char_u *)"conceal", -1) != NULL)
15883 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15884 (char_u *)"conceal", FALSE);
15885 }
15886 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015887 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015888 if (error == TRUE)
15889 return;
15890 if (id >= 1 && id <= 3)
15891 {
15892 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15893 return;
15894 }
15895
Bram Moolenaar6561d522015-07-21 15:48:27 +020015896 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15897 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015898#endif
15899}
15900
15901/*
15902 * "matchaddpos()" function
15903 */
15904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015905f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015906{
15907#ifdef FEAT_SEARCH_EXTRA
15908 char_u buf[NUMBUFLEN];
15909 char_u *group;
15910 int prio = 10;
15911 int id = -1;
15912 int error = FALSE;
15913 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015914 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015915
15916 rettv->vval.v_number = -1;
15917
15918 group = get_tv_string_buf_chk(&argvars[0], buf);
15919 if (group == NULL)
15920 return;
15921
15922 if (argvars[1].v_type != VAR_LIST)
15923 {
15924 EMSG2(_(e_listarg), "matchaddpos()");
15925 return;
15926 }
15927 l = argvars[1].vval.v_list;
15928 if (l == NULL)
15929 return;
15930
15931 if (argvars[2].v_type != VAR_UNKNOWN)
15932 {
15933 prio = get_tv_number_chk(&argvars[2], &error);
15934 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015935 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015936 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015937 if (argvars[4].v_type != VAR_UNKNOWN)
15938 {
15939 if (argvars[4].v_type != VAR_DICT)
15940 {
15941 EMSG(_(e_dictreq));
15942 return;
15943 }
15944 if (dict_find(argvars[4].vval.v_dict,
15945 (char_u *)"conceal", -1) != NULL)
15946 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15947 (char_u *)"conceal", FALSE);
15948 }
15949 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015950 }
15951 if (error == TRUE)
15952 return;
15953
15954 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15955 if (id == 1 || id == 2)
15956 {
15957 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15958 return;
15959 }
15960
Bram Moolenaar6561d522015-07-21 15:48:27 +020015961 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15962 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015963#endif
15964}
15965
15966/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015967 * "matcharg()" function
15968 */
15969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015970f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015971{
15972 if (rettv_list_alloc(rettv) == OK)
15973 {
15974#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015975 int id = get_tv_number(&argvars[0]);
15976 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015977
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015978 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015979 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015980 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15981 {
15982 list_append_string(rettv->vval.v_list,
15983 syn_id2name(m->hlg_id), -1);
15984 list_append_string(rettv->vval.v_list, m->pattern, -1);
15985 }
15986 else
15987 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015988 list_append_string(rettv->vval.v_list, NULL, -1);
15989 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015990 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015991 }
15992#endif
15993 }
15994}
15995
15996/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015997 * "matchdelete()" function
15998 */
15999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016000f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016001{
16002#ifdef FEAT_SEARCH_EXTRA
16003 rettv->vval.v_number = match_delete(curwin,
16004 (int)get_tv_number(&argvars[0]), TRUE);
16005#endif
16006}
16007
16008/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016009 * "matchend()" function
16010 */
16011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016012f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016013{
16014 find_some_match(argvars, rettv, 0);
16015}
16016
16017/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016018 * "matchlist()" function
16019 */
16020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016021f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016022{
16023 find_some_match(argvars, rettv, 3);
16024}
16025
16026/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016027 * "matchstr()" function
16028 */
16029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016030f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016031{
16032 find_some_match(argvars, rettv, 2);
16033}
16034
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016035/*
16036 * "matchstrpos()" function
16037 */
16038 static void
16039f_matchstrpos(typval_T *argvars, typval_T *rettv)
16040{
16041 find_some_match(argvars, rettv, 4);
16042}
16043
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016044static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016045
16046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016047max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016048{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016049 long n = 0;
16050 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016051 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016052
16053 if (argvars[0].v_type == VAR_LIST)
16054 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016055 list_T *l;
16056 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016057
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016058 l = argvars[0].vval.v_list;
16059 if (l != NULL)
16060 {
16061 li = l->lv_first;
16062 if (li != NULL)
16063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016064 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016065 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016066 {
16067 li = li->li_next;
16068 if (li == NULL)
16069 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016070 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016071 if (domax ? i > n : i < n)
16072 n = i;
16073 }
16074 }
16075 }
16076 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016077 else if (argvars[0].v_type == VAR_DICT)
16078 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016079 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016080 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016081 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016082 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016083
16084 d = argvars[0].vval.v_dict;
16085 if (d != NULL)
16086 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016087 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016088 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016089 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016090 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016092 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016093 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016094 if (first)
16095 {
16096 n = i;
16097 first = FALSE;
16098 }
16099 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016100 n = i;
16101 }
16102 }
16103 }
16104 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016105 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016106 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016107 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016108}
16109
16110/*
16111 * "max()" function
16112 */
16113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016114f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016115{
16116 max_min(argvars, rettv, TRUE);
16117}
16118
16119/*
16120 * "min()" function
16121 */
16122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016123f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016124{
16125 max_min(argvars, rettv, FALSE);
16126}
16127
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016128static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016129
16130/*
16131 * Create the directory in which "dir" is located, and higher levels when
16132 * needed.
16133 */
16134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016135mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016136{
16137 char_u *p;
16138 char_u *updir;
16139 int r = FAIL;
16140
16141 /* Get end of directory name in "dir".
16142 * We're done when it's "/" or "c:/". */
16143 p = gettail_sep(dir);
16144 if (p <= get_past_head(dir))
16145 return OK;
16146
16147 /* If the directory exists we're done. Otherwise: create it.*/
16148 updir = vim_strnsave(dir, (int)(p - dir));
16149 if (updir == NULL)
16150 return FAIL;
16151 if (mch_isdir(updir))
16152 r = OK;
16153 else if (mkdir_recurse(updir, prot) == OK)
16154 r = vim_mkdir_emsg(updir, prot);
16155 vim_free(updir);
16156 return r;
16157}
16158
16159#ifdef vim_mkdir
16160/*
16161 * "mkdir()" function
16162 */
16163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016164f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016165{
16166 char_u *dir;
16167 char_u buf[NUMBUFLEN];
16168 int prot = 0755;
16169
16170 rettv->vval.v_number = FAIL;
16171 if (check_restricted() || check_secure())
16172 return;
16173
16174 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016175 if (*dir == NUL)
16176 rettv->vval.v_number = FAIL;
16177 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016178 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016179 if (*gettail(dir) == NUL)
16180 /* remove trailing slashes */
16181 *gettail_sep(dir) = NUL;
16182
16183 if (argvars[1].v_type != VAR_UNKNOWN)
16184 {
16185 if (argvars[2].v_type != VAR_UNKNOWN)
16186 prot = get_tv_number_chk(&argvars[2], NULL);
16187 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16188 mkdir_recurse(dir, prot);
16189 }
16190 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016191 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016192}
16193#endif
16194
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 * "mode()" function
16197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016199f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016201 char_u buf[3];
16202
16203 buf[1] = NUL;
16204 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 if (VIsual_active)
16207 {
16208 if (VIsual_select)
16209 buf[0] = VIsual_mode + 's' - 'v';
16210 else
16211 buf[0] = VIsual_mode;
16212 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016213 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016214 || State == CONFIRM)
16215 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016217 if (State == ASKMORE)
16218 buf[1] = 'm';
16219 else if (State == CONFIRM)
16220 buf[1] = '?';
16221 }
16222 else if (State == EXTERNCMD)
16223 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 else if (State & INSERT)
16225 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016226#ifdef FEAT_VREPLACE
16227 if (State & VREPLACE_FLAG)
16228 {
16229 buf[0] = 'R';
16230 buf[1] = 'v';
16231 }
16232 else
16233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234 if (State & REPLACE_FLAG)
16235 buf[0] = 'R';
16236 else
16237 buf[0] = 'i';
16238 }
16239 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016242 if (exmode_active)
16243 buf[1] = 'v';
16244 }
16245 else if (exmode_active)
16246 {
16247 buf[0] = 'c';
16248 buf[1] = 'e';
16249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016250 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016251 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016253 if (finish_op)
16254 buf[1] = 'o';
16255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016257 /* Clear out the minor mode when the argument is not a non-zero number or
16258 * non-empty string. */
16259 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016260 buf[1] = NUL;
16261
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016262 rettv->vval.v_string = vim_strsave(buf);
16263 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264}
16265
Bram Moolenaar429fa852013-04-15 12:27:36 +020016266#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016267/*
16268 * "mzeval()" function
16269 */
16270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016271f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016272{
16273 char_u *str;
16274 char_u buf[NUMBUFLEN];
16275
16276 str = get_tv_string_buf(&argvars[0], buf);
16277 do_mzeval(str, rettv);
16278}
Bram Moolenaar75676462013-01-30 14:55:42 +010016279
16280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016281mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016282{
16283 typval_T argvars[3];
16284
16285 argvars[0].v_type = VAR_STRING;
16286 argvars[0].vval.v_string = name;
16287 copy_tv(args, &argvars[1]);
16288 argvars[2].v_type = VAR_UNKNOWN;
16289 f_call(argvars, rettv);
16290 clear_tv(&argvars[1]);
16291}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016292#endif
16293
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295 * "nextnonblank()" function
16296 */
16297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016298f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016299{
16300 linenr_T lnum;
16301
16302 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016304 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016305 {
16306 lnum = 0;
16307 break;
16308 }
16309 if (*skipwhite(ml_get(lnum)) != NUL)
16310 break;
16311 }
16312 rettv->vval.v_number = lnum;
16313}
16314
16315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316 * "nr2char()" function
16317 */
16318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016319f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320{
16321 char_u buf[NUMBUFLEN];
16322
16323#ifdef FEAT_MBYTE
16324 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016325 {
16326 int utf8 = 0;
16327
16328 if (argvars[1].v_type != VAR_UNKNOWN)
16329 utf8 = get_tv_number_chk(&argvars[1], NULL);
16330 if (utf8)
16331 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16332 else
16333 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 else
16336#endif
16337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016338 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 buf[1] = NUL;
16340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016341 rettv->v_type = VAR_STRING;
16342 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016343}
16344
16345/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016346 * "or(expr, expr)" function
16347 */
16348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016349f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016350{
16351 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16352 | get_tv_number_chk(&argvars[1], NULL);
16353}
16354
16355/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016356 * "pathshorten()" function
16357 */
16358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016359f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016360{
16361 char_u *p;
16362
16363 rettv->v_type = VAR_STRING;
16364 p = get_tv_string_chk(&argvars[0]);
16365 if (p == NULL)
16366 rettv->vval.v_string = NULL;
16367 else
16368 {
16369 p = vim_strsave(p);
16370 rettv->vval.v_string = p;
16371 if (p != NULL)
16372 shorten_dir(p);
16373 }
16374}
16375
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016376#ifdef FEAT_PERL
16377/*
16378 * "perleval()" function
16379 */
16380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016381f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016382{
16383 char_u *str;
16384 char_u buf[NUMBUFLEN];
16385
16386 str = get_tv_string_buf(&argvars[0], buf);
16387 do_perleval(str, rettv);
16388}
16389#endif
16390
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016391#ifdef FEAT_FLOAT
16392/*
16393 * "pow()" function
16394 */
16395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016396f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016397{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016398 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016399
16400 rettv->v_type = VAR_FLOAT;
16401 if (get_float_arg(argvars, &fx) == OK
16402 && get_float_arg(&argvars[1], &fy) == OK)
16403 rettv->vval.v_float = pow(fx, fy);
16404 else
16405 rettv->vval.v_float = 0.0;
16406}
16407#endif
16408
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016409/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410 * "prevnonblank()" function
16411 */
16412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016413f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414{
16415 linenr_T lnum;
16416
16417 lnum = get_tv_lnum(argvars);
16418 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16419 lnum = 0;
16420 else
16421 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16422 --lnum;
16423 rettv->vval.v_number = lnum;
16424}
16425
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016426/* This dummy va_list is here because:
16427 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16428 * - locally in the function results in a "used before set" warning
16429 * - using va_start() to initialize it gives "function with fixed args" error */
16430static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016431
Bram Moolenaar8c711452005-01-14 21:53:12 +000016432/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016433 * "printf()" function
16434 */
16435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016436f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016437{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016438 char_u buf[NUMBUFLEN];
16439 int len;
16440 char_u *s;
16441 int saved_did_emsg = did_emsg;
16442 char *fmt;
16443
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016444 rettv->v_type = VAR_STRING;
16445 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016446
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016447 /* Get the required length, allocate the buffer and do it for real. */
16448 did_emsg = FALSE;
16449 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16450 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16451 if (!did_emsg)
16452 {
16453 s = alloc(len + 1);
16454 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016455 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016456 rettv->vval.v_string = s;
16457 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016458 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016459 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016460 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016461}
16462
16463/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016464 * "pumvisible()" function
16465 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016467f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016468{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016469#ifdef FEAT_INS_EXPAND
16470 if (pum_visible())
16471 rettv->vval.v_number = 1;
16472#endif
16473}
16474
Bram Moolenaardb913952012-06-29 12:54:53 +020016475#ifdef FEAT_PYTHON3
16476/*
16477 * "py3eval()" function
16478 */
16479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016480f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016481{
16482 char_u *str;
16483 char_u buf[NUMBUFLEN];
16484
16485 str = get_tv_string_buf(&argvars[0], buf);
16486 do_py3eval(str, rettv);
16487}
16488#endif
16489
16490#ifdef FEAT_PYTHON
16491/*
16492 * "pyeval()" function
16493 */
16494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016495f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016496{
16497 char_u *str;
16498 char_u buf[NUMBUFLEN];
16499
16500 str = get_tv_string_buf(&argvars[0], buf);
16501 do_pyeval(str, rettv);
16502}
16503#endif
16504
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016505/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016506 * "range()" function
16507 */
16508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016509f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016510{
16511 long start;
16512 long end;
16513 long stride = 1;
16514 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016515 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016517 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016518 if (argvars[1].v_type == VAR_UNKNOWN)
16519 {
16520 end = start - 1;
16521 start = 0;
16522 }
16523 else
16524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016525 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016526 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016527 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016528 }
16529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016530 if (error)
16531 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016532 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016533 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016534 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016535 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016536 else
16537 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016538 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016539 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016540 if (list_append_number(rettv->vval.v_list,
16541 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016542 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016543 }
16544}
16545
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016546/*
16547 * "readfile()" function
16548 */
16549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016550f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016551{
16552 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016553 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016554 char_u *fname;
16555 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016556 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16557 int io_size = sizeof(buf);
16558 int readlen; /* size of last fread() */
16559 char_u *prev = NULL; /* previously read bytes, if any */
16560 long prevlen = 0; /* length of data in prev */
16561 long prevsize = 0; /* size of prev buffer */
16562 long maxline = MAXLNUM;
16563 long cnt = 0;
16564 char_u *p; /* position in buf */
16565 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016566
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016567 if (argvars[1].v_type != VAR_UNKNOWN)
16568 {
16569 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16570 binary = TRUE;
16571 if (argvars[2].v_type != VAR_UNKNOWN)
16572 maxline = get_tv_number(&argvars[2]);
16573 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016574
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016575 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016576 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016577
16578 /* Always open the file in binary mode, library functions have a mind of
16579 * their own about CR-LF conversion. */
16580 fname = get_tv_string(&argvars[0]);
16581 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16582 {
16583 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16584 return;
16585 }
16586
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016587 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016588 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016589 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016590
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016591 /* This for loop processes what was read, but is also entered at end
16592 * of file so that either:
16593 * - an incomplete line gets written
16594 * - a "binary" file gets an empty line at the end if it ends in a
16595 * newline. */
16596 for (p = buf, start = buf;
16597 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16598 ++p)
16599 {
16600 if (*p == '\n' || readlen <= 0)
16601 {
16602 listitem_T *li;
16603 char_u *s = NULL;
16604 long_u len = p - start;
16605
16606 /* Finished a line. Remove CRs before NL. */
16607 if (readlen > 0 && !binary)
16608 {
16609 while (len > 0 && start[len - 1] == '\r')
16610 --len;
16611 /* removal may cross back to the "prev" string */
16612 if (len == 0)
16613 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16614 --prevlen;
16615 }
16616 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016617 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016618 else
16619 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016620 /* Change "prev" buffer to be the right size. This way
16621 * the bytes are only copied once, and very long lines are
16622 * allocated only once. */
16623 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016624 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016625 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016626 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016627 prev = NULL; /* the list will own the string */
16628 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016629 }
16630 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016631 if (s == NULL)
16632 {
16633 do_outofmem_msg((long_u) prevlen + len + 1);
16634 failed = TRUE;
16635 break;
16636 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016637
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016638 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016639 {
16640 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016641 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016642 break;
16643 }
16644 li->li_tv.v_type = VAR_STRING;
16645 li->li_tv.v_lock = 0;
16646 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016647 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016648
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016649 start = p + 1; /* step over newline */
16650 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016651 break;
16652 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016653 else if (*p == NUL)
16654 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016655#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016656 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16657 * when finding the BF and check the previous two bytes. */
16658 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016659 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016660 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16661 * + 1, these may be in the "prev" string. */
16662 char_u back1 = p >= buf + 1 ? p[-1]
16663 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16664 char_u back2 = p >= buf + 2 ? p[-2]
16665 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16666 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016667
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016668 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016669 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016670 char_u *dest = p - 2;
16671
16672 /* Usually a BOM is at the beginning of a file, and so at
16673 * the beginning of a line; then we can just step over it.
16674 */
16675 if (start == dest)
16676 start = p + 1;
16677 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016678 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016679 /* have to shuffle buf to close gap */
16680 int adjust_prevlen = 0;
16681
16682 if (dest < buf)
16683 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016684 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016685 dest = buf;
16686 }
16687 if (readlen > p - buf + 1)
16688 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16689 readlen -= 3 - adjust_prevlen;
16690 prevlen -= adjust_prevlen;
16691 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016692 }
16693 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016694 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016695#endif
16696 } /* for */
16697
16698 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16699 break;
16700 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016701 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016702 /* There's part of a line in buf, store it in "prev". */
16703 if (p - start + prevlen >= prevsize)
16704 {
16705 /* need bigger "prev" buffer */
16706 char_u *newprev;
16707
16708 /* A common use case is ordinary text files and "prev" gets a
16709 * fragment of a line, so the first allocation is made
16710 * small, to avoid repeatedly 'allocing' large and
16711 * 'reallocing' small. */
16712 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016713 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016714 else
16715 {
16716 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016717 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016718 prevsize = grow50pc > growmin ? grow50pc : growmin;
16719 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016720 newprev = prev == NULL ? alloc(prevsize)
16721 : vim_realloc(prev, prevsize);
16722 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016723 {
16724 do_outofmem_msg((long_u)prevsize);
16725 failed = TRUE;
16726 break;
16727 }
16728 prev = newprev;
16729 }
16730 /* Add the line part to end of "prev". */
16731 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016732 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016733 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016734 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016735
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016736 /*
16737 * For a negative line count use only the lines at the end of the file,
16738 * free the rest.
16739 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016740 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016741 while (cnt > -maxline)
16742 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016743 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016744 --cnt;
16745 }
16746
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016747 if (failed)
16748 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016749 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016750 /* readfile doc says an empty list is returned on error */
16751 rettv->vval.v_list = list_alloc();
16752 }
16753
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016754 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016755 fclose(fd);
16756}
16757
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016758#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016759static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016760
16761/*
16762 * Convert a List to proftime_T.
16763 * Return FAIL when there is something wrong.
16764 */
16765 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016766list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016767{
16768 long n1, n2;
16769 int error = FALSE;
16770
16771 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16772 || arg->vval.v_list->lv_len != 2)
16773 return FAIL;
16774 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16775 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16776# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016777 tm->HighPart = n1;
16778 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016779# else
16780 tm->tv_sec = n1;
16781 tm->tv_usec = n2;
16782# endif
16783 return error ? FAIL : OK;
16784}
16785#endif /* FEAT_RELTIME */
16786
16787/*
16788 * "reltime()" function
16789 */
16790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016791f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016792{
16793#ifdef FEAT_RELTIME
16794 proftime_T res;
16795 proftime_T start;
16796
16797 if (argvars[0].v_type == VAR_UNKNOWN)
16798 {
16799 /* No arguments: get current time. */
16800 profile_start(&res);
16801 }
16802 else if (argvars[1].v_type == VAR_UNKNOWN)
16803 {
16804 if (list2proftime(&argvars[0], &res) == FAIL)
16805 return;
16806 profile_end(&res);
16807 }
16808 else
16809 {
16810 /* Two arguments: compute the difference. */
16811 if (list2proftime(&argvars[0], &start) == FAIL
16812 || list2proftime(&argvars[1], &res) == FAIL)
16813 return;
16814 profile_sub(&res, &start);
16815 }
16816
16817 if (rettv_list_alloc(rettv) == OK)
16818 {
16819 long n1, n2;
16820
16821# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016822 n1 = res.HighPart;
16823 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016824# else
16825 n1 = res.tv_sec;
16826 n2 = res.tv_usec;
16827# endif
16828 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16829 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16830 }
16831#endif
16832}
16833
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016834#ifdef FEAT_FLOAT
16835/*
16836 * "reltimefloat()" function
16837 */
16838 static void
16839f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16840{
16841# ifdef FEAT_RELTIME
16842 proftime_T tm;
16843# endif
16844
16845 rettv->v_type = VAR_FLOAT;
16846 rettv->vval.v_float = 0;
16847# ifdef FEAT_RELTIME
16848 if (list2proftime(&argvars[0], &tm) == OK)
16849 rettv->vval.v_float = profile_float(&tm);
16850# endif
16851}
16852#endif
16853
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016854/*
16855 * "reltimestr()" function
16856 */
16857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016858f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016859{
16860#ifdef FEAT_RELTIME
16861 proftime_T tm;
16862#endif
16863
16864 rettv->v_type = VAR_STRING;
16865 rettv->vval.v_string = NULL;
16866#ifdef FEAT_RELTIME
16867 if (list2proftime(&argvars[0], &tm) == OK)
16868 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16869#endif
16870}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016871
Bram Moolenaar0d660222005-01-07 21:51:51 +000016872#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016873static void make_connection(void);
16874static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875
16876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016877make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016878{
16879 if (X_DISPLAY == NULL
16880# ifdef FEAT_GUI
16881 && !gui.in_use
16882# endif
16883 )
16884 {
16885 x_force_connect = TRUE;
16886 setup_term_clip();
16887 x_force_connect = FALSE;
16888 }
16889}
16890
16891 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016892check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016893{
16894 make_connection();
16895 if (X_DISPLAY == NULL)
16896 {
16897 EMSG(_("E240: No connection to Vim server"));
16898 return FAIL;
16899 }
16900 return OK;
16901}
16902#endif
16903
16904#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016906remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907{
16908 char_u *server_name;
16909 char_u *keys;
16910 char_u *r = NULL;
16911 char_u buf[NUMBUFLEN];
16912# ifdef WIN32
16913 HWND w;
16914# else
16915 Window w;
16916# endif
16917
16918 if (check_restricted() || check_secure())
16919 return;
16920
16921# ifdef FEAT_X11
16922 if (check_connection() == FAIL)
16923 return;
16924# endif
16925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016926 server_name = get_tv_string_chk(&argvars[0]);
16927 if (server_name == NULL)
16928 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929 keys = get_tv_string_buf(&argvars[1], buf);
16930# ifdef WIN32
16931 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16932# else
16933 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16934 < 0)
16935# endif
16936 {
16937 if (r != NULL)
16938 EMSG(r); /* sending worked but evaluation failed */
16939 else
16940 EMSG2(_("E241: Unable to send to %s"), server_name);
16941 return;
16942 }
16943
16944 rettv->vval.v_string = r;
16945
16946 if (argvars[2].v_type != VAR_UNKNOWN)
16947 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016948 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016949 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016951
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016952 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016953 v.di_tv.v_type = VAR_STRING;
16954 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016955 idvar = get_tv_string_chk(&argvars[2]);
16956 if (idvar != NULL)
16957 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016958 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959 }
16960}
16961#endif
16962
16963/*
16964 * "remote_expr()" function
16965 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016967f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968{
16969 rettv->v_type = VAR_STRING;
16970 rettv->vval.v_string = NULL;
16971#ifdef FEAT_CLIENTSERVER
16972 remote_common(argvars, rettv, TRUE);
16973#endif
16974}
16975
16976/*
16977 * "remote_foreground()" function
16978 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016980f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016981{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982#ifdef FEAT_CLIENTSERVER
16983# ifdef WIN32
16984 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016985 {
16986 char_u *server_name = get_tv_string_chk(&argvars[0]);
16987
16988 if (server_name != NULL)
16989 serverForeground(server_name);
16990 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991# else
16992 /* Send a foreground() expression to the server. */
16993 argvars[1].v_type = VAR_STRING;
16994 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16995 argvars[2].v_type = VAR_UNKNOWN;
16996 remote_common(argvars, rettv, TRUE);
16997 vim_free(argvars[1].vval.v_string);
16998# endif
16999#endif
17000}
17001
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017003f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004{
17005#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017006 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007 char_u *s = NULL;
17008# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017009 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017010# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017011 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012
17013 if (check_restricted() || check_secure())
17014 {
17015 rettv->vval.v_number = -1;
17016 return;
17017 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 serverid = get_tv_string_chk(&argvars[0]);
17019 if (serverid == NULL)
17020 {
17021 rettv->vval.v_number = -1;
17022 return; /* type error; errmsg already given */
17023 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017025 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026 if (n == 0)
17027 rettv->vval.v_number = -1;
17028 else
17029 {
17030 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17031 rettv->vval.v_number = (s != NULL);
17032 }
17033# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034 if (check_connection() == FAIL)
17035 return;
17036
17037 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017038 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017039# endif
17040
17041 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017043 char_u *retvar;
17044
Bram Moolenaar33570922005-01-25 22:26:29 +000017045 v.di_tv.v_type = VAR_STRING;
17046 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017047 retvar = get_tv_string_chk(&argvars[1]);
17048 if (retvar != NULL)
17049 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017050 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017051 }
17052#else
17053 rettv->vval.v_number = -1;
17054#endif
17055}
17056
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017058f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017059{
17060 char_u *r = NULL;
17061
17062#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017063 char_u *serverid = get_tv_string_chk(&argvars[0]);
17064
17065 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017066 {
17067# ifdef WIN32
17068 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017069 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017070
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017071 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072 if (n != 0)
17073 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17074 if (r == NULL)
17075# else
17076 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017077 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078# endif
17079 EMSG(_("E277: Unable to read a server reply"));
17080 }
17081#endif
17082 rettv->v_type = VAR_STRING;
17083 rettv->vval.v_string = r;
17084}
17085
17086/*
17087 * "remote_send()" function
17088 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017090f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091{
17092 rettv->v_type = VAR_STRING;
17093 rettv->vval.v_string = NULL;
17094#ifdef FEAT_CLIENTSERVER
17095 remote_common(argvars, rettv, FALSE);
17096#endif
17097}
17098
17099/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017100 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017101 */
17102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017103f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017104{
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 list_T *l;
17106 listitem_T *item, *item2;
17107 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017108 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017109 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017110 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017111 dict_T *d;
17112 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017113 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017114
Bram Moolenaar8c711452005-01-14 21:53:12 +000017115 if (argvars[0].v_type == VAR_DICT)
17116 {
17117 if (argvars[2].v_type != VAR_UNKNOWN)
17118 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017119 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017120 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017121 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017122 key = get_tv_string_chk(&argvars[1]);
17123 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017125 di = dict_find(d, key, -1);
17126 if (di == NULL)
17127 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017128 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17129 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017130 {
17131 *rettv = di->di_tv;
17132 init_tv(&di->di_tv);
17133 dictitem_remove(d, di);
17134 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017135 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017136 }
17137 }
17138 else if (argvars[0].v_type != VAR_LIST)
17139 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017140 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017141 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017143 int error = FALSE;
17144
17145 idx = get_tv_number_chk(&argvars[1], &error);
17146 if (error)
17147 ; /* type error: do nothing, errmsg already given */
17148 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017149 EMSGN(_(e_listidx), idx);
17150 else
17151 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017152 if (argvars[2].v_type == VAR_UNKNOWN)
17153 {
17154 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017155 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017156 *rettv = item->li_tv;
17157 vim_free(item);
17158 }
17159 else
17160 {
17161 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017162 end = get_tv_number_chk(&argvars[2], &error);
17163 if (error)
17164 ; /* type error: do nothing */
17165 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017166 EMSGN(_(e_listidx), end);
17167 else
17168 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017169 int cnt = 0;
17170
17171 for (li = item; li != NULL; li = li->li_next)
17172 {
17173 ++cnt;
17174 if (li == item2)
17175 break;
17176 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017177 if (li == NULL) /* didn't find "item2" after "item" */
17178 EMSG(_(e_invrange));
17179 else
17180 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017181 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017182 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017183 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017184 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017185 l->lv_first = item;
17186 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017187 item->li_prev = NULL;
17188 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017189 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017190 }
17191 }
17192 }
17193 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017194 }
17195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196}
17197
17198/*
17199 * "rename({from}, {to})" function
17200 */
17201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017202f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203{
17204 char_u buf[NUMBUFLEN];
17205
17206 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017207 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017209 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17210 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211}
17212
17213/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017214 * "repeat()" function
17215 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017217f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017218{
17219 char_u *p;
17220 int n;
17221 int slen;
17222 int len;
17223 char_u *r;
17224 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017225
17226 n = get_tv_number(&argvars[1]);
17227 if (argvars[0].v_type == VAR_LIST)
17228 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017229 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017230 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017231 if (list_extend(rettv->vval.v_list,
17232 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017233 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017234 }
17235 else
17236 {
17237 p = get_tv_string(&argvars[0]);
17238 rettv->v_type = VAR_STRING;
17239 rettv->vval.v_string = NULL;
17240
17241 slen = (int)STRLEN(p);
17242 len = slen * n;
17243 if (len <= 0)
17244 return;
17245
17246 r = alloc(len + 1);
17247 if (r != NULL)
17248 {
17249 for (i = 0; i < n; i++)
17250 mch_memmove(r + i * slen, p, (size_t)slen);
17251 r[len] = NUL;
17252 }
17253
17254 rettv->vval.v_string = r;
17255 }
17256}
17257
17258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017259 * "resolve()" function
17260 */
17261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017262f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263{
17264 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017265#ifdef HAVE_READLINK
17266 char_u *buf = NULL;
17267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017269 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270#ifdef FEAT_SHORTCUT
17271 {
17272 char_u *v = NULL;
17273
17274 v = mch_resolve_shortcut(p);
17275 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017276 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017278 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 }
17280#else
17281# ifdef HAVE_READLINK
17282 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 char_u *cpy;
17284 int len;
17285 char_u *remain = NULL;
17286 char_u *q;
17287 int is_relative_to_current = FALSE;
17288 int has_trailing_pathsep = FALSE;
17289 int limit = 100;
17290
17291 p = vim_strsave(p);
17292
17293 if (p[0] == '.' && (vim_ispathsep(p[1])
17294 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17295 is_relative_to_current = TRUE;
17296
17297 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017298 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017299 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017301 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303
17304 q = getnextcomp(p);
17305 if (*q != NUL)
17306 {
17307 /* Separate the first path component in "p", and keep the
17308 * remainder (beginning with the path separator). */
17309 remain = vim_strsave(q - 1);
17310 q[-1] = NUL;
17311 }
17312
Bram Moolenaard9462e32011-04-11 21:35:11 +020017313 buf = alloc(MAXPATHL + 1);
17314 if (buf == NULL)
17315 goto fail;
17316
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 for (;;)
17318 {
17319 for (;;)
17320 {
17321 len = readlink((char *)p, (char *)buf, MAXPATHL);
17322 if (len <= 0)
17323 break;
17324 buf[len] = NUL;
17325
17326 if (limit-- == 0)
17327 {
17328 vim_free(p);
17329 vim_free(remain);
17330 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017331 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 goto fail;
17333 }
17334
17335 /* Ensure that the result will have a trailing path separator
17336 * if the argument has one. */
17337 if (remain == NULL && has_trailing_pathsep)
17338 add_pathsep(buf);
17339
17340 /* Separate the first path component in the link value and
17341 * concatenate the remainders. */
17342 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17343 if (*q != NUL)
17344 {
17345 if (remain == NULL)
17346 remain = vim_strsave(q - 1);
17347 else
17348 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017349 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 if (cpy != NULL)
17351 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 vim_free(remain);
17353 remain = cpy;
17354 }
17355 }
17356 q[-1] = NUL;
17357 }
17358
17359 q = gettail(p);
17360 if (q > p && *q == NUL)
17361 {
17362 /* Ignore trailing path separator. */
17363 q[-1] = NUL;
17364 q = gettail(p);
17365 }
17366 if (q > p && !mch_isFullName(buf))
17367 {
17368 /* symlink is relative to directory of argument */
17369 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17370 if (cpy != NULL)
17371 {
17372 STRCPY(cpy, p);
17373 STRCPY(gettail(cpy), buf);
17374 vim_free(p);
17375 p = cpy;
17376 }
17377 }
17378 else
17379 {
17380 vim_free(p);
17381 p = vim_strsave(buf);
17382 }
17383 }
17384
17385 if (remain == NULL)
17386 break;
17387
17388 /* Append the first path component of "remain" to "p". */
17389 q = getnextcomp(remain + 1);
17390 len = q - remain - (*q != NUL);
17391 cpy = vim_strnsave(p, STRLEN(p) + len);
17392 if (cpy != NULL)
17393 {
17394 STRNCAT(cpy, remain, len);
17395 vim_free(p);
17396 p = cpy;
17397 }
17398 /* Shorten "remain". */
17399 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017400 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 else
17402 {
17403 vim_free(remain);
17404 remain = NULL;
17405 }
17406 }
17407
17408 /* If the result is a relative path name, make it explicitly relative to
17409 * the current directory if and only if the argument had this form. */
17410 if (!vim_ispathsep(*p))
17411 {
17412 if (is_relative_to_current
17413 && *p != NUL
17414 && !(p[0] == '.'
17415 && (p[1] == NUL
17416 || vim_ispathsep(p[1])
17417 || (p[1] == '.'
17418 && (p[2] == NUL
17419 || vim_ispathsep(p[2]))))))
17420 {
17421 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017422 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 if (cpy != NULL)
17424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 vim_free(p);
17426 p = cpy;
17427 }
17428 }
17429 else if (!is_relative_to_current)
17430 {
17431 /* Strip leading "./". */
17432 q = p;
17433 while (q[0] == '.' && vim_ispathsep(q[1]))
17434 q += 2;
17435 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017436 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 }
17438 }
17439
17440 /* Ensure that the result will have no trailing path separator
17441 * if the argument had none. But keep "/" or "//". */
17442 if (!has_trailing_pathsep)
17443 {
17444 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017445 if (after_pathsep(p, q))
17446 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 }
17448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017449 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 }
17451# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017452 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453# endif
17454#endif
17455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017456 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457
17458#ifdef HAVE_READLINK
17459fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017460 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017462 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463}
17464
17465/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017466 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467 */
17468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017469f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470{
Bram Moolenaar33570922005-01-25 22:26:29 +000017471 list_T *l;
17472 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473
Bram Moolenaar0d660222005-01-07 21:51:51 +000017474 if (argvars[0].v_type != VAR_LIST)
17475 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017476 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017477 && !tv_check_lock(l->lv_lock,
17478 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017479 {
17480 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017481 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017482 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017483 while (li != NULL)
17484 {
17485 ni = li->li_prev;
17486 list_append(l, li);
17487 li = ni;
17488 }
17489 rettv->vval.v_list = l;
17490 rettv->v_type = VAR_LIST;
17491 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017492 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494}
17495
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017496#define SP_NOMOVE 0x01 /* don't move cursor */
17497#define SP_REPEAT 0x02 /* repeat to find outer pair */
17498#define SP_RETCOUNT 0x04 /* return matchcount */
17499#define SP_SETPCMARK 0x08 /* set previous context mark */
17500#define SP_START 0x10 /* accept match at start position */
17501#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17502#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017503#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017504
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017505static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017506
17507/*
17508 * Get flags for a search function.
17509 * Possibly sets "p_ws".
17510 * Returns BACKWARD, FORWARD or zero (for an error).
17511 */
17512 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017513get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017514{
17515 int dir = FORWARD;
17516 char_u *flags;
17517 char_u nbuf[NUMBUFLEN];
17518 int mask;
17519
17520 if (varp->v_type != VAR_UNKNOWN)
17521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017522 flags = get_tv_string_buf_chk(varp, nbuf);
17523 if (flags == NULL)
17524 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017525 while (*flags != NUL)
17526 {
17527 switch (*flags)
17528 {
17529 case 'b': dir = BACKWARD; break;
17530 case 'w': p_ws = TRUE; break;
17531 case 'W': p_ws = FALSE; break;
17532 default: mask = 0;
17533 if (flagsp != NULL)
17534 switch (*flags)
17535 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017536 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017537 case 'e': mask = SP_END; break;
17538 case 'm': mask = SP_RETCOUNT; break;
17539 case 'n': mask = SP_NOMOVE; break;
17540 case 'p': mask = SP_SUBPAT; break;
17541 case 'r': mask = SP_REPEAT; break;
17542 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017543 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017544 }
17545 if (mask == 0)
17546 {
17547 EMSG2(_(e_invarg2), flags);
17548 dir = 0;
17549 }
17550 else
17551 *flagsp |= mask;
17552 }
17553 if (dir == 0)
17554 break;
17555 ++flags;
17556 }
17557 }
17558 return dir;
17559}
17560
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017562 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017565search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017567 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568 char_u *pat;
17569 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017570 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 int save_p_ws = p_ws;
17572 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017573 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017574 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017575 proftime_T tm;
17576#ifdef FEAT_RELTIME
17577 long time_limit = 0;
17578#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017579 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017580 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017582 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017583 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017584 if (dir == 0)
17585 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017586 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017587 if (flags & SP_START)
17588 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017589 if (flags & SP_END)
17590 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017591 if (flags & SP_COLUMN)
17592 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017593
Bram Moolenaar76929292008-01-06 19:07:36 +000017594 /* Optional arguments: line number to stop searching and timeout. */
17595 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017596 {
17597 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17598 if (lnum_stop < 0)
17599 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017600#ifdef FEAT_RELTIME
17601 if (argvars[3].v_type != VAR_UNKNOWN)
17602 {
17603 time_limit = get_tv_number_chk(&argvars[3], NULL);
17604 if (time_limit < 0)
17605 goto theend;
17606 }
17607#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017608 }
17609
Bram Moolenaar76929292008-01-06 19:07:36 +000017610#ifdef FEAT_RELTIME
17611 /* Set the time limit, if there is one. */
17612 profile_setlimit(time_limit, &tm);
17613#endif
17614
Bram Moolenaar231334e2005-07-25 20:46:57 +000017615 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017616 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017617 * Check to make sure only those flags are set.
17618 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17619 * flags cannot be set. Check for that condition also.
17620 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017621 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017622 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017624 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017625 goto theend;
17626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017628 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017629 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017630 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017631 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017633 if (flags & SP_SUBPAT)
17634 retval = subpatnum;
17635 else
17636 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017637 if (flags & SP_SETPCMARK)
17638 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017640 if (match_pos != NULL)
17641 {
17642 /* Store the match cursor position */
17643 match_pos->lnum = pos.lnum;
17644 match_pos->col = pos.col + 1;
17645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 /* "/$" will put the cursor after the end of the line, may need to
17647 * correct that here */
17648 check_cursor();
17649 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017650
17651 /* If 'n' flag is used: restore cursor position. */
17652 if (flags & SP_NOMOVE)
17653 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017654 else
17655 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017656theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017658
17659 return retval;
17660}
17661
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017662#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017663
17664/*
17665 * round() is not in C90, use ceil() or floor() instead.
17666 */
17667 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017668vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017669{
17670 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17671}
17672
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017673/*
17674 * "round({float})" function
17675 */
17676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017677f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017678{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017679 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017680
17681 rettv->v_type = VAR_FLOAT;
17682 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017683 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017684 else
17685 rettv->vval.v_float = 0.0;
17686}
17687#endif
17688
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017689/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017690 * "screenattr()" function
17691 */
17692 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017693f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017694{
17695 int row;
17696 int col;
17697 int c;
17698
17699 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17700 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17701 if (row < 0 || row >= screen_Rows
17702 || col < 0 || col >= screen_Columns)
17703 c = -1;
17704 else
17705 c = ScreenAttrs[LineOffset[row] + col];
17706 rettv->vval.v_number = c;
17707}
17708
17709/*
17710 * "screenchar()" function
17711 */
17712 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017713f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017714{
17715 int row;
17716 int col;
17717 int off;
17718 int c;
17719
17720 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17721 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17722 if (row < 0 || row >= screen_Rows
17723 || col < 0 || col >= screen_Columns)
17724 c = -1;
17725 else
17726 {
17727 off = LineOffset[row] + col;
17728#ifdef FEAT_MBYTE
17729 if (enc_utf8 && ScreenLinesUC[off] != 0)
17730 c = ScreenLinesUC[off];
17731 else
17732#endif
17733 c = ScreenLines[off];
17734 }
17735 rettv->vval.v_number = c;
17736}
17737
17738/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017739 * "screencol()" function
17740 *
17741 * First column is 1 to be consistent with virtcol().
17742 */
17743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017744f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017745{
17746 rettv->vval.v_number = screen_screencol() + 1;
17747}
17748
17749/*
17750 * "screenrow()" function
17751 */
17752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017753f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017754{
17755 rettv->vval.v_number = screen_screenrow() + 1;
17756}
17757
17758/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017759 * "search()" function
17760 */
17761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017762f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017763{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017764 int flags = 0;
17765
17766 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017767}
17768
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017770 * "searchdecl()" function
17771 */
17772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017773f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017774{
17775 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017776 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017777 int error = FALSE;
17778 char_u *name;
17779
17780 rettv->vval.v_number = 1; /* default: FAIL */
17781
17782 name = get_tv_string_chk(&argvars[0]);
17783 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017784 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017785 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017786 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17787 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17788 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017789 if (!error && name != NULL)
17790 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017791 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017792}
17793
17794/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017795 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017797 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017798searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799{
17800 char_u *spat, *mpat, *epat;
17801 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 int dir;
17804 int flags = 0;
17805 char_u nbuf1[NUMBUFLEN];
17806 char_u nbuf2[NUMBUFLEN];
17807 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017808 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017809 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017810 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017813 spat = get_tv_string_chk(&argvars[0]);
17814 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17815 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17816 if (spat == NULL || mpat == NULL || epat == NULL)
17817 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819 /* Handle the optional fourth argument: flags */
17820 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017821 if (dir == 0)
17822 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017823
17824 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017825 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17826 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017827 if ((flags & (SP_END | SP_SUBPAT)) != 0
17828 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017829 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017830 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017831 goto theend;
17832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017834 /* Using 'r' implies 'W', otherwise it doesn't work. */
17835 if (flags & SP_REPEAT)
17836 p_ws = FALSE;
17837
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017838 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017839 if (argvars[3].v_type == VAR_UNKNOWN
17840 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017841 skip = (char_u *)"";
17842 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017844 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017845 if (argvars[5].v_type != VAR_UNKNOWN)
17846 {
17847 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17848 if (lnum_stop < 0)
17849 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017850#ifdef FEAT_RELTIME
17851 if (argvars[6].v_type != VAR_UNKNOWN)
17852 {
17853 time_limit = get_tv_number_chk(&argvars[6], NULL);
17854 if (time_limit < 0)
17855 goto theend;
17856 }
17857#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017858 }
17859 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017860 if (skip == NULL)
17861 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017863 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017864 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017865
17866theend:
17867 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017868
17869 return retval;
17870}
17871
17872/*
17873 * "searchpair()" function
17874 */
17875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017876f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017877{
17878 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17879}
17880
17881/*
17882 * "searchpairpos()" function
17883 */
17884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017885f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017886{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017887 pos_T match_pos;
17888 int lnum = 0;
17889 int col = 0;
17890
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017891 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017892 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017893
17894 if (searchpair_cmn(argvars, &match_pos) > 0)
17895 {
17896 lnum = match_pos.lnum;
17897 col = match_pos.col;
17898 }
17899
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017900 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17901 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017902}
17903
17904/*
17905 * Search for a start/middle/end thing.
17906 * Used by searchpair(), see its documentation for the details.
17907 * Returns 0 or -1 for no match,
17908 */
17909 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017910do_searchpair(
17911 char_u *spat, /* start pattern */
17912 char_u *mpat, /* middle pattern */
17913 char_u *epat, /* end pattern */
17914 int dir, /* BACKWARD or FORWARD */
17915 char_u *skip, /* skip expression */
17916 int flags, /* SP_SETPCMARK and other SP_ values */
17917 pos_T *match_pos,
17918 linenr_T lnum_stop, /* stop at this line if not zero */
17919 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017920{
17921 char_u *save_cpo;
17922 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17923 long retval = 0;
17924 pos_T pos;
17925 pos_T firstpos;
17926 pos_T foundpos;
17927 pos_T save_cursor;
17928 pos_T save_pos;
17929 int n;
17930 int r;
17931 int nest = 1;
17932 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017933 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017934 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017935
17936 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17937 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017938 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017939
Bram Moolenaar76929292008-01-06 19:07:36 +000017940#ifdef FEAT_RELTIME
17941 /* Set the time limit, if there is one. */
17942 profile_setlimit(time_limit, &tm);
17943#endif
17944
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017945 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17946 * start/middle/end (pat3, for the top pair). */
17947 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17948 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17949 if (pat2 == NULL || pat3 == NULL)
17950 goto theend;
17951 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17952 if (*mpat == NUL)
17953 STRCPY(pat3, pat2);
17954 else
17955 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17956 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017957 if (flags & SP_START)
17958 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017959
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 save_cursor = curwin->w_cursor;
17961 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017962 clearpos(&firstpos);
17963 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 pat = pat3;
17965 for (;;)
17966 {
17967 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017968 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17970 /* didn't find it or found the first match again: FAIL */
17971 break;
17972
17973 if (firstpos.lnum == 0)
17974 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017975 if (equalpos(pos, foundpos))
17976 {
17977 /* Found the same position again. Can happen with a pattern that
17978 * has "\zs" at the end and searching backwards. Advance one
17979 * character and try again. */
17980 if (dir == BACKWARD)
17981 decl(&pos);
17982 else
17983 incl(&pos);
17984 }
17985 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017986
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017987 /* clear the start flag to avoid getting stuck here */
17988 options &= ~SEARCH_START;
17989
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 /* If the skip pattern matches, ignore this match. */
17991 if (*skip != NUL)
17992 {
17993 save_pos = curwin->w_cursor;
17994 curwin->w_cursor = pos;
17995 r = eval_to_bool(skip, &err, NULL, FALSE);
17996 curwin->w_cursor = save_pos;
17997 if (err)
17998 {
17999 /* Evaluating {skip} caused an error, break here. */
18000 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018001 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002 break;
18003 }
18004 if (r)
18005 continue;
18006 }
18007
18008 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18009 {
18010 /* Found end when searching backwards or start when searching
18011 * forward: nested pair. */
18012 ++nest;
18013 pat = pat2; /* nested, don't search for middle */
18014 }
18015 else
18016 {
18017 /* Found end when searching forward or start when searching
18018 * backward: end of (nested) pair; or found middle in outer pair. */
18019 if (--nest == 1)
18020 pat = pat3; /* outer level, search for middle */
18021 }
18022
18023 if (nest == 0)
18024 {
18025 /* Found the match: return matchcount or line number. */
18026 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018027 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018029 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018030 if (flags & SP_SETPCMARK)
18031 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 curwin->w_cursor = pos;
18033 if (!(flags & SP_REPEAT))
18034 break;
18035 nest = 1; /* search for next unmatched */
18036 }
18037 }
18038
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018039 if (match_pos != NULL)
18040 {
18041 /* Store the match cursor position */
18042 match_pos->lnum = curwin->w_cursor.lnum;
18043 match_pos->col = curwin->w_cursor.col + 1;
18044 }
18045
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018047 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 curwin->w_cursor = save_cursor;
18049
18050theend:
18051 vim_free(pat2);
18052 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018053 if (p_cpo == empty_option)
18054 p_cpo = save_cpo;
18055 else
18056 /* Darn, evaluating the {skip} expression changed the value. */
18057 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018058
18059 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060}
18061
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018062/*
18063 * "searchpos()" function
18064 */
18065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018066f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018067{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018068 pos_T match_pos;
18069 int lnum = 0;
18070 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018071 int n;
18072 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018073
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018074 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018075 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018076
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018077 n = search_cmn(argvars, &match_pos, &flags);
18078 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018079 {
18080 lnum = match_pos.lnum;
18081 col = match_pos.col;
18082 }
18083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018084 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18085 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018086 if (flags & SP_SUBPAT)
18087 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018088}
18089
Bram Moolenaar0d660222005-01-07 21:51:51 +000018090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018091f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018093#ifdef FEAT_CLIENTSERVER
18094 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018095 char_u *server = get_tv_string_chk(&argvars[0]);
18096 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097
Bram Moolenaar0d660222005-01-07 21:51:51 +000018098 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018099 if (server == NULL || reply == NULL)
18100 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018101 if (check_restricted() || check_secure())
18102 return;
18103# ifdef FEAT_X11
18104 if (check_connection() == FAIL)
18105 return;
18106# endif
18107
18108 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018110 EMSG(_("E258: Unable to send to client"));
18111 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018113 rettv->vval.v_number = 0;
18114#else
18115 rettv->vval.v_number = -1;
18116#endif
18117}
18118
Bram Moolenaar0d660222005-01-07 21:51:51 +000018119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018120f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018121{
18122 char_u *r = NULL;
18123
18124#ifdef FEAT_CLIENTSERVER
18125# ifdef WIN32
18126 r = serverGetVimNames();
18127# else
18128 make_connection();
18129 if (X_DISPLAY != NULL)
18130 r = serverGetVimNames(X_DISPLAY);
18131# endif
18132#endif
18133 rettv->v_type = VAR_STRING;
18134 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135}
18136
18137/*
18138 * "setbufvar()" function
18139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018141f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142{
18143 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018146 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 char_u nbuf[NUMBUFLEN];
18148
18149 if (check_restricted() || check_secure())
18150 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018151 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18152 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018153 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 varp = &argvars[2];
18155
18156 if (buf != NULL && varname != NULL && varp != NULL)
18157 {
18158 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160
18161 if (*varname == '&')
18162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018163 long numval;
18164 char_u *strval;
18165 int error = FALSE;
18166
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018168 numval = get_tv_number_chk(varp, &error);
18169 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018170 if (!error && strval != NULL)
18171 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 }
18173 else
18174 {
18175 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18176 if (bufvarname != NULL)
18177 {
18178 STRCPY(bufvarname, "b:");
18179 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018180 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 vim_free(bufvarname);
18182 }
18183 }
18184
18185 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188}
18189
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018191f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018192{
18193 dict_T *d;
18194 dictitem_T *di;
18195 char_u *csearch;
18196
18197 if (argvars[0].v_type != VAR_DICT)
18198 {
18199 EMSG(_(e_dictreq));
18200 return;
18201 }
18202
18203 if ((d = argvars[0].vval.v_dict) != NULL)
18204 {
18205 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18206 if (csearch != NULL)
18207 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018208#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018209 if (enc_utf8)
18210 {
18211 int pcc[MAX_MCO];
18212 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018213
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018214 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18215 }
18216 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018217#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018218 set_last_csearch(PTR2CHAR(csearch),
18219 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018220 }
18221
18222 di = dict_find(d, (char_u *)"forward", -1);
18223 if (di != NULL)
18224 set_csearch_direction(get_tv_number(&di->di_tv)
18225 ? FORWARD : BACKWARD);
18226
18227 di = dict_find(d, (char_u *)"until", -1);
18228 if (di != NULL)
18229 set_csearch_until(!!get_tv_number(&di->di_tv));
18230 }
18231}
18232
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233/*
18234 * "setcmdpos()" function
18235 */
18236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018237f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018239 int pos = (int)get_tv_number(&argvars[0]) - 1;
18240
18241 if (pos >= 0)
18242 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243}
18244
18245/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018246 * "setfperm({fname}, {mode})" function
18247 */
18248 static void
18249f_setfperm(typval_T *argvars, typval_T *rettv)
18250{
18251 char_u *fname;
18252 char_u modebuf[NUMBUFLEN];
18253 char_u *mode_str;
18254 int i;
18255 int mask;
18256 int mode = 0;
18257
18258 rettv->vval.v_number = 0;
18259 fname = get_tv_string_chk(&argvars[0]);
18260 if (fname == NULL)
18261 return;
18262 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18263 if (mode_str == NULL)
18264 return;
18265 if (STRLEN(mode_str) != 9)
18266 {
18267 EMSG2(_(e_invarg2), mode_str);
18268 return;
18269 }
18270
18271 mask = 1;
18272 for (i = 8; i >= 0; --i)
18273 {
18274 if (mode_str[i] != '-')
18275 mode |= mask;
18276 mask = mask << 1;
18277 }
18278 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18279}
18280
18281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 * "setline()" function
18283 */
18284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018285f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286{
18287 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018288 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018289 list_T *l = NULL;
18290 listitem_T *li = NULL;
18291 long added = 0;
18292 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018294 lnum = get_tv_lnum(&argvars[0]);
18295 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018296 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018297 l = argvars[1].vval.v_list;
18298 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018300 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018301 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018302
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018303 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018304 for (;;)
18305 {
18306 if (l != NULL)
18307 {
18308 /* list argument, get next string */
18309 if (li == NULL)
18310 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018311 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018312 li = li->li_next;
18313 }
18314
18315 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018316 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018317 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018318
18319 /* When coming here from Insert mode, sync undo, so that this can be
18320 * undone separately from what was previously inserted. */
18321 if (u_sync_once == 2)
18322 {
18323 u_sync_once = 1; /* notify that u_sync() was called */
18324 u_sync(TRUE);
18325 }
18326
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018327 if (lnum <= curbuf->b_ml.ml_line_count)
18328 {
18329 /* existing line, replace it */
18330 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18331 {
18332 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018333 if (lnum == curwin->w_cursor.lnum)
18334 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018335 rettv->vval.v_number = 0; /* OK */
18336 }
18337 }
18338 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18339 {
18340 /* lnum is one past the last line, append the line */
18341 ++added;
18342 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18343 rettv->vval.v_number = 0; /* OK */
18344 }
18345
18346 if (l == NULL) /* only one string argument */
18347 break;
18348 ++lnum;
18349 }
18350
18351 if (added > 0)
18352 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353}
18354
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018355static 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 +000018356
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018358 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018359 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018361set_qf_ll_list(
18362 win_T *wp UNUSED,
18363 typval_T *list_arg UNUSED,
18364 typval_T *action_arg UNUSED,
18365 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018366{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018367#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018368 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018369 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018370 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018371#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018372
Bram Moolenaar2641f772005-03-25 21:58:17 +000018373 rettv->vval.v_number = -1;
18374
18375#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018376 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018377 EMSG(_(e_listreq));
18378 else
18379 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018380 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018381
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018382 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018383 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018384 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018385 if (act == NULL)
18386 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018387 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018388 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018389 else
18390 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018391 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018392 else if (action_arg->v_type == VAR_UNKNOWN)
18393 action = ' ';
18394 else
18395 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018396
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018397 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018398 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018399 rettv->vval.v_number = 0;
18400 }
18401#endif
18402}
18403
18404/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018405 * "setloclist()" function
18406 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018408f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018409{
18410 win_T *win;
18411
18412 rettv->vval.v_number = -1;
18413
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018414 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018415 if (win != NULL)
18416 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18417}
18418
18419/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018420 * "setmatches()" function
18421 */
18422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018423f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018424{
18425#ifdef FEAT_SEARCH_EXTRA
18426 list_T *l;
18427 listitem_T *li;
18428 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018429 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018430
18431 rettv->vval.v_number = -1;
18432 if (argvars[0].v_type != VAR_LIST)
18433 {
18434 EMSG(_(e_listreq));
18435 return;
18436 }
18437 if ((l = argvars[0].vval.v_list) != NULL)
18438 {
18439
18440 /* To some extent make sure that we are dealing with a list from
18441 * "getmatches()". */
18442 li = l->lv_first;
18443 while (li != NULL)
18444 {
18445 if (li->li_tv.v_type != VAR_DICT
18446 || (d = li->li_tv.vval.v_dict) == NULL)
18447 {
18448 EMSG(_(e_invarg));
18449 return;
18450 }
18451 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018452 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18453 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018454 && dict_find(d, (char_u *)"priority", -1) != NULL
18455 && dict_find(d, (char_u *)"id", -1) != NULL))
18456 {
18457 EMSG(_(e_invarg));
18458 return;
18459 }
18460 li = li->li_next;
18461 }
18462
18463 clear_matches(curwin);
18464 li = l->lv_first;
18465 while (li != NULL)
18466 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018467 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018468 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018469 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018470 char_u *group;
18471 int priority;
18472 int id;
18473 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018474
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018475 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018476 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18477 {
18478 if (s == NULL)
18479 {
18480 s = list_alloc();
18481 if (s == NULL)
18482 return;
18483 }
18484
18485 /* match from matchaddpos() */
18486 for (i = 1; i < 9; i++)
18487 {
18488 sprintf((char *)buf, (char *)"pos%d", i);
18489 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18490 {
18491 if (di->di_tv.v_type != VAR_LIST)
18492 return;
18493
18494 list_append_tv(s, &di->di_tv);
18495 s->lv_refcount++;
18496 }
18497 else
18498 break;
18499 }
18500 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018501
18502 group = get_dict_string(d, (char_u *)"group", FALSE);
18503 priority = (int)get_dict_number(d, (char_u *)"priority");
18504 id = (int)get_dict_number(d, (char_u *)"id");
18505 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18506 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18507 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018508 if (i == 0)
18509 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018510 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018511 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018512 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018513 }
18514 else
18515 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018516 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018517 list_unref(s);
18518 s = NULL;
18519 }
18520
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018521 li = li->li_next;
18522 }
18523 rettv->vval.v_number = 0;
18524 }
18525#endif
18526}
18527
18528/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018529 * "setpos()" function
18530 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018532f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018533{
18534 pos_T pos;
18535 int fnum;
18536 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018537 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018538
Bram Moolenaar08250432008-02-13 11:42:46 +000018539 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018540 name = get_tv_string_chk(argvars);
18541 if (name != NULL)
18542 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018543 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018544 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018545 if (--pos.col < 0)
18546 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018547 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018548 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018549 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018550 if (fnum == curbuf->b_fnum)
18551 {
18552 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018553 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018554 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018555 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018556 curwin->w_set_curswant = FALSE;
18557 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018558 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018559 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018560 }
18561 else
18562 EMSG(_(e_invarg));
18563 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018564 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18565 {
18566 /* set mark */
18567 if (setmark_pos(name[1], &pos, fnum) == OK)
18568 rettv->vval.v_number = 0;
18569 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018570 else
18571 EMSG(_(e_invarg));
18572 }
18573 }
18574}
18575
18576/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018577 * "setqflist()" function
18578 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018580f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018581{
18582 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18583}
18584
18585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586 * "setreg()" function
18587 */
18588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018589f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590{
18591 int regname;
18592 char_u *strregname;
18593 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018594 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 int append;
18596 char_u yank_type;
18597 long block_len;
18598
18599 block_len = -1;
18600 yank_type = MAUTO;
18601 append = FALSE;
18602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018603 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018604 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018606 if (strregname == NULL)
18607 return; /* type error; errmsg already given */
18608 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 if (regname == 0 || regname == '@')
18610 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018614 stropt = get_tv_string_chk(&argvars[2]);
18615 if (stropt == NULL)
18616 return; /* type error */
18617 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 switch (*stropt)
18619 {
18620 case 'a': case 'A': /* append */
18621 append = TRUE;
18622 break;
18623 case 'v': case 'c': /* character-wise selection */
18624 yank_type = MCHAR;
18625 break;
18626 case 'V': case 'l': /* line-wise selection */
18627 yank_type = MLINE;
18628 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 case 'b': case Ctrl_V: /* block-wise selection */
18630 yank_type = MBLOCK;
18631 if (VIM_ISDIGIT(stropt[1]))
18632 {
18633 ++stropt;
18634 block_len = getdigits(&stropt) - 1;
18635 --stropt;
18636 }
18637 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 }
18639 }
18640
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018641 if (argvars[1].v_type == VAR_LIST)
18642 {
18643 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018644 char_u **allocval;
18645 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018646 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018647 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018648 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018649 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018650 int len;
18651
18652 /* If the list is NULL handle like an empty list. */
18653 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018654
Bram Moolenaar7d647822014-04-05 21:28:56 +020018655 /* First half: use for pointers to result lines; second half: use for
18656 * pointers to allocated copies. */
18657 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018658 if (lstval == NULL)
18659 return;
18660 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018661 allocval = lstval + len + 2;
18662 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018663
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018664 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018665 li = li->li_next)
18666 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018667 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018668 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018669 goto free_lstval;
18670 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018671 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018672 /* Need to make a copy, next get_tv_string_buf_chk() will
18673 * overwrite the string. */
18674 strval = vim_strsave(buf);
18675 if (strval == NULL)
18676 goto free_lstval;
18677 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018678 }
18679 *curval++ = strval;
18680 }
18681 *curval++ = NULL;
18682
18683 write_reg_contents_lst(regname, lstval, -1,
18684 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018685free_lstval:
18686 while (curallocval > allocval)
18687 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018688 vim_free(lstval);
18689 }
18690 else
18691 {
18692 strval = get_tv_string_chk(&argvars[1]);
18693 if (strval == NULL)
18694 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018695 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699}
18700
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018701/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018702 * "settabvar()" function
18703 */
18704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018705f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018706{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018707#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018708 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018709 tabpage_T *tp;
18710#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018711 char_u *varname, *tabvarname;
18712 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018713
18714 rettv->vval.v_number = 0;
18715
18716 if (check_restricted() || check_secure())
18717 return;
18718
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018719#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018720 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018721#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018722 varname = get_tv_string_chk(&argvars[1]);
18723 varp = &argvars[2];
18724
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018725 if (varname != NULL && varp != NULL
18726#ifdef FEAT_WINDOWS
18727 && tp != NULL
18728#endif
18729 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018730 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018731#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018732 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018733 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018734#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018735
18736 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18737 if (tabvarname != NULL)
18738 {
18739 STRCPY(tabvarname, "t:");
18740 STRCPY(tabvarname + 2, varname);
18741 set_var(tabvarname, varp, TRUE);
18742 vim_free(tabvarname);
18743 }
18744
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018745#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018746 /* Restore current tabpage */
18747 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018748 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018749#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018750 }
18751}
18752
18753/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018754 * "settabwinvar()" function
18755 */
18756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018757f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018758{
18759 setwinvar(argvars, rettv, 1);
18760}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761
18762/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018763 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018766f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018768 setwinvar(argvars, rettv, 0);
18769}
18770
18771/*
18772 * "setwinvar()" and "settabwinvar()" functions
18773 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018774
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018776setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018777{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 win_T *win;
18779#ifdef FEAT_WINDOWS
18780 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018781 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018782 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783#endif
18784 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018785 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018787 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018788
18789 if (check_restricted() || check_secure())
18790 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018791
18792#ifdef FEAT_WINDOWS
18793 if (off == 1)
18794 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18795 else
18796 tp = curtab;
18797#endif
18798 win = find_win_by_nr(&argvars[off], tp);
18799 varname = get_tv_string_chk(&argvars[off + 1]);
18800 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801
18802 if (win != NULL && varname != NULL && varp != NULL)
18803 {
18804#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018805 need_switch_win = !(tp == curtab && win == curwin);
18806 if (!need_switch_win
18807 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018810 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018811 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018812 long numval;
18813 char_u *strval;
18814 int error = FALSE;
18815
18816 ++varname;
18817 numval = get_tv_number_chk(varp, &error);
18818 strval = get_tv_string_buf_chk(varp, nbuf);
18819 if (!error && strval != NULL)
18820 set_option_value(varname, numval, strval, OPT_LOCAL);
18821 }
18822 else
18823 {
18824 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18825 if (winvarname != NULL)
18826 {
18827 STRCPY(winvarname, "w:");
18828 STRCPY(winvarname + 2, varname);
18829 set_var(winvarname, varp, TRUE);
18830 vim_free(winvarname);
18831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 }
18833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018835 if (need_switch_win)
18836 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837#endif
18838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839}
18840
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018841#ifdef FEAT_CRYPT
18842/*
18843 * "sha256({string})" function
18844 */
18845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018846f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018847{
18848 char_u *p;
18849
18850 p = get_tv_string(&argvars[0]);
18851 rettv->vval.v_string = vim_strsave(
18852 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18853 rettv->v_type = VAR_STRING;
18854}
18855#endif /* FEAT_CRYPT */
18856
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018858 * "shellescape({string})" function
18859 */
18860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018861f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018862{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018863 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018864 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018865 rettv->v_type = VAR_STRING;
18866}
18867
18868/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018869 * shiftwidth() function
18870 */
18871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018872f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018873{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018874 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018875}
18876
18877/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018878 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 */
18880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018881f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018883 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884
Bram Moolenaar0d660222005-01-07 21:51:51 +000018885 p = get_tv_string(&argvars[0]);
18886 rettv->vval.v_string = vim_strsave(p);
18887 simplify_filename(rettv->vval.v_string); /* simplify in place */
18888 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889}
18890
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018891#ifdef FEAT_FLOAT
18892/*
18893 * "sin()" function
18894 */
18895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018896f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018897{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018898 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018899
18900 rettv->v_type = VAR_FLOAT;
18901 if (get_float_arg(argvars, &f) == OK)
18902 rettv->vval.v_float = sin(f);
18903 else
18904 rettv->vval.v_float = 0.0;
18905}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018906
18907/*
18908 * "sinh()" function
18909 */
18910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018911f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018912{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018913 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018914
18915 rettv->v_type = VAR_FLOAT;
18916 if (get_float_arg(argvars, &f) == OK)
18917 rettv->vval.v_float = sinh(f);
18918 else
18919 rettv->vval.v_float = 0.0;
18920}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018921#endif
18922
Bram Moolenaar0d660222005-01-07 21:51:51 +000018923static int
18924#ifdef __BORLANDC__
18925 _RTLENTRYF
18926#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018927 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018928static int
18929#ifdef __BORLANDC__
18930 _RTLENTRYF
18931#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018932 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018933
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018934/* struct used in the array that's given to qsort() */
18935typedef struct
18936{
18937 listitem_T *item;
18938 int idx;
18939} sortItem_T;
18940
Bram Moolenaar0b962472016-02-22 22:51:33 +010018941/* struct storing information about current sort */
18942typedef struct
18943{
18944 int item_compare_ic;
18945 int item_compare_numeric;
18946 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018947#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018948 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018949#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018950 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018951 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018952 dict_T *item_compare_selfdict;
18953 int item_compare_func_err;
18954 int item_compare_keep_zero;
18955} sortinfo_T;
18956static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018957static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018958#define ITEM_COMPARE_FAIL 999
18959
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018961 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018963 static int
18964#ifdef __BORLANDC__
18965_RTLENTRYF
18966#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018967item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018969 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018970 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018971 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018972 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018973 int res;
18974 char_u numbuf1[NUMBUFLEN];
18975 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018977 si1 = (sortItem_T *)s1;
18978 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018979 tv1 = &si1->item->li_tv;
18980 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018981
Bram Moolenaar0b962472016-02-22 22:51:33 +010018982 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018983 {
18984 long v1 = get_tv_number(tv1);
18985 long v2 = get_tv_number(tv2);
18986
18987 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18988 }
18989
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018990#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018991 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018992 {
18993 float_T v1 = get_tv_float(tv1);
18994 float_T v2 = get_tv_float(tv2);
18995
18996 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18997 }
18998#endif
18999
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019000 /* tv2string() puts quotes around a string and allocates memory. Don't do
19001 * that for string variables. Use a single quote when comparing with a
19002 * non-string to do what the docs promise. */
19003 if (tv1->v_type == VAR_STRING)
19004 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019005 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019006 p1 = (char_u *)"'";
19007 else
19008 p1 = tv1->vval.v_string;
19009 }
19010 else
19011 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19012 if (tv2->v_type == VAR_STRING)
19013 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019014 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019015 p2 = (char_u *)"'";
19016 else
19017 p2 = tv2->vval.v_string;
19018 }
19019 else
19020 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019021 if (p1 == NULL)
19022 p1 = (char_u *)"";
19023 if (p2 == NULL)
19024 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019025 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019026 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019027 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019028 res = STRICMP(p1, p2);
19029 else
19030 res = STRCMP(p1, p2);
19031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019033 {
19034 double n1, n2;
19035 n1 = strtod((char *)p1, (char **)&p1);
19036 n2 = strtod((char *)p2, (char **)&p2);
19037 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19038 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019039
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019040 /* When the result would be zero, compare the item indexes. Makes the
19041 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019042 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019043 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019044
Bram Moolenaar0d660222005-01-07 21:51:51 +000019045 vim_free(tofree1);
19046 vim_free(tofree2);
19047 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048}
19049
19050 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051#ifdef __BORLANDC__
19052_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019054item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019055{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019056 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019057 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019058 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019059 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019060 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019061 char_u *func_name;
19062 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019064 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019065 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019066 return 0;
19067
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019068 si1 = (sortItem_T *)s1;
19069 si2 = (sortItem_T *)s2;
19070
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019071 if (partial == NULL)
19072 func_name = sortinfo->item_compare_func;
19073 else
19074 func_name = partial->pt_name;
19075
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019076 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019077 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019078 copy_tv(&si1->item->li_tv, &argv[0]);
19079 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019080
19081 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019082 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019083 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019084 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019085 clear_tv(&argv[0]);
19086 clear_tv(&argv[1]);
19087
19088 if (res == FAIL)
19089 res = ITEM_COMPARE_FAIL;
19090 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019091 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19092 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019093 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019094 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019095
19096 /* When the result would be zero, compare the pointers themselves. Makes
19097 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019098 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019099 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019100
Bram Moolenaar0d660222005-01-07 21:51:51 +000019101 return res;
19102}
19103
19104/*
19105 * "sort({list})" function
19106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019108do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019109{
Bram Moolenaar33570922005-01-25 22:26:29 +000019110 list_T *l;
19111 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019112 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019113 sortinfo_T *old_sortinfo;
19114 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019115 long len;
19116 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117
Bram Moolenaar0b962472016-02-22 22:51:33 +010019118 /* Pointer to current info struct used in compare function. Save and
19119 * restore the current one for nested calls. */
19120 old_sortinfo = sortinfo;
19121 sortinfo = &info;
19122
Bram Moolenaar0d660222005-01-07 21:51:51 +000019123 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019124 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 else
19126 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019127 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019128 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019129 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19130 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019131 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019132 rettv->vval.v_list = l;
19133 rettv->v_type = VAR_LIST;
19134 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
Bram Moolenaar0d660222005-01-07 21:51:51 +000019136 len = list_len(l);
19137 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019138 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019139
Bram Moolenaar0b962472016-02-22 22:51:33 +010019140 info.item_compare_ic = FALSE;
19141 info.item_compare_numeric = FALSE;
19142 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019143#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019144 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019145#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019146 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019147 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019148 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019149 if (argvars[1].v_type != VAR_UNKNOWN)
19150 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019151 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019152 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019153 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019154 else if (argvars[1].v_type == VAR_PARTIAL)
19155 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019156 else
19157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019158 int error = FALSE;
19159
19160 i = get_tv_number_chk(&argvars[1], &error);
19161 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019162 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019163 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019164 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019165 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019166 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019167 else if (i != 0)
19168 {
19169 EMSG(_(e_invarg));
19170 goto theend;
19171 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019172 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019173 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019174 if (*info.item_compare_func == NUL)
19175 {
19176 /* empty string means default sort */
19177 info.item_compare_func = NULL;
19178 }
19179 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019180 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019181 info.item_compare_func = NULL;
19182 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019183 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019184 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019185 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019186 info.item_compare_func = NULL;
19187 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019188 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019189#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019190 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019191 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019192 info.item_compare_func = NULL;
19193 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019194 }
19195#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019196 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019197 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019198 info.item_compare_func = NULL;
19199 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019200 }
19201 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019202 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019203
19204 if (argvars[2].v_type != VAR_UNKNOWN)
19205 {
19206 /* optional third argument: {dict} */
19207 if (argvars[2].v_type != VAR_DICT)
19208 {
19209 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019210 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019211 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019212 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019213 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215
Bram Moolenaar0d660222005-01-07 21:51:51 +000019216 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019217 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019218 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019219 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220
Bram Moolenaar327aa022014-03-25 18:24:23 +010019221 i = 0;
19222 if (sort)
19223 {
19224 /* sort(): ptrs will be the list to sort */
19225 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019226 {
19227 ptrs[i].item = li;
19228 ptrs[i].idx = i;
19229 ++i;
19230 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019231
Bram Moolenaar0b962472016-02-22 22:51:33 +010019232 info.item_compare_func_err = FALSE;
19233 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019234 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019235 if ((info.item_compare_func != NULL
19236 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019237 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019238 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019239 EMSG(_("E702: Sort compare function failed"));
19240 else
19241 {
19242 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019243 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019244 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019245 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019246 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019247
Bram Moolenaar0b962472016-02-22 22:51:33 +010019248 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019249 {
19250 /* Clear the List and append the items in sorted order. */
19251 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19252 l->lv_len = 0;
19253 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019254 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019255 }
19256 }
19257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019258 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019259 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019260 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019261
19262 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019263 info.item_compare_func_err = FALSE;
19264 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019265 item_compare_func_ptr = info.item_compare_func != NULL
19266 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019267 ? item_compare2 : item_compare;
19268
19269 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19270 li = li->li_next)
19271 {
19272 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19273 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019274 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019275 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019276 {
19277 EMSG(_("E882: Uniq compare function failed"));
19278 break;
19279 }
19280 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019281
Bram Moolenaar0b962472016-02-22 22:51:33 +010019282 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019283 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019284 while (--i >= 0)
19285 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019286 li = ptrs[i].item->li_next;
19287 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019288 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019289 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019290 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019291 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019292 list_fix_watch(l, li);
19293 listitem_free(li);
19294 l->lv_len--;
19295 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019296 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019297 }
19298
19299 vim_free(ptrs);
19300 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019301theend:
19302 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019303}
19304
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019305/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019306 * "sort({list})" function
19307 */
19308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019309f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019310{
19311 do_sort_uniq(argvars, rettv, TRUE);
19312}
19313
19314/*
19315 * "uniq({list})" function
19316 */
19317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019318f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019319{
19320 do_sort_uniq(argvars, rettv, FALSE);
19321}
19322
19323/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019324 * "soundfold({word})" function
19325 */
19326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019327f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019328{
19329 char_u *s;
19330
19331 rettv->v_type = VAR_STRING;
19332 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019333#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019334 rettv->vval.v_string = eval_soundfold(s);
19335#else
19336 rettv->vval.v_string = vim_strsave(s);
19337#endif
19338}
19339
19340/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019341 * "spellbadword()" function
19342 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019344f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019345{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019346 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019347 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019348 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019349
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019350 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019351 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019352
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019353#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019354 if (argvars[0].v_type == VAR_UNKNOWN)
19355 {
19356 /* Find the start and length of the badly spelled word. */
19357 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19358 if (len != 0)
19359 word = ml_get_cursor();
19360 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019361 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019362 {
19363 char_u *str = get_tv_string_chk(&argvars[0]);
19364 int capcol = -1;
19365
19366 if (str != NULL)
19367 {
19368 /* Check the argument for spelling. */
19369 while (*str != NUL)
19370 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019371 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019372 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019373 {
19374 word = str;
19375 break;
19376 }
19377 str += len;
19378 }
19379 }
19380 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019381#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019382
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019383 list_append_string(rettv->vval.v_list, word, len);
19384 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019385 attr == HLF_SPB ? "bad" :
19386 attr == HLF_SPR ? "rare" :
19387 attr == HLF_SPL ? "local" :
19388 attr == HLF_SPC ? "caps" :
19389 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019390}
19391
19392/*
19393 * "spellsuggest()" function
19394 */
19395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019396f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019397{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019398#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019399 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019400 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019401 int maxcount;
19402 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019403 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019404 listitem_T *li;
19405 int need_capital = FALSE;
19406#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019407
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019408 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019409 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019410
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019411#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019412 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019413 {
19414 str = get_tv_string(&argvars[0]);
19415 if (argvars[1].v_type != VAR_UNKNOWN)
19416 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019417 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019418 if (maxcount <= 0)
19419 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019420 if (argvars[2].v_type != VAR_UNKNOWN)
19421 {
19422 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19423 if (typeerr)
19424 return;
19425 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019426 }
19427 else
19428 maxcount = 25;
19429
Bram Moolenaar4770d092006-01-12 23:22:24 +000019430 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019431
19432 for (i = 0; i < ga.ga_len; ++i)
19433 {
19434 str = ((char_u **)ga.ga_data)[i];
19435
19436 li = listitem_alloc();
19437 if (li == NULL)
19438 vim_free(str);
19439 else
19440 {
19441 li->li_tv.v_type = VAR_STRING;
19442 li->li_tv.v_lock = 0;
19443 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019444 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019445 }
19446 }
19447 ga_clear(&ga);
19448 }
19449#endif
19450}
19451
Bram Moolenaar0d660222005-01-07 21:51:51 +000019452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019453f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019454{
19455 char_u *str;
19456 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019457 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019458 regmatch_T regmatch;
19459 char_u patbuf[NUMBUFLEN];
19460 char_u *save_cpo;
19461 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019462 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019463 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019464 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019465
19466 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19467 save_cpo = p_cpo;
19468 p_cpo = (char_u *)"";
19469
19470 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019471 if (argvars[1].v_type != VAR_UNKNOWN)
19472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019473 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19474 if (pat == NULL)
19475 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019476 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019477 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019478 }
19479 if (pat == NULL || *pat == NUL)
19480 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019481
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019482 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019484 if (typeerr)
19485 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486
Bram Moolenaar0d660222005-01-07 21:51:51 +000019487 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19488 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019490 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019491 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019492 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019493 if (*str == NUL)
19494 match = FALSE; /* empty item at the end */
19495 else
19496 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019497 if (match)
19498 end = regmatch.startp[0];
19499 else
19500 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019501 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19502 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019503 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019504 if (list_append_string(rettv->vval.v_list, str,
19505 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019506 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019507 }
19508 if (!match)
19509 break;
19510 /* Advance to just after the match. */
19511 if (regmatch.endp[0] > str)
19512 col = 0;
19513 else
19514 {
19515 /* Don't get stuck at the same match. */
19516#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019517 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019518#else
19519 col = 1;
19520#endif
19521 }
19522 str = regmatch.endp[0];
19523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524
Bram Moolenaar473de612013-06-08 18:19:48 +020019525 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527
Bram Moolenaar0d660222005-01-07 21:51:51 +000019528 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529}
19530
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019531#ifdef FEAT_FLOAT
19532/*
19533 * "sqrt()" function
19534 */
19535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019536f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019537{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019538 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019539
19540 rettv->v_type = VAR_FLOAT;
19541 if (get_float_arg(argvars, &f) == OK)
19542 rettv->vval.v_float = sqrt(f);
19543 else
19544 rettv->vval.v_float = 0.0;
19545}
19546
19547/*
19548 * "str2float()" function
19549 */
19550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019551f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019552{
19553 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19554
19555 if (*p == '+')
19556 p = skipwhite(p + 1);
19557 (void)string2float(p, &rettv->vval.v_float);
19558 rettv->v_type = VAR_FLOAT;
19559}
19560#endif
19561
Bram Moolenaar2c932302006-03-18 21:42:09 +000019562/*
19563 * "str2nr()" function
19564 */
19565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019566f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019567{
19568 int base = 10;
19569 char_u *p;
19570 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019571 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019572
19573 if (argvars[1].v_type != VAR_UNKNOWN)
19574 {
19575 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019576 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019577 {
19578 EMSG(_(e_invarg));
19579 return;
19580 }
19581 }
19582
19583 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019584 if (*p == '+')
19585 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019586 switch (base)
19587 {
19588 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19589 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19590 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19591 default: what = 0;
19592 }
19593 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019594 rettv->vval.v_number = n;
19595}
19596
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597#ifdef HAVE_STRFTIME
19598/*
19599 * "strftime({format}[, {time}])" function
19600 */
19601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019602f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603{
19604 char_u result_buf[256];
19605 struct tm *curtime;
19606 time_t seconds;
19607 char_u *p;
19608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019611 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019612 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613 seconds = time(NULL);
19614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019615 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 curtime = localtime(&seconds);
19617 /* MSVC returns NULL for an invalid value of seconds. */
19618 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019619 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620 else
19621 {
19622# ifdef FEAT_MBYTE
19623 vimconv_T conv;
19624 char_u *enc;
19625
19626 conv.vc_type = CONV_NONE;
19627 enc = enc_locale();
19628 convert_setup(&conv, p_enc, enc);
19629 if (conv.vc_type != CONV_NONE)
19630 p = string_convert(&conv, p, NULL);
19631# endif
19632 if (p != NULL)
19633 (void)strftime((char *)result_buf, sizeof(result_buf),
19634 (char *)p, curtime);
19635 else
19636 result_buf[0] = NUL;
19637
19638# ifdef FEAT_MBYTE
19639 if (conv.vc_type != CONV_NONE)
19640 vim_free(p);
19641 convert_setup(&conv, enc, p_enc);
19642 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019643 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 else
19645# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647
19648# ifdef FEAT_MBYTE
19649 /* Release conversion descriptors */
19650 convert_setup(&conv, NULL, NULL);
19651 vim_free(enc);
19652# endif
19653 }
19654}
19655#endif
19656
19657/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019658 * "strgetchar()" function
19659 */
19660 static void
19661f_strgetchar(typval_T *argvars, typval_T *rettv)
19662{
19663 char_u *str;
19664 int len;
19665 int error = FALSE;
19666 int charidx;
19667
19668 rettv->vval.v_number = -1;
19669 str = get_tv_string_chk(&argvars[0]);
19670 if (str == NULL)
19671 return;
19672 len = (int)STRLEN(str);
19673 charidx = get_tv_number_chk(&argvars[1], &error);
19674 if (error)
19675 return;
19676#ifdef FEAT_MBYTE
19677 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019678 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019679
19680 while (charidx >= 0 && byteidx < len)
19681 {
19682 if (charidx == 0)
19683 {
19684 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19685 break;
19686 }
19687 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019688 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019689 }
19690 }
19691#else
19692 if (charidx < len)
19693 rettv->vval.v_number = str[charidx];
19694#endif
19695}
19696
19697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 * "stridx()" function
19699 */
19700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019701f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702{
19703 char_u buf[NUMBUFLEN];
19704 char_u *needle;
19705 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019706 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019710 needle = get_tv_string_chk(&argvars[1]);
19711 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019712 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019713 if (needle == NULL || haystack == NULL)
19714 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715
Bram Moolenaar33570922005-01-25 22:26:29 +000019716 if (argvars[2].v_type != VAR_UNKNOWN)
19717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019718 int error = FALSE;
19719
19720 start_idx = get_tv_number_chk(&argvars[2], &error);
19721 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019722 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019723 if (start_idx >= 0)
19724 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019725 }
19726
19727 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19728 if (pos != NULL)
19729 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730}
19731
19732/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019733 * "string()" function
19734 */
19735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019736f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019737{
19738 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019739 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019741 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019742 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19743 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019744 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019745 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747}
19748
19749/*
19750 * "strlen()" function
19751 */
19752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019753f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019755 rettv->vval.v_number = (varnumber_T)(STRLEN(
19756 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757}
19758
19759/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019760 * "strchars()" function
19761 */
19762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019763f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019764{
19765 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019766 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019767#ifdef FEAT_MBYTE
19768 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019769 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019770#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019771
19772 if (argvars[1].v_type != VAR_UNKNOWN)
19773 skipcc = get_tv_number_chk(&argvars[1], NULL);
19774 if (skipcc < 0 || skipcc > 1)
19775 EMSG(_(e_invarg));
19776 else
19777 {
19778#ifdef FEAT_MBYTE
19779 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19780 while (*s != NUL)
19781 {
19782 func_mb_ptr2char_adv(&s);
19783 ++len;
19784 }
19785 rettv->vval.v_number = len;
19786#else
19787 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19788#endif
19789 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019790}
19791
19792/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019793 * "strdisplaywidth()" function
19794 */
19795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019796f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019797{
19798 char_u *s = get_tv_string(&argvars[0]);
19799 int col = 0;
19800
19801 if (argvars[1].v_type != VAR_UNKNOWN)
19802 col = get_tv_number(&argvars[1]);
19803
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019804 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019805}
19806
19807/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019808 * "strwidth()" function
19809 */
19810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019811f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019812{
19813 char_u *s = get_tv_string(&argvars[0]);
19814
19815 rettv->vval.v_number = (varnumber_T)(
19816#ifdef FEAT_MBYTE
19817 mb_string2cells(s, -1)
19818#else
19819 STRLEN(s)
19820#endif
19821 );
19822}
19823
19824/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019825 * "strcharpart()" function
19826 */
19827 static void
19828f_strcharpart(typval_T *argvars, typval_T *rettv)
19829{
19830#ifdef FEAT_MBYTE
19831 char_u *p;
19832 int nchar;
19833 int nbyte = 0;
19834 int charlen;
19835 int len = 0;
19836 int slen;
19837 int error = FALSE;
19838
19839 p = get_tv_string(&argvars[0]);
19840 slen = (int)STRLEN(p);
19841
19842 nchar = get_tv_number_chk(&argvars[1], &error);
19843 if (!error)
19844 {
19845 if (nchar > 0)
19846 while (nchar > 0 && nbyte < slen)
19847 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019848 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019849 --nchar;
19850 }
19851 else
19852 nbyte = nchar;
19853 if (argvars[2].v_type != VAR_UNKNOWN)
19854 {
19855 charlen = get_tv_number(&argvars[2]);
19856 while (charlen > 0 && nbyte + len < slen)
19857 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019858 int off = nbyte + len;
19859
19860 if (off < 0)
19861 len += 1;
19862 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019863 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019864 --charlen;
19865 }
19866 }
19867 else
19868 len = slen - nbyte; /* default: all bytes that are available. */
19869 }
19870
19871 /*
19872 * Only return the overlap between the specified part and the actual
19873 * string.
19874 */
19875 if (nbyte < 0)
19876 {
19877 len += nbyte;
19878 nbyte = 0;
19879 }
19880 else if (nbyte > slen)
19881 nbyte = slen;
19882 if (len < 0)
19883 len = 0;
19884 else if (nbyte + len > slen)
19885 len = slen - nbyte;
19886
19887 rettv->v_type = VAR_STRING;
19888 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19889#else
19890 f_strpart(argvars, rettv);
19891#endif
19892}
19893
19894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 * "strpart()" function
19896 */
19897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019898f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899{
19900 char_u *p;
19901 int n;
19902 int len;
19903 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019904 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019906 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 slen = (int)STRLEN(p);
19908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019909 n = get_tv_number_chk(&argvars[1], &error);
19910 if (error)
19911 len = 0;
19912 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019913 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914 else
19915 len = slen - n; /* default len: all bytes that are available. */
19916
19917 /*
19918 * Only return the overlap between the specified part and the actual
19919 * string.
19920 */
19921 if (n < 0)
19922 {
19923 len += n;
19924 n = 0;
19925 }
19926 else if (n > slen)
19927 n = slen;
19928 if (len < 0)
19929 len = 0;
19930 else if (n + len > slen)
19931 len = slen - n;
19932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019933 rettv->v_type = VAR_STRING;
19934 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935}
19936
19937/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019938 * "strridx()" function
19939 */
19940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019941f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019942{
19943 char_u buf[NUMBUFLEN];
19944 char_u *needle;
19945 char_u *haystack;
19946 char_u *rest;
19947 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019948 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019950 needle = get_tv_string_chk(&argvars[1]);
19951 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019952
19953 rettv->vval.v_number = -1;
19954 if (needle == NULL || haystack == NULL)
19955 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019956
19957 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019958 if (argvars[2].v_type != VAR_UNKNOWN)
19959 {
19960 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019961 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019962 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019963 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019964 }
19965 else
19966 end_idx = haystack_len;
19967
Bram Moolenaar0d660222005-01-07 21:51:51 +000019968 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019969 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019970 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019971 lastmatch = haystack + end_idx;
19972 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019973 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019974 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019975 for (rest = haystack; *rest != '\0'; ++rest)
19976 {
19977 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019978 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019979 break;
19980 lastmatch = rest;
19981 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019982 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019983
19984 if (lastmatch == NULL)
19985 rettv->vval.v_number = -1;
19986 else
19987 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19988}
19989
19990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991 * "strtrans()" function
19992 */
19993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019994f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996 rettv->v_type = VAR_STRING;
19997 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998}
19999
20000/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020001 * "submatch()" function
20002 */
20003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020004f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020005{
Bram Moolenaar41571762014-04-02 19:00:58 +020020006 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020007 int no;
20008 int retList = 0;
20009
20010 no = (int)get_tv_number_chk(&argvars[0], &error);
20011 if (error)
20012 return;
20013 error = FALSE;
20014 if (argvars[1].v_type != VAR_UNKNOWN)
20015 retList = get_tv_number_chk(&argvars[1], &error);
20016 if (error)
20017 return;
20018
20019 if (retList == 0)
20020 {
20021 rettv->v_type = VAR_STRING;
20022 rettv->vval.v_string = reg_submatch(no);
20023 }
20024 else
20025 {
20026 rettv->v_type = VAR_LIST;
20027 rettv->vval.v_list = reg_submatch_list(no);
20028 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020029}
20030
20031/*
20032 * "substitute()" function
20033 */
20034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020035f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020036{
20037 char_u patbuf[NUMBUFLEN];
20038 char_u subbuf[NUMBUFLEN];
20039 char_u flagsbuf[NUMBUFLEN];
20040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020041 char_u *str = get_tv_string_chk(&argvars[0]);
20042 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20043 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20044 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20045
Bram Moolenaar0d660222005-01-07 21:51:51 +000020046 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020047 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20048 rettv->vval.v_string = NULL;
20049 else
20050 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020051}
20052
20053/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020054 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020057f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058{
20059 int id = 0;
20060#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020061 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 long col;
20063 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020064 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020066 lnum = get_tv_lnum(argvars); /* -1 on type error */
20067 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20068 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020070 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020071 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020072 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073#endif
20074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076}
20077
20078/*
20079 * "synIDattr(id, what [, mode])" function
20080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020082f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083{
20084 char_u *p = NULL;
20085#ifdef FEAT_SYN_HL
20086 int id;
20087 char_u *what;
20088 char_u *mode;
20089 char_u modebuf[NUMBUFLEN];
20090 int modec;
20091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092 id = get_tv_number(&argvars[0]);
20093 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020098 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 modec = 0; /* replace invalid with current */
20100 }
20101 else
20102 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020103#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020104 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 modec = 'g';
20106 else
20107#endif
20108 if (t_colors > 1)
20109 modec = 'c';
20110 else
20111 modec = 't';
20112 }
20113
20114
20115 switch (TOLOWER_ASC(what[0]))
20116 {
20117 case 'b':
20118 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20119 p = highlight_color(id, what, modec);
20120 else /* bold */
20121 p = highlight_has_attr(id, HL_BOLD, modec);
20122 break;
20123
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020124 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020125 p = highlight_color(id, what, modec);
20126 break;
20127
20128 case 'i':
20129 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20130 p = highlight_has_attr(id, HL_INVERSE, modec);
20131 else /* italic */
20132 p = highlight_has_attr(id, HL_ITALIC, modec);
20133 break;
20134
20135 case 'n': /* name */
20136 p = get_highlight_name(NULL, id - 1);
20137 break;
20138
20139 case 'r': /* reverse */
20140 p = highlight_has_attr(id, HL_INVERSE, modec);
20141 break;
20142
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020143 case 's':
20144 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20145 p = highlight_color(id, what, modec);
20146 else /* standout */
20147 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 break;
20149
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020150 case 'u':
20151 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20152 /* underline */
20153 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20154 else
20155 /* undercurl */
20156 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157 break;
20158 }
20159
20160 if (p != NULL)
20161 p = vim_strsave(p);
20162#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163 rettv->v_type = VAR_STRING;
20164 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165}
20166
20167/*
20168 * "synIDtrans(id)" function
20169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020171f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172{
20173 int id;
20174
20175#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020176 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177
20178 if (id > 0)
20179 id = syn_get_final_id(id);
20180 else
20181#endif
20182 id = 0;
20183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020184 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185}
20186
20187/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020188 * "synconcealed(lnum, col)" function
20189 */
20190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020191f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020192{
20193#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20194 long lnum;
20195 long col;
20196 int syntax_flags = 0;
20197 int cchar;
20198 int matchid = 0;
20199 char_u str[NUMBUFLEN];
20200#endif
20201
20202 rettv->v_type = VAR_LIST;
20203 rettv->vval.v_list = NULL;
20204
20205#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20206 lnum = get_tv_lnum(argvars); /* -1 on type error */
20207 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20208
20209 vim_memset(str, NUL, sizeof(str));
20210
20211 if (rettv_list_alloc(rettv) != FAIL)
20212 {
20213 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20214 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20215 && curwin->w_p_cole > 0)
20216 {
20217 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20218 syntax_flags = get_syntax_info(&matchid);
20219
20220 /* get the conceal character */
20221 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20222 {
20223 cchar = syn_get_sub_char();
20224 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20225 cchar = lcs_conceal;
20226 if (cchar != NUL)
20227 {
20228# ifdef FEAT_MBYTE
20229 if (has_mbyte)
20230 (*mb_char2bytes)(cchar, str);
20231 else
20232# endif
20233 str[0] = cchar;
20234 }
20235 }
20236 }
20237
20238 list_append_number(rettv->vval.v_list,
20239 (syntax_flags & HL_CONCEAL) != 0);
20240 /* -1 to auto-determine strlen */
20241 list_append_string(rettv->vval.v_list, str, -1);
20242 list_append_number(rettv->vval.v_list, matchid);
20243 }
20244#endif
20245}
20246
20247/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020248 * "synstack(lnum, col)" function
20249 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020251f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020252{
20253#ifdef FEAT_SYN_HL
20254 long lnum;
20255 long col;
20256 int i;
20257 int id;
20258#endif
20259
20260 rettv->v_type = VAR_LIST;
20261 rettv->vval.v_list = NULL;
20262
20263#ifdef FEAT_SYN_HL
20264 lnum = get_tv_lnum(argvars); /* -1 on type error */
20265 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20266
20267 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020268 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020269 && rettv_list_alloc(rettv) != FAIL)
20270 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020271 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020272 for (i = 0; ; ++i)
20273 {
20274 id = syn_get_stack_item(i);
20275 if (id < 0)
20276 break;
20277 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20278 break;
20279 }
20280 }
20281#endif
20282}
20283
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020285get_cmd_output_as_rettv(
20286 typval_T *argvars,
20287 typval_T *rettv,
20288 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020290 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020292 char_u *infile = NULL;
20293 char_u buf[NUMBUFLEN];
20294 int err = FALSE;
20295 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020296 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020297 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020299 rettv->v_type = VAR_STRING;
20300 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020301 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020302 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020303
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020304 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020305 {
20306 /*
20307 * Write the string to a temp file, to be used for input of the shell
20308 * command.
20309 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020310 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020311 {
20312 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020313 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020314 }
20315
20316 fd = mch_fopen((char *)infile, WRITEBIN);
20317 if (fd == NULL)
20318 {
20319 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020320 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020321 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020322 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020323 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020324 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20325 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020326 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020327 else
20328 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020329 size_t len;
20330
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020331 p = get_tv_string_buf_chk(&argvars[1], buf);
20332 if (p == NULL)
20333 {
20334 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020335 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020336 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020337 len = STRLEN(p);
20338 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020339 err = TRUE;
20340 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020341 if (fclose(fd) != 0)
20342 err = TRUE;
20343 if (err)
20344 {
20345 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020346 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020347 }
20348 }
20349
Bram Moolenaar52a72462014-08-29 15:53:52 +020020350 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20351 * echoes typeahead, that messes up the display. */
20352 if (!msg_silent)
20353 flags += SHELL_COOKED;
20354
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020355 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020357 int len;
20358 listitem_T *li;
20359 char_u *s = NULL;
20360 char_u *start;
20361 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020362 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363
Bram Moolenaar52a72462014-08-29 15:53:52 +020020364 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020365 if (res == NULL)
20366 goto errret;
20367
20368 list = list_alloc();
20369 if (list == NULL)
20370 goto errret;
20371
20372 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020374 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020375 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020376 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020377 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020378
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020379 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020380 if (s == NULL)
20381 goto errret;
20382
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020383 for (p = s; start < end; ++p, ++start)
20384 *p = *start == NUL ? NL : *start;
20385 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020386
20387 li = listitem_alloc();
20388 if (li == NULL)
20389 {
20390 vim_free(s);
20391 goto errret;
20392 }
20393 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020394 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020395 li->li_tv.vval.v_string = s;
20396 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020398
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020399 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020400 rettv->v_type = VAR_LIST;
20401 rettv->vval.v_list = list;
20402 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020404 else
20405 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020406 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020407#ifdef USE_CR
20408 /* translate <CR> into <NL> */
20409 if (res != NULL)
20410 {
20411 char_u *s;
20412
20413 for (s = res; *s; ++s)
20414 {
20415 if (*s == CAR)
20416 *s = NL;
20417 }
20418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419#else
20420# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020421 /* translate <CR><NL> into <NL> */
20422 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020424 char_u *s, *d;
20425
20426 d = res;
20427 for (s = res; *s; ++s)
20428 {
20429 if (s[0] == CAR && s[1] == NL)
20430 ++s;
20431 *d++ = *s;
20432 }
20433 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435# endif
20436#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020437 rettv->vval.v_string = res;
20438 res = NULL;
20439 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020440
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020441errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020442 if (infile != NULL)
20443 {
20444 mch_remove(infile);
20445 vim_free(infile);
20446 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020447 if (res != NULL)
20448 vim_free(res);
20449 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020450 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020451}
20452
20453/*
20454 * "system()" function
20455 */
20456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020457f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020458{
20459 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20460}
20461
20462/*
20463 * "systemlist()" function
20464 */
20465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020466f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020467{
20468 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469}
20470
20471/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020472 * "tabpagebuflist()" function
20473 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020475f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020476{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020477#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020478 tabpage_T *tp;
20479 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020480
20481 if (argvars[0].v_type == VAR_UNKNOWN)
20482 wp = firstwin;
20483 else
20484 {
20485 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20486 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020487 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020488 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020489 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020490 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020491 for (; wp != NULL; wp = wp->w_next)
20492 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020493 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020494 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020495 }
20496#endif
20497}
20498
20499
20500/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020501 * "tabpagenr()" function
20502 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020504f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020505{
20506 int nr = 1;
20507#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020508 char_u *arg;
20509
20510 if (argvars[0].v_type != VAR_UNKNOWN)
20511 {
20512 arg = get_tv_string_chk(&argvars[0]);
20513 nr = 0;
20514 if (arg != NULL)
20515 {
20516 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020517 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020518 else
20519 EMSG2(_(e_invexpr2), arg);
20520 }
20521 }
20522 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020523 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020524#endif
20525 rettv->vval.v_number = nr;
20526}
20527
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020528
20529#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020530static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020531
20532/*
20533 * Common code for tabpagewinnr() and winnr().
20534 */
20535 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020536get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020537{
20538 win_T *twin;
20539 int nr = 1;
20540 win_T *wp;
20541 char_u *arg;
20542
20543 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20544 if (argvar->v_type != VAR_UNKNOWN)
20545 {
20546 arg = get_tv_string_chk(argvar);
20547 if (arg == NULL)
20548 nr = 0; /* type error; errmsg already given */
20549 else if (STRCMP(arg, "$") == 0)
20550 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20551 else if (STRCMP(arg, "#") == 0)
20552 {
20553 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20554 if (twin == NULL)
20555 nr = 0;
20556 }
20557 else
20558 {
20559 EMSG2(_(e_invexpr2), arg);
20560 nr = 0;
20561 }
20562 }
20563
20564 if (nr > 0)
20565 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20566 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020567 {
20568 if (wp == NULL)
20569 {
20570 /* didn't find it in this tabpage */
20571 nr = 0;
20572 break;
20573 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020574 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020575 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020576 return nr;
20577}
20578#endif
20579
20580/*
20581 * "tabpagewinnr()" function
20582 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020584f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020585{
20586 int nr = 1;
20587#ifdef FEAT_WINDOWS
20588 tabpage_T *tp;
20589
20590 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20591 if (tp == NULL)
20592 nr = 0;
20593 else
20594 nr = get_winnr(tp, &argvars[1]);
20595#endif
20596 rettv->vval.v_number = nr;
20597}
20598
20599
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020600/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020601 * "tagfiles()" function
20602 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020604f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020605{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020606 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020607 tagname_T tn;
20608 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020609
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020610 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020611 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020612 fname = alloc(MAXPATHL);
20613 if (fname == NULL)
20614 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020615
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020616 for (first = TRUE; ; first = FALSE)
20617 if (get_tagfname(&tn, first, fname) == FAIL
20618 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020619 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020620 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020621 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020622}
20623
20624/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020625 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020626 */
20627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020628f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020629{
20630 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020631
20632 tag_pattern = get_tv_string(&argvars[0]);
20633
20634 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020635 if (*tag_pattern == NUL)
20636 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020637
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020638 if (rettv_list_alloc(rettv) == OK)
20639 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020640}
20641
20642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 * "tempname()" function
20644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020646f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647{
20648 static int x = 'A';
20649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020650 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020651 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652
20653 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20654 * names. Skip 'I' and 'O', they are used for shell redirection. */
20655 do
20656 {
20657 if (x == 'Z')
20658 x = '0';
20659 else if (x == '9')
20660 x = 'A';
20661 else
20662 {
20663#ifdef EBCDIC
20664 if (x == 'I')
20665 x = 'J';
20666 else if (x == 'R')
20667 x = 'S';
20668 else
20669#endif
20670 ++x;
20671 }
20672 } while (x == 'I' || x == 'O');
20673}
20674
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020675#ifdef FEAT_FLOAT
20676/*
20677 * "tan()" function
20678 */
20679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020680f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020681{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020682 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020683
20684 rettv->v_type = VAR_FLOAT;
20685 if (get_float_arg(argvars, &f) == OK)
20686 rettv->vval.v_float = tan(f);
20687 else
20688 rettv->vval.v_float = 0.0;
20689}
20690
20691/*
20692 * "tanh()" function
20693 */
20694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020695f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020696{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020697 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020698
20699 rettv->v_type = VAR_FLOAT;
20700 if (get_float_arg(argvars, &f) == OK)
20701 rettv->vval.v_float = tanh(f);
20702 else
20703 rettv->vval.v_float = 0.0;
20704}
20705#endif
20706
Bram Moolenaar574860b2016-05-24 17:33:34 +020020707/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020708 * "test_alloc_fail(id, countdown, repeat)" function
20709 */
20710 static void
20711f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20712{
20713 if (argvars[0].v_type != VAR_NUMBER
20714 || argvars[0].vval.v_number <= 0
20715 || argvars[1].v_type != VAR_NUMBER
20716 || argvars[1].vval.v_number < 0
20717 || argvars[2].v_type != VAR_NUMBER)
20718 EMSG(_(e_invarg));
20719 else
20720 {
20721 alloc_fail_id = argvars[0].vval.v_number;
20722 if (alloc_fail_id >= aid_last)
20723 EMSG(_(e_invarg));
20724 alloc_fail_countdown = argvars[1].vval.v_number;
20725 alloc_fail_repeat = argvars[2].vval.v_number;
20726 did_outofmem_msg = FALSE;
20727 }
20728}
20729
20730/*
20731 * "test_disable_char_avail({expr})" function
20732 */
20733 static void
20734f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20735{
20736 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20737}
20738
20739/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020740 * "test_garbagecollect_now()" function
20741 */
20742 static void
20743f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20744{
20745 /* This is dangerous, any Lists and Dicts used internally may be freed
20746 * while still in use. */
20747 garbage_collect(TRUE);
20748}
20749
20750#ifdef FEAT_JOB_CHANNEL
20751 static void
20752f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20753{
20754 rettv->v_type = VAR_CHANNEL;
20755 rettv->vval.v_channel = NULL;
20756}
20757#endif
20758
20759 static void
20760f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20761{
20762 rettv->v_type = VAR_DICT;
20763 rettv->vval.v_dict = NULL;
20764}
20765
20766#ifdef FEAT_JOB_CHANNEL
20767 static void
20768f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20769{
20770 rettv->v_type = VAR_JOB;
20771 rettv->vval.v_job = NULL;
20772}
20773#endif
20774
20775 static void
20776f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20777{
20778 rettv->v_type = VAR_LIST;
20779 rettv->vval.v_list = NULL;
20780}
20781
20782 static void
20783f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20784{
20785 rettv->v_type = VAR_PARTIAL;
20786 rettv->vval.v_partial = NULL;
20787}
20788
20789 static void
20790f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20791{
20792 rettv->v_type = VAR_STRING;
20793 rettv->vval.v_string = NULL;
20794}
20795
Bram Moolenaar975b5272016-03-15 23:10:59 +010020796#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20797/*
20798 * Get a callback from "arg". It can be a Funcref or a function name.
20799 * When "arg" is zero return an empty string.
20800 * Return NULL for an invalid argument.
20801 */
20802 char_u *
20803get_callback(typval_T *arg, partial_T **pp)
20804{
20805 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20806 {
20807 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020808 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020809 return (*pp)->pt_name;
20810 }
20811 *pp = NULL;
20812 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20813 return arg->vval.v_string;
20814 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20815 return (char_u *)"";
20816 EMSG(_("E921: Invalid callback argument"));
20817 return NULL;
20818}
20819#endif
20820
20821#ifdef FEAT_TIMERS
20822/*
20823 * "timer_start(time, callback [, options])" function
20824 */
20825 static void
20826f_timer_start(typval_T *argvars, typval_T *rettv)
20827{
20828 long msec = get_tv_number(&argvars[0]);
20829 timer_T *timer;
20830 int repeat = 0;
20831 char_u *callback;
20832 dict_T *dict;
20833
Bram Moolenaar38499922016-04-22 20:46:52 +020020834 if (check_secure())
20835 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020836 if (argvars[2].v_type != VAR_UNKNOWN)
20837 {
20838 if (argvars[2].v_type != VAR_DICT
20839 || (dict = argvars[2].vval.v_dict) == NULL)
20840 {
20841 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20842 return;
20843 }
20844 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20845 repeat = get_dict_number(dict, (char_u *)"repeat");
20846 }
20847
20848 timer = create_timer(msec, repeat);
20849 callback = get_callback(&argvars[1], &timer->tr_partial);
20850 if (callback == NULL)
20851 {
20852 stop_timer(timer);
20853 rettv->vval.v_number = -1;
20854 }
20855 else
20856 {
20857 timer->tr_callback = vim_strsave(callback);
20858 rettv->vval.v_number = timer->tr_id;
20859 }
20860}
20861
20862/*
20863 * "timer_stop(timer)" function
20864 */
20865 static void
20866f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20867{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020868 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020869
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020870 if (argvars[0].v_type != VAR_NUMBER)
20871 {
20872 EMSG(_(e_number_exp));
20873 return;
20874 }
20875 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020876 if (timer != NULL)
20877 stop_timer(timer);
20878}
20879#endif
20880
Bram Moolenaard52d9742005-08-21 22:20:28 +000020881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 * "tolower(string)" function
20883 */
20884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020885f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886{
20887 char_u *p;
20888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020889 p = vim_strsave(get_tv_string(&argvars[0]));
20890 rettv->v_type = VAR_STRING;
20891 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892
20893 if (p != NULL)
20894 while (*p != NUL)
20895 {
20896#ifdef FEAT_MBYTE
20897 int l;
20898
20899 if (enc_utf8)
20900 {
20901 int c, lc;
20902
20903 c = utf_ptr2char(p);
20904 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020905 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 /* TODO: reallocate string when byte count changes. */
20907 if (utf_char2len(lc) == l)
20908 utf_char2bytes(lc, p);
20909 p += l;
20910 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020911 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 p += l; /* skip multi-byte character */
20913 else
20914#endif
20915 {
20916 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20917 ++p;
20918 }
20919 }
20920}
20921
20922/*
20923 * "toupper(string)" function
20924 */
20925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020926f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020928 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020929 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930}
20931
20932/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020933 * "tr(string, fromstr, tostr)" function
20934 */
20935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020936f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020937{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020938 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020939 char_u *fromstr;
20940 char_u *tostr;
20941 char_u *p;
20942#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020943 int inlen;
20944 int fromlen;
20945 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020946 int idx;
20947 char_u *cpstr;
20948 int cplen;
20949 int first = TRUE;
20950#endif
20951 char_u buf[NUMBUFLEN];
20952 char_u buf2[NUMBUFLEN];
20953 garray_T ga;
20954
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020955 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020956 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20957 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020958
20959 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020960 rettv->v_type = VAR_STRING;
20961 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020962 if (fromstr == NULL || tostr == NULL)
20963 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020964 ga_init2(&ga, (int)sizeof(char), 80);
20965
20966#ifdef FEAT_MBYTE
20967 if (!has_mbyte)
20968#endif
20969 /* not multi-byte: fromstr and tostr must be the same length */
20970 if (STRLEN(fromstr) != STRLEN(tostr))
20971 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020972#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020973error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020974#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020975 EMSG2(_(e_invarg2), fromstr);
20976 ga_clear(&ga);
20977 return;
20978 }
20979
20980 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020981 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020982 {
20983#ifdef FEAT_MBYTE
20984 if (has_mbyte)
20985 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020986 inlen = (*mb_ptr2len)(in_str);
20987 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020988 cplen = inlen;
20989 idx = 0;
20990 for (p = fromstr; *p != NUL; p += fromlen)
20991 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020992 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020993 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020994 {
20995 for (p = tostr; *p != NUL; p += tolen)
20996 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020997 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020998 if (idx-- == 0)
20999 {
21000 cplen = tolen;
21001 cpstr = p;
21002 break;
21003 }
21004 }
21005 if (*p == NUL) /* tostr is shorter than fromstr */
21006 goto error;
21007 break;
21008 }
21009 ++idx;
21010 }
21011
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021012 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021013 {
21014 /* Check that fromstr and tostr have the same number of
21015 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021016 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021017 first = FALSE;
21018 for (p = tostr; *p != NUL; p += tolen)
21019 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021020 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021021 --idx;
21022 }
21023 if (idx != 0)
21024 goto error;
21025 }
21026
Bram Moolenaarcde88542015-08-11 19:14:00 +020021027 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021028 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021029 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021030
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021031 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021032 }
21033 else
21034#endif
21035 {
21036 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021037 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021038 if (p != NULL)
21039 ga_append(&ga, tostr[p - fromstr]);
21040 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021041 ga_append(&ga, *in_str);
21042 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021043 }
21044 }
21045
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021046 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021047 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021048 ga_append(&ga, NUL);
21049
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021050 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021051}
21052
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021053#ifdef FEAT_FLOAT
21054/*
21055 * "trunc({float})" function
21056 */
21057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021058f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021059{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021060 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021061
21062 rettv->v_type = VAR_FLOAT;
21063 if (get_float_arg(argvars, &f) == OK)
21064 /* trunc() is not in C90, use floor() or ceil() instead. */
21065 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21066 else
21067 rettv->vval.v_float = 0.0;
21068}
21069#endif
21070
Bram Moolenaar8299df92004-07-10 09:47:34 +000021071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072 * "type(expr)" function
21073 */
21074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021075f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021077 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021078
21079 switch (argvars[0].v_type)
21080 {
21081 case VAR_NUMBER: n = 0; break;
21082 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021083 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021084 case VAR_FUNC: n = 2; break;
21085 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021086 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021087 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021088 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021089 if (argvars[0].vval.v_number == VVAL_FALSE
21090 || argvars[0].vval.v_number == VVAL_TRUE)
21091 n = 6;
21092 else
21093 n = 7;
21094 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021095 case VAR_JOB: n = 8; break;
21096 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021097 case VAR_UNKNOWN:
21098 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21099 n = -1;
21100 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021101 }
21102 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103}
21104
21105/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021106 * "undofile(name)" function
21107 */
21108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021109f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021110{
21111 rettv->v_type = VAR_STRING;
21112#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021113 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021114 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021115
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021116 if (*fname == NUL)
21117 {
21118 /* If there is no file name there will be no undo file. */
21119 rettv->vval.v_string = NULL;
21120 }
21121 else
21122 {
21123 char_u *ffname = FullName_save(fname, FALSE);
21124
21125 if (ffname != NULL)
21126 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21127 vim_free(ffname);
21128 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021129 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021130#else
21131 rettv->vval.v_string = NULL;
21132#endif
21133}
21134
21135/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021136 * "undotree()" function
21137 */
21138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021139f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021140{
21141 if (rettv_dict_alloc(rettv) == OK)
21142 {
21143 dict_T *dict = rettv->vval.v_dict;
21144 list_T *list;
21145
Bram Moolenaar730cde92010-06-27 05:18:54 +020021146 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021147 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021148 dict_add_nr_str(dict, "save_last",
21149 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021150 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21151 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021152 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021153
21154 list = list_alloc();
21155 if (list != NULL)
21156 {
21157 u_eval_tree(curbuf->b_u_oldhead, list);
21158 dict_add_list(dict, "entries", list);
21159 }
21160 }
21161}
21162
21163/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021164 * "values(dict)" function
21165 */
21166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021167f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021168{
21169 dict_list(argvars, rettv, 1);
21170}
21171
21172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 * "virtcol(string)" function
21174 */
21175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021176f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177{
21178 colnr_T vcol = 0;
21179 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021180 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021182 fp = var2fpos(&argvars[0], FALSE, &fnum);
21183 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21184 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185 {
21186 getvvcol(curwin, fp, NULL, NULL, &vcol);
21187 ++vcol;
21188 }
21189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021190 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191}
21192
21193/*
21194 * "visualmode()" function
21195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021197f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021199 char_u str[2];
21200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021201 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021202 str[0] = curbuf->b_visual_mode_eval;
21203 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021204 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205
21206 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021207 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209}
21210
21211/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021212 * "wildmenumode()" function
21213 */
21214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021215f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021216{
21217#ifdef FEAT_WILDMENU
21218 if (wild_menu_showing)
21219 rettv->vval.v_number = 1;
21220#endif
21221}
21222
21223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 * "winbufnr(nr)" function
21225 */
21226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021227f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228{
21229 win_T *wp;
21230
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021231 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021233 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021235 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236}
21237
21238/*
21239 * "wincol()" function
21240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021242f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243{
21244 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021245 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246}
21247
21248/*
21249 * "winheight(nr)" function
21250 */
21251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021252f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253{
21254 win_T *wp;
21255
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021256 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021258 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021260 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021261}
21262
21263/*
21264 * "winline()" function
21265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021267f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268{
21269 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021270 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271}
21272
21273/*
21274 * "winnr()" function
21275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021277f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278{
21279 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021280
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021282 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021284 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285}
21286
21287/*
21288 * "winrestcmd()" function
21289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021291f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292{
21293#ifdef FEAT_WINDOWS
21294 win_T *wp;
21295 int winnr = 1;
21296 garray_T ga;
21297 char_u buf[50];
21298
21299 ga_init2(&ga, (int)sizeof(char), 70);
21300 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21301 {
21302 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21303 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21305 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 ++winnr;
21307 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021308 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021310 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021312 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021314 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315}
21316
21317/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021318 * "winrestview()" function
21319 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021321f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021322{
21323 dict_T *dict;
21324
21325 if (argvars[0].v_type != VAR_DICT
21326 || (dict = argvars[0].vval.v_dict) == NULL)
21327 EMSG(_(e_invarg));
21328 else
21329 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021330 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21331 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21332 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21333 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021334#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021335 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21336 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021337#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021338 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21339 {
21340 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21341 curwin->w_set_curswant = FALSE;
21342 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021343
Bram Moolenaar82c25852014-05-28 16:47:16 +020021344 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21345 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021346#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021347 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21348 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021349#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021350 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21351 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21352 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21353 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021354
21355 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021356 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021357# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021358 win_new_width(curwin, W_WIDTH(curwin));
21359# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021360 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021361
Bram Moolenaarb851a962014-10-31 15:45:52 +010021362 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021363 curwin->w_topline = 1;
21364 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21365 curwin->w_topline = curbuf->b_ml.ml_line_count;
21366#ifdef FEAT_DIFF
21367 check_topfill(curwin, TRUE);
21368#endif
21369 }
21370}
21371
21372/*
21373 * "winsaveview()" function
21374 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021376f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021377{
21378 dict_T *dict;
21379
Bram Moolenaara800b422010-06-27 01:15:55 +020021380 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021381 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021382 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021383
21384 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21385 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21386#ifdef FEAT_VIRTUALEDIT
21387 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21388#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021389 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021390 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21391
21392 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21393#ifdef FEAT_DIFF
21394 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21395#endif
21396 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21397 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21398}
21399
21400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 * "winwidth(nr)" function
21402 */
21403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021404f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405{
21406 win_T *wp;
21407
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021408 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021410 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021412#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021413 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021415 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416#endif
21417}
21418
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021420 * "wordcount()" function
21421 */
21422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021423f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021424{
21425 if (rettv_dict_alloc(rettv) == FAIL)
21426 return;
21427 cursor_pos_info(rettv->vval.v_dict);
21428}
21429
21430/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021431 * Write list of strings to file
21432 */
21433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021434write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021435{
21436 listitem_T *li;
21437 int c;
21438 int ret = OK;
21439 char_u *s;
21440
21441 for (li = list->lv_first; li != NULL; li = li->li_next)
21442 {
21443 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21444 {
21445 if (*s == '\n')
21446 c = putc(NUL, fd);
21447 else
21448 c = putc(*s, fd);
21449 if (c == EOF)
21450 {
21451 ret = FAIL;
21452 break;
21453 }
21454 }
21455 if (!binary || li->li_next != NULL)
21456 if (putc('\n', fd) == EOF)
21457 {
21458 ret = FAIL;
21459 break;
21460 }
21461 if (ret == FAIL)
21462 {
21463 EMSG(_(e_write));
21464 break;
21465 }
21466 }
21467 return ret;
21468}
21469
21470/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021471 * "writefile()" function
21472 */
21473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021474f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021475{
21476 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021477 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021478 char_u *fname;
21479 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021480 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021481
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021482 if (check_restricted() || check_secure())
21483 return;
21484
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021485 if (argvars[0].v_type != VAR_LIST)
21486 {
21487 EMSG2(_(e_listarg), "writefile()");
21488 return;
21489 }
21490 if (argvars[0].vval.v_list == NULL)
21491 return;
21492
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021493 if (argvars[2].v_type != VAR_UNKNOWN)
21494 {
21495 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21496 binary = TRUE;
21497 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21498 append = TRUE;
21499 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021500
21501 /* Always open the file in binary mode, library functions have a mind of
21502 * their own about CR-LF conversion. */
21503 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021504 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21505 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021506 {
21507 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21508 ret = -1;
21509 }
21510 else
21511 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021512 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21513 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021514 fclose(fd);
21515 }
21516
21517 rettv->vval.v_number = ret;
21518}
21519
21520/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021521 * "xor(expr, expr)" function
21522 */
21523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021524f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021525{
21526 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21527 ^ get_tv_number_chk(&argvars[1], NULL);
21528}
21529
21530
21531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021533 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 */
21535 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021536var2fpos(
21537 typval_T *varp,
21538 int dollar_lnum, /* TRUE when $ is last line */
21539 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021541 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021543 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544
Bram Moolenaara5525202006-03-02 22:52:09 +000021545 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021546 if (varp->v_type == VAR_LIST)
21547 {
21548 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021549 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021550 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021551 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021552
21553 l = varp->vval.v_list;
21554 if (l == NULL)
21555 return NULL;
21556
21557 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021558 pos.lnum = list_find_nr(l, 0L, &error);
21559 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021560 return NULL; /* invalid line number */
21561
21562 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021563 pos.col = list_find_nr(l, 1L, &error);
21564 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021565 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021566 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021567
21568 /* We accept "$" for the column number: last column. */
21569 li = list_find(l, 1L);
21570 if (li != NULL && li->li_tv.v_type == VAR_STRING
21571 && li->li_tv.vval.v_string != NULL
21572 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21573 pos.col = len + 1;
21574
Bram Moolenaara5525202006-03-02 22:52:09 +000021575 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021576 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021577 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021578 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021579
Bram Moolenaara5525202006-03-02 22:52:09 +000021580#ifdef FEAT_VIRTUALEDIT
21581 /* Get the virtual offset. Defaults to zero. */
21582 pos.coladd = list_find_nr(l, 2L, &error);
21583 if (error)
21584 pos.coladd = 0;
21585#endif
21586
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021587 return &pos;
21588 }
21589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021590 name = get_tv_string_chk(varp);
21591 if (name == NULL)
21592 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021593 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021595 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21596 {
21597 if (VIsual_active)
21598 return &VIsual;
21599 return &curwin->w_cursor;
21600 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021601 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021603 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21605 return NULL;
21606 return pp;
21607 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021608
21609#ifdef FEAT_VIRTUALEDIT
21610 pos.coladd = 0;
21611#endif
21612
Bram Moolenaar477933c2007-07-17 14:32:23 +000021613 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021614 {
21615 pos.col = 0;
21616 if (name[1] == '0') /* "w0": first visible line */
21617 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021618 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021619 pos.lnum = curwin->w_topline;
21620 return &pos;
21621 }
21622 else if (name[1] == '$') /* "w$": last visible line */
21623 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021624 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021625 pos.lnum = curwin->w_botline - 1;
21626 return &pos;
21627 }
21628 }
21629 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021631 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 {
21633 pos.lnum = curbuf->b_ml.ml_line_count;
21634 pos.col = 0;
21635 }
21636 else
21637 {
21638 pos.lnum = curwin->w_cursor.lnum;
21639 pos.col = (colnr_T)STRLEN(ml_get_curline());
21640 }
21641 return &pos;
21642 }
21643 return NULL;
21644}
21645
21646/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021647 * Convert list in "arg" into a position and optional file number.
21648 * When "fnump" is NULL there is no file number, only 3 items.
21649 * Note that the column is passed on as-is, the caller may want to decrement
21650 * it to use 1 for the first column.
21651 * Return FAIL when conversion is not possible, doesn't check the position for
21652 * validity.
21653 */
21654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021655list2fpos(
21656 typval_T *arg,
21657 pos_T *posp,
21658 int *fnump,
21659 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021660{
21661 list_T *l = arg->vval.v_list;
21662 long i = 0;
21663 long n;
21664
Bram Moolenaar493c1782014-05-28 14:34:46 +020021665 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21666 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021667 if (arg->v_type != VAR_LIST
21668 || l == NULL
21669 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021670 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021671 return FAIL;
21672
21673 if (fnump != NULL)
21674 {
21675 n = list_find_nr(l, i++, NULL); /* fnum */
21676 if (n < 0)
21677 return FAIL;
21678 if (n == 0)
21679 n = curbuf->b_fnum; /* current buffer */
21680 *fnump = n;
21681 }
21682
21683 n = list_find_nr(l, i++, NULL); /* lnum */
21684 if (n < 0)
21685 return FAIL;
21686 posp->lnum = n;
21687
21688 n = list_find_nr(l, i++, NULL); /* col */
21689 if (n < 0)
21690 return FAIL;
21691 posp->col = n;
21692
21693#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021694 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021695 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021696 posp->coladd = 0;
21697 else
21698 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021699#endif
21700
Bram Moolenaar493c1782014-05-28 14:34:46 +020021701 if (curswantp != NULL)
21702 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21703
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021704 return OK;
21705}
21706
21707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 * Get the length of an environment variable name.
21709 * Advance "arg" to the first character after the name.
21710 * Return 0 for error.
21711 */
21712 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021713get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714{
21715 char_u *p;
21716 int len;
21717
21718 for (p = *arg; vim_isIDc(*p); ++p)
21719 ;
21720 if (p == *arg) /* no name found */
21721 return 0;
21722
21723 len = (int)(p - *arg);
21724 *arg = p;
21725 return len;
21726}
21727
21728/*
21729 * Get the length of the name of a function or internal variable.
21730 * "arg" is advanced to the first non-white character after the name.
21731 * Return 0 if something is wrong.
21732 */
21733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021734get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735{
21736 char_u *p;
21737 int len;
21738
21739 /* Find the end of the name. */
21740 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021741 {
21742 if (*p == ':')
21743 {
21744 /* "s:" is start of "s:var", but "n:" is not and can be used in
21745 * slice "[n:]". Also "xx:" is not a namespace. */
21746 len = (int)(p - *arg);
21747 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21748 || len > 1)
21749 break;
21750 }
21751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 if (p == *arg) /* no name found */
21753 return 0;
21754
21755 len = (int)(p - *arg);
21756 *arg = skipwhite(p);
21757
21758 return len;
21759}
21760
21761/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021762 * Get the length of the name of a variable or function.
21763 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021765 * Return -1 if curly braces expansion failed.
21766 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 * If the name contains 'magic' {}'s, expand them and return the
21768 * expanded name in an allocated string via 'alias' - caller must free.
21769 */
21770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021771get_name_len(
21772 char_u **arg,
21773 char_u **alias,
21774 int evaluate,
21775 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776{
21777 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 char_u *p;
21779 char_u *expr_start;
21780 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781
21782 *alias = NULL; /* default to no alias */
21783
21784 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21785 && (*arg)[2] == (int)KE_SNR)
21786 {
21787 /* hard coded <SNR>, already translated */
21788 *arg += 3;
21789 return get_id_len(arg) + 3;
21790 }
21791 len = eval_fname_script(*arg);
21792 if (len > 0)
21793 {
21794 /* literal "<SID>", "s:" or "<SNR>" */
21795 *arg += len;
21796 }
21797
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021799 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021801 p = find_name_end(*arg, &expr_start, &expr_end,
21802 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 if (expr_start != NULL)
21804 {
21805 char_u *temp_string;
21806
21807 if (!evaluate)
21808 {
21809 len += (int)(p - *arg);
21810 *arg = skipwhite(p);
21811 return len;
21812 }
21813
21814 /*
21815 * Include any <SID> etc in the expanded string:
21816 * Thus the -len here.
21817 */
21818 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21819 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021820 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021821 *alias = temp_string;
21822 *arg = skipwhite(p);
21823 return (int)STRLEN(temp_string);
21824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825
21826 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021827 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828 EMSG2(_(e_invexpr2), *arg);
21829
21830 return len;
21831}
21832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021833/*
21834 * Find the end of a variable or function name, taking care of magic braces.
21835 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21836 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021837 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021838 * Return a pointer to just after the name. Equal to "arg" if there is no
21839 * valid name.
21840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021842find_name_end(
21843 char_u *arg,
21844 char_u **expr_start,
21845 char_u **expr_end,
21846 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021848 int mb_nest = 0;
21849 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021851 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021853 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021855 *expr_start = NULL;
21856 *expr_end = NULL;
21857 }
21858
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021859 /* Quick check for valid starting character. */
21860 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21861 return arg;
21862
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021863 for (p = arg; *p != NUL
21864 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021865 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021866 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021867 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021868 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021869 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021870 if (*p == '\'')
21871 {
21872 /* skip over 'string' to avoid counting [ and ] inside it. */
21873 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21874 ;
21875 if (*p == NUL)
21876 break;
21877 }
21878 else if (*p == '"')
21879 {
21880 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21881 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21882 if (*p == '\\' && p[1] != NUL)
21883 ++p;
21884 if (*p == NUL)
21885 break;
21886 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021887 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21888 {
21889 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021890 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021891 len = (int)(p - arg);
21892 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021893 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021894 break;
21895 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021897 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021899 if (*p == '[')
21900 ++br_nest;
21901 else if (*p == ']')
21902 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021905 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021907 if (*p == '{')
21908 {
21909 mb_nest++;
21910 if (expr_start != NULL && *expr_start == NULL)
21911 *expr_start = p;
21912 }
21913 else if (*p == '}')
21914 {
21915 mb_nest--;
21916 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21917 *expr_end = p;
21918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 }
21921
21922 return p;
21923}
21924
21925/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021926 * Expands out the 'magic' {}'s in a variable/function name.
21927 * Note that this can call itself recursively, to deal with
21928 * constructs like foo{bar}{baz}{bam}
21929 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21930 * "in_start" ^
21931 * "expr_start" ^
21932 * "expr_end" ^
21933 * "in_end" ^
21934 *
21935 * Returns a new allocated string, which the caller must free.
21936 * Returns NULL for failure.
21937 */
21938 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021939make_expanded_name(
21940 char_u *in_start,
21941 char_u *expr_start,
21942 char_u *expr_end,
21943 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021944{
21945 char_u c1;
21946 char_u *retval = NULL;
21947 char_u *temp_result;
21948 char_u *nextcmd = NULL;
21949
21950 if (expr_end == NULL || in_end == NULL)
21951 return NULL;
21952 *expr_start = NUL;
21953 *expr_end = NUL;
21954 c1 = *in_end;
21955 *in_end = NUL;
21956
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021957 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021958 if (temp_result != NULL && nextcmd == NULL)
21959 {
21960 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21961 + (in_end - expr_end) + 1));
21962 if (retval != NULL)
21963 {
21964 STRCPY(retval, in_start);
21965 STRCAT(retval, temp_result);
21966 STRCAT(retval, expr_end + 1);
21967 }
21968 }
21969 vim_free(temp_result);
21970
21971 *in_end = c1; /* put char back for error messages */
21972 *expr_start = '{';
21973 *expr_end = '}';
21974
21975 if (retval != NULL)
21976 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021977 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021978 if (expr_start != NULL)
21979 {
21980 /* Further expansion! */
21981 temp_result = make_expanded_name(retval, expr_start,
21982 expr_end, temp_result);
21983 vim_free(retval);
21984 retval = temp_result;
21985 }
21986 }
21987
21988 return retval;
21989}
21990
21991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021993 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 */
21995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021996eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021998 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21999}
22000
22001/*
22002 * Return TRUE if character "c" can be used as the first character in a
22003 * variable or function name (excluding '{' and '}').
22004 */
22005 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022006eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022007{
22008 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009}
22010
22011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 * Set number v: variable to "val".
22013 */
22014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022015set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022017 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018}
22019
22020/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022021 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 */
22023 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022024get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022026 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027}
22028
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022029/*
22030 * Get string v: variable value. Uses a static buffer, can only be used once.
22031 */
22032 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022033get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022034{
22035 return get_tv_string(&vimvars[idx].vv_tv);
22036}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022037
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022039 * Get List v: variable value. Caller must take care of reference count when
22040 * needed.
22041 */
22042 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022043get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022044{
22045 return vimvars[idx].vv_list;
22046}
22047
22048/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022049 * Set v:char to character "c".
22050 */
22051 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022052set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022053{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022054 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022055
22056#ifdef FEAT_MBYTE
22057 if (has_mbyte)
22058 buf[(*mb_char2bytes)(c, buf)] = NUL;
22059 else
22060#endif
22061 {
22062 buf[0] = c;
22063 buf[1] = NUL;
22064 }
22065 set_vim_var_string(VV_CHAR, buf, -1);
22066}
22067
22068/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022069 * Set v:count to "count" and v:count1 to "count1".
22070 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 */
22072 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022073set_vcount(
22074 long count,
22075 long count1,
22076 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022078 if (set_prevcount)
22079 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022080 vimvars[VV_COUNT].vv_nr = count;
22081 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082}
22083
22084/*
22085 * Set string v: variable to a copy of "val".
22086 */
22087 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022088set_vim_var_string(
22089 int idx,
22090 char_u *val,
22091 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092{
Bram Moolenaara542c682016-01-31 16:28:04 +010022093 clear_tv(&vimvars[idx].vv_di.di_tv);
22094 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022096 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022098 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022100 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101}
22102
22103/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022104 * Set List v: variable to "val".
22105 */
22106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022107set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022108{
Bram Moolenaara542c682016-01-31 16:28:04 +010022109 clear_tv(&vimvars[idx].vv_di.di_tv);
22110 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022111 vimvars[idx].vv_list = val;
22112 if (val != NULL)
22113 ++val->lv_refcount;
22114}
22115
22116/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022117 * Set Dictionary v: variable to "val".
22118 */
22119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022120set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022121{
22122 int todo;
22123 hashitem_T *hi;
22124
Bram Moolenaara542c682016-01-31 16:28:04 +010022125 clear_tv(&vimvars[idx].vv_di.di_tv);
22126 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022127 vimvars[idx].vv_dict = val;
22128 if (val != NULL)
22129 {
22130 ++val->dv_refcount;
22131
22132 /* Set readonly */
22133 todo = (int)val->dv_hashtab.ht_used;
22134 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22135 {
22136 if (HASHITEM_EMPTY(hi))
22137 continue;
22138 --todo;
22139 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22140 }
22141 }
22142}
22143
22144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 * Set v:register if needed.
22146 */
22147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022148set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149{
22150 char_u regname;
22151
22152 if (c == 0 || c == ' ')
22153 regname = '"';
22154 else
22155 regname = c;
22156 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022157 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 set_vim_var_string(VV_REG, &regname, 1);
22159}
22160
22161/*
22162 * Get or set v:exception. If "oldval" == NULL, return the current value.
22163 * Otherwise, restore the value to "oldval" and return NULL.
22164 * Must always be called in pairs to save and restore v:exception! Does not
22165 * take care of memory allocations.
22166 */
22167 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022168v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169{
22170 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022171 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172
Bram Moolenaare9a41262005-01-15 22:18:47 +000022173 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 return NULL;
22175}
22176
22177/*
22178 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22179 * Otherwise, restore the value to "oldval" and return NULL.
22180 * Must always be called in pairs to save and restore v:throwpoint! Does not
22181 * take care of memory allocations.
22182 */
22183 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022184v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185{
22186 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022187 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188
Bram Moolenaare9a41262005-01-15 22:18:47 +000022189 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022190 return NULL;
22191}
22192
22193#if defined(FEAT_AUTOCMD) || defined(PROTO)
22194/*
22195 * Set v:cmdarg.
22196 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22197 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22198 * Must always be called in pairs!
22199 */
22200 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022201set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202{
22203 char_u *oldval;
22204 char_u *newval;
22205 unsigned len;
22206
Bram Moolenaare9a41262005-01-15 22:18:47 +000022207 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022208 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022210 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022211 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022212 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 }
22214
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022215 if (eap->force_bin == FORCE_BIN)
22216 len = 6;
22217 else if (eap->force_bin == FORCE_NOBIN)
22218 len = 8;
22219 else
22220 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022221
22222 if (eap->read_edit)
22223 len += 7;
22224
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022225 if (eap->force_ff != 0)
22226 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22227# ifdef FEAT_MBYTE
22228 if (eap->force_enc != 0)
22229 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022230 if (eap->bad_char != 0)
22231 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022232# endif
22233
22234 newval = alloc(len + 1);
22235 if (newval == NULL)
22236 return NULL;
22237
22238 if (eap->force_bin == FORCE_BIN)
22239 sprintf((char *)newval, " ++bin");
22240 else if (eap->force_bin == FORCE_NOBIN)
22241 sprintf((char *)newval, " ++nobin");
22242 else
22243 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022244
22245 if (eap->read_edit)
22246 STRCAT(newval, " ++edit");
22247
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022248 if (eap->force_ff != 0)
22249 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22250 eap->cmd + eap->force_ff);
22251# ifdef FEAT_MBYTE
22252 if (eap->force_enc != 0)
22253 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22254 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022255 if (eap->bad_char == BAD_KEEP)
22256 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22257 else if (eap->bad_char == BAD_DROP)
22258 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22259 else if (eap->bad_char != 0)
22260 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022261# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022262 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022263 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264}
22265#endif
22266
22267/*
22268 * Get the value of internal variable "name".
22269 * Return OK or FAIL.
22270 */
22271 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022272get_var_tv(
22273 char_u *name,
22274 int len, /* length of "name" */
22275 typval_T *rettv, /* NULL when only checking existence */
22276 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22277 int verbose, /* may give error message */
22278 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279{
22280 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022281 typval_T *tv = NULL;
22282 typval_T atv;
22283 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285
22286 /* truncate the name, so that we can use strcmp() */
22287 cc = name[len];
22288 name[len] = NUL;
22289
22290 /*
22291 * Check for "b:changedtick".
22292 */
22293 if (STRCMP(name, "b:changedtick") == 0)
22294 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022295 atv.v_type = VAR_NUMBER;
22296 atv.vval.v_number = curbuf->b_changedtick;
22297 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 }
22299
22300 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301 * Check for user-defined variables.
22302 */
22303 else
22304 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022305 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022307 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022308 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022309 if (dip != NULL)
22310 *dip = v;
22311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022312 }
22313
Bram Moolenaare9a41262005-01-15 22:18:47 +000022314 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022316 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022317 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318 ret = FAIL;
22319 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022320 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022321 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322
22323 name[len] = cc;
22324
22325 return ret;
22326}
22327
22328/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022329 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22330 * Also handle function call with Funcref variable: func(expr)
22331 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22332 */
22333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022334handle_subscript(
22335 char_u **arg,
22336 typval_T *rettv,
22337 int evaluate, /* do more than finding the end */
22338 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022339{
22340 int ret = OK;
22341 dict_T *selfdict = NULL;
22342 char_u *s;
22343 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022344 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022345
22346 while (ret == OK
22347 && (**arg == '['
22348 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022349 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22350 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022351 && !vim_iswhite(*(*arg - 1)))
22352 {
22353 if (**arg == '(')
22354 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022355 partial_T *pt = NULL;
22356
Bram Moolenaard9fba312005-06-26 22:34:35 +000022357 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022358 if (evaluate)
22359 {
22360 functv = *rettv;
22361 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022362
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022363 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022364 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022365 {
22366 pt = functv.vval.v_partial;
22367 s = pt->pt_name;
22368 }
22369 else
22370 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022371 }
22372 else
22373 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022374 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022375 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022376 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022377
22378 /* Clear the funcref afterwards, so that deleting it while
22379 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022380 if (evaluate)
22381 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022382
22383 /* Stop the expression evaluation when immediately aborting on
22384 * error, or when an interrupt occurred or an exception was thrown
22385 * but not caught. */
22386 if (aborting())
22387 {
22388 if (ret == OK)
22389 clear_tv(rettv);
22390 ret = FAIL;
22391 }
22392 dict_unref(selfdict);
22393 selfdict = NULL;
22394 }
22395 else /* **arg == '[' || **arg == '.' */
22396 {
22397 dict_unref(selfdict);
22398 if (rettv->v_type == VAR_DICT)
22399 {
22400 selfdict = rettv->vval.v_dict;
22401 if (selfdict != NULL)
22402 ++selfdict->dv_refcount;
22403 }
22404 else
22405 selfdict = NULL;
22406 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22407 {
22408 clear_tv(rettv);
22409 ret = FAIL;
22410 }
22411 }
22412 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022413
Bram Moolenaar1d429612016-05-24 15:44:17 +020022414 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22415 * Don't do this when "Func" is already a partial that was bound
22416 * explicitly (pt_auto is FALSE). */
22417 if (selfdict != NULL
22418 && (rettv->v_type == VAR_FUNC
22419 || (rettv->v_type == VAR_PARTIAL
22420 && (rettv->vval.v_partial->pt_auto
22421 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022422 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022423 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22424 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022425 char_u *tofree = NULL;
22426 ufunc_T *fp;
22427 char_u fname_buf[FLEN_FIXED + 1];
22428 int error;
22429
22430 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022431 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022432 fp = find_func(fname);
22433 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022434
Bram Moolenaar65639032016-03-16 21:40:30 +010022435 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022436 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022437 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22438
22439 if (pt != NULL)
22440 {
22441 pt->pt_refcount = 1;
22442 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022443 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022444 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022445 if (rettv->v_type == VAR_FUNC)
22446 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022447 /* Just a function: Take over the function name and use
22448 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022449 pt->pt_name = rettv->vval.v_string;
22450 }
22451 else
22452 {
22453 partial_T *ret_pt = rettv->vval.v_partial;
22454 int i;
22455
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022456 /* Partial: copy the function name, use selfdict and copy
22457 * args. Can't take over name or args, the partial might
22458 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022459 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022460 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022461 if (ret_pt->pt_argc > 0)
22462 {
22463 pt->pt_argv = (typval_T *)alloc(
22464 sizeof(typval_T) * ret_pt->pt_argc);
22465 if (pt->pt_argv == NULL)
22466 /* out of memory: drop the arguments */
22467 pt->pt_argc = 0;
22468 else
22469 {
22470 pt->pt_argc = ret_pt->pt_argc;
22471 for (i = 0; i < pt->pt_argc; i++)
22472 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22473 }
22474 }
22475 partial_unref(ret_pt);
22476 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022477 rettv->v_type = VAR_PARTIAL;
22478 rettv->vval.v_partial = pt;
22479 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022480 }
22481 }
22482
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022483 dict_unref(selfdict);
22484 return ret;
22485}
22486
22487/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022488 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022489 * value).
22490 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022491 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022492alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022493{
Bram Moolenaar33570922005-01-25 22:26:29 +000022494 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022495}
22496
22497/*
22498 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 * The string "s" must have been allocated, it is consumed.
22500 * Return NULL for out of memory, the variable otherwise.
22501 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022502 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022503alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504{
Bram Moolenaar33570922005-01-25 22:26:29 +000022505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022507 rettv = alloc_tv();
22508 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022510 rettv->v_type = VAR_STRING;
22511 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 }
22513 else
22514 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022515 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516}
22517
22518/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022519 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022520 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022521 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022522free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523{
22524 if (varp != NULL)
22525 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022526 switch (varp->v_type)
22527 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022528 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022529 func_unref(varp->vval.v_string);
22530 /*FALLTHROUGH*/
22531 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022532 vim_free(varp->vval.v_string);
22533 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022534 case VAR_PARTIAL:
22535 partial_unref(varp->vval.v_partial);
22536 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022537 case VAR_LIST:
22538 list_unref(varp->vval.v_list);
22539 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022540 case VAR_DICT:
22541 dict_unref(varp->vval.v_dict);
22542 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022543 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022544#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022545 job_unref(varp->vval.v_job);
22546 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022547#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022548 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022549#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022550 channel_unref(varp->vval.v_channel);
22551 break;
22552#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022553 case VAR_NUMBER:
22554 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022555 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022556 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022557 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 vim_free(varp);
22560 }
22561}
22562
22563/*
22564 * Free the memory for a variable value and set the value to NULL or 0.
22565 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022566 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022567clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568{
22569 if (varp != NULL)
22570 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022571 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022573 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022574 func_unref(varp->vval.v_string);
22575 /*FALLTHROUGH*/
22576 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022577 vim_free(varp->vval.v_string);
22578 varp->vval.v_string = NULL;
22579 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022580 case VAR_PARTIAL:
22581 partial_unref(varp->vval.v_partial);
22582 varp->vval.v_partial = NULL;
22583 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022584 case VAR_LIST:
22585 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022586 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022587 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022588 case VAR_DICT:
22589 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022590 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022591 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022592 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022593 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022594 varp->vval.v_number = 0;
22595 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022596 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022597#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022598 varp->vval.v_float = 0.0;
22599 break;
22600#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022601 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022602#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022603 job_unref(varp->vval.v_job);
22604 varp->vval.v_job = NULL;
22605#endif
22606 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022607 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022608#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022609 channel_unref(varp->vval.v_channel);
22610 varp->vval.v_channel = NULL;
22611#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022612 case VAR_UNKNOWN:
22613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022615 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 }
22617}
22618
22619/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022620 * Set the value of a variable to NULL without freeing items.
22621 */
22622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022623init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022624{
22625 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022626 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022627}
22628
22629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 * Get the number value of a variable.
22631 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022632 * For incompatible types, return 0.
22633 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22634 * caller of incompatible types: it sets *denote to TRUE if "denote"
22635 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022637 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022638get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022640 int error = FALSE;
22641
22642 return get_tv_number_chk(varp, &error); /* return 0L on error */
22643}
22644
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022645 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022646get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022647{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022648 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022650 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022652 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022653 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022654 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022655#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022656 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022657 break;
22658#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022659 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022660 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022661 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022662 break;
22663 case VAR_STRING:
22664 if (varp->vval.v_string != NULL)
22665 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022666 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022667 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022668 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022669 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022670 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022671 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022672 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022673 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022674 case VAR_SPECIAL:
22675 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22676 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022677 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022678#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022679 EMSG(_("E910: Using a Job as a Number"));
22680 break;
22681#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022682 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022683#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022684 EMSG(_("E913: Using a Channel as a Number"));
22685 break;
22686#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022687 case VAR_UNKNOWN:
22688 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022689 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022691 if (denote == NULL) /* useful for values that must be unsigned */
22692 n = -1;
22693 else
22694 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022695 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696}
22697
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022698#ifdef FEAT_FLOAT
22699 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022700get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022701{
22702 switch (varp->v_type)
22703 {
22704 case VAR_NUMBER:
22705 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022706 case VAR_FLOAT:
22707 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022708 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022709 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022710 EMSG(_("E891: Using a Funcref as a Float"));
22711 break;
22712 case VAR_STRING:
22713 EMSG(_("E892: Using a String as a Float"));
22714 break;
22715 case VAR_LIST:
22716 EMSG(_("E893: Using a List as a Float"));
22717 break;
22718 case VAR_DICT:
22719 EMSG(_("E894: Using a Dictionary as a Float"));
22720 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022721 case VAR_SPECIAL:
22722 EMSG(_("E907: Using a special value as a Float"));
22723 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022724 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022725# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022726 EMSG(_("E911: Using a Job as a Float"));
22727 break;
22728# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022729 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022730# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022731 EMSG(_("E914: Using a Channel as a Float"));
22732 break;
22733# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022734 case VAR_UNKNOWN:
22735 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022736 break;
22737 }
22738 return 0;
22739}
22740#endif
22741
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022743 * Get the lnum from the first argument.
22744 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022745 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746 */
22747 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022748get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749{
Bram Moolenaar33570922005-01-25 22:26:29 +000022750 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 linenr_T lnum;
22752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022753 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 if (lnum == 0) /* no valid number, try using line() */
22755 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022756 rettv.v_type = VAR_NUMBER;
22757 f_line(argvars, &rettv);
22758 lnum = rettv.vval.v_number;
22759 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 }
22761 return lnum;
22762}
22763
22764/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022765 * Get the lnum from the first argument.
22766 * Also accepts "$", then "buf" is used.
22767 * Returns 0 on error.
22768 */
22769 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022770get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022771{
22772 if (argvars[0].v_type == VAR_STRING
22773 && argvars[0].vval.v_string != NULL
22774 && argvars[0].vval.v_string[0] == '$'
22775 && buf != NULL)
22776 return buf->b_ml.ml_line_count;
22777 return get_tv_number_chk(&argvars[0], NULL);
22778}
22779
22780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781 * Get the string value of a variable.
22782 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022783 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22784 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022785 * If the String variable has never been set, return an empty string.
22786 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022787 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22788 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022790 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022791get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792{
22793 static char_u mybuf[NUMBUFLEN];
22794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022795 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796}
22797
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022798 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022799get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022801 char_u *res = get_tv_string_buf_chk(varp, buf);
22802
22803 return res != NULL ? res : (char_u *)"";
22804}
22805
Bram Moolenaar7d647822014-04-05 21:28:56 +020022806/*
22807 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22808 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022809 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022810get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022811{
22812 static char_u mybuf[NUMBUFLEN];
22813
22814 return get_tv_string_buf_chk(varp, mybuf);
22815}
22816
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022817 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022818get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022819{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022820 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022821 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022822 case VAR_NUMBER:
22823 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22824 return buf;
22825 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022826 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022827 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022828 break;
22829 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022830 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022831 break;
22832 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022833 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022834 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022835 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022836#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022837 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022838 break;
22839#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022840 case VAR_STRING:
22841 if (varp->vval.v_string != NULL)
22842 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022843 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022844 case VAR_SPECIAL:
22845 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22846 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022847 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022848#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022849 {
22850 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022851 char *status;
22852
22853 if (job == NULL)
22854 return (char_u *)"no process";
22855 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022856 : job->jv_status == JOB_ENDED ? "dead"
22857 : "run";
22858# ifdef UNIX
22859 vim_snprintf((char *)buf, NUMBUFLEN,
22860 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022861# elif defined(WIN32)
22862 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022863 "process %ld %s",
22864 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022865 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022866# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022867 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022868 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22869# endif
22870 return buf;
22871 }
22872#endif
22873 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022874 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022875#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022876 {
22877 channel_T *channel = varp->vval.v_channel;
22878 char *status = channel_status(channel);
22879
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022880 if (channel == NULL)
22881 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22882 else
22883 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022884 "channel %d %s", channel->ch_id, status);
22885 return buf;
22886 }
22887#endif
22888 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022889 case VAR_UNKNOWN:
22890 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022891 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022893 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894}
22895
22896/*
22897 * Find variable "name" in the list of variables.
22898 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022899 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022900 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022901 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022902 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022903 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022904find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022907 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908
Bram Moolenaara7043832005-01-21 11:56:39 +000022909 ht = find_var_ht(name, &varname);
22910 if (htp != NULL)
22911 *htp = ht;
22912 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022914 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915}
22916
22917/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022918 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022919 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022921 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022922find_var_in_ht(
22923 hashtab_T *ht,
22924 int htname,
22925 char_u *varname,
22926 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022927{
Bram Moolenaar33570922005-01-25 22:26:29 +000022928 hashitem_T *hi;
22929
22930 if (*varname == NUL)
22931 {
22932 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022933 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022934 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022935 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022936 case 'g': return &globvars_var;
22937 case 'v': return &vimvars_var;
22938 case 'b': return &curbuf->b_bufvar;
22939 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022940#ifdef FEAT_WINDOWS
22941 case 't': return &curtab->tp_winvar;
22942#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022943 case 'l': return current_funccal == NULL
22944 ? NULL : &current_funccal->l_vars_var;
22945 case 'a': return current_funccal == NULL
22946 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022947 }
22948 return NULL;
22949 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022950
22951 hi = hash_find(ht, varname);
22952 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022953 {
22954 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022955 * worked find the variable again. Don't auto-load a script if it was
22956 * loaded already, otherwise it would be loaded every time when
22957 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022958 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022959 {
22960 /* Note: script_autoload() may make "hi" invalid. It must either
22961 * be obtained again or not used. */
22962 if (!script_autoload(varname, FALSE) || aborting())
22963 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022964 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022965 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022966 if (HASHITEM_EMPTY(hi))
22967 return NULL;
22968 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022969 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022970}
22971
22972/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022973 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022974 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022975 * Set "varname" to the start of name without ':'.
22976 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022977 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022978find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022980 hashitem_T *hi;
22981
Bram Moolenaar73627d02015-08-11 15:46:09 +020022982 if (name[0] == NUL)
22983 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 if (name[1] != ':')
22985 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022986 /* The name must not start with a colon or #. */
22987 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 return NULL;
22989 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022990
22991 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022992 hi = hash_find(&compat_hashtab, name);
22993 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022994 return &compat_hashtab;
22995
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022997 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022998 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 }
23000 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023001 if (*name == 'g') /* global variable */
23002 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023003 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23004 */
23005 if (vim_strchr(name + 2, ':') != NULL
23006 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023007 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023009 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023011 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023012#ifdef FEAT_WINDOWS
23013 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023014 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023015#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023016 if (*name == 'v') /* v: variable */
23017 return &vimvarht;
23018 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023019 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023020 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023021 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022 if (*name == 's' /* script variable */
23023 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23024 return &SCRIPT_VARS(current_SID);
23025 return NULL;
23026}
23027
23028/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023029 * Get function call environment based on bactrace debug level
23030 */
23031 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023032get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023033{
23034 int i;
23035 funccall_T *funccal;
23036 funccall_T *temp_funccal;
23037
23038 funccal = current_funccal;
23039 if (debug_backtrace_level > 0)
23040 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023041 for (i = 0; i < debug_backtrace_level; i++)
23042 {
23043 temp_funccal = funccal->caller;
23044 if (temp_funccal)
23045 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023046 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023047 /* backtrace level overflow. reset to max */
23048 debug_backtrace_level = i;
23049 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023050 }
23051 return funccal;
23052}
23053
23054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023056 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 * Returns NULL when it doesn't exist.
23058 */
23059 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023060get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061{
Bram Moolenaar33570922005-01-25 22:26:29 +000023062 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023064 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 if (v == NULL)
23066 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023067 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068}
23069
23070/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023071 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072 * sourcing this script and when executing functions defined in the script.
23073 */
23074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023075new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076{
Bram Moolenaara7043832005-01-21 11:56:39 +000023077 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023078 hashtab_T *ht;
23079 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023080
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23082 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023083 /* Re-allocating ga_data means that an ht_array pointing to
23084 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023085 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023086 for (i = 1; i <= ga_scripts.ga_len; ++i)
23087 {
23088 ht = &SCRIPT_VARS(i);
23089 if (ht->ht_mask == HT_INIT_SIZE - 1)
23090 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023091 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023092 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023093 }
23094
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 while (ga_scripts.ga_len < id)
23096 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023097 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023098 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023099 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 }
23102 }
23103}
23104
23105/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023106 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23107 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 */
23109 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023110init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111{
Bram Moolenaar33570922005-01-25 22:26:29 +000023112 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023113 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023114 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023115 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023116 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023117 dict_var->di_tv.vval.v_dict = dict;
23118 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023119 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023120 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23121 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122}
23123
23124/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023125 * Unreference a dictionary initialized by init_var_dict().
23126 */
23127 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023128unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023129{
23130 /* Now the dict needs to be freed if no one else is using it, go back to
23131 * normal reference counting. */
23132 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23133 dict_unref(dict);
23134}
23135
23136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023138 * Frees all allocated variables and the value they contain.
23139 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023140 */
23141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023142vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023143{
23144 vars_clear_ext(ht, TRUE);
23145}
23146
23147/*
23148 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23149 */
23150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023151vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152{
Bram Moolenaara7043832005-01-21 11:56:39 +000023153 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023154 hashitem_T *hi;
23155 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156
Bram Moolenaar33570922005-01-25 22:26:29 +000023157 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023158 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023159 for (hi = ht->ht_array; todo > 0; ++hi)
23160 {
23161 if (!HASHITEM_EMPTY(hi))
23162 {
23163 --todo;
23164
Bram Moolenaar33570922005-01-25 22:26:29 +000023165 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023166 * ht_array might change then. hash_clear() takes care of it
23167 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023168 v = HI2DI(hi);
23169 if (free_val)
23170 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023171 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023172 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023173 }
23174 }
23175 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023176 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177}
23178
Bram Moolenaara7043832005-01-21 11:56:39 +000023179/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023180 * Delete a variable from hashtab "ht" at item "hi".
23181 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023184delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185{
Bram Moolenaar33570922005-01-25 22:26:29 +000023186 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023187
23188 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023189 clear_tv(&di->di_tv);
23190 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191}
23192
23193/*
23194 * List the value of one internal variable.
23195 */
23196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023197list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023198{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023199 char_u *tofree;
23200 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023201 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023202
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023203 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023204 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023205 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023206 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207}
23208
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023210list_one_var_a(
23211 char_u *prefix,
23212 char_u *name,
23213 int type,
23214 char_u *string,
23215 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216{
Bram Moolenaar31859182007-08-14 20:41:13 +000023217 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23218 msg_start();
23219 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 if (name != NULL) /* "a:" vars don't have a name stored */
23221 msg_puts(name);
23222 msg_putchar(' ');
23223 msg_advance(22);
23224 if (type == VAR_NUMBER)
23225 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023226 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023227 msg_putchar('*');
23228 else if (type == VAR_LIST)
23229 {
23230 msg_putchar('[');
23231 if (*string == '[')
23232 ++string;
23233 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023234 else if (type == VAR_DICT)
23235 {
23236 msg_putchar('{');
23237 if (*string == '{')
23238 ++string;
23239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 else
23241 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023242
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023244
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023245 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023246 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023247 if (*first)
23248 {
23249 msg_clr_eos();
23250 *first = FALSE;
23251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252}
23253
23254/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023255 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 * If the variable already exists, the value is updated.
23257 * Otherwise the variable is created.
23258 */
23259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023260set_var(
23261 char_u *name,
23262 typval_T *tv,
23263 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264{
Bram Moolenaar33570922005-01-25 22:26:29 +000023265 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023267 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023269 ht = find_var_ht(name, &varname);
23270 if (ht == NULL || *varname == NUL)
23271 {
23272 EMSG2(_(e_illvar), name);
23273 return;
23274 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023275 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023276
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023277 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23278 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023279 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023280
Bram Moolenaar33570922005-01-25 22:26:29 +000023281 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023283 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023284 if (var_check_ro(v->di_flags, name, FALSE)
23285 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023286 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023287
23288 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023289 * Handle setting internal v: variables separately where needed to
23290 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023291 */
23292 if (ht == &vimvarht)
23293 {
23294 if (v->di_tv.v_type == VAR_STRING)
23295 {
23296 vim_free(v->di_tv.vval.v_string);
23297 if (copy || tv->v_type != VAR_STRING)
23298 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23299 else
23300 {
23301 /* Take over the string to avoid an extra alloc/free. */
23302 v->di_tv.vval.v_string = tv->vval.v_string;
23303 tv->vval.v_string = NULL;
23304 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023305 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023306 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023307 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023308 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023309 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023310 if (STRCMP(varname, "searchforward") == 0)
23311 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023312#ifdef FEAT_SEARCH_EXTRA
23313 else if (STRCMP(varname, "hlsearch") == 0)
23314 {
23315 no_hlsearch = !v->di_tv.vval.v_number;
23316 redraw_all_later(SOME_VALID);
23317 }
23318#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023319 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023320 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023321 else if (v->di_tv.v_type != tv->v_type)
23322 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023323 }
23324
23325 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 }
23327 else /* add a new variable */
23328 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023329 /* Can't add "v:" variable. */
23330 if (ht == &vimvarht)
23331 {
23332 EMSG2(_(e_illvar), name);
23333 return;
23334 }
23335
Bram Moolenaar92124a32005-06-17 22:03:40 +000023336 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023337 if (!valid_varname(varname))
23338 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023339
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023340 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23341 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023342 if (v == NULL)
23343 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023344 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023345 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023347 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023348 return;
23349 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023350 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023351 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023352
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023353 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023354 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023355 else
23356 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023357 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023358 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023359 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361}
23362
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023363/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023364 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023365 * Also give an error message.
23366 */
23367 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023368var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023369{
23370 if (flags & DI_FLAGS_RO)
23371 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023372 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023373 return TRUE;
23374 }
23375 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23376 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023377 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023378 return TRUE;
23379 }
23380 return FALSE;
23381}
23382
23383/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023384 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23385 * Also give an error message.
23386 */
23387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023388var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023389{
23390 if (flags & DI_FLAGS_FIX)
23391 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023392 EMSG2(_("E795: Cannot delete variable %s"),
23393 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023394 return TRUE;
23395 }
23396 return FALSE;
23397}
23398
23399/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023400 * Check if a funcref is assigned to a valid variable name.
23401 * Return TRUE and give an error if not.
23402 */
23403 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023404var_check_func_name(
23405 char_u *name, /* points to start of variable name */
23406 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023407{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023408 /* Allow for w: b: s: and t:. */
23409 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023410 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23411 ? name[2] : name[0]))
23412 {
23413 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23414 name);
23415 return TRUE;
23416 }
23417 /* Don't allow hiding a function. When "v" is not NULL we might be
23418 * assigning another function to the same var, the type is checked
23419 * below. */
23420 if (new_var && function_exists(name))
23421 {
23422 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23423 name);
23424 return TRUE;
23425 }
23426 return FALSE;
23427}
23428
23429/*
23430 * Check if a variable name is valid.
23431 * Return FALSE and give an error if not.
23432 */
23433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023434valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023435{
23436 char_u *p;
23437
23438 for (p = varname; *p != NUL; ++p)
23439 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23440 && *p != AUTOLOAD_CHAR)
23441 {
23442 EMSG2(_(e_illvar), varname);
23443 return FALSE;
23444 }
23445 return TRUE;
23446}
23447
23448/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023449 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023450 * Also give an error message, using "name" or _("name") when use_gettext is
23451 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023452 */
23453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023454tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023455{
23456 if (lock & VAR_LOCKED)
23457 {
23458 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023459 name == NULL ? (char_u *)_("Unknown")
23460 : use_gettext ? (char_u *)_(name)
23461 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023462 return TRUE;
23463 }
23464 if (lock & VAR_FIXED)
23465 {
23466 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023467 name == NULL ? (char_u *)_("Unknown")
23468 : use_gettext ? (char_u *)_(name)
23469 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023470 return TRUE;
23471 }
23472 return FALSE;
23473}
23474
23475/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023476 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023477 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023478 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023479 * It is OK for "from" and "to" to point to the same item. This is used to
23480 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023481 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023483copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023484{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023485 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023486 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023487 switch (from->v_type)
23488 {
23489 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023490 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023491 to->vval.v_number = from->vval.v_number;
23492 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023493 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023494#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023495 to->vval.v_float = from->vval.v_float;
23496 break;
23497#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023498 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023500 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023501 if (to->vval.v_job != NULL)
23502 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023503 break;
23504#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023505 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023506#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023507 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023508 if (to->vval.v_channel != NULL)
23509 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023510 break;
23511#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023512 case VAR_STRING:
23513 case VAR_FUNC:
23514 if (from->vval.v_string == NULL)
23515 to->vval.v_string = NULL;
23516 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023517 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023518 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023519 if (from->v_type == VAR_FUNC)
23520 func_ref(to->vval.v_string);
23521 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023522 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023523 case VAR_PARTIAL:
23524 if (from->vval.v_partial == NULL)
23525 to->vval.v_partial = NULL;
23526 else
23527 {
23528 to->vval.v_partial = from->vval.v_partial;
23529 ++to->vval.v_partial->pt_refcount;
23530 }
23531 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023532 case VAR_LIST:
23533 if (from->vval.v_list == NULL)
23534 to->vval.v_list = NULL;
23535 else
23536 {
23537 to->vval.v_list = from->vval.v_list;
23538 ++to->vval.v_list->lv_refcount;
23539 }
23540 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023541 case VAR_DICT:
23542 if (from->vval.v_dict == NULL)
23543 to->vval.v_dict = NULL;
23544 else
23545 {
23546 to->vval.v_dict = from->vval.v_dict;
23547 ++to->vval.v_dict->dv_refcount;
23548 }
23549 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023550 case VAR_UNKNOWN:
23551 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023552 break;
23553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554}
23555
23556/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023557 * Make a copy of an item.
23558 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023559 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23560 * reference to an already copied list/dict can be used.
23561 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023562 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023563 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023564item_copy(
23565 typval_T *from,
23566 typval_T *to,
23567 int deep,
23568 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023569{
23570 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023571 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023572
Bram Moolenaar33570922005-01-25 22:26:29 +000023573 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023574 {
23575 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023576 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023577 }
23578 ++recurse;
23579
23580 switch (from->v_type)
23581 {
23582 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023583 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023584 case VAR_STRING:
23585 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023586 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023587 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023588 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023589 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023590 copy_tv(from, to);
23591 break;
23592 case VAR_LIST:
23593 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023594 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023595 if (from->vval.v_list == NULL)
23596 to->vval.v_list = NULL;
23597 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23598 {
23599 /* use the copy made earlier */
23600 to->vval.v_list = from->vval.v_list->lv_copylist;
23601 ++to->vval.v_list->lv_refcount;
23602 }
23603 else
23604 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23605 if (to->vval.v_list == NULL)
23606 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023607 break;
23608 case VAR_DICT:
23609 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023610 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023611 if (from->vval.v_dict == NULL)
23612 to->vval.v_dict = NULL;
23613 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23614 {
23615 /* use the copy made earlier */
23616 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23617 ++to->vval.v_dict->dv_refcount;
23618 }
23619 else
23620 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23621 if (to->vval.v_dict == NULL)
23622 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023623 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023624 case VAR_UNKNOWN:
23625 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023626 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023627 }
23628 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023629 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023630}
23631
23632/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 * ":echo expr1 ..." print each argument separated with a space, add a
23634 * newline at the end.
23635 * ":echon expr1 ..." print each argument plain.
23636 */
23637 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023638ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639{
23640 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023641 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023642 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 char_u *p;
23644 int needclr = TRUE;
23645 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023646 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647
23648 if (eap->skip)
23649 ++emsg_skip;
23650 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23651 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023652 /* If eval1() causes an error message the text from the command may
23653 * still need to be cleared. E.g., "echo 22,44". */
23654 need_clr_eos = needclr;
23655
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023657 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 {
23659 /*
23660 * Report the invalid expression unless the expression evaluation
23661 * has been cancelled due to an aborting error, an interrupt, or an
23662 * exception.
23663 */
23664 if (!aborting())
23665 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023666 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 break;
23668 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023669 need_clr_eos = FALSE;
23670
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671 if (!eap->skip)
23672 {
23673 if (atstart)
23674 {
23675 atstart = FALSE;
23676 /* Call msg_start() after eval1(), evaluating the expression
23677 * may cause a message to appear. */
23678 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023679 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023680 /* Mark the saved text as finishing the line, so that what
23681 * follows is displayed on a new line when scrolling back
23682 * at the more prompt. */
23683 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023686 }
23687 else if (eap->cmdidx == CMD_echo)
23688 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023689 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023690 if (p != NULL)
23691 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023693 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023694 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023695 if (*p != TAB && needclr)
23696 {
23697 /* remove any text still there from the command */
23698 msg_clr_eos();
23699 needclr = FALSE;
23700 }
23701 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 }
23703 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023704 {
23705#ifdef FEAT_MBYTE
23706 if (has_mbyte)
23707 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023708 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023709
23710 (void)msg_outtrans_len_attr(p, i, echo_attr);
23711 p += i - 1;
23712 }
23713 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023715 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023718 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023720 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023721 arg = skipwhite(arg);
23722 }
23723 eap->nextcmd = check_nextcmd(arg);
23724
23725 if (eap->skip)
23726 --emsg_skip;
23727 else
23728 {
23729 /* remove text that may still be there from the command */
23730 if (needclr)
23731 msg_clr_eos();
23732 if (eap->cmdidx == CMD_echo)
23733 msg_end();
23734 }
23735}
23736
23737/*
23738 * ":echohl {name}".
23739 */
23740 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023741ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742{
23743 int id;
23744
23745 id = syn_name2id(eap->arg);
23746 if (id == 0)
23747 echo_attr = 0;
23748 else
23749 echo_attr = syn_id2attr(id);
23750}
23751
23752/*
23753 * ":execute expr1 ..." execute the result of an expression.
23754 * ":echomsg expr1 ..." Print a message
23755 * ":echoerr expr1 ..." Print an error
23756 * Each gets spaces around each argument and a newline at the end for
23757 * echo commands
23758 */
23759 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023760ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023761{
23762 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023763 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 int ret = OK;
23765 char_u *p;
23766 garray_T ga;
23767 int len;
23768 int save_did_emsg;
23769
23770 ga_init2(&ga, 1, 80);
23771
23772 if (eap->skip)
23773 ++emsg_skip;
23774 while (*arg != NUL && *arg != '|' && *arg != '\n')
23775 {
23776 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023777 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023778 {
23779 /*
23780 * Report the invalid expression unless the expression evaluation
23781 * has been cancelled due to an aborting error, an interrupt, or an
23782 * exception.
23783 */
23784 if (!aborting())
23785 EMSG2(_(e_invexpr2), p);
23786 ret = FAIL;
23787 break;
23788 }
23789
23790 if (!eap->skip)
23791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023792 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023793 len = (int)STRLEN(p);
23794 if (ga_grow(&ga, len + 2) == FAIL)
23795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023796 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797 ret = FAIL;
23798 break;
23799 }
23800 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803 ga.ga_len += len;
23804 }
23805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023806 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807 arg = skipwhite(arg);
23808 }
23809
23810 if (ret != FAIL && ga.ga_data != NULL)
23811 {
23812 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023813 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023815 out_flush();
23816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 else if (eap->cmdidx == CMD_echoerr)
23818 {
23819 /* We don't want to abort following commands, restore did_emsg. */
23820 save_did_emsg = did_emsg;
23821 EMSG((char_u *)ga.ga_data);
23822 if (!force_abort)
23823 did_emsg = save_did_emsg;
23824 }
23825 else if (eap->cmdidx == CMD_execute)
23826 do_cmdline((char_u *)ga.ga_data,
23827 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23828 }
23829
23830 ga_clear(&ga);
23831
23832 if (eap->skip)
23833 --emsg_skip;
23834
23835 eap->nextcmd = check_nextcmd(arg);
23836}
23837
23838/*
23839 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23840 * "arg" points to the "&" or '+' when called, to "option" when returning.
23841 * Returns NULL when no option name found. Otherwise pointer to the char
23842 * after the option name.
23843 */
23844 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023845find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846{
23847 char_u *p = *arg;
23848
23849 ++p;
23850 if (*p == 'g' && p[1] == ':')
23851 {
23852 *opt_flags = OPT_GLOBAL;
23853 p += 2;
23854 }
23855 else if (*p == 'l' && p[1] == ':')
23856 {
23857 *opt_flags = OPT_LOCAL;
23858 p += 2;
23859 }
23860 else
23861 *opt_flags = 0;
23862
23863 if (!ASCII_ISALPHA(*p))
23864 return NULL;
23865 *arg = p;
23866
23867 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23868 p += 4; /* termcap option */
23869 else
23870 while (ASCII_ISALPHA(*p))
23871 ++p;
23872 return p;
23873}
23874
23875/*
23876 * ":function"
23877 */
23878 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023879ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880{
23881 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023882 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 int j;
23884 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023886 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 char_u *name = NULL;
23888 char_u *p;
23889 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023890 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 garray_T newargs;
23892 garray_T newlines;
23893 int varargs = FALSE;
23894 int mustend = FALSE;
23895 int flags = 0;
23896 ufunc_T *fp;
23897 int indent;
23898 int nesting;
23899 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023900 dictitem_T *v;
23901 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023902 static int func_nr = 0; /* number for nameless function */
23903 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023904 hashtab_T *ht;
23905 int todo;
23906 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023907 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023908
23909 /*
23910 * ":function" without argument: list functions.
23911 */
23912 if (ends_excmd(*eap->arg))
23913 {
23914 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023915 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023916 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023917 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023918 {
23919 if (!HASHITEM_EMPTY(hi))
23920 {
23921 --todo;
23922 fp = HI2UF(hi);
23923 if (!isdigit(*fp->uf_name))
23924 list_func_head(fp, FALSE);
23925 }
23926 }
23927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023928 eap->nextcmd = check_nextcmd(eap->arg);
23929 return;
23930 }
23931
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023932 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023933 * ":function /pat": list functions matching pattern.
23934 */
23935 if (*eap->arg == '/')
23936 {
23937 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23938 if (!eap->skip)
23939 {
23940 regmatch_T regmatch;
23941
23942 c = *p;
23943 *p = NUL;
23944 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23945 *p = c;
23946 if (regmatch.regprog != NULL)
23947 {
23948 regmatch.rm_ic = p_ic;
23949
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023950 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023951 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23952 {
23953 if (!HASHITEM_EMPTY(hi))
23954 {
23955 --todo;
23956 fp = HI2UF(hi);
23957 if (!isdigit(*fp->uf_name)
23958 && vim_regexec(&regmatch, fp->uf_name, 0))
23959 list_func_head(fp, FALSE);
23960 }
23961 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023962 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023963 }
23964 }
23965 if (*p == '/')
23966 ++p;
23967 eap->nextcmd = check_nextcmd(p);
23968 return;
23969 }
23970
23971 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023972 * Get the function name. There are these situations:
23973 * func normal function name
23974 * "name" == func, "fudi.fd_dict" == NULL
23975 * dict.func new dictionary entry
23976 * "name" == NULL, "fudi.fd_dict" set,
23977 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23978 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023979 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023980 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23981 * dict.func existing dict entry that's not a Funcref
23982 * "name" == NULL, "fudi.fd_dict" set,
23983 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023984 * s:func script-local function name
23985 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023988 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023989 paren = (vim_strchr(p, '(') != NULL);
23990 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 {
23992 /*
23993 * Return on an invalid expression in braces, unless the expression
23994 * evaluation has been cancelled due to an aborting error, an
23995 * interrupt, or an exception.
23996 */
23997 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023998 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023999 if (!eap->skip && fudi.fd_newkey != NULL)
24000 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024001 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 else
24005 eap->skip = TRUE;
24006 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024007
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 /* An error in a function call during evaluation of an expression in magic
24009 * braces should not cause the function not to be defined. */
24010 saved_did_emsg = did_emsg;
24011 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012
24013 /*
24014 * ":function func" with only function name: list function.
24015 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024016 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024017 {
24018 if (!ends_excmd(*skipwhite(p)))
24019 {
24020 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024021 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 }
24023 eap->nextcmd = check_nextcmd(p);
24024 if (eap->nextcmd != NULL)
24025 *p = NUL;
24026 if (!eap->skip && !got_int)
24027 {
24028 fp = find_func(name);
24029 if (fp != NULL)
24030 {
24031 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024032 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024034 if (FUNCLINE(fp, j) == NULL)
24035 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036 msg_putchar('\n');
24037 msg_outnum((long)(j + 1));
24038 if (j < 9)
24039 msg_putchar(' ');
24040 if (j < 99)
24041 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024042 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024043 out_flush(); /* show a line at a time */
24044 ui_breakcheck();
24045 }
24046 if (!got_int)
24047 {
24048 msg_putchar('\n');
24049 msg_puts((char_u *)" endfunction");
24050 }
24051 }
24052 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024053 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024055 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024056 }
24057
24058 /*
24059 * ":function name(arg1, arg2)" Define function.
24060 */
24061 p = skipwhite(p);
24062 if (*p != '(')
24063 {
24064 if (!eap->skip)
24065 {
24066 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024067 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024068 }
24069 /* attempt to continue by skipping some text */
24070 if (vim_strchr(p, '(') != NULL)
24071 p = vim_strchr(p, '(');
24072 }
24073 p = skipwhite(p + 1);
24074
24075 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24076 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24077
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024078 if (!eap->skip)
24079 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024080 /* Check the name of the function. Unless it's a dictionary function
24081 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024082 if (name != NULL)
24083 arg = name;
24084 else
24085 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024086 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024087 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24088 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024089 {
24090 if (*arg == K_SPECIAL)
24091 j = 3;
24092 else
24093 j = 0;
24094 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24095 : eval_isnamec(arg[j])))
24096 ++j;
24097 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024098 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024099 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024100 /* Disallow using the g: dict. */
24101 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24102 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024103 }
24104
Bram Moolenaar071d4272004-06-13 20:20:40 +000024105 /*
24106 * Isolate the arguments: "arg1, arg2, ...)"
24107 */
24108 while (*p != ')')
24109 {
24110 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24111 {
24112 varargs = TRUE;
24113 p += 3;
24114 mustend = TRUE;
24115 }
24116 else
24117 {
24118 arg = p;
24119 while (ASCII_ISALNUM(*p) || *p == '_')
24120 ++p;
24121 if (arg == p || isdigit(*arg)
24122 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24123 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24124 {
24125 if (!eap->skip)
24126 EMSG2(_("E125: Illegal argument: %s"), arg);
24127 break;
24128 }
24129 if (ga_grow(&newargs, 1) == FAIL)
24130 goto erret;
24131 c = *p;
24132 *p = NUL;
24133 arg = vim_strsave(arg);
24134 if (arg == NULL)
24135 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024136
24137 /* Check for duplicate argument name. */
24138 for (i = 0; i < newargs.ga_len; ++i)
24139 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24140 {
24141 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024142 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024143 goto erret;
24144 }
24145
Bram Moolenaar071d4272004-06-13 20:20:40 +000024146 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24147 *p = c;
24148 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 if (*p == ',')
24150 ++p;
24151 else
24152 mustend = TRUE;
24153 }
24154 p = skipwhite(p);
24155 if (mustend && *p != ')')
24156 {
24157 if (!eap->skip)
24158 EMSG2(_(e_invarg2), eap->arg);
24159 break;
24160 }
24161 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024162 if (*p != ')')
24163 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164 ++p; /* skip the ')' */
24165
Bram Moolenaare9a41262005-01-15 22:18:47 +000024166 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 for (;;)
24168 {
24169 p = skipwhite(p);
24170 if (STRNCMP(p, "range", 5) == 0)
24171 {
24172 flags |= FC_RANGE;
24173 p += 5;
24174 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024175 else if (STRNCMP(p, "dict", 4) == 0)
24176 {
24177 flags |= FC_DICT;
24178 p += 4;
24179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024180 else if (STRNCMP(p, "abort", 5) == 0)
24181 {
24182 flags |= FC_ABORT;
24183 p += 5;
24184 }
24185 else
24186 break;
24187 }
24188
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024189 /* When there is a line break use what follows for the function body.
24190 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24191 if (*p == '\n')
24192 line_arg = p + 1;
24193 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194 EMSG(_(e_trailing));
24195
24196 /*
24197 * Read the body of the function, until ":endfunction" is found.
24198 */
24199 if (KeyTyped)
24200 {
24201 /* Check if the function already exists, don't let the user type the
24202 * whole function before telling him it doesn't work! For a script we
24203 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024204 if (!eap->skip && !eap->forceit)
24205 {
24206 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24207 EMSG(_(e_funcdict));
24208 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024209 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024212 if (!eap->skip && did_emsg)
24213 goto erret;
24214
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215 msg_putchar('\n'); /* don't overwrite the function name */
24216 cmdline_row = msg_row;
24217 }
24218
24219 indent = 2;
24220 nesting = 0;
24221 for (;;)
24222 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024223 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024224 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024225 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024226 saved_wait_return = FALSE;
24227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024228 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024229 sourcing_lnum_off = sourcing_lnum;
24230
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024231 if (line_arg != NULL)
24232 {
24233 /* Use eap->arg, split up in parts by line breaks. */
24234 theline = line_arg;
24235 p = vim_strchr(theline, '\n');
24236 if (p == NULL)
24237 line_arg += STRLEN(line_arg);
24238 else
24239 {
24240 *p = NUL;
24241 line_arg = p + 1;
24242 }
24243 }
24244 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 theline = getcmdline(':', 0L, indent);
24246 else
24247 theline = eap->getline(':', eap->cookie, indent);
24248 if (KeyTyped)
24249 lines_left = Rows - 1;
24250 if (theline == NULL)
24251 {
24252 EMSG(_("E126: Missing :endfunction"));
24253 goto erret;
24254 }
24255
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024256 /* Detect line continuation: sourcing_lnum increased more than one. */
24257 if (sourcing_lnum > sourcing_lnum_off + 1)
24258 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24259 else
24260 sourcing_lnum_off = 0;
24261
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 if (skip_until != NULL)
24263 {
24264 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24265 * don't check for ":endfunc". */
24266 if (STRCMP(theline, skip_until) == 0)
24267 {
24268 vim_free(skip_until);
24269 skip_until = NULL;
24270 }
24271 }
24272 else
24273 {
24274 /* skip ':' and blanks*/
24275 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24276 ;
24277
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024278 /* Check for "endfunction". */
24279 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024280 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024281 if (line_arg == NULL)
24282 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283 break;
24284 }
24285
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024286 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287 * at "end". */
24288 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24289 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024290 else if (STRNCMP(p, "if", 2) == 0
24291 || STRNCMP(p, "wh", 2) == 0
24292 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 || STRNCMP(p, "try", 3) == 0)
24294 indent += 2;
24295
24296 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024297 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024299 if (*p == '!')
24300 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024302 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024303 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024305 ++nesting;
24306 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307 }
24308 }
24309
24310 /* Check for ":append" or ":insert". */
24311 p = skip_range(p, NULL);
24312 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24313 || (p[0] == 'i'
24314 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24315 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24316 skip_until = vim_strsave((char_u *)".");
24317
24318 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24319 arg = skipwhite(skiptowhite(p));
24320 if (arg[0] == '<' && arg[1] =='<'
24321 && ((p[0] == 'p' && p[1] == 'y'
24322 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24323 || (p[0] == 'p' && p[1] == 'e'
24324 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24325 || (p[0] == 't' && p[1] == 'c'
24326 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024327 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24328 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24330 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024331 || (p[0] == 'm' && p[1] == 'z'
24332 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333 ))
24334 {
24335 /* ":python <<" continues until a dot, like ":append" */
24336 p = skipwhite(arg + 2);
24337 if (*p == NUL)
24338 skip_until = vim_strsave((char_u *)".");
24339 else
24340 skip_until = vim_strsave(p);
24341 }
24342 }
24343
24344 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024345 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024346 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024347 if (line_arg == NULL)
24348 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024350 }
24351
24352 /* Copy the line to newly allocated memory. get_one_sourceline()
24353 * allocates 250 bytes per line, this saves 80% on average. The cost
24354 * is an extra alloc/free. */
24355 p = vim_strsave(theline);
24356 if (p != NULL)
24357 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024358 if (line_arg == NULL)
24359 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024360 theline = p;
24361 }
24362
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024363 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24364
24365 /* Add NULL lines for continuation lines, so that the line count is
24366 * equal to the index in the growarray. */
24367 while (sourcing_lnum_off-- > 0)
24368 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024369
24370 /* Check for end of eap->arg. */
24371 if (line_arg != NULL && *line_arg == NUL)
24372 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 }
24374
24375 /* Don't define the function when skipping commands or when an error was
24376 * detected. */
24377 if (eap->skip || did_emsg)
24378 goto erret;
24379
24380 /*
24381 * If there are no errors, add the function
24382 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024383 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024384 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024385 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024386 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024387 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024388 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024389 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024390 goto erret;
24391 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024393 fp = find_func(name);
24394 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024396 if (!eap->forceit)
24397 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024398 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024399 goto erret;
24400 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024401 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024402 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024403 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024404 name);
24405 goto erret;
24406 }
24407 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024408 ga_clear_strings(&(fp->uf_args));
24409 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024410 vim_free(name);
24411 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024413 }
24414 else
24415 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024416 char numbuf[20];
24417
24418 fp = NULL;
24419 if (fudi.fd_newkey == NULL && !eap->forceit)
24420 {
24421 EMSG(_(e_funcdict));
24422 goto erret;
24423 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024424 if (fudi.fd_di == NULL)
24425 {
24426 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024427 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024428 goto erret;
24429 }
24430 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024431 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024432 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024433
24434 /* Give the function a sequential number. Can only be used with a
24435 * Funcref! */
24436 vim_free(name);
24437 sprintf(numbuf, "%d", ++func_nr);
24438 name = vim_strsave((char_u *)numbuf);
24439 if (name == NULL)
24440 goto erret;
24441 }
24442
24443 if (fp == NULL)
24444 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024445 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024446 {
24447 int slen, plen;
24448 char_u *scriptname;
24449
24450 /* Check that the autoload name matches the script name. */
24451 j = FAIL;
24452 if (sourcing_name != NULL)
24453 {
24454 scriptname = autoload_name(name);
24455 if (scriptname != NULL)
24456 {
24457 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024458 plen = (int)STRLEN(p);
24459 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024460 if (slen > plen && fnamecmp(p,
24461 sourcing_name + slen - plen) == 0)
24462 j = OK;
24463 vim_free(scriptname);
24464 }
24465 }
24466 if (j == FAIL)
24467 {
24468 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24469 goto erret;
24470 }
24471 }
24472
24473 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 if (fp == NULL)
24475 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024476
24477 if (fudi.fd_dict != NULL)
24478 {
24479 if (fudi.fd_di == NULL)
24480 {
24481 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024482 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024483 if (fudi.fd_di == NULL)
24484 {
24485 vim_free(fp);
24486 goto erret;
24487 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024488 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24489 {
24490 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024491 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024492 goto erret;
24493 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024494 }
24495 else
24496 /* overwrite existing dict entry */
24497 clear_tv(&fudi.fd_di->di_tv);
24498 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024499 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024500 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024501 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024502
24503 /* behave like "dict" was used */
24504 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024505 }
24506
Bram Moolenaar071d4272004-06-13 20:20:40 +000024507 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024508 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024509 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24510 {
24511 vim_free(fp);
24512 goto erret;
24513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024515 fp->uf_args = newargs;
24516 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024517#ifdef FEAT_PROFILE
24518 fp->uf_tml_count = NULL;
24519 fp->uf_tml_total = NULL;
24520 fp->uf_tml_self = NULL;
24521 fp->uf_profiling = FALSE;
24522 if (prof_def_func())
24523 func_do_profile(fp);
24524#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024525 fp->uf_varargs = varargs;
24526 fp->uf_flags = flags;
24527 fp->uf_calls = 0;
24528 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024529 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530
24531erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024532 ga_clear_strings(&newargs);
24533 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024534ret_free:
24535 vim_free(skip_until);
24536 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024539 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540}
24541
24542/*
24543 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024544 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024546 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024547 * TFN_INT: internal function name OK
24548 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024549 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 * Advances "pp" to just after the function name (if no error).
24551 */
24552 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024553trans_function_name(
24554 char_u **pp,
24555 int skip, /* only find the end, don't evaluate */
24556 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024557 funcdict_T *fdp, /* return: info about dictionary used */
24558 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024560 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024561 char_u *start;
24562 char_u *end;
24563 int lead;
24564 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024565 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024566 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024567
24568 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024569 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024571
24572 /* Check for hard coded <SNR>: already translated function ID (from a user
24573 * command). */
24574 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24575 && (*pp)[2] == (int)KE_SNR)
24576 {
24577 *pp += 3;
24578 len = get_id_len(pp) + 3;
24579 return vim_strnsave(start, len);
24580 }
24581
24582 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24583 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024585 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024586 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024587
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024588 /* Note that TFN_ flags use the same values as GLV_ flags. */
24589 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024590 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024591 if (end == start)
24592 {
24593 if (!skip)
24594 EMSG(_("E129: Function name required"));
24595 goto theend;
24596 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024597 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024598 {
24599 /*
24600 * Report an invalid expression in braces, unless the expression
24601 * evaluation has been cancelled due to an aborting error, an
24602 * interrupt, or an exception.
24603 */
24604 if (!aborting())
24605 {
24606 if (end != NULL)
24607 EMSG2(_(e_invarg2), start);
24608 }
24609 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024610 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024611 goto theend;
24612 }
24613
24614 if (lv.ll_tv != NULL)
24615 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024616 if (fdp != NULL)
24617 {
24618 fdp->fd_dict = lv.ll_dict;
24619 fdp->fd_newkey = lv.ll_newkey;
24620 lv.ll_newkey = NULL;
24621 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024622 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024623 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24624 {
24625 name = vim_strsave(lv.ll_tv->vval.v_string);
24626 *pp = end;
24627 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024628 else if (lv.ll_tv->v_type == VAR_PARTIAL
24629 && lv.ll_tv->vval.v_partial != NULL)
24630 {
24631 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24632 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024633 if (partial != NULL)
24634 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024635 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024636 else
24637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024638 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24639 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024640 EMSG(_(e_funcref));
24641 else
24642 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024643 name = NULL;
24644 }
24645 goto theend;
24646 }
24647
24648 if (lv.ll_name == NULL)
24649 {
24650 /* Error found, but continue after the function name. */
24651 *pp = end;
24652 goto theend;
24653 }
24654
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024655 /* Check if the name is a Funcref. If so, use the value. */
24656 if (lv.ll_exp_name != NULL)
24657 {
24658 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024659 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024660 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024661 if (name == lv.ll_exp_name)
24662 name = NULL;
24663 }
24664 else
24665 {
24666 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024667 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024668 if (name == *pp)
24669 name = NULL;
24670 }
24671 if (name != NULL)
24672 {
24673 name = vim_strsave(name);
24674 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024675 if (STRNCMP(name, "<SNR>", 5) == 0)
24676 {
24677 /* Change "<SNR>" to the byte sequence. */
24678 name[0] = K_SPECIAL;
24679 name[1] = KS_EXTRA;
24680 name[2] = (int)KE_SNR;
24681 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24682 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024683 goto theend;
24684 }
24685
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024686 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024687 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024688 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024689 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24690 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24691 {
24692 /* When there was "s:" already or the name expanded to get a
24693 * leading "s:" then remove it. */
24694 lv.ll_name += 2;
24695 len -= 2;
24696 lead = 2;
24697 }
24698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024699 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024700 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024701 /* skip over "s:" and "g:" */
24702 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024703 lv.ll_name += 2;
24704 len = (int)(end - lv.ll_name);
24705 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024706
24707 /*
24708 * Copy the function name to allocated memory.
24709 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24710 * Accept <SNR>123_name() outside a script.
24711 */
24712 if (skip)
24713 lead = 0; /* do nothing */
24714 else if (lead > 0)
24715 {
24716 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024717 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24718 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024719 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024720 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024721 if (current_SID <= 0)
24722 {
24723 EMSG(_(e_usingsid));
24724 goto theend;
24725 }
24726 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24727 lead += (int)STRLEN(sid_buf);
24728 }
24729 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024730 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024731 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024732 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024733 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024734 goto theend;
24735 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024736 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024737 {
24738 char_u *cp = vim_strchr(lv.ll_name, ':');
24739
24740 if (cp != NULL && cp < end)
24741 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024742 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024743 goto theend;
24744 }
24745 }
24746
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024747 name = alloc((unsigned)(len + lead + 1));
24748 if (name != NULL)
24749 {
24750 if (lead > 0)
24751 {
24752 name[0] = K_SPECIAL;
24753 name[1] = KS_EXTRA;
24754 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024755 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024756 STRCPY(name + 3, sid_buf);
24757 }
24758 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024759 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024760 }
24761 *pp = end;
24762
24763theend:
24764 clear_lval(&lv);
24765 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766}
24767
24768/*
24769 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24770 * Return 2 if "p" starts with "s:".
24771 * Return 0 otherwise.
24772 */
24773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024774eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024776 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24777 * the standard library function. */
24778 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24779 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780 return 5;
24781 if (p[0] == 's' && p[1] == ':')
24782 return 2;
24783 return 0;
24784}
24785
24786/*
24787 * Return TRUE if "p" starts with "<SID>" or "s:".
24788 * Only works if eval_fname_script() returned non-zero for "p"!
24789 */
24790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024791eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792{
24793 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24794}
24795
24796/*
24797 * List the head of the function: "name(arg1, arg2)".
24798 */
24799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024800list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801{
24802 int j;
24803
24804 msg_start();
24805 if (indent)
24806 MSG_PUTS(" ");
24807 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024808 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809 {
24810 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024811 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812 }
24813 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024814 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024816 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817 {
24818 if (j)
24819 MSG_PUTS(", ");
24820 msg_puts(FUNCARG(fp, j));
24821 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024822 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823 {
24824 if (j)
24825 MSG_PUTS(", ");
24826 MSG_PUTS("...");
24827 }
24828 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024829 if (fp->uf_flags & FC_ABORT)
24830 MSG_PUTS(" abort");
24831 if (fp->uf_flags & FC_RANGE)
24832 MSG_PUTS(" range");
24833 if (fp->uf_flags & FC_DICT)
24834 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024835 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024836 if (p_verbose > 0)
24837 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838}
24839
24840/*
24841 * Find a function by name, return pointer to it in ufuncs.
24842 * Return NULL for unknown function.
24843 */
24844 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024845find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024847 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024848
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024849 hi = hash_find(&func_hashtab, name);
24850 if (!HASHITEM_EMPTY(hi))
24851 return HI2UF(hi);
24852 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853}
24854
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024855#if defined(EXITFREE) || defined(PROTO)
24856 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024857free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024858{
24859 hashitem_T *hi;
24860
24861 /* Need to start all over every time, because func_free() may change the
24862 * hash table. */
24863 while (func_hashtab.ht_used > 0)
24864 for (hi = func_hashtab.ht_array; ; ++hi)
24865 if (!HASHITEM_EMPTY(hi))
24866 {
24867 func_free(HI2UF(hi));
24868 break;
24869 }
24870}
24871#endif
24872
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024873 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024874translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024875{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024876 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024877 return find_internal_func(name) >= 0;
24878 return find_func(name) != NULL;
24879}
24880
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024881/*
24882 * Return TRUE if a function "name" exists.
24883 */
24884 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024885function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024886{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024887 char_u *nm = name;
24888 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024889 int n = FALSE;
24890
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024891 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024892 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024893 nm = skipwhite(nm);
24894
24895 /* Only accept "funcname", "funcname ", "funcname (..." and
24896 * "funcname(...", not "funcname!...". */
24897 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024898 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024899 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024900 return n;
24901}
24902
Bram Moolenaara1544c02013-05-30 12:35:52 +020024903 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024904get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024905{
24906 char_u *nm = name;
24907 char_u *p;
24908
Bram Moolenaar65639032016-03-16 21:40:30 +010024909 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024910
24911 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024912 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024913 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024914
Bram Moolenaara1544c02013-05-30 12:35:52 +020024915 vim_free(p);
24916 return NULL;
24917}
24918
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024919/*
24920 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024921 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24922 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024923 */
24924 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024925builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024926{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024927 char_u *p;
24928
24929 if (!ASCII_ISLOWER(name[0]))
24930 return FALSE;
24931 p = vim_strchr(name, AUTOLOAD_CHAR);
24932 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024933}
24934
Bram Moolenaar05159a02005-02-26 23:04:13 +000024935#if defined(FEAT_PROFILE) || defined(PROTO)
24936/*
24937 * Start profiling function "fp".
24938 */
24939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024940func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024941{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024942 int len = fp->uf_lines.ga_len;
24943
24944 if (len == 0)
24945 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024946 fp->uf_tm_count = 0;
24947 profile_zero(&fp->uf_tm_self);
24948 profile_zero(&fp->uf_tm_total);
24949 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024950 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024951 if (fp->uf_tml_total == NULL)
24952 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024953 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024954 if (fp->uf_tml_self == NULL)
24955 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024956 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024957 fp->uf_tml_idx = -1;
24958 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24959 || fp->uf_tml_self == NULL)
24960 return; /* out of memory */
24961
24962 fp->uf_profiling = TRUE;
24963}
24964
24965/*
24966 * Dump the profiling results for all functions in file "fd".
24967 */
24968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024969func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024970{
24971 hashitem_T *hi;
24972 int todo;
24973 ufunc_T *fp;
24974 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024975 ufunc_T **sorttab;
24976 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024977
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024978 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024979 if (todo == 0)
24980 return; /* nothing to dump */
24981
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024982 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024983
Bram Moolenaar05159a02005-02-26 23:04:13 +000024984 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24985 {
24986 if (!HASHITEM_EMPTY(hi))
24987 {
24988 --todo;
24989 fp = HI2UF(hi);
24990 if (fp->uf_profiling)
24991 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024992 if (sorttab != NULL)
24993 sorttab[st_len++] = fp;
24994
Bram Moolenaar05159a02005-02-26 23:04:13 +000024995 if (fp->uf_name[0] == K_SPECIAL)
24996 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24997 else
24998 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24999 if (fp->uf_tm_count == 1)
25000 fprintf(fd, "Called 1 time\n");
25001 else
25002 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25003 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25004 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25005 fprintf(fd, "\n");
25006 fprintf(fd, "count total (s) self (s)\n");
25007
25008 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25009 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025010 if (FUNCLINE(fp, i) == NULL)
25011 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025012 prof_func_line(fd, fp->uf_tml_count[i],
25013 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025014 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25015 }
25016 fprintf(fd, "\n");
25017 }
25018 }
25019 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025020
25021 if (sorttab != NULL && st_len > 0)
25022 {
25023 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25024 prof_total_cmp);
25025 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25026 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25027 prof_self_cmp);
25028 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25029 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025030
25031 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025032}
Bram Moolenaar73830342005-02-28 22:48:19 +000025033
25034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025035prof_sort_list(
25036 FILE *fd,
25037 ufunc_T **sorttab,
25038 int st_len,
25039 char *title,
25040 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025041{
25042 int i;
25043 ufunc_T *fp;
25044
25045 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25046 fprintf(fd, "count total (s) self (s) function\n");
25047 for (i = 0; i < 20 && i < st_len; ++i)
25048 {
25049 fp = sorttab[i];
25050 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25051 prefer_self);
25052 if (fp->uf_name[0] == K_SPECIAL)
25053 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25054 else
25055 fprintf(fd, " %s()\n", fp->uf_name);
25056 }
25057 fprintf(fd, "\n");
25058}
25059
25060/*
25061 * Print the count and times for one function or function line.
25062 */
25063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025064prof_func_line(
25065 FILE *fd,
25066 int count,
25067 proftime_T *total,
25068 proftime_T *self,
25069 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025070{
25071 if (count > 0)
25072 {
25073 fprintf(fd, "%5d ", count);
25074 if (prefer_self && profile_equal(total, self))
25075 fprintf(fd, " ");
25076 else
25077 fprintf(fd, "%s ", profile_msg(total));
25078 if (!prefer_self && profile_equal(total, self))
25079 fprintf(fd, " ");
25080 else
25081 fprintf(fd, "%s ", profile_msg(self));
25082 }
25083 else
25084 fprintf(fd, " ");
25085}
25086
25087/*
25088 * Compare function for total time sorting.
25089 */
25090 static int
25091#ifdef __BORLANDC__
25092_RTLENTRYF
25093#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025094prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025095{
25096 ufunc_T *p1, *p2;
25097
25098 p1 = *(ufunc_T **)s1;
25099 p2 = *(ufunc_T **)s2;
25100 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25101}
25102
25103/*
25104 * Compare function for self time sorting.
25105 */
25106 static int
25107#ifdef __BORLANDC__
25108_RTLENTRYF
25109#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025110prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025111{
25112 ufunc_T *p1, *p2;
25113
25114 p1 = *(ufunc_T **)s1;
25115 p2 = *(ufunc_T **)s2;
25116 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25117}
25118
Bram Moolenaar05159a02005-02-26 23:04:13 +000025119#endif
25120
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025121/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025122 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025123 * Return TRUE if a package was loaded.
25124 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025126script_autoload(
25127 char_u *name,
25128 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025129{
25130 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025131 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025132 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025133 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025134
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025135 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025136 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025137 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025138 return FALSE;
25139
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025140 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025141
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025142 /* Find the name in the list of previously loaded package names. Skip
25143 * "autoload/", it's always the same. */
25144 for (i = 0; i < ga_loaded.ga_len; ++i)
25145 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25146 break;
25147 if (!reload && i < ga_loaded.ga_len)
25148 ret = FALSE; /* was loaded already */
25149 else
25150 {
25151 /* Remember the name if it wasn't loaded already. */
25152 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25153 {
25154 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25155 tofree = NULL;
25156 }
25157
25158 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025159 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025160 ret = TRUE;
25161 }
25162
25163 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025164 return ret;
25165}
25166
25167/*
25168 * Return the autoload script name for a function or variable name.
25169 * Returns NULL when out of memory.
25170 */
25171 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025172autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025173{
25174 char_u *p;
25175 char_u *scriptname;
25176
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025177 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025178 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25179 if (scriptname == NULL)
25180 return FALSE;
25181 STRCPY(scriptname, "autoload/");
25182 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025183 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025184 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025185 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025186 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025187 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025188}
25189
Bram Moolenaar071d4272004-06-13 20:20:40 +000025190#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25191
25192/*
25193 * Function given to ExpandGeneric() to obtain the list of user defined
25194 * function names.
25195 */
25196 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025197get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025198{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025199 static long_u done;
25200 static hashitem_T *hi;
25201 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202
25203 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025205 done = 0;
25206 hi = func_hashtab.ht_array;
25207 }
25208 if (done < func_hashtab.ht_used)
25209 {
25210 if (done++ > 0)
25211 ++hi;
25212 while (HASHITEM_EMPTY(hi))
25213 ++hi;
25214 fp = HI2UF(hi);
25215
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025216 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025217 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025219 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25220 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025221
25222 cat_func_name(IObuff, fp);
25223 if (xp->xp_context != EXPAND_USER_FUNC)
25224 {
25225 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025226 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025227 STRCAT(IObuff, ")");
25228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025229 return IObuff;
25230 }
25231 return NULL;
25232}
25233
25234#endif /* FEAT_CMDL_COMPL */
25235
25236/*
25237 * Copy the function name of "fp" to buffer "buf".
25238 * "buf" must be able to hold the function name plus three bytes.
25239 * Takes care of script-local function names.
25240 */
25241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025242cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025244 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025245 {
25246 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025247 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248 }
25249 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025250 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025251}
25252
25253/*
25254 * ":delfunction {name}"
25255 */
25256 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025257ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025259 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025260 char_u *p;
25261 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025262 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025263
25264 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025265 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025266 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025267 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025268 {
25269 if (fudi.fd_dict != NULL && !eap->skip)
25270 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025271 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 if (!ends_excmd(*skipwhite(p)))
25274 {
25275 vim_free(name);
25276 EMSG(_(e_trailing));
25277 return;
25278 }
25279 eap->nextcmd = check_nextcmd(p);
25280 if (eap->nextcmd != NULL)
25281 *p = NUL;
25282
25283 if (!eap->skip)
25284 fp = find_func(name);
25285 vim_free(name);
25286
25287 if (!eap->skip)
25288 {
25289 if (fp == NULL)
25290 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025291 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 return;
25293 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025294 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295 {
25296 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25297 return;
25298 }
25299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025300 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025301 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025302 /* Delete the dict item that refers to the function, it will
25303 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025304 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025305 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025306 else
25307 func_free(fp);
25308 }
25309}
25310
25311/*
25312 * Free a function and remove it from the list of functions.
25313 */
25314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025315func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025316{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025317 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025318
25319 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025320 ga_clear_strings(&(fp->uf_args));
25321 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025322#ifdef FEAT_PROFILE
25323 vim_free(fp->uf_tml_count);
25324 vim_free(fp->uf_tml_total);
25325 vim_free(fp->uf_tml_self);
25326#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025327
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025328 /* remove the function from the function hashtable */
25329 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25330 if (HASHITEM_EMPTY(hi))
25331 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025332 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025333 hash_remove(&func_hashtab, hi);
25334
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025335 vim_free(fp);
25336}
25337
25338/*
25339 * Unreference a Function: decrement the reference count and free it when it
25340 * becomes zero. Only for numbered functions.
25341 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025343func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025344{
25345 ufunc_T *fp;
25346
25347 if (name != NULL && isdigit(*name))
25348 {
25349 fp = find_func(name);
25350 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025351 {
25352 /* Ignore when invoked through free_all_mem(). */
25353 if (!really_exiting)
25354 EMSG2(_(e_intern2), "func_unref()");
25355 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025356 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025357 {
25358 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025359 * when "uf_calls" becomes zero. */
25360 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025361 func_free(fp);
25362 }
25363 }
25364}
25365
25366/*
25367 * Count a reference to a Function.
25368 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025370func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025371{
25372 ufunc_T *fp;
25373
25374 if (name != NULL && isdigit(*name))
25375 {
25376 fp = find_func(name);
25377 if (fp == NULL)
25378 EMSG2(_(e_intern2), "func_ref()");
25379 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025380 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025381 }
25382}
25383
25384/*
25385 * Call a user function.
25386 */
25387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025388call_user_func(
25389 ufunc_T *fp, /* pointer to function */
25390 int argcount, /* nr of args */
25391 typval_T *argvars, /* arguments */
25392 typval_T *rettv, /* return value */
25393 linenr_T firstline, /* first line of range */
25394 linenr_T lastline, /* last line of range */
25395 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025396{
Bram Moolenaar33570922005-01-25 22:26:29 +000025397 char_u *save_sourcing_name;
25398 linenr_T save_sourcing_lnum;
25399 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025400 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025401 int save_did_emsg;
25402 static int depth = 0;
25403 dictitem_T *v;
25404 int fixvar_idx = 0; /* index in fixvar[] */
25405 int i;
25406 int ai;
25407 char_u numbuf[NUMBUFLEN];
25408 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025409 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025410#ifdef FEAT_PROFILE
25411 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025412 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414
25415 /* If depth of calling is getting too high, don't execute the function */
25416 if (depth >= p_mfd)
25417 {
25418 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025419 rettv->v_type = VAR_NUMBER;
25420 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421 return;
25422 }
25423 ++depth;
25424
25425 line_breakcheck(); /* check for CTRL-C hit */
25426
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025427 fc = (funccall_T *)alloc(sizeof(funccall_T));
25428 fc->caller = current_funccal;
25429 current_funccal = fc;
25430 fc->func = fp;
25431 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025432 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025433 fc->linenr = 0;
25434 fc->returned = FALSE;
25435 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025436 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025437 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25438 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025439
Bram Moolenaar33570922005-01-25 22:26:29 +000025440 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025441 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025442 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25443 * each argument variable and saves a lot of time.
25444 */
25445 /*
25446 * Init l: variables.
25447 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025448 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025449 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025450 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025451 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25452 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025453 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025454 name = v->di_key;
25455 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025456 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025457 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025458 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025459 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025460 v->di_tv.vval.v_dict = selfdict;
25461 ++selfdict->dv_refcount;
25462 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025463
Bram Moolenaar33570922005-01-25 22:26:29 +000025464 /*
25465 * Init a: variables.
25466 * Set a:0 to "argcount".
25467 * Set a:000 to a list with room for the "..." arguments.
25468 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025469 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025470 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025471 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025472 /* Use "name" to avoid a warning from some compiler that checks the
25473 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025474 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025475 name = v->di_key;
25476 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025477 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025478 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025479 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025480 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025481 v->di_tv.vval.v_list = &fc->l_varlist;
25482 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25483 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25484 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025485
25486 /*
25487 * Set a:firstline to "firstline" and a:lastline to "lastline".
25488 * Set a:name to named arguments.
25489 * Set a:N to the "..." arguments.
25490 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025491 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025492 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025493 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025494 (varnumber_T)lastline);
25495 for (i = 0; i < argcount; ++i)
25496 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025497 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025498 if (ai < 0)
25499 /* named argument a:name */
25500 name = FUNCARG(fp, i);
25501 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025502 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025503 /* "..." argument a:1, a:2, etc. */
25504 sprintf((char *)numbuf, "%d", ai + 1);
25505 name = numbuf;
25506 }
25507 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25508 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025509 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025510 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25511 }
25512 else
25513 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025514 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25515 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025516 if (v == NULL)
25517 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025518 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025519 }
25520 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025521 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025522
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025523 /* Note: the values are copied directly to avoid alloc/free.
25524 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025525 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025526 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025527
25528 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25529 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025530 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25531 fc->l_listitems[ai].li_tv = argvars[i];
25532 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025533 }
25534 }
25535
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536 /* Don't redraw while executing the function. */
25537 ++RedrawingDisabled;
25538 save_sourcing_name = sourcing_name;
25539 save_sourcing_lnum = sourcing_lnum;
25540 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025541 /* need space for function name + ("function " + 3) or "[number]" */
25542 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25543 + STRLEN(fp->uf_name) + 20;
25544 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545 if (sourcing_name != NULL)
25546 {
25547 if (save_sourcing_name != NULL
25548 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025549 sprintf((char *)sourcing_name, "%s[%d]..",
25550 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551 else
25552 STRCPY(sourcing_name, "function ");
25553 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25554
25555 if (p_verbose >= 12)
25556 {
25557 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025558 verbose_enter_scroll();
25559
Bram Moolenaar555b2802005-05-19 21:08:39 +000025560 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561 if (p_verbose >= 14)
25562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025564 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025565 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025566 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567
25568 msg_puts((char_u *)"(");
25569 for (i = 0; i < argcount; ++i)
25570 {
25571 if (i > 0)
25572 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025573 if (argvars[i].v_type == VAR_NUMBER)
25574 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025575 else
25576 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025577 /* Do not want errors such as E724 here. */
25578 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025579 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025580 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025581 if (s != NULL)
25582 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025583 if (vim_strsize(s) > MSG_BUF_CLEN)
25584 {
25585 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25586 s = buf;
25587 }
25588 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025589 vim_free(tofree);
25590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591 }
25592 }
25593 msg_puts((char_u *)")");
25594 }
25595 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025596
25597 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598 --no_wait_return;
25599 }
25600 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025601#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025602 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025603 {
25604 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25605 func_do_profile(fp);
25606 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025607 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025608 {
25609 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025610 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025611 profile_zero(&fp->uf_tm_children);
25612 }
25613 script_prof_save(&wait_start);
25614 }
25615#endif
25616
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025618 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619 save_did_emsg = did_emsg;
25620 did_emsg = FALSE;
25621
25622 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025623 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25625
25626 --RedrawingDisabled;
25627
25628 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025629 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025631 clear_tv(rettv);
25632 rettv->v_type = VAR_NUMBER;
25633 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025634 }
25635
Bram Moolenaar05159a02005-02-26 23:04:13 +000025636#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025637 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025638 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025639 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025640 profile_end(&call_start);
25641 profile_sub_wait(&wait_start, &call_start);
25642 profile_add(&fp->uf_tm_total, &call_start);
25643 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025644 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025645 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025646 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25647 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025648 }
25649 }
25650#endif
25651
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 /* when being verbose, mention the return value */
25653 if (p_verbose >= 12)
25654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025656 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025659 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025660 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025661 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025662 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025665 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025666 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025667 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025668 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025669
Bram Moolenaar555b2802005-05-19 21:08:39 +000025670 /* The value may be very long. Skip the middle part, so that we
25671 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025672 * truncate it at the end. Don't want errors such as E724 here. */
25673 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025674 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025675 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025676 if (s != NULL)
25677 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025678 if (vim_strsize(s) > MSG_BUF_CLEN)
25679 {
25680 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25681 s = buf;
25682 }
25683 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025684 vim_free(tofree);
25685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686 }
25687 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025688
25689 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025690 --no_wait_return;
25691 }
25692
25693 vim_free(sourcing_name);
25694 sourcing_name = save_sourcing_name;
25695 sourcing_lnum = save_sourcing_lnum;
25696 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025697#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025698 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025699 script_prof_restore(&wait_start);
25700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701
25702 if (p_verbose >= 12 && sourcing_name != NULL)
25703 {
25704 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025705 verbose_enter_scroll();
25706
Bram Moolenaar555b2802005-05-19 21:08:39 +000025707 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025708 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025709
25710 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025711 --no_wait_return;
25712 }
25713
25714 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025715 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025717
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025718 /* If the a:000 list and the l: and a: dicts are not referenced we can
25719 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025720 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25721 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25722 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25723 {
25724 free_funccal(fc, FALSE);
25725 }
25726 else
25727 {
25728 hashitem_T *hi;
25729 listitem_T *li;
25730 int todo;
25731
25732 /* "fc" is still in use. This can happen when returning "a:000" or
25733 * assigning "l:" to a global variable.
25734 * Link "fc" in the list for garbage collection later. */
25735 fc->caller = previous_funccal;
25736 previous_funccal = fc;
25737
25738 /* Make a copy of the a: variables, since we didn't do that above. */
25739 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25740 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25741 {
25742 if (!HASHITEM_EMPTY(hi))
25743 {
25744 --todo;
25745 v = HI2DI(hi);
25746 copy_tv(&v->di_tv, &v->di_tv);
25747 }
25748 }
25749
25750 /* Make a copy of the a:000 items, since we didn't do that above. */
25751 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25752 copy_tv(&li->li_tv, &li->li_tv);
25753 }
25754}
25755
25756/*
25757 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025758 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025759 */
25760 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025761can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025762{
25763 return (fc->l_varlist.lv_copyID != copyID
25764 && fc->l_vars.dv_copyID != copyID
25765 && fc->l_avars.dv_copyID != copyID);
25766}
25767
25768/*
25769 * Free "fc" and what it contains.
25770 */
25771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025772free_funccal(
25773 funccall_T *fc,
25774 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025775{
25776 listitem_T *li;
25777
25778 /* The a: variables typevals may not have been allocated, only free the
25779 * allocated variables. */
25780 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25781
25782 /* free all l: variables */
25783 vars_clear(&fc->l_vars.dv_hashtab);
25784
25785 /* Free the a:000 variables if they were allocated. */
25786 if (free_val)
25787 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25788 clear_tv(&li->li_tv);
25789
25790 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791}
25792
25793/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025794 * Add a number variable "name" to dict "dp" with value "nr".
25795 */
25796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025797add_nr_var(
25798 dict_T *dp,
25799 dictitem_T *v,
25800 char *name,
25801 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025802{
25803 STRCPY(v->di_key, name);
25804 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25805 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25806 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025807 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025808 v->di_tv.vval.v_number = nr;
25809}
25810
25811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812 * ":return [expr]"
25813 */
25814 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025815ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816{
25817 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025818 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025819 int returning = FALSE;
25820
25821 if (current_funccal == NULL)
25822 {
25823 EMSG(_("E133: :return not inside a function"));
25824 return;
25825 }
25826
25827 if (eap->skip)
25828 ++emsg_skip;
25829
25830 eap->nextcmd = NULL;
25831 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025832 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025833 {
25834 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025835 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025837 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 }
25839 /* It's safer to return also on error. */
25840 else if (!eap->skip)
25841 {
25842 /*
25843 * Return unless the expression evaluation has been cancelled due to an
25844 * aborting error, an interrupt, or an exception.
25845 */
25846 if (!aborting())
25847 returning = do_return(eap, FALSE, TRUE, NULL);
25848 }
25849
25850 /* When skipping or the return gets pending, advance to the next command
25851 * in this line (!returning). Otherwise, ignore the rest of the line.
25852 * Following lines will be ignored by get_func_line(). */
25853 if (returning)
25854 eap->nextcmd = NULL;
25855 else if (eap->nextcmd == NULL) /* no argument */
25856 eap->nextcmd = check_nextcmd(arg);
25857
25858 if (eap->skip)
25859 --emsg_skip;
25860}
25861
25862/*
25863 * Return from a function. Possibly makes the return pending. Also called
25864 * for a pending return at the ":endtry" or after returning from an extra
25865 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025866 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025867 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868 * FALSE when the return gets pending.
25869 */
25870 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025871do_return(
25872 exarg_T *eap,
25873 int reanimate,
25874 int is_cmd,
25875 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025876{
25877 int idx;
25878 struct condstack *cstack = eap->cstack;
25879
25880 if (reanimate)
25881 /* Undo the return. */
25882 current_funccal->returned = FALSE;
25883
25884 /*
25885 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25886 * not in its finally clause (which then is to be executed next) is found.
25887 * In this case, make the ":return" pending for execution at the ":endtry".
25888 * Otherwise, return normally.
25889 */
25890 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25891 if (idx >= 0)
25892 {
25893 cstack->cs_pending[idx] = CSTP_RETURN;
25894
25895 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025896 /* A pending return again gets pending. "rettv" points to an
25897 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025898 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025899 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900 else
25901 {
25902 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025903 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025905 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025906
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025907 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025908 {
25909 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025910 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025911 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025912 else
25913 EMSG(_(e_outofmem));
25914 }
25915 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025916 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025917
25918 if (reanimate)
25919 {
25920 /* The pending return value could be overwritten by a ":return"
25921 * without argument in a finally clause; reset the default
25922 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025923 current_funccal->rettv->v_type = VAR_NUMBER;
25924 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025925 }
25926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025927 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025928 }
25929 else
25930 {
25931 current_funccal->returned = TRUE;
25932
25933 /* If the return is carried out now, store the return value. For
25934 * a return immediately after reanimation, the value is already
25935 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025936 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025938 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025939 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025940 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025941 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942 }
25943 }
25944
25945 return idx < 0;
25946}
25947
25948/*
25949 * Free the variable with a pending return value.
25950 */
25951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025952discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953{
Bram Moolenaar33570922005-01-25 22:26:29 +000025954 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025955}
25956
25957/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025958 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 * is an allocated string. Used by report_pending() for verbose messages.
25960 */
25961 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025962get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025963{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025964 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025965 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025966 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025967
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025968 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025969 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025970 if (s == NULL)
25971 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025972
25973 STRCPY(IObuff, ":return ");
25974 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25975 if (STRLEN(s) + 8 >= IOSIZE)
25976 STRCPY(IObuff + IOSIZE - 4, "...");
25977 vim_free(tofree);
25978 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025979}
25980
25981/*
25982 * Get next function line.
25983 * Called by do_cmdline() to get the next line.
25984 * Returns allocated string, or NULL for end of function.
25985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025986 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025987get_func_line(
25988 int c UNUSED,
25989 void *cookie,
25990 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025991{
Bram Moolenaar33570922005-01-25 22:26:29 +000025992 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025993 ufunc_T *fp = fcp->func;
25994 char_u *retval;
25995 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996
25997 /* If breakpoints have been added/deleted need to check for it. */
25998 if (fcp->dbg_tick != debug_tick)
25999 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026000 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026001 sourcing_lnum);
26002 fcp->dbg_tick = debug_tick;
26003 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026004#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026005 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026006 func_line_end(cookie);
26007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026008
Bram Moolenaar05159a02005-02-26 23:04:13 +000026009 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026010 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26011 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026012 retval = NULL;
26013 else
26014 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026015 /* Skip NULL lines (continuation lines). */
26016 while (fcp->linenr < gap->ga_len
26017 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26018 ++fcp->linenr;
26019 if (fcp->linenr >= gap->ga_len)
26020 retval = NULL;
26021 else
26022 {
26023 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26024 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026025#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026026 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026027 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026028#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026030 }
26031
26032 /* Did we encounter a breakpoint? */
26033 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26034 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026035 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026036 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026037 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026038 sourcing_lnum);
26039 fcp->dbg_tick = debug_tick;
26040 }
26041
26042 return retval;
26043}
26044
Bram Moolenaar05159a02005-02-26 23:04:13 +000026045#if defined(FEAT_PROFILE) || defined(PROTO)
26046/*
26047 * Called when starting to read a function line.
26048 * "sourcing_lnum" must be correct!
26049 * When skipping lines it may not actually be executed, but we won't find out
26050 * until later and we need to store the time now.
26051 */
26052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026053func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026054{
26055 funccall_T *fcp = (funccall_T *)cookie;
26056 ufunc_T *fp = fcp->func;
26057
26058 if (fp->uf_profiling && sourcing_lnum >= 1
26059 && sourcing_lnum <= fp->uf_lines.ga_len)
26060 {
26061 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026062 /* Skip continuation lines. */
26063 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26064 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026065 fp->uf_tml_execed = FALSE;
26066 profile_start(&fp->uf_tml_start);
26067 profile_zero(&fp->uf_tml_children);
26068 profile_get_wait(&fp->uf_tml_wait);
26069 }
26070}
26071
26072/*
26073 * Called when actually executing a function line.
26074 */
26075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026076func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026077{
26078 funccall_T *fcp = (funccall_T *)cookie;
26079 ufunc_T *fp = fcp->func;
26080
26081 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26082 fp->uf_tml_execed = TRUE;
26083}
26084
26085/*
26086 * Called when done with a function line.
26087 */
26088 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026089func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026090{
26091 funccall_T *fcp = (funccall_T *)cookie;
26092 ufunc_T *fp = fcp->func;
26093
26094 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26095 {
26096 if (fp->uf_tml_execed)
26097 {
26098 ++fp->uf_tml_count[fp->uf_tml_idx];
26099 profile_end(&fp->uf_tml_start);
26100 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026101 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026102 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26103 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026104 }
26105 fp->uf_tml_idx = -1;
26106 }
26107}
26108#endif
26109
Bram Moolenaar071d4272004-06-13 20:20:40 +000026110/*
26111 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026112 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026113 */
26114 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026115func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026116{
Bram Moolenaar33570922005-01-25 22:26:29 +000026117 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026118
26119 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26120 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026121 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026122 || fcp->returned);
26123}
26124
26125/*
26126 * return TRUE if cookie indicates a function which "abort"s on errors.
26127 */
26128 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026129func_has_abort(
26130 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026132 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026133}
26134
26135#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26136typedef enum
26137{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026138 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26139 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26140 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141} var_flavour_T;
26142
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026143static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144
26145 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026146var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026147{
26148 char_u *p = varname;
26149
26150 if (ASCII_ISUPPER(*p))
26151 {
26152 while (*(++p))
26153 if (ASCII_ISLOWER(*p))
26154 return VAR_FLAVOUR_SESSION;
26155 return VAR_FLAVOUR_VIMINFO;
26156 }
26157 else
26158 return VAR_FLAVOUR_DEFAULT;
26159}
26160#endif
26161
26162#if defined(FEAT_VIMINFO) || defined(PROTO)
26163/*
26164 * Restore global vars that start with a capital from the viminfo file
26165 */
26166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026167read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026168{
26169 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026170 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026171 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026172 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026173
26174 if (!writing && (find_viminfo_parameter('!') != NULL))
26175 {
26176 tab = vim_strchr(virp->vir_line + 1, '\t');
26177 if (tab != NULL)
26178 {
26179 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026180 switch (*tab)
26181 {
26182 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026183#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026184 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026185#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026186 case 'D': type = VAR_DICT; break;
26187 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026188 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190
26191 tab = vim_strchr(tab, '\t');
26192 if (tab != NULL)
26193 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026194 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026195 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026196 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026197 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026198#ifdef FEAT_FLOAT
26199 else if (type == VAR_FLOAT)
26200 (void)string2float(tab + 1, &tv.vval.v_float);
26201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026202 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026203 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026204 if (type == VAR_DICT || type == VAR_LIST)
26205 {
26206 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26207
26208 if (etv == NULL)
26209 /* Failed to parse back the dict or list, use it as a
26210 * string. */
26211 tv.v_type = VAR_STRING;
26212 else
26213 {
26214 vim_free(tv.vval.v_string);
26215 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026216 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026217 }
26218 }
26219
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026220 /* when in a function use global variables */
26221 save_funccal = current_funccal;
26222 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026223 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026224 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026225
26226 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026227 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026228 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26229 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026230 }
26231 }
26232 }
26233
26234 return viminfo_readline(virp);
26235}
26236
26237/*
26238 * Write global vars that start with a capital to the viminfo file
26239 */
26240 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026241write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026242{
Bram Moolenaar33570922005-01-25 22:26:29 +000026243 hashitem_T *hi;
26244 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026245 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026246 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026247 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026248 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026249 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026250
26251 if (find_viminfo_parameter('!') == NULL)
26252 return;
26253
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026254 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026255
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026256 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026257 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026258 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026259 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026260 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026261 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026262 this_var = HI2DI(hi);
26263 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026264 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026265 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026266 {
26267 case VAR_STRING: s = "STR"; break;
26268 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026269 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026270 case VAR_DICT: s = "DIC"; break;
26271 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026272 case VAR_SPECIAL: s = "XPL"; break;
26273
26274 case VAR_UNKNOWN:
26275 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026276 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026277 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026278 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026279 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026280 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026281 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026282 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026283 if (p != NULL)
26284 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026285 vim_free(tofree);
26286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026287 }
26288 }
26289}
26290#endif
26291
26292#if defined(FEAT_SESSION) || defined(PROTO)
26293 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026294store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295{
Bram Moolenaar33570922005-01-25 22:26:29 +000026296 hashitem_T *hi;
26297 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026298 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026299 char_u *p, *t;
26300
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026301 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026302 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026303 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026304 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026305 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026306 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026307 this_var = HI2DI(hi);
26308 if ((this_var->di_tv.v_type == VAR_NUMBER
26309 || this_var->di_tv.v_type == VAR_STRING)
26310 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026311 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026312 /* Escape special characters with a backslash. Turn a LF and
26313 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026314 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026315 (char_u *)"\\\"\n\r");
26316 if (p == NULL) /* out of memory */
26317 break;
26318 for (t = p; *t != NUL; ++t)
26319 if (*t == '\n')
26320 *t = 'n';
26321 else if (*t == '\r')
26322 *t = 'r';
26323 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026324 this_var->di_key,
26325 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26326 : ' ',
26327 p,
26328 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26329 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026330 || put_eol(fd) == FAIL)
26331 {
26332 vim_free(p);
26333 return FAIL;
26334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026335 vim_free(p);
26336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026337#ifdef FEAT_FLOAT
26338 else if (this_var->di_tv.v_type == VAR_FLOAT
26339 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26340 {
26341 float_T f = this_var->di_tv.vval.v_float;
26342 int sign = ' ';
26343
26344 if (f < 0)
26345 {
26346 f = -f;
26347 sign = '-';
26348 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026349 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026350 this_var->di_key, sign, f) < 0)
26351 || put_eol(fd) == FAIL)
26352 return FAIL;
26353 }
26354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026355 }
26356 }
26357 return OK;
26358}
26359#endif
26360
Bram Moolenaar661b1822005-07-28 22:36:45 +000026361/*
26362 * Display script name where an item was last set.
26363 * Should only be invoked when 'verbose' is non-zero.
26364 */
26365 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026366last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026367{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026368 char_u *p;
26369
Bram Moolenaar661b1822005-07-28 22:36:45 +000026370 if (scriptID != 0)
26371 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026372 p = home_replace_save(NULL, get_scriptname(scriptID));
26373 if (p != NULL)
26374 {
26375 verbose_enter();
26376 MSG_PUTS(_("\n\tLast set from "));
26377 MSG_PUTS(p);
26378 vim_free(p);
26379 verbose_leave();
26380 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026381 }
26382}
26383
Bram Moolenaard812df62008-11-09 12:46:09 +000026384/*
26385 * List v:oldfiles in a nice way.
26386 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026387 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026388ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026389{
26390 list_T *l = vimvars[VV_OLDFILES].vv_list;
26391 listitem_T *li;
26392 int nr = 0;
26393
26394 if (l == NULL)
26395 msg((char_u *)_("No old files"));
26396 else
26397 {
26398 msg_start();
26399 msg_scroll = TRUE;
26400 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26401 {
26402 msg_outnum((long)++nr);
26403 MSG_PUTS(": ");
26404 msg_outtrans(get_tv_string(&li->li_tv));
26405 msg_putchar('\n');
26406 out_flush(); /* output one line at a time */
26407 ui_breakcheck();
26408 }
26409 /* Assume "got_int" was set to truncate the listing. */
26410 got_int = FALSE;
26411
26412#ifdef FEAT_BROWSE_CMD
26413 if (cmdmod.browse)
26414 {
26415 quit_more = FALSE;
26416 nr = prompt_for_number(FALSE);
26417 msg_starthere();
26418 if (nr > 0)
26419 {
26420 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26421 (long)nr);
26422
26423 if (p != NULL)
26424 {
26425 p = expand_env_save(p);
26426 eap->arg = p;
26427 eap->cmdidx = CMD_edit;
26428 cmdmod.browse = FALSE;
26429 do_exedit(eap, NULL);
26430 vim_free(p);
26431 }
26432 }
26433 }
26434#endif
26435 }
26436}
26437
Bram Moolenaar53744302015-07-17 17:38:22 +020026438/* reset v:option_new, v:option_old and v:option_type */
26439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026440reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026441{
26442 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26443 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26444 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26445}
26446
26447
Bram Moolenaar071d4272004-06-13 20:20:40 +000026448#endif /* FEAT_EVAL */
26449
Bram Moolenaar071d4272004-06-13 20:20:40 +000026450
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026451#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452
26453#ifdef WIN3264
26454/*
26455 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26456 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026457static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26458static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26459static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460
26461/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026462 * Get the short path (8.3) for the filename in "fnamep".
26463 * Only works for a valid file name.
26464 * When the path gets longer "fnamep" is changed and the allocated buffer
26465 * is put in "bufp".
26466 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26467 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026468 */
26469 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026470get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026471{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026472 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026473 char_u *newbuf;
26474
26475 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026476 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026477 if (l > len - 1)
26478 {
26479 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026480 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026481 newbuf = vim_strnsave(*fnamep, l);
26482 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026483 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026484
26485 vim_free(*bufp);
26486 *fnamep = *bufp = newbuf;
26487
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026488 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026489 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026490 }
26491
26492 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026493 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026494}
26495
26496/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026497 * Get the short path (8.3) for the filename in "fname". The converted
26498 * path is returned in "bufp".
26499 *
26500 * Some of the directories specified in "fname" may not exist. This function
26501 * will shorten the existing directories at the beginning of the path and then
26502 * append the remaining non-existing path.
26503 *
26504 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026505 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026506 * bufp - Pointer to an allocated buffer for the filename.
26507 * fnamelen - Length of the filename pointed to by fname
26508 *
26509 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026510 */
26511 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026512shortpath_for_invalid_fname(
26513 char_u **fname,
26514 char_u **bufp,
26515 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026516{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026517 char_u *short_fname, *save_fname, *pbuf_unused;
26518 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026519 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026520 int old_len, len;
26521 int new_len, sfx_len;
26522 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026523
26524 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026525 old_len = *fnamelen;
26526 save_fname = vim_strnsave(*fname, old_len);
26527 pbuf_unused = NULL;
26528 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026529
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026530 endp = save_fname + old_len - 1; /* Find the end of the copy */
26531 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026533 /*
26534 * Try shortening the supplied path till it succeeds by removing one
26535 * directory at a time from the tail of the path.
26536 */
26537 len = 0;
26538 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026539 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026540 /* go back one path-separator */
26541 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26542 --endp;
26543 if (endp <= save_fname)
26544 break; /* processed the complete path */
26545
26546 /*
26547 * Replace the path separator with a NUL and try to shorten the
26548 * resulting path.
26549 */
26550 ch = *endp;
26551 *endp = 0;
26552 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026553 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026554 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26555 {
26556 retval = FAIL;
26557 goto theend;
26558 }
26559 *endp = ch; /* preserve the string */
26560
26561 if (len > 0)
26562 break; /* successfully shortened the path */
26563
26564 /* failed to shorten the path. Skip the path separator */
26565 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026566 }
26567
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026568 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026569 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026570 /*
26571 * Succeeded in shortening the path. Now concatenate the shortened
26572 * path with the remaining path at the tail.
26573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026574
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026575 /* Compute the length of the new path. */
26576 sfx_len = (int)(save_endp - endp) + 1;
26577 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026578
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026579 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026580 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026581 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026582 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026583 /* There is not enough space in the currently allocated string,
26584 * copy it to a buffer big enough. */
26585 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026586 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026587 {
26588 retval = FAIL;
26589 goto theend;
26590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026591 }
26592 else
26593 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026594 /* Transfer short_fname to the main buffer (it's big enough),
26595 * unless get_short_pathname() did its work in-place. */
26596 *fname = *bufp = save_fname;
26597 if (short_fname != save_fname)
26598 vim_strncpy(save_fname, short_fname, len);
26599 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026600 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026601
26602 /* concat the not-shortened part of the path */
26603 vim_strncpy(*fname + len, endp, sfx_len);
26604 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026605 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026606
26607theend:
26608 vim_free(pbuf_unused);
26609 vim_free(save_fname);
26610
26611 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026612}
26613
26614/*
26615 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026616 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026617 */
26618 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026619shortpath_for_partial(
26620 char_u **fnamep,
26621 char_u **bufp,
26622 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026623{
26624 int sepcount, len, tflen;
26625 char_u *p;
26626 char_u *pbuf, *tfname;
26627 int hasTilde;
26628
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026629 /* Count up the path separators from the RHS.. so we know which part
26630 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026631 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026632 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026633 if (vim_ispathsep(*p))
26634 ++sepcount;
26635
26636 /* Need full path first (use expand_env() to remove a "~/") */
26637 hasTilde = (**fnamep == '~');
26638 if (hasTilde)
26639 pbuf = tfname = expand_env_save(*fnamep);
26640 else
26641 pbuf = tfname = FullName_save(*fnamep, FALSE);
26642
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026643 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026644
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026645 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26646 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026647
26648 if (len == 0)
26649 {
26650 /* Don't have a valid filename, so shorten the rest of the
26651 * path if we can. This CAN give us invalid 8.3 filenames, but
26652 * there's not a lot of point in guessing what it might be.
26653 */
26654 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026655 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26656 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026657 }
26658
26659 /* Count the paths backward to find the beginning of the desired string. */
26660 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026661 {
26662#ifdef FEAT_MBYTE
26663 if (has_mbyte)
26664 p -= mb_head_off(tfname, p);
26665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026666 if (vim_ispathsep(*p))
26667 {
26668 if (sepcount == 0 || (hasTilde && sepcount == 1))
26669 break;
26670 else
26671 sepcount --;
26672 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026674 if (hasTilde)
26675 {
26676 --p;
26677 if (p >= tfname)
26678 *p = '~';
26679 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026681 }
26682 else
26683 ++p;
26684
26685 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26686 vim_free(*bufp);
26687 *fnamelen = (int)STRLEN(p);
26688 *bufp = pbuf;
26689 *fnamep = p;
26690
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026691 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026692}
26693#endif /* WIN3264 */
26694
26695/*
26696 * Adjust a filename, according to a string of modifiers.
26697 * *fnamep must be NUL terminated when called. When returning, the length is
26698 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026699 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026700 * When there is an error, *fnamep is set to NULL.
26701 */
26702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026703modify_fname(
26704 char_u *src, /* string with modifiers */
26705 int *usedlen, /* characters after src that are used */
26706 char_u **fnamep, /* file name so far */
26707 char_u **bufp, /* buffer for allocated file name or NULL */
26708 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026709{
26710 int valid = 0;
26711 char_u *tail;
26712 char_u *s, *p, *pbuf;
26713 char_u dirname[MAXPATHL];
26714 int c;
26715 int has_fullname = 0;
26716#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026717 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026718 int has_shortname = 0;
26719#endif
26720
26721repeat:
26722 /* ":p" - full path/file_name */
26723 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26724 {
26725 has_fullname = 1;
26726
26727 valid |= VALID_PATH;
26728 *usedlen += 2;
26729
26730 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26731 if ((*fnamep)[0] == '~'
26732#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26733 && ((*fnamep)[1] == '/'
26734# ifdef BACKSLASH_IN_FILENAME
26735 || (*fnamep)[1] == '\\'
26736# endif
26737 || (*fnamep)[1] == NUL)
26738
26739#endif
26740 )
26741 {
26742 *fnamep = expand_env_save(*fnamep);
26743 vim_free(*bufp); /* free any allocated file name */
26744 *bufp = *fnamep;
26745 if (*fnamep == NULL)
26746 return -1;
26747 }
26748
26749 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026750 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026751 {
26752 if (vim_ispathsep(*p)
26753 && p[1] == '.'
26754 && (p[2] == NUL
26755 || vim_ispathsep(p[2])
26756 || (p[2] == '.'
26757 && (p[3] == NUL || vim_ispathsep(p[3])))))
26758 break;
26759 }
26760
26761 /* FullName_save() is slow, don't use it when not needed. */
26762 if (*p != NUL || !vim_isAbsName(*fnamep))
26763 {
26764 *fnamep = FullName_save(*fnamep, *p != NUL);
26765 vim_free(*bufp); /* free any allocated file name */
26766 *bufp = *fnamep;
26767 if (*fnamep == NULL)
26768 return -1;
26769 }
26770
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026771#ifdef WIN3264
26772# if _WIN32_WINNT >= 0x0500
26773 if (vim_strchr(*fnamep, '~') != NULL)
26774 {
26775 /* Expand 8.3 filename to full path. Needed to make sure the same
26776 * file does not have two different names.
26777 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26778 p = alloc(_MAX_PATH + 1);
26779 if (p != NULL)
26780 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026781 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026782 {
26783 vim_free(*bufp);
26784 *bufp = *fnamep = p;
26785 }
26786 else
26787 vim_free(p);
26788 }
26789 }
26790# endif
26791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026792 /* Append a path separator to a directory. */
26793 if (mch_isdir(*fnamep))
26794 {
26795 /* Make room for one or two extra characters. */
26796 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26797 vim_free(*bufp); /* free any allocated file name */
26798 *bufp = *fnamep;
26799 if (*fnamep == NULL)
26800 return -1;
26801 add_pathsep(*fnamep);
26802 }
26803 }
26804
26805 /* ":." - path relative to the current directory */
26806 /* ":~" - path relative to the home directory */
26807 /* ":8" - shortname path - postponed till after */
26808 while (src[*usedlen] == ':'
26809 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26810 {
26811 *usedlen += 2;
26812 if (c == '8')
26813 {
26814#ifdef WIN3264
26815 has_shortname = 1; /* Postpone this. */
26816#endif
26817 continue;
26818 }
26819 pbuf = NULL;
26820 /* Need full path first (use expand_env() to remove a "~/") */
26821 if (!has_fullname)
26822 {
26823 if (c == '.' && **fnamep == '~')
26824 p = pbuf = expand_env_save(*fnamep);
26825 else
26826 p = pbuf = FullName_save(*fnamep, FALSE);
26827 }
26828 else
26829 p = *fnamep;
26830
26831 has_fullname = 0;
26832
26833 if (p != NULL)
26834 {
26835 if (c == '.')
26836 {
26837 mch_dirname(dirname, MAXPATHL);
26838 s = shorten_fname(p, dirname);
26839 if (s != NULL)
26840 {
26841 *fnamep = s;
26842 if (pbuf != NULL)
26843 {
26844 vim_free(*bufp); /* free any allocated file name */
26845 *bufp = pbuf;
26846 pbuf = NULL;
26847 }
26848 }
26849 }
26850 else
26851 {
26852 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26853 /* Only replace it when it starts with '~' */
26854 if (*dirname == '~')
26855 {
26856 s = vim_strsave(dirname);
26857 if (s != NULL)
26858 {
26859 *fnamep = s;
26860 vim_free(*bufp);
26861 *bufp = s;
26862 }
26863 }
26864 }
26865 vim_free(pbuf);
26866 }
26867 }
26868
26869 tail = gettail(*fnamep);
26870 *fnamelen = (int)STRLEN(*fnamep);
26871
26872 /* ":h" - head, remove "/file_name", can be repeated */
26873 /* Don't remove the first "/" or "c:\" */
26874 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26875 {
26876 valid |= VALID_HEAD;
26877 *usedlen += 2;
26878 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026879 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026880 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026881 *fnamelen = (int)(tail - *fnamep);
26882#ifdef VMS
26883 if (*fnamelen > 0)
26884 *fnamelen += 1; /* the path separator is part of the path */
26885#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026886 if (*fnamelen == 0)
26887 {
26888 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26889 p = vim_strsave((char_u *)".");
26890 if (p == NULL)
26891 return -1;
26892 vim_free(*bufp);
26893 *bufp = *fnamep = tail = p;
26894 *fnamelen = 1;
26895 }
26896 else
26897 {
26898 while (tail > s && !after_pathsep(s, tail))
26899 mb_ptr_back(*fnamep, tail);
26900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026901 }
26902
26903 /* ":8" - shortname */
26904 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26905 {
26906 *usedlen += 2;
26907#ifdef WIN3264
26908 has_shortname = 1;
26909#endif
26910 }
26911
26912#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026913 /*
26914 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026915 */
26916 if (has_shortname)
26917 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026918 /* Copy the string if it is shortened by :h and when it wasn't copied
26919 * yet, because we are going to change it in place. Avoids changing
26920 * the buffer name for "%:8". */
26921 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026922 {
26923 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026924 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026925 return -1;
26926 vim_free(*bufp);
26927 *bufp = *fnamep = p;
26928 }
26929
26930 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026931 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026932 if (!has_fullname && !vim_isAbsName(*fnamep))
26933 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026934 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026935 return -1;
26936 }
26937 else
26938 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026939 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026940
Bram Moolenaardc935552011-08-17 15:23:23 +020026941 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026942 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026943 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026944 return -1;
26945
26946 if (l == 0)
26947 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026948 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026949 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026950 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026951 return -1;
26952 }
26953 *fnamelen = l;
26954 }
26955 }
26956#endif /* WIN3264 */
26957
26958 /* ":t" - tail, just the basename */
26959 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26960 {
26961 *usedlen += 2;
26962 *fnamelen -= (int)(tail - *fnamep);
26963 *fnamep = tail;
26964 }
26965
26966 /* ":e" - extension, can be repeated */
26967 /* ":r" - root, without extension, can be repeated */
26968 while (src[*usedlen] == ':'
26969 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26970 {
26971 /* find a '.' in the tail:
26972 * - for second :e: before the current fname
26973 * - otherwise: The last '.'
26974 */
26975 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26976 s = *fnamep - 2;
26977 else
26978 s = *fnamep + *fnamelen - 1;
26979 for ( ; s > tail; --s)
26980 if (s[0] == '.')
26981 break;
26982 if (src[*usedlen + 1] == 'e') /* :e */
26983 {
26984 if (s > tail)
26985 {
26986 *fnamelen += (int)(*fnamep - (s + 1));
26987 *fnamep = s + 1;
26988#ifdef VMS
26989 /* cut version from the extension */
26990 s = *fnamep + *fnamelen - 1;
26991 for ( ; s > *fnamep; --s)
26992 if (s[0] == ';')
26993 break;
26994 if (s > *fnamep)
26995 *fnamelen = s - *fnamep;
26996#endif
26997 }
26998 else if (*fnamep <= tail)
26999 *fnamelen = 0;
27000 }
27001 else /* :r */
27002 {
27003 if (s > tail) /* remove one extension */
27004 *fnamelen = (int)(s - *fnamep);
27005 }
27006 *usedlen += 2;
27007 }
27008
27009 /* ":s?pat?foo?" - substitute */
27010 /* ":gs?pat?foo?" - global substitute */
27011 if (src[*usedlen] == ':'
27012 && (src[*usedlen + 1] == 's'
27013 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27014 {
27015 char_u *str;
27016 char_u *pat;
27017 char_u *sub;
27018 int sep;
27019 char_u *flags;
27020 int didit = FALSE;
27021
27022 flags = (char_u *)"";
27023 s = src + *usedlen + 2;
27024 if (src[*usedlen + 1] == 'g')
27025 {
27026 flags = (char_u *)"g";
27027 ++s;
27028 }
27029
27030 sep = *s++;
27031 if (sep)
27032 {
27033 /* find end of pattern */
27034 p = vim_strchr(s, sep);
27035 if (p != NULL)
27036 {
27037 pat = vim_strnsave(s, (int)(p - s));
27038 if (pat != NULL)
27039 {
27040 s = p + 1;
27041 /* find end of substitution */
27042 p = vim_strchr(s, sep);
27043 if (p != NULL)
27044 {
27045 sub = vim_strnsave(s, (int)(p - s));
27046 str = vim_strnsave(*fnamep, *fnamelen);
27047 if (sub != NULL && str != NULL)
27048 {
27049 *usedlen = (int)(p + 1 - src);
27050 s = do_string_sub(str, pat, sub, flags);
27051 if (s != NULL)
27052 {
27053 *fnamep = s;
27054 *fnamelen = (int)STRLEN(s);
27055 vim_free(*bufp);
27056 *bufp = s;
27057 didit = TRUE;
27058 }
27059 }
27060 vim_free(sub);
27061 vim_free(str);
27062 }
27063 vim_free(pat);
27064 }
27065 }
27066 /* after using ":s", repeat all the modifiers */
27067 if (didit)
27068 goto repeat;
27069 }
27070 }
27071
Bram Moolenaar26df0922014-02-23 23:39:13 +010027072 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27073 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027074 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027075 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027076 if (c != NUL)
27077 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027078 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027079 if (c != NUL)
27080 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027081 if (p == NULL)
27082 return -1;
27083 vim_free(*bufp);
27084 *bufp = *fnamep = p;
27085 *fnamelen = (int)STRLEN(p);
27086 *usedlen += 2;
27087 }
27088
Bram Moolenaar071d4272004-06-13 20:20:40 +000027089 return valid;
27090}
27091
27092/*
27093 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27094 * "flags" can be "g" to do a global substitute.
27095 * Returns an allocated string, NULL for error.
27096 */
27097 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027098do_string_sub(
27099 char_u *str,
27100 char_u *pat,
27101 char_u *sub,
27102 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027103{
27104 int sublen;
27105 regmatch_T regmatch;
27106 int i;
27107 int do_all;
27108 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027109 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027110 garray_T ga;
27111 char_u *ret;
27112 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027113 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027114
27115 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27116 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027117 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027118
27119 ga_init2(&ga, 1, 200);
27120
27121 do_all = (flags[0] == 'g');
27122
27123 regmatch.rm_ic = p_ic;
27124 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27125 if (regmatch.regprog != NULL)
27126 {
27127 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027128 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027129 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27130 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027131 /* Skip empty match except for first match. */
27132 if (regmatch.startp[0] == regmatch.endp[0])
27133 {
27134 if (zero_width == regmatch.startp[0])
27135 {
27136 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027137 i = MB_PTR2LEN(tail);
27138 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27139 (size_t)i);
27140 ga.ga_len += i;
27141 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027142 continue;
27143 }
27144 zero_width = regmatch.startp[0];
27145 }
27146
Bram Moolenaar071d4272004-06-13 20:20:40 +000027147 /*
27148 * Get some space for a temporary buffer to do the substitution
27149 * into. It will contain:
27150 * - The text up to where the match is.
27151 * - The substituted text.
27152 * - The text after the match.
27153 */
27154 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027155 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027156 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27157 {
27158 ga_clear(&ga);
27159 break;
27160 }
27161
27162 /* copy the text up to where the match is */
27163 i = (int)(regmatch.startp[0] - tail);
27164 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27165 /* add the substituted text */
27166 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27167 + ga.ga_len + i, TRUE, TRUE, FALSE);
27168 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027169 tail = regmatch.endp[0];
27170 if (*tail == NUL)
27171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027172 if (!do_all)
27173 break;
27174 }
27175
27176 if (ga.ga_data != NULL)
27177 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27178
Bram Moolenaar473de612013-06-08 18:19:48 +020027179 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027180 }
27181
27182 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27183 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027184 if (p_cpo == empty_option)
27185 p_cpo = save_cpo;
27186 else
27187 /* Darn, evaluating {sub} expression changed the value. */
27188 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027189
27190 return ret;
27191}
27192
27193#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */