blob: 2a7a0906f95e63bbb1199e526b1931ae97947124 [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},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200352 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000353 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
355 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000356 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000357 {VV_NAME("swapname", VAR_STRING), VV_RO},
358 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000359 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200360 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000361 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200362 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000363 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
364 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000365 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100367 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000368 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200369 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200370 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200371 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200372 {VV_NAME("option_new", VAR_STRING), VV_RO},
373 {VV_NAME("option_old", VAR_STRING), VV_RO},
374 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100375 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100376 {VV_NAME("false", VAR_SPECIAL), VV_RO},
377 {VV_NAME("true", VAR_SPECIAL), VV_RO},
378 {VV_NAME("null", VAR_SPECIAL), VV_RO},
379 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100380 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200381 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000382};
383
384/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_type vv_di.di_tv.v_type
386#define vv_nr vv_di.di_tv.vval.v_number
387#define vv_float vv_di.di_tv.vval.v_float
388#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000389#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200390#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000391#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000392
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200393static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000394#define vimvarht vimvardict.dv_hashtab
395
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100396static void prepare_vimvar(int idx, typval_T *save_tv);
397static void restore_vimvar(int idx, typval_T *save_tv);
398static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
399static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
400static char_u *skip_var_one(char_u *arg);
401static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
402static void list_glob_vars(int *first);
403static void list_buf_vars(int *first);
404static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000407#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100408static void list_vim_vars(int *first);
409static void list_script_vars(int *first);
410static void list_func_vars(int *first);
411static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
412static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
413static int check_changedtick(char_u *arg);
414static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
415static void clear_lval(lval_T *lp);
416static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
417static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
418static void list_fix_watch(list_T *l, listitem_T *item);
419static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
420static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
421static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
422static void item_lock(typval_T *tv, int deep, int lock);
423static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000424
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100425static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
426static int eval1(char_u **arg, typval_T *rettv, int evaluate);
427static int eval2(char_u **arg, typval_T *rettv, int evaluate);
428static int eval3(char_u **arg, typval_T *rettv, int evaluate);
429static int eval4(char_u **arg, typval_T *rettv, int evaluate);
430static int eval5(char_u **arg, typval_T *rettv, int evaluate);
431static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
432static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000433
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100434static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
435static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
437static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
438static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200439static void list_free_contents(list_T *l);
440static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100441static long list_len(list_T *l);
442static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
443static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
444static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
445static long list_find_nr(list_T *l, long idx, int *errorp);
446static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100447static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
448static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
449static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200450static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
451static 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);
452static 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 +0100453static int free_unref_items(int copyID);
454static dictitem_T *dictitem_copy(dictitem_T *org);
455static void dictitem_remove(dict_T *dict, dictitem_T *item);
456static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
457static long dict_len(dict_T *d);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200458static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200460static 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 +0100461static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static char_u *string_quote(char_u *str, int function);
463static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
464static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100465static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
466static 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 +0100467static void emsg_funcname(char *ermsg, char_u *name);
468static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000469
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200470static void dict_free_contents(dict_T *d);
471static void dict_free_dict(dict_T *d);
472
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_abs(typval_T *argvars, typval_T *rettv);
475static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100477static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_and(typval_T *argvars, typval_T *rettv);
479static void f_append(typval_T *argvars, typval_T *rettv);
480static void f_argc(typval_T *argvars, typval_T *rettv);
481static void f_argidx(typval_T *argvars, typval_T *rettv);
482static void f_arglistid(typval_T *argvars, typval_T *rettv);
483static void f_argv(typval_T *argvars, typval_T *rettv);
484static void f_assert_equal(typval_T *argvars, typval_T *rettv);
485static void f_assert_exception(typval_T *argvars, typval_T *rettv);
486static void f_assert_fails(typval_T *argvars, typval_T *rettv);
487static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200488static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200489static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
490static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100493static void f_asin(typval_T *argvars, typval_T *rettv);
494static void f_atan(typval_T *argvars, typval_T *rettv);
495static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_browse(typval_T *argvars, typval_T *rettv);
498static void f_browsedir(typval_T *argvars, typval_T *rettv);
499static void f_bufexists(typval_T *argvars, typval_T *rettv);
500static void f_buflisted(typval_T *argvars, typval_T *rettv);
501static void f_bufloaded(typval_T *argvars, typval_T *rettv);
502static void f_bufname(typval_T *argvars, typval_T *rettv);
503static void f_bufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb3619a92016-06-04 17:58:52 +0200504static void f_bufwinid(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100505static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
506static void f_byte2line(typval_T *argvars, typval_T *rettv);
507static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
508static void f_byteidx(typval_T *argvars, typval_T *rettv);
509static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
510static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000513#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100516static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
517static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100518static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100519static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100520static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100521static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
523static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100524static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100525static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100526static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
527static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100528static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100529static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_changenr(typval_T *argvars, typval_T *rettv);
532static void f_char2nr(typval_T *argvars, typval_T *rettv);
533static void f_cindent(typval_T *argvars, typval_T *rettv);
534static void f_clearmatches(typval_T *argvars, typval_T *rettv);
535static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000536#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_complete(typval_T *argvars, typval_T *rettv);
538static void f_complete_add(typval_T *argvars, typval_T *rettv);
539static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000540#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_confirm(typval_T *argvars, typval_T *rettv);
542static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_cos(typval_T *argvars, typval_T *rettv);
545static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_count(typval_T *argvars, typval_T *rettv);
548static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
549static void f_cursor(typval_T *argsvars, typval_T *rettv);
550static void f_deepcopy(typval_T *argvars, typval_T *rettv);
551static void f_delete(typval_T *argvars, typval_T *rettv);
552static void f_did_filetype(typval_T *argvars, typval_T *rettv);
553static void f_diff_filler(typval_T *argvars, typval_T *rettv);
554static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
555static void f_empty(typval_T *argvars, typval_T *rettv);
556static void f_escape(typval_T *argvars, typval_T *rettv);
557static void f_eval(typval_T *argvars, typval_T *rettv);
558static void f_eventhandler(typval_T *argvars, typval_T *rettv);
559static void f_executable(typval_T *argvars, typval_T *rettv);
560static void f_exepath(typval_T *argvars, typval_T *rettv);
561static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200562#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200564#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100565static void f_expand(typval_T *argvars, typval_T *rettv);
566static void f_extend(typval_T *argvars, typval_T *rettv);
567static void f_feedkeys(typval_T *argvars, typval_T *rettv);
568static void f_filereadable(typval_T *argvars, typval_T *rettv);
569static void f_filewritable(typval_T *argvars, typval_T *rettv);
570static void f_filter(typval_T *argvars, typval_T *rettv);
571static void f_finddir(typval_T *argvars, typval_T *rettv);
572static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000573#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100574static void f_float2nr(typval_T *argvars, typval_T *rettv);
575static void f_floor(typval_T *argvars, typval_T *rettv);
576static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000577#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100578static void f_fnameescape(typval_T *argvars, typval_T *rettv);
579static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
580static void f_foldclosed(typval_T *argvars, typval_T *rettv);
581static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
582static void f_foldlevel(typval_T *argvars, typval_T *rettv);
583static void f_foldtext(typval_T *argvars, typval_T *rettv);
584static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
585static void f_foreground(typval_T *argvars, typval_T *rettv);
586static void f_function(typval_T *argvars, typval_T *rettv);
587static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
588static void f_get(typval_T *argvars, typval_T *rettv);
589static void f_getbufline(typval_T *argvars, typval_T *rettv);
590static void f_getbufvar(typval_T *argvars, typval_T *rettv);
591static void f_getchar(typval_T *argvars, typval_T *rettv);
592static void f_getcharmod(typval_T *argvars, typval_T *rettv);
593static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
594static void f_getcmdline(typval_T *argvars, typval_T *rettv);
595static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
596static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
597static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
598static void f_getcwd(typval_T *argvars, typval_T *rettv);
599static void f_getfontname(typval_T *argvars, typval_T *rettv);
600static void f_getfperm(typval_T *argvars, typval_T *rettv);
601static void f_getfsize(typval_T *argvars, typval_T *rettv);
602static void f_getftime(typval_T *argvars, typval_T *rettv);
603static void f_getftype(typval_T *argvars, typval_T *rettv);
604static void f_getline(typval_T *argvars, typval_T *rettv);
605static void f_getmatches(typval_T *argvars, typval_T *rettv);
606static void f_getpid(typval_T *argvars, typval_T *rettv);
607static void f_getcurpos(typval_T *argvars, typval_T *rettv);
608static void f_getpos(typval_T *argvars, typval_T *rettv);
609static void f_getqflist(typval_T *argvars, typval_T *rettv);
610static void f_getreg(typval_T *argvars, typval_T *rettv);
611static void f_getregtype(typval_T *argvars, typval_T *rettv);
612static void f_gettabvar(typval_T *argvars, typval_T *rettv);
613static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
614static void f_getwinposx(typval_T *argvars, typval_T *rettv);
615static void f_getwinposy(typval_T *argvars, typval_T *rettv);
616static void f_getwinvar(typval_T *argvars, typval_T *rettv);
617static void f_glob(typval_T *argvars, typval_T *rettv);
618static void f_globpath(typval_T *argvars, typval_T *rettv);
619static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
620static void f_has(typval_T *argvars, typval_T *rettv);
621static void f_has_key(typval_T *argvars, typval_T *rettv);
622static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
623static void f_hasmapto(typval_T *argvars, typval_T *rettv);
624static void f_histadd(typval_T *argvars, typval_T *rettv);
625static void f_histdel(typval_T *argvars, typval_T *rettv);
626static void f_histget(typval_T *argvars, typval_T *rettv);
627static void f_histnr(typval_T *argvars, typval_T *rettv);
628static void f_hlID(typval_T *argvars, typval_T *rettv);
629static void f_hlexists(typval_T *argvars, typval_T *rettv);
630static void f_hostname(typval_T *argvars, typval_T *rettv);
631static void f_iconv(typval_T *argvars, typval_T *rettv);
632static void f_indent(typval_T *argvars, typval_T *rettv);
633static void f_index(typval_T *argvars, typval_T *rettv);
634static void f_input(typval_T *argvars, typval_T *rettv);
635static void f_inputdialog(typval_T *argvars, typval_T *rettv);
636static void f_inputlist(typval_T *argvars, typval_T *rettv);
637static void f_inputrestore(typval_T *argvars, typval_T *rettv);
638static void f_inputsave(typval_T *argvars, typval_T *rettv);
639static void f_inputsecret(typval_T *argvars, typval_T *rettv);
640static void f_insert(typval_T *argvars, typval_T *rettv);
641static void f_invert(typval_T *argvars, typval_T *rettv);
642static void f_isdirectory(typval_T *argvars, typval_T *rettv);
643static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100644#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
645static void f_isnan(typval_T *argvars, typval_T *rettv);
646#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100647static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100648#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100649static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100650static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100651static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100652static void f_job_start(typval_T *argvars, typval_T *rettv);
653static void f_job_stop(typval_T *argvars, typval_T *rettv);
654static void f_job_status(typval_T *argvars, typval_T *rettv);
655#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100657static void f_js_decode(typval_T *argvars, typval_T *rettv);
658static void f_js_encode(typval_T *argvars, typval_T *rettv);
659static void f_json_decode(typval_T *argvars, typval_T *rettv);
660static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_keys(typval_T *argvars, typval_T *rettv);
662static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
663static void f_len(typval_T *argvars, typval_T *rettv);
664static void f_libcall(typval_T *argvars, typval_T *rettv);
665static void f_libcallnr(typval_T *argvars, typval_T *rettv);
666static void f_line(typval_T *argvars, typval_T *rettv);
667static void f_line2byte(typval_T *argvars, typval_T *rettv);
668static void f_lispindent(typval_T *argvars, typval_T *rettv);
669static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_log(typval_T *argvars, typval_T *rettv);
672static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000673#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200674#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200676#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_map(typval_T *argvars, typval_T *rettv);
678static void f_maparg(typval_T *argvars, typval_T *rettv);
679static void f_mapcheck(typval_T *argvars, typval_T *rettv);
680static void f_match(typval_T *argvars, typval_T *rettv);
681static void f_matchadd(typval_T *argvars, typval_T *rettv);
682static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
683static void f_matcharg(typval_T *argvars, typval_T *rettv);
684static void f_matchdelete(typval_T *argvars, typval_T *rettv);
685static void f_matchend(typval_T *argvars, typval_T *rettv);
686static void f_matchlist(typval_T *argvars, typval_T *rettv);
687static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200688static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_max(typval_T *argvars, typval_T *rettv);
690static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000691#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000693#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100695#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100697#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
699static void f_nr2char(typval_T *argvars, typval_T *rettv);
700static void f_or(typval_T *argvars, typval_T *rettv);
701static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100702#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100704#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100706static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000707#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
709static void f_printf(typval_T *argvars, typval_T *rettv);
710static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200711#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200713#endif
714#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100715static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200716#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100717static void f_range(typval_T *argvars, typval_T *rettv);
718static void f_readfile(typval_T *argvars, typval_T *rettv);
719static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100720#ifdef FEAT_FLOAT
721static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
722#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100723static void f_reltimestr(typval_T *argvars, typval_T *rettv);
724static void f_remote_expr(typval_T *argvars, typval_T *rettv);
725static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
726static void f_remote_peek(typval_T *argvars, typval_T *rettv);
727static void f_remote_read(typval_T *argvars, typval_T *rettv);
728static void f_remote_send(typval_T *argvars, typval_T *rettv);
729static void f_remove(typval_T *argvars, typval_T *rettv);
730static void f_rename(typval_T *argvars, typval_T *rettv);
731static void f_repeat(typval_T *argvars, typval_T *rettv);
732static void f_resolve(typval_T *argvars, typval_T *rettv);
733static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000734#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000736#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100737static void f_screenattr(typval_T *argvars, typval_T *rettv);
738static void f_screenchar(typval_T *argvars, typval_T *rettv);
739static void f_screencol(typval_T *argvars, typval_T *rettv);
740static void f_screenrow(typval_T *argvars, typval_T *rettv);
741static void f_search(typval_T *argvars, typval_T *rettv);
742static void f_searchdecl(typval_T *argvars, typval_T *rettv);
743static void f_searchpair(typval_T *argvars, typval_T *rettv);
744static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
745static void f_searchpos(typval_T *argvars, typval_T *rettv);
746static void f_server2client(typval_T *argvars, typval_T *rettv);
747static void f_serverlist(typval_T *argvars, typval_T *rettv);
748static void f_setbufvar(typval_T *argvars, typval_T *rettv);
749static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
750static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100751static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_setline(typval_T *argvars, typval_T *rettv);
753static void f_setloclist(typval_T *argvars, typval_T *rettv);
754static void f_setmatches(typval_T *argvars, typval_T *rettv);
755static void f_setpos(typval_T *argvars, typval_T *rettv);
756static void f_setqflist(typval_T *argvars, typval_T *rettv);
757static void f_setreg(typval_T *argvars, typval_T *rettv);
758static void f_settabvar(typval_T *argvars, typval_T *rettv);
759static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
760static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100761#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100763#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_shellescape(typval_T *argvars, typval_T *rettv);
765static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
766static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000767#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_sin(typval_T *argvars, typval_T *rettv);
769static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000770#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_sort(typval_T *argvars, typval_T *rettv);
772static void f_soundfold(typval_T *argvars, typval_T *rettv);
773static void f_spellbadword(typval_T *argvars, typval_T *rettv);
774static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
775static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000776#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_sqrt(typval_T *argvars, typval_T *rettv);
778static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000779#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_str2nr(typval_T *argvars, typval_T *rettv);
781static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000782#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100783static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000784#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200785static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100786static void f_stridx(typval_T *argvars, typval_T *rettv);
787static void f_string(typval_T *argvars, typval_T *rettv);
788static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200789static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100790static void f_strpart(typval_T *argvars, typval_T *rettv);
791static void f_strridx(typval_T *argvars, typval_T *rettv);
792static void f_strtrans(typval_T *argvars, typval_T *rettv);
793static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
794static void f_strwidth(typval_T *argvars, typval_T *rettv);
795static void f_submatch(typval_T *argvars, typval_T *rettv);
796static void f_substitute(typval_T *argvars, typval_T *rettv);
797static void f_synID(typval_T *argvars, typval_T *rettv);
798static void f_synIDattr(typval_T *argvars, typval_T *rettv);
799static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
800static void f_synstack(typval_T *argvars, typval_T *rettv);
801static void f_synconcealed(typval_T *argvars, typval_T *rettv);
802static void f_system(typval_T *argvars, typval_T *rettv);
803static void f_systemlist(typval_T *argvars, typval_T *rettv);
804static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
805static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
806static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
807static void f_taglist(typval_T *argvars, typval_T *rettv);
808static void f_tagfiles(typval_T *argvars, typval_T *rettv);
809static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200810static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
811static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200812static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
813#ifdef FEAT_JOB_CHANNEL
814static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
815#endif
816static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
817#ifdef FEAT_JOB_CHANNEL
818static void f_test_null_job(typval_T *argvars, typval_T *rettv);
819#endif
820static void f_test_null_list(typval_T *argvars, typval_T *rettv);
821static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
822static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200823#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100824static void f_tan(typval_T *argvars, typval_T *rettv);
825static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200826#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100827#ifdef FEAT_TIMERS
828static void f_timer_start(typval_T *argvars, typval_T *rettv);
829static void f_timer_stop(typval_T *argvars, typval_T *rettv);
830#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100831static void f_tolower(typval_T *argvars, typval_T *rettv);
832static void f_toupper(typval_T *argvars, typval_T *rettv);
833static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000834#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000836#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static void f_type(typval_T *argvars, typval_T *rettv);
838static void f_undofile(typval_T *argvars, typval_T *rettv);
839static void f_undotree(typval_T *argvars, typval_T *rettv);
840static void f_uniq(typval_T *argvars, typval_T *rettv);
841static void f_values(typval_T *argvars, typval_T *rettv);
842static void f_virtcol(typval_T *argvars, typval_T *rettv);
843static void f_visualmode(typval_T *argvars, typval_T *rettv);
844static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100845static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100846static void f_win_getid(typval_T *argvars, typval_T *rettv);
847static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
848static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
849static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100850static void f_winbufnr(typval_T *argvars, typval_T *rettv);
851static void f_wincol(typval_T *argvars, typval_T *rettv);
852static void f_winheight(typval_T *argvars, typval_T *rettv);
853static void f_winline(typval_T *argvars, typval_T *rettv);
854static void f_winnr(typval_T *argvars, typval_T *rettv);
855static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
856static void f_winrestview(typval_T *argvars, typval_T *rettv);
857static void f_winsaveview(typval_T *argvars, typval_T *rettv);
858static void f_winwidth(typval_T *argvars, typval_T *rettv);
859static void f_writefile(typval_T *argvars, typval_T *rettv);
860static void f_wordcount(typval_T *argvars, typval_T *rettv);
861static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000862
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100863static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
864static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
865static int get_env_len(char_u **arg);
866static int get_id_len(char_u **arg);
867static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
868static 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 +0000869#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
870#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
871 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100872static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
873static int eval_isnamec(int c);
874static int eval_isnamec1(int c);
875static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
876static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100877static typval_T *alloc_string_tv(char_u *string);
878static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100879#ifdef FEAT_FLOAT
880static float_T get_tv_float(typval_T *varp);
881#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100882static linenr_T get_tv_lnum(typval_T *argvars);
883static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
885static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
886static hashtab_T *find_var_ht(char_u *name, char_u **varname);
887static funccall_T *get_funccal(void);
888static void vars_clear_ext(hashtab_T *ht, int free_val);
889static void delete_var(hashtab_T *ht, hashitem_T *hi);
890static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
891static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
892static void set_var(char_u *name, typval_T *varp, int copy);
893static int var_check_ro(int flags, char_u *name, int use_gettext);
894static int var_check_fixed(int flags, char_u *name, int use_gettext);
895static int var_check_func_name(char_u *name, int new_var);
896static int valid_varname(char_u *varname);
897static int tv_check_lock(int lock, char_u *name, int use_gettext);
898static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
899static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100900static 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 +0100901static int eval_fname_script(char_u *p);
902static int eval_fname_sid(char_u *p);
903static void list_func_head(ufunc_T *fp, int indent);
904static ufunc_T *find_func(char_u *name);
905static int function_exists(char_u *name);
906static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000907#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100908static void func_do_profile(ufunc_T *fp);
909static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
910static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000911static int
912# ifdef __BORLANDC__
913 _RTLENTRYF
914# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100915 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000916static int
917# ifdef __BORLANDC__
918 _RTLENTRYF
919# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100920 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000921#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100922static int script_autoload(char_u *name, int reload);
923static char_u *autoload_name(char_u *name);
924static void cat_func_name(char_u *buf, ufunc_T *fp);
925static void func_free(ufunc_T *fp);
926static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
927static int can_free_funccal(funccall_T *fc, int copyID) ;
928static void free_funccal(funccall_T *fc, int free_val);
929static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
930static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
931static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
932static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
933static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
934static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
935static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
936static int write_list(FILE *fd, list_T *list, int binary);
937static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000938
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200939
940#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100941static int compare_func_name(const void *s1, const void *s2);
942static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200943#endif
944
Bram Moolenaar33570922005-01-25 22:26:29 +0000945/*
946 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000947 */
948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100949eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000950{
Bram Moolenaar33570922005-01-25 22:26:29 +0000951 int i;
952 struct vimvar *p;
953
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200954 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
955 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200956 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000957 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000958 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000959
960 for (i = 0; i < VV_LEN; ++i)
961 {
962 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200963 if (STRLEN(p->vv_name) > 16)
964 {
965 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
966 getout(1);
967 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000968 STRCPY(p->vv_di.di_key, p->vv_name);
969 if (p->vv_flags & VV_RO)
970 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
971 else if (p->vv_flags & VV_RO_SBX)
972 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
973 else
974 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000975
976 /* add to v: scope dict, unless the value is not always available */
977 if (p->vv_type != VAR_UNKNOWN)
978 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000980 /* add to compat scope dict */
981 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100983 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
984
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000985 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100986 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200987 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100988 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100989
990 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
991 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
992 set_vim_var_nr(VV_NONE, VVAL_NONE);
993 set_vim_var_nr(VV_NULL, VVAL_NULL);
994
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200995 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200996
997#ifdef EBCDIC
998 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100999 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001000 */
1001 sortFunctions();
1002#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001003}
1004
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001005#if defined(EXITFREE) || defined(PROTO)
1006 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001007eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001008{
1009 int i;
1010 struct vimvar *p;
1011
1012 for (i = 0; i < VV_LEN; ++i)
1013 {
1014 p = &vimvars[i];
1015 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001016 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001017 vim_free(p->vv_str);
1018 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001019 }
1020 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1021 {
1022 list_unref(p->vv_list);
1023 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001024 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001025 }
1026 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001027 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001028 hash_clear(&compat_hashtab);
1029
Bram Moolenaard9fba312005-06-26 22:34:35 +00001030 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001031# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001032 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001033# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001034
1035 /* global variables */
1036 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001037
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001038 /* autoloaded script names */
1039 ga_clear_strings(&ga_loaded);
1040
Bram Moolenaarcca74132013-09-25 21:00:28 +02001041 /* Script-local variables. First clear all the variables and in a second
1042 * loop free the scriptvar_T, because a variable in one script might hold
1043 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001044 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001045 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001046 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001047 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001048 ga_clear(&ga_scripts);
1049
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001050 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001051 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001052
1053 /* functions */
1054 free_all_functions();
1055 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001056}
1057#endif
1058
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 * Return the name of the executed function.
1061 */
1062 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001063func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066}
1067
1068/*
1069 * Return the address holding the next breakpoint line for a funccall cookie.
1070 */
1071 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001072func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073{
Bram Moolenaar33570922005-01-25 22:26:29 +00001074 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075}
1076
1077/*
1078 * Return the address holding the debug tick for a funccall cookie.
1079 */
1080 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001081func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
Bram Moolenaar33570922005-01-25 22:26:29 +00001083 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084}
1085
1086/*
1087 * Return the nesting level for a funccall cookie.
1088 */
1089 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001090func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
Bram Moolenaar33570922005-01-25 22:26:29 +00001092 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093}
1094
1095/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001096funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001098/* pointer to list of previously used funccal, still around because some
1099 * item in it is still being used. */
1100funccall_T *previous_funccal = NULL;
1101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102/*
1103 * Return TRUE when a function was ended by a ":return" command.
1104 */
1105 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001106current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107{
1108 return current_funccal->returned;
1109}
1110
1111
1112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 * Set an internal variable to a string value. Creates the variable if it does
1114 * not already exist.
1115 */
1116 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001117set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001119 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001120 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121
1122 val = vim_strsave(value);
1123 if (val != NULL)
1124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001126 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001128 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001129 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 }
1132}
1133
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136static char_u *redir_endp = NULL;
1137static char_u *redir_varname = NULL;
1138
1139/*
1140 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001141 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 * Returns OK if successfully completed the setup. FAIL otherwise.
1143 */
1144 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001145var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146{
1147 int save_emsg;
1148 int err;
1149 typval_T tv;
1150
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001151 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001152 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 {
1154 EMSG(_(e_invarg));
1155 return FAIL;
1156 }
1157
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 redir_varname = vim_strsave(name);
1160 if (redir_varname == NULL)
1161 return FAIL;
1162
1163 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1164 if (redir_lval == NULL)
1165 {
1166 var_redir_stop();
1167 return FAIL;
1168 }
1169
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001170 /* The output is stored in growarray "redir_ga" until redirection ends. */
1171 ga_init2(&redir_ga, (int)sizeof(char), 500);
1172
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001174 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001175 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1177 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001178 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 if (redir_endp != NULL && *redir_endp != NUL)
1180 /* Trailing characters are present after the variable name */
1181 EMSG(_(e_trailing));
1182 else
1183 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001184 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185 var_redir_stop();
1186 return FAIL;
1187 }
1188
1189 /* check if we can write to the variable: set it to or append an empty
1190 * string */
1191 save_emsg = did_emsg;
1192 did_emsg = FALSE;
1193 tv.v_type = VAR_STRING;
1194 tv.vval.v_string = (char_u *)"";
1195 if (append)
1196 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1197 else
1198 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001199 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001201 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 if (err)
1203 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001204 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 var_redir_stop();
1206 return FAIL;
1207 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208
1209 return OK;
1210}
1211
1212/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001213 * Append "value[value_len]" to the variable set by var_redir_start().
1214 * The actual appending is postponed until redirection ends, because the value
1215 * appended may in fact be the string we write to, changing it may cause freed
1216 * memory to be used:
1217 * :redir => foo
1218 * :let foo
1219 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220 */
1221 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001222var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001223{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001224 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001225
1226 if (redir_lval == NULL)
1227 return;
1228
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001229 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001230 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001231 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001232 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001233
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001234 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001235 {
1236 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001237 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001238 }
1239 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001240 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001241}
1242
1243/*
1244 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001245 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001246 */
1247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001248var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001249{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001250 typval_T tv;
1251
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001252 if (redir_lval != NULL)
1253 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001254 /* If there was no error: assign the text to the variable. */
1255 if (redir_endp != NULL)
1256 {
1257 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1258 tv.v_type = VAR_STRING;
1259 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001260 /* Call get_lval() again, if it's inside a Dict or List it may
1261 * have changed. */
1262 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001263 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001264 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1265 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1266 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001267 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001268
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001269 /* free the collected output */
1270 vim_free(redir_ga.ga_data);
1271 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001272
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001273 vim_free(redir_lval);
1274 redir_lval = NULL;
1275 }
1276 vim_free(redir_varname);
1277 redir_varname = NULL;
1278}
1279
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280# if defined(FEAT_MBYTE) || defined(PROTO)
1281 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001282eval_charconvert(
1283 char_u *enc_from,
1284 char_u *enc_to,
1285 char_u *fname_from,
1286 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287{
1288 int err = FALSE;
1289
1290 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1291 set_vim_var_string(VV_CC_TO, enc_to, -1);
1292 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1293 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1294 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1295 err = TRUE;
1296 set_vim_var_string(VV_CC_FROM, NULL, -1);
1297 set_vim_var_string(VV_CC_TO, NULL, -1);
1298 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1299 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1300
1301 if (err)
1302 return FAIL;
1303 return OK;
1304}
1305# endif
1306
1307# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310{
1311 int err = FALSE;
1312
1313 set_vim_var_string(VV_FNAME_IN, fname, -1);
1314 set_vim_var_string(VV_CMDARG, args, -1);
1315 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1316 err = TRUE;
1317 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1318 set_vim_var_string(VV_CMDARG, NULL, -1);
1319
1320 if (err)
1321 {
1322 mch_remove(fname);
1323 return FAIL;
1324 }
1325 return OK;
1326}
1327# endif
1328
1329# if defined(FEAT_DIFF) || defined(PROTO)
1330 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001331eval_diff(
1332 char_u *origfile,
1333 char_u *newfile,
1334 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 int err = FALSE;
1337
1338 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1339 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1340 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1341 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1342 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1343 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1344 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1345}
1346
1347 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001348eval_patch(
1349 char_u *origfile,
1350 char_u *difffile,
1351 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 int err;
1354
1355 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1356 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1357 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1358 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1359 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1360 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1361 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1362}
1363# endif
1364
1365/*
1366 * Top level evaluation function, returning a boolean.
1367 * Sets "error" to TRUE if there was an error.
1368 * Return TRUE or FALSE.
1369 */
1370 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001371eval_to_bool(
1372 char_u *arg,
1373 int *error,
1374 char_u **nextcmd,
1375 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 int retval = FALSE;
1379
1380 if (skip)
1381 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 else
1385 {
1386 *error = FALSE;
1387 if (!skip)
1388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001389 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392 }
1393 if (skip)
1394 --emsg_skip;
1395
1396 return retval;
1397}
1398
1399/*
1400 * Top level evaluation function, returning a string. If "skip" is TRUE,
1401 * only parsing to "nextcmd" is done, without reporting errors. Return
1402 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1403 */
1404 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001405eval_to_string_skip(
1406 char_u *arg,
1407 char_u **nextcmd,
1408 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
Bram Moolenaar33570922005-01-25 22:26:29 +00001410 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 char_u *retval;
1412
1413 if (skip)
1414 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 retval = NULL;
1417 else
1418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 retval = vim_strsave(get_tv_string(&tv));
1420 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 }
1422 if (skip)
1423 --emsg_skip;
1424
1425 return retval;
1426}
1427
1428/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001429 * Skip over an expression at "*pp".
1430 * Return FAIL for an error, OK otherwise.
1431 */
1432 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001433skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001434{
Bram Moolenaar33570922005-01-25 22:26:29 +00001435 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001436
1437 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001438 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001439}
1440
1441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001443 * When "convert" is TRUE convert a List into a sequence of lines and convert
1444 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 * Return pointer to allocated memory, or NULL for failure.
1446 */
1447 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001448eval_to_string(
1449 char_u *arg,
1450 char_u **nextcmd,
1451 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
Bram Moolenaar33570922005-01-25 22:26:29 +00001453 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001455 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001456#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001457 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001460 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 retval = NULL;
1462 else
1463 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001464 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001465 {
1466 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001467 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001468 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001469 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001470 if (tv.vval.v_list->lv_len > 0)
1471 ga_append(&ga, NL);
1472 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001473 ga_append(&ga, NUL);
1474 retval = (char_u *)ga.ga_data;
1475 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001476#ifdef FEAT_FLOAT
1477 else if (convert && tv.v_type == VAR_FLOAT)
1478 {
1479 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1480 retval = vim_strsave(numbuf);
1481 }
1482#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001483 else
1484 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 }
1487
1488 return retval;
1489}
1490
1491/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001492 * Call eval_to_string() without using current local variables and using
1493 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 */
1495 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001496eval_to_string_safe(
1497 char_u *arg,
1498 char_u **nextcmd,
1499 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500{
1501 char_u *retval;
1502 void *save_funccalp;
1503
1504 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001505 if (use_sandbox)
1506 ++sandbox;
1507 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001508 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001509 if (use_sandbox)
1510 --sandbox;
1511 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 restore_funccal(save_funccalp);
1513 return retval;
1514}
1515
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516/*
1517 * Top level evaluation function, returning a number.
1518 * Evaluates "expr" silently.
1519 * Returns -1 for an error.
1520 */
1521 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001522eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
Bram Moolenaar33570922005-01-25 22:26:29 +00001524 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001526 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
1528 ++emsg_off;
1529
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001530 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 retval = -1;
1532 else
1533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001534 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001535 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
1537 --emsg_off;
1538
1539 return retval;
1540}
1541
Bram Moolenaara40058a2005-07-11 22:42:07 +00001542/*
1543 * Prepare v: variable "idx" to be used.
1544 * Save the current typeval in "save_tv".
1545 * When not used yet add the variable to the v: hashtable.
1546 */
1547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001548prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001549{
1550 *save_tv = vimvars[idx].vv_tv;
1551 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1552 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1553}
1554
1555/*
1556 * Restore v: variable "idx" to typeval "save_tv".
1557 * When no longer defined, remove the variable from the v: hashtable.
1558 */
1559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001560restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001561{
1562 hashitem_T *hi;
1563
Bram Moolenaara40058a2005-07-11 22:42:07 +00001564 vimvars[idx].vv_tv = *save_tv;
1565 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1566 {
1567 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1568 if (HASHITEM_EMPTY(hi))
1569 EMSG2(_(e_intern2), "restore_vimvar()");
1570 else
1571 hash_remove(&vimvarht, hi);
1572 }
1573}
1574
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001575#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001576/*
1577 * Evaluate an expression to a list with suggestions.
1578 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001579 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580 */
1581 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001582eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001583{
1584 typval_T save_val;
1585 typval_T rettv;
1586 list_T *list = NULL;
1587 char_u *p = skipwhite(expr);
1588
1589 /* Set "v:val" to the bad word. */
1590 prepare_vimvar(VV_VAL, &save_val);
1591 vimvars[VV_VAL].vv_type = VAR_STRING;
1592 vimvars[VV_VAL].vv_str = badword;
1593 if (p_verbose == 0)
1594 ++emsg_off;
1595
1596 if (eval1(&p, &rettv, TRUE) == OK)
1597 {
1598 if (rettv.v_type != VAR_LIST)
1599 clear_tv(&rettv);
1600 else
1601 list = rettv.vval.v_list;
1602 }
1603
1604 if (p_verbose == 0)
1605 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001606 restore_vimvar(VV_VAL, &save_val);
1607
1608 return list;
1609}
1610
1611/*
1612 * "list" is supposed to contain two items: a word and a number. Return the
1613 * word in "pp" and the number as the return value.
1614 * Return -1 if anything isn't right.
1615 * Used to get the good word and score from the eval_spell_expr() result.
1616 */
1617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001618get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001619{
1620 listitem_T *li;
1621
1622 li = list->lv_first;
1623 if (li == NULL)
1624 return -1;
1625 *pp = get_tv_string(&li->li_tv);
1626
1627 li = li->li_next;
1628 if (li == NULL)
1629 return -1;
1630 return get_tv_number(&li->li_tv);
1631}
1632#endif
1633
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001634/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001635 * Top level evaluation function.
1636 * Returns an allocated typval_T with the result.
1637 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001638 */
1639 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001640eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001641{
1642 typval_T *tv;
1643
1644 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001645 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001646 {
1647 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001648 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001649 }
1650
1651 return tv;
1652}
1653
1654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001657 * Uses argv[argc] for the function arguments. Only Number and String
1658 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001661 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001662call_vim_function(
1663 char_u *func,
1664 int argc,
1665 char_u **argv,
1666 int safe, /* use the sandbox */
1667 int str_arg_only, /* all arguments are strings */
1668 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669{
Bram Moolenaar33570922005-01-25 22:26:29 +00001670 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 long n;
1672 int len;
1673 int i;
1674 int doesrange;
1675 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001678 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
1682 for (i = 0; i < argc; i++)
1683 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001684 /* Pass a NULL or empty argument as an empty string */
1685 if (argv[i] == NULL || *argv[i] == NUL)
1686 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001687 argvars[i].v_type = VAR_STRING;
1688 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001689 continue;
1690 }
1691
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001692 if (str_arg_only)
1693 len = 0;
1694 else
1695 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001696 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 if (len != 0 && len == (int)STRLEN(argv[i]))
1698 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001699 argvars[i].v_type = VAR_NUMBER;
1700 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702 else
1703 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001704 argvars[i].v_type = VAR_STRING;
1705 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 }
1707 }
1708
1709 if (safe)
1710 {
1711 save_funccalp = save_funccal();
1712 ++sandbox;
1713 }
1714
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001715 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1716 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001718 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (safe)
1720 {
1721 --sandbox;
1722 restore_funccal(save_funccalp);
1723 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 vim_free(argvars);
1725
1726 if (ret == FAIL)
1727 clear_tv(rettv);
1728
1729 return ret;
1730}
1731
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001732/*
1733 * Call vimL function "func" and return the result as a number.
1734 * Returns -1 when calling the function fails.
1735 * Uses argv[argc] for the function arguments.
1736 */
1737 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001738call_func_retnr(
1739 char_u *func,
1740 int argc,
1741 char_u **argv,
1742 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001743{
1744 typval_T rettv;
1745 long retval;
1746
1747 /* All arguments are passed as strings, no conversion to number. */
1748 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1749 return -1;
1750
1751 retval = get_tv_number_chk(&rettv, NULL);
1752 clear_tv(&rettv);
1753 return retval;
1754}
1755
1756#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1757 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1758
Bram Moolenaar4f688582007-07-24 12:34:30 +00001759# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001761 * Call vimL function "func" and return the result as a string.
1762 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763 * Uses argv[argc] for the function arguments.
1764 */
1765 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001766call_func_retstr(
1767 char_u *func,
1768 int argc,
1769 char_u **argv,
1770 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001771{
1772 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001773 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001774
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001775 /* All arguments are passed as strings, no conversion to number. */
1776 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001777 return NULL;
1778
1779 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001780 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 return retval;
1782}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001783# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001784
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001785/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001786 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001787 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001788 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001789 */
1790 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001791call_func_retlist(
1792 char_u *func,
1793 int argc,
1794 char_u **argv,
1795 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001796{
1797 typval_T rettv;
1798
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001799 /* All arguments are passed as strings, no conversion to number. */
1800 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001801 return NULL;
1802
1803 if (rettv.v_type != VAR_LIST)
1804 {
1805 clear_tv(&rettv);
1806 return NULL;
1807 }
1808
1809 return rettv.vval.v_list;
1810}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811#endif
1812
1813/*
1814 * Save the current function call pointer, and set it to NULL.
1815 * Used when executing autocommands and for ":source".
1816 */
1817 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001820 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 current_funccal = NULL;
1823 return (void *)fc;
1824}
1825
1826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001829 funccall_T *fc = (funccall_T *)vfc;
1830
1831 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832}
1833
Bram Moolenaar05159a02005-02-26 23:04:13 +00001834#if defined(FEAT_PROFILE) || defined(PROTO)
1835/*
1836 * Prepare profiling for entering a child or something else that is not
1837 * counted for the script/function itself.
1838 * Should always be called in pair with prof_child_exit().
1839 */
1840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001841prof_child_enter(
1842 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001843{
1844 funccall_T *fc = current_funccal;
1845
1846 if (fc != NULL && fc->func->uf_profiling)
1847 profile_start(&fc->prof_child);
1848 script_prof_save(tm);
1849}
1850
1851/*
1852 * Take care of time spent in a child.
1853 * Should always be called after prof_child_enter().
1854 */
1855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001856prof_child_exit(
1857 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001858{
1859 funccall_T *fc = current_funccal;
1860
1861 if (fc != NULL && fc->func->uf_profiling)
1862 {
1863 profile_end(&fc->prof_child);
1864 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1865 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1866 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1867 }
1868 script_prof_restore(tm);
1869}
1870#endif
1871
1872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873#ifdef FEAT_FOLDING
1874/*
1875 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1876 * it in "*cp". Doesn't give error messages.
1877 */
1878 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001879eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
Bram Moolenaar33570922005-01-25 22:26:29 +00001881 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 int retval;
1883 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001884 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1885 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
1887 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001888 if (use_sandbox)
1889 ++sandbox;
1890 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 retval = 0;
1894 else
1895 {
1896 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 if (tv.v_type == VAR_NUMBER)
1898 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001899 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 retval = 0;
1901 else
1902 {
1903 /* If the result is a string, check if there is a non-digit before
1904 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 if (!VIM_ISDIGIT(*s) && *s != '-')
1907 *cp = *s++;
1908 retval = atol((char *)s);
1909 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001913 if (use_sandbox)
1914 --sandbox;
1915 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916
1917 return retval;
1918}
1919#endif
1920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 * ":let" list all variable values
1923 * ":let var1 var2" list variable values
1924 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001925 * ":let var += expr" assignment command.
1926 * ":let var -= expr" assignment command.
1927 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 */
1930 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001931ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
1933 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001935 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 int var_count = 0;
1938 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001940 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001941 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
Bram Moolenaardb552d602006-03-23 22:59:57 +00001943 argend = skip_var_list(arg, &var_count, &semicolon);
1944 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001946 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1947 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001948 expr = skipwhite(argend);
1949 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1950 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001952 /*
1953 * ":let" without "=": list variables
1954 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 if (*arg == '[')
1956 EMSG(_(e_invarg));
1957 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001959 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001963 list_glob_vars(&first);
1964 list_buf_vars(&first);
1965 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001966#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001967 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001968#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001969 list_script_vars(&first);
1970 list_func_vars(&first);
1971 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 eap->nextcmd = check_nextcmd(arg);
1974 }
1975 else
1976 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 op[0] = '=';
1978 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001979 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001981 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1982 op[0] = *expr; /* +=, -= or .= */
1983 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001984 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001985 else
1986 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (eap->skip)
1989 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (eap->skip)
1992 {
1993 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 --emsg_skip;
1996 }
1997 else if (i != FAIL)
1998 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002000 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002001 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
2003 }
2004}
2005
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006/*
2007 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2008 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 * When "nextchars" is not NULL it points to a string with characters that
2010 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2011 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 * Returns OK or FAIL;
2013 */
2014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002015ex_let_vars(
2016 char_u *arg_start,
2017 typval_T *tv,
2018 int copy, /* copy values from "tv", don't move */
2019 int semicolon, /* from skip_var_list() */
2020 int var_count, /* from skip_var_list() */
2021 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022{
2023 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002024 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002026 listitem_T *item;
2027 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028
2029 if (*arg != '[')
2030 {
2031 /*
2032 * ":let var = expr" or ":for var in list"
2033 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002034 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035 return FAIL;
2036 return OK;
2037 }
2038
2039 /*
2040 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2041 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002042 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002043 {
2044 EMSG(_(e_listreq));
2045 return FAIL;
2046 }
2047
2048 i = list_len(l);
2049 if (semicolon == 0 && var_count < i)
2050 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002051 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 return FAIL;
2053 }
2054 if (var_count - semicolon > i)
2055 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002056 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 return FAIL;
2058 }
2059
2060 item = l->lv_first;
2061 while (*arg != ']')
2062 {
2063 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002064 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065 item = item->li_next;
2066 if (arg == NULL)
2067 return FAIL;
2068
2069 arg = skipwhite(arg);
2070 if (*arg == ';')
2071 {
2072 /* Put the rest of the list (may be empty) in the var after ';'.
2073 * Create a new list for this. */
2074 l = list_alloc();
2075 if (l == NULL)
2076 return FAIL;
2077 while (item != NULL)
2078 {
2079 list_append_tv(l, &item->li_tv);
2080 item = item->li_next;
2081 }
2082
2083 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002084 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085 ltv.vval.v_list = l;
2086 l->lv_refcount = 1;
2087
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002088 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2089 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002090 clear_tv(&ltv);
2091 if (arg == NULL)
2092 return FAIL;
2093 break;
2094 }
2095 else if (*arg != ',' && *arg != ']')
2096 {
2097 EMSG2(_(e_intern2), "ex_let_vars()");
2098 return FAIL;
2099 }
2100 }
2101
2102 return OK;
2103}
2104
2105/*
2106 * Skip over assignable variable "var" or list of variables "[var, var]".
2107 * Used for ":let varvar = expr" and ":for varvar in expr".
2108 * For "[var, var]" increment "*var_count" for each variable.
2109 * for "[var, var; var]" set "semicolon".
2110 * Return NULL for an error.
2111 */
2112 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002113skip_var_list(
2114 char_u *arg,
2115 int *var_count,
2116 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002117{
2118 char_u *p, *s;
2119
2120 if (*arg == '[')
2121 {
2122 /* "[var, var]": find the matching ']'. */
2123 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002124 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002125 {
2126 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2127 s = skip_var_one(p);
2128 if (s == p)
2129 {
2130 EMSG2(_(e_invarg2), p);
2131 return NULL;
2132 }
2133 ++*var_count;
2134
2135 p = skipwhite(s);
2136 if (*p == ']')
2137 break;
2138 else if (*p == ';')
2139 {
2140 if (*semicolon == 1)
2141 {
2142 EMSG(_("Double ; in list of variables"));
2143 return NULL;
2144 }
2145 *semicolon = 1;
2146 }
2147 else if (*p != ',')
2148 {
2149 EMSG2(_(e_invarg2), p);
2150 return NULL;
2151 }
2152 }
2153 return p + 1;
2154 }
2155 else
2156 return skip_var_one(arg);
2157}
2158
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002159/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002160 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002161 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002162 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002163 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002165{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002166 if (*arg == '@' && arg[1] != NUL)
2167 return arg + 2;
2168 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2169 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002170}
2171
Bram Moolenaara7043832005-01-21 11:56:39 +00002172/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002173 * List variables for hashtab "ht" with prefix "prefix".
2174 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002177list_hashtable_vars(
2178 hashtab_T *ht,
2179 char_u *prefix,
2180 int empty,
2181 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002182{
Bram Moolenaar33570922005-01-25 22:26:29 +00002183 hashitem_T *hi;
2184 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002185 int todo;
2186
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002187 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002188 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2189 {
2190 if (!HASHITEM_EMPTY(hi))
2191 {
2192 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002193 di = HI2DI(hi);
2194 if (empty || di->di_tv.v_type != VAR_STRING
2195 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002197 }
2198 }
2199}
2200
2201/*
2202 * List global variables.
2203 */
2204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002205list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002206{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002208}
2209
2210/*
2211 * List buffer variables.
2212 */
2213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002215{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 char_u numbuf[NUMBUFLEN];
2217
Bram Moolenaar429fa852013-04-15 12:27:36 +02002218 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220
2221 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2223 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002224}
2225
2226/*
2227 * List window variables.
2228 */
2229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002230list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002231{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002232 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002233 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002234}
2235
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002236#ifdef FEAT_WINDOWS
2237/*
2238 * List tab page variables.
2239 */
2240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002241list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002242{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002243 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002244 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245}
2246#endif
2247
Bram Moolenaara7043832005-01-21 11:56:39 +00002248/*
2249 * List Vim variables.
2250 */
2251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002252list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002254 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255}
2256
2257/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002258 * List script-local variables, if there is a script.
2259 */
2260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002261list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002262{
2263 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2265 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002266}
2267
2268/*
2269 * List function variables, if there is a function.
2270 */
2271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002272list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002273{
2274 if (current_funccal != NULL)
2275 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002276 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002277}
2278
2279/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 * List variables in "arg".
2281 */
2282 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002283list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284{
2285 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 char_u *name_start;
2289 char_u *arg_subsc;
2290 char_u *tofree;
2291 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292
2293 while (!ends_excmd(*arg) && !got_int)
2294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002297 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2299 {
2300 emsg_severe = TRUE;
2301 EMSG(_(e_trailing));
2302 break;
2303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 /* get_name_len() takes care of expanding curly braces */
2308 name_start = name = arg;
2309 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2310 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 /* This is mainly to keep test 49 working: when expanding
2313 * curly braces fails overrule the exception error message. */
2314 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 emsg_severe = TRUE;
2317 EMSG2(_(e_invarg2), arg);
2318 break;
2319 }
2320 error = TRUE;
2321 }
2322 else
2323 {
2324 if (tofree != NULL)
2325 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002326 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 else
2329 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002330 /* handle d.key, l[idx], f(expr) */
2331 arg_subsc = arg;
2332 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002335 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002337 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002338 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002340 case 'g': list_glob_vars(first); break;
2341 case 'b': list_buf_vars(first); break;
2342 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002343#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002344 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002345#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002346 case 'v': list_vim_vars(first); break;
2347 case 's': list_script_vars(first); break;
2348 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002349 default:
2350 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002351 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002352 }
2353 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002354 {
2355 char_u numbuf[NUMBUFLEN];
2356 char_u *tf;
2357 int c;
2358 char_u *s;
2359
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002360 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002361 c = *arg;
2362 *arg = NUL;
2363 list_one_var_a((char_u *)"",
2364 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002365 tv.v_type,
2366 s == NULL ? (char_u *)"" : s,
2367 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002368 *arg = c;
2369 vim_free(tf);
2370 }
2371 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002372 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
2374 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002375
2376 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002378
2379 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002380 }
2381
2382 return arg;
2383}
2384
2385/*
2386 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2387 * Returns a pointer to the char just after the var name.
2388 * Returns NULL if there is an error.
2389 */
2390 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002391ex_let_one(
2392 char_u *arg, /* points to variable name */
2393 typval_T *tv, /* value to assign to variable */
2394 int copy, /* copy value from "tv" */
2395 char_u *endchars, /* valid chars after variable name or NULL */
2396 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397{
2398 int c1;
2399 char_u *name;
2400 char_u *p;
2401 char_u *arg_end = NULL;
2402 int len;
2403 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002405
2406 /*
2407 * ":let $VAR = expr": Set environment variable.
2408 */
2409 if (*arg == '$')
2410 {
2411 /* Find the end of the name. */
2412 ++arg;
2413 name = arg;
2414 len = get_env_len(&arg);
2415 if (len == 0)
2416 EMSG2(_(e_invarg2), name - 1);
2417 else
2418 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 if (op != NULL && (*op == '+' || *op == '-'))
2420 EMSG2(_(e_letwrong), op);
2421 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002422 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002424 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 {
2426 c1 = name[len];
2427 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002428 p = get_tv_string_chk(tv);
2429 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 {
2431 int mustfree = FALSE;
2432 char_u *s = vim_getenv(name, &mustfree);
2433
2434 if (s != NULL)
2435 {
2436 p = tofree = concat_str(s, p);
2437 if (mustfree)
2438 vim_free(s);
2439 }
2440 }
2441 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 if (STRICMP(name, "HOME") == 0)
2445 init_homedir();
2446 else if (didset_vim && STRICMP(name, "VIM") == 0)
2447 didset_vim = FALSE;
2448 else if (didset_vimruntime
2449 && STRICMP(name, "VIMRUNTIME") == 0)
2450 didset_vimruntime = FALSE;
2451 arg_end = arg;
2452 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 }
2456 }
2457 }
2458
2459 /*
2460 * ":let &option = expr": Set option value.
2461 * ":let &l:option = expr": Set local option value.
2462 * ":let &g:option = expr": Set global option value.
2463 */
2464 else if (*arg == '&')
2465 {
2466 /* Find the end of the name. */
2467 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002468 if (p == NULL || (endchars != NULL
2469 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 long n;
2474 int opt_type;
2475 long numval;
2476 char_u *stringval = NULL;
2477 char_u *s;
2478
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 c1 = *p;
2480 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481
2482 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002483 s = get_tv_string_chk(tv); /* != NULL if number or string */
2484 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 {
2486 opt_type = get_option_value(arg, &numval,
2487 &stringval, opt_flags);
2488 if ((opt_type == 1 && *op == '.')
2489 || (opt_type == 0 && *op != '.'))
2490 EMSG2(_(e_letwrong), op);
2491 else
2492 {
2493 if (opt_type == 1) /* number */
2494 {
2495 if (*op == '+')
2496 n = numval + n;
2497 else
2498 n = numval - n;
2499 }
2500 else if (opt_type == 0 && stringval != NULL) /* string */
2501 {
2502 s = concat_str(stringval, s);
2503 vim_free(stringval);
2504 stringval = s;
2505 }
2506 }
2507 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002508 if (s != NULL)
2509 {
2510 set_option_value(arg, n, s, opt_flags);
2511 arg_end = p;
2512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002514 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 }
2516 }
2517
2518 /*
2519 * ":let @r = expr": Set register contents.
2520 */
2521 else if (*arg == '@')
2522 {
2523 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002524 if (op != NULL && (*op == '+' || *op == '-'))
2525 EMSG2(_(e_letwrong), op);
2526 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002527 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 EMSG(_(e_letunexp));
2529 else
2530 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002531 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 char_u *s;
2533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002534 p = get_tv_string_chk(tv);
2535 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002536 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002537 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002538 if (s != NULL)
2539 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002540 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002541 vim_free(s);
2542 }
2543 }
2544 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002545 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002546 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002547 arg_end = arg + 1;
2548 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002549 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 }
2551 }
2552
2553 /*
2554 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002557 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002559 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002561 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002562 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2565 EMSG(_(e_letunexp));
2566 else
2567 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002568 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 arg_end = p;
2570 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 }
2574
2575 else
2576 EMSG2(_(e_invarg2), arg);
2577
2578 return arg_end;
2579}
2580
2581/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2583 */
2584 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002585check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002586{
2587 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2588 {
2589 EMSG2(_(e_readonlyvar), arg);
2590 return TRUE;
2591 }
2592 return FALSE;
2593}
2594
2595/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 * Get an lval: variable, Dict item or List item that can be assigned a value
2597 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2598 * "name.key", "name.key[expr]" etc.
2599 * Indexing only works if "name" is an existing List or Dictionary.
2600 * "name" points to the start of the name.
2601 * If "rettv" is not NULL it points to the value to be assigned.
2602 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2603 * wrong; must end in space or cmd separator.
2604 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002605 * flags:
2606 * GLV_QUIET: do not give error messages
2607 * GLV_NO_AUTOLOAD: do not use script autoloading
2608 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002610 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 */
2613 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002614get_lval(
2615 char_u *name,
2616 typval_T *rettv,
2617 lval_T *lp,
2618 int unlet,
2619 int skip,
2620 int flags, /* GLV_ values */
2621 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 char_u *expr_start, *expr_end;
2625 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 dictitem_T *v;
2627 typval_T var1;
2628 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002630 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002633 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002634 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002637 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638
2639 if (skip)
2640 {
2641 /* When skipping just find the end of the name. */
2642 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002643 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 }
2645
2646 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002647 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (expr_start != NULL)
2649 {
2650 /* Don't expand the name when we already know there is an error. */
2651 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2652 && *p != '[' && *p != '.')
2653 {
2654 EMSG(_(e_trailing));
2655 return NULL;
2656 }
2657
2658 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2659 if (lp->ll_exp_name == NULL)
2660 {
2661 /* Report an invalid expression in braces, unless the
2662 * expression evaluation has been cancelled due to an
2663 * aborting error, an interrupt, or an exception. */
2664 if (!aborting() && !quiet)
2665 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002666 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 EMSG2(_(e_invarg2), name);
2668 return NULL;
2669 }
2670 }
2671 lp->ll_name = lp->ll_exp_name;
2672 }
2673 else
2674 lp->ll_name = name;
2675
2676 /* Without [idx] or .key we are done. */
2677 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2678 return p;
2679
2680 cc = *p;
2681 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002682 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (v == NULL && !quiet)
2684 EMSG2(_(e_undefvar), lp->ll_name);
2685 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002686 if (v == NULL)
2687 return NULL;
2688
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 /*
2690 * Loop until no more [idx] or .key is following.
2691 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002692 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2696 && !(lp->ll_tv->v_type == VAR_DICT
2697 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
2700 EMSG(_("E689: Can only index a List or Dictionary"));
2701 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002702 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (!quiet)
2706 EMSG(_("E708: [:] must come last"));
2707 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002708 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 len = -1;
2711 if (*p == '.')
2712 {
2713 key = p + 1;
2714 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2715 ;
2716 if (len == 0)
2717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_(e_emptykey));
2720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
2722 p = key + len;
2723 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 else
2725 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (*p == ':')
2729 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002730 else
2731 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 empty1 = FALSE;
2733 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002735 if (get_tv_string_chk(&var1) == NULL)
2736 {
2737 /* not a number or string */
2738 clear_tv(&var1);
2739 return NULL;
2740 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
2742
2743 /* Optionally get the second index [ :expr]. */
2744 if (*p == ':')
2745 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002749 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 if (!empty1)
2751 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (rettv != NULL && (rettv->v_type != VAR_LIST
2755 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (!quiet)
2758 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 if (!empty1)
2760 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 }
2763 p = skipwhite(p + 1);
2764 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 else
2767 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2770 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (!empty1)
2772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002775 if (get_tv_string_chk(&var2) == NULL)
2776 {
2777 /* not a number or string */
2778 if (!empty1)
2779 clear_tv(&var1);
2780 clear_tv(&var2);
2781 return NULL;
2782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002785 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002788
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002790 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (!quiet)
2792 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 if (!empty1)
2794 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 }
2799
2800 /* Skip to past ']'. */
2801 ++p;
2802 }
2803
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 {
2806 if (len == -1)
2807 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002809 key = get_tv_string_chk(&var1); /* is number or string */
2810 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 }
2815 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002816 lp->ll_list = NULL;
2817 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002818 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002819
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002820 /* When assigning to a scope dictionary check that a function and
2821 * variable name is valid (only variable name unless it is l: or
2822 * g: dictionary). Disallow overwriting a builtin function. */
2823 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002825 int prevval;
2826 int wrong;
2827
2828 if (len != -1)
2829 {
2830 prevval = key[len];
2831 key[len] = NUL;
2832 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002833 else
2834 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002835 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2836 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002837 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002838 || !valid_varname(key);
2839 if (len != -1)
2840 key[len] = prevval;
2841 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002842 return NULL;
2843 }
2844
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002847 /* Can't add "v:" variable. */
2848 if (lp->ll_dict == &vimvardict)
2849 {
2850 EMSG2(_(e_illvar), name);
2851 return NULL;
2852 }
2853
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002854 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002858 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 if (len == -1)
2860 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 }
2863 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 if (len == -1)
2868 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 p = NULL;
2871 break;
2872 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002873 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002874 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002875 return NULL;
2876
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 if (len == -1)
2878 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 }
2881 else
2882 {
2883 /*
2884 * Get the number and item for the only or first index of the List.
2885 */
2886 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 else
2889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002890 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 clear_tv(&var1);
2892 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002893 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 lp->ll_list = lp->ll_tv->vval.v_list;
2895 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2896 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002897 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002898 if (lp->ll_n1 < 0)
2899 {
2900 lp->ll_n1 = 0;
2901 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2902 }
2903 }
2904 if (lp->ll_li == NULL)
2905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002908 if (!quiet)
2909 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002911 }
2912
2913 /*
2914 * May need to find the item or absolute index for the second
2915 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 * When no index given: "lp->ll_empty2" is TRUE.
2917 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002921 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002924 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002926 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002927 {
2928 if (!quiet)
2929 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002931 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002933 }
2934
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2936 if (lp->ll_n1 < 0)
2937 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2938 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002939 {
2940 if (!quiet)
2941 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002943 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002944 }
2945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002947 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002948 }
2949
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 return p;
2951}
2952
2953/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002954 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 */
2956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002957clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958{
2959 vim_free(lp->ll_exp_name);
2960 vim_free(lp->ll_newkey);
2961}
2962
2963/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002964 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002966 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 */
2968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002969set_var_lval(
2970 lval_T *lp,
2971 char_u *endp,
2972 typval_T *rettv,
2973 int copy,
2974 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975{
2976 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002977 listitem_T *ri;
2978 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979
2980 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 cc = *endp;
2985 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986 if (op != NULL && *op != '=')
2987 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002988 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002989
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002990 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002991 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002992 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002993 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002995 if ((di == NULL
2996 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2997 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2998 FALSE)))
2999 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 set_var(lp->ll_name, &tv, FALSE);
3001 clear_tv(&tv);
3002 }
3003 }
3004 else
3005 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003006 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003007 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003009 else if (tv_check_lock(lp->ll_newkey == NULL
3010 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003011 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003012 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 else if (lp->ll_range)
3014 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003015 listitem_T *ll_li = lp->ll_li;
3016 int ll_n1 = lp->ll_n1;
3017
3018 /*
3019 * Check whether any of the list items is locked
3020 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003021 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003022 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003023 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003024 return;
3025 ri = ri->li_next;
3026 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3027 break;
3028 ll_li = ll_li->li_next;
3029 ++ll_n1;
3030 }
3031
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 /*
3033 * Assign the List values to the list items.
3034 */
3035 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003036 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 if (op != NULL && *op != '=')
3038 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3039 else
3040 {
3041 clear_tv(&lp->ll_li->li_tv);
3042 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3043 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 ri = ri->li_next;
3045 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3046 break;
3047 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003048 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003050 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 ri = NULL;
3053 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003054 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003055 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003056 lp->ll_li = lp->ll_li->li_next;
3057 ++lp->ll_n1;
3058 }
3059 if (ri != NULL)
3060 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003061 else if (lp->ll_empty2
3062 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 : lp->ll_n1 != lp->ll_n2)
3064 EMSG(_("E711: List value has not enough items"));
3065 }
3066 else
3067 {
3068 /*
3069 * Assign to a List or Dictionary item.
3070 */
3071 if (lp->ll_newkey != NULL)
3072 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003073 if (op != NULL && *op != '=')
3074 {
3075 EMSG2(_(e_letwrong), op);
3076 return;
3077 }
3078
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003079 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003080 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003081 if (di == NULL)
3082 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003083 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3084 {
3085 vim_free(di);
3086 return;
3087 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003088 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 else if (op != NULL && *op != '=')
3091 {
3092 tv_op(lp->ll_tv, rettv, op);
3093 return;
3094 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003096 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003097
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003098 /*
3099 * Assign the value to the variable or list item.
3100 */
3101 if (copy)
3102 copy_tv(rettv, lp->ll_tv);
3103 else
3104 {
3105 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003106 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003107 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003108 }
3109 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003110}
3111
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3114 * Returns OK or FAIL.
3115 */
3116 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003117tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003118{
3119 long n;
3120 char_u numbuf[NUMBUFLEN];
3121 char_u *s;
3122
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003123 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3124 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3125 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 {
3127 switch (tv1->v_type)
3128 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003129 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 case VAR_DICT:
3131 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003132 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003133 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003134 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003135 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003136 break;
3137
3138 case VAR_LIST:
3139 if (*op != '+' || tv2->v_type != VAR_LIST)
3140 break;
3141 /* List += List */
3142 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3143 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3144 return OK;
3145
3146 case VAR_NUMBER:
3147 case VAR_STRING:
3148 if (tv2->v_type == VAR_LIST)
3149 break;
3150 if (*op == '+' || *op == '-')
3151 {
3152 /* nr += nr or nr -= nr*/
3153 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154#ifdef FEAT_FLOAT
3155 if (tv2->v_type == VAR_FLOAT)
3156 {
3157 float_T f = n;
3158
3159 if (*op == '+')
3160 f += tv2->vval.v_float;
3161 else
3162 f -= tv2->vval.v_float;
3163 clear_tv(tv1);
3164 tv1->v_type = VAR_FLOAT;
3165 tv1->vval.v_float = f;
3166 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003167 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168#endif
3169 {
3170 if (*op == '+')
3171 n += get_tv_number(tv2);
3172 else
3173 n -= get_tv_number(tv2);
3174 clear_tv(tv1);
3175 tv1->v_type = VAR_NUMBER;
3176 tv1->vval.v_number = n;
3177 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003178 }
3179 else
3180 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003181 if (tv2->v_type == VAR_FLOAT)
3182 break;
3183
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003184 /* str .= str */
3185 s = get_tv_string(tv1);
3186 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3187 clear_tv(tv1);
3188 tv1->v_type = VAR_STRING;
3189 tv1->vval.v_string = s;
3190 }
3191 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003193 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003194#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003195 {
3196 float_T f;
3197
3198 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3199 && tv2->v_type != VAR_NUMBER
3200 && tv2->v_type != VAR_STRING))
3201 break;
3202 if (tv2->v_type == VAR_FLOAT)
3203 f = tv2->vval.v_float;
3204 else
3205 f = get_tv_number(tv2);
3206 if (*op == '+')
3207 tv1->vval.v_float += f;
3208 else
3209 tv1->vval.v_float -= f;
3210 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003211#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003212 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003213 }
3214 }
3215
3216 EMSG2(_(e_letwrong), op);
3217 return FAIL;
3218}
3219
3220/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 * Add a watcher to a list.
3222 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003223 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225{
3226 lw->lw_next = l->lv_watch;
3227 l->lv_watch = lw;
3228}
3229
3230/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003231 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 * No warning when it isn't found...
3233 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236{
Bram Moolenaar33570922005-01-25 22:26:29 +00003237 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238
3239 lwp = &l->lv_watch;
3240 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3241 {
3242 if (lw == lwrem)
3243 {
3244 *lwp = lw->lw_next;
3245 break;
3246 }
3247 lwp = &lw->lw_next;
3248 }
3249}
3250
3251/*
3252 * Just before removing an item from a list: advance watchers to the next
3253 * item.
3254 */
3255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003256list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257{
Bram Moolenaar33570922005-01-25 22:26:29 +00003258 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259
3260 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3261 if (lw->lw_item == item)
3262 lw->lw_item = item->li_next;
3263}
3264
3265/*
3266 * Evaluate the expression used in a ":for var in expr" command.
3267 * "arg" points to "var".
3268 * Set "*errp" to TRUE for an error, FALSE otherwise;
3269 * Return a pointer that holds the info. Null when there is an error.
3270 */
3271 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003272eval_for_line(
3273 char_u *arg,
3274 int *errp,
3275 char_u **nextcmdp,
3276 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277{
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003280 typval_T tv;
3281 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282
3283 *errp = TRUE; /* default: there is an error */
3284
Bram Moolenaar33570922005-01-25 22:26:29 +00003285 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 if (fi == NULL)
3287 return NULL;
3288
3289 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3290 if (expr == NULL)
3291 return fi;
3292
3293 expr = skipwhite(expr);
3294 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3295 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003296 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 return fi;
3298 }
3299
3300 if (skip)
3301 ++emsg_skip;
3302 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3303 {
3304 *errp = FALSE;
3305 if (!skip)
3306 {
3307 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003308 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003309 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003310 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003311 clear_tv(&tv);
3312 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003313 else if (l == NULL)
3314 {
3315 /* a null list is like an empty list: do nothing */
3316 clear_tv(&tv);
3317 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 else
3319 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003320 /* No need to increment the refcount, it's already set for the
3321 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322 fi->fi_list = l;
3323 list_add_watch(l, &fi->fi_lw);
3324 fi->fi_lw.lw_item = l->lv_first;
3325 }
3326 }
3327 }
3328 if (skip)
3329 --emsg_skip;
3330
3331 return fi;
3332}
3333
3334/*
3335 * Use the first item in a ":for" list. Advance to the next.
3336 * Assign the values to the variable (list). "arg" points to the first one.
3337 * Return TRUE when a valid item was found, FALSE when at end of list or
3338 * something wrong.
3339 */
3340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003341next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003343 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003345 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346
3347 item = fi->fi_lw.lw_item;
3348 if (item == NULL)
3349 result = FALSE;
3350 else
3351 {
3352 fi->fi_lw.lw_item = item->li_next;
3353 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3354 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3355 }
3356 return result;
3357}
3358
3359/*
3360 * Free the structure used to store info used by ":for".
3361 */
3362 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003363free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003364{
Bram Moolenaar33570922005-01-25 22:26:29 +00003365 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003366
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003367 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003368 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003369 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003370 list_unref(fi->fi_list);
3371 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003372 vim_free(fi);
3373}
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3376
3377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003378set_context_for_expression(
3379 expand_T *xp,
3380 char_u *arg,
3381 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 int got_eq = FALSE;
3384 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003385 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003387 if (cmdidx == CMD_let)
3388 {
3389 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003390 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003391 {
3392 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003393 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003394 {
3395 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003396 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003397 if (vim_iswhite(*p))
3398 break;
3399 }
3400 return;
3401 }
3402 }
3403 else
3404 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3405 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 while ((xp->xp_pattern = vim_strpbrk(arg,
3407 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3408 {
3409 c = *xp->xp_pattern;
3410 if (c == '&')
3411 {
3412 c = xp->xp_pattern[1];
3413 if (c == '&')
3414 {
3415 ++xp->xp_pattern;
3416 xp->xp_context = cmdidx != CMD_let || got_eq
3417 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3418 }
3419 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003422 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3423 xp->xp_pattern += 2;
3424
3425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 }
3427 else if (c == '$')
3428 {
3429 /* environment variable */
3430 xp->xp_context = EXPAND_ENV_VARS;
3431 }
3432 else if (c == '=')
3433 {
3434 got_eq = TRUE;
3435 xp->xp_context = EXPAND_EXPRESSION;
3436 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003437 else if (c == '#'
3438 && xp->xp_context == EXPAND_EXPRESSION)
3439 {
3440 /* Autoload function/variable contains '#'. */
3441 break;
3442 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003443 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 && xp->xp_context == EXPAND_FUNCTIONS
3445 && vim_strchr(xp->xp_pattern, '(') == NULL)
3446 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003447 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 break;
3449 }
3450 else if (cmdidx != CMD_let || got_eq)
3451 {
3452 if (c == '"') /* string */
3453 {
3454 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3455 if (c == '\\' && xp->xp_pattern[1] != NUL)
3456 ++xp->xp_pattern;
3457 xp->xp_context = EXPAND_NOTHING;
3458 }
3459 else if (c == '\'') /* literal string */
3460 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003461 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3463 /* skip */ ;
3464 xp->xp_context = EXPAND_NOTHING;
3465 }
3466 else if (c == '|')
3467 {
3468 if (xp->xp_pattern[1] == '|')
3469 {
3470 ++xp->xp_pattern;
3471 xp->xp_context = EXPAND_EXPRESSION;
3472 }
3473 else
3474 xp->xp_context = EXPAND_COMMANDS;
3475 }
3476 else
3477 xp->xp_context = EXPAND_EXPRESSION;
3478 }
3479 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003480 /* Doesn't look like something valid, expand as an expression
3481 * anyway. */
3482 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 arg = xp->xp_pattern;
3484 if (*arg != NUL)
3485 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3486 /* skip */ ;
3487 }
3488 xp->xp_pattern = arg;
3489}
3490
3491#endif /* FEAT_CMDL_COMPL */
3492
3493/*
3494 * ":1,25call func(arg1, arg2)" function call.
3495 */
3496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003497ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498{
3499 char_u *arg = eap->arg;
3500 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003502 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003504 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 linenr_T lnum;
3506 int doesrange;
3507 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003508 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003509 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003511 if (eap->skip)
3512 {
3513 /* trans_function_name() doesn't work well when skipping, use eval0()
3514 * instead to skip to any following command, e.g. for:
3515 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003516 ++emsg_skip;
3517 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3518 clear_tv(&rettv);
3519 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003520 return;
3521 }
3522
Bram Moolenaar65639032016-03-16 21:40:30 +01003523 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003524 if (fudi.fd_newkey != NULL)
3525 {
3526 /* Still need to give an error message for missing key. */
3527 EMSG2(_(e_dictkey), fudi.fd_newkey);
3528 vim_free(fudi.fd_newkey);
3529 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003530 if (tofree == NULL)
3531 return;
3532
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003533 /* Increase refcount on dictionary, it could get deleted when evaluating
3534 * the arguments. */
3535 if (fudi.fd_dict != NULL)
3536 ++fudi.fd_dict->dv_refcount;
3537
Bram Moolenaar65639032016-03-16 21:40:30 +01003538 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3539 * contents. For VAR_PARTIAL get its partial, unless we already have one
3540 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003541 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003542 name = deref_func_name(tofree, &len,
3543 partial != NULL ? NULL : &partial, FALSE);
3544
Bram Moolenaar532c7802005-01-27 14:44:31 +00003545 /* Skip white space to allow ":call func ()". Not good, but required for
3546 * backward compatibility. */
3547 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003548 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
3550 if (*startarg != '(')
3551 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003552 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 goto end;
3554 }
3555
3556 /*
3557 * When skipping, evaluate the function once, to find the end of the
3558 * arguments.
3559 * When the function takes a range, this is discovered after the first
3560 * call, and the loop is broken.
3561 */
3562 if (eap->skip)
3563 {
3564 ++emsg_skip;
3565 lnum = eap->line2; /* do it once, also with an invalid range */
3566 }
3567 else
3568 lnum = eap->line1;
3569 for ( ; lnum <= eap->line2; ++lnum)
3570 {
3571 if (!eap->skip && eap->addr_count > 0)
3572 {
3573 curwin->w_cursor.lnum = lnum;
3574 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003575#ifdef FEAT_VIRTUALEDIT
3576 curwin->w_cursor.coladd = 0;
3577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003580 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003581 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003582 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 failed = TRUE;
3585 break;
3586 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003587
3588 /* Handle a function returning a Funcref, Dictionary or List. */
3589 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3590 {
3591 failed = TRUE;
3592 break;
3593 }
3594
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003595 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (doesrange || eap->skip)
3597 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003598
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003600 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003601 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003602 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 if (aborting())
3604 break;
3605 }
3606 if (eap->skip)
3607 --emsg_skip;
3608
3609 if (!failed)
3610 {
3611 /* Check for trailing illegal characters and a following command. */
3612 if (!ends_excmd(*arg))
3613 {
3614 emsg_severe = TRUE;
3615 EMSG(_(e_trailing));
3616 }
3617 else
3618 eap->nextcmd = check_nextcmd(arg);
3619 }
3620
3621end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003622 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003623 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624}
3625
3626/*
3627 * ":unlet[!] var1 ... " command.
3628 */
3629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003630ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003632 ex_unletlock(eap, eap->arg, 0);
3633}
3634
3635/*
3636 * ":lockvar" and ":unlockvar" commands
3637 */
3638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003639ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003640{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003642 int deep = 2;
3643
3644 if (eap->forceit)
3645 deep = -1;
3646 else if (vim_isdigit(*arg))
3647 {
3648 deep = getdigits(&arg);
3649 arg = skipwhite(arg);
3650 }
3651
3652 ex_unletlock(eap, arg, deep);
3653}
3654
3655/*
3656 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3657 */
3658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003659ex_unletlock(
3660 exarg_T *eap,
3661 char_u *argstart,
3662 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663{
3664 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
3669 do
3670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003672 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003673 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 if (lv.ll_name == NULL)
3675 error = TRUE; /* error but continue parsing */
3676 if (name_end == NULL || (!vim_iswhite(*name_end)
3677 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 if (name_end != NULL)
3680 {
3681 emsg_severe = TRUE;
3682 EMSG(_(e_trailing));
3683 }
3684 if (!(eap->skip || error))
3685 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 break;
3687 }
3688
3689 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003690 {
3691 if (eap->cmdidx == CMD_unlet)
3692 {
3693 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3694 error = TRUE;
3695 }
3696 else
3697 {
3698 if (do_lock_var(&lv, name_end, deep,
3699 eap->cmdidx == CMD_lockvar) == FAIL)
3700 error = TRUE;
3701 }
3702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704 if (!eap->skip)
3705 clear_lval(&lv);
3706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 arg = skipwhite(name_end);
3708 } while (!ends_excmd(*arg));
3709
3710 eap->nextcmd = check_nextcmd(arg);
3711}
3712
Bram Moolenaar8c711452005-01-14 21:53:12 +00003713 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003714do_unlet_var(
3715 lval_T *lp,
3716 char_u *name_end,
3717 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003718{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 int ret = OK;
3720 int cc;
3721
3722 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 cc = *name_end;
3725 *name_end = NUL;
3726
3727 /* Normal name or expanded name. */
3728 if (check_changedtick(lp->ll_name))
3729 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003730 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003731 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003732 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003733 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003735 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003736 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003737 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003738 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003739 else if (lp->ll_range)
3740 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003742 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003743 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003744
3745 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3746 {
3747 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003748 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003749 return FAIL;
3750 ll_li = li;
3751 ++ll_n1;
3752 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003753
3754 /* Delete a range of List items. */
3755 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3756 {
3757 li = lp->ll_li->li_next;
3758 listitem_remove(lp->ll_list, lp->ll_li);
3759 lp->ll_li = li;
3760 ++lp->ll_n1;
3761 }
3762 }
3763 else
3764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003765 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003766 /* unlet a List item. */
3767 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003768 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003769 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003770 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003771 }
3772
3773 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003774}
3775
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776/*
3777 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 */
3780 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003781do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
Bram Moolenaar33570922005-01-25 22:26:29 +00003783 hashtab_T *ht;
3784 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003785 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003786 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003787 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
Bram Moolenaar33570922005-01-25 22:26:29 +00003789 ht = find_var_ht(name, &varname);
3790 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003792 if (ht == &globvarht)
3793 d = &globvardict;
3794 else if (current_funccal != NULL
3795 && ht == &current_funccal->l_vars.dv_hashtab)
3796 d = &current_funccal->l_vars;
3797 else if (ht == &compat_hashtab)
3798 d = &vimvardict;
3799 else
3800 {
3801 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3802 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3803 }
3804 if (d == NULL)
3805 {
3806 EMSG2(_(e_intern2), "do_unlet()");
3807 return FAIL;
3808 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003809 hi = hash_find(ht, varname);
3810 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003811 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003812 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003813 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003814 || var_check_ro(di->di_flags, name, FALSE)
3815 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003816 return FAIL;
3817
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 delete_var(ht, hi);
3819 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003822 if (forceit)
3823 return OK;
3824 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 return FAIL;
3826}
3827
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003828/*
3829 * Lock or unlock variable indicated by "lp".
3830 * "deep" is the levels to go (-1 for unlimited);
3831 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3832 */
3833 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003834do_lock_var(
3835 lval_T *lp,
3836 char_u *name_end,
3837 int deep,
3838 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003839{
3840 int ret = OK;
3841 int cc;
3842 dictitem_T *di;
3843
3844 if (deep == 0) /* nothing to do */
3845 return OK;
3846
3847 if (lp->ll_tv == NULL)
3848 {
3849 cc = *name_end;
3850 *name_end = NUL;
3851
3852 /* Normal name or expanded name. */
3853 if (check_changedtick(lp->ll_name))
3854 ret = FAIL;
3855 else
3856 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003857 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003858 if (di == NULL)
3859 ret = FAIL;
3860 else
3861 {
3862 if (lock)
3863 di->di_flags |= DI_FLAGS_LOCK;
3864 else
3865 di->di_flags &= ~DI_FLAGS_LOCK;
3866 item_lock(&di->di_tv, deep, lock);
3867 }
3868 }
3869 *name_end = cc;
3870 }
3871 else if (lp->ll_range)
3872 {
3873 listitem_T *li = lp->ll_li;
3874
3875 /* (un)lock a range of List items. */
3876 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3877 {
3878 item_lock(&li->li_tv, deep, lock);
3879 li = li->li_next;
3880 ++lp->ll_n1;
3881 }
3882 }
3883 else if (lp->ll_list != NULL)
3884 /* (un)lock a List item. */
3885 item_lock(&lp->ll_li->li_tv, deep, lock);
3886 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003887 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003888 item_lock(&lp->ll_di->di_tv, deep, lock);
3889
3890 return ret;
3891}
3892
3893/*
3894 * Lock or unlock an item. "deep" is nr of levels to go.
3895 */
3896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003897item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003898{
3899 static int recurse = 0;
3900 list_T *l;
3901 listitem_T *li;
3902 dict_T *d;
3903 hashitem_T *hi;
3904 int todo;
3905
3906 if (recurse >= DICT_MAXNEST)
3907 {
3908 EMSG(_("E743: variable nested too deep for (un)lock"));
3909 return;
3910 }
3911 if (deep == 0)
3912 return;
3913 ++recurse;
3914
3915 /* lock/unlock the item itself */
3916 if (lock)
3917 tv->v_lock |= VAR_LOCKED;
3918 else
3919 tv->v_lock &= ~VAR_LOCKED;
3920
3921 switch (tv->v_type)
3922 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003923 case VAR_UNKNOWN:
3924 case VAR_NUMBER:
3925 case VAR_STRING:
3926 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003927 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003928 case VAR_FLOAT:
3929 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003930 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003931 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003932 break;
3933
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003934 case VAR_LIST:
3935 if ((l = tv->vval.v_list) != NULL)
3936 {
3937 if (lock)
3938 l->lv_lock |= VAR_LOCKED;
3939 else
3940 l->lv_lock &= ~VAR_LOCKED;
3941 if (deep < 0 || deep > 1)
3942 /* recursive: lock/unlock the items the List contains */
3943 for (li = l->lv_first; li != NULL; li = li->li_next)
3944 item_lock(&li->li_tv, deep - 1, lock);
3945 }
3946 break;
3947 case VAR_DICT:
3948 if ((d = tv->vval.v_dict) != NULL)
3949 {
3950 if (lock)
3951 d->dv_lock |= VAR_LOCKED;
3952 else
3953 d->dv_lock &= ~VAR_LOCKED;
3954 if (deep < 0 || deep > 1)
3955 {
3956 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003957 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003958 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3959 {
3960 if (!HASHITEM_EMPTY(hi))
3961 {
3962 --todo;
3963 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3964 }
3965 }
3966 }
3967 }
3968 }
3969 --recurse;
3970}
3971
Bram Moolenaara40058a2005-07-11 22:42:07 +00003972/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003973 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3974 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003975 */
3976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003977tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003978{
3979 return (tv->v_lock & VAR_LOCKED)
3980 || (tv->v_type == VAR_LIST
3981 && tv->vval.v_list != NULL
3982 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3983 || (tv->v_type == VAR_DICT
3984 && tv->vval.v_dict != NULL
3985 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3986}
3987
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3989/*
3990 * Delete all "menutrans_" variables.
3991 */
3992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003993del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994{
Bram Moolenaar33570922005-01-25 22:26:29 +00003995 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003996 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997
Bram Moolenaar33570922005-01-25 22:26:29 +00003998 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003999 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004001 {
4002 if (!HASHITEM_EMPTY(hi))
4003 {
4004 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004005 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4006 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004007 }
4008 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010}
4011#endif
4012
4013#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4014
4015/*
4016 * Local string buffer for the next two functions to store a variable name
4017 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4018 * get_user_var_name().
4019 */
4020
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004021static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
4023static char_u *varnamebuf = NULL;
4024static int varnamebuflen = 0;
4025
4026/*
4027 * Function to concatenate a prefix and a variable name.
4028 */
4029 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004030cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031{
4032 int len;
4033
4034 len = (int)STRLEN(name) + 3;
4035 if (len > varnamebuflen)
4036 {
4037 vim_free(varnamebuf);
4038 len += 10; /* some additional space */
4039 varnamebuf = alloc(len);
4040 if (varnamebuf == NULL)
4041 {
4042 varnamebuflen = 0;
4043 return NULL;
4044 }
4045 varnamebuflen = len;
4046 }
4047 *varnamebuf = prefix;
4048 varnamebuf[1] = ':';
4049 STRCPY(varnamebuf + 2, name);
4050 return varnamebuf;
4051}
4052
4053/*
4054 * Function given to ExpandGeneric() to obtain the list of user defined
4055 * (global/buffer/window/built-in) variable names.
4056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004058get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004060 static long_u gdone;
4061 static long_u bdone;
4062 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004063#ifdef FEAT_WINDOWS
4064 static long_u tdone;
4065#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004066 static int vidx;
4067 static hashitem_T *hi;
4068 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004071 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004072 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073#ifdef FEAT_WINDOWS
4074 tdone = 0;
4075#endif
4076 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004077
4078 /* Global variables */
4079 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004081 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004083 else
4084 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004085 while (HASHITEM_EMPTY(hi))
4086 ++hi;
4087 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4088 return cat_prefix_varname('g', hi->hi_key);
4089 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004091
4092 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004093 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004094 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004097 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004098 else
4099 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004100 while (HASHITEM_EMPTY(hi))
4101 ++hi;
4102 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004104 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004106 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 return (char_u *)"b:changedtick";
4108 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004109
4110 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004111 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004112 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004114 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004115 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004116 else
4117 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004118 while (HASHITEM_EMPTY(hi))
4119 ++hi;
4120 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004122
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004123#ifdef FEAT_WINDOWS
4124 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004125 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004126 if (tdone < ht->ht_used)
4127 {
4128 if (tdone++ == 0)
4129 hi = ht->ht_array;
4130 else
4131 ++hi;
4132 while (HASHITEM_EMPTY(hi))
4133 ++hi;
4134 return cat_prefix_varname('t', hi->hi_key);
4135 }
4136#endif
4137
Bram Moolenaar33570922005-01-25 22:26:29 +00004138 /* v: variables */
4139 if (vidx < VV_LEN)
4140 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
4142 vim_free(varnamebuf);
4143 varnamebuf = NULL;
4144 varnamebuflen = 0;
4145 return NULL;
4146}
4147
4148#endif /* FEAT_CMDL_COMPL */
4149
4150/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004151 * Return TRUE if "pat" matches "text".
4152 * Does not use 'cpo' and always uses 'magic'.
4153 */
4154 static int
4155pattern_match(char_u *pat, char_u *text, int ic)
4156{
4157 int matches = FALSE;
4158 char_u *save_cpo;
4159 regmatch_T regmatch;
4160
4161 /* avoid 'l' flag in 'cpoptions' */
4162 save_cpo = p_cpo;
4163 p_cpo = (char_u *)"";
4164 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4165 if (regmatch.regprog != NULL)
4166 {
4167 regmatch.rm_ic = ic;
4168 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4169 vim_regfree(regmatch.regprog);
4170 }
4171 p_cpo = save_cpo;
4172 return matches;
4173}
4174
4175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 * types for expressions.
4177 */
4178typedef enum
4179{
4180 TYPE_UNKNOWN = 0
4181 , TYPE_EQUAL /* == */
4182 , TYPE_NEQUAL /* != */
4183 , TYPE_GREATER /* > */
4184 , TYPE_GEQUAL /* >= */
4185 , TYPE_SMALLER /* < */
4186 , TYPE_SEQUAL /* <= */
4187 , TYPE_MATCH /* =~ */
4188 , TYPE_NOMATCH /* !~ */
4189} exptype_T;
4190
4191/*
4192 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004193 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4195 */
4196
4197/*
4198 * Handle zero level expression.
4199 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004201 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 * Return OK or FAIL.
4203 */
4204 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004205eval0(
4206 char_u *arg,
4207 typval_T *rettv,
4208 char_u **nextcmd,
4209 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211 int ret;
4212 char_u *p;
4213
4214 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 if (ret == FAIL || !ends_excmd(*p))
4217 {
4218 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 /*
4221 * Report the invalid expression unless the expression evaluation has
4222 * been cancelled due to an aborting error, an interrupt, or an
4223 * exception.
4224 */
4225 if (!aborting())
4226 EMSG2(_(e_invexpr2), arg);
4227 ret = FAIL;
4228 }
4229 if (nextcmd != NULL)
4230 *nextcmd = check_nextcmd(p);
4231
4232 return ret;
4233}
4234
4235/*
4236 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004237 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 *
4239 * "arg" must point to the first non-white of the expression.
4240 * "arg" is advanced to the next non-white after the recognized expression.
4241 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004242 * Note: "rettv.v_lock" is not set.
4243 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 * Return OK or FAIL.
4245 */
4246 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004247eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248{
4249 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004250 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 /*
4253 * Get the first variable.
4254 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return FAIL;
4257
4258 if ((*arg)[0] == '?')
4259 {
4260 result = FALSE;
4261 if (evaluate)
4262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 int error = FALSE;
4264
4265 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (error)
4269 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271
4272 /*
4273 * Get the second variable.
4274 */
4275 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004276 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 return FAIL;
4278
4279 /*
4280 * Check for the ":".
4281 */
4282 if ((*arg)[0] != ':')
4283 {
4284 EMSG(_("E109: Missing ':' after '?'"));
4285 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 return FAIL;
4288 }
4289
4290 /*
4291 * Get the third variable.
4292 */
4293 *arg = skipwhite(*arg + 1);
4294 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4295 {
4296 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 return FAIL;
4299 }
4300 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303
4304 return OK;
4305}
4306
4307/*
4308 * Handle first level expression:
4309 * expr2 || expr2 || expr2 logical OR
4310 *
4311 * "arg" must point to the first non-white of the expression.
4312 * "arg" is advanced to the next non-white after the recognized expression.
4313 *
4314 * Return OK or FAIL.
4315 */
4316 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004317eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318{
Bram Moolenaar33570922005-01-25 22:26:29 +00004319 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 long result;
4321 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004322 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 /*
4325 * Get the first variable.
4326 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004327 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 return FAIL;
4329
4330 /*
4331 * Repeat until there is no following "||".
4332 */
4333 first = TRUE;
4334 result = FALSE;
4335 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4336 {
4337 if (evaluate && first)
4338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 if (error)
4343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 first = FALSE;
4345 }
4346
4347 /*
4348 * Get the second variable.
4349 */
4350 *arg = skipwhite(*arg + 2);
4351 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4352 return FAIL;
4353
4354 /*
4355 * Compute the result.
4356 */
4357 if (evaluate && !result)
4358 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004359 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004361 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004362 if (error)
4363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 if (evaluate)
4366 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004367 rettv->v_type = VAR_NUMBER;
4368 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 }
4371
4372 return OK;
4373}
4374
4375/*
4376 * Handle second level expression:
4377 * expr3 && expr3 && expr3 logical AND
4378 *
4379 * "arg" must point to the first non-white of the expression.
4380 * "arg" is advanced to the next non-white after the recognized expression.
4381 *
4382 * Return OK or FAIL.
4383 */
4384 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004385eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386{
Bram Moolenaar33570922005-01-25 22:26:29 +00004387 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 long result;
4389 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004390 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
4392 /*
4393 * Get the first variable.
4394 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004395 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 return FAIL;
4397
4398 /*
4399 * Repeat until there is no following "&&".
4400 */
4401 first = TRUE;
4402 result = TRUE;
4403 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4404 {
4405 if (evaluate && first)
4406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004410 if (error)
4411 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 first = FALSE;
4413 }
4414
4415 /*
4416 * Get the second variable.
4417 */
4418 *arg = skipwhite(*arg + 2);
4419 if (eval4(arg, &var2, evaluate && result) == FAIL)
4420 return FAIL;
4421
4422 /*
4423 * Compute the result.
4424 */
4425 if (evaluate && result)
4426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004427 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004429 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004430 if (error)
4431 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433 if (evaluate)
4434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004435 rettv->v_type = VAR_NUMBER;
4436 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 }
4439
4440 return OK;
4441}
4442
4443/*
4444 * Handle third level expression:
4445 * var1 == var2
4446 * var1 =~ var2
4447 * var1 != var2
4448 * var1 !~ var2
4449 * var1 > var2
4450 * var1 >= var2
4451 * var1 < var2
4452 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004453 * var1 is var2
4454 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 *
4456 * "arg" must point to the first non-white of the expression.
4457 * "arg" is advanced to the next non-white after the recognized expression.
4458 *
4459 * Return OK or FAIL.
4460 */
4461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004462eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463{
Bram Moolenaar33570922005-01-25 22:26:29 +00004464 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 char_u *p;
4466 int i;
4467 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 int len = 2;
4470 long n1, n2;
4471 char_u *s1, *s2;
4472 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
4475 /*
4476 * Get the first variable.
4477 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004478 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 return FAIL;
4480
4481 p = *arg;
4482 switch (p[0])
4483 {
4484 case '=': if (p[1] == '=')
4485 type = TYPE_EQUAL;
4486 else if (p[1] == '~')
4487 type = TYPE_MATCH;
4488 break;
4489 case '!': if (p[1] == '=')
4490 type = TYPE_NEQUAL;
4491 else if (p[1] == '~')
4492 type = TYPE_NOMATCH;
4493 break;
4494 case '>': if (p[1] != '=')
4495 {
4496 type = TYPE_GREATER;
4497 len = 1;
4498 }
4499 else
4500 type = TYPE_GEQUAL;
4501 break;
4502 case '<': if (p[1] != '=')
4503 {
4504 type = TYPE_SMALLER;
4505 len = 1;
4506 }
4507 else
4508 type = TYPE_SEQUAL;
4509 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004510 case 'i': if (p[1] == 's')
4511 {
4512 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4513 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004514 i = p[len];
4515 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004516 {
4517 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4518 type_is = TRUE;
4519 }
4520 }
4521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523
4524 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004525 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 */
4527 if (type != TYPE_UNKNOWN)
4528 {
4529 /* extra question mark appended: ignore case */
4530 if (p[len] == '?')
4531 {
4532 ic = TRUE;
4533 ++len;
4534 }
4535 /* extra '#' appended: match case */
4536 else if (p[len] == '#')
4537 {
4538 ic = FALSE;
4539 ++len;
4540 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004541 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 else
4543 ic = p_ic;
4544
4545 /*
4546 * Get the second variable.
4547 */
4548 *arg = skipwhite(p + len);
4549 if (eval5(arg, &var2, evaluate) == FAIL)
4550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004551 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 return FAIL;
4553 }
4554
4555 if (evaluate)
4556 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004557 if (type_is && rettv->v_type != var2.v_type)
4558 {
4559 /* For "is" a different type always means FALSE, for "notis"
4560 * it means TRUE. */
4561 n1 = (type == TYPE_NEQUAL);
4562 }
4563 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4564 {
4565 if (type_is)
4566 {
4567 n1 = (rettv->v_type == var2.v_type
4568 && rettv->vval.v_list == var2.vval.v_list);
4569 if (type == TYPE_NEQUAL)
4570 n1 = !n1;
4571 }
4572 else if (rettv->v_type != var2.v_type
4573 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4574 {
4575 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004576 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004577 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004578 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004579 clear_tv(rettv);
4580 clear_tv(&var2);
4581 return FAIL;
4582 }
4583 else
4584 {
4585 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004586 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4587 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004588 if (type == TYPE_NEQUAL)
4589 n1 = !n1;
4590 }
4591 }
4592
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004593 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4594 {
4595 if (type_is)
4596 {
4597 n1 = (rettv->v_type == var2.v_type
4598 && rettv->vval.v_dict == var2.vval.v_dict);
4599 if (type == TYPE_NEQUAL)
4600 n1 = !n1;
4601 }
4602 else if (rettv->v_type != var2.v_type
4603 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4604 {
4605 if (rettv->v_type != var2.v_type)
4606 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4607 else
4608 EMSG(_("E736: Invalid operation for Dictionary"));
4609 clear_tv(rettv);
4610 clear_tv(&var2);
4611 return FAIL;
4612 }
4613 else
4614 {
4615 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004616 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4617 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004618 if (type == TYPE_NEQUAL)
4619 n1 = !n1;
4620 }
4621 }
4622
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004623 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4624 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004625 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004626 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004627 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004628 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004629 clear_tv(rettv);
4630 clear_tv(&var2);
4631 return FAIL;
4632 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004633 if ((rettv->v_type == VAR_PARTIAL
4634 && rettv->vval.v_partial == NULL)
4635 || (var2.v_type == VAR_PARTIAL
4636 && var2.vval.v_partial == NULL))
4637 /* when a partial is NULL assume not equal */
4638 n1 = FALSE;
4639 else if (type_is)
4640 {
4641 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4642 /* strings are considered the same if their value is
4643 * the same */
4644 n1 = tv_equal(rettv, &var2, ic, FALSE);
4645 else if (rettv->v_type == VAR_PARTIAL
4646 && var2.v_type == VAR_PARTIAL)
4647 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4648 else
4649 n1 = FALSE;
4650 }
4651 else
4652 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004653 if (type == TYPE_NEQUAL)
4654 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004655 }
4656
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004657#ifdef FEAT_FLOAT
4658 /*
4659 * If one of the two variables is a float, compare as a float.
4660 * When using "=~" or "!~", always compare as string.
4661 */
4662 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4663 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4664 {
4665 float_T f1, f2;
4666
4667 if (rettv->v_type == VAR_FLOAT)
4668 f1 = rettv->vval.v_float;
4669 else
4670 f1 = get_tv_number(rettv);
4671 if (var2.v_type == VAR_FLOAT)
4672 f2 = var2.vval.v_float;
4673 else
4674 f2 = get_tv_number(&var2);
4675 n1 = FALSE;
4676 switch (type)
4677 {
4678 case TYPE_EQUAL: n1 = (f1 == f2); break;
4679 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4680 case TYPE_GREATER: n1 = (f1 > f2); break;
4681 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4682 case TYPE_SMALLER: n1 = (f1 < f2); break;
4683 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4684 case TYPE_UNKNOWN:
4685 case TYPE_MATCH:
4686 case TYPE_NOMATCH: break; /* avoid gcc warning */
4687 }
4688 }
4689#endif
4690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 /*
4692 * If one of the two variables is a number, compare as a number.
4693 * When using "=~" or "!~", always compare as string.
4694 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004695 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4697 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004698 n1 = get_tv_number(rettv);
4699 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 switch (type)
4701 {
4702 case TYPE_EQUAL: n1 = (n1 == n2); break;
4703 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4704 case TYPE_GREATER: n1 = (n1 > n2); break;
4705 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4706 case TYPE_SMALLER: n1 = (n1 < n2); break;
4707 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4708 case TYPE_UNKNOWN:
4709 case TYPE_MATCH:
4710 case TYPE_NOMATCH: break; /* avoid gcc warning */
4711 }
4712 }
4713 else
4714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004715 s1 = get_tv_string_buf(rettv, buf1);
4716 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4718 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4719 else
4720 i = 0;
4721 n1 = FALSE;
4722 switch (type)
4723 {
4724 case TYPE_EQUAL: n1 = (i == 0); break;
4725 case TYPE_NEQUAL: n1 = (i != 0); break;
4726 case TYPE_GREATER: n1 = (i > 0); break;
4727 case TYPE_GEQUAL: n1 = (i >= 0); break;
4728 case TYPE_SMALLER: n1 = (i < 0); break;
4729 case TYPE_SEQUAL: n1 = (i <= 0); break;
4730
4731 case TYPE_MATCH:
4732 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004733 n1 = pattern_match(s2, s1, ic);
4734 if (type == TYPE_NOMATCH)
4735 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 break;
4737
4738 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4739 }
4740 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004741 clear_tv(rettv);
4742 clear_tv(&var2);
4743 rettv->v_type = VAR_NUMBER;
4744 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
4746 }
4747
4748 return OK;
4749}
4750
4751/*
4752 * Handle fourth level expression:
4753 * + number addition
4754 * - number subtraction
4755 * . string concatenation
4756 *
4757 * "arg" must point to the first non-white of the expression.
4758 * "arg" is advanced to the next non-white after the recognized expression.
4759 *
4760 * Return OK or FAIL.
4761 */
4762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004763eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
Bram Moolenaar33570922005-01-25 22:26:29 +00004765 typval_T var2;
4766 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 int op;
4768 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004769#ifdef FEAT_FLOAT
4770 float_T f1 = 0, f2 = 0;
4771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 char_u *s1, *s2;
4773 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4774 char_u *p;
4775
4776 /*
4777 * Get the first variable.
4778 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004779 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 return FAIL;
4781
4782 /*
4783 * Repeat computing, until no '+', '-' or '.' is following.
4784 */
4785 for (;;)
4786 {
4787 op = **arg;
4788 if (op != '+' && op != '-' && op != '.')
4789 break;
4790
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004791 if ((op != '+' || rettv->v_type != VAR_LIST)
4792#ifdef FEAT_FLOAT
4793 && (op == '.' || rettv->v_type != VAR_FLOAT)
4794#endif
4795 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004796 {
4797 /* For "list + ...", an illegal use of the first operand as
4798 * a number cannot be determined before evaluating the 2nd
4799 * operand: if this is also a list, all is ok.
4800 * For "something . ...", "something - ..." or "non-list + ...",
4801 * we know that the first operand needs to be a string or number
4802 * without evaluating the 2nd operand. So check before to avoid
4803 * side effects after an error. */
4804 if (evaluate && get_tv_string_chk(rettv) == NULL)
4805 {
4806 clear_tv(rettv);
4807 return FAIL;
4808 }
4809 }
4810
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 /*
4812 * Get the second variable.
4813 */
4814 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004815 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return FAIL;
4819 }
4820
4821 if (evaluate)
4822 {
4823 /*
4824 * Compute the result.
4825 */
4826 if (op == '.')
4827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4829 s2 = get_tv_string_buf_chk(&var2, buf2);
4830 if (s2 == NULL) /* type error ? */
4831 {
4832 clear_tv(rettv);
4833 clear_tv(&var2);
4834 return FAIL;
4835 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004836 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837 clear_tv(rettv);
4838 rettv->v_type = VAR_STRING;
4839 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004841 else if (op == '+' && rettv->v_type == VAR_LIST
4842 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004843 {
4844 /* concatenate Lists */
4845 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4846 &var3) == FAIL)
4847 {
4848 clear_tv(rettv);
4849 clear_tv(&var2);
4850 return FAIL;
4851 }
4852 clear_tv(rettv);
4853 *rettv = var3;
4854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 else
4856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004857 int error = FALSE;
4858
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004859#ifdef FEAT_FLOAT
4860 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004861 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004862 f1 = rettv->vval.v_float;
4863 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004864 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865 else
4866#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004867 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004868 n1 = get_tv_number_chk(rettv, &error);
4869 if (error)
4870 {
4871 /* This can only happen for "list + non-list". For
4872 * "non-list + ..." or "something - ...", we returned
4873 * before evaluating the 2nd operand. */
4874 clear_tv(rettv);
4875 return FAIL;
4876 }
4877#ifdef FEAT_FLOAT
4878 if (var2.v_type == VAR_FLOAT)
4879 f1 = n1;
4880#endif
4881 }
4882#ifdef FEAT_FLOAT
4883 if (var2.v_type == VAR_FLOAT)
4884 {
4885 f2 = var2.vval.v_float;
4886 n2 = 0;
4887 }
4888 else
4889#endif
4890 {
4891 n2 = get_tv_number_chk(&var2, &error);
4892 if (error)
4893 {
4894 clear_tv(rettv);
4895 clear_tv(&var2);
4896 return FAIL;
4897 }
4898#ifdef FEAT_FLOAT
4899 if (rettv->v_type == VAR_FLOAT)
4900 f2 = n2;
4901#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004902 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904
4905#ifdef FEAT_FLOAT
4906 /* If there is a float on either side the result is a float. */
4907 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4908 {
4909 if (op == '+')
4910 f1 = f1 + f2;
4911 else
4912 f1 = f1 - f2;
4913 rettv->v_type = VAR_FLOAT;
4914 rettv->vval.v_float = f1;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917#endif
4918 {
4919 if (op == '+')
4920 n1 = n1 + n2;
4921 else
4922 n1 = n1 - n2;
4923 rettv->v_type = VAR_NUMBER;
4924 rettv->vval.v_number = n1;
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004927 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929 }
4930 return OK;
4931}
4932
4933/*
4934 * Handle fifth level expression:
4935 * * number multiplication
4936 * / number division
4937 * % number modulo
4938 *
4939 * "arg" must point to the first non-white of the expression.
4940 * "arg" is advanced to the next non-white after the recognized expression.
4941 *
4942 * Return OK or FAIL.
4943 */
4944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004945eval6(
4946 char_u **arg,
4947 typval_T *rettv,
4948 int evaluate,
4949 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
Bram Moolenaar33570922005-01-25 22:26:29 +00004951 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 int op;
4953 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954#ifdef FEAT_FLOAT
4955 int use_float = FALSE;
4956 float_T f1 = 0, f2;
4957#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004958 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
4960 /*
4961 * Get the first variable.
4962 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004963 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 return FAIL;
4965
4966 /*
4967 * Repeat computing, until no '*', '/' or '%' is following.
4968 */
4969 for (;;)
4970 {
4971 op = **arg;
4972 if (op != '*' && op != '/' && op != '%')
4973 break;
4974
4975 if (evaluate)
4976 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977#ifdef FEAT_FLOAT
4978 if (rettv->v_type == VAR_FLOAT)
4979 {
4980 f1 = rettv->vval.v_float;
4981 use_float = TRUE;
4982 n1 = 0;
4983 }
4984 else
4985#endif
4986 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004987 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004988 if (error)
4989 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 else
4992 n1 = 0;
4993
4994 /*
4995 * Get the second variable.
4996 */
4997 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004998 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 return FAIL;
5000
5001 if (evaluate)
5002 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003#ifdef FEAT_FLOAT
5004 if (var2.v_type == VAR_FLOAT)
5005 {
5006 if (!use_float)
5007 {
5008 f1 = n1;
5009 use_float = TRUE;
5010 }
5011 f2 = var2.vval.v_float;
5012 n2 = 0;
5013 }
5014 else
5015#endif
5016 {
5017 n2 = get_tv_number_chk(&var2, &error);
5018 clear_tv(&var2);
5019 if (error)
5020 return FAIL;
5021#ifdef FEAT_FLOAT
5022 if (use_float)
5023 f2 = n2;
5024#endif
5025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
5027 /*
5028 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005031#ifdef FEAT_FLOAT
5032 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 if (op == '*')
5035 f1 = f1 * f2;
5036 else if (op == '/')
5037 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005038# ifdef VMS
5039 /* VMS crashes on divide by zero, work around it */
5040 if (f2 == 0.0)
5041 {
5042 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005043 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005044 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005045 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005046 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005047 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005048 }
5049 else
5050 f1 = f1 / f2;
5051# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005052 /* We rely on the floating point library to handle divide
5053 * by zero to result in "inf" and not a crash. */
5054 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005055# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005058 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005059 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005060 return FAIL;
5061 }
5062 rettv->v_type = VAR_FLOAT;
5063 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005068 if (op == '*')
5069 n1 = n1 * n2;
5070 else if (op == '/')
5071 {
5072 if (n2 == 0) /* give an error message? */
5073 {
5074 if (n1 == 0)
5075 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5076 else if (n1 < 0)
5077 n1 = -0x7fffffffL;
5078 else
5079 n1 = 0x7fffffffL;
5080 }
5081 else
5082 n1 = n1 / n2;
5083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005085 {
5086 if (n2 == 0) /* give an error message? */
5087 n1 = 0;
5088 else
5089 n1 = n1 % n2;
5090 }
5091 rettv->v_type = VAR_NUMBER;
5092 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095 }
5096
5097 return OK;
5098}
5099
5100/*
5101 * Handle sixth level expression:
5102 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005103 * "string" string constant
5104 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 * &option-name option value
5106 * @r register contents
5107 * identifier variable value
5108 * function() function call
5109 * $VAR environment variable
5110 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005111 * [expr, expr] List
5112 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 *
5114 * Also handle:
5115 * ! in front logical NOT
5116 * - in front unary minus
5117 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 * trailing [] subscript in String or List
5119 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 *
5121 * "arg" must point to the first non-white of the expression.
5122 * "arg" is advanced to the next non-white after the recognized expression.
5123 *
5124 * Return OK or FAIL.
5125 */
5126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005127eval7(
5128 char_u **arg,
5129 typval_T *rettv,
5130 int evaluate,
5131 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 long n;
5134 int len;
5135 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 char_u *start_leader, *end_leader;
5137 int ret = OK;
5138 char_u *alias;
5139
5140 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005144 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
5146 /*
5147 * Skip '!' and '-' characters. They are handled later.
5148 */
5149 start_leader = *arg;
5150 while (**arg == '!' || **arg == '-' || **arg == '+')
5151 *arg = skipwhite(*arg + 1);
5152 end_leader = *arg;
5153
5154 switch (**arg)
5155 {
5156 /*
5157 * Number constant.
5158 */
5159 case '0':
5160 case '1':
5161 case '2':
5162 case '3':
5163 case '4':
5164 case '5':
5165 case '6':
5166 case '7':
5167 case '8':
5168 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005169 {
5170#ifdef FEAT_FLOAT
5171 char_u *p = skipdigits(*arg + 1);
5172 int get_float = FALSE;
5173
5174 /* We accept a float when the format matches
5175 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005176 * strict to avoid backwards compatibility problems.
5177 * Don't look for a float after the "." operator, so that
5178 * ":let vers = 1.2.3" doesn't fail. */
5179 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005181 get_float = TRUE;
5182 p = skipdigits(p + 2);
5183 if (*p == 'e' || *p == 'E')
5184 {
5185 ++p;
5186 if (*p == '-' || *p == '+')
5187 ++p;
5188 if (!vim_isdigit(*p))
5189 get_float = FALSE;
5190 else
5191 p = skipdigits(p + 1);
5192 }
5193 if (ASCII_ISALPHA(*p) || *p == '.')
5194 get_float = FALSE;
5195 }
5196 if (get_float)
5197 {
5198 float_T f;
5199
5200 *arg += string2float(*arg, &f);
5201 if (evaluate)
5202 {
5203 rettv->v_type = VAR_FLOAT;
5204 rettv->vval.v_float = f;
5205 }
5206 }
5207 else
5208#endif
5209 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005210 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005211 *arg += len;
5212 if (evaluate)
5213 {
5214 rettv->v_type = VAR_NUMBER;
5215 rettv->vval.v_number = n;
5216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
5221 /*
5222 * String constant: "string".
5223 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 break;
5226
5227 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005228 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005230 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005231 break;
5232
5233 /*
5234 * List: [expr, expr]
5235 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005236 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 break;
5238
5239 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 * Dictionary: {key: val, key: val}
5241 */
5242 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5243 break;
5244
5245 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005246 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005248 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 break;
5250
5251 /*
5252 * Environment variable: $VAR.
5253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005254 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 break;
5256
5257 /*
5258 * Register contents: @r.
5259 */
5260 case '@': ++*arg;
5261 if (evaluate)
5262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005263 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005264 rettv->vval.v_string = get_reg_contents(**arg,
5265 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
5267 if (**arg != NUL)
5268 ++*arg;
5269 break;
5270
5271 /*
5272 * nested expression: (expression).
5273 */
5274 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005275 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 if (**arg == ')')
5277 ++*arg;
5278 else if (ret == OK)
5279 {
5280 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005281 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 ret = FAIL;
5283 }
5284 break;
5285
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 break;
5288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289
5290 if (ret == NOTDONE)
5291 {
5292 /*
5293 * Must be a variable or function name.
5294 * Can also be a curly-braces kind of name: {expr}.
5295 */
5296 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005297 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 if (alias != NULL)
5299 s = alias;
5300
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005301 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 ret = FAIL;
5303 else
5304 {
5305 if (**arg == '(') /* recursive! */
5306 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005307 partial_T *partial;
5308
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 /* If "s" is the name of a variable of type VAR_FUNC
5310 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005311 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005312
5313 /* Invoke the function. */
5314 ret = get_func_tv(s, len, rettv, arg,
5315 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005316 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005317
5318 /* If evaluate is FALSE rettv->v_type was not set in
5319 * get_func_tv, but it's needed in handle_subscript() to parse
5320 * what follows. So set it here. */
5321 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5322 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005323 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005324 rettv->v_type = VAR_FUNC;
5325 }
5326
Bram Moolenaar8c711452005-01-14 21:53:12 +00005327 /* Stop the expression evaluation when immediately
5328 * aborting on error, or when an interrupt occurred or
5329 * an exception was thrown but not caught. */
5330 if (aborting())
5331 {
5332 if (ret == OK)
5333 clear_tv(rettv);
5334 ret = FAIL;
5335 }
5336 }
5337 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005338 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005339 else
5340 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005342 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005343 }
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 *arg = skipwhite(*arg);
5346
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005347 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5348 * expr(expr). */
5349 if (ret == OK)
5350 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352 /*
5353 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5354 */
5355 if (ret == OK && evaluate && end_leader > start_leader)
5356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005357 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005358 int val = 0;
5359#ifdef FEAT_FLOAT
5360 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005361
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005362 if (rettv->v_type == VAR_FLOAT)
5363 f = rettv->vval.v_float;
5364 else
5365#endif
5366 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005367 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005369 clear_tv(rettv);
5370 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005372 else
5373 {
5374 while (end_leader > start_leader)
5375 {
5376 --end_leader;
5377 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005378 {
5379#ifdef FEAT_FLOAT
5380 if (rettv->v_type == VAR_FLOAT)
5381 f = !f;
5382 else
5383#endif
5384 val = !val;
5385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005386 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005387 {
5388#ifdef FEAT_FLOAT
5389 if (rettv->v_type == VAR_FLOAT)
5390 f = -f;
5391 else
5392#endif
5393 val = -val;
5394 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005395 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005396#ifdef FEAT_FLOAT
5397 if (rettv->v_type == VAR_FLOAT)
5398 {
5399 clear_tv(rettv);
5400 rettv->vval.v_float = f;
5401 }
5402 else
5403#endif
5404 {
5405 clear_tv(rettv);
5406 rettv->v_type = VAR_NUMBER;
5407 rettv->vval.v_number = val;
5408 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 }
5411
5412 return ret;
5413}
5414
5415/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005416 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5417 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5419 */
5420 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005421eval_index(
5422 char_u **arg,
5423 typval_T *rettv,
5424 int evaluate,
5425 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426{
5427 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005428 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 long len = -1;
5431 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434
Bram Moolenaara03f2332016-02-06 18:09:59 +01005435 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005437 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005438 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005439 if (verbose)
5440 EMSG(_("E695: Cannot index a Funcref"));
5441 return FAIL;
5442 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005443#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005444 if (verbose)
5445 EMSG(_(e_float_as_string));
5446 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005447#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005448 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005449 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005450 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005451 if (verbose)
5452 EMSG(_("E909: Cannot index a special variable"));
5453 return FAIL;
5454 case VAR_UNKNOWN:
5455 if (evaluate)
5456 return FAIL;
5457 /* FALLTHROUGH */
5458
5459 case VAR_STRING:
5460 case VAR_NUMBER:
5461 case VAR_LIST:
5462 case VAR_DICT:
5463 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005464 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005466 init_tv(&var1);
5467 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005468 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470 /*
5471 * dict.name
5472 */
5473 key = *arg + 1;
5474 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5475 ;
5476 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482 /*
5483 * something[idx]
5484 *
5485 * Get the (first) variable from inside the [].
5486 */
5487 *arg = skipwhite(*arg + 1);
5488 if (**arg == ':')
5489 empty1 = TRUE;
5490 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5491 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005492 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5493 {
5494 /* not a number or string */
5495 clear_tv(&var1);
5496 return FAIL;
5497 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498
5499 /*
5500 * Get the second variable from inside the [:].
5501 */
5502 if (**arg == ':')
5503 {
5504 range = TRUE;
5505 *arg = skipwhite(*arg + 1);
5506 if (**arg == ']')
5507 empty2 = TRUE;
5508 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005510 if (!empty1)
5511 clear_tv(&var1);
5512 return FAIL;
5513 }
5514 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5515 {
5516 /* not a number or string */
5517 if (!empty1)
5518 clear_tv(&var1);
5519 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005520 return FAIL;
5521 }
5522 }
5523
5524 /* Check for the ']'. */
5525 if (**arg != ']')
5526 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005527 if (verbose)
5528 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005529 clear_tv(&var1);
5530 if (range)
5531 clear_tv(&var2);
5532 return FAIL;
5533 }
5534 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 }
5536
5537 if (evaluate)
5538 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005539 n1 = 0;
5540 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 n1 = get_tv_number(&var1);
5543 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 }
5545 if (range)
5546 {
5547 if (empty2)
5548 n2 = -1;
5549 else
5550 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005551 n2 = get_tv_number(&var2);
5552 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 }
5554 }
5555
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005558 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005559 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005560 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005561 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005562 case VAR_SPECIAL:
5563 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005564 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005565 break; /* not evaluating, skipping over subscript */
5566
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 case VAR_NUMBER:
5568 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 len = (long)STRLEN(s);
5571 if (range)
5572 {
5573 /* The resulting variable is a substring. If the indexes
5574 * are out of range the result is empty. */
5575 if (n1 < 0)
5576 {
5577 n1 = len + n1;
5578 if (n1 < 0)
5579 n1 = 0;
5580 }
5581 if (n2 < 0)
5582 n2 = len + n2;
5583 else if (n2 >= len)
5584 n2 = len;
5585 if (n1 >= len || n2 < 0 || n1 > n2)
5586 s = NULL;
5587 else
5588 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5589 }
5590 else
5591 {
5592 /* The resulting variable is a string of a single
5593 * character. If the index is too big or negative the
5594 * result is empty. */
5595 if (n1 >= len || n1 < 0)
5596 s = NULL;
5597 else
5598 s = vim_strnsave(s + n1, 1);
5599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600 clear_tv(rettv);
5601 rettv->v_type = VAR_STRING;
5602 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603 break;
5604
5605 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005606 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005607 if (n1 < 0)
5608 n1 = len + n1;
5609 if (!empty1 && (n1 < 0 || n1 >= len))
5610 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005611 /* For a range we allow invalid values and return an empty
5612 * list. A list index out of range is an error. */
5613 if (!range)
5614 {
5615 if (verbose)
5616 EMSGN(_(e_listidx), n1);
5617 return FAIL;
5618 }
5619 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 }
5621 if (range)
5622 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005623 list_T *l;
5624 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625
5626 if (n2 < 0)
5627 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005628 else if (n2 >= len)
5629 n2 = len - 1;
5630 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005631 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 l = list_alloc();
5633 if (l == NULL)
5634 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005636 n1 <= n2; ++n1)
5637 {
5638 if (list_append_tv(l, &item->li_tv) == FAIL)
5639 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005640 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005641 return FAIL;
5642 }
5643 item = item->li_next;
5644 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005645 clear_tv(rettv);
5646 rettv->v_type = VAR_LIST;
5647 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005648 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649 }
5650 else
5651 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005652 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 clear_tv(rettv);
5654 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005655 }
5656 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005657
5658 case VAR_DICT:
5659 if (range)
5660 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005661 if (verbose)
5662 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005663 if (len == -1)
5664 clear_tv(&var1);
5665 return FAIL;
5666 }
5667 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005668 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669
5670 if (len == -1)
5671 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005672 key = get_tv_string_chk(&var1);
5673 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005675 clear_tv(&var1);
5676 return FAIL;
5677 }
5678 }
5679
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005680 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005682 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005683 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 if (len == -1)
5685 clear_tv(&var1);
5686 if (item == NULL)
5687 return FAIL;
5688
5689 copy_tv(&item->di_tv, &var1);
5690 clear_tv(rettv);
5691 *rettv = var1;
5692 }
5693 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694 }
5695 }
5696
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005697 return OK;
5698}
5699
5700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 * Get an option value.
5702 * "arg" points to the '&' or '+' before the option name.
5703 * "arg" is advanced to character after the option name.
5704 * Return OK or FAIL.
5705 */
5706 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005707get_option_tv(
5708 char_u **arg,
5709 typval_T *rettv, /* when NULL, only check if option exists */
5710 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 char_u *option_end;
5713 long numval;
5714 char_u *stringval;
5715 int opt_type;
5716 int c;
5717 int working = (**arg == '+'); /* has("+option") */
5718 int ret = OK;
5719 int opt_flags;
5720
5721 /*
5722 * Isolate the option name and find its value.
5723 */
5724 option_end = find_option_end(arg, &opt_flags);
5725 if (option_end == NULL)
5726 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005727 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 EMSG2(_("E112: Option name missing: %s"), *arg);
5729 return FAIL;
5730 }
5731
5732 if (!evaluate)
5733 {
5734 *arg = option_end;
5735 return OK;
5736 }
5737
5738 c = *option_end;
5739 *option_end = NUL;
5740 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005741 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
5743 if (opt_type == -3) /* invalid name */
5744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005745 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 EMSG2(_("E113: Unknown option: %s"), *arg);
5747 ret = FAIL;
5748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005749 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 if (opt_type == -2) /* hidden string option */
5752 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005753 rettv->v_type = VAR_STRING;
5754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
5756 else if (opt_type == -1) /* hidden number option */
5757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005758 rettv->v_type = VAR_NUMBER;
5759 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
5761 else if (opt_type == 1) /* number option */
5762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005763 rettv->v_type = VAR_NUMBER;
5764 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766 else /* string option */
5767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005768 rettv->v_type = VAR_STRING;
5769 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771 }
5772 else if (working && (opt_type == -2 || opt_type == -1))
5773 ret = FAIL;
5774
5775 *option_end = c; /* put back for error messages */
5776 *arg = option_end;
5777
5778 return ret;
5779}
5780
5781/*
5782 * Allocate a variable for a string constant.
5783 * Return OK or FAIL.
5784 */
5785 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005786get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 char_u *p;
5789 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 int extra = 0;
5791
5792 /*
5793 * Find the end of the string, skipping backslashed characters.
5794 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
5797 if (*p == '\\' && p[1] != NUL)
5798 {
5799 ++p;
5800 /* A "\<x>" form occupies at least 4 characters, and produces up
5801 * to 6 characters: reserve space for 2 extra */
5802 if (*p == '<')
5803 extra += 2;
5804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 }
5806
5807 if (*p != '"')
5808 {
5809 EMSG2(_("E114: Missing quote: %s"), *arg);
5810 return FAIL;
5811 }
5812
5813 /* If only parsing, set *arg and return here */
5814 if (!evaluate)
5815 {
5816 *arg = p + 1;
5817 return OK;
5818 }
5819
5820 /*
5821 * Copy the string into allocated memory, handling backslashed
5822 * characters.
5823 */
5824 name = alloc((unsigned)(p - *arg + extra));
5825 if (name == NULL)
5826 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 rettv->v_type = VAR_STRING;
5828 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
5832 if (*p == '\\')
5833 {
5834 switch (*++p)
5835 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 case 'b': *name++ = BS; ++p; break;
5837 case 'e': *name++ = ESC; ++p; break;
5838 case 'f': *name++ = FF; ++p; break;
5839 case 'n': *name++ = NL; ++p; break;
5840 case 'r': *name++ = CAR; ++p; break;
5841 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842
5843 case 'X': /* hex: "\x1", "\x12" */
5844 case 'x':
5845 case 'u': /* Unicode: "\u0023" */
5846 case 'U':
5847 if (vim_isxdigit(p[1]))
5848 {
5849 int n, nr;
5850 int c = toupper(*p);
5851
5852 if (c == 'X')
5853 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005854 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005856 else
5857 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 nr = 0;
5859 while (--n >= 0 && vim_isxdigit(p[1]))
5860 {
5861 ++p;
5862 nr = (nr << 4) + hex2nr(*p);
5863 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865#ifdef FEAT_MBYTE
5866 /* For "\u" store the number according to
5867 * 'encoding'. */
5868 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 else
5871#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 break;
5875
5876 /* octal: "\1", "\12", "\123" */
5877 case '0':
5878 case '1':
5879 case '2':
5880 case '3':
5881 case '4':
5882 case '5':
5883 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 case '7': *name = *p++ - '0';
5885 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 *name = (*name << 3) + *p++ - '0';
5888 if (*p >= '0' && *p <= '7')
5889 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 break;
5893
5894 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 if (extra != 0)
5897 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 break;
5900 }
5901 /* FALLTHROUGH */
5902
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 break;
5905 }
5906 }
5907 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 *arg = p + 1;
5913
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 return OK;
5915}
5916
5917/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 * Return OK or FAIL.
5920 */
5921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005922get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 int reduce = 0;
5927
5928 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005930 */
5931 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5932 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936 break;
5937 ++reduce;
5938 ++p;
5939 }
5940 }
5941
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005943 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005944 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005945 return FAIL;
5946 }
5947
Bram Moolenaar8c711452005-01-14 21:53:12 +00005948 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005949 if (!evaluate)
5950 {
5951 *arg = p + 1;
5952 return OK;
5953 }
5954
5955 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005956 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005957 */
5958 str = alloc((unsigned)((p - *arg) - reduce));
5959 if (str == NULL)
5960 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005961 rettv->v_type = VAR_STRING;
5962 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005963
Bram Moolenaar8c711452005-01-14 21:53:12 +00005964 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005966 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969 break;
5970 ++p;
5971 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005973 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005974 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005975 *arg = p + 1;
5976
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005977 return OK;
5978}
5979
Bram Moolenaarddecc252016-04-06 22:59:37 +02005980 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005981partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005982{
5983 int i;
5984
5985 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005986 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005987 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005988 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005989 func_unref(pt->pt_name);
5990 vim_free(pt->pt_name);
5991 vim_free(pt);
5992}
5993
5994/*
5995 * Unreference a closure: decrement the reference count and free it when it
5996 * becomes zero.
5997 */
5998 void
5999partial_unref(partial_T *pt)
6000{
6001 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006002 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006003}
6004
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006005/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006 * Allocate a variable for a List and fill it from "*arg".
6007 * Return OK or FAIL.
6008 */
6009 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006010get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011{
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 list_T *l = NULL;
6013 typval_T tv;
6014 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015
6016 if (evaluate)
6017 {
6018 l = list_alloc();
6019 if (l == NULL)
6020 return FAIL;
6021 }
6022
6023 *arg = skipwhite(*arg + 1);
6024 while (**arg != ']' && **arg != NUL)
6025 {
6026 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6027 goto failret;
6028 if (evaluate)
6029 {
6030 item = listitem_alloc();
6031 if (item != NULL)
6032 {
6033 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006034 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 list_append(l, item);
6036 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006037 else
6038 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 }
6040
6041 if (**arg == ']')
6042 break;
6043 if (**arg != ',')
6044 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006045 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 goto failret;
6047 }
6048 *arg = skipwhite(*arg + 1);
6049 }
6050
6051 if (**arg != ']')
6052 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006053 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054failret:
6055 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006056 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 return FAIL;
6058 }
6059
6060 *arg = skipwhite(*arg + 1);
6061 if (evaluate)
6062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006063 rettv->v_type = VAR_LIST;
6064 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 ++l->lv_refcount;
6066 }
6067
6068 return OK;
6069}
6070
6071/*
6072 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006073 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006075 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006076list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006078 list_T *l;
6079
6080 l = (list_T *)alloc_clear(sizeof(list_T));
6081 if (l != NULL)
6082 {
6083 /* Prepend the list to the list of lists for garbage collection. */
6084 if (first_list != NULL)
6085 first_list->lv_used_prev = l;
6086 l->lv_used_prev = NULL;
6087 l->lv_used_next = first_list;
6088 first_list = l;
6089 }
6090 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091}
6092
6093/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006094 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006095 * Returns OK or FAIL.
6096 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006098rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006099{
6100 list_T *l = list_alloc();
6101
6102 if (l == NULL)
6103 return FAIL;
6104
6105 rettv->vval.v_list = l;
6106 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006107 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006108 ++l->lv_refcount;
6109 return OK;
6110}
6111
6112/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006113 * Unreference a list: decrement the reference count and free it when it
6114 * becomes zero.
6115 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006117list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006118{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006119 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006120 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006121}
6122
6123/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006124 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006125 * Ignores the reference count.
6126 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006127 static void
6128list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129{
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006131
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006132 for (item = l->lv_first; item != NULL; item = l->lv_first)
6133 {
6134 /* Remove the item before deleting it. */
6135 l->lv_first = item->li_next;
6136 clear_tv(&item->li_tv);
6137 vim_free(item);
6138 }
6139}
6140
6141 static void
6142list_free_list(list_T *l)
6143{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006144 /* Remove the list from the list of lists for garbage collection. */
6145 if (l->lv_used_prev == NULL)
6146 first_list = l->lv_used_next;
6147 else
6148 l->lv_used_prev->lv_used_next = l->lv_used_next;
6149 if (l->lv_used_next != NULL)
6150 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6151
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006152 vim_free(l);
6153}
6154
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006155 void
6156list_free(list_T *l)
6157{
6158 if (!in_free_unref_items)
6159 {
6160 list_free_contents(l);
6161 list_free_list(l);
6162 }
6163}
6164
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165/*
6166 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006167 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006169 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006170listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171{
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173}
6174
6175/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006176 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006179listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006180{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006181 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006182 vim_free(item);
6183}
6184
6185/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006186 * Remove a list item from a List and free it. Also clears the value.
6187 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006188 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006189listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006190{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006191 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006192 listitem_free(item);
6193}
6194
6195/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006196 * Get the number of items in a list.
6197 */
6198 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006199list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201 if (l == NULL)
6202 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006203 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204}
6205
6206/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006207 * Return TRUE when two lists have exactly the same values.
6208 */
6209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006210list_equal(
6211 list_T *l1,
6212 list_T *l2,
6213 int ic, /* ignore case for strings */
6214 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006215{
Bram Moolenaar33570922005-01-25 22:26:29 +00006216 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006217
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006218 if (l1 == NULL || l2 == NULL)
6219 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006220 if (l1 == l2)
6221 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006222 if (list_len(l1) != list_len(l2))
6223 return FALSE;
6224
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006225 for (item1 = l1->lv_first, item2 = l2->lv_first;
6226 item1 != NULL && item2 != NULL;
6227 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006228 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006229 return FALSE;
6230 return item1 == NULL && item2 == NULL;
6231}
6232
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006233/*
6234 * Return the dictitem that an entry in a hashtable points to.
6235 */
6236 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006237dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006238{
6239 return HI2DI(hi);
6240}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006241
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006243 * Return TRUE when two dictionaries have exactly the same key/values.
6244 */
6245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006246dict_equal(
6247 dict_T *d1,
6248 dict_T *d2,
6249 int ic, /* ignore case for strings */
6250 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006251{
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 hashitem_T *hi;
6253 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006254 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006255
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006256 if (d1 == NULL && d2 == NULL)
6257 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006258 if (d1 == NULL || d2 == NULL)
6259 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006260 if (d1 == d2)
6261 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006262 if (dict_len(d1) != dict_len(d2))
6263 return FALSE;
6264
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006265 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006266 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006267 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006268 if (!HASHITEM_EMPTY(hi))
6269 {
6270 item2 = dict_find(d2, hi->hi_key, -1);
6271 if (item2 == NULL)
6272 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006273 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006274 return FALSE;
6275 --todo;
6276 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006277 }
6278 return TRUE;
6279}
6280
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006281static int tv_equal_recurse_limit;
6282
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006283 static int
6284func_equal(
6285 typval_T *tv1,
6286 typval_T *tv2,
6287 int ic) /* ignore case */
6288{
6289 char_u *s1, *s2;
6290 dict_T *d1, *d2;
6291 int a1, a2;
6292 int i;
6293
6294 /* empty and NULL function name considered the same */
6295 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6296 : tv1->vval.v_partial->pt_name;
6297 if (s1 != NULL && *s1 == NUL)
6298 s1 = NULL;
6299 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6300 : tv2->vval.v_partial->pt_name;
6301 if (s2 != NULL && *s2 == NUL)
6302 s2 = NULL;
6303 if (s1 == NULL || s2 == NULL)
6304 {
6305 if (s1 != s2)
6306 return FALSE;
6307 }
6308 else if (STRCMP(s1, s2) != 0)
6309 return FALSE;
6310
6311 /* empty dict and NULL dict is different */
6312 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6313 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6314 if (d1 == NULL || d2 == NULL)
6315 {
6316 if (d1 != d2)
6317 return FALSE;
6318 }
6319 else if (!dict_equal(d1, d2, ic, TRUE))
6320 return FALSE;
6321
6322 /* empty list and no list considered the same */
6323 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6324 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6325 if (a1 != a2)
6326 return FALSE;
6327 for (i = 0; i < a1; ++i)
6328 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6329 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6330 return FALSE;
6331
6332 return TRUE;
6333}
6334
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006335/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006337 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006338 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006339 */
6340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006341tv_equal(
6342 typval_T *tv1,
6343 typval_T *tv2,
6344 int ic, /* ignore case */
6345 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006346{
6347 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006348 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006349 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006350 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006352 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006353 * recursiveness to a limit. We guess they are equal then.
6354 * A fixed limit has the problem of still taking an awful long time.
6355 * Reduce the limit every time running into it. That should work fine for
6356 * deeply linked structures that are not recursively linked and catch
6357 * recursiveness quickly. */
6358 if (!recursive)
6359 tv_equal_recurse_limit = 1000;
6360 if (recursive_cnt >= tv_equal_recurse_limit)
6361 {
6362 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006363 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006364 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006365
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006366 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6367 if ((tv1->v_type == VAR_FUNC
6368 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6369 && (tv2->v_type == VAR_FUNC
6370 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6371 {
6372 ++recursive_cnt;
6373 r = func_equal(tv1, tv2, ic);
6374 --recursive_cnt;
6375 return r;
6376 }
6377
6378 if (tv1->v_type != tv2->v_type)
6379 return FALSE;
6380
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006381 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006382 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006383 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006384 ++recursive_cnt;
6385 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6386 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006387 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006388
6389 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006390 ++recursive_cnt;
6391 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6392 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006393 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006394
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006395 case VAR_NUMBER:
6396 return tv1->vval.v_number == tv2->vval.v_number;
6397
6398 case VAR_STRING:
6399 s1 = get_tv_string_buf(tv1, buf1);
6400 s2 = get_tv_string_buf(tv2, buf2);
6401 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006402
6403 case VAR_SPECIAL:
6404 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006405
6406 case VAR_FLOAT:
6407#ifdef FEAT_FLOAT
6408 return tv1->vval.v_float == tv2->vval.v_float;
6409#endif
6410 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006411#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006412 return tv1->vval.v_job == tv2->vval.v_job;
6413#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006414 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006415#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006416 return tv1->vval.v_channel == tv2->vval.v_channel;
6417#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006418 case VAR_FUNC:
6419 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006420 case VAR_UNKNOWN:
6421 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006423
Bram Moolenaara03f2332016-02-06 18:09:59 +01006424 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6425 * does not equal anything, not even itself. */
6426 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427}
6428
6429/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 * Locate item with index "n" in list "l" and return it.
6431 * A negative index is counted from the end; -1 is the last item.
6432 * Returns NULL when "n" is out of range.
6433 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006434 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006435list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436{
Bram Moolenaar33570922005-01-25 22:26:29 +00006437 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006438 long idx;
6439
6440 if (l == NULL)
6441 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006442
6443 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006444 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006445 n = l->lv_len + n;
6446
6447 /* Check for index out of range. */
6448 if (n < 0 || n >= l->lv_len)
6449 return NULL;
6450
6451 /* When there is a cached index may start search from there. */
6452 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006454 if (n < l->lv_idx / 2)
6455 {
6456 /* closest to the start of the list */
6457 item = l->lv_first;
6458 idx = 0;
6459 }
6460 else if (n > (l->lv_idx + l->lv_len) / 2)
6461 {
6462 /* closest to the end of the list */
6463 item = l->lv_last;
6464 idx = l->lv_len - 1;
6465 }
6466 else
6467 {
6468 /* closest to the cached index */
6469 item = l->lv_idx_item;
6470 idx = l->lv_idx;
6471 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006472 }
6473 else
6474 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006475 if (n < l->lv_len / 2)
6476 {
6477 /* closest to the start of the list */
6478 item = l->lv_first;
6479 idx = 0;
6480 }
6481 else
6482 {
6483 /* closest to the end of the list */
6484 item = l->lv_last;
6485 idx = l->lv_len - 1;
6486 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006488
6489 while (n > idx)
6490 {
6491 /* search forward */
6492 item = item->li_next;
6493 ++idx;
6494 }
6495 while (n < idx)
6496 {
6497 /* search backward */
6498 item = item->li_prev;
6499 --idx;
6500 }
6501
6502 /* cache the used index */
6503 l->lv_idx = idx;
6504 l->lv_idx_item = item;
6505
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506 return item;
6507}
6508
6509/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006510 * Get list item "l[idx]" as a number.
6511 */
6512 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006513list_find_nr(
6514 list_T *l,
6515 long idx,
6516 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006517{
6518 listitem_T *li;
6519
6520 li = list_find(l, idx);
6521 if (li == NULL)
6522 {
6523 if (errorp != NULL)
6524 *errorp = TRUE;
6525 return -1L;
6526 }
6527 return get_tv_number_chk(&li->li_tv, errorp);
6528}
6529
6530/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006531 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6532 */
6533 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006535{
6536 listitem_T *li;
6537
6538 li = list_find(l, idx - 1);
6539 if (li == NULL)
6540 {
6541 EMSGN(_(e_listidx), idx);
6542 return NULL;
6543 }
6544 return get_tv_string(&li->li_tv);
6545}
6546
6547/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006548 * Locate "item" list "l" and return its index.
6549 * Returns -1 when "item" is not in the list.
6550 */
6551 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006552list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006553{
6554 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006556
6557 if (l == NULL)
6558 return -1;
6559 idx = 0;
6560 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6561 ++idx;
6562 if (li == NULL)
6563 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006564 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006565}
6566
6567/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 * Append item "item" to the end of list "l".
6569 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006570 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006571list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572{
6573 if (l->lv_last == NULL)
6574 {
6575 /* empty list */
6576 l->lv_first = item;
6577 l->lv_last = item;
6578 item->li_prev = NULL;
6579 }
6580 else
6581 {
6582 l->lv_last->li_next = item;
6583 item->li_prev = l->lv_last;
6584 l->lv_last = item;
6585 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006586 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 item->li_next = NULL;
6588}
6589
6590/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006594 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006595list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006596{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006597 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598
Bram Moolenaar05159a02005-02-26 23:04:13 +00006599 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006601 copy_tv(tv, &li->li_tv);
6602 list_append(l, li);
6603 return OK;
6604}
6605
6606/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006607 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006608 * Return FAIL when out of memory.
6609 */
6610 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006611list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006612{
6613 listitem_T *li = listitem_alloc();
6614
6615 if (li == NULL)
6616 return FAIL;
6617 li->li_tv.v_type = VAR_DICT;
6618 li->li_tv.v_lock = 0;
6619 li->li_tv.vval.v_dict = dict;
6620 list_append(list, li);
6621 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622 return OK;
6623}
6624
6625/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006626 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006627 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006628 * Returns FAIL when out of memory.
6629 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006631list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006632{
6633 listitem_T *li = listitem_alloc();
6634
6635 if (li == NULL)
6636 return FAIL;
6637 list_append(l, li);
6638 li->li_tv.v_type = VAR_STRING;
6639 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006640 if (str == NULL)
6641 li->li_tv.vval.v_string = NULL;
6642 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006643 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006644 return FAIL;
6645 return OK;
6646}
6647
6648/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006649 * Append "n" to list "l".
6650 * Returns FAIL when out of memory.
6651 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006652 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006653list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006654{
6655 listitem_T *li;
6656
6657 li = listitem_alloc();
6658 if (li == NULL)
6659 return FAIL;
6660 li->li_tv.v_type = VAR_NUMBER;
6661 li->li_tv.v_lock = 0;
6662 li->li_tv.vval.v_number = n;
6663 list_append(l, li);
6664 return OK;
6665}
6666
6667/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006668 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006669 * If "item" is NULL append at the end.
6670 * Return FAIL when out of memory.
6671 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006672 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006673list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006674{
Bram Moolenaar33570922005-01-25 22:26:29 +00006675 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006676
6677 if (ni == NULL)
6678 return FAIL;
6679 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006680 list_insert(l, ni, item);
6681 return OK;
6682}
6683
6684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006686{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006687 if (item == NULL)
6688 /* Append new item at end of list. */
6689 list_append(l, ni);
6690 else
6691 {
6692 /* Insert new item before existing item. */
6693 ni->li_prev = item->li_prev;
6694 ni->li_next = item;
6695 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006696 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006697 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006698 ++l->lv_idx;
6699 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006700 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006701 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006702 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006703 l->lv_idx_item = NULL;
6704 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006705 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006706 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006707 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006708}
6709
6710/*
6711 * Extend "l1" with "l2".
6712 * If "bef" is NULL append at the end, otherwise insert before this item.
6713 * Returns FAIL when out of memory.
6714 */
6715 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006716list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006717{
Bram Moolenaar33570922005-01-25 22:26:29 +00006718 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006719 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006720
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006721 /* We also quit the loop when we have inserted the original item count of
6722 * the list, avoid a hang when we extend a list with itself. */
6723 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006724 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6725 return FAIL;
6726 return OK;
6727}
6728
6729/*
6730 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6731 * Return FAIL when out of memory.
6732 */
6733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006734list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006735{
Bram Moolenaar33570922005-01-25 22:26:29 +00006736 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006737
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006738 if (l1 == NULL || l2 == NULL)
6739 return FAIL;
6740
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006742 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006743 if (l == NULL)
6744 return FAIL;
6745 tv->v_type = VAR_LIST;
6746 tv->vval.v_list = l;
6747
6748 /* append all items from the second list */
6749 return list_extend(l, l2, NULL);
6750}
6751
6752/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006753 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006754 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006755 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006756 * Returns NULL when out of memory.
6757 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006758 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006759list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006760{
Bram Moolenaar33570922005-01-25 22:26:29 +00006761 list_T *copy;
6762 listitem_T *item;
6763 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006764
6765 if (orig == NULL)
6766 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006767
6768 copy = list_alloc();
6769 if (copy != NULL)
6770 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006771 if (copyID != 0)
6772 {
6773 /* Do this before adding the items, because one of the items may
6774 * refer back to this list. */
6775 orig->lv_copyID = copyID;
6776 orig->lv_copylist = copy;
6777 }
6778 for (item = orig->lv_first; item != NULL && !got_int;
6779 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780 {
6781 ni = listitem_alloc();
6782 if (ni == NULL)
6783 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006784 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006785 {
6786 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6787 {
6788 vim_free(ni);
6789 break;
6790 }
6791 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006792 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006793 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006794 list_append(copy, ni);
6795 }
6796 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006797 if (item != NULL)
6798 {
6799 list_unref(copy);
6800 copy = NULL;
6801 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006802 }
6803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006804 return copy;
6805}
6806
6807/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006808 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006809 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006810 * This used to be called list_remove, but that conflicts with a Sun header
6811 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006812 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006814vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006815{
Bram Moolenaar33570922005-01-25 22:26:29 +00006816 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006817
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006818 /* notify watchers */
6819 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006820 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006821 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006822 list_fix_watch(l, ip);
6823 if (ip == item2)
6824 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006826
6827 if (item2->li_next == NULL)
6828 l->lv_last = item->li_prev;
6829 else
6830 item2->li_next->li_prev = item->li_prev;
6831 if (item->li_prev == NULL)
6832 l->lv_first = item2->li_next;
6833 else
6834 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006835 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006836}
6837
6838/*
6839 * Return an allocated string with the string representation of a list.
6840 * May return NULL.
6841 */
6842 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006843list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006844{
6845 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006846
6847 if (tv->vval.v_list == NULL)
6848 return NULL;
6849 ga_init2(&ga, (int)sizeof(char), 80);
6850 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006851 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6852 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006853 {
6854 vim_free(ga.ga_data);
6855 return NULL;
6856 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006857 ga_append(&ga, ']');
6858 ga_append(&ga, NUL);
6859 return (char_u *)ga.ga_data;
6860}
6861
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006862typedef struct join_S {
6863 char_u *s;
6864 char_u *tofree;
6865} join_T;
6866
6867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006868list_join_inner(
6869 garray_T *gap, /* to store the result in */
6870 list_T *l,
6871 char_u *sep,
6872 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006873 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006874 int copyID,
6875 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006876{
6877 int i;
6878 join_T *p;
6879 int len;
6880 int sumlen = 0;
6881 int first = TRUE;
6882 char_u *tofree;
6883 char_u numbuf[NUMBUFLEN];
6884 listitem_T *item;
6885 char_u *s;
6886
6887 /* Stringify each item in the list. */
6888 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6889 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006890 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6891 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006892 if (s == NULL)
6893 return FAIL;
6894
6895 len = (int)STRLEN(s);
6896 sumlen += len;
6897
Bram Moolenaarcde88542015-08-11 19:14:00 +02006898 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006899 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6900 if (tofree != NULL || s != numbuf)
6901 {
6902 p->s = s;
6903 p->tofree = tofree;
6904 }
6905 else
6906 {
6907 p->s = vim_strnsave(s, len);
6908 p->tofree = p->s;
6909 }
6910
6911 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006912 if (did_echo_string_emsg) /* recursion error, bail out */
6913 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006914 }
6915
6916 /* Allocate result buffer with its total size, avoid re-allocation and
6917 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6918 if (join_gap->ga_len >= 2)
6919 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6920 if (ga_grow(gap, sumlen + 2) == FAIL)
6921 return FAIL;
6922
6923 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6924 {
6925 if (first)
6926 first = FALSE;
6927 else
6928 ga_concat(gap, sep);
6929 p = ((join_T *)join_gap->ga_data) + i;
6930
6931 if (p->s != NULL)
6932 ga_concat(gap, p->s);
6933 line_breakcheck();
6934 }
6935
6936 return OK;
6937}
6938
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006939/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006940 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006941 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006942 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006943 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006945list_join(
6946 garray_T *gap,
6947 list_T *l,
6948 char_u *sep,
6949 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006950 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006951 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006952{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006953 garray_T join_ga;
6954 int retval;
6955 join_T *p;
6956 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006957
Bram Moolenaard39a7512015-04-16 22:51:22 +02006958 if (l->lv_len < 1)
6959 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006960 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006961 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6962 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006963
6964 /* Dispose each item in join_ga. */
6965 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006966 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006967 p = (join_T *)join_ga.ga_data;
6968 for (i = 0; i < join_ga.ga_len; ++i)
6969 {
6970 vim_free(p->tofree);
6971 ++p;
6972 }
6973 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006974 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006975
6976 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006977}
6978
6979/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006980 * Return the next (unique) copy ID.
6981 * Used for serializing nested structures.
6982 */
6983 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006984get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006985{
6986 current_copyID += COPYID_INC;
6987 return current_copyID;
6988}
6989
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006990/* Used by get_func_tv() */
6991static garray_T funcargs = GA_EMPTY;
6992
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006993/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 * Garbage collection for lists and dictionaries.
6995 *
6996 * We use reference counts to be able to free most items right away when they
6997 * are no longer used. But for composite items it's possible that it becomes
6998 * unused while the reference count is > 0: When there is a recursive
6999 * reference. Example:
7000 * :let l = [1, 2, 3]
7001 * :let d = {9: l}
7002 * :let l[1] = d
7003 *
7004 * Since this is quite unusual we handle this with garbage collection: every
7005 * once in a while find out which lists and dicts are not referenced from any
7006 * variable.
7007 *
7008 * Here is a good reference text about garbage collection (refers to Python
7009 * but it applies to all reference-counting mechanisms):
7010 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007011 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007012
7013/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007015 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007017 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007019garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007020{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007021 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007022 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 buf_T *buf;
7024 win_T *wp;
7025 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007026 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007027 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007028 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007029#ifdef FEAT_WINDOWS
7030 tabpage_T *tp;
7031#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007033 if (!testing)
7034 {
7035 /* Only do this once. */
7036 want_garbage_collect = FALSE;
7037 may_garbage_collect = FALSE;
7038 garbage_collect_at_exit = FALSE;
7039 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007040
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007041 /* We advance by two because we add one for items referenced through
7042 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007043 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007044
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 /*
7046 * 1. Go through all accessible variables and mark all lists and dicts
7047 * with copyID.
7048 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007049
7050 /* Don't free variables in the previous_funccal list unless they are only
7051 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007052 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007053 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7054 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007055 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7056 NULL);
7057 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7058 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007059 }
7060
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007061 /* script-local variables */
7062 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007063 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064
7065 /* buffer-local variables */
7066 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7068 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069
7070 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007071 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7073 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007074#ifdef FEAT_AUTOCMD
7075 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7077 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007078#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007080#ifdef FEAT_WINDOWS
7081 /* tabpage-local variables */
7082 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7084 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007085#endif
7086
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007089
7090 /* function-local variables */
7091 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7092 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007093 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7094 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095 }
7096
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007097 /* function call arguments, if v:testing is set. */
7098 for (i = 0; i < funcargs.ga_len; ++i)
7099 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7100 copyID, NULL, NULL);
7101
Bram Moolenaard812df62008-11-09 12:46:09 +00007102 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007104
Bram Moolenaar1dced572012-04-05 16:54:08 +02007105#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007106 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007107#endif
7108
Bram Moolenaardb913952012-06-29 12:54:53 +02007109#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007111#endif
7112
7113#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007114 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007115#endif
7116
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007117#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007118 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007119 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007120#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007121#ifdef FEAT_NETBEANS_INTG
7122 abort = abort || set_ref_in_nb_channel(copyID);
7123#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007124
Bram Moolenaare3188e22016-05-31 21:13:04 +02007125#ifdef FEAT_TIMERS
7126 abort = abort || set_ref_in_timer(copyID);
7127#endif
7128
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007129 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007130 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 /*
7132 * 2. Free lists and dictionaries that are not referenced.
7133 */
7134 did_free = free_unref_items(copyID);
7135
7136 /*
7137 * 3. Check if any funccal can be freed now.
7138 */
7139 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007140 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007141 if (can_free_funccal(*pfc, copyID))
7142 {
7143 fc = *pfc;
7144 *pfc = fc->caller;
7145 free_funccal(fc, TRUE);
7146 did_free = TRUE;
7147 did_free_funccal = TRUE;
7148 }
7149 else
7150 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007151 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007152 if (did_free_funccal)
7153 /* When a funccal was freed some more items might be garbage
7154 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007155 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007156 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007157 else if (p_verbose > 0)
7158 {
7159 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7160 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007161
7162 return did_free;
7163}
7164
7165/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007166 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007167 */
7168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007169free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007170{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007171 dict_T *dd, *dd_next;
7172 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007173 int did_free = FALSE;
7174
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007175 /* Let all "free" functions know that we are here. This means no
7176 * dictionaries, lists, channels or jobs are to be freed, because we will
7177 * do that here. */
7178 in_free_unref_items = TRUE;
7179
7180 /*
7181 * PASS 1: free the contents of the items. We don't free the items
7182 * themselves yet, so that it is possible to decrement refcount counters
7183 */
7184
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007186 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007188 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007189 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007190 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007191 /* Free the Dictionary and ordinary items it contains, but don't
7192 * recurse into Lists and Dictionaries, they will be in the list
7193 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007194 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007195 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007196 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007197
7198 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007199 * Go through the list of lists and free items without the copyID.
7200 * But don't free a list that has a watcher (used in a for loop), these
7201 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007202 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007203 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7204 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7205 && ll->lv_watch == NULL)
7206 {
7207 /* Free the List and ordinary items it contains, but don't recurse
7208 * into Lists and Dictionaries, they will be in the list of dicts
7209 * or list of lists. */
7210 list_free_contents(ll);
7211 did_free = TRUE;
7212 }
7213
7214#ifdef FEAT_JOB_CHANNEL
7215 /* Go through the list of jobs and free items without the copyID. This
7216 * must happen before doing channels, because jobs refer to channels, but
7217 * the reference from the channel to the job isn't tracked. */
7218 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7219
7220 /* Go through the list of channels and free items without the copyID. */
7221 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7222#endif
7223
7224 /*
7225 * PASS 2: free the items themselves.
7226 */
7227 for (dd = first_dict; dd != NULL; dd = dd_next)
7228 {
7229 dd_next = dd->dv_used_next;
7230 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7231 dict_free_dict(dd);
7232 }
7233
7234 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007235 {
7236 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007237 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7238 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007239 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007240 /* Free the List and ordinary items it contains, but don't recurse
7241 * into Lists and Dictionaries, they will be in the list of dicts
7242 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007243 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007244 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007245 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007246
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007247#ifdef FEAT_JOB_CHANNEL
7248 /* Go through the list of jobs and free items without the copyID. This
7249 * must happen before doing channels, because jobs refer to channels, but
7250 * the reference from the channel to the job isn't tracked. */
7251 free_unused_jobs(copyID, COPYID_MASK);
7252
7253 /* Go through the list of channels and free items without the copyID. */
7254 free_unused_channels(copyID, COPYID_MASK);
7255#endif
7256
7257 in_free_unref_items = FALSE;
7258
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007259 return did_free;
7260}
7261
7262/*
7263 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007264 * "list_stack" is used to add lists to be marked. Can be NULL.
7265 *
7266 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007267 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007268 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007269set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007270{
7271 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007272 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007273 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007274 hashtab_T *cur_ht;
7275 ht_stack_T *ht_stack = NULL;
7276 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007277
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007278 cur_ht = ht;
7279 for (;;)
7280 {
7281 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007282 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007283 /* Mark each item in the hashtab. If the item contains a hashtab
7284 * it is added to ht_stack, if it contains a list it is added to
7285 * list_stack. */
7286 todo = (int)cur_ht->ht_used;
7287 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7288 if (!HASHITEM_EMPTY(hi))
7289 {
7290 --todo;
7291 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7292 &ht_stack, list_stack);
7293 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007294 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007295
7296 if (ht_stack == NULL)
7297 break;
7298
7299 /* take an item from the stack */
7300 cur_ht = ht_stack->ht;
7301 tempitem = ht_stack;
7302 ht_stack = ht_stack->prev;
7303 free(tempitem);
7304 }
7305
7306 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007307}
7308
7309/*
7310 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007311 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7312 *
7313 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007314 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007315 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007316set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007317{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007318 listitem_T *li;
7319 int abort = FALSE;
7320 list_T *cur_l;
7321 list_stack_T *list_stack = NULL;
7322 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007323
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007324 cur_l = l;
7325 for (;;)
7326 {
7327 if (!abort)
7328 /* Mark each item in the list. If the item contains a hashtab
7329 * it is added to ht_stack, if it contains a list it is added to
7330 * list_stack. */
7331 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7332 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7333 ht_stack, &list_stack);
7334 if (list_stack == NULL)
7335 break;
7336
7337 /* take an item from the stack */
7338 cur_l = list_stack->list;
7339 tempitem = list_stack;
7340 list_stack = list_stack->prev;
7341 free(tempitem);
7342 }
7343
7344 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007345}
7346
7347/*
7348 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007349 * "list_stack" is used to add lists to be marked. Can be NULL.
7350 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7351 *
7352 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007353 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007354 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007355set_ref_in_item(
7356 typval_T *tv,
7357 int copyID,
7358 ht_stack_T **ht_stack,
7359 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007360{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007361 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007362
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007363 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007364 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007365 dict_T *dd = tv->vval.v_dict;
7366
Bram Moolenaara03f2332016-02-06 18:09:59 +01007367 if (dd != NULL && dd->dv_copyID != copyID)
7368 {
7369 /* Didn't see this dict yet. */
7370 dd->dv_copyID = copyID;
7371 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007372 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007373 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7374 }
7375 else
7376 {
7377 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7378 if (newitem == NULL)
7379 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007380 else
7381 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007382 newitem->ht = &dd->dv_hashtab;
7383 newitem->prev = *ht_stack;
7384 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007385 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007386 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007387 }
7388 }
7389 else if (tv->v_type == VAR_LIST)
7390 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007391 list_T *ll = tv->vval.v_list;
7392
Bram Moolenaara03f2332016-02-06 18:09:59 +01007393 if (ll != NULL && ll->lv_copyID != copyID)
7394 {
7395 /* Didn't see this list yet. */
7396 ll->lv_copyID = copyID;
7397 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007398 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007399 abort = set_ref_in_list(ll, copyID, ht_stack);
7400 }
7401 else
7402 {
7403 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007404 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007405 if (newitem == NULL)
7406 abort = TRUE;
7407 else
7408 {
7409 newitem->list = ll;
7410 newitem->prev = *list_stack;
7411 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007412 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007413 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007414 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007415 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007416 else if (tv->v_type == VAR_PARTIAL)
7417 {
7418 partial_T *pt = tv->vval.v_partial;
7419 int i;
7420
7421 /* A partial does not have a copyID, because it cannot contain itself.
7422 */
7423 if (pt != NULL)
7424 {
7425 if (pt->pt_dict != NULL)
7426 {
7427 typval_T dtv;
7428
7429 dtv.v_type = VAR_DICT;
7430 dtv.vval.v_dict = pt->pt_dict;
7431 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7432 }
7433
7434 for (i = 0; i < pt->pt_argc; ++i)
7435 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7436 ht_stack, list_stack);
7437 }
7438 }
7439#ifdef FEAT_JOB_CHANNEL
7440 else if (tv->v_type == VAR_JOB)
7441 {
7442 job_T *job = tv->vval.v_job;
7443 typval_T dtv;
7444
7445 if (job != NULL && job->jv_copyID != copyID)
7446 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007447 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007448 if (job->jv_channel != NULL)
7449 {
7450 dtv.v_type = VAR_CHANNEL;
7451 dtv.vval.v_channel = job->jv_channel;
7452 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7453 }
7454 if (job->jv_exit_partial != NULL)
7455 {
7456 dtv.v_type = VAR_PARTIAL;
7457 dtv.vval.v_partial = job->jv_exit_partial;
7458 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7459 }
7460 }
7461 }
7462 else if (tv->v_type == VAR_CHANNEL)
7463 {
7464 channel_T *ch =tv->vval.v_channel;
7465 int part;
7466 typval_T dtv;
7467 jsonq_T *jq;
7468 cbq_T *cq;
7469
7470 if (ch != NULL && ch->ch_copyID != copyID)
7471 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007472 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007473 for (part = PART_SOCK; part <= PART_IN; ++part)
7474 {
7475 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7476 jq = jq->jq_next)
7477 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7478 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7479 cq = cq->cq_next)
7480 if (cq->cq_partial != NULL)
7481 {
7482 dtv.v_type = VAR_PARTIAL;
7483 dtv.vval.v_partial = cq->cq_partial;
7484 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7485 }
7486 if (ch->ch_part[part].ch_partial != NULL)
7487 {
7488 dtv.v_type = VAR_PARTIAL;
7489 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7490 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7491 }
7492 }
7493 if (ch->ch_partial != NULL)
7494 {
7495 dtv.v_type = VAR_PARTIAL;
7496 dtv.vval.v_partial = ch->ch_partial;
7497 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7498 }
7499 if (ch->ch_close_partial != NULL)
7500 {
7501 dtv.v_type = VAR_PARTIAL;
7502 dtv.vval.v_partial = ch->ch_close_partial;
7503 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7504 }
7505 }
7506 }
7507#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007508 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007509}
7510
7511/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512 * Allocate an empty header for a dictionary.
7513 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007514 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007515dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516{
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518
Bram Moolenaar33570922005-01-25 22:26:29 +00007519 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007520 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007521 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007522 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007523 if (first_dict != NULL)
7524 first_dict->dv_used_prev = d;
7525 d->dv_used_next = first_dict;
7526 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007527 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007528
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007530 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007531 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007532 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007533 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007534 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536}
7537
7538/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007539 * Allocate an empty dict for a return value.
7540 * Returns OK or FAIL.
7541 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007542 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007543rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007544{
7545 dict_T *d = dict_alloc();
7546
7547 if (d == NULL)
7548 return FAIL;
7549
7550 rettv->vval.v_dict = d;
7551 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007552 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007553 ++d->dv_refcount;
7554 return OK;
7555}
7556
7557
7558/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 * Unreference a Dictionary: decrement the reference count and free it when it
7560 * becomes zero.
7561 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007562 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007563dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007565 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007566 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567}
7568
7569/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007570 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 * Ignores the reference count.
7572 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007573 static void
7574dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007576 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007578 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007580 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007581 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007582 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007585 if (!HASHITEM_EMPTY(hi))
7586 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007587 /* Remove the item before deleting it, just in case there is
7588 * something recursive causing trouble. */
7589 di = HI2DI(hi);
7590 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007591 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007592 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007593 --todo;
7594 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007596 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007597}
7598
7599 static void
7600dict_free_dict(dict_T *d)
7601{
7602 /* Remove the dict from the list of dicts for garbage collection. */
7603 if (d->dv_used_prev == NULL)
7604 first_dict = d->dv_used_next;
7605 else
7606 d->dv_used_prev->dv_used_next = d->dv_used_next;
7607 if (d->dv_used_next != NULL)
7608 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 vim_free(d);
7610}
7611
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007612 void
7613dict_free(dict_T *d)
7614{
7615 if (!in_free_unref_items)
7616 {
7617 dict_free_contents(d);
7618 dict_free_dict(d);
7619 }
7620}
7621
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622/*
7623 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007624 * The "key" is copied to the new item.
7625 * Note that the value of the item "di_tv" still needs to be initialized!
7626 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007628 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007629dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630{
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007632
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007633 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007637 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007638 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007639 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640}
7641
7642/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 * Make a copy of a Dictionary item.
7644 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007645 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007646dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647{
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007649
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007650 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7651 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007652 if (di != NULL)
7653 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007654 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007655 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007656 copy_tv(&org->di_tv, &di->di_tv);
7657 }
7658 return di;
7659}
7660
7661/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007662 * Remove item "item" from Dictionary "dict" and free it.
7663 */
7664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007665dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007666{
Bram Moolenaar33570922005-01-25 22:26:29 +00007667 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007668
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007670 if (HASHITEM_EMPTY(hi))
7671 EMSG2(_(e_intern2), "dictitem_remove()");
7672 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007674 dictitem_free(item);
7675}
7676
7677/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 * Free a dict item. Also clears the value.
7679 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007681dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007684 if (item->di_flags & DI_FLAGS_ALLOC)
7685 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686}
7687
7688/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7690 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007691 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692 * Returns NULL when out of memory.
7693 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007695dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007696{
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 dict_T *copy;
7698 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007701
7702 if (orig == NULL)
7703 return NULL;
7704
7705 copy = dict_alloc();
7706 if (copy != NULL)
7707 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007708 if (copyID != 0)
7709 {
7710 orig->dv_copyID = copyID;
7711 orig->dv_copydict = copy;
7712 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007713 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007714 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007715 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007716 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007717 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007718 --todo;
7719
7720 di = dictitem_alloc(hi->hi_key);
7721 if (di == NULL)
7722 break;
7723 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007724 {
7725 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7726 copyID) == FAIL)
7727 {
7728 vim_free(di);
7729 break;
7730 }
7731 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007732 else
7733 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7734 if (dict_add(copy, di) == FAIL)
7735 {
7736 dictitem_free(di);
7737 break;
7738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007739 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007740 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007741
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007743 if (todo > 0)
7744 {
7745 dict_unref(copy);
7746 copy = NULL;
7747 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007748 }
7749
7750 return copy;
7751}
7752
7753/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007754 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007755 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007757 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007758dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759{
Bram Moolenaar33570922005-01-25 22:26:29 +00007760 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007761}
7762
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007764 * Add a number or string entry to dictionary "d".
7765 * When "str" is NULL use number "nr", otherwise use "str".
7766 * Returns FAIL when out of memory and when key already exists.
7767 */
7768 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007769dict_add_nr_str(
7770 dict_T *d,
7771 char *key,
7772 long nr,
7773 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007774{
7775 dictitem_T *item;
7776
7777 item = dictitem_alloc((char_u *)key);
7778 if (item == NULL)
7779 return FAIL;
7780 item->di_tv.v_lock = 0;
7781 if (str == NULL)
7782 {
7783 item->di_tv.v_type = VAR_NUMBER;
7784 item->di_tv.vval.v_number = nr;
7785 }
7786 else
7787 {
7788 item->di_tv.v_type = VAR_STRING;
7789 item->di_tv.vval.v_string = vim_strsave(str);
7790 }
7791 if (dict_add(d, item) == FAIL)
7792 {
7793 dictitem_free(item);
7794 return FAIL;
7795 }
7796 return OK;
7797}
7798
7799/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007800 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007801 * Returns FAIL when out of memory and when key already exists.
7802 */
7803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007804dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007805{
7806 dictitem_T *item;
7807
7808 item = dictitem_alloc((char_u *)key);
7809 if (item == NULL)
7810 return FAIL;
7811 item->di_tv.v_lock = 0;
7812 item->di_tv.v_type = VAR_LIST;
7813 item->di_tv.vval.v_list = list;
7814 if (dict_add(d, item) == FAIL)
7815 {
7816 dictitem_free(item);
7817 return FAIL;
7818 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007819 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007820 return OK;
7821}
7822
7823/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 * Get the number of items in a Dictionary.
7825 */
7826 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007827dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829 if (d == NULL)
7830 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007831 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832}
7833
7834/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007835 * Find item "key[len]" in Dictionary "d".
7836 * If "len" is negative use strlen(key).
7837 * Returns NULL when not found.
7838 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007839 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007840dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007841{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007842#define AKEYLEN 200
7843 char_u buf[AKEYLEN];
7844 char_u *akey;
7845 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007846 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007847
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007848 if (d == NULL)
7849 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007850 if (len < 0)
7851 akey = key;
7852 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007853 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007854 tofree = akey = vim_strnsave(key, len);
7855 if (akey == NULL)
7856 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007857 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007858 else
7859 {
7860 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007861 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007862 akey = buf;
7863 }
7864
Bram Moolenaar33570922005-01-25 22:26:29 +00007865 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007866 vim_free(tofree);
7867 if (HASHITEM_EMPTY(hi))
7868 return NULL;
7869 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007870}
7871
7872/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007873 * Get a string item from a dictionary.
7874 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007875 * Returns NULL if the entry doesn't exist or out of memory.
7876 */
7877 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007878get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007879{
7880 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007881 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007882
7883 di = dict_find(d, key, -1);
7884 if (di == NULL)
7885 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007886 s = get_tv_string(&di->di_tv);
7887 if (save && s != NULL)
7888 s = vim_strsave(s);
7889 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007890}
7891
7892/*
7893 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007894 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007895 */
7896 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007897get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007898{
7899 dictitem_T *di;
7900
7901 di = dict_find(d, key, -1);
7902 if (di == NULL)
7903 return 0;
7904 return get_tv_number(&di->di_tv);
7905}
7906
7907/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007908 * Return an allocated string with the string representation of a Dictionary.
7909 * May return NULL.
7910 */
7911 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007912dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913{
7914 garray_T ga;
7915 int first = TRUE;
7916 char_u *tofree;
7917 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007918 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007919 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007920 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007921 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007922
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007923 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007924 return NULL;
7925 ga_init2(&ga, (int)sizeof(char), 80);
7926 ga_append(&ga, '{');
7927
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007928 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007929 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007930 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007931 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007933 --todo;
7934
7935 if (first)
7936 first = FALSE;
7937 else
7938 ga_concat(&ga, (char_u *)", ");
7939
7940 tofree = string_quote(hi->hi_key, FALSE);
7941 if (tofree != NULL)
7942 {
7943 ga_concat(&ga, tofree);
7944 vim_free(tofree);
7945 }
7946 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007947 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7948 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007949 if (s != NULL)
7950 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007951 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007952 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007953 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007954 line_breakcheck();
7955
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007957 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007958 if (todo > 0)
7959 {
7960 vim_free(ga.ga_data);
7961 return NULL;
7962 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007963
7964 ga_append(&ga, '}');
7965 ga_append(&ga, NUL);
7966 return (char_u *)ga.ga_data;
7967}
7968
7969/*
7970 * Allocate a variable for a Dictionary and fill it from "*arg".
7971 * Return OK or FAIL. Returns NOTDONE for {expr}.
7972 */
7973 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007974get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007975{
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 dict_T *d = NULL;
7977 typval_T tvkey;
7978 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007979 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007982 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007983
7984 /*
7985 * First check if it's not a curly-braces thing: {expr}.
7986 * Must do this without evaluating, otherwise a function may be called
7987 * twice. Unfortunately this means we need to call eval1() twice for the
7988 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007989 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007990 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007991 if (*start != '}')
7992 {
7993 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7994 return FAIL;
7995 if (*start == '}')
7996 return NOTDONE;
7997 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007998
7999 if (evaluate)
8000 {
8001 d = dict_alloc();
8002 if (d == NULL)
8003 return FAIL;
8004 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008005 tvkey.v_type = VAR_UNKNOWN;
8006 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008007
8008 *arg = skipwhite(*arg + 1);
8009 while (**arg != '}' && **arg != NUL)
8010 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008011 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008012 goto failret;
8013 if (**arg != ':')
8014 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008015 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008016 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008017 goto failret;
8018 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008019 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008020 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008021 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008022 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008023 {
8024 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008025 clear_tv(&tvkey);
8026 goto failret;
8027 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008028 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008029
8030 *arg = skipwhite(*arg + 1);
8031 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8032 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008033 if (evaluate)
8034 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008035 goto failret;
8036 }
8037 if (evaluate)
8038 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008039 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008040 if (item != NULL)
8041 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008042 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008043 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008044 clear_tv(&tv);
8045 goto failret;
8046 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008047 item = dictitem_alloc(key);
8048 clear_tv(&tvkey);
8049 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008050 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008051 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008052 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008053 if (dict_add(d, item) == FAIL)
8054 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008055 }
8056 }
8057
8058 if (**arg == '}')
8059 break;
8060 if (**arg != ',')
8061 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008062 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008063 goto failret;
8064 }
8065 *arg = skipwhite(*arg + 1);
8066 }
8067
8068 if (**arg != '}')
8069 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008070 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008071failret:
8072 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008073 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008074 return FAIL;
8075 }
8076
8077 *arg = skipwhite(*arg + 1);
8078 if (evaluate)
8079 {
8080 rettv->v_type = VAR_DICT;
8081 rettv->vval.v_dict = d;
8082 ++d->dv_refcount;
8083 }
8084
8085 return OK;
8086}
8087
Bram Moolenaar17a13432016-01-24 14:22:10 +01008088 static char *
8089get_var_special_name(int nr)
8090{
8091 switch (nr)
8092 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008093 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008094 case VVAL_TRUE: return "v:true";
8095 case VVAL_NONE: return "v:none";
8096 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008097 }
8098 EMSG2(_(e_intern2), "get_var_special_name()");
8099 return "42";
8100}
8101
Bram Moolenaar8c711452005-01-14 21:53:12 +00008102/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008103 * Return a string with the string representation of a variable.
8104 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008105 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008106 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008107 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8108 * "string()", otherwise does not put quotes around strings, as ":echo"
8109 * displays values.
8110 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8111 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008112 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008113 */
8114 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008115echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008116 typval_T *tv,
8117 char_u **tofree,
8118 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008119 int copyID,
8120 int echo_style,
8121 int restore_copyID,
8122 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008123{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008124 static int recurse = 0;
8125 char_u *r = NULL;
8126
Bram Moolenaar33570922005-01-25 22:26:29 +00008127 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008128 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008129 if (!did_echo_string_emsg)
8130 {
8131 /* Only give this message once for a recursive call to avoid
8132 * flooding the user with errors. And stop iterating over lists
8133 * and dicts. */
8134 did_echo_string_emsg = TRUE;
8135 EMSG(_("E724: variable nested too deep for displaying"));
8136 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008137 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008138 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008139 }
8140 ++recurse;
8141
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008142 switch (tv->v_type)
8143 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008144 case VAR_STRING:
8145 if (echo_style && !dict_val)
8146 {
8147 *tofree = NULL;
8148 r = get_tv_string_buf(tv, numbuf);
8149 }
8150 else
8151 {
8152 *tofree = string_quote(tv->vval.v_string, FALSE);
8153 r = *tofree;
8154 }
8155 break;
8156
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008157 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008158 if (echo_style)
8159 {
8160 *tofree = NULL;
8161 r = tv->vval.v_string;
8162 }
8163 else
8164 {
8165 *tofree = string_quote(tv->vval.v_string, TRUE);
8166 r = *tofree;
8167 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008168 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008169
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008170 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008171 {
8172 partial_T *pt = tv->vval.v_partial;
8173 char_u *fname = string_quote(pt == NULL ? NULL
8174 : pt->pt_name, FALSE);
8175 garray_T ga;
8176 int i;
8177 char_u *tf;
8178
8179 ga_init2(&ga, 1, 100);
8180 ga_concat(&ga, (char_u *)"function(");
8181 if (fname != NULL)
8182 {
8183 ga_concat(&ga, fname);
8184 vim_free(fname);
8185 }
8186 if (pt != NULL && pt->pt_argc > 0)
8187 {
8188 ga_concat(&ga, (char_u *)", [");
8189 for (i = 0; i < pt->pt_argc; ++i)
8190 {
8191 if (i > 0)
8192 ga_concat(&ga, (char_u *)", ");
8193 ga_concat(&ga,
8194 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8195 vim_free(tf);
8196 }
8197 ga_concat(&ga, (char_u *)"]");
8198 }
8199 if (pt != NULL && pt->pt_dict != NULL)
8200 {
8201 typval_T dtv;
8202
8203 ga_concat(&ga, (char_u *)", ");
8204 dtv.v_type = VAR_DICT;
8205 dtv.vval.v_dict = pt->pt_dict;
8206 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8207 vim_free(tf);
8208 }
8209 ga_concat(&ga, (char_u *)")");
8210
8211 *tofree = ga.ga_data;
8212 r = *tofree;
8213 break;
8214 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008215
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008216 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008217 if (tv->vval.v_list == NULL)
8218 {
8219 *tofree = NULL;
8220 r = NULL;
8221 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008222 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8223 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008224 {
8225 *tofree = NULL;
8226 r = (char_u *)"[...]";
8227 }
8228 else
8229 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008230 int old_copyID = tv->vval.v_list->lv_copyID;
8231
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008232 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008233 *tofree = list2string(tv, copyID, restore_copyID);
8234 if (restore_copyID)
8235 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008236 r = *tofree;
8237 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008238 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008239
Bram Moolenaar8c711452005-01-14 21:53:12 +00008240 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008241 if (tv->vval.v_dict == NULL)
8242 {
8243 *tofree = NULL;
8244 r = NULL;
8245 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008246 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8247 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008248 {
8249 *tofree = NULL;
8250 r = (char_u *)"{...}";
8251 }
8252 else
8253 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008254 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008255 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008256 *tofree = dict2string(tv, copyID, restore_copyID);
8257 if (restore_copyID)
8258 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008259 r = *tofree;
8260 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008261 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008262
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008263 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008264 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008265 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008266 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008267 *tofree = NULL;
8268 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008271 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008272#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273 *tofree = NULL;
8274 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8275 r = numbuf;
8276 break;
8277#endif
8278
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008279 case VAR_SPECIAL:
8280 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008281 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008282 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008283 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008284
Bram Moolenaar8502c702014-06-17 12:51:16 +02008285 if (--recurse == 0)
8286 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008287 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008288}
8289
8290/*
8291 * Return a string with the string representation of a variable.
8292 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8293 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008294 * Does not put quotes around strings, as ":echo" displays values.
8295 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8296 * May return NULL.
8297 */
8298 static char_u *
8299echo_string(
8300 typval_T *tv,
8301 char_u **tofree,
8302 char_u *numbuf,
8303 int copyID)
8304{
8305 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8306}
8307
8308/*
8309 * Return a string with the string representation of a variable.
8310 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8311 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008312 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008313 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008314 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008315 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008316tv2string(
8317 typval_T *tv,
8318 char_u **tofree,
8319 char_u *numbuf,
8320 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008321{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008322 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008323}
8324
8325/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008326 * Return string "str" in ' quotes, doubling ' characters.
8327 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008328 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008329 */
8330 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008331string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008332{
Bram Moolenaar33570922005-01-25 22:26:29 +00008333 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008334 char_u *p, *r, *s;
8335
Bram Moolenaar33570922005-01-25 22:26:29 +00008336 len = (function ? 13 : 3);
8337 if (str != NULL)
8338 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008339 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008340 for (p = str; *p != NUL; mb_ptr_adv(p))
8341 if (*p == '\'')
8342 ++len;
8343 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008344 s = r = alloc(len);
8345 if (r != NULL)
8346 {
8347 if (function)
8348 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008349 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008350 r += 10;
8351 }
8352 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008353 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008354 if (str != NULL)
8355 for (p = str; *p != NUL; )
8356 {
8357 if (*p == '\'')
8358 *r++ = '\'';
8359 MB_COPY_CHAR(p, r);
8360 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008361 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008362 if (function)
8363 *r++ = ')';
8364 *r++ = NUL;
8365 }
8366 return s;
8367}
8368
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008369#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370/*
8371 * Convert the string "text" to a floating point number.
8372 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8373 * this always uses a decimal point.
8374 * Returns the length of the text that was consumed.
8375 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008376 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008377string2float(
8378 char_u *text,
8379 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008380{
8381 char *s = (char *)text;
8382 float_T f;
8383
8384 f = strtod(s, &s);
8385 *value = f;
8386 return (int)((char_u *)s - text);
8387}
8388#endif
8389
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 * Get the value of an environment variable.
8392 * "arg" is pointing to the '$'. It is advanced to after the name.
8393 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008394 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 */
8396 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008397get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398{
8399 char_u *string = NULL;
8400 int len;
8401 int cc;
8402 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008403 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
8405 ++*arg;
8406 name = *arg;
8407 len = get_env_len(arg);
8408 if (evaluate)
8409 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008410 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008411 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008412
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008413 cc = name[len];
8414 name[len] = NUL;
8415 /* first try vim_getenv(), fast for normal environment vars */
8416 string = vim_getenv(name, &mustfree);
8417 if (string != NULL && *string != NUL)
8418 {
8419 if (!mustfree)
8420 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008422 else
8423 {
8424 if (mustfree)
8425 vim_free(string);
8426
8427 /* next try expanding things like $VIM and ${HOME} */
8428 string = expand_env_save(name - 1);
8429 if (string != NULL && *string == '$')
8430 {
8431 vim_free(string);
8432 string = NULL;
8433 }
8434 }
8435 name[len] = cc;
8436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008437 rettv->v_type = VAR_STRING;
8438 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 }
8440
8441 return OK;
8442}
8443
8444/*
8445 * Array with names and number of arguments of all internal functions
8446 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8447 */
8448static struct fst
8449{
8450 char *f_name; /* function name */
8451 char f_min_argc; /* minimal number of arguments */
8452 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008453 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008454 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455} functions[] =
8456{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008457#ifdef FEAT_FLOAT
8458 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008459 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008460#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008461 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008462 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {"append", 2, 2, f_append},
8464 {"argc", 0, 0, f_argc},
8465 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008466 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008467 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008468#ifdef FEAT_FLOAT
8469 {"asin", 1, 1, f_asin}, /* WJMc */
8470#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008471 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008472 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008473 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008474 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008475 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008476 {"assert_notequal", 2, 3, f_assert_notequal},
8477 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008478 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008479#ifdef FEAT_FLOAT
8480 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008481 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008484 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 {"bufexists", 1, 1, f_bufexists},
8486 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8487 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8488 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8489 {"buflisted", 1, 1, f_buflisted},
8490 {"bufloaded", 1, 1, f_bufloaded},
8491 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008492 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008493 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {"bufwinnr", 1, 1, f_bufwinnr},
8495 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008496 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008497 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008498 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008499#ifdef FEAT_FLOAT
8500 {"ceil", 1, 1, f_ceil},
8501#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008502#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008503 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008504 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8505 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008506 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008507 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008508 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008509 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008510 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008511 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008512 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008513 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008514 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8515 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008516 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008517 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008518#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008519 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008520 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008522 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008524#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008525 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008526 {"complete_add", 1, 1, f_complete_add},
8527 {"complete_check", 0, 0, f_complete_check},
8528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008531#ifdef FEAT_FLOAT
8532 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008533 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008534#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008535 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008537 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008538 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008539 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008541 {"diff_filler", 1, 1, f_diff_filler},
8542 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008543 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008545 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {"eventhandler", 0, 0, f_eventhandler},
8547 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008548 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008550#ifdef FEAT_FLOAT
8551 {"exp", 1, 1, f_exp},
8552#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008553 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008554 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008555 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8557 {"filereadable", 1, 1, f_filereadable},
8558 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008559 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008560 {"finddir", 1, 3, f_finddir},
8561 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008562#ifdef FEAT_FLOAT
8563 {"float2nr", 1, 1, f_float2nr},
8564 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008565 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008566#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008567 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {"fnamemodify", 2, 2, f_fnamemodify},
8569 {"foldclosed", 1, 1, f_foldclosed},
8570 {"foldclosedend", 1, 1, f_foldclosedend},
8571 {"foldlevel", 1, 1, f_foldlevel},
8572 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008573 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008575 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008576 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008577 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008578 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008579 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 {"getchar", 0, 1, f_getchar},
8581 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008582 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 {"getcmdline", 0, 0, f_getcmdline},
8584 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008585 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008586 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008587 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008588 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008589 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008590 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 {"getfsize", 1, 1, f_getfsize},
8592 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008593 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008594 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008595 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008596 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008597 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008598 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008599 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008600 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008602 {"gettabvar", 2, 3, f_gettabvar},
8603 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 {"getwinposx", 0, 0, f_getwinposx},
8605 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008606 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008607 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008608 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008609 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008611 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008612 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008613 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8615 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8616 {"histadd", 2, 2, f_histadd},
8617 {"histdel", 1, 2, f_histdel},
8618 {"histget", 1, 2, f_histget},
8619 {"histnr", 1, 1, f_histnr},
8620 {"hlID", 1, 1, f_hlID},
8621 {"hlexists", 1, 1, f_hlexists},
8622 {"hostname", 0, 0, f_hostname},
8623 {"iconv", 3, 3, f_iconv},
8624 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008625 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008626 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008628 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 {"inputrestore", 0, 0, f_inputrestore},
8630 {"inputsave", 0, 0, f_inputsave},
8631 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008632 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008633 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008635 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008636#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8637 {"isnan", 1, 1, f_isnan},
8638#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008639 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008640#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008641 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008642 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008643 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008644 {"job_start", 1, 2, f_job_start},
8645 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008646 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008647#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008648 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008649 {"js_decode", 1, 1, f_js_decode},
8650 {"js_encode", 1, 1, f_js_encode},
8651 {"json_decode", 1, 1, f_json_decode},
8652 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008653 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008655 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {"libcall", 3, 3, f_libcall},
8657 {"libcallnr", 3, 3, f_libcallnr},
8658 {"line", 1, 1, f_line},
8659 {"line2byte", 1, 1, f_line2byte},
8660 {"lispindent", 1, 1, f_lispindent},
8661 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008662#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008663 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008664 {"log10", 1, 1, f_log10},
8665#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008666#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008667 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008668#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008669 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008670 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008671 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008672 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008673 {"matchadd", 2, 5, f_matchadd},
8674 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008675 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008676 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008677 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008678 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008679 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008680 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008681 {"max", 1, 1, f_max},
8682 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008683#ifdef vim_mkdir
8684 {"mkdir", 1, 3, f_mkdir},
8685#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008687#ifdef FEAT_MZSCHEME
8688 {"mzeval", 1, 1, f_mzeval},
8689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008691 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008692 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008693 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008694#ifdef FEAT_PERL
8695 {"perleval", 1, 1, f_perleval},
8696#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008697#ifdef FEAT_FLOAT
8698 {"pow", 2, 2, f_pow},
8699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008701 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008702 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008703#ifdef FEAT_PYTHON3
8704 {"py3eval", 1, 1, f_py3eval},
8705#endif
8706#ifdef FEAT_PYTHON
8707 {"pyeval", 1, 1, f_pyeval},
8708#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008709 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008710 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008711 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008712#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008713 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008714#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008715 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 {"remote_expr", 2, 3, f_remote_expr},
8717 {"remote_foreground", 1, 1, f_remote_foreground},
8718 {"remote_peek", 1, 2, f_remote_peek},
8719 {"remote_read", 1, 1, f_remote_read},
8720 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008721 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008723 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008725 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008726#ifdef FEAT_FLOAT
8727 {"round", 1, 1, f_round},
8728#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008729 {"screenattr", 2, 2, f_screenattr},
8730 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008731 {"screencol", 0, 0, f_screencol},
8732 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008733 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008734 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008735 {"searchpair", 3, 7, f_searchpair},
8736 {"searchpairpos", 3, 7, f_searchpairpos},
8737 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {"server2client", 2, 2, f_server2client},
8739 {"serverlist", 0, 0, f_serverlist},
8740 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008741 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008743 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008745 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008746 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008747 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008748 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008750 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008751 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008753#ifdef FEAT_CRYPT
8754 {"sha256", 1, 1, f_sha256},
8755#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008756 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008757 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008759#ifdef FEAT_FLOAT
8760 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008761 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008762#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008763 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008764 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008765 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008766 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008767 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008768#ifdef FEAT_FLOAT
8769 {"sqrt", 1, 1, f_sqrt},
8770 {"str2float", 1, 1, f_str2float},
8771#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008772 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008773 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008774 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008775 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776#ifdef HAVE_STRFTIME
8777 {"strftime", 1, 2, f_strftime},
8778#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008779 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008780 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008781 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782 {"strlen", 1, 1, f_strlen},
8783 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008784 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008786 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008787 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 {"substitute", 4, 4, f_substitute},
8789 {"synID", 3, 3, f_synID},
8790 {"synIDattr", 2, 3, f_synIDattr},
8791 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008792 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008793 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008794 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008795 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008796 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008797 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008798 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008799 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008800 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008801#ifdef FEAT_FLOAT
8802 {"tan", 1, 1, f_tan},
8803 {"tanh", 1, 1, f_tanh},
8804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008806 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8807 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008808 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8809#ifdef FEAT_JOB_CHANNEL
8810 {"test_null_channel", 0, 0, f_test_null_channel},
8811#endif
8812 {"test_null_dict", 0, 0, f_test_null_dict},
8813#ifdef FEAT_JOB_CHANNEL
8814 {"test_null_job", 0, 0, f_test_null_job},
8815#endif
8816 {"test_null_list", 0, 0, f_test_null_list},
8817 {"test_null_partial", 0, 0, f_test_null_partial},
8818 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008819#ifdef FEAT_TIMERS
8820 {"timer_start", 2, 3, f_timer_start},
8821 {"timer_stop", 1, 1, f_timer_stop},
8822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 {"tolower", 1, 1, f_tolower},
8824 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008825 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008826#ifdef FEAT_FLOAT
8827 {"trunc", 1, 1, f_trunc},
8828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008830 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008831 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008832 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008833 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {"virtcol", 1, 1, f_virtcol},
8835 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008836 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008837 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008838 {"win_getid", 0, 2, f_win_getid},
8839 {"win_gotoid", 1, 1, f_win_gotoid},
8840 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8841 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 {"winbufnr", 1, 1, f_winbufnr},
8843 {"wincol", 0, 0, f_wincol},
8844 {"winheight", 1, 1, f_winheight},
8845 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008846 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008848 {"winrestview", 1, 1, f_winrestview},
8849 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008851 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008852 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008853 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854};
8855
8856#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8857
8858/*
8859 * Function given to ExpandGeneric() to obtain the list of internal
8860 * or user defined function names.
8861 */
8862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008863get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864{
8865 static int intidx = -1;
8866 char_u *name;
8867
8868 if (idx == 0)
8869 intidx = -1;
8870 if (intidx < 0)
8871 {
8872 name = get_user_func_name(xp, idx);
8873 if (name != NULL)
8874 return name;
8875 }
8876 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8877 {
8878 STRCPY(IObuff, functions[intidx].f_name);
8879 STRCAT(IObuff, "(");
8880 if (functions[intidx].f_max_argc == 0)
8881 STRCAT(IObuff, ")");
8882 return IObuff;
8883 }
8884
8885 return NULL;
8886}
8887
8888/*
8889 * Function given to ExpandGeneric() to obtain the list of internal or
8890 * user defined variable or function names.
8891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008893get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895 static int intidx = -1;
8896 char_u *name;
8897
8898 if (idx == 0)
8899 intidx = -1;
8900 if (intidx < 0)
8901 {
8902 name = get_function_name(xp, idx);
8903 if (name != NULL)
8904 return name;
8905 }
8906 return get_user_var_name(xp, ++intidx);
8907}
8908
8909#endif /* FEAT_CMDL_COMPL */
8910
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008911#if defined(EBCDIC) || defined(PROTO)
8912/*
8913 * Compare struct fst by function name.
8914 */
8915 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008916compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008917{
8918 struct fst *p1 = (struct fst *)s1;
8919 struct fst *p2 = (struct fst *)s2;
8920
8921 return STRCMP(p1->f_name, p2->f_name);
8922}
8923
8924/*
8925 * Sort the function table by function name.
8926 * The sorting of the table above is ASCII dependant.
8927 * On machines using EBCDIC we have to sort it.
8928 */
8929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008930sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008931{
8932 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8933
8934 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8935}
8936#endif
8937
8938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939/*
8940 * Find internal function in table above.
8941 * Return index, or -1 if not found
8942 */
8943 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008944find_internal_func(
8945 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
8947 int first = 0;
8948 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8949 int cmp;
8950 int x;
8951
8952 /*
8953 * Find the function name in the table. Binary search.
8954 */
8955 while (first <= last)
8956 {
8957 x = first + ((unsigned)(last - first) >> 1);
8958 cmp = STRCMP(name, functions[x].f_name);
8959 if (cmp < 0)
8960 last = x - 1;
8961 else if (cmp > 0)
8962 first = x + 1;
8963 else
8964 return x;
8965 }
8966 return -1;
8967}
8968
8969/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008970 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8971 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008972 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8973 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008974 */
8975 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008976deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008977{
Bram Moolenaar33570922005-01-25 22:26:29 +00008978 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008979 int cc;
8980
Bram Moolenaar65639032016-03-16 21:40:30 +01008981 if (partialp != NULL)
8982 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008983
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008984 cc = name[*lenp];
8985 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008986 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008987 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008989 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008990 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008991 {
8992 *lenp = 0;
8993 return (char_u *)""; /* just in case */
8994 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008995 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008997 }
8998
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008999 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
9000 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009001 partial_T *pt = v->di_tv.vval.v_partial;
9002
9003 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009004 {
9005 *lenp = 0;
9006 return (char_u *)""; /* just in case */
9007 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009008 if (partialp != NULL)
9009 *partialp = pt;
9010 *lenp = (int)STRLEN(pt->pt_name);
9011 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009012 }
9013
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009014 return name;
9015}
9016
9017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 * Allocate a variable for the result of a function.
9019 * Return OK or FAIL.
9020 */
9021 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009022get_func_tv(
9023 char_u *name, /* name of the function */
9024 int len, /* length of "name" */
9025 typval_T *rettv,
9026 char_u **arg, /* argument, pointing to the '(' */
9027 linenr_T firstline, /* first line of range */
9028 linenr_T lastline, /* last line of range */
9029 int *doesrange, /* return: function handled range */
9030 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009031 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009032 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
9034 char_u *argp;
9035 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009036 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 int argcount = 0; /* number of arguments found */
9038
9039 /*
9040 * Get the arguments.
9041 */
9042 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009043 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 {
9045 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9046 if (*argp == ')' || *argp == ',' || *argp == NUL)
9047 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9049 {
9050 ret = FAIL;
9051 break;
9052 }
9053 ++argcount;
9054 if (*argp != ',')
9055 break;
9056 }
9057 if (*argp == ')')
9058 ++argp;
9059 else
9060 ret = FAIL;
9061
9062 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009063 {
9064 int i = 0;
9065
9066 if (get_vim_var_nr(VV_TESTING))
9067 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009068 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009069 * what variables are used on the call stack. */
9070 if (funcargs.ga_itemsize == 0)
9071 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9072 for (i = 0; i < argcount; ++i)
9073 if (ga_grow(&funcargs, 1) == OK)
9074 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9075 &argvars[i];
9076 }
9077
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009078 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009079 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009080
9081 funcargs.ga_len -= i;
9082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 {
9085 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009086 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009087 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009088 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090
9091 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093
9094 *arg = skipwhite(argp);
9095 return ret;
9096}
9097
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009098#define ERROR_UNKNOWN 0
9099#define ERROR_TOOMANY 1
9100#define ERROR_TOOFEW 2
9101#define ERROR_SCRIPT 3
9102#define ERROR_DICT 4
9103#define ERROR_NONE 5
9104#define ERROR_OTHER 6
9105#define FLEN_FIXED 40
9106
9107/*
9108 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9109 * Change <SNR>123_name() to K_SNR 123_name().
9110 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9111 * (slow).
9112 */
9113 static char_u *
9114fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9115{
9116 int llen;
9117 char_u *fname;
9118 int i;
9119
9120 llen = eval_fname_script(name);
9121 if (llen > 0)
9122 {
9123 fname_buf[0] = K_SPECIAL;
9124 fname_buf[1] = KS_EXTRA;
9125 fname_buf[2] = (int)KE_SNR;
9126 i = 3;
9127 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9128 {
9129 if (current_SID <= 0)
9130 *error = ERROR_SCRIPT;
9131 else
9132 {
9133 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9134 i = (int)STRLEN(fname_buf);
9135 }
9136 }
9137 if (i + STRLEN(name + llen) < FLEN_FIXED)
9138 {
9139 STRCPY(fname_buf + i, name + llen);
9140 fname = fname_buf;
9141 }
9142 else
9143 {
9144 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9145 if (fname == NULL)
9146 *error = ERROR_OTHER;
9147 else
9148 {
9149 *tofree = fname;
9150 mch_memmove(fname, fname_buf, (size_t)i);
9151 STRCPY(fname + i, name + llen);
9152 }
9153 }
9154 }
9155 else
9156 fname = name;
9157 return fname;
9158}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159
9160/*
9161 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009162 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009163 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009165 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009166call_func(
9167 char_u *funcname, /* name of the function */
9168 int len, /* length of "name" */
9169 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009170 int argcount_in, /* number of "argvars" */
9171 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009172 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009173 linenr_T firstline, /* first line of range */
9174 linenr_T lastline, /* last line of range */
9175 int *doesrange, /* return: function handled range */
9176 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009177 partial_T *partial, /* optional, can be NULL */
9178 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179{
9180 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 int error = ERROR_NONE;
9182 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009185 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009187 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009188 int argcount = argcount_in;
9189 typval_T *argvars = argvars_in;
9190 dict_T *selfdict = selfdict_in;
9191 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9192 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009193
9194 /* Make a copy of the name, if it comes from a funcref variable it could
9195 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009196 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009197 if (name == NULL)
9198 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009200 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201
9202 *doesrange = FALSE;
9203
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009204 if (partial != NULL)
9205 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009206 /* When the function has a partial with a dict and there is a dict
9207 * argument, use the dict argument. That is backwards compatible.
9208 * When the dict was bound explicitly use the one from the partial. */
9209 if (partial->pt_dict != NULL
9210 && (selfdict_in == NULL || !partial->pt_auto))
9211 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009212 if (error == ERROR_NONE && partial->pt_argc > 0)
9213 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009214 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9215 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9216 for (i = 0; i < argcount_in; ++i)
9217 argv[i + argv_clear] = argvars_in[i];
9218 argvars = argv;
9219 argcount = partial->pt_argc + argcount_in;
9220 }
9221 }
9222
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223
9224 /* execute the function if no errors detected and executing */
9225 if (evaluate && error == ERROR_NONE)
9226 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009227 char_u *rfname = fname;
9228
9229 /* Ignore "g:" before a function name. */
9230 if (fname[0] == 'g' && fname[1] == ':')
9231 rfname = fname + 2;
9232
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009233 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9234 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 error = ERROR_UNKNOWN;
9236
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009237 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 {
9239 /*
9240 * User defined function.
9241 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009242 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009243
Bram Moolenaar071d4272004-06-13 20:20:40 +00009244#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009245 /* Trigger FuncUndefined event, may load the function. */
9246 if (fp == NULL
9247 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009248 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009249 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009251 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009252 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 }
9254#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009255 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009256 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009257 {
9258 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009259 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009260 }
9261
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 if (fp != NULL)
9263 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009264 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009266 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009268 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009270 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009271 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 else
9273 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009274 int did_save_redo = FALSE;
9275
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 /*
9277 * Call the user function.
9278 * Save and restore search patterns, script variables and
9279 * redo buffer.
9280 */
9281 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009282#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009283 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009284#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009285 {
9286 saveRedobuff();
9287 did_save_redo = TRUE;
9288 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009289 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009290 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009291 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009292 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9293 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9294 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009295 /* Function was unreferenced while being used, free it
9296 * now. */
9297 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009298 if (did_save_redo)
9299 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 restore_search_patterns();
9301 error = ERROR_NONE;
9302 }
9303 }
9304 }
9305 else
9306 {
9307 /*
9308 * Find the function name in the table, call its implementation.
9309 */
9310 i = find_internal_func(fname);
9311 if (i >= 0)
9312 {
9313 if (argcount < functions[i].f_min_argc)
9314 error = ERROR_TOOFEW;
9315 else if (argcount > functions[i].f_max_argc)
9316 error = ERROR_TOOMANY;
9317 else
9318 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009319 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009320 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 error = ERROR_NONE;
9322 }
9323 }
9324 }
9325 /*
9326 * The function call (or "FuncUndefined" autocommand sequence) might
9327 * have been aborted by an error, an interrupt, or an explicitly thrown
9328 * exception that has not been caught so far. This situation can be
9329 * tested for by calling aborting(). For an error in an internal
9330 * function or for the "E132" error in call_user_func(), however, the
9331 * throw point at which the "force_abort" flag (temporarily reset by
9332 * emsg()) is normally updated has not been reached yet. We need to
9333 * update that flag first to make aborting() reliable.
9334 */
9335 update_force_abort();
9336 }
9337 if (error == ERROR_NONE)
9338 ret = OK;
9339
9340 /*
9341 * Report an error unless the argument evaluation or function call has been
9342 * cancelled due to an aborting error, an interrupt, or an exception.
9343 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009344 if (!aborting())
9345 {
9346 switch (error)
9347 {
9348 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009349 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009350 break;
9351 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009352 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009353 break;
9354 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009355 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009356 name);
9357 break;
9358 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009359 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009360 name);
9361 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009362 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009363 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009364 name);
9365 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009366 }
9367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009369 while (argv_clear > 0)
9370 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009371 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009372 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373
9374 return ret;
9375}
9376
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009377/*
9378 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009379 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009380 */
9381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009382emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009383{
9384 char_u *p;
9385
9386 if (*name == K_SPECIAL)
9387 p = concat_str((char_u *)"<SNR>", name + 3);
9388 else
9389 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009390 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009391 if (p != name)
9392 vim_free(p);
9393}
9394
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009395/*
9396 * Return TRUE for a non-zero Number and a non-empty String.
9397 */
9398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009399non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009400{
9401 return ((argvars[0].v_type == VAR_NUMBER
9402 && argvars[0].vval.v_number != 0)
9403 || (argvars[0].v_type == VAR_STRING
9404 && argvars[0].vval.v_string != NULL
9405 && *argvars[0].vval.v_string != NUL));
9406}
9407
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408/*********************************************
9409 * Implementation of the built-in functions
9410 */
9411
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009412#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009413static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009414
9415/*
9416 * Get the float value of "argvars[0]" into "f".
9417 * Returns FAIL when the argument is not a Number or Float.
9418 */
9419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009420get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009421{
9422 if (argvars[0].v_type == VAR_FLOAT)
9423 {
9424 *f = argvars[0].vval.v_float;
9425 return OK;
9426 }
9427 if (argvars[0].v_type == VAR_NUMBER)
9428 {
9429 *f = (float_T)argvars[0].vval.v_number;
9430 return OK;
9431 }
9432 EMSG(_("E808: Number or Float required"));
9433 return FAIL;
9434}
9435
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009436/*
9437 * "abs(expr)" function
9438 */
9439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009440f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009441{
9442 if (argvars[0].v_type == VAR_FLOAT)
9443 {
9444 rettv->v_type = VAR_FLOAT;
9445 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9446 }
9447 else
9448 {
9449 varnumber_T n;
9450 int error = FALSE;
9451
9452 n = get_tv_number_chk(&argvars[0], &error);
9453 if (error)
9454 rettv->vval.v_number = -1;
9455 else if (n > 0)
9456 rettv->vval.v_number = n;
9457 else
9458 rettv->vval.v_number = -n;
9459 }
9460}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009461
9462/*
9463 * "acos()" function
9464 */
9465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009466f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009467{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009468 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009469
9470 rettv->v_type = VAR_FLOAT;
9471 if (get_float_arg(argvars, &f) == OK)
9472 rettv->vval.v_float = acos(f);
9473 else
9474 rettv->vval.v_float = 0.0;
9475}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009476#endif
9477
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009479 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 */
9481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009482f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
Bram Moolenaar33570922005-01-25 22:26:29 +00009484 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009486 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009487 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009489 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009490 && !tv_check_lock(l->lv_lock,
9491 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009492 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009494 }
9495 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009496 EMSG(_(e_listreq));
9497}
9498
9499/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009500 * "and(expr, expr)" function
9501 */
9502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009503f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009504{
9505 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9506 & get_tv_number_chk(&argvars[1], NULL);
9507}
9508
9509/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009510 * "append(lnum, string/list)" function
9511 */
9512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009513f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009514{
9515 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009516 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009517 list_T *l = NULL;
9518 listitem_T *li = NULL;
9519 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009520 long added = 0;
9521
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009522 /* When coming here from Insert mode, sync undo, so that this can be
9523 * undone separately from what was previously inserted. */
9524 if (u_sync_once == 2)
9525 {
9526 u_sync_once = 1; /* notify that u_sync() was called */
9527 u_sync(TRUE);
9528 }
9529
Bram Moolenaar0d660222005-01-07 21:51:51 +00009530 lnum = get_tv_lnum(argvars);
9531 if (lnum >= 0
9532 && lnum <= curbuf->b_ml.ml_line_count
9533 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009534 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009535 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009536 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009537 l = argvars[1].vval.v_list;
9538 if (l == NULL)
9539 return;
9540 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009541 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009542 for (;;)
9543 {
9544 if (l == NULL)
9545 tv = &argvars[1]; /* append a string */
9546 else if (li == NULL)
9547 break; /* end of list */
9548 else
9549 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 line = get_tv_string_chk(tv);
9551 if (line == NULL) /* type error */
9552 {
9553 rettv->vval.v_number = 1; /* Failed */
9554 break;
9555 }
9556 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009557 ++added;
9558 if (l == NULL)
9559 break;
9560 li = li->li_next;
9561 }
9562
9563 appended_lines_mark(lnum, added);
9564 if (curwin->w_cursor.lnum > lnum)
9565 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 else
9568 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
9571/*
9572 * "argc()" function
9573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009575f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578}
9579
9580/*
9581 * "argidx()" function
9582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009584f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587}
9588
9589/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009590 * "arglistid()" function
9591 */
9592 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009593f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009594{
9595 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009596
9597 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009598 wp = find_tabwin(&argvars[0], &argvars[1]);
9599 if (wp != NULL)
9600 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009601}
9602
9603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 * "argv(nr)" function
9605 */
9606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009607f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609 int idx;
9610
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009611 if (argvars[0].v_type != VAR_UNKNOWN)
9612 {
9613 idx = get_tv_number_chk(&argvars[0], NULL);
9614 if (idx >= 0 && idx < ARGCOUNT)
9615 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9616 else
9617 rettv->vval.v_string = NULL;
9618 rettv->v_type = VAR_STRING;
9619 }
9620 else if (rettv_list_alloc(rettv) == OK)
9621 for (idx = 0; idx < ARGCOUNT; ++idx)
9622 list_append_string(rettv->vval.v_list,
9623 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624}
9625
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009626typedef enum
9627{
9628 ASSERT_EQUAL,
9629 ASSERT_NOTEQUAL,
9630 ASSERT_MATCH,
9631 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009632 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009633} assert_type_T;
9634
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009635static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009636static 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 +01009637static void assert_error(garray_T *gap);
9638static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009639
9640/*
9641 * Prepare "gap" for an assert error and add the sourcing position.
9642 */
9643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009644prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009645{
9646 char buf[NUMBUFLEN];
9647
9648 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009649 if (sourcing_name != NULL)
9650 {
9651 ga_concat(gap, sourcing_name);
9652 if (sourcing_lnum > 0)
9653 ga_concat(gap, (char_u *)" ");
9654 }
9655 if (sourcing_lnum > 0)
9656 {
9657 sprintf(buf, "line %ld", (long)sourcing_lnum);
9658 ga_concat(gap, (char_u *)buf);
9659 }
9660 if (sourcing_name != NULL || sourcing_lnum > 0)
9661 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009662}
9663
9664/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009665 * Append "str" to "gap", escaping unprintable characters.
9666 * Changes NL to \n, CR to \r, etc.
9667 */
9668 static void
9669ga_concat_esc(garray_T *gap, char_u *str)
9670{
9671 char_u *p;
9672 char_u buf[NUMBUFLEN];
9673
Bram Moolenaarf1551962016-03-15 12:55:58 +01009674 if (str == NULL)
9675 {
9676 ga_concat(gap, (char_u *)"NULL");
9677 return;
9678 }
9679
Bram Moolenaar23689172016-02-15 22:37:37 +01009680 for (p = str; *p != NUL; ++p)
9681 switch (*p)
9682 {
9683 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9684 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9685 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9686 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9687 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9688 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9689 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9690 default:
9691 if (*p < ' ')
9692 {
9693 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9694 ga_concat(gap, buf);
9695 }
9696 else
9697 ga_append(gap, *p);
9698 break;
9699 }
9700}
9701
9702/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009703 * Fill "gap" with information about an assert error.
9704 */
9705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009706fill_assert_error(
9707 garray_T *gap,
9708 typval_T *opt_msg_tv,
9709 char_u *exp_str,
9710 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009711 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009712 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009713{
9714 char_u numbuf[NUMBUFLEN];
9715 char_u *tofree;
9716
9717 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9718 {
9719 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9720 vim_free(tofree);
9721 }
9722 else
9723 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009724 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009725 ga_concat(gap, (char_u *)"Pattern ");
9726 else
9727 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009728 if (exp_str == NULL)
9729 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009730 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009731 vim_free(tofree);
9732 }
9733 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009734 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009735 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009736 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009737 else if (atype == ASSERT_NOTMATCH)
9738 ga_concat(gap, (char_u *)" does match ");
9739 else if (atype == ASSERT_NOTEQUAL)
9740 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009741 else
9742 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009743 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009744 vim_free(tofree);
9745 }
9746}
Bram Moolenaar43345542015-11-29 17:35:35 +01009747
9748/*
9749 * Add an assert error to v:errors.
9750 */
9751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009752assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009753{
9754 struct vimvar *vp = &vimvars[VV_ERRORS];
9755
9756 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9757 /* Make sure v:errors is a list. */
9758 set_vim_var_list(VV_ERRORS, list_alloc());
9759 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9760}
9761
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009762 static void
9763assert_equal_common(typval_T *argvars, assert_type_T atype)
9764{
9765 garray_T ga;
9766
9767 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9768 != (atype == ASSERT_EQUAL))
9769 {
9770 prepare_assert_error(&ga);
9771 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9772 atype);
9773 assert_error(&ga);
9774 ga_clear(&ga);
9775 }
9776}
9777
Bram Moolenaar43345542015-11-29 17:35:35 +01009778/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009779 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009780 */
9781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009782f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009783{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009784 assert_equal_common(argvars, ASSERT_EQUAL);
9785}
Bram Moolenaar43345542015-11-29 17:35:35 +01009786
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009787/*
9788 * "assert_notequal(expected, actual[, msg])" function
9789 */
9790 static void
9791f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9792{
9793 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009794}
9795
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009796/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009797 * "assert_exception(string[, msg])" function
9798 */
9799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009800f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009801{
9802 garray_T ga;
9803 char *error;
9804
9805 error = (char *)get_tv_string_chk(&argvars[0]);
9806 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9807 {
9808 prepare_assert_error(&ga);
9809 ga_concat(&ga, (char_u *)"v:exception is not set");
9810 assert_error(&ga);
9811 ga_clear(&ga);
9812 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009813 else if (error != NULL
9814 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009815 {
9816 prepare_assert_error(&ga);
9817 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009818 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009819 assert_error(&ga);
9820 ga_clear(&ga);
9821 }
9822}
9823
9824/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009825 * "assert_fails(cmd [, error])" function
9826 */
9827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009828f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009829{
9830 char_u *cmd = get_tv_string_chk(&argvars[0]);
9831 garray_T ga;
9832
9833 called_emsg = FALSE;
9834 suppress_errthrow = TRUE;
9835 emsg_silent = TRUE;
9836 do_cmdline_cmd(cmd);
9837 if (!called_emsg)
9838 {
9839 prepare_assert_error(&ga);
9840 ga_concat(&ga, (char_u *)"command did not fail: ");
9841 ga_concat(&ga, cmd);
9842 assert_error(&ga);
9843 ga_clear(&ga);
9844 }
9845 else if (argvars[1].v_type != VAR_UNKNOWN)
9846 {
9847 char_u buf[NUMBUFLEN];
9848 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9849
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009850 if (error == NULL
9851 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009852 {
9853 prepare_assert_error(&ga);
9854 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009855 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009856 assert_error(&ga);
9857 ga_clear(&ga);
9858 }
9859 }
9860
9861 called_emsg = FALSE;
9862 suppress_errthrow = FALSE;
9863 emsg_silent = FALSE;
9864 emsg_on_display = FALSE;
9865 set_vim_var_string(VV_ERRMSG, NULL, 0);
9866}
9867
9868/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009869 * Common for assert_true() and assert_false().
9870 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009872assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009873{
9874 int error = FALSE;
9875 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009876
Bram Moolenaar37127922016-02-06 20:29:28 +01009877 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009878 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009879 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009880 if (argvars[0].v_type != VAR_NUMBER
9881 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9882 || error)
9883 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009884 prepare_assert_error(&ga);
9885 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009886 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009887 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009888 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009889 ga_clear(&ga);
9890 }
9891}
9892
9893/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009894 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009895 */
9896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009897f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009898{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009899 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009900}
9901
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009902 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009903assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009904{
9905 garray_T ga;
9906 char_u buf1[NUMBUFLEN];
9907 char_u buf2[NUMBUFLEN];
9908 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9909 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9910
Bram Moolenaar72188e92016-03-28 22:48:29 +02009911 if (pat == NULL || text == NULL)
9912 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009913 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009914 {
9915 prepare_assert_error(&ga);
9916 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009917 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009918 assert_error(&ga);
9919 ga_clear(&ga);
9920 }
9921}
9922
9923/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009924 * "assert_match(pattern, actual[, msg])" function
9925 */
9926 static void
9927f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9928{
9929 assert_match_common(argvars, ASSERT_MATCH);
9930}
9931
9932/*
9933 * "assert_notmatch(pattern, actual[, msg])" function
9934 */
9935 static void
9936f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9937{
9938 assert_match_common(argvars, ASSERT_NOTMATCH);
9939}
9940
9941/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009942 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009943 */
9944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009945f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009946{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009947 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009948}
9949
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009950#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009951/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009952 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009953 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009955f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009956{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009957 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009958
9959 rettv->v_type = VAR_FLOAT;
9960 if (get_float_arg(argvars, &f) == OK)
9961 rettv->vval.v_float = asin(f);
9962 else
9963 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009964}
9965
9966/*
9967 * "atan()" function
9968 */
9969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009970f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009971{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009972 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009973
9974 rettv->v_type = VAR_FLOAT;
9975 if (get_float_arg(argvars, &f) == OK)
9976 rettv->vval.v_float = atan(f);
9977 else
9978 rettv->vval.v_float = 0.0;
9979}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009980
9981/*
9982 * "atan2()" function
9983 */
9984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009985f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009986{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009987 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009988
9989 rettv->v_type = VAR_FLOAT;
9990 if (get_float_arg(argvars, &fx) == OK
9991 && get_float_arg(&argvars[1], &fy) == OK)
9992 rettv->vval.v_float = atan2(fx, fy);
9993 else
9994 rettv->vval.v_float = 0.0;
9995}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009996#endif
9997
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998/*
9999 * "browse(save, title, initdir, default)" function
10000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010002f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003{
10004#ifdef FEAT_BROWSE
10005 int save;
10006 char_u *title;
10007 char_u *initdir;
10008 char_u *defname;
10009 char_u buf[NUMBUFLEN];
10010 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010011 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010013 save = get_tv_number_chk(&argvars[0], &error);
10014 title = get_tv_string_chk(&argvars[1]);
10015 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10016 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 if (error || title == NULL || initdir == NULL || defname == NULL)
10019 rettv->vval.v_string = NULL;
10020 else
10021 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010022 do_browse(save ? BROWSE_SAVE : 0,
10023 title, defname, NULL, initdir, NULL, curbuf);
10024#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010026#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010028}
10029
10030/*
10031 * "browsedir(title, initdir)" function
10032 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010034f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010035{
10036#ifdef FEAT_BROWSE
10037 char_u *title;
10038 char_u *initdir;
10039 char_u buf[NUMBUFLEN];
10040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010041 title = get_tv_string_chk(&argvars[0]);
10042 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010044 if (title == NULL || initdir == NULL)
10045 rettv->vval.v_string = NULL;
10046 else
10047 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010048 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053}
10054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010055static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010056
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057/*
10058 * Find a buffer by number or exact name.
10059 */
10060 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010061find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062{
10063 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010065 if (avar->v_type == VAR_NUMBER)
10066 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010067 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010069 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010070 if (buf == NULL)
10071 {
10072 /* No full path name match, try a match with a URL or a "nofile"
10073 * buffer, these don't use the full path. */
10074 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10075 if (buf->b_fname != NULL
10076 && (path_with_url(buf->b_fname)
10077#ifdef FEAT_QUICKFIX
10078 || bt_nofile(buf)
10079#endif
10080 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010081 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010082 break;
10083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 }
10085 return buf;
10086}
10087
10088/*
10089 * "bufexists(expr)" function
10090 */
10091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010092f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010094 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095}
10096
10097/*
10098 * "buflisted(expr)" function
10099 */
10100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010101f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102{
10103 buf_T *buf;
10104
10105 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107}
10108
10109/*
10110 * "bufloaded(expr)" function
10111 */
10112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010113f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114{
10115 buf_T *buf;
10116
10117 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010118 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119}
10120
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010121 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010122buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 int save_magic;
10125 char_u *save_cpo;
10126 buf_T *buf;
10127
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10129 save_magic = p_magic;
10130 p_magic = TRUE;
10131 save_cpo = p_cpo;
10132 p_cpo = (char_u *)"";
10133
10134 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010135 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136
10137 p_magic = save_magic;
10138 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010139 return buf;
10140}
10141
10142/*
10143 * Get buffer by number or pattern.
10144 */
10145 static buf_T *
10146get_buf_tv(typval_T *tv, int curtab_only)
10147{
10148 char_u *name = tv->vval.v_string;
10149 buf_T *buf;
10150
10151 if (tv->v_type == VAR_NUMBER)
10152 return buflist_findnr((int)tv->vval.v_number);
10153 if (tv->v_type != VAR_STRING)
10154 return NULL;
10155 if (name == NULL || *name == NUL)
10156 return curbuf;
10157 if (name[0] == '$' && name[1] == NUL)
10158 return lastbuf;
10159
10160 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
10162 /* If not found, try expanding the name, like done for bufexists(). */
10163 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165
10166 return buf;
10167}
10168
10169/*
10170 * "bufname(expr)" function
10171 */
10172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010173f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174{
10175 buf_T *buf;
10176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010179 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010180 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010182 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010184 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185 --emsg_off;
10186}
10187
10188/*
10189 * "bufnr(expr)" function
10190 */
10191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010192f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193{
10194 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010195 int error = FALSE;
10196 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010200 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010201 --emsg_off;
10202
10203 /* If the buffer isn't found and the second argument is not zero create a
10204 * new buffer. */
10205 if (buf == NULL
10206 && argvars[1].v_type != VAR_UNKNOWN
10207 && get_tv_number_chk(&argvars[1], &error) != 0
10208 && !error
10209 && (name = get_tv_string_chk(&argvars[0])) != NULL
10210 && !error)
10211 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10212
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010214 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010216 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217}
10218
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010220buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221{
10222#ifdef FEAT_WINDOWS
10223 win_T *wp;
10224 int winnr = 0;
10225#endif
10226 buf_T *buf;
10227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010230 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231#ifdef FEAT_WINDOWS
10232 for (wp = firstwin; wp; wp = wp->w_next)
10233 {
10234 ++winnr;
10235 if (wp->w_buffer == buf)
10236 break;
10237 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010238 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010240 rettv->vval.v_number = (curwin->w_buffer == buf
10241 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242#endif
10243 --emsg_off;
10244}
10245
10246/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010247 * "bufwinid(nr)" function
10248 */
10249 static void
10250f_bufwinid(typval_T *argvars, typval_T *rettv)
10251{
10252 buf_win_common(argvars, rettv, FALSE);
10253}
10254
10255/*
10256 * "bufwinnr(nr)" function
10257 */
10258 static void
10259f_bufwinnr(typval_T *argvars, typval_T *rettv)
10260{
10261 buf_win_common(argvars, rettv, TRUE);
10262}
10263
10264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 * "byte2line(byte)" function
10266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010268f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269{
10270#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010271 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272#else
10273 long boff = 0;
10274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010275 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010279 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 (linenr_T)0, &boff);
10281#endif
10282}
10283
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010285byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010286{
10287#ifdef FEAT_MBYTE
10288 char_u *t;
10289#endif
10290 char_u *str;
10291 long idx;
10292
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 str = get_tv_string_chk(&argvars[0]);
10294 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010296 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010297 return;
10298
10299#ifdef FEAT_MBYTE
10300 t = str;
10301 for ( ; idx > 0; idx--)
10302 {
10303 if (*t == NUL) /* EOL reached */
10304 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010305 if (enc_utf8 && comp)
10306 t += utf_ptr2len(t);
10307 else
10308 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010309 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010310 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010311#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010312 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010314#endif
10315}
10316
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010317/*
10318 * "byteidx()" function
10319 */
10320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010321f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010322{
10323 byteidx(argvars, rettv, FALSE);
10324}
10325
10326/*
10327 * "byteidxcomp()" function
10328 */
10329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010330f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010331{
10332 byteidx(argvars, rettv, TRUE);
10333}
10334
Bram Moolenaardb913952012-06-29 12:54:53 +020010335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010336func_call(
10337 char_u *name,
10338 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010339 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010340 dict_T *selfdict,
10341 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010342{
10343 listitem_T *item;
10344 typval_T argv[MAX_FUNC_ARGS + 1];
10345 int argc = 0;
10346 int dummy;
10347 int r = 0;
10348
10349 for (item = args->vval.v_list->lv_first; item != NULL;
10350 item = item->li_next)
10351 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010352 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010353 {
10354 EMSG(_("E699: Too many arguments"));
10355 break;
10356 }
10357 /* Make a copy of each argument. This is needed to be able to set
10358 * v_lock to VAR_FIXED in the copy without changing the original list.
10359 */
10360 copy_tv(&item->li_tv, &argv[argc++]);
10361 }
10362
10363 if (item == NULL)
10364 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10365 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010366 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010367
10368 /* Free the arguments. */
10369 while (argc > 0)
10370 clear_tv(&argv[--argc]);
10371
10372 return r;
10373}
10374
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010375/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010376 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010377 */
10378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010379f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010380{
10381 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010382 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010383 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010384
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010385 if (argvars[1].v_type != VAR_LIST)
10386 {
10387 EMSG(_(e_listreq));
10388 return;
10389 }
10390 if (argvars[1].vval.v_list == NULL)
10391 return;
10392
10393 if (argvars[0].v_type == VAR_FUNC)
10394 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010395 else if (argvars[0].v_type == VAR_PARTIAL)
10396 {
10397 partial = argvars[0].vval.v_partial;
10398 func = partial->pt_name;
10399 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010400 else
10401 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010402 if (*func == NUL)
10403 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010404
Bram Moolenaare9a41262005-01-15 22:18:47 +000010405 if (argvars[2].v_type != VAR_UNKNOWN)
10406 {
10407 if (argvars[2].v_type != VAR_DICT)
10408 {
10409 EMSG(_(e_dictreq));
10410 return;
10411 }
10412 selfdict = argvars[2].vval.v_dict;
10413 }
10414
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010415 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010416}
10417
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010418#ifdef FEAT_FLOAT
10419/*
10420 * "ceil({float})" function
10421 */
10422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010423f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010424{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010425 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010426
10427 rettv->v_type = VAR_FLOAT;
10428 if (get_float_arg(argvars, &f) == OK)
10429 rettv->vval.v_float = ceil(f);
10430 else
10431 rettv->vval.v_float = 0.0;
10432}
10433#endif
10434
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010435#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010436/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010437 * "ch_close()" function
10438 */
10439 static void
10440f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10441{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010442 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010443
10444 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010445 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010446 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010447 channel_clear(channel);
10448 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010449}
10450
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010451/*
10452 * "ch_getbufnr()" function
10453 */
10454 static void
10455f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10456{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010457 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010458
10459 rettv->vval.v_number = -1;
10460 if (channel != NULL)
10461 {
10462 char_u *what = get_tv_string(&argvars[1]);
10463 int part;
10464
10465 if (STRCMP(what, "err") == 0)
10466 part = PART_ERR;
10467 else if (STRCMP(what, "out") == 0)
10468 part = PART_OUT;
10469 else if (STRCMP(what, "in") == 0)
10470 part = PART_IN;
10471 else
10472 part = PART_SOCK;
10473 if (channel->ch_part[part].ch_buffer != NULL)
10474 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10475 }
10476}
10477
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010478/*
10479 * "ch_getjob()" function
10480 */
10481 static void
10482f_ch_getjob(typval_T *argvars, typval_T *rettv)
10483{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010484 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010485
10486 if (channel != NULL)
10487 {
10488 rettv->v_type = VAR_JOB;
10489 rettv->vval.v_job = channel->ch_job;
10490 if (channel->ch_job != NULL)
10491 ++channel->ch_job->jv_refcount;
10492 }
10493}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010494
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010495/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010496 * "ch_info()" function
10497 */
10498 static void
10499f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10500{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010501 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010502
10503 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10504 channel_info(channel, rettv->vval.v_dict);
10505}
10506
10507/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010508 * "ch_log()" function
10509 */
10510 static void
10511f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10512{
10513 char_u *msg = get_tv_string(&argvars[0]);
10514 channel_T *channel = NULL;
10515
10516 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010517 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010518
10519 ch_log(channel, (char *)msg);
10520}
10521
10522/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010523 * "ch_logfile()" function
10524 */
10525 static void
10526f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10527{
10528 char_u *fname;
10529 char_u *opt = (char_u *)"";
10530 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010531
10532 fname = get_tv_string(&argvars[0]);
10533 if (argvars[1].v_type == VAR_STRING)
10534 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010535 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010536}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010537
10538/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010539 * "ch_open()" function
10540 */
10541 static void
10542f_ch_open(typval_T *argvars, typval_T *rettv)
10543{
Bram Moolenaar77073442016-02-13 23:23:53 +010010544 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010545 if (check_restricted() || check_secure())
10546 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010547 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010548}
10549
10550/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010551 * "ch_read()" function
10552 */
10553 static void
10554f_ch_read(typval_T *argvars, typval_T *rettv)
10555{
10556 common_channel_read(argvars, rettv, FALSE);
10557}
10558
10559/*
10560 * "ch_readraw()" function
10561 */
10562 static void
10563f_ch_readraw(typval_T *argvars, typval_T *rettv)
10564{
10565 common_channel_read(argvars, rettv, TRUE);
10566}
10567
10568/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010569 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010570 */
10571 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010572f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10573{
10574 ch_expr_common(argvars, rettv, TRUE);
10575}
10576
10577/*
10578 * "ch_sendexpr()" function
10579 */
10580 static void
10581f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10582{
10583 ch_expr_common(argvars, rettv, FALSE);
10584}
10585
10586/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010587 * "ch_evalraw()" function
10588 */
10589 static void
10590f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10591{
10592 ch_raw_common(argvars, rettv, TRUE);
10593}
10594
10595/*
10596 * "ch_sendraw()" function
10597 */
10598 static void
10599f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10600{
10601 ch_raw_common(argvars, rettv, FALSE);
10602}
10603
10604/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010605 * "ch_setoptions()" function
10606 */
10607 static void
10608f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10609{
10610 channel_T *channel;
10611 jobopt_T opt;
10612
Bram Moolenaar437905c2016-04-26 19:01:05 +020010613 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010614 if (channel == NULL)
10615 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010616 clear_job_options(&opt);
10617 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010618 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10619 channel_set_options(channel, &opt);
10620 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010621}
10622
10623/*
10624 * "ch_status()" function
10625 */
10626 static void
10627f_ch_status(typval_T *argvars, typval_T *rettv)
10628{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010629 channel_T *channel;
10630
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010631 /* return an empty string by default */
10632 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010633 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010634
Bram Moolenaar437905c2016-04-26 19:01:05 +020010635 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010636 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010637}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010638#endif
10639
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010640/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010641 * "changenr()" function
10642 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010644f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010645{
10646 rettv->vval.v_number = curbuf->b_u_seq_cur;
10647}
10648
10649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 * "char2nr(string)" function
10651 */
10652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010653f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654{
10655#ifdef FEAT_MBYTE
10656 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010657 {
10658 int utf8 = 0;
10659
10660 if (argvars[1].v_type != VAR_UNKNOWN)
10661 utf8 = get_tv_number_chk(&argvars[1], NULL);
10662
10663 if (utf8)
10664 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10665 else
10666 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 else
10669#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671}
10672
10673/*
10674 * "cindent(lnum)" function
10675 */
10676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010677f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678{
10679#ifdef FEAT_CINDENT
10680 pos_T pos;
10681 linenr_T lnum;
10682
10683 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10686 {
10687 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 curwin->w_cursor = pos;
10690 }
10691 else
10692#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694}
10695
10696/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010697 * "clearmatches()" function
10698 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010700f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010701{
10702#ifdef FEAT_SEARCH_EXTRA
10703 clear_matches(curwin);
10704#endif
10705}
10706
10707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 * "col(string)" function
10709 */
10710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010711f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712{
10713 colnr_T col = 0;
10714 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010715 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010717 fp = var2fpos(&argvars[0], FALSE, &fnum);
10718 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 {
10720 if (fp->col == MAXCOL)
10721 {
10722 /* '> can be MAXCOL, get the length of the line then */
10723 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010724 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 else
10726 col = MAXCOL;
10727 }
10728 else
10729 {
10730 col = fp->col + 1;
10731#ifdef FEAT_VIRTUALEDIT
10732 /* col(".") when the cursor is on the NUL at the end of the line
10733 * because of "coladd" can be seen as an extra column. */
10734 if (virtual_active() && fp == &curwin->w_cursor)
10735 {
10736 char_u *p = ml_get_cursor();
10737
10738 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10739 curwin->w_virtcol - curwin->w_cursor.coladd))
10740 {
10741# ifdef FEAT_MBYTE
10742 int l;
10743
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010744 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 col += l;
10746# else
10747 if (*p != NUL && p[1] == NUL)
10748 ++col;
10749# endif
10750 }
10751 }
10752#endif
10753 }
10754 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756}
10757
Bram Moolenaar572cb562005-08-05 21:35:02 +000010758#if defined(FEAT_INS_EXPAND)
10759/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010760 * "complete()" function
10761 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010763f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010764{
10765 int startcol;
10766
10767 if ((State & INSERT) == 0)
10768 {
10769 EMSG(_("E785: complete() can only be used in Insert mode"));
10770 return;
10771 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010772
10773 /* Check for undo allowed here, because if something was already inserted
10774 * the line was already saved for undo and this check isn't done. */
10775 if (!undo_allowed())
10776 return;
10777
Bram Moolenaarade00832006-03-10 21:46:58 +000010778 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10779 {
10780 EMSG(_(e_invarg));
10781 return;
10782 }
10783
10784 startcol = get_tv_number_chk(&argvars[0], NULL);
10785 if (startcol <= 0)
10786 return;
10787
10788 set_completion(startcol - 1, argvars[1].vval.v_list);
10789}
10790
10791/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010792 * "complete_add()" function
10793 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010795f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010796{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010797 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010798}
10799
10800/*
10801 * "complete_check()" function
10802 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010804f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010805{
10806 int saved = RedrawingDisabled;
10807
10808 RedrawingDisabled = 0;
10809 ins_compl_check_keys(0);
10810 rettv->vval.v_number = compl_interrupted;
10811 RedrawingDisabled = saved;
10812}
10813#endif
10814
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815/*
10816 * "confirm(message, buttons[, default [, type]])" function
10817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010819f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10822 char_u *message;
10823 char_u *buttons = NULL;
10824 char_u buf[NUMBUFLEN];
10825 char_u buf2[NUMBUFLEN];
10826 int def = 1;
10827 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 char_u *typestr;
10829 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010831 message = get_tv_string_chk(&argvars[0]);
10832 if (message == NULL)
10833 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010834 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10837 if (buttons == NULL)
10838 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010839 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010841 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010842 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010844 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10845 if (typestr == NULL)
10846 error = TRUE;
10847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 switch (TOUPPER_ASC(*typestr))
10850 {
10851 case 'E': type = VIM_ERROR; break;
10852 case 'Q': type = VIM_QUESTION; break;
10853 case 'I': type = VIM_INFO; break;
10854 case 'W': type = VIM_WARNING; break;
10855 case 'G': type = VIM_GENERIC; break;
10856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 }
10858 }
10859 }
10860 }
10861
10862 if (buttons == NULL || *buttons == NUL)
10863 buttons = (char_u *)_("&Ok");
10864
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010865 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010866 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010867 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868#endif
10869}
10870
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010871/*
10872 * "copy()" function
10873 */
10874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010875f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010876{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010877 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010878}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010880#ifdef FEAT_FLOAT
10881/*
10882 * "cos()" function
10883 */
10884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010885f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010886{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010887 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010888
10889 rettv->v_type = VAR_FLOAT;
10890 if (get_float_arg(argvars, &f) == OK)
10891 rettv->vval.v_float = cos(f);
10892 else
10893 rettv->vval.v_float = 0.0;
10894}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010895
10896/*
10897 * "cosh()" function
10898 */
10899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010900f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010901{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010902 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010903
10904 rettv->v_type = VAR_FLOAT;
10905 if (get_float_arg(argvars, &f) == OK)
10906 rettv->vval.v_float = cosh(f);
10907 else
10908 rettv->vval.v_float = 0.0;
10909}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010910#endif
10911
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010913 * "count()" function
10914 */
10915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010916f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010917{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010918 long n = 0;
10919 int ic = FALSE;
10920
Bram Moolenaare9a41262005-01-15 22:18:47 +000010921 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010922 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010923 listitem_T *li;
10924 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010925 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010926
Bram Moolenaare9a41262005-01-15 22:18:47 +000010927 if ((l = argvars[0].vval.v_list) != NULL)
10928 {
10929 li = l->lv_first;
10930 if (argvars[2].v_type != VAR_UNKNOWN)
10931 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010932 int error = FALSE;
10933
10934 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010935 if (argvars[3].v_type != VAR_UNKNOWN)
10936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010937 idx = get_tv_number_chk(&argvars[3], &error);
10938 if (!error)
10939 {
10940 li = list_find(l, idx);
10941 if (li == NULL)
10942 EMSGN(_(e_listidx), idx);
10943 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010945 if (error)
10946 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947 }
10948
10949 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010950 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951 ++n;
10952 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010953 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010954 else if (argvars[0].v_type == VAR_DICT)
10955 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010956 int todo;
10957 dict_T *d;
10958 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010959
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010960 if ((d = argvars[0].vval.v_dict) != NULL)
10961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010962 int error = FALSE;
10963
Bram Moolenaare9a41262005-01-15 22:18:47 +000010964 if (argvars[2].v_type != VAR_UNKNOWN)
10965 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010966 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010967 if (argvars[3].v_type != VAR_UNKNOWN)
10968 EMSG(_(e_invarg));
10969 }
10970
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010971 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010972 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010973 {
10974 if (!HASHITEM_EMPTY(hi))
10975 {
10976 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010977 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010978 ++n;
10979 }
10980 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010981 }
10982 }
10983 else
10984 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010985 rettv->vval.v_number = n;
10986}
10987
10988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10990 *
10991 * Checks the existence of a cscope connection.
10992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010994f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995{
10996#ifdef FEAT_CSCOPE
10997 int num = 0;
10998 char_u *dbpath = NULL;
10999 char_u *prepend = NULL;
11000 char_u buf[NUMBUFLEN];
11001
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011002 if (argvars[0].v_type != VAR_UNKNOWN
11003 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011005 num = (int)get_tv_number(&argvars[0]);
11006 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011007 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 }
11010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012#endif
11013}
11014
11015/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011016 * "cursor(lnum, col)" function, or
11017 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011019 * Moves the cursor to the specified line and column.
11020 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011023f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024{
11025 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011026#ifdef FEAT_VIRTUALEDIT
11027 long coladd = 0;
11028#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011029 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011031 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011032 if (argvars[1].v_type == VAR_UNKNOWN)
11033 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011034 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011035 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011036
Bram Moolenaar493c1782014-05-28 14:34:46 +020011037 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011038 {
11039 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011040 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011041 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011042 line = pos.lnum;
11043 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011044#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011045 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011046#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011047 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011048 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011049 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011050 set_curswant = FALSE;
11051 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011052 }
11053 else
11054 {
11055 line = get_tv_lnum(argvars);
11056 col = get_tv_number_chk(&argvars[1], NULL);
11057#ifdef FEAT_VIRTUALEDIT
11058 if (argvars[2].v_type != VAR_UNKNOWN)
11059 coladd = get_tv_number_chk(&argvars[2], NULL);
11060#endif
11061 }
11062 if (line < 0 || col < 0
11063#ifdef FEAT_VIRTUALEDIT
11064 || coladd < 0
11065#endif
11066 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011067 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 if (line > 0)
11069 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 if (col > 0)
11071 curwin->w_cursor.col = col - 1;
11072#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011073 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074#endif
11075
11076 /* Make sure the cursor is in a valid position. */
11077 check_cursor();
11078#ifdef FEAT_MBYTE
11079 /* Correct cursor for multi-byte character. */
11080 if (has_mbyte)
11081 mb_adjust_cursor();
11082#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011083
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011084 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011085 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086}
11087
11088/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011089 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090 */
11091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011092f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011094 int noref = 0;
11095
11096 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011097 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011098 if (noref < 0 || noref > 1)
11099 EMSG(_(e_invarg));
11100 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011101 {
11102 current_copyID += COPYID_INC;
11103 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105}
11106
11107/*
11108 * "delete()" function
11109 */
11110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011111f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011113 char_u nbuf[NUMBUFLEN];
11114 char_u *name;
11115 char_u *flags;
11116
11117 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011119 return;
11120
11121 name = get_tv_string(&argvars[0]);
11122 if (name == NULL || *name == NUL)
11123 {
11124 EMSG(_(e_invarg));
11125 return;
11126 }
11127
11128 if (argvars[1].v_type != VAR_UNKNOWN)
11129 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011131 flags = (char_u *)"";
11132
11133 if (*flags == NUL)
11134 /* delete a file */
11135 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11136 else if (STRCMP(flags, "d") == 0)
11137 /* delete an empty directory */
11138 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11139 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011140 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011141 rettv->vval.v_number = delete_recursive(name);
11142 else
11143 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144}
11145
11146/*
11147 * "did_filetype()" function
11148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011150f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151{
11152#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154#endif
11155}
11156
11157/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011158 * "diff_filler()" function
11159 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011161f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011162{
11163#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011165#endif
11166}
11167
11168/*
11169 * "diff_hlID()" function
11170 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011172f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011173{
11174#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011176 static linenr_T prev_lnum = 0;
11177 static int changedtick = 0;
11178 static int fnum = 0;
11179 static int change_start = 0;
11180 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011181 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011182 int filler_lines;
11183 int col;
11184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 if (lnum < 0) /* ignore type error in {lnum} arg */
11186 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011187 if (lnum != prev_lnum
11188 || changedtick != curbuf->b_changedtick
11189 || fnum != curbuf->b_fnum)
11190 {
11191 /* New line, buffer, change: need to get the values. */
11192 filler_lines = diff_check(curwin, lnum);
11193 if (filler_lines < 0)
11194 {
11195 if (filler_lines == -1)
11196 {
11197 change_start = MAXCOL;
11198 change_end = -1;
11199 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11200 hlID = HLF_ADD; /* added line */
11201 else
11202 hlID = HLF_CHD; /* changed line */
11203 }
11204 else
11205 hlID = HLF_ADD; /* added line */
11206 }
11207 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011208 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011209 prev_lnum = lnum;
11210 changedtick = curbuf->b_changedtick;
11211 fnum = curbuf->b_fnum;
11212 }
11213
11214 if (hlID == HLF_CHD || hlID == HLF_TXD)
11215 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011217 if (col >= change_start && col <= change_end)
11218 hlID = HLF_TXD; /* changed text */
11219 else
11220 hlID = HLF_CHD; /* changed line */
11221 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011222 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011223#endif
11224}
11225
11226/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011227 * "empty({expr})" function
11228 */
11229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011230f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011231{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011232 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011233
11234 switch (argvars[0].v_type)
11235 {
11236 case VAR_STRING:
11237 case VAR_FUNC:
11238 n = argvars[0].vval.v_string == NULL
11239 || *argvars[0].vval.v_string == NUL;
11240 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011241 case VAR_PARTIAL:
11242 n = FALSE;
11243 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011244 case VAR_NUMBER:
11245 n = argvars[0].vval.v_number == 0;
11246 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011247 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011248#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011249 n = argvars[0].vval.v_float == 0.0;
11250 break;
11251#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011252 case VAR_LIST:
11253 n = argvars[0].vval.v_list == NULL
11254 || argvars[0].vval.v_list->lv_first == NULL;
11255 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011256 case VAR_DICT:
11257 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011258 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011259 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011260 case VAR_SPECIAL:
11261 n = argvars[0].vval.v_number != VVAL_TRUE;
11262 break;
11263
Bram Moolenaar835dc632016-02-07 14:27:38 +010011264 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011265#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011266 n = argvars[0].vval.v_job == NULL
11267 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11268 break;
11269#endif
11270 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011271#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011272 n = argvars[0].vval.v_channel == NULL
11273 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011274 break;
11275#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011276 case VAR_UNKNOWN:
11277 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11278 n = TRUE;
11279 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011280 }
11281
11282 rettv->vval.v_number = n;
11283}
11284
11285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 * "escape({string}, {chars})" function
11287 */
11288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011289f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
11291 char_u buf[NUMBUFLEN];
11292
Bram Moolenaar758711c2005-02-02 23:11:38 +000011293 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11294 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011295 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296}
11297
11298/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011299 * "eval()" function
11300 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011302f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011303{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011304 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306 s = get_tv_string_chk(&argvars[0]);
11307 if (s != NULL)
11308 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011309
Bram Moolenaar615b9972015-01-14 17:15:05 +010011310 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011311 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11312 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011313 if (p != NULL && !aborting())
11314 EMSG2(_(e_invexpr2), p);
11315 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011316 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011317 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011319 else if (*s != NUL)
11320 EMSG(_(e_trailing));
11321}
11322
11323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324 * "eventhandler()" function
11325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011327f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330}
11331
11332/*
11333 * "executable()" function
11334 */
11335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011336f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011338 char_u *name = get_tv_string(&argvars[0]);
11339
11340 /* Check in $PATH and also check directly if there is a directory name. */
11341 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11342 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011343}
11344
11345/*
11346 * "exepath()" function
11347 */
11348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011349f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011350{
11351 char_u *p = NULL;
11352
Bram Moolenaarb5971142015-03-21 17:32:19 +010011353 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011354 rettv->v_type = VAR_STRING;
11355 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356}
11357
11358/*
11359 * "exists()" function
11360 */
11361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011362f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363{
11364 char_u *p;
11365 char_u *name;
11366 int n = FALSE;
11367 int len = 0;
11368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 if (*p == '$') /* environment variable */
11371 {
11372 /* first try "normal" environment variables (fast) */
11373 if (mch_getenv(p + 1) != NULL)
11374 n = TRUE;
11375 else
11376 {
11377 /* try expanding things like $VIM and ${HOME} */
11378 p = expand_env_save(p);
11379 if (p != NULL && *p != '$')
11380 n = TRUE;
11381 vim_free(p);
11382 }
11383 }
11384 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011386 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011387 if (*skipwhite(p) != NUL)
11388 n = FALSE; /* trailing garbage */
11389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 else if (*p == '*') /* internal or user defined function */
11391 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011392 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 }
11394 else if (*p == ':')
11395 {
11396 n = cmd_exists(p + 1);
11397 }
11398 else if (*p == '#')
11399 {
11400#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011401 if (p[1] == '#')
11402 n = autocmd_supported(p + 2);
11403 else
11404 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405#endif
11406 }
11407 else /* internal variable */
11408 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011409 char_u *tofree;
11410 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011412 /* get_name_len() takes care of expanding curly braces */
11413 name = p;
11414 len = get_name_len(&p, &tofree, TRUE, FALSE);
11415 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011417 if (tofree != NULL)
11418 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011419 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011420 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011422 /* handle d.key, l[idx], f(expr) */
11423 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11424 if (n)
11425 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 }
11427 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011428 if (*p != NUL)
11429 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011431 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 }
11433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435}
11436
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011437#ifdef FEAT_FLOAT
11438/*
11439 * "exp()" function
11440 */
11441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011442f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011443{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011444 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011445
11446 rettv->v_type = VAR_FLOAT;
11447 if (get_float_arg(argvars, &f) == OK)
11448 rettv->vval.v_float = exp(f);
11449 else
11450 rettv->vval.v_float = 0.0;
11451}
11452#endif
11453
Bram Moolenaar071d4272004-06-13 20:20:40 +000011454/*
11455 * "expand()" function
11456 */
11457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011458f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459{
11460 char_u *s;
11461 int len;
11462 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011463 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011466 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011469 if (argvars[1].v_type != VAR_UNKNOWN
11470 && argvars[2].v_type != VAR_UNKNOWN
11471 && get_tv_number_chk(&argvars[2], &error)
11472 && !error)
11473 {
11474 rettv->v_type = VAR_LIST;
11475 rettv->vval.v_list = NULL;
11476 }
11477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 if (*s == '%' || *s == '#' || *s == '<')
11480 {
11481 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011482 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011484 if (rettv->v_type == VAR_LIST)
11485 {
11486 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11487 list_append_string(rettv->vval.v_list, result, -1);
11488 else
11489 vim_free(result);
11490 }
11491 else
11492 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493 }
11494 else
11495 {
11496 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011497 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011498 if (argvars[1].v_type != VAR_UNKNOWN
11499 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011500 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 if (!error)
11502 {
11503 ExpandInit(&xpc);
11504 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011505 if (p_wic)
11506 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011507 if (rettv->v_type == VAR_STRING)
11508 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11509 options, WILD_ALL);
11510 else if (rettv_list_alloc(rettv) != FAIL)
11511 {
11512 int i;
11513
11514 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11515 for (i = 0; i < xpc.xp_numfiles; i++)
11516 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11517 ExpandCleanup(&xpc);
11518 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 }
11520 else
11521 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 }
11523}
11524
11525/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011526 * Go over all entries in "d2" and add them to "d1".
11527 * When "action" is "error" then a duplicate key is an error.
11528 * When "action" is "force" then a duplicate key is overwritten.
11529 * Otherwise duplicate keys are ignored ("action" is "keep").
11530 */
11531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011532dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011533{
11534 dictitem_T *di1;
11535 hashitem_T *hi2;
11536 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011537 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011538
11539 todo = (int)d2->dv_hashtab.ht_used;
11540 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11541 {
11542 if (!HASHITEM_EMPTY(hi2))
11543 {
11544 --todo;
11545 di1 = dict_find(d1, hi2->hi_key, -1);
11546 if (d1->dv_scope != 0)
11547 {
11548 /* Disallow replacing a builtin function in l: and g:.
11549 * Check the key to be valid when adding to any
11550 * scope. */
11551 if (d1->dv_scope == VAR_DEF_SCOPE
11552 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11553 && var_check_func_name(hi2->hi_key,
11554 di1 == NULL))
11555 break;
11556 if (!valid_varname(hi2->hi_key))
11557 break;
11558 }
11559 if (di1 == NULL)
11560 {
11561 di1 = dictitem_copy(HI2DI(hi2));
11562 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11563 dictitem_free(di1);
11564 }
11565 else if (*action == 'e')
11566 {
11567 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11568 break;
11569 }
11570 else if (*action == 'f' && HI2DI(hi2) != di1)
11571 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011572 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11573 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011574 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011575 clear_tv(&di1->di_tv);
11576 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11577 }
11578 }
11579 }
11580}
11581
11582/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011583 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011584 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011585 */
11586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011587f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011588{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011589 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011590
Bram Moolenaare9a41262005-01-15 22:18:47 +000011591 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011592 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011593 list_T *l1, *l2;
11594 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011595 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011596 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011597
Bram Moolenaare9a41262005-01-15 22:18:47 +000011598 l1 = argvars[0].vval.v_list;
11599 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011600 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011601 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011602 {
11603 if (argvars[2].v_type != VAR_UNKNOWN)
11604 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605 before = get_tv_number_chk(&argvars[2], &error);
11606 if (error)
11607 return; /* type error; errmsg already given */
11608
Bram Moolenaar758711c2005-02-02 23:11:38 +000011609 if (before == l1->lv_len)
11610 item = NULL;
11611 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011612 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011613 item = list_find(l1, before);
11614 if (item == NULL)
11615 {
11616 EMSGN(_(e_listidx), before);
11617 return;
11618 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011619 }
11620 }
11621 else
11622 item = NULL;
11623 list_extend(l1, l2, item);
11624
Bram Moolenaare9a41262005-01-15 22:18:47 +000011625 copy_tv(&argvars[0], rettv);
11626 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011627 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011628 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11629 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011630 dict_T *d1, *d2;
11631 char_u *action;
11632 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011633
11634 d1 = argvars[0].vval.v_dict;
11635 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011636 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011637 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011638 {
11639 /* Check the third argument. */
11640 if (argvars[2].v_type != VAR_UNKNOWN)
11641 {
11642 static char *(av[]) = {"keep", "force", "error"};
11643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011644 action = get_tv_string_chk(&argvars[2]);
11645 if (action == NULL)
11646 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011647 for (i = 0; i < 3; ++i)
11648 if (STRCMP(action, av[i]) == 0)
11649 break;
11650 if (i == 3)
11651 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011652 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011653 return;
11654 }
11655 }
11656 else
11657 action = (char_u *)"force";
11658
Bram Moolenaara9922d62013-05-30 13:01:18 +020011659 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011660
Bram Moolenaare9a41262005-01-15 22:18:47 +000011661 copy_tv(&argvars[0], rettv);
11662 }
11663 }
11664 else
11665 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011666}
11667
11668/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011669 * "feedkeys()" function
11670 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011672f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011673{
11674 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011675 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011676 char_u *keys, *flags;
11677 char_u nbuf[NUMBUFLEN];
11678 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011679 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011680 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011681 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011682
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011683 /* This is not allowed in the sandbox. If the commands would still be
11684 * executed in the sandbox it would be OK, but it probably happens later,
11685 * when "sandbox" is no longer set. */
11686 if (check_secure())
11687 return;
11688
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011689 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011690
11691 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011692 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011693 flags = get_tv_string_buf(&argvars[1], nbuf);
11694 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011695 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011696 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011697 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011698 case 'n': remap = FALSE; break;
11699 case 'm': remap = TRUE; break;
11700 case 't': typed = TRUE; break;
11701 case 'i': insert = TRUE; break;
11702 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011703 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011704 }
11705 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011706 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011707
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011708 if (*keys != NUL || execute)
11709 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011710 /* Need to escape K_SPECIAL and CSI before putting the string in the
11711 * typeahead buffer. */
11712 keys_esc = vim_strsave_escape_csi(keys);
11713 if (keys_esc != NULL)
11714 {
11715 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011716 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011717 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011718 if (vgetc_busy)
11719 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011720 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011721 {
11722 int save_msg_scroll = msg_scroll;
11723
11724 /* Avoid a 1 second delay when the keys start Insert mode. */
11725 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011726
Bram Moolenaar245c4102016-04-20 17:37:41 +020011727 if (!dangerous)
11728 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011729 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011730 if (!dangerous)
11731 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011732 msg_scroll |= save_msg_scroll;
11733 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011734 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011735 }
11736}
11737
11738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 * "filereadable()" function
11740 */
11741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011742f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011743{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011744 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 char_u *p;
11746 int n;
11747
Bram Moolenaarc236c162008-07-13 17:41:49 +000011748#ifndef O_NONBLOCK
11749# define O_NONBLOCK 0
11750#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011751 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011752 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11753 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 {
11755 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011756 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757 }
11758 else
11759 n = FALSE;
11760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762}
11763
11764/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011765 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766 * rights to write into.
11767 */
11768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011769f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011771 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772}
11773
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011774 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011775findfilendir(
11776 typval_T *argvars UNUSED,
11777 typval_T *rettv,
11778 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011779{
11780#ifdef FEAT_SEARCHPATH
11781 char_u *fname;
11782 char_u *fresult = NULL;
11783 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11784 char_u *p;
11785 char_u pathbuf[NUMBUFLEN];
11786 int count = 1;
11787 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011788 int error = FALSE;
11789#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011790
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011791 rettv->vval.v_string = NULL;
11792 rettv->v_type = VAR_STRING;
11793
11794#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011796
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011797 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011799 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11800 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011801 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011802 else
11803 {
11804 if (*p != NUL)
11805 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011807 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011808 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011810 }
11811
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011812 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11813 error = TRUE;
11814
11815 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011817 do
11818 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011819 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011820 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011821 fresult = find_file_in_path_option(first ? fname : NULL,
11822 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011823 0, first, path,
11824 find_what,
11825 curbuf->b_ffname,
11826 find_what == FINDFILE_DIR
11827 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011829
11830 if (fresult != NULL && rettv->v_type == VAR_LIST)
11831 list_append_string(rettv->vval.v_list, fresult, -1);
11832
11833 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011834 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011835
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011836 if (rettv->v_type == VAR_STRING)
11837 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011838#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011839}
11840
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011841static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11842static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011843
11844/*
11845 * Implementation of map() and filter().
11846 */
11847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011848filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011849{
11850 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011851 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011852 listitem_T *li, *nli;
11853 list_T *l = NULL;
11854 dictitem_T *di;
11855 hashtab_T *ht;
11856 hashitem_T *hi;
11857 dict_T *d = NULL;
11858 typval_T save_val;
11859 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011860 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011861 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011862 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011863 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011864 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011865 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011866 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011867
Bram Moolenaare9a41262005-01-15 22:18:47 +000011868 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011869 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011870 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011871 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011872 return;
11873 }
11874 else if (argvars[0].v_type == VAR_DICT)
11875 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011876 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011877 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011878 return;
11879 }
11880 else
11881 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011882 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011883 return;
11884 }
11885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011886 expr = get_tv_string_buf_chk(&argvars[1], buf);
11887 /* On type errors, the preceding call has already displayed an error
11888 * message. Avoid a misleading error message for an empty string that
11889 * was not passed as argument. */
11890 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011892 prepare_vimvar(VV_VAL, &save_val);
11893 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011894
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011895 /* We reset "did_emsg" to be able to detect whether an error
11896 * occurred during evaluation of the expression. */
11897 save_did_emsg = did_emsg;
11898 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011899
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011900 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011901 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011902 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 vimvars[VV_KEY].vv_type = VAR_STRING;
11904
11905 ht = &d->dv_hashtab;
11906 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011907 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011908 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011909 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011910 if (!HASHITEM_EMPTY(hi))
11911 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011912 int r;
11913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 --todo;
11915 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011916 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011917 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11918 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011919 break;
11920 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011921 r = filter_map_one(&di->di_tv, expr, map, &rem);
11922 clear_tv(&vimvars[VV_KEY].vv_tv);
11923 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011924 break;
11925 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011926 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011927 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11928 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011929 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011931 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011932 }
11933 }
11934 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011935 }
11936 else
11937 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011938 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011940 for (li = l->lv_first; li != NULL; li = nli)
11941 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011942 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011943 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011945 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011946 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011947 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011948 break;
11949 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011950 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011951 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011952 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011953 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011954
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011955 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011956 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011957
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011958 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011960
11961 copy_tv(&argvars[0], rettv);
11962}
11963
11964 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011965filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011966{
Bram Moolenaar33570922005-01-25 22:26:29 +000011967 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011968 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011969 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011970
Bram Moolenaar33570922005-01-25 22:26:29 +000011971 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011972 s = expr;
11973 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011974 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011975 if (*s != NUL) /* check for trailing chars after expr */
11976 {
11977 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011978 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011979 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011980 }
11981 if (map)
11982 {
11983 /* map(): replace the list item value */
11984 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011985 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011986 *tv = rettv;
11987 }
11988 else
11989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990 int error = FALSE;
11991
Bram Moolenaare9a41262005-01-15 22:18:47 +000011992 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011993 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011994 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011995 /* On type error, nothing has been removed; return FAIL to stop the
11996 * loop. The error message was given by get_tv_number_chk(). */
11997 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011998 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011999 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012000 retval = OK;
12001theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012002 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012003 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012004}
12005
12006/*
12007 * "filter()" function
12008 */
12009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012010f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012011{
12012 filter_map(argvars, rettv, FALSE);
12013}
12014
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012015/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012016 * "finddir({fname}[, {path}[, {count}]])" function
12017 */
12018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012019f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012020{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012021 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012022}
12023
12024/*
12025 * "findfile({fname}[, {path}[, {count}]])" function
12026 */
12027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012028f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012029{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012030 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012031}
12032
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012033#ifdef FEAT_FLOAT
12034/*
12035 * "float2nr({float})" function
12036 */
12037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012038f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012039{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012040 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012041
12042 if (get_float_arg(argvars, &f) == OK)
12043 {
12044 if (f < -0x7fffffff)
12045 rettv->vval.v_number = -0x7fffffff;
12046 else if (f > 0x7fffffff)
12047 rettv->vval.v_number = 0x7fffffff;
12048 else
12049 rettv->vval.v_number = (varnumber_T)f;
12050 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012051}
12052
12053/*
12054 * "floor({float})" function
12055 */
12056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012057f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012058{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012059 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012060
12061 rettv->v_type = VAR_FLOAT;
12062 if (get_float_arg(argvars, &f) == OK)
12063 rettv->vval.v_float = floor(f);
12064 else
12065 rettv->vval.v_float = 0.0;
12066}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012067
12068/*
12069 * "fmod()" function
12070 */
12071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012072f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012073{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012074 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012075
12076 rettv->v_type = VAR_FLOAT;
12077 if (get_float_arg(argvars, &fx) == OK
12078 && get_float_arg(&argvars[1], &fy) == OK)
12079 rettv->vval.v_float = fmod(fx, fy);
12080 else
12081 rettv->vval.v_float = 0.0;
12082}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012083#endif
12084
Bram Moolenaar0d660222005-01-07 21:51:51 +000012085/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012086 * "fnameescape({string})" function
12087 */
12088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012089f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012090{
12091 rettv->vval.v_string = vim_strsave_fnameescape(
12092 get_tv_string(&argvars[0]), FALSE);
12093 rettv->v_type = VAR_STRING;
12094}
12095
12096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 * "fnamemodify({fname}, {mods})" function
12098 */
12099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012100f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101{
12102 char_u *fname;
12103 char_u *mods;
12104 int usedlen = 0;
12105 int len;
12106 char_u *fbuf = NULL;
12107 char_u buf[NUMBUFLEN];
12108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012109 fname = get_tv_string_chk(&argvars[0]);
12110 mods = get_tv_string_buf_chk(&argvars[1], buf);
12111 if (fname == NULL || mods == NULL)
12112 fname = NULL;
12113 else
12114 {
12115 len = (int)STRLEN(fname);
12116 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012119 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012121 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124 vim_free(fbuf);
12125}
12126
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012127static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012128
12129/*
12130 * "foldclosed()" function
12131 */
12132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012133foldclosed_both(
12134 typval_T *argvars UNUSED,
12135 typval_T *rettv,
12136 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137{
12138#ifdef FEAT_FOLDING
12139 linenr_T lnum;
12140 linenr_T first, last;
12141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12144 {
12145 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12146 {
12147 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012148 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 return;
12152 }
12153 }
12154#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012156}
12157
12158/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012159 * "foldclosed()" function
12160 */
12161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012162f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012163{
12164 foldclosed_both(argvars, rettv, FALSE);
12165}
12166
12167/*
12168 * "foldclosedend()" function
12169 */
12170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012171f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012172{
12173 foldclosed_both(argvars, rettv, TRUE);
12174}
12175
12176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177 * "foldlevel()" function
12178 */
12179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012180f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181{
12182#ifdef FEAT_FOLDING
12183 linenr_T lnum;
12184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012185 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012187 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189}
12190
12191/*
12192 * "foldtext()" function
12193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012195f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196{
12197#ifdef FEAT_FOLDING
12198 linenr_T lnum;
12199 char_u *s;
12200 char_u *r;
12201 int len;
12202 char *txt;
12203#endif
12204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 rettv->v_type = VAR_STRING;
12206 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012208 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12209 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12210 <= curbuf->b_ml.ml_line_count
12211 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 {
12213 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012214 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12215 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 {
12217 if (!linewhite(lnum))
12218 break;
12219 ++lnum;
12220 }
12221
12222 /* Find interesting text in this line. */
12223 s = skipwhite(ml_get(lnum));
12224 /* skip C comment-start */
12225 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012228 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012229 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012230 {
12231 s = skipwhite(ml_get(lnum + 1));
12232 if (*s == '*')
12233 s = skipwhite(s + 1);
12234 }
12235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 txt = _("+-%s%3ld lines: ");
12237 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012238 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239 + 20 /* for %3ld */
12240 + STRLEN(s))); /* concatenated */
12241 if (r != NULL)
12242 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012243 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12244 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12245 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246 len = (int)STRLEN(r);
12247 STRCAT(r, s);
12248 /* remove 'foldmarker' and 'commentstring' */
12249 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012250 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251 }
12252 }
12253#endif
12254}
12255
12256/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012257 * "foldtextresult(lnum)" function
12258 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012260f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012261{
12262#ifdef FEAT_FOLDING
12263 linenr_T lnum;
12264 char_u *text;
12265 char_u buf[51];
12266 foldinfo_T foldinfo;
12267 int fold_count;
12268#endif
12269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 rettv->v_type = VAR_STRING;
12271 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012272#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012273 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012274 /* treat illegal types and illegal string values for {lnum} the same */
12275 if (lnum < 0)
12276 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012277 fold_count = foldedCount(curwin, lnum, &foldinfo);
12278 if (fold_count > 0)
12279 {
12280 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12281 &foldinfo, buf);
12282 if (text == buf)
12283 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012285 }
12286#endif
12287}
12288
12289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 * "foreground()" function
12291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012293f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295#ifdef FEAT_GUI
12296 if (gui.in_use)
12297 gui_mch_set_foreground();
12298#else
12299# ifdef WIN32
12300 win32_set_foreground();
12301# endif
12302#endif
12303}
12304
12305/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012306 * "function()" function
12307 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012309f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012310{
12311 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012312 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012313 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012314 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012315
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012316 if (argvars[0].v_type == VAR_FUNC)
12317 {
12318 /* function(MyFunc, [arg], dict) */
12319 s = argvars[0].vval.v_string;
12320 }
12321 else if (argvars[0].v_type == VAR_PARTIAL
12322 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012323 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012324 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012325 arg_pt = argvars[0].vval.v_partial;
12326 s = arg_pt->pt_name;
12327 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012328 else
12329 {
12330 /* function('MyFunc', [arg], dict) */
12331 s = get_tv_string(&argvars[0]);
12332 use_string = TRUE;
12333 }
12334
12335 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012336 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012337 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012338 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12339 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012340 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012341 else
12342 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012343 int dict_idx = 0;
12344 int arg_idx = 0;
12345 list_T *list = NULL;
12346
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012347 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012348 {
12349 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012350 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012351
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012352 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12353 * also be called from another script. Using trans_function_name()
12354 * would also work, but some plugins depend on the name being
12355 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012356 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012357 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12358 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012359 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012360 STRCPY(name, sid_buf);
12361 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012362 }
12363 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012364 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012365 name = vim_strsave(s);
12366
12367 if (argvars[1].v_type != VAR_UNKNOWN)
12368 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012369 if (argvars[2].v_type != VAR_UNKNOWN)
12370 {
12371 /* function(name, [args], dict) */
12372 arg_idx = 1;
12373 dict_idx = 2;
12374 }
12375 else if (argvars[1].v_type == VAR_DICT)
12376 /* function(name, dict) */
12377 dict_idx = 1;
12378 else
12379 /* function(name, [args]) */
12380 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012381 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012382 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012383 if (argvars[dict_idx].v_type != VAR_DICT)
12384 {
12385 EMSG(_("E922: expected a dict"));
12386 vim_free(name);
12387 return;
12388 }
12389 if (argvars[dict_idx].vval.v_dict == NULL)
12390 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012391 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012392 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012393 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012394 if (argvars[arg_idx].v_type != VAR_LIST)
12395 {
12396 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12397 vim_free(name);
12398 return;
12399 }
12400 list = argvars[arg_idx].vval.v_list;
12401 if (list == NULL || list->lv_len == 0)
12402 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012403 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012404 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012405 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012406 {
12407 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012408
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012409 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012410 if (pt == NULL)
12411 vim_free(name);
12412 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012413 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012414 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012415 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012416 listitem_T *li;
12417 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012418 int arg_len = 0;
12419 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012420
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012421 if (arg_pt != NULL)
12422 arg_len = arg_pt->pt_argc;
12423 if (list != NULL)
12424 lv_len = list->lv_len;
12425 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012426 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012427 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012428 if (pt->pt_argv == NULL)
12429 {
12430 vim_free(pt);
12431 vim_free(name);
12432 return;
12433 }
12434 else
12435 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012436 for (i = 0; i < arg_len; i++)
12437 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12438 if (lv_len > 0)
12439 for (li = list->lv_first; li != NULL;
12440 li = li->li_next)
12441 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012442 }
12443 }
12444
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012445 /* For "function(dict.func, [], dict)" and "func" is a partial
12446 * use "dict". That is backwards compatible. */
12447 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012448 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012449 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012450 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12451 ++pt->pt_dict->dv_refcount;
12452 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012453 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012454 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012455 /* If the dict was bound automatically the result is also
12456 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012457 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012458 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012459 if (pt->pt_dict != NULL)
12460 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012461 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012462
12463 pt->pt_refcount = 1;
12464 pt->pt_name = name;
12465 func_ref(pt->pt_name);
12466 }
12467 rettv->v_type = VAR_PARTIAL;
12468 rettv->vval.v_partial = pt;
12469 }
12470 else
12471 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012472 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012473 rettv->v_type = VAR_FUNC;
12474 rettv->vval.v_string = name;
12475 func_ref(name);
12476 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012477 }
12478}
12479
12480/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012481 * "garbagecollect()" function
12482 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012484f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012485{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012486 /* This is postponed until we are back at the toplevel, because we may be
12487 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12488 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012489
12490 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12491 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012492}
12493
12494/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495 * "get()" function
12496 */
12497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012498f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499{
Bram Moolenaar33570922005-01-25 22:26:29 +000012500 listitem_T *li;
12501 list_T *l;
12502 dictitem_T *di;
12503 dict_T *d;
12504 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012505
Bram Moolenaare9a41262005-01-15 22:18:47 +000012506 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012507 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012508 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510 int error = FALSE;
12511
12512 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12513 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012514 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012515 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012516 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012517 else if (argvars[0].v_type == VAR_DICT)
12518 {
12519 if ((d = argvars[0].vval.v_dict) != NULL)
12520 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012521 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012522 if (di != NULL)
12523 tv = &di->di_tv;
12524 }
12525 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012526 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012527 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012528 partial_T *pt;
12529 partial_T fref_pt;
12530
12531 if (argvars[0].v_type == VAR_PARTIAL)
12532 pt = argvars[0].vval.v_partial;
12533 else
12534 {
12535 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12536 fref_pt.pt_name = argvars[0].vval.v_string;
12537 pt = &fref_pt;
12538 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012539
12540 if (pt != NULL)
12541 {
12542 char_u *what = get_tv_string(&argvars[1]);
12543
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012544 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012545 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012546 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012547 if (pt->pt_name == NULL)
12548 rettv->vval.v_string = NULL;
12549 else
12550 rettv->vval.v_string = vim_strsave(pt->pt_name);
12551 }
12552 else if (STRCMP(what, "dict") == 0)
12553 {
12554 rettv->v_type = VAR_DICT;
12555 rettv->vval.v_dict = pt->pt_dict;
12556 if (pt->pt_dict != NULL)
12557 ++pt->pt_dict->dv_refcount;
12558 }
12559 else if (STRCMP(what, "args") == 0)
12560 {
12561 rettv->v_type = VAR_LIST;
12562 if (rettv_list_alloc(rettv) == OK)
12563 {
12564 int i;
12565
12566 for (i = 0; i < pt->pt_argc; ++i)
12567 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12568 }
12569 }
12570 else
12571 EMSG2(_(e_invarg2), what);
12572 return;
12573 }
12574 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012575 else
12576 EMSG2(_(e_listdictarg), "get()");
12577
12578 if (tv == NULL)
12579 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012580 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012581 copy_tv(&argvars[2], rettv);
12582 }
12583 else
12584 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012585}
12586
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012587static 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 +000012588
12589/*
12590 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012591 * Return a range (from start to end) of lines in rettv from the specified
12592 * buffer.
12593 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012594 */
12595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012596get_buffer_lines(
12597 buf_T *buf,
12598 linenr_T start,
12599 linenr_T end,
12600 int retlist,
12601 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012602{
12603 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012604
Bram Moolenaar959a1432013-12-14 12:17:38 +010012605 rettv->v_type = VAR_STRING;
12606 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012607 if (retlist && rettv_list_alloc(rettv) == FAIL)
12608 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012609
12610 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12611 return;
12612
12613 if (!retlist)
12614 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012615 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12616 p = ml_get_buf(buf, start, FALSE);
12617 else
12618 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012619 rettv->vval.v_string = vim_strsave(p);
12620 }
12621 else
12622 {
12623 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012624 return;
12625
12626 if (start < 1)
12627 start = 1;
12628 if (end > buf->b_ml.ml_line_count)
12629 end = buf->b_ml.ml_line_count;
12630 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012631 if (list_append_string(rettv->vval.v_list,
12632 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012633 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012634 }
12635}
12636
12637/*
12638 * "getbufline()" function
12639 */
12640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012641f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012642{
12643 linenr_T lnum;
12644 linenr_T end;
12645 buf_T *buf;
12646
12647 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12648 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012649 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012650 --emsg_off;
12651
Bram Moolenaar661b1822005-07-28 22:36:45 +000012652 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012653 if (argvars[2].v_type == VAR_UNKNOWN)
12654 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012655 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012656 end = get_tv_lnum_buf(&argvars[2], buf);
12657
Bram Moolenaar342337a2005-07-21 21:11:17 +000012658 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012659}
12660
Bram Moolenaar0d660222005-01-07 21:51:51 +000012661/*
12662 * "getbufvar()" function
12663 */
12664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012665f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012666{
12667 buf_T *buf;
12668 buf_T *save_curbuf;
12669 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012670 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012671 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012673 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12674 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012675 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012676 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012677
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012678 rettv->v_type = VAR_STRING;
12679 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012680
12681 if (buf != NULL && varname != NULL)
12682 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012683 /* set curbuf to be our buf, temporarily */
12684 save_curbuf = curbuf;
12685 curbuf = buf;
12686
Bram Moolenaar0d660222005-01-07 21:51:51 +000012687 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012688 {
12689 if (get_option_tv(&varname, rettv, TRUE) == OK)
12690 done = TRUE;
12691 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012692 else if (STRCMP(varname, "changedtick") == 0)
12693 {
12694 rettv->v_type = VAR_NUMBER;
12695 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012696 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012697 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012698 else
12699 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012700 /* Look up the variable. */
12701 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12702 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12703 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012704 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012705 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012706 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012707 done = TRUE;
12708 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012709 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012710
12711 /* restore previous notion of curbuf */
12712 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012713 }
12714
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012715 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12716 /* use the default value */
12717 copy_tv(&argvars[2], rettv);
12718
Bram Moolenaar0d660222005-01-07 21:51:51 +000012719 --emsg_off;
12720}
12721
12722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723 * "getchar()" function
12724 */
12725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012726f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727{
12728 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012729 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012731 /* Position the cursor. Needed after a message that ends in a space. */
12732 windgoto(msg_row, msg_col);
12733
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 ++no_mapping;
12735 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012736 for (;;)
12737 {
12738 if (argvars[0].v_type == VAR_UNKNOWN)
12739 /* getchar(): blocking wait. */
12740 n = safe_vgetc();
12741 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12742 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012743 n = vpeekc_any();
12744 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012745 /* illegal argument or getchar(0) and no char avail: return zero */
12746 n = 0;
12747 else
12748 /* getchar(0) and char avail: return char */
12749 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012750
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012751 if (n == K_IGNORE)
12752 continue;
12753 break;
12754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 --no_mapping;
12756 --allow_keys;
12757
Bram Moolenaar219b8702006-11-01 14:32:36 +000012758 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012759 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012760 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12761 vimvars[VV_MOUSE_COL].vv_nr = 0;
12762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764 if (IS_SPECIAL(n) || mod_mask != 0)
12765 {
12766 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12767 int i = 0;
12768
12769 /* Turn a special key into three bytes, plus modifier. */
12770 if (mod_mask != 0)
12771 {
12772 temp[i++] = K_SPECIAL;
12773 temp[i++] = KS_MODIFIER;
12774 temp[i++] = mod_mask;
12775 }
12776 if (IS_SPECIAL(n))
12777 {
12778 temp[i++] = K_SPECIAL;
12779 temp[i++] = K_SECOND(n);
12780 temp[i++] = K_THIRD(n);
12781 }
12782#ifdef FEAT_MBYTE
12783 else if (has_mbyte)
12784 i += (*mb_char2bytes)(n, temp + i);
12785#endif
12786 else
12787 temp[i++] = n;
12788 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012789 rettv->v_type = VAR_STRING;
12790 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012791
12792#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012793 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012794 {
12795 int row = mouse_row;
12796 int col = mouse_col;
12797 win_T *win;
12798 linenr_T lnum;
12799# ifdef FEAT_WINDOWS
12800 win_T *wp;
12801# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012802 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012803
12804 if (row >= 0 && col >= 0)
12805 {
12806 /* Find the window at the mouse coordinates and compute the
12807 * text position. */
12808 win = mouse_find_win(&row, &col);
12809 (void)mouse_comp_pos(win, &row, &col, &lnum);
12810# ifdef FEAT_WINDOWS
12811 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012812 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012813# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012814 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012815 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012816 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12817 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12818 }
12819 }
12820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821 }
12822}
12823
12824/*
12825 * "getcharmod()" function
12826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012828f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831}
12832
12833/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012834 * "getcharsearch()" function
12835 */
12836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012837f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012838{
12839 if (rettv_dict_alloc(rettv) != FAIL)
12840 {
12841 dict_T *dict = rettv->vval.v_dict;
12842
12843 dict_add_nr_str(dict, "char", 0L, last_csearch());
12844 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12845 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12846 }
12847}
12848
12849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850 * "getcmdline()" function
12851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012853f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 rettv->v_type = VAR_STRING;
12856 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857}
12858
12859/*
12860 * "getcmdpos()" function
12861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012863f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866}
12867
12868/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012869 * "getcmdtype()" function
12870 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012872f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012873{
12874 rettv->v_type = VAR_STRING;
12875 rettv->vval.v_string = alloc(2);
12876 if (rettv->vval.v_string != NULL)
12877 {
12878 rettv->vval.v_string[0] = get_cmdline_type();
12879 rettv->vval.v_string[1] = NUL;
12880 }
12881}
12882
12883/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012884 * "getcmdwintype()" function
12885 */
12886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012887f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012888{
12889 rettv->v_type = VAR_STRING;
12890 rettv->vval.v_string = NULL;
12891#ifdef FEAT_CMDWIN
12892 rettv->vval.v_string = alloc(2);
12893 if (rettv->vval.v_string != NULL)
12894 {
12895 rettv->vval.v_string[0] = cmdwin_type;
12896 rettv->vval.v_string[1] = NUL;
12897 }
12898#endif
12899}
12900
12901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 * "getcwd()" function
12903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012905f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012907 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012908 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012910 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012911 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012912
12913 wp = find_tabwin(&argvars[0], &argvars[1]);
12914 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012916 if (wp->w_localdir != NULL)
12917 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12918 else if(globaldir != NULL)
12919 rettv->vval.v_string = vim_strsave(globaldir);
12920 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012921 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012922 cwd = alloc(MAXPATHL);
12923 if (cwd != NULL)
12924 {
12925 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12926 rettv->vval.v_string = vim_strsave(cwd);
12927 vim_free(cwd);
12928 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012929 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012930#ifdef BACKSLASH_IN_FILENAME
12931 if (rettv->vval.v_string != NULL)
12932 slash_adjust(rettv->vval.v_string);
12933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012934 }
12935}
12936
12937/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012938 * "getfontname()" function
12939 */
12940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012941f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012943 rettv->v_type = VAR_STRING;
12944 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012945#ifdef FEAT_GUI
12946 if (gui.in_use)
12947 {
12948 GuiFont font;
12949 char_u *name = NULL;
12950
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012951 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012952 {
12953 /* Get the "Normal" font. Either the name saved by
12954 * hl_set_font_name() or from the font ID. */
12955 font = gui.norm_font;
12956 name = hl_get_font_name();
12957 }
12958 else
12959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012961 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12962 return;
12963 font = gui_mch_get_font(name, FALSE);
12964 if (font == NOFONT)
12965 return; /* Invalid font name, return empty string. */
12966 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012968 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012969 gui_mch_free_font(font);
12970 }
12971#endif
12972}
12973
12974/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012975 * "getfperm({fname})" function
12976 */
12977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012978f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012979{
12980 char_u *fname;
12981 struct stat st;
12982 char_u *perm = NULL;
12983 char_u flags[] = "rwx";
12984 int i;
12985
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012986 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012988 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012989 if (mch_stat((char *)fname, &st) >= 0)
12990 {
12991 perm = vim_strsave((char_u *)"---------");
12992 if (perm != NULL)
12993 {
12994 for (i = 0; i < 9; i++)
12995 {
12996 if (st.st_mode & (1 << (8 - i)))
12997 perm[i] = flags[i % 3];
12998 }
12999 }
13000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013002}
13003
13004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005 * "getfsize({fname})" function
13006 */
13007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013008f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013009{
13010 char_u *fname;
13011 struct stat st;
13012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013013 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013015 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016
13017 if (mch_stat((char *)fname, &st) >= 0)
13018 {
13019 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013020 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013022 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013024
13025 /* non-perfect check for overflow */
13026 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
13027 rettv->vval.v_number = -2;
13028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 }
13030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013031 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032}
13033
13034/*
13035 * "getftime({fname})" function
13036 */
13037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013038f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039{
13040 char_u *fname;
13041 struct stat st;
13042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044
13045 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013047 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013048 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049}
13050
13051/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013052 * "getftype({fname})" function
13053 */
13054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013055f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013056{
13057 char_u *fname;
13058 struct stat st;
13059 char_u *type = NULL;
13060 char *t;
13061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013062 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013065 if (mch_lstat((char *)fname, &st) >= 0)
13066 {
13067#ifdef S_ISREG
13068 if (S_ISREG(st.st_mode))
13069 t = "file";
13070 else if (S_ISDIR(st.st_mode))
13071 t = "dir";
13072# ifdef S_ISLNK
13073 else if (S_ISLNK(st.st_mode))
13074 t = "link";
13075# endif
13076# ifdef S_ISBLK
13077 else if (S_ISBLK(st.st_mode))
13078 t = "bdev";
13079# endif
13080# ifdef S_ISCHR
13081 else if (S_ISCHR(st.st_mode))
13082 t = "cdev";
13083# endif
13084# ifdef S_ISFIFO
13085 else if (S_ISFIFO(st.st_mode))
13086 t = "fifo";
13087# endif
13088# ifdef S_ISSOCK
13089 else if (S_ISSOCK(st.st_mode))
13090 t = "fifo";
13091# endif
13092 else
13093 t = "other";
13094#else
13095# ifdef S_IFMT
13096 switch (st.st_mode & S_IFMT)
13097 {
13098 case S_IFREG: t = "file"; break;
13099 case S_IFDIR: t = "dir"; break;
13100# ifdef S_IFLNK
13101 case S_IFLNK: t = "link"; break;
13102# endif
13103# ifdef S_IFBLK
13104 case S_IFBLK: t = "bdev"; break;
13105# endif
13106# ifdef S_IFCHR
13107 case S_IFCHR: t = "cdev"; break;
13108# endif
13109# ifdef S_IFIFO
13110 case S_IFIFO: t = "fifo"; break;
13111# endif
13112# ifdef S_IFSOCK
13113 case S_IFSOCK: t = "socket"; break;
13114# endif
13115 default: t = "other";
13116 }
13117# else
13118 if (mch_isdir(fname))
13119 t = "dir";
13120 else
13121 t = "file";
13122# endif
13123#endif
13124 type = vim_strsave((char_u *)t);
13125 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013126 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013127}
13128
13129/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013130 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013131 */
13132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013133f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013134{
13135 linenr_T lnum;
13136 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013137 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013138
13139 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013140 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013141 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013142 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013143 retlist = FALSE;
13144 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013145 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013146 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013147 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013148 retlist = TRUE;
13149 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013150
Bram Moolenaar342337a2005-07-21 21:11:17 +000013151 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013152}
13153
13154/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013155 * "getmatches()" function
13156 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013158f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013159{
13160#ifdef FEAT_SEARCH_EXTRA
13161 dict_T *dict;
13162 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013163 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013164
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013165 if (rettv_list_alloc(rettv) == OK)
13166 {
13167 while (cur != NULL)
13168 {
13169 dict = dict_alloc();
13170 if (dict == NULL)
13171 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013172 if (cur->match.regprog == NULL)
13173 {
13174 /* match added with matchaddpos() */
13175 for (i = 0; i < MAXPOSMATCH; ++i)
13176 {
13177 llpos_T *llpos;
13178 char buf[6];
13179 list_T *l;
13180
13181 llpos = &cur->pos.pos[i];
13182 if (llpos->lnum == 0)
13183 break;
13184 l = list_alloc();
13185 if (l == NULL)
13186 break;
13187 list_append_number(l, (varnumber_T)llpos->lnum);
13188 if (llpos->col > 0)
13189 {
13190 list_append_number(l, (varnumber_T)llpos->col);
13191 list_append_number(l, (varnumber_T)llpos->len);
13192 }
13193 sprintf(buf, "pos%d", i + 1);
13194 dict_add_list(dict, buf, l);
13195 }
13196 }
13197 else
13198 {
13199 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13200 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013201 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013202 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13203 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013204# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013205 if (cur->conceal_char)
13206 {
13207 char_u buf[MB_MAXBYTES + 1];
13208
13209 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13210 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13211 }
13212# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013213 list_append_dict(rettv->vval.v_list, dict);
13214 cur = cur->next;
13215 }
13216 }
13217#endif
13218}
13219
13220/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013221 * "getpid()" function
13222 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013224f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013225{
13226 rettv->vval.v_number = mch_get_pid();
13227}
13228
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013230getpos_both(
13231 typval_T *argvars,
13232 typval_T *rettv,
13233 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013234{
Bram Moolenaara5525202006-03-02 22:52:09 +000013235 pos_T *fp;
13236 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013237 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013238
13239 if (rettv_list_alloc(rettv) == OK)
13240 {
13241 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013242 if (getcurpos)
13243 fp = &curwin->w_cursor;
13244 else
13245 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013246 if (fnum != -1)
13247 list_append_number(l, (varnumber_T)fnum);
13248 else
13249 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013250 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13251 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013252 list_append_number(l, (fp != NULL)
13253 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013254 : (varnumber_T)0);
13255 list_append_number(l,
13256#ifdef FEAT_VIRTUALEDIT
13257 (fp != NULL) ? (varnumber_T)fp->coladd :
13258#endif
13259 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013260 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013261 {
13262 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013263 list_append_number(l, curwin->w_curswant == MAXCOL ?
13264 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013265 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013266 }
13267 else
13268 rettv->vval.v_number = FALSE;
13269}
13270
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013271
13272/*
13273 * "getcurpos()" function
13274 */
13275 static void
13276f_getcurpos(typval_T *argvars, typval_T *rettv)
13277{
13278 getpos_both(argvars, rettv, TRUE);
13279}
13280
13281/*
13282 * "getpos(string)" function
13283 */
13284 static void
13285f_getpos(typval_T *argvars, typval_T *rettv)
13286{
13287 getpos_both(argvars, rettv, FALSE);
13288}
13289
Bram Moolenaara5525202006-03-02 22:52:09 +000013290/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013291 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013292 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013294f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013295{
13296#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013297 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013298#endif
13299
Bram Moolenaar2641f772005-03-25 21:58:17 +000013300#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013301 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013302 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013303 wp = NULL;
13304 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13305 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013306 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013307 if (wp == NULL)
13308 return;
13309 }
13310
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013311 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013312 }
13313#endif
13314}
13315
13316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 * "getreg()" function
13318 */
13319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013320f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321{
13322 char_u *strregname;
13323 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013324 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013325 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013326 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013328 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013330 strregname = get_tv_string_chk(&argvars[0]);
13331 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013332 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013334 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013335 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13336 return_list = get_tv_number_chk(&argvars[2], &error);
13337 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013340 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013341
13342 if (error)
13343 return;
13344
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345 regname = (strregname == NULL ? '"' : *strregname);
13346 if (regname == 0)
13347 regname = '"';
13348
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013349 if (return_list)
13350 {
13351 rettv->v_type = VAR_LIST;
13352 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13353 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013354 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013355 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013356 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013357 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013358 }
13359 else
13360 {
13361 rettv->v_type = VAR_STRING;
13362 rettv->vval.v_string = get_reg_contents(regname,
13363 arg2 ? GREG_EXPR_SRC : 0);
13364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365}
13366
13367/*
13368 * "getregtype()" function
13369 */
13370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013371f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372{
13373 char_u *strregname;
13374 int regname;
13375 char_u buf[NUMBUFLEN + 2];
13376 long reglen = 0;
13377
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013378 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013379 {
13380 strregname = get_tv_string_chk(&argvars[0]);
13381 if (strregname == NULL) /* type error; errmsg already given */
13382 {
13383 rettv->v_type = VAR_STRING;
13384 rettv->vval.v_string = NULL;
13385 return;
13386 }
13387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388 else
13389 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013390 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391
13392 regname = (strregname == NULL ? '"' : *strregname);
13393 if (regname == 0)
13394 regname = '"';
13395
13396 buf[0] = NUL;
13397 buf[1] = NUL;
13398 switch (get_reg_type(regname, &reglen))
13399 {
13400 case MLINE: buf[0] = 'V'; break;
13401 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 case MBLOCK:
13403 buf[0] = Ctrl_V;
13404 sprintf((char *)buf + 1, "%ld", reglen + 1);
13405 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013407 rettv->v_type = VAR_STRING;
13408 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409}
13410
13411/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013412 * "gettabvar()" function
13413 */
13414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013415f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013416{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013417 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013418 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013419 dictitem_T *v;
13420 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013421 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013422
13423 rettv->v_type = VAR_STRING;
13424 rettv->vval.v_string = NULL;
13425
13426 varname = get_tv_string_chk(&argvars[1]);
13427 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13428 if (tp != NULL && varname != NULL)
13429 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013430 /* Set tp to be our tabpage, temporarily. Also set the window to the
13431 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013432 if (switch_win(&oldcurwin, &oldtabpage,
13433 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013434 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013435 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013436 /* look up the variable */
13437 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13438 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13439 if (v != NULL)
13440 {
13441 copy_tv(&v->di_tv, rettv);
13442 done = TRUE;
13443 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013444 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013445
13446 /* restore previous notion of curwin */
13447 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013448 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013449
13450 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013451 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013452}
13453
13454/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013455 * "gettabwinvar()" function
13456 */
13457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013458f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013459{
13460 getwinvar(argvars, rettv, 1);
13461}
13462
13463/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 * "getwinposx()" function
13465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013467f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013469 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470#ifdef FEAT_GUI
13471 if (gui.in_use)
13472 {
13473 int x, y;
13474
13475 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013476 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477 }
13478#endif
13479}
13480
13481/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013482 * "win_findbuf()" function
13483 */
13484 static void
13485f_win_findbuf(typval_T *argvars, typval_T *rettv)
13486{
13487 if (rettv_list_alloc(rettv) != FAIL)
13488 win_findbuf(argvars, rettv->vval.v_list);
13489}
13490
13491/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013492 * "win_getid()" function
13493 */
13494 static void
13495f_win_getid(typval_T *argvars, typval_T *rettv)
13496{
13497 rettv->vval.v_number = win_getid(argvars);
13498}
13499
13500/*
13501 * "win_gotoid()" function
13502 */
13503 static void
13504f_win_gotoid(typval_T *argvars, typval_T *rettv)
13505{
13506 rettv->vval.v_number = win_gotoid(argvars);
13507}
13508
13509/*
13510 * "win_id2tabwin()" function
13511 */
13512 static void
13513f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13514{
13515 if (rettv_list_alloc(rettv) != FAIL)
13516 win_id2tabwin(argvars, rettv->vval.v_list);
13517}
13518
13519/*
13520 * "win_id2win()" function
13521 */
13522 static void
13523f_win_id2win(typval_T *argvars, typval_T *rettv)
13524{
13525 rettv->vval.v_number = win_id2win(argvars);
13526}
13527
13528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 * "getwinposy()" function
13530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013532f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535#ifdef FEAT_GUI
13536 if (gui.in_use)
13537 {
13538 int x, y;
13539
13540 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013541 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542 }
13543#endif
13544}
13545
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013546/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013547 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013548 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013549 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013550find_win_by_nr(
13551 typval_T *vp,
13552 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013553{
13554#ifdef FEAT_WINDOWS
13555 win_T *wp;
13556#endif
13557 int nr;
13558
13559 nr = get_tv_number_chk(vp, NULL);
13560
13561#ifdef FEAT_WINDOWS
13562 if (nr < 0)
13563 return NULL;
13564 if (nr == 0)
13565 return curwin;
13566
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013567 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13568 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013569 if (--nr <= 0)
13570 break;
13571 return wp;
13572#else
13573 if (nr == 0 || nr == 1)
13574 return curwin;
13575 return NULL;
13576#endif
13577}
13578
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013580 * Find window specified by "wvp" in tabpage "tvp".
13581 */
13582 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013583find_tabwin(
13584 typval_T *wvp, /* VAR_UNKNOWN for current window */
13585 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013586{
13587 win_T *wp = NULL;
13588 tabpage_T *tp = NULL;
13589 long n;
13590
13591 if (wvp->v_type != VAR_UNKNOWN)
13592 {
13593 if (tvp->v_type != VAR_UNKNOWN)
13594 {
13595 n = get_tv_number(tvp);
13596 if (n >= 0)
13597 tp = find_tabpage(n);
13598 }
13599 else
13600 tp = curtab;
13601
13602 if (tp != NULL)
13603 wp = find_win_by_nr(wvp, tp);
13604 }
13605 else
13606 wp = curwin;
13607
13608 return wp;
13609}
13610
13611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 * "getwinvar()" function
13613 */
13614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013615f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013617 getwinvar(argvars, rettv, 0);
13618}
13619
13620/*
13621 * getwinvar() and gettabwinvar()
13622 */
13623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013624getwinvar(
13625 typval_T *argvars,
13626 typval_T *rettv,
13627 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013628{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013629 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013631 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013632 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013633 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013634#ifdef FEAT_WINDOWS
13635 win_T *oldcurwin;
13636 tabpage_T *oldtabpage;
13637 int need_switch_win;
13638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013640#ifdef FEAT_WINDOWS
13641 if (off == 1)
13642 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13643 else
13644 tp = curtab;
13645#endif
13646 win = find_win_by_nr(&argvars[off], tp);
13647 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013648 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013650 rettv->v_type = VAR_STRING;
13651 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652
13653 if (win != NULL && varname != NULL)
13654 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013655#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013656 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013657 * otherwise the window is not valid. Only do this when needed,
13658 * autocommands get blocked. */
13659 need_switch_win = !(tp == curtab && win == curwin);
13660 if (!need_switch_win
13661 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13662#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013663 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013664 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013665 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013666 if (get_option_tv(&varname, rettv, 1) == OK)
13667 done = TRUE;
13668 }
13669 else
13670 {
13671 /* Look up the variable. */
13672 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13673 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13674 varname, FALSE);
13675 if (v != NULL)
13676 {
13677 copy_tv(&v->di_tv, rettv);
13678 done = TRUE;
13679 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013682
Bram Moolenaarba117c22015-09-29 16:53:22 +020013683#ifdef FEAT_WINDOWS
13684 if (need_switch_win)
13685 /* restore previous notion of curwin */
13686 restore_win(oldcurwin, oldtabpage, TRUE);
13687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 }
13689
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013690 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13691 /* use the default return value */
13692 copy_tv(&argvars[off + 2], rettv);
13693
Bram Moolenaar071d4272004-06-13 20:20:40 +000013694 --emsg_off;
13695}
13696
13697/*
13698 * "glob()" function
13699 */
13700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013701f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013703 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013705 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013707 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013708 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013709 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013710 if (argvars[1].v_type != VAR_UNKNOWN)
13711 {
13712 if (get_tv_number_chk(&argvars[1], &error))
13713 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013714 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013715 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013716 if (get_tv_number_chk(&argvars[2], &error))
13717 {
13718 rettv->v_type = VAR_LIST;
13719 rettv->vval.v_list = NULL;
13720 }
13721 if (argvars[3].v_type != VAR_UNKNOWN
13722 && get_tv_number_chk(&argvars[3], &error))
13723 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013724 }
13725 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013726 if (!error)
13727 {
13728 ExpandInit(&xpc);
13729 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013730 if (p_wic)
13731 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013732 if (rettv->v_type == VAR_STRING)
13733 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013734 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013735 else if (rettv_list_alloc(rettv) != FAIL)
13736 {
13737 int i;
13738
13739 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13740 NULL, options, WILD_ALL_KEEP);
13741 for (i = 0; i < xpc.xp_numfiles; i++)
13742 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13743
13744 ExpandCleanup(&xpc);
13745 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013746 }
13747 else
13748 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749}
13750
13751/*
13752 * "globpath()" function
13753 */
13754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013755f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013757 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013759 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013760 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013761 garray_T ga;
13762 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013764 /* When the optional second argument is non-zero, don't remove matches
13765 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013766 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013767 if (argvars[2].v_type != VAR_UNKNOWN)
13768 {
13769 if (get_tv_number_chk(&argvars[2], &error))
13770 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013771 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013772 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013773 if (get_tv_number_chk(&argvars[3], &error))
13774 {
13775 rettv->v_type = VAR_LIST;
13776 rettv->vval.v_list = NULL;
13777 }
13778 if (argvars[4].v_type != VAR_UNKNOWN
13779 && get_tv_number_chk(&argvars[4], &error))
13780 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013781 }
13782 }
13783 if (file != NULL && !error)
13784 {
13785 ga_init2(&ga, (int)sizeof(char_u *), 10);
13786 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13787 if (rettv->v_type == VAR_STRING)
13788 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13789 else if (rettv_list_alloc(rettv) != FAIL)
13790 for (i = 0; i < ga.ga_len; ++i)
13791 list_append_string(rettv->vval.v_list,
13792 ((char_u **)(ga.ga_data))[i], -1);
13793 ga_clear_strings(&ga);
13794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013795 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013796 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013797}
13798
13799/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013800 * "glob2regpat()" function
13801 */
13802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013803f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013804{
13805 char_u *pat = get_tv_string_chk(&argvars[0]);
13806
13807 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013808 rettv->vval.v_string = (pat == NULL)
13809 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013810}
13811
13812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 * "has()" function
13814 */
13815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013816f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817{
13818 int i;
13819 char_u *name;
13820 int n = FALSE;
13821 static char *(has_list[]) =
13822 {
13823#ifdef AMIGA
13824 "amiga",
13825# ifdef FEAT_ARP
13826 "arp",
13827# endif
13828#endif
13829#ifdef __BEOS__
13830 "beos",
13831#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013832#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 "mac",
13834#endif
13835#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013836 "macunix", /* built with 'darwin' enabled */
13837#endif
13838#if defined(__APPLE__) && __APPLE__ == 1
13839 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841#ifdef __QNX__
13842 "qnx",
13843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844#ifdef UNIX
13845 "unix",
13846#endif
13847#ifdef VMS
13848 "vms",
13849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850#ifdef WIN32
13851 "win32",
13852#endif
13853#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13854 "win32unix",
13855#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013856#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857 "win64",
13858#endif
13859#ifdef EBCDIC
13860 "ebcdic",
13861#endif
13862#ifndef CASE_INSENSITIVE_FILENAME
13863 "fname_case",
13864#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013865#ifdef HAVE_ACL
13866 "acl",
13867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868#ifdef FEAT_ARABIC
13869 "arabic",
13870#endif
13871#ifdef FEAT_AUTOCMD
13872 "autocmd",
13873#endif
13874#ifdef FEAT_BEVAL
13875 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013876# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13877 "balloon_multiline",
13878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879#endif
13880#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13881 "builtin_terms",
13882# ifdef ALL_BUILTIN_TCAPS
13883 "all_builtin_terms",
13884# endif
13885#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013886#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13887 || defined(FEAT_GUI_W32) \
13888 || defined(FEAT_GUI_MOTIF))
13889 "browsefilter",
13890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891#ifdef FEAT_BYTEOFF
13892 "byte_offset",
13893#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013894#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013895 "channel",
13896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897#ifdef FEAT_CINDENT
13898 "cindent",
13899#endif
13900#ifdef FEAT_CLIENTSERVER
13901 "clientserver",
13902#endif
13903#ifdef FEAT_CLIPBOARD
13904 "clipboard",
13905#endif
13906#ifdef FEAT_CMDL_COMPL
13907 "cmdline_compl",
13908#endif
13909#ifdef FEAT_CMDHIST
13910 "cmdline_hist",
13911#endif
13912#ifdef FEAT_COMMENTS
13913 "comments",
13914#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013915#ifdef FEAT_CONCEAL
13916 "conceal",
13917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918#ifdef FEAT_CRYPT
13919 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013920 "crypt-blowfish",
13921 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922#endif
13923#ifdef FEAT_CSCOPE
13924 "cscope",
13925#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013926#ifdef FEAT_CURSORBIND
13927 "cursorbind",
13928#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013929#ifdef CURSOR_SHAPE
13930 "cursorshape",
13931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932#ifdef DEBUG
13933 "debug",
13934#endif
13935#ifdef FEAT_CON_DIALOG
13936 "dialog_con",
13937#endif
13938#ifdef FEAT_GUI_DIALOG
13939 "dialog_gui",
13940#endif
13941#ifdef FEAT_DIFF
13942 "diff",
13943#endif
13944#ifdef FEAT_DIGRAPHS
13945 "digraphs",
13946#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013947#ifdef FEAT_DIRECTX
13948 "directx",
13949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950#ifdef FEAT_DND
13951 "dnd",
13952#endif
13953#ifdef FEAT_EMACS_TAGS
13954 "emacs_tags",
13955#endif
13956 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013957 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958#ifdef FEAT_SEARCH_EXTRA
13959 "extra_search",
13960#endif
13961#ifdef FEAT_FKMAP
13962 "farsi",
13963#endif
13964#ifdef FEAT_SEARCHPATH
13965 "file_in_path",
13966#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013967#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013968 "filterpipe",
13969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970#ifdef FEAT_FIND_ID
13971 "find_in_path",
13972#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013973#ifdef FEAT_FLOAT
13974 "float",
13975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976#ifdef FEAT_FOLDING
13977 "folding",
13978#endif
13979#ifdef FEAT_FOOTER
13980 "footer",
13981#endif
13982#if !defined(USE_SYSTEM) && defined(UNIX)
13983 "fork",
13984#endif
13985#ifdef FEAT_GETTEXT
13986 "gettext",
13987#endif
13988#ifdef FEAT_GUI
13989 "gui",
13990#endif
13991#ifdef FEAT_GUI_ATHENA
13992# ifdef FEAT_GUI_NEXTAW
13993 "gui_neXtaw",
13994# else
13995 "gui_athena",
13996# endif
13997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998#ifdef FEAT_GUI_GTK
13999 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014000# ifdef USE_GTK3
14001 "gui_gtk3",
14002# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014006#ifdef FEAT_GUI_GNOME
14007 "gui_gnome",
14008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009#ifdef FEAT_GUI_MAC
14010 "gui_mac",
14011#endif
14012#ifdef FEAT_GUI_MOTIF
14013 "gui_motif",
14014#endif
14015#ifdef FEAT_GUI_PHOTON
14016 "gui_photon",
14017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018#ifdef FEAT_GUI_W32
14019 "gui_win32",
14020#endif
14021#ifdef FEAT_HANGULIN
14022 "hangul_input",
14023#endif
14024#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14025 "iconv",
14026#endif
14027#ifdef FEAT_INS_EXPAND
14028 "insert_expand",
14029#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014030#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014031 "job",
14032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033#ifdef FEAT_JUMPLIST
14034 "jumplist",
14035#endif
14036#ifdef FEAT_KEYMAP
14037 "keymap",
14038#endif
14039#ifdef FEAT_LANGMAP
14040 "langmap",
14041#endif
14042#ifdef FEAT_LIBCALL
14043 "libcall",
14044#endif
14045#ifdef FEAT_LINEBREAK
14046 "linebreak",
14047#endif
14048#ifdef FEAT_LISP
14049 "lispindent",
14050#endif
14051#ifdef FEAT_LISTCMDS
14052 "listcmds",
14053#endif
14054#ifdef FEAT_LOCALMAP
14055 "localmap",
14056#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014057#ifdef FEAT_LUA
14058# ifndef DYNAMIC_LUA
14059 "lua",
14060# endif
14061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062#ifdef FEAT_MENU
14063 "menu",
14064#endif
14065#ifdef FEAT_SESSION
14066 "mksession",
14067#endif
14068#ifdef FEAT_MODIFY_FNAME
14069 "modify_fname",
14070#endif
14071#ifdef FEAT_MOUSE
14072 "mouse",
14073#endif
14074#ifdef FEAT_MOUSESHAPE
14075 "mouseshape",
14076#endif
14077#if defined(UNIX) || defined(VMS)
14078# ifdef FEAT_MOUSE_DEC
14079 "mouse_dec",
14080# endif
14081# ifdef FEAT_MOUSE_GPM
14082 "mouse_gpm",
14083# endif
14084# ifdef FEAT_MOUSE_JSB
14085 "mouse_jsbterm",
14086# endif
14087# ifdef FEAT_MOUSE_NET
14088 "mouse_netterm",
14089# endif
14090# ifdef FEAT_MOUSE_PTERM
14091 "mouse_pterm",
14092# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014093# ifdef FEAT_MOUSE_SGR
14094 "mouse_sgr",
14095# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014096# ifdef FEAT_SYSMOUSE
14097 "mouse_sysmouse",
14098# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014099# ifdef FEAT_MOUSE_URXVT
14100 "mouse_urxvt",
14101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102# ifdef FEAT_MOUSE_XTERM
14103 "mouse_xterm",
14104# endif
14105#endif
14106#ifdef FEAT_MBYTE
14107 "multi_byte",
14108#endif
14109#ifdef FEAT_MBYTE_IME
14110 "multi_byte_ime",
14111#endif
14112#ifdef FEAT_MULTI_LANG
14113 "multi_lang",
14114#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014115#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014116#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014117 "mzscheme",
14118#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120#ifdef FEAT_OLE
14121 "ole",
14122#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014123 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124#ifdef FEAT_PATH_EXTRA
14125 "path_extra",
14126#endif
14127#ifdef FEAT_PERL
14128#ifndef DYNAMIC_PERL
14129 "perl",
14130#endif
14131#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014132#ifdef FEAT_PERSISTENT_UNDO
14133 "persistent_undo",
14134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135#ifdef FEAT_PYTHON
14136#ifndef DYNAMIC_PYTHON
14137 "python",
14138#endif
14139#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014140#ifdef FEAT_PYTHON3
14141#ifndef DYNAMIC_PYTHON3
14142 "python3",
14143#endif
14144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145#ifdef FEAT_POSTSCRIPT
14146 "postscript",
14147#endif
14148#ifdef FEAT_PRINTER
14149 "printer",
14150#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014151#ifdef FEAT_PROFILE
14152 "profile",
14153#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014154#ifdef FEAT_RELTIME
14155 "reltime",
14156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157#ifdef FEAT_QUICKFIX
14158 "quickfix",
14159#endif
14160#ifdef FEAT_RIGHTLEFT
14161 "rightleft",
14162#endif
14163#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14164 "ruby",
14165#endif
14166#ifdef FEAT_SCROLLBIND
14167 "scrollbind",
14168#endif
14169#ifdef FEAT_CMDL_INFO
14170 "showcmd",
14171 "cmdline_info",
14172#endif
14173#ifdef FEAT_SIGNS
14174 "signs",
14175#endif
14176#ifdef FEAT_SMARTINDENT
14177 "smartindent",
14178#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014179#ifdef STARTUPTIME
14180 "startuptime",
14181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182#ifdef FEAT_STL_OPT
14183 "statusline",
14184#endif
14185#ifdef FEAT_SUN_WORKSHOP
14186 "sun_workshop",
14187#endif
14188#ifdef FEAT_NETBEANS_INTG
14189 "netbeans_intg",
14190#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014191#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014192 "spell",
14193#endif
14194#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 "syntax",
14196#endif
14197#if defined(USE_SYSTEM) || !defined(UNIX)
14198 "system",
14199#endif
14200#ifdef FEAT_TAG_BINS
14201 "tag_binary",
14202#endif
14203#ifdef FEAT_TAG_OLDSTATIC
14204 "tag_old_static",
14205#endif
14206#ifdef FEAT_TAG_ANYWHITE
14207 "tag_any_white",
14208#endif
14209#ifdef FEAT_TCL
14210# ifndef DYNAMIC_TCL
14211 "tcl",
14212# endif
14213#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014214#ifdef FEAT_TERMGUICOLORS
14215 "termguicolors",
14216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217#ifdef TERMINFO
14218 "terminfo",
14219#endif
14220#ifdef FEAT_TERMRESPONSE
14221 "termresponse",
14222#endif
14223#ifdef FEAT_TEXTOBJ
14224 "textobjects",
14225#endif
14226#ifdef HAVE_TGETENT
14227 "tgetent",
14228#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014229#ifdef FEAT_TIMERS
14230 "timers",
14231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232#ifdef FEAT_TITLE
14233 "title",
14234#endif
14235#ifdef FEAT_TOOLBAR
14236 "toolbar",
14237#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014238#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14239 "unnamedplus",
14240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241#ifdef FEAT_USR_CMDS
14242 "user-commands", /* was accidentally included in 5.4 */
14243 "user_commands",
14244#endif
14245#ifdef FEAT_VIMINFO
14246 "viminfo",
14247#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014248#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249 "vertsplit",
14250#endif
14251#ifdef FEAT_VIRTUALEDIT
14252 "virtualedit",
14253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255#ifdef FEAT_VISUALEXTRA
14256 "visualextra",
14257#endif
14258#ifdef FEAT_VREPLACE
14259 "vreplace",
14260#endif
14261#ifdef FEAT_WILDIGN
14262 "wildignore",
14263#endif
14264#ifdef FEAT_WILDMENU
14265 "wildmenu",
14266#endif
14267#ifdef FEAT_WINDOWS
14268 "windows",
14269#endif
14270#ifdef FEAT_WAK
14271 "winaltkeys",
14272#endif
14273#ifdef FEAT_WRITEBACKUP
14274 "writebackup",
14275#endif
14276#ifdef FEAT_XIM
14277 "xim",
14278#endif
14279#ifdef FEAT_XFONTSET
14280 "xfontset",
14281#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014282#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014283 "xpm",
14284 "xpm_w32", /* for backward compatibility */
14285#else
14286# if defined(HAVE_XPM)
14287 "xpm",
14288# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290#ifdef USE_XSMP
14291 "xsmp",
14292#endif
14293#ifdef USE_XSMP_INTERACT
14294 "xsmp_interact",
14295#endif
14296#ifdef FEAT_XCLIPBOARD
14297 "xterm_clipboard",
14298#endif
14299#ifdef FEAT_XTERM_SAVE
14300 "xterm_save",
14301#endif
14302#if defined(UNIX) && defined(FEAT_X11)
14303 "X11",
14304#endif
14305 NULL
14306 };
14307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014308 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 for (i = 0; has_list[i] != NULL; ++i)
14310 if (STRICMP(name, has_list[i]) == 0)
14311 {
14312 n = TRUE;
14313 break;
14314 }
14315
14316 if (n == FALSE)
14317 {
14318 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014319 {
14320 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014321 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014322 && vim_isdigit(name[6])
14323 && vim_isdigit(name[8])
14324 && vim_isdigit(name[10]))
14325 {
14326 int major = atoi((char *)name + 6);
14327 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014328
14329 /* Expect "patch-9.9.01234". */
14330 n = (major < VIM_VERSION_MAJOR
14331 || (major == VIM_VERSION_MAJOR
14332 && (minor < VIM_VERSION_MINOR
14333 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014334 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014335 }
14336 else
14337 n = has_patch(atoi((char *)name + 5));
14338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 else if (STRICMP(name, "vim_starting") == 0)
14340 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014341#ifdef FEAT_MBYTE
14342 else if (STRICMP(name, "multi_byte_encoding") == 0)
14343 n = has_mbyte;
14344#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014345#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14346 else if (STRICMP(name, "balloon_multiline") == 0)
14347 n = multiline_balloon_available();
14348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349#ifdef DYNAMIC_TCL
14350 else if (STRICMP(name, "tcl") == 0)
14351 n = tcl_enabled(FALSE);
14352#endif
14353#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14354 else if (STRICMP(name, "iconv") == 0)
14355 n = iconv_enabled(FALSE);
14356#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014357#ifdef DYNAMIC_LUA
14358 else if (STRICMP(name, "lua") == 0)
14359 n = lua_enabled(FALSE);
14360#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014361#ifdef DYNAMIC_MZSCHEME
14362 else if (STRICMP(name, "mzscheme") == 0)
14363 n = mzscheme_enabled(FALSE);
14364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365#ifdef DYNAMIC_RUBY
14366 else if (STRICMP(name, "ruby") == 0)
14367 n = ruby_enabled(FALSE);
14368#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014369#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370#ifdef DYNAMIC_PYTHON
14371 else if (STRICMP(name, "python") == 0)
14372 n = python_enabled(FALSE);
14373#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014374#endif
14375#ifdef FEAT_PYTHON3
14376#ifdef DYNAMIC_PYTHON3
14377 else if (STRICMP(name, "python3") == 0)
14378 n = python3_enabled(FALSE);
14379#endif
14380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381#ifdef DYNAMIC_PERL
14382 else if (STRICMP(name, "perl") == 0)
14383 n = perl_enabled(FALSE);
14384#endif
14385#ifdef FEAT_GUI
14386 else if (STRICMP(name, "gui_running") == 0)
14387 n = (gui.in_use || gui.starting);
14388# ifdef FEAT_GUI_W32
14389 else if (STRICMP(name, "gui_win32s") == 0)
14390 n = gui_is_win32s();
14391# endif
14392# ifdef FEAT_BROWSE
14393 else if (STRICMP(name, "browse") == 0)
14394 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14395# endif
14396#endif
14397#ifdef FEAT_SYN_HL
14398 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014399 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400#endif
14401#if defined(WIN3264)
14402 else if (STRICMP(name, "win95") == 0)
14403 n = mch_windows95();
14404#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014405#ifdef FEAT_NETBEANS_INTG
14406 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014407 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409 }
14410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412}
14413
14414/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014415 * "has_key()" function
14416 */
14417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014418f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014419{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014420 if (argvars[0].v_type != VAR_DICT)
14421 {
14422 EMSG(_(e_dictreq));
14423 return;
14424 }
14425 if (argvars[0].vval.v_dict == NULL)
14426 return;
14427
14428 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014429 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014430}
14431
14432/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014433 * "haslocaldir()" function
14434 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014436f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014437{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014438 win_T *wp = NULL;
14439
14440 wp = find_tabwin(&argvars[0], &argvars[1]);
14441 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014442}
14443
14444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 * "hasmapto()" function
14446 */
14447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014448f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449{
14450 char_u *name;
14451 char_u *mode;
14452 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014453 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014456 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 mode = (char_u *)"nvo";
14458 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014461 if (argvars[2].v_type != VAR_UNKNOWN)
14462 abbr = get_tv_number(&argvars[2]);
14463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464
Bram Moolenaar2c932302006-03-18 21:42:09 +000014465 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469}
14470
14471/*
14472 * "histadd()" function
14473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014475f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476{
14477#ifdef FEAT_CMDHIST
14478 int histype;
14479 char_u *str;
14480 char_u buf[NUMBUFLEN];
14481#endif
14482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 if (check_restricted() || check_secure())
14485 return;
14486#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014487 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14488 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 if (histype >= 0)
14490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014491 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 if (*str != NUL)
14493 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014494 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 return;
14498 }
14499 }
14500#endif
14501}
14502
14503/*
14504 * "histdel()" function
14505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014507f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508{
14509#ifdef FEAT_CMDHIST
14510 int n;
14511 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014512 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014514 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14515 if (str == NULL)
14516 n = 0;
14517 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014519 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014520 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014521 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014522 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014523 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 else
14525 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014527 get_tv_string_buf(&argvars[1], buf));
14528 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014529#endif
14530}
14531
14532/*
14533 * "histget()" function
14534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014536f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537{
14538#ifdef FEAT_CMDHIST
14539 int type;
14540 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014541 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014543 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14544 if (str == NULL)
14545 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014547 {
14548 type = get_histtype(str);
14549 if (argvars[1].v_type == VAR_UNKNOWN)
14550 idx = get_history_idx(type);
14551 else
14552 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14553 /* -1 on type error */
14554 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560}
14561
14562/*
14563 * "histnr()" function
14564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014566f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567{
14568 int i;
14569
14570#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014571 char_u *history = get_tv_string_chk(&argvars[0]);
14572
14573 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 if (i >= HIST_CMD && i < HIST_COUNT)
14575 i = get_history_idx(i);
14576 else
14577#endif
14578 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014579 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580}
14581
14582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583 * "highlightID(name)" function
14584 */
14585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014586f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589}
14590
14591/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014592 * "highlight_exists()" function
14593 */
14594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014595f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014596{
14597 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14598}
14599
14600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 * "hostname()" function
14602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014604f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605{
14606 char_u hostname[256];
14607
14608 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609 rettv->v_type = VAR_STRING;
14610 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611}
14612
14613/*
14614 * iconv() function
14615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014617f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618{
14619#ifdef FEAT_MBYTE
14620 char_u buf1[NUMBUFLEN];
14621 char_u buf2[NUMBUFLEN];
14622 char_u *from, *to, *str;
14623 vimconv_T vimconv;
14624#endif
14625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014626 rettv->v_type = VAR_STRING;
14627 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628
14629#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 str = get_tv_string(&argvars[0]);
14631 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14632 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633 vimconv.vc_type = CONV_NONE;
14634 convert_setup(&vimconv, from, to);
14635
14636 /* If the encodings are equal, no conversion needed. */
14637 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014638 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014640 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641
14642 convert_setup(&vimconv, NULL, NULL);
14643 vim_free(from);
14644 vim_free(to);
14645#endif
14646}
14647
14648/*
14649 * "indent()" function
14650 */
14651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014652f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653{
14654 linenr_T lnum;
14655
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014656 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014657 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014658 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014660 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661}
14662
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014663/*
14664 * "index()" function
14665 */
14666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014667f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014668{
Bram Moolenaar33570922005-01-25 22:26:29 +000014669 list_T *l;
14670 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014671 long idx = 0;
14672 int ic = FALSE;
14673
14674 rettv->vval.v_number = -1;
14675 if (argvars[0].v_type != VAR_LIST)
14676 {
14677 EMSG(_(e_listreq));
14678 return;
14679 }
14680 l = argvars[0].vval.v_list;
14681 if (l != NULL)
14682 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014683 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014684 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014686 int error = FALSE;
14687
Bram Moolenaar758711c2005-02-02 23:11:38 +000014688 /* Start at specified item. Use the cached index that list_find()
14689 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014690 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014691 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014692 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014693 ic = get_tv_number_chk(&argvars[3], &error);
14694 if (error)
14695 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014696 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014697
Bram Moolenaar758711c2005-02-02 23:11:38 +000014698 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014699 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014700 {
14701 rettv->vval.v_number = idx;
14702 break;
14703 }
14704 }
14705}
14706
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707static int inputsecret_flag = 0;
14708
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014709static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014710
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014712 * This function is used by f_input() and f_inputdialog() functions. The third
14713 * argument to f_input() specifies the type of completion to use at the
14714 * prompt. The third argument to f_inputdialog() specifies the value to return
14715 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 */
14717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014718get_user_input(
14719 typval_T *argvars,
14720 typval_T *rettv,
14721 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014723 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 char_u *p = NULL;
14725 int c;
14726 char_u buf[NUMBUFLEN];
14727 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014728 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014729 int xp_type = EXPAND_NOTHING;
14730 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014732 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014733 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734
14735#ifdef NO_CONSOLE_INPUT
14736 /* While starting up, there is no place to enter text. */
14737 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739#endif
14740
14741 cmd_silent = FALSE; /* Want to see the prompt. */
14742 if (prompt != NULL)
14743 {
14744 /* Only the part of the message after the last NL is considered as
14745 * prompt for the command line */
14746 p = vim_strrchr(prompt, '\n');
14747 if (p == NULL)
14748 p = prompt;
14749 else
14750 {
14751 ++p;
14752 c = *p;
14753 *p = NUL;
14754 msg_start();
14755 msg_clr_eos();
14756 msg_puts_attr(prompt, echo_attr);
14757 msg_didout = FALSE;
14758 msg_starthere();
14759 *p = c;
14760 }
14761 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014763 if (argvars[1].v_type != VAR_UNKNOWN)
14764 {
14765 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14766 if (defstr != NULL)
14767 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014768
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014769 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014770 {
14771 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014772 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014773 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014774
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014775 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014776 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014777
Bram Moolenaar4463f292005-09-25 22:20:24 +000014778 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14779 if (xp_name == NULL)
14780 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014781
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014782 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014783
Bram Moolenaar4463f292005-09-25 22:20:24 +000014784 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14785 &xp_arg) == FAIL)
14786 return;
14787 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014788 }
14789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014790 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014791 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014792 int save_ex_normal_busy = ex_normal_busy;
14793 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014795 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14796 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014797 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014798 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014799 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014800 && argvars[1].v_type != VAR_UNKNOWN
14801 && argvars[2].v_type != VAR_UNKNOWN)
14802 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14803 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014804
14805 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014807 /* since the user typed this, no need to wait for return */
14808 need_wait_return = FALSE;
14809 msg_didout = FALSE;
14810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811 cmd_silent = cmd_silent_save;
14812}
14813
14814/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014815 * "input()" function
14816 * Also handles inputsecret() when inputsecret is set.
14817 */
14818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014819f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014820{
14821 get_user_input(argvars, rettv, FALSE);
14822}
14823
14824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825 * "inputdialog()" function
14826 */
14827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014828f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829{
14830#if defined(FEAT_GUI_TEXTDIALOG)
14831 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14832 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14833 {
14834 char_u *message;
14835 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014836 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014838 message = get_tv_string_chk(&argvars[0]);
14839 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014840 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014841 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842 else
14843 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014844 if (message != NULL && defstr != NULL
14845 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014846 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014847 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 else
14849 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014850 if (message != NULL && defstr != NULL
14851 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014852 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014853 rettv->vval.v_string = vim_strsave(
14854 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014856 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014858 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 }
14860 else
14861#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014862 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863}
14864
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014865/*
14866 * "inputlist()" function
14867 */
14868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014869f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014870{
14871 listitem_T *li;
14872 int selected;
14873 int mouse_used;
14874
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014875#ifdef NO_CONSOLE_INPUT
14876 /* While starting up, there is no place to enter text. */
14877 if (no_console_input())
14878 return;
14879#endif
14880 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14881 {
14882 EMSG2(_(e_listarg), "inputlist()");
14883 return;
14884 }
14885
14886 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014887 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014888 lines_left = Rows; /* avoid more prompt */
14889 msg_scroll = TRUE;
14890 msg_clr_eos();
14891
14892 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14893 {
14894 msg_puts(get_tv_string(&li->li_tv));
14895 msg_putchar('\n');
14896 }
14897
14898 /* Ask for choice. */
14899 selected = prompt_for_number(&mouse_used);
14900 if (mouse_used)
14901 selected -= lines_left;
14902
14903 rettv->vval.v_number = selected;
14904}
14905
14906
Bram Moolenaar071d4272004-06-13 20:20:40 +000014907static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14908
14909/*
14910 * "inputrestore()" function
14911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014913f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914{
14915 if (ga_userinput.ga_len > 0)
14916 {
14917 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14919 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014920 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 }
14922 else if (p_verbose > 1)
14923 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014924 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014925 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926 }
14927}
14928
14929/*
14930 * "inputsave()" function
14931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014933f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014935 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014936 if (ga_grow(&ga_userinput, 1) == OK)
14937 {
14938 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14939 + ga_userinput.ga_len);
14940 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014941 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014942 }
14943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014944 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945}
14946
14947/*
14948 * "inputsecret()" function
14949 */
14950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014951f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952{
14953 ++cmdline_star;
14954 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014955 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956 --cmdline_star;
14957 --inputsecret_flag;
14958}
14959
14960/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014961 * "insert()" function
14962 */
14963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014964f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014965{
14966 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014967 listitem_T *item;
14968 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014969 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014970
14971 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014972 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014973 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014974 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014975 {
14976 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014977 before = get_tv_number_chk(&argvars[2], &error);
14978 if (error)
14979 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014980
Bram Moolenaar758711c2005-02-02 23:11:38 +000014981 if (before == l->lv_len)
14982 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014983 else
14984 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014985 item = list_find(l, before);
14986 if (item == NULL)
14987 {
14988 EMSGN(_(e_listidx), before);
14989 l = NULL;
14990 }
14991 }
14992 if (l != NULL)
14993 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014994 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014995 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014996 }
14997 }
14998}
14999
15000/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015001 * "invert(expr)" function
15002 */
15003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015004f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015005{
15006 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15007}
15008
15009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010 * "isdirectory()" function
15011 */
15012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015013f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016}
15017
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015018/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015019 * "islocked()" function
15020 */
15021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015022f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015023{
15024 lval_T lv;
15025 char_u *end;
15026 dictitem_T *di;
15027
15028 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015029 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15030 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015031 if (end != NULL && lv.ll_name != NULL)
15032 {
15033 if (*end != NUL)
15034 EMSG(_(e_trailing));
15035 else
15036 {
15037 if (lv.ll_tv == NULL)
15038 {
15039 if (check_changedtick(lv.ll_name))
15040 rettv->vval.v_number = 1; /* always locked */
15041 else
15042 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015043 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015044 if (di != NULL)
15045 {
15046 /* Consider a variable locked when:
15047 * 1. the variable itself is locked
15048 * 2. the value of the variable is locked.
15049 * 3. the List or Dict value is locked.
15050 */
15051 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15052 || tv_islocked(&di->di_tv));
15053 }
15054 }
15055 }
15056 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015057 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015058 else if (lv.ll_newkey != NULL)
15059 EMSG2(_(e_dictkey), lv.ll_newkey);
15060 else if (lv.ll_list != NULL)
15061 /* List item. */
15062 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15063 else
15064 /* Dictionary item. */
15065 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15066 }
15067 }
15068
15069 clear_lval(&lv);
15070}
15071
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015072#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15073/*
15074 * "isnan()" function
15075 */
15076 static void
15077f_isnan(typval_T *argvars, typval_T *rettv)
15078{
15079 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15080 && isnan(argvars[0].vval.v_float);
15081}
15082#endif
15083
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015084static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015085
15086/*
15087 * Turn a dict into a list:
15088 * "what" == 0: list of keys
15089 * "what" == 1: list of values
15090 * "what" == 2: list of items
15091 */
15092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015093dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015094{
Bram Moolenaar33570922005-01-25 22:26:29 +000015095 list_T *l2;
15096 dictitem_T *di;
15097 hashitem_T *hi;
15098 listitem_T *li;
15099 listitem_T *li2;
15100 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015101 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015102
Bram Moolenaar8c711452005-01-14 21:53:12 +000015103 if (argvars[0].v_type != VAR_DICT)
15104 {
15105 EMSG(_(e_dictreq));
15106 return;
15107 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015108 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015109 return;
15110
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015111 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015112 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015113
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015114 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015115 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015116 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015117 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015118 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015119 --todo;
15120 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015121
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015122 li = listitem_alloc();
15123 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015124 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015125 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015126
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015127 if (what == 0)
15128 {
15129 /* keys() */
15130 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015131 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015132 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15133 }
15134 else if (what == 1)
15135 {
15136 /* values() */
15137 copy_tv(&di->di_tv, &li->li_tv);
15138 }
15139 else
15140 {
15141 /* items() */
15142 l2 = list_alloc();
15143 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015144 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015145 li->li_tv.vval.v_list = l2;
15146 if (l2 == NULL)
15147 break;
15148 ++l2->lv_refcount;
15149
15150 li2 = listitem_alloc();
15151 if (li2 == NULL)
15152 break;
15153 list_append(l2, li2);
15154 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015155 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015156 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15157
15158 li2 = listitem_alloc();
15159 if (li2 == NULL)
15160 break;
15161 list_append(l2, li2);
15162 copy_tv(&di->di_tv, &li2->li_tv);
15163 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015164 }
15165 }
15166}
15167
15168/*
15169 * "items(dict)" function
15170 */
15171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015172f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015173{
15174 dict_list(argvars, rettv, 2);
15175}
15176
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015177#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015178/*
15179 * Get the job from the argument.
15180 * Returns NULL if the job is invalid.
15181 */
15182 static job_T *
15183get_job_arg(typval_T *tv)
15184{
15185 job_T *job;
15186
15187 if (tv->v_type != VAR_JOB)
15188 {
15189 EMSG2(_(e_invarg2), get_tv_string(tv));
15190 return NULL;
15191 }
15192 job = tv->vval.v_job;
15193
15194 if (job == NULL)
15195 EMSG(_("E916: not a valid job"));
15196 return job;
15197}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015198
Bram Moolenaar835dc632016-02-07 14:27:38 +010015199/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015200 * "job_getchannel()" function
15201 */
15202 static void
15203f_job_getchannel(typval_T *argvars, typval_T *rettv)
15204{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015205 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015206
Bram Moolenaar65edff82016-02-21 16:40:11 +010015207 if (job != NULL)
15208 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015209 rettv->v_type = VAR_CHANNEL;
15210 rettv->vval.v_channel = job->jv_channel;
15211 if (job->jv_channel != NULL)
15212 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015213 }
15214}
15215
15216/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015217 * "job_info()" function
15218 */
15219 static void
15220f_job_info(typval_T *argvars, typval_T *rettv)
15221{
15222 job_T *job = get_job_arg(&argvars[0]);
15223
15224 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15225 job_info(job, rettv->vval.v_dict);
15226}
15227
15228/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015229 * "job_setoptions()" function
15230 */
15231 static void
15232f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15233{
15234 job_T *job = get_job_arg(&argvars[0]);
15235 jobopt_T opt;
15236
15237 if (job == NULL)
15238 return;
15239 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015240 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15241 job_set_options(job, &opt);
15242 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015243}
15244
15245/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015246 * "job_start()" function
15247 */
15248 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015249f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015250{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015251 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015252 if (check_restricted() || check_secure())
15253 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015254 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015255}
15256
15257/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015258 * "job_status()" function
15259 */
15260 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015261f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015262{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015263 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015264
Bram Moolenaar65edff82016-02-21 16:40:11 +010015265 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015266 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015267 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015268 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015269 }
15270}
15271
15272/*
15273 * "job_stop()" function
15274 */
15275 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015276f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015277{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015278 job_T *job = get_job_arg(&argvars[0]);
15279
15280 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015281 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015282}
15283#endif
15284
Bram Moolenaar071d4272004-06-13 20:20:40 +000015285/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015286 * "join()" function
15287 */
15288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015289f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015290{
15291 garray_T ga;
15292 char_u *sep;
15293
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015294 if (argvars[0].v_type != VAR_LIST)
15295 {
15296 EMSG(_(e_listreq));
15297 return;
15298 }
15299 if (argvars[0].vval.v_list == NULL)
15300 return;
15301 if (argvars[1].v_type == VAR_UNKNOWN)
15302 sep = (char_u *)" ";
15303 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015304 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015305
15306 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015307
15308 if (sep != NULL)
15309 {
15310 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015311 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015312 ga_append(&ga, NUL);
15313 rettv->vval.v_string = (char_u *)ga.ga_data;
15314 }
15315 else
15316 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015317}
15318
15319/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015320 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015321 */
15322 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015323f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015324{
15325 js_read_T reader;
15326
15327 reader.js_buf = get_tv_string(&argvars[0]);
15328 reader.js_fill = NULL;
15329 reader.js_used = 0;
15330 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15331 EMSG(_(e_invarg));
15332}
15333
15334/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015335 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015336 */
15337 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015338f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015339{
15340 rettv->v_type = VAR_STRING;
15341 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15342}
15343
15344/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015345 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015346 */
15347 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015348f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015349{
15350 js_read_T reader;
15351
15352 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015353 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015354 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015355 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015356 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015357}
15358
15359/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015360 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015361 */
15362 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015363f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015364{
15365 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015366 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015367}
15368
15369/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015370 * "keys()" function
15371 */
15372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015373f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015374{
15375 dict_list(argvars, rettv, 0);
15376}
15377
15378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 * "last_buffer_nr()" function.
15380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015382f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383{
15384 int n = 0;
15385 buf_T *buf;
15386
15387 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15388 if (n < buf->b_fnum)
15389 n = buf->b_fnum;
15390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015392}
15393
15394/*
15395 * "len()" function
15396 */
15397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015398f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015399{
15400 switch (argvars[0].v_type)
15401 {
15402 case VAR_STRING:
15403 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 rettv->vval.v_number = (varnumber_T)STRLEN(
15405 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015406 break;
15407 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015408 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015409 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015410 case VAR_DICT:
15411 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15412 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015413 case VAR_UNKNOWN:
15414 case VAR_SPECIAL:
15415 case VAR_FLOAT:
15416 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015417 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015418 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015419 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015420 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015421 break;
15422 }
15423}
15424
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015425static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015426
15427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015428libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015429{
15430#ifdef FEAT_LIBCALL
15431 char_u *string_in;
15432 char_u **string_result;
15433 int nr_result;
15434#endif
15435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015436 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015437 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015438 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015439
15440 if (check_restricted() || check_secure())
15441 return;
15442
15443#ifdef FEAT_LIBCALL
15444 /* The first two args must be strings, otherwise its meaningless */
15445 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15446 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015447 string_in = NULL;
15448 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015449 string_in = argvars[2].vval.v_string;
15450 if (type == VAR_NUMBER)
15451 string_result = NULL;
15452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015453 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015454 if (mch_libcall(argvars[0].vval.v_string,
15455 argvars[1].vval.v_string,
15456 string_in,
15457 argvars[2].vval.v_number,
15458 string_result,
15459 &nr_result) == OK
15460 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015461 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015462 }
15463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464}
15465
15466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015467 * "libcall()" function
15468 */
15469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015470f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015471{
15472 libcall_common(argvars, rettv, VAR_STRING);
15473}
15474
15475/*
15476 * "libcallnr()" function
15477 */
15478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015479f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015480{
15481 libcall_common(argvars, rettv, VAR_NUMBER);
15482}
15483
15484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 * "line(string)" function
15486 */
15487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015488f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489{
15490 linenr_T lnum = 0;
15491 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015492 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015494 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 if (fp != NULL)
15496 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015497 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498}
15499
15500/*
15501 * "line2byte(lnum)" function
15502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015504f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505{
15506#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015507 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508#else
15509 linenr_T lnum;
15510
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015511 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015513 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015515 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15516 if (rettv->vval.v_number >= 0)
15517 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015518#endif
15519}
15520
15521/*
15522 * "lispindent(lnum)" function
15523 */
15524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015525f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526{
15527#ifdef FEAT_LISP
15528 pos_T pos;
15529 linenr_T lnum;
15530
15531 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015532 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15534 {
15535 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015536 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537 curwin->w_cursor = pos;
15538 }
15539 else
15540#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015541 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542}
15543
15544/*
15545 * "localtime()" function
15546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015548f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015550 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551}
15552
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015553static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554
15555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015556get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557{
15558 char_u *keys;
15559 char_u *which;
15560 char_u buf[NUMBUFLEN];
15561 char_u *keys_buf = NULL;
15562 char_u *rhs;
15563 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015564 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015565 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015566 mapblock_T *mp;
15567 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568
15569 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015570 rettv->v_type = VAR_STRING;
15571 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015573 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 if (*keys == NUL)
15575 return;
15576
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015577 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015579 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015580 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015581 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015582 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015583 if (argvars[3].v_type != VAR_UNKNOWN)
15584 get_dict = get_tv_number(&argvars[3]);
15585 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 else
15588 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015589 if (which == NULL)
15590 return;
15591
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 mode = get_map_mode(&which, 0);
15593
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015594 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015595 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015597
15598 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015600 /* Return a string. */
15601 if (rhs != NULL)
15602 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603
Bram Moolenaarbd743252010-10-20 21:23:33 +020015604 }
15605 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15606 {
15607 /* Return a dictionary. */
15608 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15609 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15610 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611
Bram Moolenaarbd743252010-10-20 21:23:33 +020015612 dict_add_nr_str(dict, "lhs", 0L, lhs);
15613 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15614 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15615 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15616 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15617 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15618 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015619 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015620 dict_add_nr_str(dict, "mode", 0L, mapmode);
15621
15622 vim_free(lhs);
15623 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 }
15625}
15626
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015627#ifdef FEAT_FLOAT
15628/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015629 * "log()" function
15630 */
15631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015632f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015633{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015634 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015635
15636 rettv->v_type = VAR_FLOAT;
15637 if (get_float_arg(argvars, &f) == OK)
15638 rettv->vval.v_float = log(f);
15639 else
15640 rettv->vval.v_float = 0.0;
15641}
15642
15643/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015644 * "log10()" function
15645 */
15646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015647f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015648{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015649 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015650
15651 rettv->v_type = VAR_FLOAT;
15652 if (get_float_arg(argvars, &f) == OK)
15653 rettv->vval.v_float = log10(f);
15654 else
15655 rettv->vval.v_float = 0.0;
15656}
15657#endif
15658
Bram Moolenaar1dced572012-04-05 16:54:08 +020015659#ifdef FEAT_LUA
15660/*
15661 * "luaeval()" function
15662 */
15663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015664f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015665{
15666 char_u *str;
15667 char_u buf[NUMBUFLEN];
15668
15669 str = get_tv_string_buf(&argvars[0], buf);
15670 do_luaeval(str, argvars + 1, rettv);
15671}
15672#endif
15673
Bram Moolenaar071d4272004-06-13 20:20:40 +000015674/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015675 * "map()" function
15676 */
15677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015678f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015679{
15680 filter_map(argvars, rettv, TRUE);
15681}
15682
15683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015684 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685 */
15686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015687f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015689 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690}
15691
15692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015693 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 */
15695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015696f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015698 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699}
15700
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015701static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702
15703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015704find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015706 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015707 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015708 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 char_u *pat;
15710 regmatch_T regmatch;
15711 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015712 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713 char_u *save_cpo;
15714 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015715 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015716 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015717 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015718 list_T *l = NULL;
15719 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015720 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015721 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722
15723 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15724 save_cpo = p_cpo;
15725 p_cpo = (char_u *)"";
15726
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015727 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015728 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015729 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015730 /* type 3: return empty list when there are no matches.
15731 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015732 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015733 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015734 if (type == 4
15735 && (list_append_string(rettv->vval.v_list,
15736 (char_u *)"", 0) == FAIL
15737 || list_append_number(rettv->vval.v_list,
15738 (varnumber_T)-1) == FAIL
15739 || list_append_number(rettv->vval.v_list,
15740 (varnumber_T)-1) == FAIL
15741 || list_append_number(rettv->vval.v_list,
15742 (varnumber_T)-1) == FAIL))
15743 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015744 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015745 rettv->vval.v_list = NULL;
15746 goto theend;
15747 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015748 }
15749 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015751 rettv->v_type = VAR_STRING;
15752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015755 if (argvars[0].v_type == VAR_LIST)
15756 {
15757 if ((l = argvars[0].vval.v_list) == NULL)
15758 goto theend;
15759 li = l->lv_first;
15760 }
15761 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015762 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015763 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015764 len = (long)STRLEN(str);
15765 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015767 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15768 if (pat == NULL)
15769 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015770
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015771 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015773 int error = FALSE;
15774
15775 start = get_tv_number_chk(&argvars[2], &error);
15776 if (error)
15777 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015778 if (l != NULL)
15779 {
15780 li = list_find(l, start);
15781 if (li == NULL)
15782 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015783 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015784 }
15785 else
15786 {
15787 if (start < 0)
15788 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015789 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015790 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015791 /* When "count" argument is there ignore matches before "start",
15792 * otherwise skip part of the string. Differs when pattern is "^"
15793 * or "\<". */
15794 if (argvars[3].v_type != VAR_UNKNOWN)
15795 startcol = start;
15796 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015797 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015798 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015799 len -= start;
15800 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015801 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015802
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015803 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015804 nth = get_tv_number_chk(&argvars[3], &error);
15805 if (error)
15806 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015807 }
15808
15809 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15810 if (regmatch.regprog != NULL)
15811 {
15812 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015813
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015814 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015815 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015816 if (l != NULL)
15817 {
15818 if (li == NULL)
15819 {
15820 match = FALSE;
15821 break;
15822 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015823 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015824 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015825 if (str == NULL)
15826 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015827 }
15828
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015829 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015830
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015831 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015832 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015833 if (l == NULL && !match)
15834 break;
15835
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015836 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015837 if (l != NULL)
15838 {
15839 li = li->li_next;
15840 ++idx;
15841 }
15842 else
15843 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015844#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015845 startcol = (colnr_T)(regmatch.startp[0]
15846 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015847#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015848 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015849#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015850 if (startcol > (colnr_T)len
15851 || str + startcol <= regmatch.startp[0])
15852 {
15853 match = FALSE;
15854 break;
15855 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015856 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015857 }
15858
15859 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015860 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015861 if (type == 4)
15862 {
15863 listitem_T *li1 = rettv->vval.v_list->lv_first;
15864 listitem_T *li2 = li1->li_next;
15865 listitem_T *li3 = li2->li_next;
15866 listitem_T *li4 = li3->li_next;
15867
Bram Moolenaar3c809342016-06-01 22:34:48 +020015868 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015869 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15870 (int)(regmatch.endp[0] - regmatch.startp[0]));
15871 li3->li_tv.vval.v_number =
15872 (varnumber_T)(regmatch.startp[0] - expr);
15873 li4->li_tv.vval.v_number =
15874 (varnumber_T)(regmatch.endp[0] - expr);
15875 if (l != NULL)
15876 li2->li_tv.vval.v_number = (varnumber_T)idx;
15877 }
15878 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015879 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015880 int i;
15881
15882 /* return list with matched string and submatches */
15883 for (i = 0; i < NSUBEXP; ++i)
15884 {
15885 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015886 {
15887 if (list_append_string(rettv->vval.v_list,
15888 (char_u *)"", 0) == FAIL)
15889 break;
15890 }
15891 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015892 regmatch.startp[i],
15893 (int)(regmatch.endp[i] - regmatch.startp[i]))
15894 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015895 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015896 }
15897 }
15898 else if (type == 2)
15899 {
15900 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015901 if (l != NULL)
15902 copy_tv(&li->li_tv, rettv);
15903 else
15904 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015906 }
15907 else if (l != NULL)
15908 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909 else
15910 {
15911 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015912 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 (varnumber_T)(regmatch.startp[0] - str);
15914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015915 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015917 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918 }
15919 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015920 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921 }
15922
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015923 if (type == 4 && l == NULL)
15924 /* matchstrpos() without a list: drop the second item. */
15925 listitem_remove(rettv->vval.v_list,
15926 rettv->vval.v_list->lv_first->li_next);
15927
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015929 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015930 p_cpo = save_cpo;
15931}
15932
15933/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015934 * "match()" function
15935 */
15936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015937f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015938{
15939 find_some_match(argvars, rettv, 1);
15940}
15941
15942/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015943 * "matchadd()" function
15944 */
15945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015946f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015947{
15948#ifdef FEAT_SEARCH_EXTRA
15949 char_u buf[NUMBUFLEN];
15950 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15951 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15952 int prio = 10; /* default priority */
15953 int id = -1;
15954 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015955 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015956
15957 rettv->vval.v_number = -1;
15958
15959 if (grp == NULL || pat == NULL)
15960 return;
15961 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015962 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015963 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015964 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015965 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015966 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015967 if (argvars[4].v_type != VAR_UNKNOWN)
15968 {
15969 if (argvars[4].v_type != VAR_DICT)
15970 {
15971 EMSG(_(e_dictreq));
15972 return;
15973 }
15974 if (dict_find(argvars[4].vval.v_dict,
15975 (char_u *)"conceal", -1) != NULL)
15976 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15977 (char_u *)"conceal", FALSE);
15978 }
15979 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015980 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015981 if (error == TRUE)
15982 return;
15983 if (id >= 1 && id <= 3)
15984 {
15985 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15986 return;
15987 }
15988
Bram Moolenaar6561d522015-07-21 15:48:27 +020015989 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15990 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015991#endif
15992}
15993
15994/*
15995 * "matchaddpos()" function
15996 */
15997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015998f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015999{
16000#ifdef FEAT_SEARCH_EXTRA
16001 char_u buf[NUMBUFLEN];
16002 char_u *group;
16003 int prio = 10;
16004 int id = -1;
16005 int error = FALSE;
16006 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016007 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016008
16009 rettv->vval.v_number = -1;
16010
16011 group = get_tv_string_buf_chk(&argvars[0], buf);
16012 if (group == NULL)
16013 return;
16014
16015 if (argvars[1].v_type != VAR_LIST)
16016 {
16017 EMSG2(_(e_listarg), "matchaddpos()");
16018 return;
16019 }
16020 l = argvars[1].vval.v_list;
16021 if (l == NULL)
16022 return;
16023
16024 if (argvars[2].v_type != VAR_UNKNOWN)
16025 {
16026 prio = get_tv_number_chk(&argvars[2], &error);
16027 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016028 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016029 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016030 if (argvars[4].v_type != VAR_UNKNOWN)
16031 {
16032 if (argvars[4].v_type != VAR_DICT)
16033 {
16034 EMSG(_(e_dictreq));
16035 return;
16036 }
16037 if (dict_find(argvars[4].vval.v_dict,
16038 (char_u *)"conceal", -1) != NULL)
16039 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16040 (char_u *)"conceal", FALSE);
16041 }
16042 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016043 }
16044 if (error == TRUE)
16045 return;
16046
16047 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16048 if (id == 1 || id == 2)
16049 {
16050 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16051 return;
16052 }
16053
Bram Moolenaar6561d522015-07-21 15:48:27 +020016054 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16055 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016056#endif
16057}
16058
16059/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016060 * "matcharg()" function
16061 */
16062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016063f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016064{
16065 if (rettv_list_alloc(rettv) == OK)
16066 {
16067#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016068 int id = get_tv_number(&argvars[0]);
16069 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016070
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016071 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016072 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016073 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16074 {
16075 list_append_string(rettv->vval.v_list,
16076 syn_id2name(m->hlg_id), -1);
16077 list_append_string(rettv->vval.v_list, m->pattern, -1);
16078 }
16079 else
16080 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016081 list_append_string(rettv->vval.v_list, NULL, -1);
16082 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016083 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016084 }
16085#endif
16086 }
16087}
16088
16089/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016090 * "matchdelete()" function
16091 */
16092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016093f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016094{
16095#ifdef FEAT_SEARCH_EXTRA
16096 rettv->vval.v_number = match_delete(curwin,
16097 (int)get_tv_number(&argvars[0]), TRUE);
16098#endif
16099}
16100
16101/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016102 * "matchend()" function
16103 */
16104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016105f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016106{
16107 find_some_match(argvars, rettv, 0);
16108}
16109
16110/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016111 * "matchlist()" function
16112 */
16113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016114f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016115{
16116 find_some_match(argvars, rettv, 3);
16117}
16118
16119/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016120 * "matchstr()" function
16121 */
16122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016123f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016124{
16125 find_some_match(argvars, rettv, 2);
16126}
16127
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016128/*
16129 * "matchstrpos()" function
16130 */
16131 static void
16132f_matchstrpos(typval_T *argvars, typval_T *rettv)
16133{
16134 find_some_match(argvars, rettv, 4);
16135}
16136
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016137static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016138
16139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016140max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016141{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016142 long n = 0;
16143 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016144 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016145
16146 if (argvars[0].v_type == VAR_LIST)
16147 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016148 list_T *l;
16149 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016150
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016151 l = argvars[0].vval.v_list;
16152 if (l != NULL)
16153 {
16154 li = l->lv_first;
16155 if (li != NULL)
16156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016157 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016158 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016159 {
16160 li = li->li_next;
16161 if (li == NULL)
16162 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016163 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016164 if (domax ? i > n : i < n)
16165 n = i;
16166 }
16167 }
16168 }
16169 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016170 else if (argvars[0].v_type == VAR_DICT)
16171 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016172 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016173 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016174 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016175 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016176
16177 d = argvars[0].vval.v_dict;
16178 if (d != NULL)
16179 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016180 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016182 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016183 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016184 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016185 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016186 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016187 if (first)
16188 {
16189 n = i;
16190 first = FALSE;
16191 }
16192 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016193 n = i;
16194 }
16195 }
16196 }
16197 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016198 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016199 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016200 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016201}
16202
16203/*
16204 * "max()" function
16205 */
16206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016207f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016208{
16209 max_min(argvars, rettv, TRUE);
16210}
16211
16212/*
16213 * "min()" function
16214 */
16215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016216f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016217{
16218 max_min(argvars, rettv, FALSE);
16219}
16220
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016221static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016222
16223/*
16224 * Create the directory in which "dir" is located, and higher levels when
16225 * needed.
16226 */
16227 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016228mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016229{
16230 char_u *p;
16231 char_u *updir;
16232 int r = FAIL;
16233
16234 /* Get end of directory name in "dir".
16235 * We're done when it's "/" or "c:/". */
16236 p = gettail_sep(dir);
16237 if (p <= get_past_head(dir))
16238 return OK;
16239
16240 /* If the directory exists we're done. Otherwise: create it.*/
16241 updir = vim_strnsave(dir, (int)(p - dir));
16242 if (updir == NULL)
16243 return FAIL;
16244 if (mch_isdir(updir))
16245 r = OK;
16246 else if (mkdir_recurse(updir, prot) == OK)
16247 r = vim_mkdir_emsg(updir, prot);
16248 vim_free(updir);
16249 return r;
16250}
16251
16252#ifdef vim_mkdir
16253/*
16254 * "mkdir()" function
16255 */
16256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016257f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016258{
16259 char_u *dir;
16260 char_u buf[NUMBUFLEN];
16261 int prot = 0755;
16262
16263 rettv->vval.v_number = FAIL;
16264 if (check_restricted() || check_secure())
16265 return;
16266
16267 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016268 if (*dir == NUL)
16269 rettv->vval.v_number = FAIL;
16270 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016271 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016272 if (*gettail(dir) == NUL)
16273 /* remove trailing slashes */
16274 *gettail_sep(dir) = NUL;
16275
16276 if (argvars[1].v_type != VAR_UNKNOWN)
16277 {
16278 if (argvars[2].v_type != VAR_UNKNOWN)
16279 prot = get_tv_number_chk(&argvars[2], NULL);
16280 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16281 mkdir_recurse(dir, prot);
16282 }
16283 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016284 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016285}
16286#endif
16287
Bram Moolenaar0d660222005-01-07 21:51:51 +000016288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 * "mode()" function
16290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016292f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016294 char_u buf[3];
16295
16296 buf[1] = NUL;
16297 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 if (VIsual_active)
16300 {
16301 if (VIsual_select)
16302 buf[0] = VIsual_mode + 's' - 'v';
16303 else
16304 buf[0] = VIsual_mode;
16305 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016306 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016307 || State == CONFIRM)
16308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016310 if (State == ASKMORE)
16311 buf[1] = 'm';
16312 else if (State == CONFIRM)
16313 buf[1] = '?';
16314 }
16315 else if (State == EXTERNCMD)
16316 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317 else if (State & INSERT)
16318 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016319#ifdef FEAT_VREPLACE
16320 if (State & VREPLACE_FLAG)
16321 {
16322 buf[0] = 'R';
16323 buf[1] = 'v';
16324 }
16325 else
16326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327 if (State & REPLACE_FLAG)
16328 buf[0] = 'R';
16329 else
16330 buf[0] = 'i';
16331 }
16332 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016335 if (exmode_active)
16336 buf[1] = 'v';
16337 }
16338 else if (exmode_active)
16339 {
16340 buf[0] = 'c';
16341 buf[1] = 'e';
16342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016346 if (finish_op)
16347 buf[1] = 'o';
16348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016350 /* Clear out the minor mode when the argument is not a non-zero number or
16351 * non-empty string. */
16352 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016353 buf[1] = NUL;
16354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016355 rettv->vval.v_string = vim_strsave(buf);
16356 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357}
16358
Bram Moolenaar429fa852013-04-15 12:27:36 +020016359#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016360/*
16361 * "mzeval()" function
16362 */
16363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016364f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016365{
16366 char_u *str;
16367 char_u buf[NUMBUFLEN];
16368
16369 str = get_tv_string_buf(&argvars[0], buf);
16370 do_mzeval(str, rettv);
16371}
Bram Moolenaar75676462013-01-30 14:55:42 +010016372
16373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016374mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016375{
16376 typval_T argvars[3];
16377
16378 argvars[0].v_type = VAR_STRING;
16379 argvars[0].vval.v_string = name;
16380 copy_tv(args, &argvars[1]);
16381 argvars[2].v_type = VAR_UNKNOWN;
16382 f_call(argvars, rettv);
16383 clear_tv(&argvars[1]);
16384}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016385#endif
16386
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016388 * "nextnonblank()" function
16389 */
16390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016391f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392{
16393 linenr_T lnum;
16394
16395 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016397 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016398 {
16399 lnum = 0;
16400 break;
16401 }
16402 if (*skipwhite(ml_get(lnum)) != NUL)
16403 break;
16404 }
16405 rettv->vval.v_number = lnum;
16406}
16407
16408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016409 * "nr2char()" function
16410 */
16411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016412f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413{
16414 char_u buf[NUMBUFLEN];
16415
16416#ifdef FEAT_MBYTE
16417 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016418 {
16419 int utf8 = 0;
16420
16421 if (argvars[1].v_type != VAR_UNKNOWN)
16422 utf8 = get_tv_number_chk(&argvars[1], NULL);
16423 if (utf8)
16424 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16425 else
16426 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 else
16429#endif
16430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016431 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432 buf[1] = NUL;
16433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016434 rettv->v_type = VAR_STRING;
16435 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016436}
16437
16438/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016439 * "or(expr, expr)" function
16440 */
16441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016442f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016443{
16444 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16445 | get_tv_number_chk(&argvars[1], NULL);
16446}
16447
16448/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016449 * "pathshorten()" function
16450 */
16451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016452f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016453{
16454 char_u *p;
16455
16456 rettv->v_type = VAR_STRING;
16457 p = get_tv_string_chk(&argvars[0]);
16458 if (p == NULL)
16459 rettv->vval.v_string = NULL;
16460 else
16461 {
16462 p = vim_strsave(p);
16463 rettv->vval.v_string = p;
16464 if (p != NULL)
16465 shorten_dir(p);
16466 }
16467}
16468
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016469#ifdef FEAT_PERL
16470/*
16471 * "perleval()" function
16472 */
16473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016474f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016475{
16476 char_u *str;
16477 char_u buf[NUMBUFLEN];
16478
16479 str = get_tv_string_buf(&argvars[0], buf);
16480 do_perleval(str, rettv);
16481}
16482#endif
16483
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016484#ifdef FEAT_FLOAT
16485/*
16486 * "pow()" function
16487 */
16488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016489f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016490{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016491 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016492
16493 rettv->v_type = VAR_FLOAT;
16494 if (get_float_arg(argvars, &fx) == OK
16495 && get_float_arg(&argvars[1], &fy) == OK)
16496 rettv->vval.v_float = pow(fx, fy);
16497 else
16498 rettv->vval.v_float = 0.0;
16499}
16500#endif
16501
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016502/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016503 * "prevnonblank()" function
16504 */
16505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016506f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016507{
16508 linenr_T lnum;
16509
16510 lnum = get_tv_lnum(argvars);
16511 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16512 lnum = 0;
16513 else
16514 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16515 --lnum;
16516 rettv->vval.v_number = lnum;
16517}
16518
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016519/* This dummy va_list is here because:
16520 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16521 * - locally in the function results in a "used before set" warning
16522 * - using va_start() to initialize it gives "function with fixed args" error */
16523static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016524
Bram Moolenaar8c711452005-01-14 21:53:12 +000016525/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016526 * "printf()" function
16527 */
16528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016529f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016530{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016531 char_u buf[NUMBUFLEN];
16532 int len;
16533 char_u *s;
16534 int saved_did_emsg = did_emsg;
16535 char *fmt;
16536
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016537 rettv->v_type = VAR_STRING;
16538 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016539
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016540 /* Get the required length, allocate the buffer and do it for real. */
16541 did_emsg = FALSE;
16542 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16543 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16544 if (!did_emsg)
16545 {
16546 s = alloc(len + 1);
16547 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016548 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016549 rettv->vval.v_string = s;
16550 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016551 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016552 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016553 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016554}
16555
16556/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016557 * "pumvisible()" function
16558 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016560f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016561{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016562#ifdef FEAT_INS_EXPAND
16563 if (pum_visible())
16564 rettv->vval.v_number = 1;
16565#endif
16566}
16567
Bram Moolenaardb913952012-06-29 12:54:53 +020016568#ifdef FEAT_PYTHON3
16569/*
16570 * "py3eval()" function
16571 */
16572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016573f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016574{
16575 char_u *str;
16576 char_u buf[NUMBUFLEN];
16577
16578 str = get_tv_string_buf(&argvars[0], buf);
16579 do_py3eval(str, rettv);
16580}
16581#endif
16582
16583#ifdef FEAT_PYTHON
16584/*
16585 * "pyeval()" function
16586 */
16587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016588f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016589{
16590 char_u *str;
16591 char_u buf[NUMBUFLEN];
16592
16593 str = get_tv_string_buf(&argvars[0], buf);
16594 do_pyeval(str, rettv);
16595}
16596#endif
16597
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016598/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016599 * "range()" function
16600 */
16601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016602f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016603{
16604 long start;
16605 long end;
16606 long stride = 1;
16607 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016608 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016610 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016611 if (argvars[1].v_type == VAR_UNKNOWN)
16612 {
16613 end = start - 1;
16614 start = 0;
16615 }
16616 else
16617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016618 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016619 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016620 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016621 }
16622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016623 if (error)
16624 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016625 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016626 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016627 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016628 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016629 else
16630 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016631 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016632 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016633 if (list_append_number(rettv->vval.v_list,
16634 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016635 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016636 }
16637}
16638
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016639/*
16640 * "readfile()" function
16641 */
16642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016643f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016644{
16645 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016646 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016647 char_u *fname;
16648 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016649 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16650 int io_size = sizeof(buf);
16651 int readlen; /* size of last fread() */
16652 char_u *prev = NULL; /* previously read bytes, if any */
16653 long prevlen = 0; /* length of data in prev */
16654 long prevsize = 0; /* size of prev buffer */
16655 long maxline = MAXLNUM;
16656 long cnt = 0;
16657 char_u *p; /* position in buf */
16658 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016659
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016660 if (argvars[1].v_type != VAR_UNKNOWN)
16661 {
16662 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16663 binary = TRUE;
16664 if (argvars[2].v_type != VAR_UNKNOWN)
16665 maxline = get_tv_number(&argvars[2]);
16666 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016667
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016668 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016669 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016670
16671 /* Always open the file in binary mode, library functions have a mind of
16672 * their own about CR-LF conversion. */
16673 fname = get_tv_string(&argvars[0]);
16674 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16675 {
16676 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16677 return;
16678 }
16679
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016680 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016681 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016682 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016683
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016684 /* This for loop processes what was read, but is also entered at end
16685 * of file so that either:
16686 * - an incomplete line gets written
16687 * - a "binary" file gets an empty line at the end if it ends in a
16688 * newline. */
16689 for (p = buf, start = buf;
16690 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16691 ++p)
16692 {
16693 if (*p == '\n' || readlen <= 0)
16694 {
16695 listitem_T *li;
16696 char_u *s = NULL;
16697 long_u len = p - start;
16698
16699 /* Finished a line. Remove CRs before NL. */
16700 if (readlen > 0 && !binary)
16701 {
16702 while (len > 0 && start[len - 1] == '\r')
16703 --len;
16704 /* removal may cross back to the "prev" string */
16705 if (len == 0)
16706 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16707 --prevlen;
16708 }
16709 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016710 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016711 else
16712 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016713 /* Change "prev" buffer to be the right size. This way
16714 * the bytes are only copied once, and very long lines are
16715 * allocated only once. */
16716 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016717 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016718 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016719 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016720 prev = NULL; /* the list will own the string */
16721 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016722 }
16723 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016724 if (s == NULL)
16725 {
16726 do_outofmem_msg((long_u) prevlen + len + 1);
16727 failed = TRUE;
16728 break;
16729 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016730
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016731 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016732 {
16733 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016734 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016735 break;
16736 }
16737 li->li_tv.v_type = VAR_STRING;
16738 li->li_tv.v_lock = 0;
16739 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016740 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016741
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016742 start = p + 1; /* step over newline */
16743 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016744 break;
16745 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016746 else if (*p == NUL)
16747 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016748#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016749 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16750 * when finding the BF and check the previous two bytes. */
16751 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016752 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016753 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16754 * + 1, these may be in the "prev" string. */
16755 char_u back1 = p >= buf + 1 ? p[-1]
16756 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16757 char_u back2 = p >= buf + 2 ? p[-2]
16758 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16759 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016760
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016761 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016762 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016763 char_u *dest = p - 2;
16764
16765 /* Usually a BOM is at the beginning of a file, and so at
16766 * the beginning of a line; then we can just step over it.
16767 */
16768 if (start == dest)
16769 start = p + 1;
16770 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016771 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016772 /* have to shuffle buf to close gap */
16773 int adjust_prevlen = 0;
16774
16775 if (dest < buf)
16776 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016777 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016778 dest = buf;
16779 }
16780 if (readlen > p - buf + 1)
16781 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16782 readlen -= 3 - adjust_prevlen;
16783 prevlen -= adjust_prevlen;
16784 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016785 }
16786 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016787 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016788#endif
16789 } /* for */
16790
16791 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16792 break;
16793 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016794 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016795 /* There's part of a line in buf, store it in "prev". */
16796 if (p - start + prevlen >= prevsize)
16797 {
16798 /* need bigger "prev" buffer */
16799 char_u *newprev;
16800
16801 /* A common use case is ordinary text files and "prev" gets a
16802 * fragment of a line, so the first allocation is made
16803 * small, to avoid repeatedly 'allocing' large and
16804 * 'reallocing' small. */
16805 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016806 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016807 else
16808 {
16809 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016810 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016811 prevsize = grow50pc > growmin ? grow50pc : growmin;
16812 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016813 newprev = prev == NULL ? alloc(prevsize)
16814 : vim_realloc(prev, prevsize);
16815 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016816 {
16817 do_outofmem_msg((long_u)prevsize);
16818 failed = TRUE;
16819 break;
16820 }
16821 prev = newprev;
16822 }
16823 /* Add the line part to end of "prev". */
16824 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016825 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016826 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016827 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016828
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016829 /*
16830 * For a negative line count use only the lines at the end of the file,
16831 * free the rest.
16832 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016833 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016834 while (cnt > -maxline)
16835 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016836 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016837 --cnt;
16838 }
16839
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016840 if (failed)
16841 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016842 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016843 /* readfile doc says an empty list is returned on error */
16844 rettv->vval.v_list = list_alloc();
16845 }
16846
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016847 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016848 fclose(fd);
16849}
16850
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016851#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016852static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016853
16854/*
16855 * Convert a List to proftime_T.
16856 * Return FAIL when there is something wrong.
16857 */
16858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016859list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016860{
16861 long n1, n2;
16862 int error = FALSE;
16863
16864 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16865 || arg->vval.v_list->lv_len != 2)
16866 return FAIL;
16867 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16868 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16869# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016870 tm->HighPart = n1;
16871 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016872# else
16873 tm->tv_sec = n1;
16874 tm->tv_usec = n2;
16875# endif
16876 return error ? FAIL : OK;
16877}
16878#endif /* FEAT_RELTIME */
16879
16880/*
16881 * "reltime()" function
16882 */
16883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016884f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016885{
16886#ifdef FEAT_RELTIME
16887 proftime_T res;
16888 proftime_T start;
16889
16890 if (argvars[0].v_type == VAR_UNKNOWN)
16891 {
16892 /* No arguments: get current time. */
16893 profile_start(&res);
16894 }
16895 else if (argvars[1].v_type == VAR_UNKNOWN)
16896 {
16897 if (list2proftime(&argvars[0], &res) == FAIL)
16898 return;
16899 profile_end(&res);
16900 }
16901 else
16902 {
16903 /* Two arguments: compute the difference. */
16904 if (list2proftime(&argvars[0], &start) == FAIL
16905 || list2proftime(&argvars[1], &res) == FAIL)
16906 return;
16907 profile_sub(&res, &start);
16908 }
16909
16910 if (rettv_list_alloc(rettv) == OK)
16911 {
16912 long n1, n2;
16913
16914# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016915 n1 = res.HighPart;
16916 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016917# else
16918 n1 = res.tv_sec;
16919 n2 = res.tv_usec;
16920# endif
16921 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16922 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16923 }
16924#endif
16925}
16926
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016927#ifdef FEAT_FLOAT
16928/*
16929 * "reltimefloat()" function
16930 */
16931 static void
16932f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16933{
16934# ifdef FEAT_RELTIME
16935 proftime_T tm;
16936# endif
16937
16938 rettv->v_type = VAR_FLOAT;
16939 rettv->vval.v_float = 0;
16940# ifdef FEAT_RELTIME
16941 if (list2proftime(&argvars[0], &tm) == OK)
16942 rettv->vval.v_float = profile_float(&tm);
16943# endif
16944}
16945#endif
16946
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016947/*
16948 * "reltimestr()" function
16949 */
16950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016951f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016952{
16953#ifdef FEAT_RELTIME
16954 proftime_T tm;
16955#endif
16956
16957 rettv->v_type = VAR_STRING;
16958 rettv->vval.v_string = NULL;
16959#ifdef FEAT_RELTIME
16960 if (list2proftime(&argvars[0], &tm) == OK)
16961 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16962#endif
16963}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016964
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016966static void make_connection(void);
16967static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968
16969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016970make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971{
16972 if (X_DISPLAY == NULL
16973# ifdef FEAT_GUI
16974 && !gui.in_use
16975# endif
16976 )
16977 {
16978 x_force_connect = TRUE;
16979 setup_term_clip();
16980 x_force_connect = FALSE;
16981 }
16982}
16983
16984 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016985check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986{
16987 make_connection();
16988 if (X_DISPLAY == NULL)
16989 {
16990 EMSG(_("E240: No connection to Vim server"));
16991 return FAIL;
16992 }
16993 return OK;
16994}
16995#endif
16996
16997#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016999remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000{
17001 char_u *server_name;
17002 char_u *keys;
17003 char_u *r = NULL;
17004 char_u buf[NUMBUFLEN];
17005# ifdef WIN32
17006 HWND w;
17007# else
17008 Window w;
17009# endif
17010
17011 if (check_restricted() || check_secure())
17012 return;
17013
17014# ifdef FEAT_X11
17015 if (check_connection() == FAIL)
17016 return;
17017# endif
17018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017019 server_name = get_tv_string_chk(&argvars[0]);
17020 if (server_name == NULL)
17021 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017022 keys = get_tv_string_buf(&argvars[1], buf);
17023# ifdef WIN32
17024 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17025# else
17026 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17027 < 0)
17028# endif
17029 {
17030 if (r != NULL)
17031 EMSG(r); /* sending worked but evaluation failed */
17032 else
17033 EMSG2(_("E241: Unable to send to %s"), server_name);
17034 return;
17035 }
17036
17037 rettv->vval.v_string = r;
17038
17039 if (argvars[2].v_type != VAR_UNKNOWN)
17040 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017041 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017042 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017043 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017044
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017045 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017046 v.di_tv.v_type = VAR_STRING;
17047 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017048 idvar = get_tv_string_chk(&argvars[2]);
17049 if (idvar != NULL)
17050 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017051 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017052 }
17053}
17054#endif
17055
17056/*
17057 * "remote_expr()" function
17058 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017060f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061{
17062 rettv->v_type = VAR_STRING;
17063 rettv->vval.v_string = NULL;
17064#ifdef FEAT_CLIENTSERVER
17065 remote_common(argvars, rettv, TRUE);
17066#endif
17067}
17068
17069/*
17070 * "remote_foreground()" function
17071 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017073f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075#ifdef FEAT_CLIENTSERVER
17076# ifdef WIN32
17077 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 {
17079 char_u *server_name = get_tv_string_chk(&argvars[0]);
17080
17081 if (server_name != NULL)
17082 serverForeground(server_name);
17083 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084# else
17085 /* Send a foreground() expression to the server. */
17086 argvars[1].v_type = VAR_STRING;
17087 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17088 argvars[2].v_type = VAR_UNKNOWN;
17089 remote_common(argvars, rettv, TRUE);
17090 vim_free(argvars[1].vval.v_string);
17091# endif
17092#endif
17093}
17094
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017096f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097{
17098#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017099 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017100 char_u *s = NULL;
17101# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017102 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017103# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017104 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105
17106 if (check_restricted() || check_secure())
17107 {
17108 rettv->vval.v_number = -1;
17109 return;
17110 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017111 serverid = get_tv_string_chk(&argvars[0]);
17112 if (serverid == NULL)
17113 {
17114 rettv->vval.v_number = -1;
17115 return; /* type error; errmsg already given */
17116 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017117# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017118 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017119 if (n == 0)
17120 rettv->vval.v_number = -1;
17121 else
17122 {
17123 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17124 rettv->vval.v_number = (s != NULL);
17125 }
17126# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017127 if (check_connection() == FAIL)
17128 return;
17129
17130 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017131 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017132# endif
17133
17134 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 char_u *retvar;
17137
Bram Moolenaar33570922005-01-25 22:26:29 +000017138 v.di_tv.v_type = VAR_STRING;
17139 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017140 retvar = get_tv_string_chk(&argvars[1]);
17141 if (retvar != NULL)
17142 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017143 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 }
17145#else
17146 rettv->vval.v_number = -1;
17147#endif
17148}
17149
Bram Moolenaar0d660222005-01-07 21:51:51 +000017150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017151f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152{
17153 char_u *r = NULL;
17154
17155#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017156 char_u *serverid = get_tv_string_chk(&argvars[0]);
17157
17158 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159 {
17160# ifdef WIN32
17161 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017162 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017163
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017164 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017165 if (n != 0)
17166 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17167 if (r == NULL)
17168# else
17169 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017170 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017171# endif
17172 EMSG(_("E277: Unable to read a server reply"));
17173 }
17174#endif
17175 rettv->v_type = VAR_STRING;
17176 rettv->vval.v_string = r;
17177}
17178
17179/*
17180 * "remote_send()" function
17181 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017183f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017184{
17185 rettv->v_type = VAR_STRING;
17186 rettv->vval.v_string = NULL;
17187#ifdef FEAT_CLIENTSERVER
17188 remote_common(argvars, rettv, FALSE);
17189#endif
17190}
17191
17192/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017193 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017194 */
17195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017196f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017197{
Bram Moolenaar33570922005-01-25 22:26:29 +000017198 list_T *l;
17199 listitem_T *item, *item2;
17200 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017201 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017202 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017203 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017204 dict_T *d;
17205 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017206 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017207
Bram Moolenaar8c711452005-01-14 21:53:12 +000017208 if (argvars[0].v_type == VAR_DICT)
17209 {
17210 if (argvars[2].v_type != VAR_UNKNOWN)
17211 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017212 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017213 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017215 key = get_tv_string_chk(&argvars[1]);
17216 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017218 di = dict_find(d, key, -1);
17219 if (di == NULL)
17220 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017221 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17222 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017223 {
17224 *rettv = di->di_tv;
17225 init_tv(&di->di_tv);
17226 dictitem_remove(d, di);
17227 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017228 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017229 }
17230 }
17231 else if (argvars[0].v_type != VAR_LIST)
17232 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017233 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017234 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017236 int error = FALSE;
17237
17238 idx = get_tv_number_chk(&argvars[1], &error);
17239 if (error)
17240 ; /* type error: do nothing, errmsg already given */
17241 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017242 EMSGN(_(e_listidx), idx);
17243 else
17244 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017245 if (argvars[2].v_type == VAR_UNKNOWN)
17246 {
17247 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017248 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017249 *rettv = item->li_tv;
17250 vim_free(item);
17251 }
17252 else
17253 {
17254 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017255 end = get_tv_number_chk(&argvars[2], &error);
17256 if (error)
17257 ; /* type error: do nothing */
17258 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017259 EMSGN(_(e_listidx), end);
17260 else
17261 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017262 int cnt = 0;
17263
17264 for (li = item; li != NULL; li = li->li_next)
17265 {
17266 ++cnt;
17267 if (li == item2)
17268 break;
17269 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017270 if (li == NULL) /* didn't find "item2" after "item" */
17271 EMSG(_(e_invrange));
17272 else
17273 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017274 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017275 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017276 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017277 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017278 l->lv_first = item;
17279 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017280 item->li_prev = NULL;
17281 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017282 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017283 }
17284 }
17285 }
17286 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017287 }
17288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289}
17290
17291/*
17292 * "rename({from}, {to})" function
17293 */
17294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017295f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296{
17297 char_u buf[NUMBUFLEN];
17298
17299 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017300 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017302 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17303 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304}
17305
17306/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017307 * "repeat()" function
17308 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017310f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017311{
17312 char_u *p;
17313 int n;
17314 int slen;
17315 int len;
17316 char_u *r;
17317 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017318
17319 n = get_tv_number(&argvars[1]);
17320 if (argvars[0].v_type == VAR_LIST)
17321 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017322 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017323 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017324 if (list_extend(rettv->vval.v_list,
17325 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017326 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017327 }
17328 else
17329 {
17330 p = get_tv_string(&argvars[0]);
17331 rettv->v_type = VAR_STRING;
17332 rettv->vval.v_string = NULL;
17333
17334 slen = (int)STRLEN(p);
17335 len = slen * n;
17336 if (len <= 0)
17337 return;
17338
17339 r = alloc(len + 1);
17340 if (r != NULL)
17341 {
17342 for (i = 0; i < n; i++)
17343 mch_memmove(r + i * slen, p, (size_t)slen);
17344 r[len] = NUL;
17345 }
17346
17347 rettv->vval.v_string = r;
17348 }
17349}
17350
17351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 * "resolve()" function
17353 */
17354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017355f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356{
17357 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017358#ifdef HAVE_READLINK
17359 char_u *buf = NULL;
17360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017362 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363#ifdef FEAT_SHORTCUT
17364 {
17365 char_u *v = NULL;
17366
17367 v = mch_resolve_shortcut(p);
17368 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017369 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017371 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 }
17373#else
17374# ifdef HAVE_READLINK
17375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 char_u *cpy;
17377 int len;
17378 char_u *remain = NULL;
17379 char_u *q;
17380 int is_relative_to_current = FALSE;
17381 int has_trailing_pathsep = FALSE;
17382 int limit = 100;
17383
17384 p = vim_strsave(p);
17385
17386 if (p[0] == '.' && (vim_ispathsep(p[1])
17387 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17388 is_relative_to_current = TRUE;
17389
17390 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017391 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017392 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017394 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017396
17397 q = getnextcomp(p);
17398 if (*q != NUL)
17399 {
17400 /* Separate the first path component in "p", and keep the
17401 * remainder (beginning with the path separator). */
17402 remain = vim_strsave(q - 1);
17403 q[-1] = NUL;
17404 }
17405
Bram Moolenaard9462e32011-04-11 21:35:11 +020017406 buf = alloc(MAXPATHL + 1);
17407 if (buf == NULL)
17408 goto fail;
17409
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 for (;;)
17411 {
17412 for (;;)
17413 {
17414 len = readlink((char *)p, (char *)buf, MAXPATHL);
17415 if (len <= 0)
17416 break;
17417 buf[len] = NUL;
17418
17419 if (limit-- == 0)
17420 {
17421 vim_free(p);
17422 vim_free(remain);
17423 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 goto fail;
17426 }
17427
17428 /* Ensure that the result will have a trailing path separator
17429 * if the argument has one. */
17430 if (remain == NULL && has_trailing_pathsep)
17431 add_pathsep(buf);
17432
17433 /* Separate the first path component in the link value and
17434 * concatenate the remainders. */
17435 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17436 if (*q != NUL)
17437 {
17438 if (remain == NULL)
17439 remain = vim_strsave(q - 1);
17440 else
17441 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017442 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 if (cpy != NULL)
17444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017445 vim_free(remain);
17446 remain = cpy;
17447 }
17448 }
17449 q[-1] = NUL;
17450 }
17451
17452 q = gettail(p);
17453 if (q > p && *q == NUL)
17454 {
17455 /* Ignore trailing path separator. */
17456 q[-1] = NUL;
17457 q = gettail(p);
17458 }
17459 if (q > p && !mch_isFullName(buf))
17460 {
17461 /* symlink is relative to directory of argument */
17462 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17463 if (cpy != NULL)
17464 {
17465 STRCPY(cpy, p);
17466 STRCPY(gettail(cpy), buf);
17467 vim_free(p);
17468 p = cpy;
17469 }
17470 }
17471 else
17472 {
17473 vim_free(p);
17474 p = vim_strsave(buf);
17475 }
17476 }
17477
17478 if (remain == NULL)
17479 break;
17480
17481 /* Append the first path component of "remain" to "p". */
17482 q = getnextcomp(remain + 1);
17483 len = q - remain - (*q != NUL);
17484 cpy = vim_strnsave(p, STRLEN(p) + len);
17485 if (cpy != NULL)
17486 {
17487 STRNCAT(cpy, remain, len);
17488 vim_free(p);
17489 p = cpy;
17490 }
17491 /* Shorten "remain". */
17492 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017493 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 else
17495 {
17496 vim_free(remain);
17497 remain = NULL;
17498 }
17499 }
17500
17501 /* If the result is a relative path name, make it explicitly relative to
17502 * the current directory if and only if the argument had this form. */
17503 if (!vim_ispathsep(*p))
17504 {
17505 if (is_relative_to_current
17506 && *p != NUL
17507 && !(p[0] == '.'
17508 && (p[1] == NUL
17509 || vim_ispathsep(p[1])
17510 || (p[1] == '.'
17511 && (p[2] == NUL
17512 || vim_ispathsep(p[2]))))))
17513 {
17514 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017515 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 if (cpy != NULL)
17517 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518 vim_free(p);
17519 p = cpy;
17520 }
17521 }
17522 else if (!is_relative_to_current)
17523 {
17524 /* Strip leading "./". */
17525 q = p;
17526 while (q[0] == '.' && vim_ispathsep(q[1]))
17527 q += 2;
17528 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017529 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 }
17531 }
17532
17533 /* Ensure that the result will have no trailing path separator
17534 * if the argument had none. But keep "/" or "//". */
17535 if (!has_trailing_pathsep)
17536 {
17537 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017538 if (after_pathsep(p, q))
17539 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 }
17541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017542 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 }
17544# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017545 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546# endif
17547#endif
17548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017549 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550
17551#ifdef HAVE_READLINK
17552fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017553 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017555 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556}
17557
17558/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017559 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 */
17561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017562f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563{
Bram Moolenaar33570922005-01-25 22:26:29 +000017564 list_T *l;
17565 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567 if (argvars[0].v_type != VAR_LIST)
17568 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017569 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017570 && !tv_check_lock(l->lv_lock,
17571 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017572 {
17573 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017574 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017575 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017576 while (li != NULL)
17577 {
17578 ni = li->li_prev;
17579 list_append(l, li);
17580 li = ni;
17581 }
17582 rettv->vval.v_list = l;
17583 rettv->v_type = VAR_LIST;
17584 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017585 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587}
17588
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017589#define SP_NOMOVE 0x01 /* don't move cursor */
17590#define SP_REPEAT 0x02 /* repeat to find outer pair */
17591#define SP_RETCOUNT 0x04 /* return matchcount */
17592#define SP_SETPCMARK 0x08 /* set previous context mark */
17593#define SP_START 0x10 /* accept match at start position */
17594#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17595#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017596#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017597
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017598static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017599
17600/*
17601 * Get flags for a search function.
17602 * Possibly sets "p_ws".
17603 * Returns BACKWARD, FORWARD or zero (for an error).
17604 */
17605 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017606get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017607{
17608 int dir = FORWARD;
17609 char_u *flags;
17610 char_u nbuf[NUMBUFLEN];
17611 int mask;
17612
17613 if (varp->v_type != VAR_UNKNOWN)
17614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017615 flags = get_tv_string_buf_chk(varp, nbuf);
17616 if (flags == NULL)
17617 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017618 while (*flags != NUL)
17619 {
17620 switch (*flags)
17621 {
17622 case 'b': dir = BACKWARD; break;
17623 case 'w': p_ws = TRUE; break;
17624 case 'W': p_ws = FALSE; break;
17625 default: mask = 0;
17626 if (flagsp != NULL)
17627 switch (*flags)
17628 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017629 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017630 case 'e': mask = SP_END; break;
17631 case 'm': mask = SP_RETCOUNT; break;
17632 case 'n': mask = SP_NOMOVE; break;
17633 case 'p': mask = SP_SUBPAT; break;
17634 case 'r': mask = SP_REPEAT; break;
17635 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017636 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017637 }
17638 if (mask == 0)
17639 {
17640 EMSG2(_(e_invarg2), flags);
17641 dir = 0;
17642 }
17643 else
17644 *flagsp |= mask;
17645 }
17646 if (dir == 0)
17647 break;
17648 ++flags;
17649 }
17650 }
17651 return dir;
17652}
17653
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017655 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017656 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017658search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017660 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 char_u *pat;
17662 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017663 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 int save_p_ws = p_ws;
17665 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017666 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017667 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017668 proftime_T tm;
17669#ifdef FEAT_RELTIME
17670 long time_limit = 0;
17671#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017672 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017673 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017675 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017676 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017677 if (dir == 0)
17678 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017679 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017680 if (flags & SP_START)
17681 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017682 if (flags & SP_END)
17683 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017684 if (flags & SP_COLUMN)
17685 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017686
Bram Moolenaar76929292008-01-06 19:07:36 +000017687 /* Optional arguments: line number to stop searching and timeout. */
17688 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017689 {
17690 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17691 if (lnum_stop < 0)
17692 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017693#ifdef FEAT_RELTIME
17694 if (argvars[3].v_type != VAR_UNKNOWN)
17695 {
17696 time_limit = get_tv_number_chk(&argvars[3], NULL);
17697 if (time_limit < 0)
17698 goto theend;
17699 }
17700#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017701 }
17702
Bram Moolenaar76929292008-01-06 19:07:36 +000017703#ifdef FEAT_RELTIME
17704 /* Set the time limit, if there is one. */
17705 profile_setlimit(time_limit, &tm);
17706#endif
17707
Bram Moolenaar231334e2005-07-25 20:46:57 +000017708 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017709 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017710 * Check to make sure only those flags are set.
17711 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17712 * flags cannot be set. Check for that condition also.
17713 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017714 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017715 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017717 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017718 goto theend;
17719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017721 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017722 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017723 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017724 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017726 if (flags & SP_SUBPAT)
17727 retval = subpatnum;
17728 else
17729 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017730 if (flags & SP_SETPCMARK)
17731 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017733 if (match_pos != NULL)
17734 {
17735 /* Store the match cursor position */
17736 match_pos->lnum = pos.lnum;
17737 match_pos->col = pos.col + 1;
17738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 /* "/$" will put the cursor after the end of the line, may need to
17740 * correct that here */
17741 check_cursor();
17742 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017743
17744 /* If 'n' flag is used: restore cursor position. */
17745 if (flags & SP_NOMOVE)
17746 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017747 else
17748 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017749theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017751
17752 return retval;
17753}
17754
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017755#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017756
17757/*
17758 * round() is not in C90, use ceil() or floor() instead.
17759 */
17760 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017761vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017762{
17763 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17764}
17765
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017766/*
17767 * "round({float})" function
17768 */
17769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017770f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017771{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017772 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017773
17774 rettv->v_type = VAR_FLOAT;
17775 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017776 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017777 else
17778 rettv->vval.v_float = 0.0;
17779}
17780#endif
17781
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017782/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017783 * "screenattr()" function
17784 */
17785 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017786f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017787{
17788 int row;
17789 int col;
17790 int c;
17791
17792 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17793 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17794 if (row < 0 || row >= screen_Rows
17795 || col < 0 || col >= screen_Columns)
17796 c = -1;
17797 else
17798 c = ScreenAttrs[LineOffset[row] + col];
17799 rettv->vval.v_number = c;
17800}
17801
17802/*
17803 * "screenchar()" function
17804 */
17805 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017806f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017807{
17808 int row;
17809 int col;
17810 int off;
17811 int c;
17812
17813 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17814 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17815 if (row < 0 || row >= screen_Rows
17816 || col < 0 || col >= screen_Columns)
17817 c = -1;
17818 else
17819 {
17820 off = LineOffset[row] + col;
17821#ifdef FEAT_MBYTE
17822 if (enc_utf8 && ScreenLinesUC[off] != 0)
17823 c = ScreenLinesUC[off];
17824 else
17825#endif
17826 c = ScreenLines[off];
17827 }
17828 rettv->vval.v_number = c;
17829}
17830
17831/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017832 * "screencol()" function
17833 *
17834 * First column is 1 to be consistent with virtcol().
17835 */
17836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017837f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017838{
17839 rettv->vval.v_number = screen_screencol() + 1;
17840}
17841
17842/*
17843 * "screenrow()" function
17844 */
17845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017846f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017847{
17848 rettv->vval.v_number = screen_screenrow() + 1;
17849}
17850
17851/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017852 * "search()" function
17853 */
17854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017855f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017856{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017857 int flags = 0;
17858
17859 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860}
17861
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017863 * "searchdecl()" function
17864 */
17865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017866f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017867{
17868 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017869 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017870 int error = FALSE;
17871 char_u *name;
17872
17873 rettv->vval.v_number = 1; /* default: FAIL */
17874
17875 name = get_tv_string_chk(&argvars[0]);
17876 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017877 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017878 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017879 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17880 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17881 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017882 if (!error && name != NULL)
17883 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017884 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017885}
17886
17887/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017888 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017890 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017891searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892{
17893 char_u *spat, *mpat, *epat;
17894 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 int dir;
17897 int flags = 0;
17898 char_u nbuf1[NUMBUFLEN];
17899 char_u nbuf2[NUMBUFLEN];
17900 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017901 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017902 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017903 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017906 spat = get_tv_string_chk(&argvars[0]);
17907 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17908 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17909 if (spat == NULL || mpat == NULL || epat == NULL)
17910 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 /* Handle the optional fourth argument: flags */
17913 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017914 if (dir == 0)
17915 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017916
17917 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017918 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17919 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017920 if ((flags & (SP_END | SP_SUBPAT)) != 0
17921 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017922 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017923 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017924 goto theend;
17925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017926
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017927 /* Using 'r' implies 'W', otherwise it doesn't work. */
17928 if (flags & SP_REPEAT)
17929 p_ws = FALSE;
17930
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017931 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017932 if (argvars[3].v_type == VAR_UNKNOWN
17933 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 skip = (char_u *)"";
17935 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017937 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017938 if (argvars[5].v_type != VAR_UNKNOWN)
17939 {
17940 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17941 if (lnum_stop < 0)
17942 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017943#ifdef FEAT_RELTIME
17944 if (argvars[6].v_type != VAR_UNKNOWN)
17945 {
17946 time_limit = get_tv_number_chk(&argvars[6], NULL);
17947 if (time_limit < 0)
17948 goto theend;
17949 }
17950#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017951 }
17952 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017953 if (skip == NULL)
17954 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017956 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017957 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017958
17959theend:
17960 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017961
17962 return retval;
17963}
17964
17965/*
17966 * "searchpair()" function
17967 */
17968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017969f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017970{
17971 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17972}
17973
17974/*
17975 * "searchpairpos()" function
17976 */
17977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017978f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017979{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017980 pos_T match_pos;
17981 int lnum = 0;
17982 int col = 0;
17983
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017984 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017985 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017986
17987 if (searchpair_cmn(argvars, &match_pos) > 0)
17988 {
17989 lnum = match_pos.lnum;
17990 col = match_pos.col;
17991 }
17992
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017993 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17994 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017995}
17996
17997/*
17998 * Search for a start/middle/end thing.
17999 * Used by searchpair(), see its documentation for the details.
18000 * Returns 0 or -1 for no match,
18001 */
18002 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018003do_searchpair(
18004 char_u *spat, /* start pattern */
18005 char_u *mpat, /* middle pattern */
18006 char_u *epat, /* end pattern */
18007 int dir, /* BACKWARD or FORWARD */
18008 char_u *skip, /* skip expression */
18009 int flags, /* SP_SETPCMARK and other SP_ values */
18010 pos_T *match_pos,
18011 linenr_T lnum_stop, /* stop at this line if not zero */
18012 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018013{
18014 char_u *save_cpo;
18015 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18016 long retval = 0;
18017 pos_T pos;
18018 pos_T firstpos;
18019 pos_T foundpos;
18020 pos_T save_cursor;
18021 pos_T save_pos;
18022 int n;
18023 int r;
18024 int nest = 1;
18025 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018026 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018027 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018028
18029 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18030 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018031 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018032
Bram Moolenaar76929292008-01-06 19:07:36 +000018033#ifdef FEAT_RELTIME
18034 /* Set the time limit, if there is one. */
18035 profile_setlimit(time_limit, &tm);
18036#endif
18037
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018038 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18039 * start/middle/end (pat3, for the top pair). */
18040 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18041 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18042 if (pat2 == NULL || pat3 == NULL)
18043 goto theend;
18044 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18045 if (*mpat == NUL)
18046 STRCPY(pat3, pat2);
18047 else
18048 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18049 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018050 if (flags & SP_START)
18051 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018052
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 save_cursor = curwin->w_cursor;
18054 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018055 clearpos(&firstpos);
18056 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 pat = pat3;
18058 for (;;)
18059 {
18060 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018061 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18063 /* didn't find it or found the first match again: FAIL */
18064 break;
18065
18066 if (firstpos.lnum == 0)
18067 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018068 if (equalpos(pos, foundpos))
18069 {
18070 /* Found the same position again. Can happen with a pattern that
18071 * has "\zs" at the end and searching backwards. Advance one
18072 * character and try again. */
18073 if (dir == BACKWARD)
18074 decl(&pos);
18075 else
18076 incl(&pos);
18077 }
18078 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018080 /* clear the start flag to avoid getting stuck here */
18081 options &= ~SEARCH_START;
18082
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 /* If the skip pattern matches, ignore this match. */
18084 if (*skip != NUL)
18085 {
18086 save_pos = curwin->w_cursor;
18087 curwin->w_cursor = pos;
18088 r = eval_to_bool(skip, &err, NULL, FALSE);
18089 curwin->w_cursor = save_pos;
18090 if (err)
18091 {
18092 /* Evaluating {skip} caused an error, break here. */
18093 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018094 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 break;
18096 }
18097 if (r)
18098 continue;
18099 }
18100
18101 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18102 {
18103 /* Found end when searching backwards or start when searching
18104 * forward: nested pair. */
18105 ++nest;
18106 pat = pat2; /* nested, don't search for middle */
18107 }
18108 else
18109 {
18110 /* Found end when searching forward or start when searching
18111 * backward: end of (nested) pair; or found middle in outer pair. */
18112 if (--nest == 1)
18113 pat = pat3; /* outer level, search for middle */
18114 }
18115
18116 if (nest == 0)
18117 {
18118 /* Found the match: return matchcount or line number. */
18119 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018120 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018122 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018123 if (flags & SP_SETPCMARK)
18124 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 curwin->w_cursor = pos;
18126 if (!(flags & SP_REPEAT))
18127 break;
18128 nest = 1; /* search for next unmatched */
18129 }
18130 }
18131
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018132 if (match_pos != NULL)
18133 {
18134 /* Store the match cursor position */
18135 match_pos->lnum = curwin->w_cursor.lnum;
18136 match_pos->col = curwin->w_cursor.col + 1;
18137 }
18138
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018140 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141 curwin->w_cursor = save_cursor;
18142
18143theend:
18144 vim_free(pat2);
18145 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018146 if (p_cpo == empty_option)
18147 p_cpo = save_cpo;
18148 else
18149 /* Darn, evaluating the {skip} expression changed the value. */
18150 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018151
18152 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153}
18154
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018155/*
18156 * "searchpos()" function
18157 */
18158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018159f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018160{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018161 pos_T match_pos;
18162 int lnum = 0;
18163 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018164 int n;
18165 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018166
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018167 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018168 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018169
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018170 n = search_cmn(argvars, &match_pos, &flags);
18171 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018172 {
18173 lnum = match_pos.lnum;
18174 col = match_pos.col;
18175 }
18176
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018177 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18178 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018179 if (flags & SP_SUBPAT)
18180 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018181}
18182
Bram Moolenaar0d660222005-01-07 21:51:51 +000018183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018184f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018186#ifdef FEAT_CLIENTSERVER
18187 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018188 char_u *server = get_tv_string_chk(&argvars[0]);
18189 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190
Bram Moolenaar0d660222005-01-07 21:51:51 +000018191 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018192 if (server == NULL || reply == NULL)
18193 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018194 if (check_restricted() || check_secure())
18195 return;
18196# ifdef FEAT_X11
18197 if (check_connection() == FAIL)
18198 return;
18199# endif
18200
18201 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018203 EMSG(_("E258: Unable to send to client"));
18204 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018206 rettv->vval.v_number = 0;
18207#else
18208 rettv->vval.v_number = -1;
18209#endif
18210}
18211
Bram Moolenaar0d660222005-01-07 21:51:51 +000018212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018213f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018214{
18215 char_u *r = NULL;
18216
18217#ifdef FEAT_CLIENTSERVER
18218# ifdef WIN32
18219 r = serverGetVimNames();
18220# else
18221 make_connection();
18222 if (X_DISPLAY != NULL)
18223 r = serverGetVimNames(X_DISPLAY);
18224# endif
18225#endif
18226 rettv->v_type = VAR_STRING;
18227 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228}
18229
18230/*
18231 * "setbufvar()" function
18232 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018234f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235{
18236 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018239 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 char_u nbuf[NUMBUFLEN];
18241
18242 if (check_restricted() || check_secure())
18243 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018244 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18245 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018246 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 varp = &argvars[2];
18248
18249 if (buf != NULL && varname != NULL && varp != NULL)
18250 {
18251 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253
18254 if (*varname == '&')
18255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018256 long numval;
18257 char_u *strval;
18258 int error = FALSE;
18259
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018261 numval = get_tv_number_chk(varp, &error);
18262 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018263 if (!error && strval != NULL)
18264 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 }
18266 else
18267 {
18268 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18269 if (bufvarname != NULL)
18270 {
18271 STRCPY(bufvarname, "b:");
18272 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018273 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 vim_free(bufvarname);
18275 }
18276 }
18277
18278 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281}
18282
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018284f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018285{
18286 dict_T *d;
18287 dictitem_T *di;
18288 char_u *csearch;
18289
18290 if (argvars[0].v_type != VAR_DICT)
18291 {
18292 EMSG(_(e_dictreq));
18293 return;
18294 }
18295
18296 if ((d = argvars[0].vval.v_dict) != NULL)
18297 {
18298 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18299 if (csearch != NULL)
18300 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018301#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018302 if (enc_utf8)
18303 {
18304 int pcc[MAX_MCO];
18305 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018306
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018307 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18308 }
18309 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018310#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018311 set_last_csearch(PTR2CHAR(csearch),
18312 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018313 }
18314
18315 di = dict_find(d, (char_u *)"forward", -1);
18316 if (di != NULL)
18317 set_csearch_direction(get_tv_number(&di->di_tv)
18318 ? FORWARD : BACKWARD);
18319
18320 di = dict_find(d, (char_u *)"until", -1);
18321 if (di != NULL)
18322 set_csearch_until(!!get_tv_number(&di->di_tv));
18323 }
18324}
18325
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326/*
18327 * "setcmdpos()" function
18328 */
18329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018330f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018331{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018332 int pos = (int)get_tv_number(&argvars[0]) - 1;
18333
18334 if (pos >= 0)
18335 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336}
18337
18338/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018339 * "setfperm({fname}, {mode})" function
18340 */
18341 static void
18342f_setfperm(typval_T *argvars, typval_T *rettv)
18343{
18344 char_u *fname;
18345 char_u modebuf[NUMBUFLEN];
18346 char_u *mode_str;
18347 int i;
18348 int mask;
18349 int mode = 0;
18350
18351 rettv->vval.v_number = 0;
18352 fname = get_tv_string_chk(&argvars[0]);
18353 if (fname == NULL)
18354 return;
18355 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18356 if (mode_str == NULL)
18357 return;
18358 if (STRLEN(mode_str) != 9)
18359 {
18360 EMSG2(_(e_invarg2), mode_str);
18361 return;
18362 }
18363
18364 mask = 1;
18365 for (i = 8; i >= 0; --i)
18366 {
18367 if (mode_str[i] != '-')
18368 mode |= mask;
18369 mask = mask << 1;
18370 }
18371 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18372}
18373
18374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 * "setline()" function
18376 */
18377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018378f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379{
18380 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018381 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018382 list_T *l = NULL;
18383 listitem_T *li = NULL;
18384 long added = 0;
18385 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018387 lnum = get_tv_lnum(&argvars[0]);
18388 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018390 l = argvars[1].vval.v_list;
18391 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018393 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018394 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018395
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018396 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018397 for (;;)
18398 {
18399 if (l != NULL)
18400 {
18401 /* list argument, get next string */
18402 if (li == NULL)
18403 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018404 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018405 li = li->li_next;
18406 }
18407
18408 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018409 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018410 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018411
18412 /* When coming here from Insert mode, sync undo, so that this can be
18413 * undone separately from what was previously inserted. */
18414 if (u_sync_once == 2)
18415 {
18416 u_sync_once = 1; /* notify that u_sync() was called */
18417 u_sync(TRUE);
18418 }
18419
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018420 if (lnum <= curbuf->b_ml.ml_line_count)
18421 {
18422 /* existing line, replace it */
18423 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18424 {
18425 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018426 if (lnum == curwin->w_cursor.lnum)
18427 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018428 rettv->vval.v_number = 0; /* OK */
18429 }
18430 }
18431 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18432 {
18433 /* lnum is one past the last line, append the line */
18434 ++added;
18435 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18436 rettv->vval.v_number = 0; /* OK */
18437 }
18438
18439 if (l == NULL) /* only one string argument */
18440 break;
18441 ++lnum;
18442 }
18443
18444 if (added > 0)
18445 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446}
18447
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018448static 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 +000018449
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018451 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018452 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018454set_qf_ll_list(
18455 win_T *wp UNUSED,
18456 typval_T *list_arg UNUSED,
18457 typval_T *action_arg UNUSED,
18458 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018459{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018460#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018461 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018462 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018463 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018464#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018465
Bram Moolenaar2641f772005-03-25 21:58:17 +000018466 rettv->vval.v_number = -1;
18467
18468#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018469 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018470 EMSG(_(e_listreq));
18471 else
18472 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018473 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018474
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018475 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018476 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018477 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018478 if (act == NULL)
18479 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018480 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018481 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018482 else
18483 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018484 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018485 else if (action_arg->v_type == VAR_UNKNOWN)
18486 action = ' ';
18487 else
18488 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018489
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018490 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018491 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018492 rettv->vval.v_number = 0;
18493 }
18494#endif
18495}
18496
18497/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018498 * "setloclist()" function
18499 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018501f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018502{
18503 win_T *win;
18504
18505 rettv->vval.v_number = -1;
18506
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018507 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018508 if (win != NULL)
18509 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18510}
18511
18512/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018513 * "setmatches()" function
18514 */
18515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018516f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018517{
18518#ifdef FEAT_SEARCH_EXTRA
18519 list_T *l;
18520 listitem_T *li;
18521 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018522 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018523
18524 rettv->vval.v_number = -1;
18525 if (argvars[0].v_type != VAR_LIST)
18526 {
18527 EMSG(_(e_listreq));
18528 return;
18529 }
18530 if ((l = argvars[0].vval.v_list) != NULL)
18531 {
18532
18533 /* To some extent make sure that we are dealing with a list from
18534 * "getmatches()". */
18535 li = l->lv_first;
18536 while (li != NULL)
18537 {
18538 if (li->li_tv.v_type != VAR_DICT
18539 || (d = li->li_tv.vval.v_dict) == NULL)
18540 {
18541 EMSG(_(e_invarg));
18542 return;
18543 }
18544 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018545 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18546 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018547 && dict_find(d, (char_u *)"priority", -1) != NULL
18548 && dict_find(d, (char_u *)"id", -1) != NULL))
18549 {
18550 EMSG(_(e_invarg));
18551 return;
18552 }
18553 li = li->li_next;
18554 }
18555
18556 clear_matches(curwin);
18557 li = l->lv_first;
18558 while (li != NULL)
18559 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018560 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018561 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018562 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018563 char_u *group;
18564 int priority;
18565 int id;
18566 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018567
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018568 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018569 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18570 {
18571 if (s == NULL)
18572 {
18573 s = list_alloc();
18574 if (s == NULL)
18575 return;
18576 }
18577
18578 /* match from matchaddpos() */
18579 for (i = 1; i < 9; i++)
18580 {
18581 sprintf((char *)buf, (char *)"pos%d", i);
18582 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18583 {
18584 if (di->di_tv.v_type != VAR_LIST)
18585 return;
18586
18587 list_append_tv(s, &di->di_tv);
18588 s->lv_refcount++;
18589 }
18590 else
18591 break;
18592 }
18593 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018594
18595 group = get_dict_string(d, (char_u *)"group", FALSE);
18596 priority = (int)get_dict_number(d, (char_u *)"priority");
18597 id = (int)get_dict_number(d, (char_u *)"id");
18598 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18599 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18600 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018601 if (i == 0)
18602 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018603 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018604 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018605 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018606 }
18607 else
18608 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018609 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018610 list_unref(s);
18611 s = NULL;
18612 }
18613
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018614 li = li->li_next;
18615 }
18616 rettv->vval.v_number = 0;
18617 }
18618#endif
18619}
18620
18621/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018622 * "setpos()" function
18623 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018625f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018626{
18627 pos_T pos;
18628 int fnum;
18629 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018630 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018631
Bram Moolenaar08250432008-02-13 11:42:46 +000018632 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018633 name = get_tv_string_chk(argvars);
18634 if (name != NULL)
18635 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018636 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018637 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018638 if (--pos.col < 0)
18639 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018640 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018641 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018642 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018643 if (fnum == curbuf->b_fnum)
18644 {
18645 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018646 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018647 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018648 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018649 curwin->w_set_curswant = FALSE;
18650 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018651 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018652 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018653 }
18654 else
18655 EMSG(_(e_invarg));
18656 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018657 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18658 {
18659 /* set mark */
18660 if (setmark_pos(name[1], &pos, fnum) == OK)
18661 rettv->vval.v_number = 0;
18662 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018663 else
18664 EMSG(_(e_invarg));
18665 }
18666 }
18667}
18668
18669/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018670 * "setqflist()" function
18671 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018673f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018674{
18675 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18676}
18677
18678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 * "setreg()" function
18680 */
18681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018682f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683{
18684 int regname;
18685 char_u *strregname;
18686 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018687 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 int append;
18689 char_u yank_type;
18690 long block_len;
18691
18692 block_len = -1;
18693 yank_type = MAUTO;
18694 append = FALSE;
18695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018696 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018699 if (strregname == NULL)
18700 return; /* type error; errmsg already given */
18701 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 if (regname == 0 || regname == '@')
18703 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018705 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018707 stropt = get_tv_string_chk(&argvars[2]);
18708 if (stropt == NULL)
18709 return; /* type error */
18710 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711 switch (*stropt)
18712 {
18713 case 'a': case 'A': /* append */
18714 append = TRUE;
18715 break;
18716 case 'v': case 'c': /* character-wise selection */
18717 yank_type = MCHAR;
18718 break;
18719 case 'V': case 'l': /* line-wise selection */
18720 yank_type = MLINE;
18721 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 case 'b': case Ctrl_V: /* block-wise selection */
18723 yank_type = MBLOCK;
18724 if (VIM_ISDIGIT(stropt[1]))
18725 {
18726 ++stropt;
18727 block_len = getdigits(&stropt) - 1;
18728 --stropt;
18729 }
18730 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 }
18732 }
18733
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018734 if (argvars[1].v_type == VAR_LIST)
18735 {
18736 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018737 char_u **allocval;
18738 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018739 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018740 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018741 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018742 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018743 int len;
18744
18745 /* If the list is NULL handle like an empty list. */
18746 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018747
Bram Moolenaar7d647822014-04-05 21:28:56 +020018748 /* First half: use for pointers to result lines; second half: use for
18749 * pointers to allocated copies. */
18750 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018751 if (lstval == NULL)
18752 return;
18753 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018754 allocval = lstval + len + 2;
18755 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018756
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018757 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018758 li = li->li_next)
18759 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018760 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018761 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018762 goto free_lstval;
18763 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018764 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018765 /* Need to make a copy, next get_tv_string_buf_chk() will
18766 * overwrite the string. */
18767 strval = vim_strsave(buf);
18768 if (strval == NULL)
18769 goto free_lstval;
18770 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018771 }
18772 *curval++ = strval;
18773 }
18774 *curval++ = NULL;
18775
18776 write_reg_contents_lst(regname, lstval, -1,
18777 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018778free_lstval:
18779 while (curallocval > allocval)
18780 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018781 vim_free(lstval);
18782 }
18783 else
18784 {
18785 strval = get_tv_string_chk(&argvars[1]);
18786 if (strval == NULL)
18787 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018788 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018791 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792}
18793
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018794/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018795 * "settabvar()" function
18796 */
18797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018798f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018799{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018800#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018801 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018802 tabpage_T *tp;
18803#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018804 char_u *varname, *tabvarname;
18805 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018806
18807 rettv->vval.v_number = 0;
18808
18809 if (check_restricted() || check_secure())
18810 return;
18811
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018812#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018813 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018814#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018815 varname = get_tv_string_chk(&argvars[1]);
18816 varp = &argvars[2];
18817
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018818 if (varname != NULL && varp != NULL
18819#ifdef FEAT_WINDOWS
18820 && tp != NULL
18821#endif
18822 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018823 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018824#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018825 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018826 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018827#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018828
18829 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18830 if (tabvarname != NULL)
18831 {
18832 STRCPY(tabvarname, "t:");
18833 STRCPY(tabvarname + 2, varname);
18834 set_var(tabvarname, varp, TRUE);
18835 vim_free(tabvarname);
18836 }
18837
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018838#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018839 /* Restore current tabpage */
18840 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018841 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018842#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018843 }
18844}
18845
18846/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018847 * "settabwinvar()" function
18848 */
18849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018850f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018851{
18852 setwinvar(argvars, rettv, 1);
18853}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854
18855/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018856 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018859f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018861 setwinvar(argvars, rettv, 0);
18862}
18863
18864/*
18865 * "setwinvar()" and "settabwinvar()" functions
18866 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018867
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018869setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018870{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871 win_T *win;
18872#ifdef FEAT_WINDOWS
18873 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018874 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018875 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876#endif
18877 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018878 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018880 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881
18882 if (check_restricted() || check_secure())
18883 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018884
18885#ifdef FEAT_WINDOWS
18886 if (off == 1)
18887 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18888 else
18889 tp = curtab;
18890#endif
18891 win = find_win_by_nr(&argvars[off], tp);
18892 varname = get_tv_string_chk(&argvars[off + 1]);
18893 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894
18895 if (win != NULL && varname != NULL && varp != NULL)
18896 {
18897#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018898 need_switch_win = !(tp == curtab && win == curwin);
18899 if (!need_switch_win
18900 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018903 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018905 long numval;
18906 char_u *strval;
18907 int error = FALSE;
18908
18909 ++varname;
18910 numval = get_tv_number_chk(varp, &error);
18911 strval = get_tv_string_buf_chk(varp, nbuf);
18912 if (!error && strval != NULL)
18913 set_option_value(varname, numval, strval, OPT_LOCAL);
18914 }
18915 else
18916 {
18917 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18918 if (winvarname != NULL)
18919 {
18920 STRCPY(winvarname, "w:");
18921 STRCPY(winvarname + 2, varname);
18922 set_var(winvarname, varp, TRUE);
18923 vim_free(winvarname);
18924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925 }
18926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018928 if (need_switch_win)
18929 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930#endif
18931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932}
18933
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018934#ifdef FEAT_CRYPT
18935/*
18936 * "sha256({string})" function
18937 */
18938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018939f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018940{
18941 char_u *p;
18942
18943 p = get_tv_string(&argvars[0]);
18944 rettv->vval.v_string = vim_strsave(
18945 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18946 rettv->v_type = VAR_STRING;
18947}
18948#endif /* FEAT_CRYPT */
18949
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018951 * "shellescape({string})" function
18952 */
18953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018954f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018955{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018956 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018957 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018958 rettv->v_type = VAR_STRING;
18959}
18960
18961/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018962 * shiftwidth() function
18963 */
18964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018965f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018966{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018967 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018968}
18969
18970/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018971 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 */
18973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018974f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018976 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977
Bram Moolenaar0d660222005-01-07 21:51:51 +000018978 p = get_tv_string(&argvars[0]);
18979 rettv->vval.v_string = vim_strsave(p);
18980 simplify_filename(rettv->vval.v_string); /* simplify in place */
18981 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982}
18983
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018984#ifdef FEAT_FLOAT
18985/*
18986 * "sin()" function
18987 */
18988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018989f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018990{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018991 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018992
18993 rettv->v_type = VAR_FLOAT;
18994 if (get_float_arg(argvars, &f) == OK)
18995 rettv->vval.v_float = sin(f);
18996 else
18997 rettv->vval.v_float = 0.0;
18998}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018999
19000/*
19001 * "sinh()" function
19002 */
19003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019004f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019005{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019006 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019007
19008 rettv->v_type = VAR_FLOAT;
19009 if (get_float_arg(argvars, &f) == OK)
19010 rettv->vval.v_float = sinh(f);
19011 else
19012 rettv->vval.v_float = 0.0;
19013}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019014#endif
19015
Bram Moolenaar0d660222005-01-07 21:51:51 +000019016static int
19017#ifdef __BORLANDC__
19018 _RTLENTRYF
19019#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019020 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021static int
19022#ifdef __BORLANDC__
19023 _RTLENTRYF
19024#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019025 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019026
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019027/* struct used in the array that's given to qsort() */
19028typedef struct
19029{
19030 listitem_T *item;
19031 int idx;
19032} sortItem_T;
19033
Bram Moolenaar0b962472016-02-22 22:51:33 +010019034/* struct storing information about current sort */
19035typedef struct
19036{
19037 int item_compare_ic;
19038 int item_compare_numeric;
19039 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019040#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019041 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019042#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019043 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019044 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019045 dict_T *item_compare_selfdict;
19046 int item_compare_func_err;
19047 int item_compare_keep_zero;
19048} sortinfo_T;
19049static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019050static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051#define ITEM_COMPARE_FAIL 999
19052
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019054 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019056 static int
19057#ifdef __BORLANDC__
19058_RTLENTRYF
19059#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019060item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019062 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019063 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019064 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019065 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019066 int res;
19067 char_u numbuf1[NUMBUFLEN];
19068 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019070 si1 = (sortItem_T *)s1;
19071 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019072 tv1 = &si1->item->li_tv;
19073 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019074
Bram Moolenaar0b962472016-02-22 22:51:33 +010019075 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019076 {
19077 long v1 = get_tv_number(tv1);
19078 long v2 = get_tv_number(tv2);
19079
19080 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19081 }
19082
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019083#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019085 {
19086 float_T v1 = get_tv_float(tv1);
19087 float_T v2 = get_tv_float(tv2);
19088
19089 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19090 }
19091#endif
19092
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019093 /* tv2string() puts quotes around a string and allocates memory. Don't do
19094 * that for string variables. Use a single quote when comparing with a
19095 * non-string to do what the docs promise. */
19096 if (tv1->v_type == VAR_STRING)
19097 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019098 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019099 p1 = (char_u *)"'";
19100 else
19101 p1 = tv1->vval.v_string;
19102 }
19103 else
19104 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19105 if (tv2->v_type == VAR_STRING)
19106 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019107 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019108 p2 = (char_u *)"'";
19109 else
19110 p2 = tv2->vval.v_string;
19111 }
19112 else
19113 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019114 if (p1 == NULL)
19115 p1 = (char_u *)"";
19116 if (p2 == NULL)
19117 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019118 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019119 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019120 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019121 res = STRICMP(p1, p2);
19122 else
19123 res = STRCMP(p1, p2);
19124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019126 {
19127 double n1, n2;
19128 n1 = strtod((char *)p1, (char **)&p1);
19129 n2 = strtod((char *)p2, (char **)&p2);
19130 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19131 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019132
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019133 /* When the result would be zero, compare the item indexes. Makes the
19134 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019135 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019136 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019137
Bram Moolenaar0d660222005-01-07 21:51:51 +000019138 vim_free(tofree1);
19139 vim_free(tofree2);
19140 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141}
19142
19143 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019144#ifdef __BORLANDC__
19145_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019147item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019148{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019149 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019150 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019151 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019152 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019153 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019154 char_u *func_name;
19155 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019157 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019158 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019159 return 0;
19160
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019161 si1 = (sortItem_T *)s1;
19162 si2 = (sortItem_T *)s2;
19163
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019164 if (partial == NULL)
19165 func_name = sortinfo->item_compare_func;
19166 else
19167 func_name = partial->pt_name;
19168
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019169 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019170 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019171 copy_tv(&si1->item->li_tv, &argv[0]);
19172 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019173
19174 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019175 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019176 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019177 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019178 clear_tv(&argv[0]);
19179 clear_tv(&argv[1]);
19180
19181 if (res == FAIL)
19182 res = ITEM_COMPARE_FAIL;
19183 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019184 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19185 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019186 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019187 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019188
19189 /* When the result would be zero, compare the pointers themselves. Makes
19190 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019191 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019192 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019193
Bram Moolenaar0d660222005-01-07 21:51:51 +000019194 return res;
19195}
19196
19197/*
19198 * "sort({list})" function
19199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019201do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202{
Bram Moolenaar33570922005-01-25 22:26:29 +000019203 list_T *l;
19204 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019205 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019206 sortinfo_T *old_sortinfo;
19207 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019208 long len;
19209 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210
Bram Moolenaar0b962472016-02-22 22:51:33 +010019211 /* Pointer to current info struct used in compare function. Save and
19212 * restore the current one for nested calls. */
19213 old_sortinfo = sortinfo;
19214 sortinfo = &info;
19215
Bram Moolenaar0d660222005-01-07 21:51:51 +000019216 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019217 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218 else
19219 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019220 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019221 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019222 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19223 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019224 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019225 rettv->vval.v_list = l;
19226 rettv->v_type = VAR_LIST;
19227 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228
Bram Moolenaar0d660222005-01-07 21:51:51 +000019229 len = list_len(l);
19230 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019231 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232
Bram Moolenaar0b962472016-02-22 22:51:33 +010019233 info.item_compare_ic = FALSE;
19234 info.item_compare_numeric = FALSE;
19235 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019236#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019237 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019238#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019239 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019240 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019241 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019242 if (argvars[1].v_type != VAR_UNKNOWN)
19243 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019244 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019245 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019246 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019247 else if (argvars[1].v_type == VAR_PARTIAL)
19248 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019249 else
19250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019251 int error = FALSE;
19252
19253 i = get_tv_number_chk(&argvars[1], &error);
19254 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019255 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019256 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019257 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019258 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019259 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019260 else if (i != 0)
19261 {
19262 EMSG(_(e_invarg));
19263 goto theend;
19264 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019265 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019266 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019267 if (*info.item_compare_func == NUL)
19268 {
19269 /* empty string means default sort */
19270 info.item_compare_func = NULL;
19271 }
19272 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019273 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019274 info.item_compare_func = NULL;
19275 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019276 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019277 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019278 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019279 info.item_compare_func = NULL;
19280 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019281 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019282#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019283 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019284 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019285 info.item_compare_func = NULL;
19286 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019287 }
19288#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019289 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019290 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019291 info.item_compare_func = NULL;
19292 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019293 }
19294 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019295 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019296
19297 if (argvars[2].v_type != VAR_UNKNOWN)
19298 {
19299 /* optional third argument: {dict} */
19300 if (argvars[2].v_type != VAR_DICT)
19301 {
19302 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019303 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019304 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019305 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019306 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019308
Bram Moolenaar0d660222005-01-07 21:51:51 +000019309 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019310 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019311 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019312 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313
Bram Moolenaar327aa022014-03-25 18:24:23 +010019314 i = 0;
19315 if (sort)
19316 {
19317 /* sort(): ptrs will be the list to sort */
19318 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019319 {
19320 ptrs[i].item = li;
19321 ptrs[i].idx = i;
19322 ++i;
19323 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019324
Bram Moolenaar0b962472016-02-22 22:51:33 +010019325 info.item_compare_func_err = FALSE;
19326 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019327 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019328 if ((info.item_compare_func != NULL
19329 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019330 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019331 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019332 EMSG(_("E702: Sort compare function failed"));
19333 else
19334 {
19335 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019336 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019337 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019338 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019339 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019340
Bram Moolenaar0b962472016-02-22 22:51:33 +010019341 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019342 {
19343 /* Clear the List and append the items in sorted order. */
19344 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19345 l->lv_len = 0;
19346 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019347 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019348 }
19349 }
19350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019352 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019353 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019354
19355 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019356 info.item_compare_func_err = FALSE;
19357 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019358 item_compare_func_ptr = info.item_compare_func != NULL
19359 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019360 ? item_compare2 : item_compare;
19361
19362 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19363 li = li->li_next)
19364 {
19365 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19366 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019367 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019368 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019369 {
19370 EMSG(_("E882: Uniq compare function failed"));
19371 break;
19372 }
19373 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019374
Bram Moolenaar0b962472016-02-22 22:51:33 +010019375 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019376 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019377 while (--i >= 0)
19378 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019379 li = ptrs[i].item->li_next;
19380 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019381 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019382 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019383 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019384 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019385 list_fix_watch(l, li);
19386 listitem_free(li);
19387 l->lv_len--;
19388 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019389 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019390 }
19391
19392 vim_free(ptrs);
19393 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019394theend:
19395 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019396}
19397
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019398/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019399 * "sort({list})" function
19400 */
19401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019402f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019403{
19404 do_sort_uniq(argvars, rettv, TRUE);
19405}
19406
19407/*
19408 * "uniq({list})" function
19409 */
19410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019411f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019412{
19413 do_sort_uniq(argvars, rettv, FALSE);
19414}
19415
19416/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019417 * "soundfold({word})" function
19418 */
19419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019420f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019421{
19422 char_u *s;
19423
19424 rettv->v_type = VAR_STRING;
19425 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019426#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019427 rettv->vval.v_string = eval_soundfold(s);
19428#else
19429 rettv->vval.v_string = vim_strsave(s);
19430#endif
19431}
19432
19433/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019434 * "spellbadword()" function
19435 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019437f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019438{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019439 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019440 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019441 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019442
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019443 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019444 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019445
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019446#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019447 if (argvars[0].v_type == VAR_UNKNOWN)
19448 {
19449 /* Find the start and length of the badly spelled word. */
19450 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19451 if (len != 0)
19452 word = ml_get_cursor();
19453 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019454 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019455 {
19456 char_u *str = get_tv_string_chk(&argvars[0]);
19457 int capcol = -1;
19458
19459 if (str != NULL)
19460 {
19461 /* Check the argument for spelling. */
19462 while (*str != NUL)
19463 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019464 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019465 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019466 {
19467 word = str;
19468 break;
19469 }
19470 str += len;
19471 }
19472 }
19473 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019474#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019475
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019476 list_append_string(rettv->vval.v_list, word, len);
19477 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019478 attr == HLF_SPB ? "bad" :
19479 attr == HLF_SPR ? "rare" :
19480 attr == HLF_SPL ? "local" :
19481 attr == HLF_SPC ? "caps" :
19482 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019483}
19484
19485/*
19486 * "spellsuggest()" function
19487 */
19488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019489f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019490{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019491#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019492 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019493 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019494 int maxcount;
19495 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019496 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019497 listitem_T *li;
19498 int need_capital = FALSE;
19499#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019500
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019501 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019502 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019503
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019504#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019505 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019506 {
19507 str = get_tv_string(&argvars[0]);
19508 if (argvars[1].v_type != VAR_UNKNOWN)
19509 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019510 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019511 if (maxcount <= 0)
19512 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019513 if (argvars[2].v_type != VAR_UNKNOWN)
19514 {
19515 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19516 if (typeerr)
19517 return;
19518 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019519 }
19520 else
19521 maxcount = 25;
19522
Bram Moolenaar4770d092006-01-12 23:22:24 +000019523 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019524
19525 for (i = 0; i < ga.ga_len; ++i)
19526 {
19527 str = ((char_u **)ga.ga_data)[i];
19528
19529 li = listitem_alloc();
19530 if (li == NULL)
19531 vim_free(str);
19532 else
19533 {
19534 li->li_tv.v_type = VAR_STRING;
19535 li->li_tv.v_lock = 0;
19536 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019537 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019538 }
19539 }
19540 ga_clear(&ga);
19541 }
19542#endif
19543}
19544
Bram Moolenaar0d660222005-01-07 21:51:51 +000019545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019546f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019547{
19548 char_u *str;
19549 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019550 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019551 regmatch_T regmatch;
19552 char_u patbuf[NUMBUFLEN];
19553 char_u *save_cpo;
19554 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019555 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019556 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019557 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019558
19559 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19560 save_cpo = p_cpo;
19561 p_cpo = (char_u *)"";
19562
19563 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019564 if (argvars[1].v_type != VAR_UNKNOWN)
19565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019566 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19567 if (pat == NULL)
19568 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019569 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019570 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019571 }
19572 if (pat == NULL || *pat == NUL)
19573 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019574
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019575 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019577 if (typeerr)
19578 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579
Bram Moolenaar0d660222005-01-07 21:51:51 +000019580 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19581 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019583 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019584 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019585 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019586 if (*str == NUL)
19587 match = FALSE; /* empty item at the end */
19588 else
19589 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019590 if (match)
19591 end = regmatch.startp[0];
19592 else
19593 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019594 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19595 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019596 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019597 if (list_append_string(rettv->vval.v_list, str,
19598 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019599 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019600 }
19601 if (!match)
19602 break;
19603 /* Advance to just after the match. */
19604 if (regmatch.endp[0] > str)
19605 col = 0;
19606 else
19607 {
19608 /* Don't get stuck at the same match. */
19609#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019610 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019611#else
19612 col = 1;
19613#endif
19614 }
19615 str = regmatch.endp[0];
19616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617
Bram Moolenaar473de612013-06-08 18:19:48 +020019618 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620
Bram Moolenaar0d660222005-01-07 21:51:51 +000019621 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622}
19623
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019624#ifdef FEAT_FLOAT
19625/*
19626 * "sqrt()" function
19627 */
19628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019629f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019630{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019631 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019632
19633 rettv->v_type = VAR_FLOAT;
19634 if (get_float_arg(argvars, &f) == OK)
19635 rettv->vval.v_float = sqrt(f);
19636 else
19637 rettv->vval.v_float = 0.0;
19638}
19639
19640/*
19641 * "str2float()" function
19642 */
19643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019644f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019645{
19646 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19647
19648 if (*p == '+')
19649 p = skipwhite(p + 1);
19650 (void)string2float(p, &rettv->vval.v_float);
19651 rettv->v_type = VAR_FLOAT;
19652}
19653#endif
19654
Bram Moolenaar2c932302006-03-18 21:42:09 +000019655/*
19656 * "str2nr()" function
19657 */
19658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019659f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019660{
19661 int base = 10;
19662 char_u *p;
19663 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019664 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019665
19666 if (argvars[1].v_type != VAR_UNKNOWN)
19667 {
19668 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019669 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019670 {
19671 EMSG(_(e_invarg));
19672 return;
19673 }
19674 }
19675
19676 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019677 if (*p == '+')
19678 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019679 switch (base)
19680 {
19681 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19682 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19683 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19684 default: what = 0;
19685 }
19686 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019687 rettv->vval.v_number = n;
19688}
19689
Bram Moolenaar071d4272004-06-13 20:20:40 +000019690#ifdef HAVE_STRFTIME
19691/*
19692 * "strftime({format}[, {time}])" function
19693 */
19694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019695f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696{
19697 char_u result_buf[256];
19698 struct tm *curtime;
19699 time_t seconds;
19700 char_u *p;
19701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019702 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019704 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019705 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 seconds = time(NULL);
19707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019708 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 curtime = localtime(&seconds);
19710 /* MSVC returns NULL for an invalid value of seconds. */
19711 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019712 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 else
19714 {
19715# ifdef FEAT_MBYTE
19716 vimconv_T conv;
19717 char_u *enc;
19718
19719 conv.vc_type = CONV_NONE;
19720 enc = enc_locale();
19721 convert_setup(&conv, p_enc, enc);
19722 if (conv.vc_type != CONV_NONE)
19723 p = string_convert(&conv, p, NULL);
19724# endif
19725 if (p != NULL)
19726 (void)strftime((char *)result_buf, sizeof(result_buf),
19727 (char *)p, curtime);
19728 else
19729 result_buf[0] = NUL;
19730
19731# ifdef FEAT_MBYTE
19732 if (conv.vc_type != CONV_NONE)
19733 vim_free(p);
19734 convert_setup(&conv, enc, p_enc);
19735 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 else
19738# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019739 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740
19741# ifdef FEAT_MBYTE
19742 /* Release conversion descriptors */
19743 convert_setup(&conv, NULL, NULL);
19744 vim_free(enc);
19745# endif
19746 }
19747}
19748#endif
19749
19750/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019751 * "strgetchar()" function
19752 */
19753 static void
19754f_strgetchar(typval_T *argvars, typval_T *rettv)
19755{
19756 char_u *str;
19757 int len;
19758 int error = FALSE;
19759 int charidx;
19760
19761 rettv->vval.v_number = -1;
19762 str = get_tv_string_chk(&argvars[0]);
19763 if (str == NULL)
19764 return;
19765 len = (int)STRLEN(str);
19766 charidx = get_tv_number_chk(&argvars[1], &error);
19767 if (error)
19768 return;
19769#ifdef FEAT_MBYTE
19770 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019771 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019772
19773 while (charidx >= 0 && byteidx < len)
19774 {
19775 if (charidx == 0)
19776 {
19777 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19778 break;
19779 }
19780 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019781 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019782 }
19783 }
19784#else
19785 if (charidx < len)
19786 rettv->vval.v_number = str[charidx];
19787#endif
19788}
19789
19790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791 * "stridx()" function
19792 */
19793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019794f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795{
19796 char_u buf[NUMBUFLEN];
19797 char_u *needle;
19798 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019799 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019801 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019803 needle = get_tv_string_chk(&argvars[1]);
19804 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019805 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019806 if (needle == NULL || haystack == NULL)
19807 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019808
Bram Moolenaar33570922005-01-25 22:26:29 +000019809 if (argvars[2].v_type != VAR_UNKNOWN)
19810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019811 int error = FALSE;
19812
19813 start_idx = get_tv_number_chk(&argvars[2], &error);
19814 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019815 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019816 if (start_idx >= 0)
19817 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019818 }
19819
19820 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19821 if (pos != NULL)
19822 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823}
19824
19825/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019826 * "string()" function
19827 */
19828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019829f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019830{
19831 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019832 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019835 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19836 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019837 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019838 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019839 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840}
19841
19842/*
19843 * "strlen()" function
19844 */
19845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019846f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019848 rettv->vval.v_number = (varnumber_T)(STRLEN(
19849 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850}
19851
19852/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019853 * "strchars()" function
19854 */
19855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019856f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019857{
19858 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019859 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019860#ifdef FEAT_MBYTE
19861 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019862 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019863#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019864
19865 if (argvars[1].v_type != VAR_UNKNOWN)
19866 skipcc = get_tv_number_chk(&argvars[1], NULL);
19867 if (skipcc < 0 || skipcc > 1)
19868 EMSG(_(e_invarg));
19869 else
19870 {
19871#ifdef FEAT_MBYTE
19872 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19873 while (*s != NUL)
19874 {
19875 func_mb_ptr2char_adv(&s);
19876 ++len;
19877 }
19878 rettv->vval.v_number = len;
19879#else
19880 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19881#endif
19882 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019883}
19884
19885/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019886 * "strdisplaywidth()" function
19887 */
19888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019889f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019890{
19891 char_u *s = get_tv_string(&argvars[0]);
19892 int col = 0;
19893
19894 if (argvars[1].v_type != VAR_UNKNOWN)
19895 col = get_tv_number(&argvars[1]);
19896
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019897 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019898}
19899
19900/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019901 * "strwidth()" function
19902 */
19903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019904f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019905{
19906 char_u *s = get_tv_string(&argvars[0]);
19907
19908 rettv->vval.v_number = (varnumber_T)(
19909#ifdef FEAT_MBYTE
19910 mb_string2cells(s, -1)
19911#else
19912 STRLEN(s)
19913#endif
19914 );
19915}
19916
19917/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019918 * "strcharpart()" function
19919 */
19920 static void
19921f_strcharpart(typval_T *argvars, typval_T *rettv)
19922{
19923#ifdef FEAT_MBYTE
19924 char_u *p;
19925 int nchar;
19926 int nbyte = 0;
19927 int charlen;
19928 int len = 0;
19929 int slen;
19930 int error = FALSE;
19931
19932 p = get_tv_string(&argvars[0]);
19933 slen = (int)STRLEN(p);
19934
19935 nchar = get_tv_number_chk(&argvars[1], &error);
19936 if (!error)
19937 {
19938 if (nchar > 0)
19939 while (nchar > 0 && nbyte < slen)
19940 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019941 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019942 --nchar;
19943 }
19944 else
19945 nbyte = nchar;
19946 if (argvars[2].v_type != VAR_UNKNOWN)
19947 {
19948 charlen = get_tv_number(&argvars[2]);
19949 while (charlen > 0 && nbyte + len < slen)
19950 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019951 int off = nbyte + len;
19952
19953 if (off < 0)
19954 len += 1;
19955 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019956 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019957 --charlen;
19958 }
19959 }
19960 else
19961 len = slen - nbyte; /* default: all bytes that are available. */
19962 }
19963
19964 /*
19965 * Only return the overlap between the specified part and the actual
19966 * string.
19967 */
19968 if (nbyte < 0)
19969 {
19970 len += nbyte;
19971 nbyte = 0;
19972 }
19973 else if (nbyte > slen)
19974 nbyte = slen;
19975 if (len < 0)
19976 len = 0;
19977 else if (nbyte + len > slen)
19978 len = slen - nbyte;
19979
19980 rettv->v_type = VAR_STRING;
19981 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19982#else
19983 f_strpart(argvars, rettv);
19984#endif
19985}
19986
19987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 * "strpart()" function
19989 */
19990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019991f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992{
19993 char_u *p;
19994 int n;
19995 int len;
19996 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019997 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 slen = (int)STRLEN(p);
20001
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020002 n = get_tv_number_chk(&argvars[1], &error);
20003 if (error)
20004 len = 0;
20005 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020006 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 else
20008 len = slen - n; /* default len: all bytes that are available. */
20009
20010 /*
20011 * Only return the overlap between the specified part and the actual
20012 * string.
20013 */
20014 if (n < 0)
20015 {
20016 len += n;
20017 n = 0;
20018 }
20019 else if (n > slen)
20020 n = slen;
20021 if (len < 0)
20022 len = 0;
20023 else if (n + len > slen)
20024 len = slen - n;
20025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020026 rettv->v_type = VAR_STRING;
20027 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028}
20029
20030/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020031 * "strridx()" function
20032 */
20033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020034f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020035{
20036 char_u buf[NUMBUFLEN];
20037 char_u *needle;
20038 char_u *haystack;
20039 char_u *rest;
20040 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020041 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020043 needle = get_tv_string_chk(&argvars[1]);
20044 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020045
20046 rettv->vval.v_number = -1;
20047 if (needle == NULL || haystack == NULL)
20048 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020049
20050 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020051 if (argvars[2].v_type != VAR_UNKNOWN)
20052 {
20053 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020054 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020055 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020056 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020057 }
20058 else
20059 end_idx = haystack_len;
20060
Bram Moolenaar0d660222005-01-07 21:51:51 +000020061 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020062 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020063 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020064 lastmatch = haystack + end_idx;
20065 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020066 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020067 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020068 for (rest = haystack; *rest != '\0'; ++rest)
20069 {
20070 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020071 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020072 break;
20073 lastmatch = rest;
20074 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020075 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020076
20077 if (lastmatch == NULL)
20078 rettv->vval.v_number = -1;
20079 else
20080 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20081}
20082
20083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 * "strtrans()" function
20085 */
20086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020087f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020089 rettv->v_type = VAR_STRING;
20090 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091}
20092
20093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020094 * "submatch()" function
20095 */
20096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020097f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020098{
Bram Moolenaar41571762014-04-02 19:00:58 +020020099 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020100 int no;
20101 int retList = 0;
20102
20103 no = (int)get_tv_number_chk(&argvars[0], &error);
20104 if (error)
20105 return;
20106 error = FALSE;
20107 if (argvars[1].v_type != VAR_UNKNOWN)
20108 retList = get_tv_number_chk(&argvars[1], &error);
20109 if (error)
20110 return;
20111
20112 if (retList == 0)
20113 {
20114 rettv->v_type = VAR_STRING;
20115 rettv->vval.v_string = reg_submatch(no);
20116 }
20117 else
20118 {
20119 rettv->v_type = VAR_LIST;
20120 rettv->vval.v_list = reg_submatch_list(no);
20121 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020122}
20123
20124/*
20125 * "substitute()" function
20126 */
20127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020128f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020129{
20130 char_u patbuf[NUMBUFLEN];
20131 char_u subbuf[NUMBUFLEN];
20132 char_u flagsbuf[NUMBUFLEN];
20133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020134 char_u *str = get_tv_string_chk(&argvars[0]);
20135 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20136 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20137 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20138
Bram Moolenaar0d660222005-01-07 21:51:51 +000020139 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020140 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20141 rettv->vval.v_string = NULL;
20142 else
20143 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020144}
20145
20146/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020147 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020150f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151{
20152 int id = 0;
20153#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020154 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155 long col;
20156 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020157 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020159 lnum = get_tv_lnum(argvars); /* -1 on type error */
20160 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20161 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020163 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020164 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020165 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166#endif
20167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020168 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169}
20170
20171/*
20172 * "synIDattr(id, what [, mode])" function
20173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020175f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176{
20177 char_u *p = NULL;
20178#ifdef FEAT_SYN_HL
20179 int id;
20180 char_u *what;
20181 char_u *mode;
20182 char_u modebuf[NUMBUFLEN];
20183 int modec;
20184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020185 id = get_tv_number(&argvars[0]);
20186 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020187 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020189 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020191 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192 modec = 0; /* replace invalid with current */
20193 }
20194 else
20195 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020196#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020197 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 modec = 'g';
20199 else
20200#endif
20201 if (t_colors > 1)
20202 modec = 'c';
20203 else
20204 modec = 't';
20205 }
20206
20207
20208 switch (TOLOWER_ASC(what[0]))
20209 {
20210 case 'b':
20211 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20212 p = highlight_color(id, what, modec);
20213 else /* bold */
20214 p = highlight_has_attr(id, HL_BOLD, modec);
20215 break;
20216
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020217 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 p = highlight_color(id, what, modec);
20219 break;
20220
20221 case 'i':
20222 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20223 p = highlight_has_attr(id, HL_INVERSE, modec);
20224 else /* italic */
20225 p = highlight_has_attr(id, HL_ITALIC, modec);
20226 break;
20227
20228 case 'n': /* name */
20229 p = get_highlight_name(NULL, id - 1);
20230 break;
20231
20232 case 'r': /* reverse */
20233 p = highlight_has_attr(id, HL_INVERSE, modec);
20234 break;
20235
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020236 case 's':
20237 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20238 p = highlight_color(id, what, modec);
20239 else /* standout */
20240 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 break;
20242
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020243 case 'u':
20244 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20245 /* underline */
20246 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20247 else
20248 /* undercurl */
20249 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 break;
20251 }
20252
20253 if (p != NULL)
20254 p = vim_strsave(p);
20255#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020256 rettv->v_type = VAR_STRING;
20257 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258}
20259
20260/*
20261 * "synIDtrans(id)" function
20262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020264f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265{
20266 int id;
20267
20268#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020269 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270
20271 if (id > 0)
20272 id = syn_get_final_id(id);
20273 else
20274#endif
20275 id = 0;
20276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020277 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278}
20279
20280/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020281 * "synconcealed(lnum, col)" function
20282 */
20283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020284f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020285{
20286#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20287 long lnum;
20288 long col;
20289 int syntax_flags = 0;
20290 int cchar;
20291 int matchid = 0;
20292 char_u str[NUMBUFLEN];
20293#endif
20294
20295 rettv->v_type = VAR_LIST;
20296 rettv->vval.v_list = NULL;
20297
20298#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20299 lnum = get_tv_lnum(argvars); /* -1 on type error */
20300 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20301
20302 vim_memset(str, NUL, sizeof(str));
20303
20304 if (rettv_list_alloc(rettv) != FAIL)
20305 {
20306 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20307 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20308 && curwin->w_p_cole > 0)
20309 {
20310 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20311 syntax_flags = get_syntax_info(&matchid);
20312
20313 /* get the conceal character */
20314 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20315 {
20316 cchar = syn_get_sub_char();
20317 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20318 cchar = lcs_conceal;
20319 if (cchar != NUL)
20320 {
20321# ifdef FEAT_MBYTE
20322 if (has_mbyte)
20323 (*mb_char2bytes)(cchar, str);
20324 else
20325# endif
20326 str[0] = cchar;
20327 }
20328 }
20329 }
20330
20331 list_append_number(rettv->vval.v_list,
20332 (syntax_flags & HL_CONCEAL) != 0);
20333 /* -1 to auto-determine strlen */
20334 list_append_string(rettv->vval.v_list, str, -1);
20335 list_append_number(rettv->vval.v_list, matchid);
20336 }
20337#endif
20338}
20339
20340/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020341 * "synstack(lnum, col)" function
20342 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020344f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020345{
20346#ifdef FEAT_SYN_HL
20347 long lnum;
20348 long col;
20349 int i;
20350 int id;
20351#endif
20352
20353 rettv->v_type = VAR_LIST;
20354 rettv->vval.v_list = NULL;
20355
20356#ifdef FEAT_SYN_HL
20357 lnum = get_tv_lnum(argvars); /* -1 on type error */
20358 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20359
20360 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020361 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020362 && rettv_list_alloc(rettv) != FAIL)
20363 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020364 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020365 for (i = 0; ; ++i)
20366 {
20367 id = syn_get_stack_item(i);
20368 if (id < 0)
20369 break;
20370 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20371 break;
20372 }
20373 }
20374#endif
20375}
20376
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020378get_cmd_output_as_rettv(
20379 typval_T *argvars,
20380 typval_T *rettv,
20381 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020382{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020383 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020385 char_u *infile = NULL;
20386 char_u buf[NUMBUFLEN];
20387 int err = FALSE;
20388 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020389 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020390 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020392 rettv->v_type = VAR_STRING;
20393 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020394 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020395 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020396
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020397 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020398 {
20399 /*
20400 * Write the string to a temp file, to be used for input of the shell
20401 * command.
20402 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020403 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020404 {
20405 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020406 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020407 }
20408
20409 fd = mch_fopen((char *)infile, WRITEBIN);
20410 if (fd == NULL)
20411 {
20412 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020413 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020414 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020415 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020416 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020417 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20418 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020419 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020420 else
20421 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020422 size_t len;
20423
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020424 p = get_tv_string_buf_chk(&argvars[1], buf);
20425 if (p == NULL)
20426 {
20427 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020428 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020429 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020430 len = STRLEN(p);
20431 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020432 err = TRUE;
20433 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020434 if (fclose(fd) != 0)
20435 err = TRUE;
20436 if (err)
20437 {
20438 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020439 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020440 }
20441 }
20442
Bram Moolenaar52a72462014-08-29 15:53:52 +020020443 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20444 * echoes typeahead, that messes up the display. */
20445 if (!msg_silent)
20446 flags += SHELL_COOKED;
20447
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020448 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020450 int len;
20451 listitem_T *li;
20452 char_u *s = NULL;
20453 char_u *start;
20454 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020455 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456
Bram Moolenaar52a72462014-08-29 15:53:52 +020020457 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020458 if (res == NULL)
20459 goto errret;
20460
20461 list = list_alloc();
20462 if (list == NULL)
20463 goto errret;
20464
20465 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020467 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020468 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020469 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020470 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020471
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020472 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020473 if (s == NULL)
20474 goto errret;
20475
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020476 for (p = s; start < end; ++p, ++start)
20477 *p = *start == NUL ? NL : *start;
20478 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020479
20480 li = listitem_alloc();
20481 if (li == NULL)
20482 {
20483 vim_free(s);
20484 goto errret;
20485 }
20486 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020487 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020488 li->li_tv.vval.v_string = s;
20489 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020491
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020492 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020493 rettv->v_type = VAR_LIST;
20494 rettv->vval.v_list = list;
20495 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020497 else
20498 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020499 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020500#ifdef USE_CR
20501 /* translate <CR> into <NL> */
20502 if (res != NULL)
20503 {
20504 char_u *s;
20505
20506 for (s = res; *s; ++s)
20507 {
20508 if (*s == CAR)
20509 *s = NL;
20510 }
20511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512#else
20513# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020514 /* translate <CR><NL> into <NL> */
20515 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020517 char_u *s, *d;
20518
20519 d = res;
20520 for (s = res; *s; ++s)
20521 {
20522 if (s[0] == CAR && s[1] == NL)
20523 ++s;
20524 *d++ = *s;
20525 }
20526 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528# endif
20529#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020530 rettv->vval.v_string = res;
20531 res = NULL;
20532 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020533
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020534errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020535 if (infile != NULL)
20536 {
20537 mch_remove(infile);
20538 vim_free(infile);
20539 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020540 if (res != NULL)
20541 vim_free(res);
20542 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020543 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020544}
20545
20546/*
20547 * "system()" function
20548 */
20549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020550f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020551{
20552 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20553}
20554
20555/*
20556 * "systemlist()" function
20557 */
20558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020559f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020560{
20561 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562}
20563
20564/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020565 * "tabpagebuflist()" function
20566 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020568f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020569{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020570#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020571 tabpage_T *tp;
20572 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020573
20574 if (argvars[0].v_type == VAR_UNKNOWN)
20575 wp = firstwin;
20576 else
20577 {
20578 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20579 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020580 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020581 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020582 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020583 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020584 for (; wp != NULL; wp = wp->w_next)
20585 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020586 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020587 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020588 }
20589#endif
20590}
20591
20592
20593/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020594 * "tabpagenr()" function
20595 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020597f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020598{
20599 int nr = 1;
20600#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020601 char_u *arg;
20602
20603 if (argvars[0].v_type != VAR_UNKNOWN)
20604 {
20605 arg = get_tv_string_chk(&argvars[0]);
20606 nr = 0;
20607 if (arg != NULL)
20608 {
20609 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020610 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020611 else
20612 EMSG2(_(e_invexpr2), arg);
20613 }
20614 }
20615 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020616 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020617#endif
20618 rettv->vval.v_number = nr;
20619}
20620
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020621
20622#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020623static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020624
20625/*
20626 * Common code for tabpagewinnr() and winnr().
20627 */
20628 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020629get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020630{
20631 win_T *twin;
20632 int nr = 1;
20633 win_T *wp;
20634 char_u *arg;
20635
20636 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20637 if (argvar->v_type != VAR_UNKNOWN)
20638 {
20639 arg = get_tv_string_chk(argvar);
20640 if (arg == NULL)
20641 nr = 0; /* type error; errmsg already given */
20642 else if (STRCMP(arg, "$") == 0)
20643 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20644 else if (STRCMP(arg, "#") == 0)
20645 {
20646 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20647 if (twin == NULL)
20648 nr = 0;
20649 }
20650 else
20651 {
20652 EMSG2(_(e_invexpr2), arg);
20653 nr = 0;
20654 }
20655 }
20656
20657 if (nr > 0)
20658 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20659 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020660 {
20661 if (wp == NULL)
20662 {
20663 /* didn't find it in this tabpage */
20664 nr = 0;
20665 break;
20666 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020667 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020668 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020669 return nr;
20670}
20671#endif
20672
20673/*
20674 * "tabpagewinnr()" function
20675 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020677f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020678{
20679 int nr = 1;
20680#ifdef FEAT_WINDOWS
20681 tabpage_T *tp;
20682
20683 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20684 if (tp == NULL)
20685 nr = 0;
20686 else
20687 nr = get_winnr(tp, &argvars[1]);
20688#endif
20689 rettv->vval.v_number = nr;
20690}
20691
20692
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020693/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020694 * "tagfiles()" function
20695 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020697f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020698{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020699 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020700 tagname_T tn;
20701 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020702
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020703 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020704 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020705 fname = alloc(MAXPATHL);
20706 if (fname == NULL)
20707 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020708
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020709 for (first = TRUE; ; first = FALSE)
20710 if (get_tagfname(&tn, first, fname) == FAIL
20711 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020712 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020713 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020714 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020715}
20716
20717/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020718 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020719 */
20720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020721f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020722{
20723 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020724
20725 tag_pattern = get_tv_string(&argvars[0]);
20726
20727 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020728 if (*tag_pattern == NUL)
20729 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020730
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020731 if (rettv_list_alloc(rettv) == OK)
20732 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020733}
20734
20735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 * "tempname()" function
20737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020739f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740{
20741 static int x = 'A';
20742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020743 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020744 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745
20746 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20747 * names. Skip 'I' and 'O', they are used for shell redirection. */
20748 do
20749 {
20750 if (x == 'Z')
20751 x = '0';
20752 else if (x == '9')
20753 x = 'A';
20754 else
20755 {
20756#ifdef EBCDIC
20757 if (x == 'I')
20758 x = 'J';
20759 else if (x == 'R')
20760 x = 'S';
20761 else
20762#endif
20763 ++x;
20764 }
20765 } while (x == 'I' || x == 'O');
20766}
20767
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020768#ifdef FEAT_FLOAT
20769/*
20770 * "tan()" function
20771 */
20772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020773f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020774{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020775 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020776
20777 rettv->v_type = VAR_FLOAT;
20778 if (get_float_arg(argvars, &f) == OK)
20779 rettv->vval.v_float = tan(f);
20780 else
20781 rettv->vval.v_float = 0.0;
20782}
20783
20784/*
20785 * "tanh()" function
20786 */
20787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020788f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020789{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020790 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020791
20792 rettv->v_type = VAR_FLOAT;
20793 if (get_float_arg(argvars, &f) == OK)
20794 rettv->vval.v_float = tanh(f);
20795 else
20796 rettv->vval.v_float = 0.0;
20797}
20798#endif
20799
Bram Moolenaar574860b2016-05-24 17:33:34 +020020800/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020801 * "test_alloc_fail(id, countdown, repeat)" function
20802 */
20803 static void
20804f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20805{
20806 if (argvars[0].v_type != VAR_NUMBER
20807 || argvars[0].vval.v_number <= 0
20808 || argvars[1].v_type != VAR_NUMBER
20809 || argvars[1].vval.v_number < 0
20810 || argvars[2].v_type != VAR_NUMBER)
20811 EMSG(_(e_invarg));
20812 else
20813 {
20814 alloc_fail_id = argvars[0].vval.v_number;
20815 if (alloc_fail_id >= aid_last)
20816 EMSG(_(e_invarg));
20817 alloc_fail_countdown = argvars[1].vval.v_number;
20818 alloc_fail_repeat = argvars[2].vval.v_number;
20819 did_outofmem_msg = FALSE;
20820 }
20821}
20822
20823/*
20824 * "test_disable_char_avail({expr})" function
20825 */
20826 static void
20827f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20828{
20829 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20830}
20831
20832/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020833 * "test_garbagecollect_now()" function
20834 */
20835 static void
20836f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20837{
20838 /* This is dangerous, any Lists and Dicts used internally may be freed
20839 * while still in use. */
20840 garbage_collect(TRUE);
20841}
20842
20843#ifdef FEAT_JOB_CHANNEL
20844 static void
20845f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20846{
20847 rettv->v_type = VAR_CHANNEL;
20848 rettv->vval.v_channel = NULL;
20849}
20850#endif
20851
20852 static void
20853f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20854{
20855 rettv->v_type = VAR_DICT;
20856 rettv->vval.v_dict = NULL;
20857}
20858
20859#ifdef FEAT_JOB_CHANNEL
20860 static void
20861f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20862{
20863 rettv->v_type = VAR_JOB;
20864 rettv->vval.v_job = NULL;
20865}
20866#endif
20867
20868 static void
20869f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20870{
20871 rettv->v_type = VAR_LIST;
20872 rettv->vval.v_list = NULL;
20873}
20874
20875 static void
20876f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20877{
20878 rettv->v_type = VAR_PARTIAL;
20879 rettv->vval.v_partial = NULL;
20880}
20881
20882 static void
20883f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20884{
20885 rettv->v_type = VAR_STRING;
20886 rettv->vval.v_string = NULL;
20887}
20888
Bram Moolenaar975b5272016-03-15 23:10:59 +010020889#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20890/*
20891 * Get a callback from "arg". It can be a Funcref or a function name.
20892 * When "arg" is zero return an empty string.
20893 * Return NULL for an invalid argument.
20894 */
20895 char_u *
20896get_callback(typval_T *arg, partial_T **pp)
20897{
20898 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20899 {
20900 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020901 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020902 return (*pp)->pt_name;
20903 }
20904 *pp = NULL;
20905 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20906 return arg->vval.v_string;
20907 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20908 return (char_u *)"";
20909 EMSG(_("E921: Invalid callback argument"));
20910 return NULL;
20911}
20912#endif
20913
20914#ifdef FEAT_TIMERS
20915/*
20916 * "timer_start(time, callback [, options])" function
20917 */
20918 static void
20919f_timer_start(typval_T *argvars, typval_T *rettv)
20920{
20921 long msec = get_tv_number(&argvars[0]);
20922 timer_T *timer;
20923 int repeat = 0;
20924 char_u *callback;
20925 dict_T *dict;
20926
Bram Moolenaar38499922016-04-22 20:46:52 +020020927 if (check_secure())
20928 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020929 if (argvars[2].v_type != VAR_UNKNOWN)
20930 {
20931 if (argvars[2].v_type != VAR_DICT
20932 || (dict = argvars[2].vval.v_dict) == NULL)
20933 {
20934 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20935 return;
20936 }
20937 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20938 repeat = get_dict_number(dict, (char_u *)"repeat");
20939 }
20940
20941 timer = create_timer(msec, repeat);
20942 callback = get_callback(&argvars[1], &timer->tr_partial);
20943 if (callback == NULL)
20944 {
20945 stop_timer(timer);
20946 rettv->vval.v_number = -1;
20947 }
20948 else
20949 {
20950 timer->tr_callback = vim_strsave(callback);
20951 rettv->vval.v_number = timer->tr_id;
20952 }
20953}
20954
20955/*
20956 * "timer_stop(timer)" function
20957 */
20958 static void
20959f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20960{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020961 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020962
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020963 if (argvars[0].v_type != VAR_NUMBER)
20964 {
20965 EMSG(_(e_number_exp));
20966 return;
20967 }
20968 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020969 if (timer != NULL)
20970 stop_timer(timer);
20971}
20972#endif
20973
Bram Moolenaard52d9742005-08-21 22:20:28 +000020974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 * "tolower(string)" function
20976 */
20977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020978f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979{
20980 char_u *p;
20981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020982 p = vim_strsave(get_tv_string(&argvars[0]));
20983 rettv->v_type = VAR_STRING;
20984 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985
20986 if (p != NULL)
20987 while (*p != NUL)
20988 {
20989#ifdef FEAT_MBYTE
20990 int l;
20991
20992 if (enc_utf8)
20993 {
20994 int c, lc;
20995
20996 c = utf_ptr2char(p);
20997 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020998 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 /* TODO: reallocate string when byte count changes. */
21000 if (utf_char2len(lc) == l)
21001 utf_char2bytes(lc, p);
21002 p += l;
21003 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021004 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 p += l; /* skip multi-byte character */
21006 else
21007#endif
21008 {
21009 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21010 ++p;
21011 }
21012 }
21013}
21014
21015/*
21016 * "toupper(string)" function
21017 */
21018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021019f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021021 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021022 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023}
21024
21025/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021026 * "tr(string, fromstr, tostr)" function
21027 */
21028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021030{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021031 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021032 char_u *fromstr;
21033 char_u *tostr;
21034 char_u *p;
21035#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021036 int inlen;
21037 int fromlen;
21038 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021039 int idx;
21040 char_u *cpstr;
21041 int cplen;
21042 int first = TRUE;
21043#endif
21044 char_u buf[NUMBUFLEN];
21045 char_u buf2[NUMBUFLEN];
21046 garray_T ga;
21047
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021048 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021049 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21050 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021051
21052 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021053 rettv->v_type = VAR_STRING;
21054 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021055 if (fromstr == NULL || tostr == NULL)
21056 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021057 ga_init2(&ga, (int)sizeof(char), 80);
21058
21059#ifdef FEAT_MBYTE
21060 if (!has_mbyte)
21061#endif
21062 /* not multi-byte: fromstr and tostr must be the same length */
21063 if (STRLEN(fromstr) != STRLEN(tostr))
21064 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021065#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021066error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021067#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021068 EMSG2(_(e_invarg2), fromstr);
21069 ga_clear(&ga);
21070 return;
21071 }
21072
21073 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021074 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021075 {
21076#ifdef FEAT_MBYTE
21077 if (has_mbyte)
21078 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021079 inlen = (*mb_ptr2len)(in_str);
21080 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021081 cplen = inlen;
21082 idx = 0;
21083 for (p = fromstr; *p != NUL; p += fromlen)
21084 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021085 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021086 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021087 {
21088 for (p = tostr; *p != NUL; p += tolen)
21089 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021090 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021091 if (idx-- == 0)
21092 {
21093 cplen = tolen;
21094 cpstr = p;
21095 break;
21096 }
21097 }
21098 if (*p == NUL) /* tostr is shorter than fromstr */
21099 goto error;
21100 break;
21101 }
21102 ++idx;
21103 }
21104
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021105 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021106 {
21107 /* Check that fromstr and tostr have the same number of
21108 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021109 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021110 first = FALSE;
21111 for (p = tostr; *p != NUL; p += tolen)
21112 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021113 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021114 --idx;
21115 }
21116 if (idx != 0)
21117 goto error;
21118 }
21119
Bram Moolenaarcde88542015-08-11 19:14:00 +020021120 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021121 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021122 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021123
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021124 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021125 }
21126 else
21127#endif
21128 {
21129 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021130 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021131 if (p != NULL)
21132 ga_append(&ga, tostr[p - fromstr]);
21133 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021134 ga_append(&ga, *in_str);
21135 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021136 }
21137 }
21138
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021139 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021140 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021141 ga_append(&ga, NUL);
21142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021143 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021144}
21145
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021146#ifdef FEAT_FLOAT
21147/*
21148 * "trunc({float})" function
21149 */
21150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021151f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021152{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021153 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021154
21155 rettv->v_type = VAR_FLOAT;
21156 if (get_float_arg(argvars, &f) == OK)
21157 /* trunc() is not in C90, use floor() or ceil() instead. */
21158 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21159 else
21160 rettv->vval.v_float = 0.0;
21161}
21162#endif
21163
Bram Moolenaar8299df92004-07-10 09:47:34 +000021164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 * "type(expr)" function
21166 */
21167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021168f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021170 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021171
21172 switch (argvars[0].v_type)
21173 {
21174 case VAR_NUMBER: n = 0; break;
21175 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021176 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021177 case VAR_FUNC: n = 2; break;
21178 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021179 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021180 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021181 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021182 if (argvars[0].vval.v_number == VVAL_FALSE
21183 || argvars[0].vval.v_number == VVAL_TRUE)
21184 n = 6;
21185 else
21186 n = 7;
21187 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021188 case VAR_JOB: n = 8; break;
21189 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021190 case VAR_UNKNOWN:
21191 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21192 n = -1;
21193 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021194 }
21195 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196}
21197
21198/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021199 * "undofile(name)" function
21200 */
21201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021202f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021203{
21204 rettv->v_type = VAR_STRING;
21205#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021206 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021207 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021208
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021209 if (*fname == NUL)
21210 {
21211 /* If there is no file name there will be no undo file. */
21212 rettv->vval.v_string = NULL;
21213 }
21214 else
21215 {
21216 char_u *ffname = FullName_save(fname, FALSE);
21217
21218 if (ffname != NULL)
21219 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21220 vim_free(ffname);
21221 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021222 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021223#else
21224 rettv->vval.v_string = NULL;
21225#endif
21226}
21227
21228/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021229 * "undotree()" function
21230 */
21231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021232f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021233{
21234 if (rettv_dict_alloc(rettv) == OK)
21235 {
21236 dict_T *dict = rettv->vval.v_dict;
21237 list_T *list;
21238
Bram Moolenaar730cde92010-06-27 05:18:54 +020021239 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021240 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021241 dict_add_nr_str(dict, "save_last",
21242 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021243 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21244 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021245 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021246
21247 list = list_alloc();
21248 if (list != NULL)
21249 {
21250 u_eval_tree(curbuf->b_u_oldhead, list);
21251 dict_add_list(dict, "entries", list);
21252 }
21253 }
21254}
21255
21256/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021257 * "values(dict)" function
21258 */
21259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021260f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021261{
21262 dict_list(argvars, rettv, 1);
21263}
21264
21265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 * "virtcol(string)" function
21267 */
21268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021269f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270{
21271 colnr_T vcol = 0;
21272 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021273 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021275 fp = var2fpos(&argvars[0], FALSE, &fnum);
21276 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21277 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 {
21279 getvvcol(curwin, fp, NULL, NULL, &vcol);
21280 ++vcol;
21281 }
21282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284}
21285
21286/*
21287 * "visualmode()" function
21288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021289 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021290f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 char_u str[2];
21293
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021294 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 str[0] = curbuf->b_visual_mode_eval;
21296 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021297 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298
21299 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021300 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302}
21303
21304/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021305 * "wildmenumode()" function
21306 */
21307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021308f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021309{
21310#ifdef FEAT_WILDMENU
21311 if (wild_menu_showing)
21312 rettv->vval.v_number = 1;
21313#endif
21314}
21315
21316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317 * "winbufnr(nr)" function
21318 */
21319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021320f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321{
21322 win_T *wp;
21323
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021324 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021326 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021328 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329}
21330
21331/*
21332 * "wincol()" function
21333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021335f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336{
21337 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021338 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339}
21340
21341/*
21342 * "winheight(nr)" function
21343 */
21344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021345f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346{
21347 win_T *wp;
21348
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021349 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021351 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021353 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354}
21355
21356/*
21357 * "winline()" function
21358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021360f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361{
21362 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021363 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021364}
21365
21366/*
21367 * "winnr()" function
21368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021370f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371{
21372 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021373
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021375 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021377 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378}
21379
21380/*
21381 * "winrestcmd()" function
21382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021384f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385{
21386#ifdef FEAT_WINDOWS
21387 win_T *wp;
21388 int winnr = 1;
21389 garray_T ga;
21390 char_u buf[50];
21391
21392 ga_init2(&ga, (int)sizeof(char), 70);
21393 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21394 {
21395 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21396 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21398 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 ++winnr;
21400 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021401 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021403 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021405 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021407 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408}
21409
21410/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021411 * "winrestview()" function
21412 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021414f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021415{
21416 dict_T *dict;
21417
21418 if (argvars[0].v_type != VAR_DICT
21419 || (dict = argvars[0].vval.v_dict) == NULL)
21420 EMSG(_(e_invarg));
21421 else
21422 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021423 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21424 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21425 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21426 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021427#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021428 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21429 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021430#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021431 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21432 {
21433 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21434 curwin->w_set_curswant = FALSE;
21435 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021436
Bram Moolenaar82c25852014-05-28 16:47:16 +020021437 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21438 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021439#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021440 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21441 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021442#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021443 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21444 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21445 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21446 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021447
21448 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021449 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021450# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021451 win_new_width(curwin, W_WIDTH(curwin));
21452# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021453 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021454
Bram Moolenaarb851a962014-10-31 15:45:52 +010021455 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021456 curwin->w_topline = 1;
21457 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21458 curwin->w_topline = curbuf->b_ml.ml_line_count;
21459#ifdef FEAT_DIFF
21460 check_topfill(curwin, TRUE);
21461#endif
21462 }
21463}
21464
21465/*
21466 * "winsaveview()" function
21467 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021469f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021470{
21471 dict_T *dict;
21472
Bram Moolenaara800b422010-06-27 01:15:55 +020021473 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021474 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021475 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021476
21477 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21478 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21479#ifdef FEAT_VIRTUALEDIT
21480 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21481#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021482 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021483 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21484
21485 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21486#ifdef FEAT_DIFF
21487 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21488#endif
21489 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21490 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21491}
21492
21493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 * "winwidth(nr)" function
21495 */
21496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021497f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498{
21499 win_T *wp;
21500
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021501 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021503 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021505#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021506 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021508 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509#endif
21510}
21511
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021513 * "wordcount()" function
21514 */
21515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021516f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021517{
21518 if (rettv_dict_alloc(rettv) == FAIL)
21519 return;
21520 cursor_pos_info(rettv->vval.v_dict);
21521}
21522
21523/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021524 * Write list of strings to file
21525 */
21526 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021527write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021528{
21529 listitem_T *li;
21530 int c;
21531 int ret = OK;
21532 char_u *s;
21533
21534 for (li = list->lv_first; li != NULL; li = li->li_next)
21535 {
21536 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21537 {
21538 if (*s == '\n')
21539 c = putc(NUL, fd);
21540 else
21541 c = putc(*s, fd);
21542 if (c == EOF)
21543 {
21544 ret = FAIL;
21545 break;
21546 }
21547 }
21548 if (!binary || li->li_next != NULL)
21549 if (putc('\n', fd) == EOF)
21550 {
21551 ret = FAIL;
21552 break;
21553 }
21554 if (ret == FAIL)
21555 {
21556 EMSG(_(e_write));
21557 break;
21558 }
21559 }
21560 return ret;
21561}
21562
21563/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021564 * "writefile()" function
21565 */
21566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021567f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021568{
21569 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021570 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021571 char_u *fname;
21572 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021573 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021574
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021575 if (check_restricted() || check_secure())
21576 return;
21577
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021578 if (argvars[0].v_type != VAR_LIST)
21579 {
21580 EMSG2(_(e_listarg), "writefile()");
21581 return;
21582 }
21583 if (argvars[0].vval.v_list == NULL)
21584 return;
21585
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021586 if (argvars[2].v_type != VAR_UNKNOWN)
21587 {
21588 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21589 binary = TRUE;
21590 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21591 append = TRUE;
21592 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021593
21594 /* Always open the file in binary mode, library functions have a mind of
21595 * their own about CR-LF conversion. */
21596 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021597 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21598 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021599 {
21600 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21601 ret = -1;
21602 }
21603 else
21604 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021605 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21606 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021607 fclose(fd);
21608 }
21609
21610 rettv->vval.v_number = ret;
21611}
21612
21613/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021614 * "xor(expr, expr)" function
21615 */
21616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021617f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021618{
21619 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21620 ^ get_tv_number_chk(&argvars[1], NULL);
21621}
21622
21623
21624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021626 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627 */
21628 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021629var2fpos(
21630 typval_T *varp,
21631 int dollar_lnum, /* TRUE when $ is last line */
21632 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021634 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021636 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637
Bram Moolenaara5525202006-03-02 22:52:09 +000021638 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021639 if (varp->v_type == VAR_LIST)
21640 {
21641 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021642 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021643 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021644 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021645
21646 l = varp->vval.v_list;
21647 if (l == NULL)
21648 return NULL;
21649
21650 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021651 pos.lnum = list_find_nr(l, 0L, &error);
21652 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021653 return NULL; /* invalid line number */
21654
21655 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021656 pos.col = list_find_nr(l, 1L, &error);
21657 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021658 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021659 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021660
21661 /* We accept "$" for the column number: last column. */
21662 li = list_find(l, 1L);
21663 if (li != NULL && li->li_tv.v_type == VAR_STRING
21664 && li->li_tv.vval.v_string != NULL
21665 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21666 pos.col = len + 1;
21667
Bram Moolenaara5525202006-03-02 22:52:09 +000021668 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021669 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021670 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021671 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021672
Bram Moolenaara5525202006-03-02 22:52:09 +000021673#ifdef FEAT_VIRTUALEDIT
21674 /* Get the virtual offset. Defaults to zero. */
21675 pos.coladd = list_find_nr(l, 2L, &error);
21676 if (error)
21677 pos.coladd = 0;
21678#endif
21679
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021680 return &pos;
21681 }
21682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021683 name = get_tv_string_chk(varp);
21684 if (name == NULL)
21685 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021686 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021688 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21689 {
21690 if (VIsual_active)
21691 return &VIsual;
21692 return &curwin->w_cursor;
21693 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021694 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021696 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21698 return NULL;
21699 return pp;
21700 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021701
21702#ifdef FEAT_VIRTUALEDIT
21703 pos.coladd = 0;
21704#endif
21705
Bram Moolenaar477933c2007-07-17 14:32:23 +000021706 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021707 {
21708 pos.col = 0;
21709 if (name[1] == '0') /* "w0": first visible line */
21710 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021711 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021712 pos.lnum = curwin->w_topline;
21713 return &pos;
21714 }
21715 else if (name[1] == '$') /* "w$": last visible line */
21716 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021717 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021718 pos.lnum = curwin->w_botline - 1;
21719 return &pos;
21720 }
21721 }
21722 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021724 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725 {
21726 pos.lnum = curbuf->b_ml.ml_line_count;
21727 pos.col = 0;
21728 }
21729 else
21730 {
21731 pos.lnum = curwin->w_cursor.lnum;
21732 pos.col = (colnr_T)STRLEN(ml_get_curline());
21733 }
21734 return &pos;
21735 }
21736 return NULL;
21737}
21738
21739/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021740 * Convert list in "arg" into a position and optional file number.
21741 * When "fnump" is NULL there is no file number, only 3 items.
21742 * Note that the column is passed on as-is, the caller may want to decrement
21743 * it to use 1 for the first column.
21744 * Return FAIL when conversion is not possible, doesn't check the position for
21745 * validity.
21746 */
21747 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021748list2fpos(
21749 typval_T *arg,
21750 pos_T *posp,
21751 int *fnump,
21752 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021753{
21754 list_T *l = arg->vval.v_list;
21755 long i = 0;
21756 long n;
21757
Bram Moolenaar493c1782014-05-28 14:34:46 +020021758 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21759 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021760 if (arg->v_type != VAR_LIST
21761 || l == NULL
21762 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021763 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021764 return FAIL;
21765
21766 if (fnump != NULL)
21767 {
21768 n = list_find_nr(l, i++, NULL); /* fnum */
21769 if (n < 0)
21770 return FAIL;
21771 if (n == 0)
21772 n = curbuf->b_fnum; /* current buffer */
21773 *fnump = n;
21774 }
21775
21776 n = list_find_nr(l, i++, NULL); /* lnum */
21777 if (n < 0)
21778 return FAIL;
21779 posp->lnum = n;
21780
21781 n = list_find_nr(l, i++, NULL); /* col */
21782 if (n < 0)
21783 return FAIL;
21784 posp->col = n;
21785
21786#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021787 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021788 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021789 posp->coladd = 0;
21790 else
21791 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021792#endif
21793
Bram Moolenaar493c1782014-05-28 14:34:46 +020021794 if (curswantp != NULL)
21795 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21796
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021797 return OK;
21798}
21799
21800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801 * Get the length of an environment variable name.
21802 * Advance "arg" to the first character after the name.
21803 * Return 0 for error.
21804 */
21805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021806get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807{
21808 char_u *p;
21809 int len;
21810
21811 for (p = *arg; vim_isIDc(*p); ++p)
21812 ;
21813 if (p == *arg) /* no name found */
21814 return 0;
21815
21816 len = (int)(p - *arg);
21817 *arg = p;
21818 return len;
21819}
21820
21821/*
21822 * Get the length of the name of a function or internal variable.
21823 * "arg" is advanced to the first non-white character after the name.
21824 * Return 0 if something is wrong.
21825 */
21826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021827get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828{
21829 char_u *p;
21830 int len;
21831
21832 /* Find the end of the name. */
21833 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021834 {
21835 if (*p == ':')
21836 {
21837 /* "s:" is start of "s:var", but "n:" is not and can be used in
21838 * slice "[n:]". Also "xx:" is not a namespace. */
21839 len = (int)(p - *arg);
21840 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21841 || len > 1)
21842 break;
21843 }
21844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 if (p == *arg) /* no name found */
21846 return 0;
21847
21848 len = (int)(p - *arg);
21849 *arg = skipwhite(p);
21850
21851 return len;
21852}
21853
21854/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021855 * Get the length of the name of a variable or function.
21856 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021858 * Return -1 if curly braces expansion failed.
21859 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 * If the name contains 'magic' {}'s, expand them and return the
21861 * expanded name in an allocated string via 'alias' - caller must free.
21862 */
21863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021864get_name_len(
21865 char_u **arg,
21866 char_u **alias,
21867 int evaluate,
21868 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869{
21870 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 char_u *p;
21872 char_u *expr_start;
21873 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874
21875 *alias = NULL; /* default to no alias */
21876
21877 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21878 && (*arg)[2] == (int)KE_SNR)
21879 {
21880 /* hard coded <SNR>, already translated */
21881 *arg += 3;
21882 return get_id_len(arg) + 3;
21883 }
21884 len = eval_fname_script(*arg);
21885 if (len > 0)
21886 {
21887 /* literal "<SID>", "s:" or "<SNR>" */
21888 *arg += len;
21889 }
21890
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021892 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021894 p = find_name_end(*arg, &expr_start, &expr_end,
21895 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896 if (expr_start != NULL)
21897 {
21898 char_u *temp_string;
21899
21900 if (!evaluate)
21901 {
21902 len += (int)(p - *arg);
21903 *arg = skipwhite(p);
21904 return len;
21905 }
21906
21907 /*
21908 * Include any <SID> etc in the expanded string:
21909 * Thus the -len here.
21910 */
21911 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21912 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021913 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914 *alias = temp_string;
21915 *arg = skipwhite(p);
21916 return (int)STRLEN(temp_string);
21917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918
21919 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021920 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 EMSG2(_(e_invexpr2), *arg);
21922
21923 return len;
21924}
21925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021926/*
21927 * Find the end of a variable or function name, taking care of magic braces.
21928 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21929 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021930 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021931 * Return a pointer to just after the name. Equal to "arg" if there is no
21932 * valid name.
21933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021935find_name_end(
21936 char_u *arg,
21937 char_u **expr_start,
21938 char_u **expr_end,
21939 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021940{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021941 int mb_nest = 0;
21942 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021944 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021946 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021948 *expr_start = NULL;
21949 *expr_end = NULL;
21950 }
21951
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021952 /* Quick check for valid starting character. */
21953 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21954 return arg;
21955
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021956 for (p = arg; *p != NUL
21957 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021958 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021959 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021960 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021961 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021962 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021963 if (*p == '\'')
21964 {
21965 /* skip over 'string' to avoid counting [ and ] inside it. */
21966 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21967 ;
21968 if (*p == NUL)
21969 break;
21970 }
21971 else if (*p == '"')
21972 {
21973 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21974 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21975 if (*p == '\\' && p[1] != NUL)
21976 ++p;
21977 if (*p == NUL)
21978 break;
21979 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021980 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21981 {
21982 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021983 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021984 len = (int)(p - arg);
21985 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021986 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021987 break;
21988 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021990 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021992 if (*p == '[')
21993 ++br_nest;
21994 else if (*p == ']')
21995 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021998 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022000 if (*p == '{')
22001 {
22002 mb_nest++;
22003 if (expr_start != NULL && *expr_start == NULL)
22004 *expr_start = p;
22005 }
22006 else if (*p == '}')
22007 {
22008 mb_nest--;
22009 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22010 *expr_end = p;
22011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 }
22014
22015 return p;
22016}
22017
22018/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022019 * Expands out the 'magic' {}'s in a variable/function name.
22020 * Note that this can call itself recursively, to deal with
22021 * constructs like foo{bar}{baz}{bam}
22022 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22023 * "in_start" ^
22024 * "expr_start" ^
22025 * "expr_end" ^
22026 * "in_end" ^
22027 *
22028 * Returns a new allocated string, which the caller must free.
22029 * Returns NULL for failure.
22030 */
22031 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022032make_expanded_name(
22033 char_u *in_start,
22034 char_u *expr_start,
22035 char_u *expr_end,
22036 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022037{
22038 char_u c1;
22039 char_u *retval = NULL;
22040 char_u *temp_result;
22041 char_u *nextcmd = NULL;
22042
22043 if (expr_end == NULL || in_end == NULL)
22044 return NULL;
22045 *expr_start = NUL;
22046 *expr_end = NUL;
22047 c1 = *in_end;
22048 *in_end = NUL;
22049
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022050 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022051 if (temp_result != NULL && nextcmd == NULL)
22052 {
22053 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22054 + (in_end - expr_end) + 1));
22055 if (retval != NULL)
22056 {
22057 STRCPY(retval, in_start);
22058 STRCAT(retval, temp_result);
22059 STRCAT(retval, expr_end + 1);
22060 }
22061 }
22062 vim_free(temp_result);
22063
22064 *in_end = c1; /* put char back for error messages */
22065 *expr_start = '{';
22066 *expr_end = '}';
22067
22068 if (retval != NULL)
22069 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022070 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022071 if (expr_start != NULL)
22072 {
22073 /* Further expansion! */
22074 temp_result = make_expanded_name(retval, expr_start,
22075 expr_end, temp_result);
22076 vim_free(retval);
22077 retval = temp_result;
22078 }
22079 }
22080
22081 return retval;
22082}
22083
22084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022086 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087 */
22088 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022089eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022091 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22092}
22093
22094/*
22095 * Return TRUE if character "c" can be used as the first character in a
22096 * variable or function name (excluding '{' and '}').
22097 */
22098 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022099eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022100{
22101 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102}
22103
22104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 * Set number v: variable to "val".
22106 */
22107 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022108set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022110 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022111}
22112
22113/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022114 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115 */
22116 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022117get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022119 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120}
22121
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022122/*
22123 * Get string v: variable value. Uses a static buffer, can only be used once.
22124 */
22125 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022126get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022127{
22128 return get_tv_string(&vimvars[idx].vv_tv);
22129}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022130
Bram Moolenaar071d4272004-06-13 20:20:40 +000022131/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022132 * Get List v: variable value. Caller must take care of reference count when
22133 * needed.
22134 */
22135 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022136get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022137{
22138 return vimvars[idx].vv_list;
22139}
22140
22141/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022142 * Set v:char to character "c".
22143 */
22144 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022145set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022146{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022147 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022148
22149#ifdef FEAT_MBYTE
22150 if (has_mbyte)
22151 buf[(*mb_char2bytes)(c, buf)] = NUL;
22152 else
22153#endif
22154 {
22155 buf[0] = c;
22156 buf[1] = NUL;
22157 }
22158 set_vim_var_string(VV_CHAR, buf, -1);
22159}
22160
22161/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022162 * Set v:count to "count" and v:count1 to "count1".
22163 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164 */
22165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022166set_vcount(
22167 long count,
22168 long count1,
22169 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022171 if (set_prevcount)
22172 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022173 vimvars[VV_COUNT].vv_nr = count;
22174 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175}
22176
22177/*
22178 * Set string v: variable to a copy of "val".
22179 */
22180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022181set_vim_var_string(
22182 int idx,
22183 char_u *val,
22184 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185{
Bram Moolenaara542c682016-01-31 16:28:04 +010022186 clear_tv(&vimvars[idx].vv_di.di_tv);
22187 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022189 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022190 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022191 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022193 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194}
22195
22196/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022197 * Set List v: variable to "val".
22198 */
22199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022200set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022201{
Bram Moolenaara542c682016-01-31 16:28:04 +010022202 clear_tv(&vimvars[idx].vv_di.di_tv);
22203 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022204 vimvars[idx].vv_list = val;
22205 if (val != NULL)
22206 ++val->lv_refcount;
22207}
22208
22209/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022210 * Set Dictionary v: variable to "val".
22211 */
22212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022213set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022214{
22215 int todo;
22216 hashitem_T *hi;
22217
Bram Moolenaara542c682016-01-31 16:28:04 +010022218 clear_tv(&vimvars[idx].vv_di.di_tv);
22219 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022220 vimvars[idx].vv_dict = val;
22221 if (val != NULL)
22222 {
22223 ++val->dv_refcount;
22224
22225 /* Set readonly */
22226 todo = (int)val->dv_hashtab.ht_used;
22227 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22228 {
22229 if (HASHITEM_EMPTY(hi))
22230 continue;
22231 --todo;
22232 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22233 }
22234 }
22235}
22236
22237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 * Set v:register if needed.
22239 */
22240 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022241set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242{
22243 char_u regname;
22244
22245 if (c == 0 || c == ' ')
22246 regname = '"';
22247 else
22248 regname = c;
22249 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022250 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251 set_vim_var_string(VV_REG, &regname, 1);
22252}
22253
22254/*
22255 * Get or set v:exception. If "oldval" == NULL, return the current value.
22256 * Otherwise, restore the value to "oldval" and return NULL.
22257 * Must always be called in pairs to save and restore v:exception! Does not
22258 * take care of memory allocations.
22259 */
22260 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022261v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262{
22263 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022264 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265
Bram Moolenaare9a41262005-01-15 22:18:47 +000022266 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 return NULL;
22268}
22269
22270/*
22271 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22272 * Otherwise, restore the value to "oldval" and return NULL.
22273 * Must always be called in pairs to save and restore v:throwpoint! Does not
22274 * take care of memory allocations.
22275 */
22276 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022277v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278{
22279 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022280 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281
Bram Moolenaare9a41262005-01-15 22:18:47 +000022282 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 return NULL;
22284}
22285
22286#if defined(FEAT_AUTOCMD) || defined(PROTO)
22287/*
22288 * Set v:cmdarg.
22289 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22290 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22291 * Must always be called in pairs!
22292 */
22293 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022294set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295{
22296 char_u *oldval;
22297 char_u *newval;
22298 unsigned len;
22299
Bram Moolenaare9a41262005-01-15 22:18:47 +000022300 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022301 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022303 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022304 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022305 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306 }
22307
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022308 if (eap->force_bin == FORCE_BIN)
22309 len = 6;
22310 else if (eap->force_bin == FORCE_NOBIN)
22311 len = 8;
22312 else
22313 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022314
22315 if (eap->read_edit)
22316 len += 7;
22317
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022318 if (eap->force_ff != 0)
22319 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22320# ifdef FEAT_MBYTE
22321 if (eap->force_enc != 0)
22322 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022323 if (eap->bad_char != 0)
22324 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022325# endif
22326
22327 newval = alloc(len + 1);
22328 if (newval == NULL)
22329 return NULL;
22330
22331 if (eap->force_bin == FORCE_BIN)
22332 sprintf((char *)newval, " ++bin");
22333 else if (eap->force_bin == FORCE_NOBIN)
22334 sprintf((char *)newval, " ++nobin");
22335 else
22336 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022337
22338 if (eap->read_edit)
22339 STRCAT(newval, " ++edit");
22340
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022341 if (eap->force_ff != 0)
22342 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22343 eap->cmd + eap->force_ff);
22344# ifdef FEAT_MBYTE
22345 if (eap->force_enc != 0)
22346 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22347 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022348 if (eap->bad_char == BAD_KEEP)
22349 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22350 else if (eap->bad_char == BAD_DROP)
22351 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22352 else if (eap->bad_char != 0)
22353 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022354# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022355 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022356 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357}
22358#endif
22359
22360/*
22361 * Get the value of internal variable "name".
22362 * Return OK or FAIL.
22363 */
22364 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022365get_var_tv(
22366 char_u *name,
22367 int len, /* length of "name" */
22368 typval_T *rettv, /* NULL when only checking existence */
22369 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22370 int verbose, /* may give error message */
22371 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372{
22373 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022374 typval_T *tv = NULL;
22375 typval_T atv;
22376 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378
22379 /* truncate the name, so that we can use strcmp() */
22380 cc = name[len];
22381 name[len] = NUL;
22382
22383 /*
22384 * Check for "b:changedtick".
22385 */
22386 if (STRCMP(name, "b:changedtick") == 0)
22387 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022388 atv.v_type = VAR_NUMBER;
22389 atv.vval.v_number = curbuf->b_changedtick;
22390 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 }
22392
22393 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 * Check for user-defined variables.
22395 */
22396 else
22397 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022398 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022400 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022401 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022402 if (dip != NULL)
22403 *dip = v;
22404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 }
22406
Bram Moolenaare9a41262005-01-15 22:18:47 +000022407 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022409 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022410 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 ret = FAIL;
22412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022413 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022414 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415
22416 name[len] = cc;
22417
22418 return ret;
22419}
22420
22421/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022422 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22423 * Also handle function call with Funcref variable: func(expr)
22424 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22425 */
22426 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022427handle_subscript(
22428 char_u **arg,
22429 typval_T *rettv,
22430 int evaluate, /* do more than finding the end */
22431 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022432{
22433 int ret = OK;
22434 dict_T *selfdict = NULL;
22435 char_u *s;
22436 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022437 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022438
22439 while (ret == OK
22440 && (**arg == '['
22441 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022442 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22443 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022444 && !vim_iswhite(*(*arg - 1)))
22445 {
22446 if (**arg == '(')
22447 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022448 partial_T *pt = NULL;
22449
Bram Moolenaard9fba312005-06-26 22:34:35 +000022450 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022451 if (evaluate)
22452 {
22453 functv = *rettv;
22454 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022455
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022456 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022457 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022458 {
22459 pt = functv.vval.v_partial;
22460 s = pt->pt_name;
22461 }
22462 else
22463 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022464 }
22465 else
22466 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022467 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022468 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022469 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022470
22471 /* Clear the funcref afterwards, so that deleting it while
22472 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022473 if (evaluate)
22474 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022475
22476 /* Stop the expression evaluation when immediately aborting on
22477 * error, or when an interrupt occurred or an exception was thrown
22478 * but not caught. */
22479 if (aborting())
22480 {
22481 if (ret == OK)
22482 clear_tv(rettv);
22483 ret = FAIL;
22484 }
22485 dict_unref(selfdict);
22486 selfdict = NULL;
22487 }
22488 else /* **arg == '[' || **arg == '.' */
22489 {
22490 dict_unref(selfdict);
22491 if (rettv->v_type == VAR_DICT)
22492 {
22493 selfdict = rettv->vval.v_dict;
22494 if (selfdict != NULL)
22495 ++selfdict->dv_refcount;
22496 }
22497 else
22498 selfdict = NULL;
22499 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22500 {
22501 clear_tv(rettv);
22502 ret = FAIL;
22503 }
22504 }
22505 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022506
Bram Moolenaar1d429612016-05-24 15:44:17 +020022507 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22508 * Don't do this when "Func" is already a partial that was bound
22509 * explicitly (pt_auto is FALSE). */
22510 if (selfdict != NULL
22511 && (rettv->v_type == VAR_FUNC
22512 || (rettv->v_type == VAR_PARTIAL
22513 && (rettv->vval.v_partial->pt_auto
22514 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022515 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022516 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22517 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022518 char_u *tofree = NULL;
22519 ufunc_T *fp;
22520 char_u fname_buf[FLEN_FIXED + 1];
22521 int error;
22522
22523 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022524 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022525 fp = find_func(fname);
22526 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022527
Bram Moolenaar65639032016-03-16 21:40:30 +010022528 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022529 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022530 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22531
22532 if (pt != NULL)
22533 {
22534 pt->pt_refcount = 1;
22535 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022536 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022537 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022538 if (rettv->v_type == VAR_FUNC)
22539 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022540 /* Just a function: Take over the function name and use
22541 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022542 pt->pt_name = rettv->vval.v_string;
22543 }
22544 else
22545 {
22546 partial_T *ret_pt = rettv->vval.v_partial;
22547 int i;
22548
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022549 /* Partial: copy the function name, use selfdict and copy
22550 * args. Can't take over name or args, the partial might
22551 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022552 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022553 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022554 if (ret_pt->pt_argc > 0)
22555 {
22556 pt->pt_argv = (typval_T *)alloc(
22557 sizeof(typval_T) * ret_pt->pt_argc);
22558 if (pt->pt_argv == NULL)
22559 /* out of memory: drop the arguments */
22560 pt->pt_argc = 0;
22561 else
22562 {
22563 pt->pt_argc = ret_pt->pt_argc;
22564 for (i = 0; i < pt->pt_argc; i++)
22565 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22566 }
22567 }
22568 partial_unref(ret_pt);
22569 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022570 rettv->v_type = VAR_PARTIAL;
22571 rettv->vval.v_partial = pt;
22572 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022573 }
22574 }
22575
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022576 dict_unref(selfdict);
22577 return ret;
22578}
22579
22580/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022581 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022582 * value).
22583 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022584 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022585alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022586{
Bram Moolenaar33570922005-01-25 22:26:29 +000022587 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022588}
22589
22590/*
22591 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 * The string "s" must have been allocated, it is consumed.
22593 * Return NULL for out of memory, the variable otherwise.
22594 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022595 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022596alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597{
Bram Moolenaar33570922005-01-25 22:26:29 +000022598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022600 rettv = alloc_tv();
22601 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022603 rettv->v_type = VAR_STRING;
22604 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 }
22606 else
22607 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022608 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609}
22610
22611/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022612 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022615free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616{
22617 if (varp != NULL)
22618 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022619 switch (varp->v_type)
22620 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022621 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022622 func_unref(varp->vval.v_string);
22623 /*FALLTHROUGH*/
22624 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022625 vim_free(varp->vval.v_string);
22626 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022627 case VAR_PARTIAL:
22628 partial_unref(varp->vval.v_partial);
22629 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022630 case VAR_LIST:
22631 list_unref(varp->vval.v_list);
22632 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022633 case VAR_DICT:
22634 dict_unref(varp->vval.v_dict);
22635 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022636 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022637#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022638 job_unref(varp->vval.v_job);
22639 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022640#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022641 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022642#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022643 channel_unref(varp->vval.v_channel);
22644 break;
22645#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022646 case VAR_NUMBER:
22647 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022648 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022649 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022650 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 vim_free(varp);
22653 }
22654}
22655
22656/*
22657 * Free the memory for a variable value and set the value to NULL or 0.
22658 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022660clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661{
22662 if (varp != NULL)
22663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022664 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022666 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022667 func_unref(varp->vval.v_string);
22668 /*FALLTHROUGH*/
22669 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022670 vim_free(varp->vval.v_string);
22671 varp->vval.v_string = NULL;
22672 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022673 case VAR_PARTIAL:
22674 partial_unref(varp->vval.v_partial);
22675 varp->vval.v_partial = NULL;
22676 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022677 case VAR_LIST:
22678 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022679 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022680 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022681 case VAR_DICT:
22682 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022683 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022684 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022685 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022686 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022687 varp->vval.v_number = 0;
22688 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022689 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022690#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022691 varp->vval.v_float = 0.0;
22692 break;
22693#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022694 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022695#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022696 job_unref(varp->vval.v_job);
22697 varp->vval.v_job = NULL;
22698#endif
22699 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022700 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022701#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022702 channel_unref(varp->vval.v_channel);
22703 varp->vval.v_channel = NULL;
22704#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022705 case VAR_UNKNOWN:
22706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022708 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 }
22710}
22711
22712/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022713 * Set the value of a variable to NULL without freeing items.
22714 */
22715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022716init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022717{
22718 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022719 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022720}
22721
22722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 * Get the number value of a variable.
22724 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022725 * For incompatible types, return 0.
22726 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22727 * caller of incompatible types: it sets *denote to TRUE if "denote"
22728 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022730 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022731get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022733 int error = FALSE;
22734
22735 return get_tv_number_chk(varp, &error); /* return 0L on error */
22736}
22737
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022738 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022739get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022740{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022741 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022743 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022745 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022746 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022747 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022748#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022749 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022750 break;
22751#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022752 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022753 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022754 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022755 break;
22756 case VAR_STRING:
22757 if (varp->vval.v_string != NULL)
22758 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022759 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022760 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022761 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022762 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022763 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022764 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022765 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022766 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022767 case VAR_SPECIAL:
22768 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22769 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022770 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022771#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022772 EMSG(_("E910: Using a Job as a Number"));
22773 break;
22774#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022775 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022776#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022777 EMSG(_("E913: Using a Channel as a Number"));
22778 break;
22779#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022780 case VAR_UNKNOWN:
22781 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022782 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022784 if (denote == NULL) /* useful for values that must be unsigned */
22785 n = -1;
22786 else
22787 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022788 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789}
22790
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022791#ifdef FEAT_FLOAT
22792 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022793get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022794{
22795 switch (varp->v_type)
22796 {
22797 case VAR_NUMBER:
22798 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022799 case VAR_FLOAT:
22800 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022801 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022802 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022803 EMSG(_("E891: Using a Funcref as a Float"));
22804 break;
22805 case VAR_STRING:
22806 EMSG(_("E892: Using a String as a Float"));
22807 break;
22808 case VAR_LIST:
22809 EMSG(_("E893: Using a List as a Float"));
22810 break;
22811 case VAR_DICT:
22812 EMSG(_("E894: Using a Dictionary as a Float"));
22813 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022814 case VAR_SPECIAL:
22815 EMSG(_("E907: Using a special value as a Float"));
22816 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022817 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022818# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022819 EMSG(_("E911: Using a Job as a Float"));
22820 break;
22821# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022822 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022823# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022824 EMSG(_("E914: Using a Channel as a Float"));
22825 break;
22826# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022827 case VAR_UNKNOWN:
22828 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022829 break;
22830 }
22831 return 0;
22832}
22833#endif
22834
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022836 * Get the lnum from the first argument.
22837 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022838 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 */
22840 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022841get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842{
Bram Moolenaar33570922005-01-25 22:26:29 +000022843 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 linenr_T lnum;
22845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022846 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 if (lnum == 0) /* no valid number, try using line() */
22848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022849 rettv.v_type = VAR_NUMBER;
22850 f_line(argvars, &rettv);
22851 lnum = rettv.vval.v_number;
22852 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853 }
22854 return lnum;
22855}
22856
22857/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022858 * Get the lnum from the first argument.
22859 * Also accepts "$", then "buf" is used.
22860 * Returns 0 on error.
22861 */
22862 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022863get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022864{
22865 if (argvars[0].v_type == VAR_STRING
22866 && argvars[0].vval.v_string != NULL
22867 && argvars[0].vval.v_string[0] == '$'
22868 && buf != NULL)
22869 return buf->b_ml.ml_line_count;
22870 return get_tv_number_chk(&argvars[0], NULL);
22871}
22872
22873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 * Get the string value of a variable.
22875 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022876 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22877 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 * If the String variable has never been set, return an empty string.
22879 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022880 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22881 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022883 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022884get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885{
22886 static char_u mybuf[NUMBUFLEN];
22887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022888 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889}
22890
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022891 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022892get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022894 char_u *res = get_tv_string_buf_chk(varp, buf);
22895
22896 return res != NULL ? res : (char_u *)"";
22897}
22898
Bram Moolenaar7d647822014-04-05 21:28:56 +020022899/*
22900 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22901 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022902 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022903get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022904{
22905 static char_u mybuf[NUMBUFLEN];
22906
22907 return get_tv_string_buf_chk(varp, mybuf);
22908}
22909
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022910 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022911get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022912{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022913 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022915 case VAR_NUMBER:
22916 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22917 return buf;
22918 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022919 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022920 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022921 break;
22922 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022923 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022924 break;
22925 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022926 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022927 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022928 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022929#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022930 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022931 break;
22932#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022933 case VAR_STRING:
22934 if (varp->vval.v_string != NULL)
22935 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022936 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022937 case VAR_SPECIAL:
22938 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22939 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022940 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022941#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022942 {
22943 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022944 char *status;
22945
22946 if (job == NULL)
22947 return (char_u *)"no process";
22948 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022949 : job->jv_status == JOB_ENDED ? "dead"
22950 : "run";
22951# ifdef UNIX
22952 vim_snprintf((char *)buf, NUMBUFLEN,
22953 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022954# elif defined(WIN32)
22955 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022956 "process %ld %s",
22957 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022958 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022959# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022960 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022961 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22962# endif
22963 return buf;
22964 }
22965#endif
22966 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022967 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022968#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022969 {
22970 channel_T *channel = varp->vval.v_channel;
22971 char *status = channel_status(channel);
22972
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022973 if (channel == NULL)
22974 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22975 else
22976 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022977 "channel %d %s", channel->ch_id, status);
22978 return buf;
22979 }
22980#endif
22981 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022982 case VAR_UNKNOWN:
22983 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022984 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022986 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987}
22988
22989/*
22990 * Find variable "name" in the list of variables.
22991 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022992 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022993 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022994 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022996 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022997find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023000 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001
Bram Moolenaara7043832005-01-21 11:56:39 +000023002 ht = find_var_ht(name, &varname);
23003 if (htp != NULL)
23004 *htp = ht;
23005 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023007 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008}
23009
23010/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023011 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023012 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023015find_var_in_ht(
23016 hashtab_T *ht,
23017 int htname,
23018 char_u *varname,
23019 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023020{
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 hashitem_T *hi;
23022
23023 if (*varname == NUL)
23024 {
23025 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023026 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023027 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023028 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023029 case 'g': return &globvars_var;
23030 case 'v': return &vimvars_var;
23031 case 'b': return &curbuf->b_bufvar;
23032 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023033#ifdef FEAT_WINDOWS
23034 case 't': return &curtab->tp_winvar;
23035#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023036 case 'l': return current_funccal == NULL
23037 ? NULL : &current_funccal->l_vars_var;
23038 case 'a': return current_funccal == NULL
23039 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023040 }
23041 return NULL;
23042 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023043
23044 hi = hash_find(ht, varname);
23045 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023046 {
23047 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023048 * worked find the variable again. Don't auto-load a script if it was
23049 * loaded already, otherwise it would be loaded every time when
23050 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023051 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023052 {
23053 /* Note: script_autoload() may make "hi" invalid. It must either
23054 * be obtained again or not used. */
23055 if (!script_autoload(varname, FALSE) || aborting())
23056 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023057 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023058 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023059 if (HASHITEM_EMPTY(hi))
23060 return NULL;
23061 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023062 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023063}
23064
23065/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023066 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023067 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023068 * Set "varname" to the start of name without ':'.
23069 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023070 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023071find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023073 hashitem_T *hi;
23074
Bram Moolenaar73627d02015-08-11 15:46:09 +020023075 if (name[0] == NUL)
23076 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077 if (name[1] != ':')
23078 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023079 /* The name must not start with a colon or #. */
23080 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 return NULL;
23082 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023083
23084 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023085 hi = hash_find(&compat_hashtab, name);
23086 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023087 return &compat_hashtab;
23088
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023090 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023091 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 }
23093 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023094 if (*name == 'g') /* global variable */
23095 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023096 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23097 */
23098 if (vim_strchr(name + 2, ':') != NULL
23099 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023100 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023102 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023104 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023105#ifdef FEAT_WINDOWS
23106 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023107 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023108#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023109 if (*name == 'v') /* v: variable */
23110 return &vimvarht;
23111 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023112 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023113 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023114 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 if (*name == 's' /* script variable */
23116 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23117 return &SCRIPT_VARS(current_SID);
23118 return NULL;
23119}
23120
23121/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023122 * Get function call environment based on bactrace debug level
23123 */
23124 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023125get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023126{
23127 int i;
23128 funccall_T *funccal;
23129 funccall_T *temp_funccal;
23130
23131 funccal = current_funccal;
23132 if (debug_backtrace_level > 0)
23133 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023134 for (i = 0; i < debug_backtrace_level; i++)
23135 {
23136 temp_funccal = funccal->caller;
23137 if (temp_funccal)
23138 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023139 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023140 /* backtrace level overflow. reset to max */
23141 debug_backtrace_level = i;
23142 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023143 }
23144 return funccal;
23145}
23146
23147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023149 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 * Returns NULL when it doesn't exist.
23151 */
23152 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023153get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154{
Bram Moolenaar33570922005-01-25 22:26:29 +000023155 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023157 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023158 if (v == NULL)
23159 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023160 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161}
23162
23163/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023164 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 * sourcing this script and when executing functions defined in the script.
23166 */
23167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023168new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169{
Bram Moolenaara7043832005-01-21 11:56:39 +000023170 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023171 hashtab_T *ht;
23172 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023173
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23175 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023176 /* Re-allocating ga_data means that an ht_array pointing to
23177 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023178 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023179 for (i = 1; i <= ga_scripts.ga_len; ++i)
23180 {
23181 ht = &SCRIPT_VARS(i);
23182 if (ht->ht_mask == HT_INIT_SIZE - 1)
23183 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023184 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023185 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023186 }
23187
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 while (ga_scripts.ga_len < id)
23189 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023190 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023191 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023192 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 }
23195 }
23196}
23197
23198/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023199 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23200 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 */
23202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023203init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204{
Bram Moolenaar33570922005-01-25 22:26:29 +000023205 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023206 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023207 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023208 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023209 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023210 dict_var->di_tv.vval.v_dict = dict;
23211 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023212 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023213 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23214 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215}
23216
23217/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023218 * Unreference a dictionary initialized by init_var_dict().
23219 */
23220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023221unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023222{
23223 /* Now the dict needs to be freed if no one else is using it, go back to
23224 * normal reference counting. */
23225 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23226 dict_unref(dict);
23227}
23228
23229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023231 * Frees all allocated variables and the value they contain.
23232 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233 */
23234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023235vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023236{
23237 vars_clear_ext(ht, TRUE);
23238}
23239
23240/*
23241 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23242 */
23243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023244vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245{
Bram Moolenaara7043832005-01-21 11:56:39 +000023246 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023247 hashitem_T *hi;
23248 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249
Bram Moolenaar33570922005-01-25 22:26:29 +000023250 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023251 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023252 for (hi = ht->ht_array; todo > 0; ++hi)
23253 {
23254 if (!HASHITEM_EMPTY(hi))
23255 {
23256 --todo;
23257
Bram Moolenaar33570922005-01-25 22:26:29 +000023258 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023259 * ht_array might change then. hash_clear() takes care of it
23260 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023261 v = HI2DI(hi);
23262 if (free_val)
23263 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023264 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023265 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023266 }
23267 }
23268 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023269 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270}
23271
Bram Moolenaara7043832005-01-21 11:56:39 +000023272/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023273 * Delete a variable from hashtab "ht" at item "hi".
23274 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023277delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278{
Bram Moolenaar33570922005-01-25 22:26:29 +000023279 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023280
23281 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023282 clear_tv(&di->di_tv);
23283 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284}
23285
23286/*
23287 * List the value of one internal variable.
23288 */
23289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023290list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023292 char_u *tofree;
23293 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023294 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023295
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023296 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023297 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023298 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023299 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300}
23301
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023303list_one_var_a(
23304 char_u *prefix,
23305 char_u *name,
23306 int type,
23307 char_u *string,
23308 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309{
Bram Moolenaar31859182007-08-14 20:41:13 +000023310 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23311 msg_start();
23312 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313 if (name != NULL) /* "a:" vars don't have a name stored */
23314 msg_puts(name);
23315 msg_putchar(' ');
23316 msg_advance(22);
23317 if (type == VAR_NUMBER)
23318 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023319 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023320 msg_putchar('*');
23321 else if (type == VAR_LIST)
23322 {
23323 msg_putchar('[');
23324 if (*string == '[')
23325 ++string;
23326 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023327 else if (type == VAR_DICT)
23328 {
23329 msg_putchar('{');
23330 if (*string == '{')
23331 ++string;
23332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333 else
23334 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023335
Bram Moolenaar071d4272004-06-13 20:20:40 +000023336 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023337
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023338 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023339 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023340 if (*first)
23341 {
23342 msg_clr_eos();
23343 *first = FALSE;
23344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345}
23346
23347/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023348 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023349 * If the variable already exists, the value is updated.
23350 * Otherwise the variable is created.
23351 */
23352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023353set_var(
23354 char_u *name,
23355 typval_T *tv,
23356 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357{
Bram Moolenaar33570922005-01-25 22:26:29 +000023358 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023359 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023360 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023362 ht = find_var_ht(name, &varname);
23363 if (ht == NULL || *varname == NUL)
23364 {
23365 EMSG2(_(e_illvar), name);
23366 return;
23367 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023368 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023369
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023370 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23371 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023372 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023373
Bram Moolenaar33570922005-01-25 22:26:29 +000023374 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023375 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023376 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023377 if (var_check_ro(v->di_flags, name, FALSE)
23378 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023379 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023380
23381 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023382 * Handle setting internal v: variables separately where needed to
23383 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023384 */
23385 if (ht == &vimvarht)
23386 {
23387 if (v->di_tv.v_type == VAR_STRING)
23388 {
23389 vim_free(v->di_tv.vval.v_string);
23390 if (copy || tv->v_type != VAR_STRING)
23391 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23392 else
23393 {
23394 /* Take over the string to avoid an extra alloc/free. */
23395 v->di_tv.vval.v_string = tv->vval.v_string;
23396 tv->vval.v_string = NULL;
23397 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023398 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023399 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023400 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023401 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023402 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023403 if (STRCMP(varname, "searchforward") == 0)
23404 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023405#ifdef FEAT_SEARCH_EXTRA
23406 else if (STRCMP(varname, "hlsearch") == 0)
23407 {
23408 no_hlsearch = !v->di_tv.vval.v_number;
23409 redraw_all_later(SOME_VALID);
23410 }
23411#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023412 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023413 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023414 else if (v->di_tv.v_type != tv->v_type)
23415 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023416 }
23417
23418 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 }
23420 else /* add a new variable */
23421 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023422 /* Can't add "v:" variable. */
23423 if (ht == &vimvarht)
23424 {
23425 EMSG2(_(e_illvar), name);
23426 return;
23427 }
23428
Bram Moolenaar92124a32005-06-17 22:03:40 +000023429 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023430 if (!valid_varname(varname))
23431 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023432
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023433 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23434 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023435 if (v == NULL)
23436 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023437 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023438 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023440 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 return;
23442 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023443 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023445
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023446 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023447 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023448 else
23449 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023450 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023451 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023452 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454}
23455
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023456/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023457 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023458 * Also give an error message.
23459 */
23460 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023461var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023462{
23463 if (flags & DI_FLAGS_RO)
23464 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023465 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023466 return TRUE;
23467 }
23468 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23469 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023470 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023471 return TRUE;
23472 }
23473 return FALSE;
23474}
23475
23476/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023477 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23478 * Also give an error message.
23479 */
23480 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023481var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023482{
23483 if (flags & DI_FLAGS_FIX)
23484 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023485 EMSG2(_("E795: Cannot delete variable %s"),
23486 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023487 return TRUE;
23488 }
23489 return FALSE;
23490}
23491
23492/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023493 * Check if a funcref is assigned to a valid variable name.
23494 * Return TRUE and give an error if not.
23495 */
23496 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023497var_check_func_name(
23498 char_u *name, /* points to start of variable name */
23499 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023500{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023501 /* Allow for w: b: s: and t:. */
23502 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023503 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23504 ? name[2] : name[0]))
23505 {
23506 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23507 name);
23508 return TRUE;
23509 }
23510 /* Don't allow hiding a function. When "v" is not NULL we might be
23511 * assigning another function to the same var, the type is checked
23512 * below. */
23513 if (new_var && function_exists(name))
23514 {
23515 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23516 name);
23517 return TRUE;
23518 }
23519 return FALSE;
23520}
23521
23522/*
23523 * Check if a variable name is valid.
23524 * Return FALSE and give an error if not.
23525 */
23526 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023527valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023528{
23529 char_u *p;
23530
23531 for (p = varname; *p != NUL; ++p)
23532 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23533 && *p != AUTOLOAD_CHAR)
23534 {
23535 EMSG2(_(e_illvar), varname);
23536 return FALSE;
23537 }
23538 return TRUE;
23539}
23540
23541/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023542 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023543 * Also give an error message, using "name" or _("name") when use_gettext is
23544 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023545 */
23546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023547tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023548{
23549 if (lock & VAR_LOCKED)
23550 {
23551 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023552 name == NULL ? (char_u *)_("Unknown")
23553 : use_gettext ? (char_u *)_(name)
23554 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023555 return TRUE;
23556 }
23557 if (lock & VAR_FIXED)
23558 {
23559 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023560 name == NULL ? (char_u *)_("Unknown")
23561 : use_gettext ? (char_u *)_(name)
23562 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023563 return TRUE;
23564 }
23565 return FALSE;
23566}
23567
23568/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023569 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023570 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023571 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023572 * It is OK for "from" and "to" to point to the same item. This is used to
23573 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023574 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023575 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023576copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023578 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023579 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023580 switch (from->v_type)
23581 {
23582 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023583 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023584 to->vval.v_number = from->vval.v_number;
23585 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023586 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023587#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023588 to->vval.v_float = from->vval.v_float;
23589 break;
23590#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023591 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023592#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023593 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023594 if (to->vval.v_job != NULL)
23595 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023596 break;
23597#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023598 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023599#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023600 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023601 if (to->vval.v_channel != NULL)
23602 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023603 break;
23604#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023605 case VAR_STRING:
23606 case VAR_FUNC:
23607 if (from->vval.v_string == NULL)
23608 to->vval.v_string = NULL;
23609 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023611 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023612 if (from->v_type == VAR_FUNC)
23613 func_ref(to->vval.v_string);
23614 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023615 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023616 case VAR_PARTIAL:
23617 if (from->vval.v_partial == NULL)
23618 to->vval.v_partial = NULL;
23619 else
23620 {
23621 to->vval.v_partial = from->vval.v_partial;
23622 ++to->vval.v_partial->pt_refcount;
23623 }
23624 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023625 case VAR_LIST:
23626 if (from->vval.v_list == NULL)
23627 to->vval.v_list = NULL;
23628 else
23629 {
23630 to->vval.v_list = from->vval.v_list;
23631 ++to->vval.v_list->lv_refcount;
23632 }
23633 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023634 case VAR_DICT:
23635 if (from->vval.v_dict == NULL)
23636 to->vval.v_dict = NULL;
23637 else
23638 {
23639 to->vval.v_dict = from->vval.v_dict;
23640 ++to->vval.v_dict->dv_refcount;
23641 }
23642 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023643 case VAR_UNKNOWN:
23644 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023645 break;
23646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647}
23648
23649/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023650 * Make a copy of an item.
23651 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023652 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23653 * reference to an already copied list/dict can be used.
23654 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023655 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023656 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023657item_copy(
23658 typval_T *from,
23659 typval_T *to,
23660 int deep,
23661 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023662{
23663 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023664 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023665
Bram Moolenaar33570922005-01-25 22:26:29 +000023666 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023667 {
23668 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023669 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023670 }
23671 ++recurse;
23672
23673 switch (from->v_type)
23674 {
23675 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023676 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023677 case VAR_STRING:
23678 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023679 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023680 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023681 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023682 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023683 copy_tv(from, to);
23684 break;
23685 case VAR_LIST:
23686 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023687 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023688 if (from->vval.v_list == NULL)
23689 to->vval.v_list = NULL;
23690 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23691 {
23692 /* use the copy made earlier */
23693 to->vval.v_list = from->vval.v_list->lv_copylist;
23694 ++to->vval.v_list->lv_refcount;
23695 }
23696 else
23697 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23698 if (to->vval.v_list == NULL)
23699 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023700 break;
23701 case VAR_DICT:
23702 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023703 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023704 if (from->vval.v_dict == NULL)
23705 to->vval.v_dict = NULL;
23706 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23707 {
23708 /* use the copy made earlier */
23709 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23710 ++to->vval.v_dict->dv_refcount;
23711 }
23712 else
23713 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23714 if (to->vval.v_dict == NULL)
23715 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023716 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023717 case VAR_UNKNOWN:
23718 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023719 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023720 }
23721 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023722 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023723}
23724
23725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726 * ":echo expr1 ..." print each argument separated with a space, add a
23727 * newline at the end.
23728 * ":echon expr1 ..." print each argument plain.
23729 */
23730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023731ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732{
23733 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023734 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023735 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 char_u *p;
23737 int needclr = TRUE;
23738 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023739 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740
23741 if (eap->skip)
23742 ++emsg_skip;
23743 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23744 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023745 /* If eval1() causes an error message the text from the command may
23746 * still need to be cleared. E.g., "echo 22,44". */
23747 need_clr_eos = needclr;
23748
Bram Moolenaar071d4272004-06-13 20:20:40 +000023749 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023750 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 {
23752 /*
23753 * Report the invalid expression unless the expression evaluation
23754 * has been cancelled due to an aborting error, an interrupt, or an
23755 * exception.
23756 */
23757 if (!aborting())
23758 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023759 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760 break;
23761 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023762 need_clr_eos = FALSE;
23763
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764 if (!eap->skip)
23765 {
23766 if (atstart)
23767 {
23768 atstart = FALSE;
23769 /* Call msg_start() after eval1(), evaluating the expression
23770 * may cause a message to appear. */
23771 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023772 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023773 /* Mark the saved text as finishing the line, so that what
23774 * follows is displayed on a new line when scrolling back
23775 * at the more prompt. */
23776 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 }
23780 else if (eap->cmdidx == CMD_echo)
23781 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023782 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023783 if (p != NULL)
23784 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023786 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023787 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023788 if (*p != TAB && needclr)
23789 {
23790 /* remove any text still there from the command */
23791 msg_clr_eos();
23792 needclr = FALSE;
23793 }
23794 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 }
23796 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023797 {
23798#ifdef FEAT_MBYTE
23799 if (has_mbyte)
23800 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023801 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023802
23803 (void)msg_outtrans_len_attr(p, i, echo_attr);
23804 p += i - 1;
23805 }
23806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023808 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023811 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023813 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 arg = skipwhite(arg);
23815 }
23816 eap->nextcmd = check_nextcmd(arg);
23817
23818 if (eap->skip)
23819 --emsg_skip;
23820 else
23821 {
23822 /* remove text that may still be there from the command */
23823 if (needclr)
23824 msg_clr_eos();
23825 if (eap->cmdidx == CMD_echo)
23826 msg_end();
23827 }
23828}
23829
23830/*
23831 * ":echohl {name}".
23832 */
23833 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023834ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835{
23836 int id;
23837
23838 id = syn_name2id(eap->arg);
23839 if (id == 0)
23840 echo_attr = 0;
23841 else
23842 echo_attr = syn_id2attr(id);
23843}
23844
23845/*
23846 * ":execute expr1 ..." execute the result of an expression.
23847 * ":echomsg expr1 ..." Print a message
23848 * ":echoerr expr1 ..." Print an error
23849 * Each gets spaces around each argument and a newline at the end for
23850 * echo commands
23851 */
23852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023853ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854{
23855 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023856 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 int ret = OK;
23858 char_u *p;
23859 garray_T ga;
23860 int len;
23861 int save_did_emsg;
23862
23863 ga_init2(&ga, 1, 80);
23864
23865 if (eap->skip)
23866 ++emsg_skip;
23867 while (*arg != NUL && *arg != '|' && *arg != '\n')
23868 {
23869 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023870 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871 {
23872 /*
23873 * Report the invalid expression unless the expression evaluation
23874 * has been cancelled due to an aborting error, an interrupt, or an
23875 * exception.
23876 */
23877 if (!aborting())
23878 EMSG2(_(e_invexpr2), p);
23879 ret = FAIL;
23880 break;
23881 }
23882
23883 if (!eap->skip)
23884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023885 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 len = (int)STRLEN(p);
23887 if (ga_grow(&ga, len + 2) == FAIL)
23888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023889 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 ret = FAIL;
23891 break;
23892 }
23893 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023895 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 ga.ga_len += len;
23897 }
23898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023899 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 arg = skipwhite(arg);
23901 }
23902
23903 if (ret != FAIL && ga.ga_data != NULL)
23904 {
23905 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023908 out_flush();
23909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 else if (eap->cmdidx == CMD_echoerr)
23911 {
23912 /* We don't want to abort following commands, restore did_emsg. */
23913 save_did_emsg = did_emsg;
23914 EMSG((char_u *)ga.ga_data);
23915 if (!force_abort)
23916 did_emsg = save_did_emsg;
23917 }
23918 else if (eap->cmdidx == CMD_execute)
23919 do_cmdline((char_u *)ga.ga_data,
23920 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23921 }
23922
23923 ga_clear(&ga);
23924
23925 if (eap->skip)
23926 --emsg_skip;
23927
23928 eap->nextcmd = check_nextcmd(arg);
23929}
23930
23931/*
23932 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23933 * "arg" points to the "&" or '+' when called, to "option" when returning.
23934 * Returns NULL when no option name found. Otherwise pointer to the char
23935 * after the option name.
23936 */
23937 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023938find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939{
23940 char_u *p = *arg;
23941
23942 ++p;
23943 if (*p == 'g' && p[1] == ':')
23944 {
23945 *opt_flags = OPT_GLOBAL;
23946 p += 2;
23947 }
23948 else if (*p == 'l' && p[1] == ':')
23949 {
23950 *opt_flags = OPT_LOCAL;
23951 p += 2;
23952 }
23953 else
23954 *opt_flags = 0;
23955
23956 if (!ASCII_ISALPHA(*p))
23957 return NULL;
23958 *arg = p;
23959
23960 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23961 p += 4; /* termcap option */
23962 else
23963 while (ASCII_ISALPHA(*p))
23964 ++p;
23965 return p;
23966}
23967
23968/*
23969 * ":function"
23970 */
23971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023972ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973{
23974 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023975 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 int j;
23977 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023979 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980 char_u *name = NULL;
23981 char_u *p;
23982 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023983 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 garray_T newargs;
23985 garray_T newlines;
23986 int varargs = FALSE;
23987 int mustend = FALSE;
23988 int flags = 0;
23989 ufunc_T *fp;
23990 int indent;
23991 int nesting;
23992 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023993 dictitem_T *v;
23994 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023995 static int func_nr = 0; /* number for nameless function */
23996 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023997 hashtab_T *ht;
23998 int todo;
23999 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024000 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001
24002 /*
24003 * ":function" without argument: list functions.
24004 */
24005 if (ends_excmd(*eap->arg))
24006 {
24007 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024008 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024009 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024010 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024011 {
24012 if (!HASHITEM_EMPTY(hi))
24013 {
24014 --todo;
24015 fp = HI2UF(hi);
24016 if (!isdigit(*fp->uf_name))
24017 list_func_head(fp, FALSE);
24018 }
24019 }
24020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024021 eap->nextcmd = check_nextcmd(eap->arg);
24022 return;
24023 }
24024
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024025 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024026 * ":function /pat": list functions matching pattern.
24027 */
24028 if (*eap->arg == '/')
24029 {
24030 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24031 if (!eap->skip)
24032 {
24033 regmatch_T regmatch;
24034
24035 c = *p;
24036 *p = NUL;
24037 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24038 *p = c;
24039 if (regmatch.regprog != NULL)
24040 {
24041 regmatch.rm_ic = p_ic;
24042
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024043 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024044 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24045 {
24046 if (!HASHITEM_EMPTY(hi))
24047 {
24048 --todo;
24049 fp = HI2UF(hi);
24050 if (!isdigit(*fp->uf_name)
24051 && vim_regexec(&regmatch, fp->uf_name, 0))
24052 list_func_head(fp, FALSE);
24053 }
24054 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024055 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024056 }
24057 }
24058 if (*p == '/')
24059 ++p;
24060 eap->nextcmd = check_nextcmd(p);
24061 return;
24062 }
24063
24064 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024065 * Get the function name. There are these situations:
24066 * func normal function name
24067 * "name" == func, "fudi.fd_dict" == NULL
24068 * dict.func new dictionary entry
24069 * "name" == NULL, "fudi.fd_dict" set,
24070 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24071 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024072 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024073 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24074 * dict.func existing dict entry that's not a Funcref
24075 * "name" == NULL, "fudi.fd_dict" set,
24076 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024077 * s:func script-local function name
24078 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024080 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024081 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024082 paren = (vim_strchr(p, '(') != NULL);
24083 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084 {
24085 /*
24086 * Return on an invalid expression in braces, unless the expression
24087 * evaluation has been cancelled due to an aborting error, an
24088 * interrupt, or an exception.
24089 */
24090 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024092 if (!eap->skip && fudi.fd_newkey != NULL)
24093 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024094 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 else
24098 eap->skip = TRUE;
24099 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024100
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 /* An error in a function call during evaluation of an expression in magic
24102 * braces should not cause the function not to be defined. */
24103 saved_did_emsg = did_emsg;
24104 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024105
24106 /*
24107 * ":function func" with only function name: list function.
24108 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024109 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110 {
24111 if (!ends_excmd(*skipwhite(p)))
24112 {
24113 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024114 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 }
24116 eap->nextcmd = check_nextcmd(p);
24117 if (eap->nextcmd != NULL)
24118 *p = NUL;
24119 if (!eap->skip && !got_int)
24120 {
24121 fp = find_func(name);
24122 if (fp != NULL)
24123 {
24124 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024125 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024127 if (FUNCLINE(fp, j) == NULL)
24128 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129 msg_putchar('\n');
24130 msg_outnum((long)(j + 1));
24131 if (j < 9)
24132 msg_putchar(' ');
24133 if (j < 99)
24134 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024135 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136 out_flush(); /* show a line at a time */
24137 ui_breakcheck();
24138 }
24139 if (!got_int)
24140 {
24141 msg_putchar('\n');
24142 msg_puts((char_u *)" endfunction");
24143 }
24144 }
24145 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024146 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024148 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 }
24150
24151 /*
24152 * ":function name(arg1, arg2)" Define function.
24153 */
24154 p = skipwhite(p);
24155 if (*p != '(')
24156 {
24157 if (!eap->skip)
24158 {
24159 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024160 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024161 }
24162 /* attempt to continue by skipping some text */
24163 if (vim_strchr(p, '(') != NULL)
24164 p = vim_strchr(p, '(');
24165 }
24166 p = skipwhite(p + 1);
24167
24168 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24169 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24170
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024171 if (!eap->skip)
24172 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024173 /* Check the name of the function. Unless it's a dictionary function
24174 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024175 if (name != NULL)
24176 arg = name;
24177 else
24178 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024179 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024180 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24181 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024182 {
24183 if (*arg == K_SPECIAL)
24184 j = 3;
24185 else
24186 j = 0;
24187 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24188 : eval_isnamec(arg[j])))
24189 ++j;
24190 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024191 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024192 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024193 /* Disallow using the g: dict. */
24194 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24195 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024196 }
24197
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 /*
24199 * Isolate the arguments: "arg1, arg2, ...)"
24200 */
24201 while (*p != ')')
24202 {
24203 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24204 {
24205 varargs = TRUE;
24206 p += 3;
24207 mustend = TRUE;
24208 }
24209 else
24210 {
24211 arg = p;
24212 while (ASCII_ISALNUM(*p) || *p == '_')
24213 ++p;
24214 if (arg == p || isdigit(*arg)
24215 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24216 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24217 {
24218 if (!eap->skip)
24219 EMSG2(_("E125: Illegal argument: %s"), arg);
24220 break;
24221 }
24222 if (ga_grow(&newargs, 1) == FAIL)
24223 goto erret;
24224 c = *p;
24225 *p = NUL;
24226 arg = vim_strsave(arg);
24227 if (arg == NULL)
24228 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024229
24230 /* Check for duplicate argument name. */
24231 for (i = 0; i < newargs.ga_len; ++i)
24232 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24233 {
24234 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024235 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024236 goto erret;
24237 }
24238
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24240 *p = c;
24241 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242 if (*p == ',')
24243 ++p;
24244 else
24245 mustend = TRUE;
24246 }
24247 p = skipwhite(p);
24248 if (mustend && *p != ')')
24249 {
24250 if (!eap->skip)
24251 EMSG2(_(e_invarg2), eap->arg);
24252 break;
24253 }
24254 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024255 if (*p != ')')
24256 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257 ++p; /* skip the ')' */
24258
Bram Moolenaare9a41262005-01-15 22:18:47 +000024259 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260 for (;;)
24261 {
24262 p = skipwhite(p);
24263 if (STRNCMP(p, "range", 5) == 0)
24264 {
24265 flags |= FC_RANGE;
24266 p += 5;
24267 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024268 else if (STRNCMP(p, "dict", 4) == 0)
24269 {
24270 flags |= FC_DICT;
24271 p += 4;
24272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273 else if (STRNCMP(p, "abort", 5) == 0)
24274 {
24275 flags |= FC_ABORT;
24276 p += 5;
24277 }
24278 else
24279 break;
24280 }
24281
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024282 /* When there is a line break use what follows for the function body.
24283 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24284 if (*p == '\n')
24285 line_arg = p + 1;
24286 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287 EMSG(_(e_trailing));
24288
24289 /*
24290 * Read the body of the function, until ":endfunction" is found.
24291 */
24292 if (KeyTyped)
24293 {
24294 /* Check if the function already exists, don't let the user type the
24295 * whole function before telling him it doesn't work! For a script we
24296 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024297 if (!eap->skip && !eap->forceit)
24298 {
24299 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24300 EMSG(_(e_funcdict));
24301 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024302 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024305 if (!eap->skip && did_emsg)
24306 goto erret;
24307
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 msg_putchar('\n'); /* don't overwrite the function name */
24309 cmdline_row = msg_row;
24310 }
24311
24312 indent = 2;
24313 nesting = 0;
24314 for (;;)
24315 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024316 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024317 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024318 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024319 saved_wait_return = FALSE;
24320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024322 sourcing_lnum_off = sourcing_lnum;
24323
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024324 if (line_arg != NULL)
24325 {
24326 /* Use eap->arg, split up in parts by line breaks. */
24327 theline = line_arg;
24328 p = vim_strchr(theline, '\n');
24329 if (p == NULL)
24330 line_arg += STRLEN(line_arg);
24331 else
24332 {
24333 *p = NUL;
24334 line_arg = p + 1;
24335 }
24336 }
24337 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338 theline = getcmdline(':', 0L, indent);
24339 else
24340 theline = eap->getline(':', eap->cookie, indent);
24341 if (KeyTyped)
24342 lines_left = Rows - 1;
24343 if (theline == NULL)
24344 {
24345 EMSG(_("E126: Missing :endfunction"));
24346 goto erret;
24347 }
24348
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024349 /* Detect line continuation: sourcing_lnum increased more than one. */
24350 if (sourcing_lnum > sourcing_lnum_off + 1)
24351 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24352 else
24353 sourcing_lnum_off = 0;
24354
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355 if (skip_until != NULL)
24356 {
24357 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24358 * don't check for ":endfunc". */
24359 if (STRCMP(theline, skip_until) == 0)
24360 {
24361 vim_free(skip_until);
24362 skip_until = NULL;
24363 }
24364 }
24365 else
24366 {
24367 /* skip ':' and blanks*/
24368 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24369 ;
24370
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024371 /* Check for "endfunction". */
24372 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024373 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024374 if (line_arg == NULL)
24375 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376 break;
24377 }
24378
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024379 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 * at "end". */
24381 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24382 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024383 else if (STRNCMP(p, "if", 2) == 0
24384 || STRNCMP(p, "wh", 2) == 0
24385 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 || STRNCMP(p, "try", 3) == 0)
24387 indent += 2;
24388
24389 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024390 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024391 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024392 if (*p == '!')
24393 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024395 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024396 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024398 ++nesting;
24399 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400 }
24401 }
24402
24403 /* Check for ":append" or ":insert". */
24404 p = skip_range(p, NULL);
24405 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24406 || (p[0] == 'i'
24407 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24408 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24409 skip_until = vim_strsave((char_u *)".");
24410
24411 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24412 arg = skipwhite(skiptowhite(p));
24413 if (arg[0] == '<' && arg[1] =='<'
24414 && ((p[0] == 'p' && p[1] == 'y'
24415 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24416 || (p[0] == 'p' && p[1] == 'e'
24417 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24418 || (p[0] == 't' && p[1] == 'c'
24419 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024420 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24421 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024422 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24423 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024424 || (p[0] == 'm' && p[1] == 'z'
24425 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024426 ))
24427 {
24428 /* ":python <<" continues until a dot, like ":append" */
24429 p = skipwhite(arg + 2);
24430 if (*p == NUL)
24431 skip_until = vim_strsave((char_u *)".");
24432 else
24433 skip_until = vim_strsave(p);
24434 }
24435 }
24436
24437 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024438 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024439 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024440 if (line_arg == NULL)
24441 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024443 }
24444
24445 /* Copy the line to newly allocated memory. get_one_sourceline()
24446 * allocates 250 bytes per line, this saves 80% on average. The cost
24447 * is an extra alloc/free. */
24448 p = vim_strsave(theline);
24449 if (p != NULL)
24450 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024451 if (line_arg == NULL)
24452 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024453 theline = p;
24454 }
24455
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024456 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24457
24458 /* Add NULL lines for continuation lines, so that the line count is
24459 * equal to the index in the growarray. */
24460 while (sourcing_lnum_off-- > 0)
24461 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024462
24463 /* Check for end of eap->arg. */
24464 if (line_arg != NULL && *line_arg == NUL)
24465 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 }
24467
24468 /* Don't define the function when skipping commands or when an error was
24469 * detected. */
24470 if (eap->skip || did_emsg)
24471 goto erret;
24472
24473 /*
24474 * If there are no errors, add the function
24475 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024476 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024477 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024478 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024479 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024480 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024481 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024482 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024483 goto erret;
24484 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024485
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024486 fp = find_func(name);
24487 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024488 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024489 if (!eap->forceit)
24490 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024491 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024492 goto erret;
24493 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024494 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024495 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024496 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024497 name);
24498 goto erret;
24499 }
24500 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024501 ga_clear_strings(&(fp->uf_args));
24502 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024503 vim_free(name);
24504 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506 }
24507 else
24508 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024509 char numbuf[20];
24510
24511 fp = NULL;
24512 if (fudi.fd_newkey == NULL && !eap->forceit)
24513 {
24514 EMSG(_(e_funcdict));
24515 goto erret;
24516 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024517 if (fudi.fd_di == NULL)
24518 {
24519 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024520 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024521 goto erret;
24522 }
24523 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024524 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024525 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024526
24527 /* Give the function a sequential number. Can only be used with a
24528 * Funcref! */
24529 vim_free(name);
24530 sprintf(numbuf, "%d", ++func_nr);
24531 name = vim_strsave((char_u *)numbuf);
24532 if (name == NULL)
24533 goto erret;
24534 }
24535
24536 if (fp == NULL)
24537 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024538 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024539 {
24540 int slen, plen;
24541 char_u *scriptname;
24542
24543 /* Check that the autoload name matches the script name. */
24544 j = FAIL;
24545 if (sourcing_name != NULL)
24546 {
24547 scriptname = autoload_name(name);
24548 if (scriptname != NULL)
24549 {
24550 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024551 plen = (int)STRLEN(p);
24552 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024553 if (slen > plen && fnamecmp(p,
24554 sourcing_name + slen - plen) == 0)
24555 j = OK;
24556 vim_free(scriptname);
24557 }
24558 }
24559 if (j == FAIL)
24560 {
24561 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24562 goto erret;
24563 }
24564 }
24565
24566 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567 if (fp == NULL)
24568 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024569
24570 if (fudi.fd_dict != NULL)
24571 {
24572 if (fudi.fd_di == NULL)
24573 {
24574 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024575 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024576 if (fudi.fd_di == NULL)
24577 {
24578 vim_free(fp);
24579 goto erret;
24580 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024581 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24582 {
24583 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024584 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024585 goto erret;
24586 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024587 }
24588 else
24589 /* overwrite existing dict entry */
24590 clear_tv(&fudi.fd_di->di_tv);
24591 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024592 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024593 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024594 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024595
24596 /* behave like "dict" was used */
24597 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024598 }
24599
Bram Moolenaar071d4272004-06-13 20:20:40 +000024600 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024601 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024602 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24603 {
24604 vim_free(fp);
24605 goto erret;
24606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024608 fp->uf_args = newargs;
24609 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024610#ifdef FEAT_PROFILE
24611 fp->uf_tml_count = NULL;
24612 fp->uf_tml_total = NULL;
24613 fp->uf_tml_self = NULL;
24614 fp->uf_profiling = FALSE;
24615 if (prof_def_func())
24616 func_do_profile(fp);
24617#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024618 fp->uf_varargs = varargs;
24619 fp->uf_flags = flags;
24620 fp->uf_calls = 0;
24621 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024622 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024623
24624erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625 ga_clear_strings(&newargs);
24626 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024627ret_free:
24628 vim_free(skip_until);
24629 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024632 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633}
24634
24635/*
24636 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024637 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024639 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024640 * TFN_INT: internal function name OK
24641 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024642 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 * Advances "pp" to just after the function name (if no error).
24644 */
24645 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024646trans_function_name(
24647 char_u **pp,
24648 int skip, /* only find the end, don't evaluate */
24649 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024650 funcdict_T *fdp, /* return: info about dictionary used */
24651 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024653 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024654 char_u *start;
24655 char_u *end;
24656 int lead;
24657 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024659 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024660
24661 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024662 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024664
24665 /* Check for hard coded <SNR>: already translated function ID (from a user
24666 * command). */
24667 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24668 && (*pp)[2] == (int)KE_SNR)
24669 {
24670 *pp += 3;
24671 len = get_id_len(pp) + 3;
24672 return vim_strnsave(start, len);
24673 }
24674
24675 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24676 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024678 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024680
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024681 /* Note that TFN_ flags use the same values as GLV_ flags. */
24682 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024683 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024684 if (end == start)
24685 {
24686 if (!skip)
24687 EMSG(_("E129: Function name required"));
24688 goto theend;
24689 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024690 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024691 {
24692 /*
24693 * Report an invalid expression in braces, unless the expression
24694 * evaluation has been cancelled due to an aborting error, an
24695 * interrupt, or an exception.
24696 */
24697 if (!aborting())
24698 {
24699 if (end != NULL)
24700 EMSG2(_(e_invarg2), start);
24701 }
24702 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024703 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024704 goto theend;
24705 }
24706
24707 if (lv.ll_tv != NULL)
24708 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024709 if (fdp != NULL)
24710 {
24711 fdp->fd_dict = lv.ll_dict;
24712 fdp->fd_newkey = lv.ll_newkey;
24713 lv.ll_newkey = NULL;
24714 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024715 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024716 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24717 {
24718 name = vim_strsave(lv.ll_tv->vval.v_string);
24719 *pp = end;
24720 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024721 else if (lv.ll_tv->v_type == VAR_PARTIAL
24722 && lv.ll_tv->vval.v_partial != NULL)
24723 {
24724 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24725 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024726 if (partial != NULL)
24727 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024728 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024729 else
24730 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024731 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24732 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024733 EMSG(_(e_funcref));
24734 else
24735 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024736 name = NULL;
24737 }
24738 goto theend;
24739 }
24740
24741 if (lv.ll_name == NULL)
24742 {
24743 /* Error found, but continue after the function name. */
24744 *pp = end;
24745 goto theend;
24746 }
24747
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024748 /* Check if the name is a Funcref. If so, use the value. */
24749 if (lv.ll_exp_name != NULL)
24750 {
24751 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024752 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024753 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024754 if (name == lv.ll_exp_name)
24755 name = NULL;
24756 }
24757 else
24758 {
24759 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024760 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024761 if (name == *pp)
24762 name = NULL;
24763 }
24764 if (name != NULL)
24765 {
24766 name = vim_strsave(name);
24767 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024768 if (STRNCMP(name, "<SNR>", 5) == 0)
24769 {
24770 /* Change "<SNR>" to the byte sequence. */
24771 name[0] = K_SPECIAL;
24772 name[1] = KS_EXTRA;
24773 name[2] = (int)KE_SNR;
24774 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24775 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024776 goto theend;
24777 }
24778
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024779 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024780 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024781 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024782 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24783 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24784 {
24785 /* When there was "s:" already or the name expanded to get a
24786 * leading "s:" then remove it. */
24787 lv.ll_name += 2;
24788 len -= 2;
24789 lead = 2;
24790 }
24791 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024792 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024793 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024794 /* skip over "s:" and "g:" */
24795 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024796 lv.ll_name += 2;
24797 len = (int)(end - lv.ll_name);
24798 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024799
24800 /*
24801 * Copy the function name to allocated memory.
24802 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24803 * Accept <SNR>123_name() outside a script.
24804 */
24805 if (skip)
24806 lead = 0; /* do nothing */
24807 else if (lead > 0)
24808 {
24809 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024810 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24811 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024812 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024813 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024814 if (current_SID <= 0)
24815 {
24816 EMSG(_(e_usingsid));
24817 goto theend;
24818 }
24819 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24820 lead += (int)STRLEN(sid_buf);
24821 }
24822 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024823 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024824 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024825 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024826 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024827 goto theend;
24828 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024829 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024830 {
24831 char_u *cp = vim_strchr(lv.ll_name, ':');
24832
24833 if (cp != NULL && cp < end)
24834 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024835 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024836 goto theend;
24837 }
24838 }
24839
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024840 name = alloc((unsigned)(len + lead + 1));
24841 if (name != NULL)
24842 {
24843 if (lead > 0)
24844 {
24845 name[0] = K_SPECIAL;
24846 name[1] = KS_EXTRA;
24847 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024848 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024849 STRCPY(name + 3, sid_buf);
24850 }
24851 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024852 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024853 }
24854 *pp = end;
24855
24856theend:
24857 clear_lval(&lv);
24858 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024859}
24860
24861/*
24862 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24863 * Return 2 if "p" starts with "s:".
24864 * Return 0 otherwise.
24865 */
24866 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024867eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024868{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024869 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24870 * the standard library function. */
24871 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24872 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024873 return 5;
24874 if (p[0] == 's' && p[1] == ':')
24875 return 2;
24876 return 0;
24877}
24878
24879/*
24880 * Return TRUE if "p" starts with "<SID>" or "s:".
24881 * Only works if eval_fname_script() returned non-zero for "p"!
24882 */
24883 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024884eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024885{
24886 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24887}
24888
24889/*
24890 * List the head of the function: "name(arg1, arg2)".
24891 */
24892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024893list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894{
24895 int j;
24896
24897 msg_start();
24898 if (indent)
24899 MSG_PUTS(" ");
24900 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024901 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024902 {
24903 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024904 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024905 }
24906 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024907 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024909 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910 {
24911 if (j)
24912 MSG_PUTS(", ");
24913 msg_puts(FUNCARG(fp, j));
24914 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024915 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916 {
24917 if (j)
24918 MSG_PUTS(", ");
24919 MSG_PUTS("...");
24920 }
24921 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024922 if (fp->uf_flags & FC_ABORT)
24923 MSG_PUTS(" abort");
24924 if (fp->uf_flags & FC_RANGE)
24925 MSG_PUTS(" range");
24926 if (fp->uf_flags & FC_DICT)
24927 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024928 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024929 if (p_verbose > 0)
24930 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931}
24932
24933/*
24934 * Find a function by name, return pointer to it in ufuncs.
24935 * Return NULL for unknown function.
24936 */
24937 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024938find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024940 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024942 hi = hash_find(&func_hashtab, name);
24943 if (!HASHITEM_EMPTY(hi))
24944 return HI2UF(hi);
24945 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946}
24947
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024948#if defined(EXITFREE) || defined(PROTO)
24949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024950free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024951{
24952 hashitem_T *hi;
24953
24954 /* Need to start all over every time, because func_free() may change the
24955 * hash table. */
24956 while (func_hashtab.ht_used > 0)
24957 for (hi = func_hashtab.ht_array; ; ++hi)
24958 if (!HASHITEM_EMPTY(hi))
24959 {
24960 func_free(HI2UF(hi));
24961 break;
24962 }
24963}
24964#endif
24965
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024967translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024968{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024969 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024970 return find_internal_func(name) >= 0;
24971 return find_func(name) != NULL;
24972}
24973
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024974/*
24975 * Return TRUE if a function "name" exists.
24976 */
24977 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024978function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024979{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024980 char_u *nm = name;
24981 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024982 int n = FALSE;
24983
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024984 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024985 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024986 nm = skipwhite(nm);
24987
24988 /* Only accept "funcname", "funcname ", "funcname (..." and
24989 * "funcname(...", not "funcname!...". */
24990 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024991 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024992 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024993 return n;
24994}
24995
Bram Moolenaara1544c02013-05-30 12:35:52 +020024996 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024997get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024998{
24999 char_u *nm = name;
25000 char_u *p;
25001
Bram Moolenaar65639032016-03-16 21:40:30 +010025002 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025003
25004 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025005 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025006 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025007
Bram Moolenaara1544c02013-05-30 12:35:52 +020025008 vim_free(p);
25009 return NULL;
25010}
25011
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025012/*
25013 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025014 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25015 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025016 */
25017 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025018builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025019{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025020 char_u *p;
25021
25022 if (!ASCII_ISLOWER(name[0]))
25023 return FALSE;
25024 p = vim_strchr(name, AUTOLOAD_CHAR);
25025 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025026}
25027
Bram Moolenaar05159a02005-02-26 23:04:13 +000025028#if defined(FEAT_PROFILE) || defined(PROTO)
25029/*
25030 * Start profiling function "fp".
25031 */
25032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025033func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025034{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025035 int len = fp->uf_lines.ga_len;
25036
25037 if (len == 0)
25038 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025039 fp->uf_tm_count = 0;
25040 profile_zero(&fp->uf_tm_self);
25041 profile_zero(&fp->uf_tm_total);
25042 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025043 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025044 if (fp->uf_tml_total == NULL)
25045 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025046 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025047 if (fp->uf_tml_self == NULL)
25048 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025049 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025050 fp->uf_tml_idx = -1;
25051 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25052 || fp->uf_tml_self == NULL)
25053 return; /* out of memory */
25054
25055 fp->uf_profiling = TRUE;
25056}
25057
25058/*
25059 * Dump the profiling results for all functions in file "fd".
25060 */
25061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025062func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025063{
25064 hashitem_T *hi;
25065 int todo;
25066 ufunc_T *fp;
25067 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025068 ufunc_T **sorttab;
25069 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025070
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025071 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025072 if (todo == 0)
25073 return; /* nothing to dump */
25074
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025075 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025076
Bram Moolenaar05159a02005-02-26 23:04:13 +000025077 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25078 {
25079 if (!HASHITEM_EMPTY(hi))
25080 {
25081 --todo;
25082 fp = HI2UF(hi);
25083 if (fp->uf_profiling)
25084 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025085 if (sorttab != NULL)
25086 sorttab[st_len++] = fp;
25087
Bram Moolenaar05159a02005-02-26 23:04:13 +000025088 if (fp->uf_name[0] == K_SPECIAL)
25089 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25090 else
25091 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25092 if (fp->uf_tm_count == 1)
25093 fprintf(fd, "Called 1 time\n");
25094 else
25095 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25096 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25097 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25098 fprintf(fd, "\n");
25099 fprintf(fd, "count total (s) self (s)\n");
25100
25101 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25102 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025103 if (FUNCLINE(fp, i) == NULL)
25104 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025105 prof_func_line(fd, fp->uf_tml_count[i],
25106 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025107 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25108 }
25109 fprintf(fd, "\n");
25110 }
25111 }
25112 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025113
25114 if (sorttab != NULL && st_len > 0)
25115 {
25116 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25117 prof_total_cmp);
25118 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25119 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25120 prof_self_cmp);
25121 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25122 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025123
25124 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025125}
Bram Moolenaar73830342005-02-28 22:48:19 +000025126
25127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025128prof_sort_list(
25129 FILE *fd,
25130 ufunc_T **sorttab,
25131 int st_len,
25132 char *title,
25133 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025134{
25135 int i;
25136 ufunc_T *fp;
25137
25138 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25139 fprintf(fd, "count total (s) self (s) function\n");
25140 for (i = 0; i < 20 && i < st_len; ++i)
25141 {
25142 fp = sorttab[i];
25143 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25144 prefer_self);
25145 if (fp->uf_name[0] == K_SPECIAL)
25146 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25147 else
25148 fprintf(fd, " %s()\n", fp->uf_name);
25149 }
25150 fprintf(fd, "\n");
25151}
25152
25153/*
25154 * Print the count and times for one function or function line.
25155 */
25156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025157prof_func_line(
25158 FILE *fd,
25159 int count,
25160 proftime_T *total,
25161 proftime_T *self,
25162 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025163{
25164 if (count > 0)
25165 {
25166 fprintf(fd, "%5d ", count);
25167 if (prefer_self && profile_equal(total, self))
25168 fprintf(fd, " ");
25169 else
25170 fprintf(fd, "%s ", profile_msg(total));
25171 if (!prefer_self && profile_equal(total, self))
25172 fprintf(fd, " ");
25173 else
25174 fprintf(fd, "%s ", profile_msg(self));
25175 }
25176 else
25177 fprintf(fd, " ");
25178}
25179
25180/*
25181 * Compare function for total time sorting.
25182 */
25183 static int
25184#ifdef __BORLANDC__
25185_RTLENTRYF
25186#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025187prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025188{
25189 ufunc_T *p1, *p2;
25190
25191 p1 = *(ufunc_T **)s1;
25192 p2 = *(ufunc_T **)s2;
25193 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25194}
25195
25196/*
25197 * Compare function for self time sorting.
25198 */
25199 static int
25200#ifdef __BORLANDC__
25201_RTLENTRYF
25202#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025203prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025204{
25205 ufunc_T *p1, *p2;
25206
25207 p1 = *(ufunc_T **)s1;
25208 p2 = *(ufunc_T **)s2;
25209 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25210}
25211
Bram Moolenaar05159a02005-02-26 23:04:13 +000025212#endif
25213
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025214/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025215 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025216 * Return TRUE if a package was loaded.
25217 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025218 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025219script_autoload(
25220 char_u *name,
25221 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025222{
25223 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025224 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025225 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025226 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025227
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025228 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025229 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025230 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025231 return FALSE;
25232
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025233 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025234
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025235 /* Find the name in the list of previously loaded package names. Skip
25236 * "autoload/", it's always the same. */
25237 for (i = 0; i < ga_loaded.ga_len; ++i)
25238 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25239 break;
25240 if (!reload && i < ga_loaded.ga_len)
25241 ret = FALSE; /* was loaded already */
25242 else
25243 {
25244 /* Remember the name if it wasn't loaded already. */
25245 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25246 {
25247 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25248 tofree = NULL;
25249 }
25250
25251 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025252 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025253 ret = TRUE;
25254 }
25255
25256 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025257 return ret;
25258}
25259
25260/*
25261 * Return the autoload script name for a function or variable name.
25262 * Returns NULL when out of memory.
25263 */
25264 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025265autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025266{
25267 char_u *p;
25268 char_u *scriptname;
25269
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025270 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025271 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25272 if (scriptname == NULL)
25273 return FALSE;
25274 STRCPY(scriptname, "autoload/");
25275 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025276 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025277 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025278 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025279 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025280 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025281}
25282
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25284
25285/*
25286 * Function given to ExpandGeneric() to obtain the list of user defined
25287 * function names.
25288 */
25289 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025290get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025292 static long_u done;
25293 static hashitem_T *hi;
25294 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295
25296 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025297 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025298 done = 0;
25299 hi = func_hashtab.ht_array;
25300 }
25301 if (done < func_hashtab.ht_used)
25302 {
25303 if (done++ > 0)
25304 ++hi;
25305 while (HASHITEM_EMPTY(hi))
25306 ++hi;
25307 fp = HI2UF(hi);
25308
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025309 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025310 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025311
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025312 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25313 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314
25315 cat_func_name(IObuff, fp);
25316 if (xp->xp_context != EXPAND_USER_FUNC)
25317 {
25318 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025319 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 STRCAT(IObuff, ")");
25321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322 return IObuff;
25323 }
25324 return NULL;
25325}
25326
25327#endif /* FEAT_CMDL_COMPL */
25328
25329/*
25330 * Copy the function name of "fp" to buffer "buf".
25331 * "buf" must be able to hold the function name plus three bytes.
25332 * Takes care of script-local function names.
25333 */
25334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025335cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025337 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 {
25339 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025340 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025341 }
25342 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025343 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344}
25345
25346/*
25347 * ":delfunction {name}"
25348 */
25349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025350ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025352 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025353 char_u *p;
25354 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025355 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025356
25357 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025358 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025359 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025361 {
25362 if (fudi.fd_dict != NULL && !eap->skip)
25363 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366 if (!ends_excmd(*skipwhite(p)))
25367 {
25368 vim_free(name);
25369 EMSG(_(e_trailing));
25370 return;
25371 }
25372 eap->nextcmd = check_nextcmd(p);
25373 if (eap->nextcmd != NULL)
25374 *p = NUL;
25375
25376 if (!eap->skip)
25377 fp = find_func(name);
25378 vim_free(name);
25379
25380 if (!eap->skip)
25381 {
25382 if (fp == NULL)
25383 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025384 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385 return;
25386 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025387 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388 {
25389 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25390 return;
25391 }
25392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025393 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025395 /* Delete the dict item that refers to the function, it will
25396 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025397 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025399 else
25400 func_free(fp);
25401 }
25402}
25403
25404/*
25405 * Free a function and remove it from the list of functions.
25406 */
25407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025408func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025409{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025410 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025411
25412 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025413 ga_clear_strings(&(fp->uf_args));
25414 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025415#ifdef FEAT_PROFILE
25416 vim_free(fp->uf_tml_count);
25417 vim_free(fp->uf_tml_total);
25418 vim_free(fp->uf_tml_self);
25419#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025420
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025421 /* remove the function from the function hashtable */
25422 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25423 if (HASHITEM_EMPTY(hi))
25424 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025425 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025426 hash_remove(&func_hashtab, hi);
25427
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025428 vim_free(fp);
25429}
25430
25431/*
25432 * Unreference a Function: decrement the reference count and free it when it
25433 * becomes zero. Only for numbered functions.
25434 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025435 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025436func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025437{
25438 ufunc_T *fp;
25439
25440 if (name != NULL && isdigit(*name))
25441 {
25442 fp = find_func(name);
25443 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025444 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025445#ifdef EXITFREE
25446 if (!entered_free_all_mem)
25447#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025448 EMSG2(_(e_intern2), "func_unref()");
25449 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025450 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025451 {
25452 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025453 * when "uf_calls" becomes zero. */
25454 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025455 func_free(fp);
25456 }
25457 }
25458}
25459
25460/*
25461 * Count a reference to a Function.
25462 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025464func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025465{
25466 ufunc_T *fp;
25467
25468 if (name != NULL && isdigit(*name))
25469 {
25470 fp = find_func(name);
25471 if (fp == NULL)
25472 EMSG2(_(e_intern2), "func_ref()");
25473 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025474 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025475 }
25476}
25477
25478/*
25479 * Call a user function.
25480 */
25481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025482call_user_func(
25483 ufunc_T *fp, /* pointer to function */
25484 int argcount, /* nr of args */
25485 typval_T *argvars, /* arguments */
25486 typval_T *rettv, /* return value */
25487 linenr_T firstline, /* first line of range */
25488 linenr_T lastline, /* last line of range */
25489 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490{
Bram Moolenaar33570922005-01-25 22:26:29 +000025491 char_u *save_sourcing_name;
25492 linenr_T save_sourcing_lnum;
25493 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025494 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025495 int save_did_emsg;
25496 static int depth = 0;
25497 dictitem_T *v;
25498 int fixvar_idx = 0; /* index in fixvar[] */
25499 int i;
25500 int ai;
25501 char_u numbuf[NUMBUFLEN];
25502 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025503 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025504#ifdef FEAT_PROFILE
25505 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025506 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025508
25509 /* If depth of calling is getting too high, don't execute the function */
25510 if (depth >= p_mfd)
25511 {
25512 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025513 rettv->v_type = VAR_NUMBER;
25514 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515 return;
25516 }
25517 ++depth;
25518
25519 line_breakcheck(); /* check for CTRL-C hit */
25520
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025521 fc = (funccall_T *)alloc(sizeof(funccall_T));
25522 fc->caller = current_funccal;
25523 current_funccal = fc;
25524 fc->func = fp;
25525 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025526 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025527 fc->linenr = 0;
25528 fc->returned = FALSE;
25529 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025531 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25532 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533
Bram Moolenaar33570922005-01-25 22:26:29 +000025534 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025535 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025536 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25537 * each argument variable and saves a lot of time.
25538 */
25539 /*
25540 * Init l: variables.
25541 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025542 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025543 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025544 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025545 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25546 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025547 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025548 name = v->di_key;
25549 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025550 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025551 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025552 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025553 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025554 v->di_tv.vval.v_dict = selfdict;
25555 ++selfdict->dv_refcount;
25556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025557
Bram Moolenaar33570922005-01-25 22:26:29 +000025558 /*
25559 * Init a: variables.
25560 * Set a:0 to "argcount".
25561 * Set a:000 to a list with room for the "..." arguments.
25562 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025563 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025564 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025565 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025566 /* Use "name" to avoid a warning from some compiler that checks the
25567 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025568 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025569 name = v->di_key;
25570 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025571 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025572 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025573 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025574 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025575 v->di_tv.vval.v_list = &fc->l_varlist;
25576 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25577 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25578 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025579
25580 /*
25581 * Set a:firstline to "firstline" and a:lastline to "lastline".
25582 * Set a:name to named arguments.
25583 * Set a:N to the "..." arguments.
25584 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025585 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025586 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025587 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025588 (varnumber_T)lastline);
25589 for (i = 0; i < argcount; ++i)
25590 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025591 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025592 if (ai < 0)
25593 /* named argument a:name */
25594 name = FUNCARG(fp, i);
25595 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025596 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025597 /* "..." argument a:1, a:2, etc. */
25598 sprintf((char *)numbuf, "%d", ai + 1);
25599 name = numbuf;
25600 }
25601 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25602 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025603 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025604 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25605 }
25606 else
25607 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025608 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25609 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025610 if (v == NULL)
25611 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025612 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025613 }
25614 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025615 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025616
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025617 /* Note: the values are copied directly to avoid alloc/free.
25618 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025619 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025620 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025621
25622 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25623 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025624 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25625 fc->l_listitems[ai].li_tv = argvars[i];
25626 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025627 }
25628 }
25629
Bram Moolenaar071d4272004-06-13 20:20:40 +000025630 /* Don't redraw while executing the function. */
25631 ++RedrawingDisabled;
25632 save_sourcing_name = sourcing_name;
25633 save_sourcing_lnum = sourcing_lnum;
25634 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025635 /* need space for function name + ("function " + 3) or "[number]" */
25636 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25637 + STRLEN(fp->uf_name) + 20;
25638 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025639 if (sourcing_name != NULL)
25640 {
25641 if (save_sourcing_name != NULL
25642 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025643 sprintf((char *)sourcing_name, "%s[%d]..",
25644 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645 else
25646 STRCPY(sourcing_name, "function ");
25647 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25648
25649 if (p_verbose >= 12)
25650 {
25651 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025652 verbose_enter_scroll();
25653
Bram Moolenaar555b2802005-05-19 21:08:39 +000025654 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 if (p_verbose >= 14)
25656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025658 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025659 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025660 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661
25662 msg_puts((char_u *)"(");
25663 for (i = 0; i < argcount; ++i)
25664 {
25665 if (i > 0)
25666 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025667 if (argvars[i].v_type == VAR_NUMBER)
25668 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669 else
25670 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025671 /* Do not want errors such as E724 here. */
25672 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025673 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025674 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025675 if (s != NULL)
25676 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025677 if (vim_strsize(s) > MSG_BUF_CLEN)
25678 {
25679 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25680 s = buf;
25681 }
25682 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025683 vim_free(tofree);
25684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025685 }
25686 }
25687 msg_puts((char_u *)")");
25688 }
25689 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025690
25691 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692 --no_wait_return;
25693 }
25694 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025695#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025696 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025697 {
25698 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25699 func_do_profile(fp);
25700 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025701 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025702 {
25703 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025704 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025705 profile_zero(&fp->uf_tm_children);
25706 }
25707 script_prof_save(&wait_start);
25708 }
25709#endif
25710
Bram Moolenaar071d4272004-06-13 20:20:40 +000025711 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025712 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025713 save_did_emsg = did_emsg;
25714 did_emsg = FALSE;
25715
25716 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025717 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025718 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25719
25720 --RedrawingDisabled;
25721
25722 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025723 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025724 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025725 clear_tv(rettv);
25726 rettv->v_type = VAR_NUMBER;
25727 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728 }
25729
Bram Moolenaar05159a02005-02-26 23:04:13 +000025730#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025731 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025732 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025733 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025734 profile_end(&call_start);
25735 profile_sub_wait(&wait_start, &call_start);
25736 profile_add(&fp->uf_tm_total, &call_start);
25737 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025738 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025739 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025740 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25741 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025742 }
25743 }
25744#endif
25745
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746 /* when being verbose, mention the return value */
25747 if (p_verbose >= 12)
25748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025750 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751
Bram Moolenaar071d4272004-06-13 20:20:40 +000025752 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025753 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025754 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025755 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025756 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025759 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025760 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025761 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025762 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025763
Bram Moolenaar555b2802005-05-19 21:08:39 +000025764 /* The value may be very long. Skip the middle part, so that we
25765 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025766 * truncate it at the end. Don't want errors such as E724 here. */
25767 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025768 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025769 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025770 if (s != NULL)
25771 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025772 if (vim_strsize(s) > MSG_BUF_CLEN)
25773 {
25774 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25775 s = buf;
25776 }
25777 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025778 vim_free(tofree);
25779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780 }
25781 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025782
25783 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025784 --no_wait_return;
25785 }
25786
25787 vim_free(sourcing_name);
25788 sourcing_name = save_sourcing_name;
25789 sourcing_lnum = save_sourcing_lnum;
25790 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025791#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025792 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025793 script_prof_restore(&wait_start);
25794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795
25796 if (p_verbose >= 12 && sourcing_name != NULL)
25797 {
25798 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025799 verbose_enter_scroll();
25800
Bram Moolenaar555b2802005-05-19 21:08:39 +000025801 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025803
25804 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025805 --no_wait_return;
25806 }
25807
25808 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025809 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025810 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025811
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025812 /* If the a:000 list and the l: and a: dicts are not referenced we can
25813 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025814 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25815 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25816 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25817 {
25818 free_funccal(fc, FALSE);
25819 }
25820 else
25821 {
25822 hashitem_T *hi;
25823 listitem_T *li;
25824 int todo;
25825
25826 /* "fc" is still in use. This can happen when returning "a:000" or
25827 * assigning "l:" to a global variable.
25828 * Link "fc" in the list for garbage collection later. */
25829 fc->caller = previous_funccal;
25830 previous_funccal = fc;
25831
25832 /* Make a copy of the a: variables, since we didn't do that above. */
25833 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25834 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25835 {
25836 if (!HASHITEM_EMPTY(hi))
25837 {
25838 --todo;
25839 v = HI2DI(hi);
25840 copy_tv(&v->di_tv, &v->di_tv);
25841 }
25842 }
25843
25844 /* Make a copy of the a:000 items, since we didn't do that above. */
25845 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25846 copy_tv(&li->li_tv, &li->li_tv);
25847 }
25848}
25849
25850/*
25851 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025852 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025853 */
25854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025855can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025856{
25857 return (fc->l_varlist.lv_copyID != copyID
25858 && fc->l_vars.dv_copyID != copyID
25859 && fc->l_avars.dv_copyID != copyID);
25860}
25861
25862/*
25863 * Free "fc" and what it contains.
25864 */
25865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025866free_funccal(
25867 funccall_T *fc,
25868 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025869{
25870 listitem_T *li;
25871
25872 /* The a: variables typevals may not have been allocated, only free the
25873 * allocated variables. */
25874 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25875
25876 /* free all l: variables */
25877 vars_clear(&fc->l_vars.dv_hashtab);
25878
25879 /* Free the a:000 variables if they were allocated. */
25880 if (free_val)
25881 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25882 clear_tv(&li->li_tv);
25883
25884 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025885}
25886
25887/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025888 * Add a number variable "name" to dict "dp" with value "nr".
25889 */
25890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025891add_nr_var(
25892 dict_T *dp,
25893 dictitem_T *v,
25894 char *name,
25895 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025896{
25897 STRCPY(v->di_key, name);
25898 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25899 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25900 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025901 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025902 v->di_tv.vval.v_number = nr;
25903}
25904
25905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025906 * ":return [expr]"
25907 */
25908 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025909ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910{
25911 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025912 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 int returning = FALSE;
25914
25915 if (current_funccal == NULL)
25916 {
25917 EMSG(_("E133: :return not inside a function"));
25918 return;
25919 }
25920
25921 if (eap->skip)
25922 ++emsg_skip;
25923
25924 eap->nextcmd = NULL;
25925 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025926 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025927 {
25928 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025929 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025930 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025931 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025932 }
25933 /* It's safer to return also on error. */
25934 else if (!eap->skip)
25935 {
25936 /*
25937 * Return unless the expression evaluation has been cancelled due to an
25938 * aborting error, an interrupt, or an exception.
25939 */
25940 if (!aborting())
25941 returning = do_return(eap, FALSE, TRUE, NULL);
25942 }
25943
25944 /* When skipping or the return gets pending, advance to the next command
25945 * in this line (!returning). Otherwise, ignore the rest of the line.
25946 * Following lines will be ignored by get_func_line(). */
25947 if (returning)
25948 eap->nextcmd = NULL;
25949 else if (eap->nextcmd == NULL) /* no argument */
25950 eap->nextcmd = check_nextcmd(arg);
25951
25952 if (eap->skip)
25953 --emsg_skip;
25954}
25955
25956/*
25957 * Return from a function. Possibly makes the return pending. Also called
25958 * for a pending return at the ":endtry" or after returning from an extra
25959 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025960 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025961 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025962 * FALSE when the return gets pending.
25963 */
25964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025965do_return(
25966 exarg_T *eap,
25967 int reanimate,
25968 int is_cmd,
25969 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970{
25971 int idx;
25972 struct condstack *cstack = eap->cstack;
25973
25974 if (reanimate)
25975 /* Undo the return. */
25976 current_funccal->returned = FALSE;
25977
25978 /*
25979 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25980 * not in its finally clause (which then is to be executed next) is found.
25981 * In this case, make the ":return" pending for execution at the ":endtry".
25982 * Otherwise, return normally.
25983 */
25984 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25985 if (idx >= 0)
25986 {
25987 cstack->cs_pending[idx] = CSTP_RETURN;
25988
25989 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025990 /* A pending return again gets pending. "rettv" points to an
25991 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025992 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025993 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025994 else
25995 {
25996 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025997 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025999 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026000
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026001 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026002 {
26003 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026004 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026005 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026006 else
26007 EMSG(_(e_outofmem));
26008 }
26009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026010 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026011
26012 if (reanimate)
26013 {
26014 /* The pending return value could be overwritten by a ":return"
26015 * without argument in a finally clause; reset the default
26016 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026017 current_funccal->rettv->v_type = VAR_NUMBER;
26018 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019 }
26020 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026021 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026022 }
26023 else
26024 {
26025 current_funccal->returned = TRUE;
26026
26027 /* If the return is carried out now, store the return value. For
26028 * a return immediately after reanimation, the value is already
26029 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026030 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026032 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026033 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026034 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026035 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026036 }
26037 }
26038
26039 return idx < 0;
26040}
26041
26042/*
26043 * Free the variable with a pending return value.
26044 */
26045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026046discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026047{
Bram Moolenaar33570922005-01-25 22:26:29 +000026048 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049}
26050
26051/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026052 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026053 * is an allocated string. Used by report_pending() for verbose messages.
26054 */
26055 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026056get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026057{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026058 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026059 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026060 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026061
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026062 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026063 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026064 if (s == NULL)
26065 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026066
26067 STRCPY(IObuff, ":return ");
26068 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26069 if (STRLEN(s) + 8 >= IOSIZE)
26070 STRCPY(IObuff + IOSIZE - 4, "...");
26071 vim_free(tofree);
26072 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026073}
26074
26075/*
26076 * Get next function line.
26077 * Called by do_cmdline() to get the next line.
26078 * Returns allocated string, or NULL for end of function.
26079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026080 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026081get_func_line(
26082 int c UNUSED,
26083 void *cookie,
26084 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026085{
Bram Moolenaar33570922005-01-25 22:26:29 +000026086 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026087 ufunc_T *fp = fcp->func;
26088 char_u *retval;
26089 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026090
26091 /* If breakpoints have been added/deleted need to check for it. */
26092 if (fcp->dbg_tick != debug_tick)
26093 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026094 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026095 sourcing_lnum);
26096 fcp->dbg_tick = debug_tick;
26097 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026098#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026099 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026100 func_line_end(cookie);
26101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102
Bram Moolenaar05159a02005-02-26 23:04:13 +000026103 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026104 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26105 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026106 retval = NULL;
26107 else
26108 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026109 /* Skip NULL lines (continuation lines). */
26110 while (fcp->linenr < gap->ga_len
26111 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26112 ++fcp->linenr;
26113 if (fcp->linenr >= gap->ga_len)
26114 retval = NULL;
26115 else
26116 {
26117 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26118 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026119#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026120 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026121 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026122#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026124 }
26125
26126 /* Did we encounter a breakpoint? */
26127 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26128 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026129 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026130 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026131 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026132 sourcing_lnum);
26133 fcp->dbg_tick = debug_tick;
26134 }
26135
26136 return retval;
26137}
26138
Bram Moolenaar05159a02005-02-26 23:04:13 +000026139#if defined(FEAT_PROFILE) || defined(PROTO)
26140/*
26141 * Called when starting to read a function line.
26142 * "sourcing_lnum" must be correct!
26143 * When skipping lines it may not actually be executed, but we won't find out
26144 * until later and we need to store the time now.
26145 */
26146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026147func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026148{
26149 funccall_T *fcp = (funccall_T *)cookie;
26150 ufunc_T *fp = fcp->func;
26151
26152 if (fp->uf_profiling && sourcing_lnum >= 1
26153 && sourcing_lnum <= fp->uf_lines.ga_len)
26154 {
26155 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026156 /* Skip continuation lines. */
26157 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26158 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026159 fp->uf_tml_execed = FALSE;
26160 profile_start(&fp->uf_tml_start);
26161 profile_zero(&fp->uf_tml_children);
26162 profile_get_wait(&fp->uf_tml_wait);
26163 }
26164}
26165
26166/*
26167 * Called when actually executing a function line.
26168 */
26169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026170func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026171{
26172 funccall_T *fcp = (funccall_T *)cookie;
26173 ufunc_T *fp = fcp->func;
26174
26175 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26176 fp->uf_tml_execed = TRUE;
26177}
26178
26179/*
26180 * Called when done with a function line.
26181 */
26182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026183func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026184{
26185 funccall_T *fcp = (funccall_T *)cookie;
26186 ufunc_T *fp = fcp->func;
26187
26188 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26189 {
26190 if (fp->uf_tml_execed)
26191 {
26192 ++fp->uf_tml_count[fp->uf_tml_idx];
26193 profile_end(&fp->uf_tml_start);
26194 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026195 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026196 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26197 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026198 }
26199 fp->uf_tml_idx = -1;
26200 }
26201}
26202#endif
26203
Bram Moolenaar071d4272004-06-13 20:20:40 +000026204/*
26205 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026206 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026207 */
26208 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026209func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026210{
Bram Moolenaar33570922005-01-25 22:26:29 +000026211 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026212
26213 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26214 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026215 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026216 || fcp->returned);
26217}
26218
26219/*
26220 * return TRUE if cookie indicates a function which "abort"s on errors.
26221 */
26222 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026223func_has_abort(
26224 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026225{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026226 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026227}
26228
26229#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26230typedef enum
26231{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026232 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26233 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26234 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026235} var_flavour_T;
26236
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026237static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238
26239 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026240var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026241{
26242 char_u *p = varname;
26243
26244 if (ASCII_ISUPPER(*p))
26245 {
26246 while (*(++p))
26247 if (ASCII_ISLOWER(*p))
26248 return VAR_FLAVOUR_SESSION;
26249 return VAR_FLAVOUR_VIMINFO;
26250 }
26251 else
26252 return VAR_FLAVOUR_DEFAULT;
26253}
26254#endif
26255
26256#if defined(FEAT_VIMINFO) || defined(PROTO)
26257/*
26258 * Restore global vars that start with a capital from the viminfo file
26259 */
26260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026261read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026262{
26263 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026264 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026265 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026266 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026267
26268 if (!writing && (find_viminfo_parameter('!') != NULL))
26269 {
26270 tab = vim_strchr(virp->vir_line + 1, '\t');
26271 if (tab != NULL)
26272 {
26273 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026274 switch (*tab)
26275 {
26276 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026277#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026278 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026279#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026280 case 'D': type = VAR_DICT; break;
26281 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026282 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026284
26285 tab = vim_strchr(tab, '\t');
26286 if (tab != NULL)
26287 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026288 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026289 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026290 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026291 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026292#ifdef FEAT_FLOAT
26293 else if (type == VAR_FLOAT)
26294 (void)string2float(tab + 1, &tv.vval.v_float);
26295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026296 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026297 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026298 if (type == VAR_DICT || type == VAR_LIST)
26299 {
26300 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26301
26302 if (etv == NULL)
26303 /* Failed to parse back the dict or list, use it as a
26304 * string. */
26305 tv.v_type = VAR_STRING;
26306 else
26307 {
26308 vim_free(tv.vval.v_string);
26309 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026310 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026311 }
26312 }
26313
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026314 /* when in a function use global variables */
26315 save_funccal = current_funccal;
26316 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026317 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026318 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026319
26320 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026321 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026322 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26323 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026324 }
26325 }
26326 }
26327
26328 return viminfo_readline(virp);
26329}
26330
26331/*
26332 * Write global vars that start with a capital to the viminfo file
26333 */
26334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026335write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026336{
Bram Moolenaar33570922005-01-25 22:26:29 +000026337 hashitem_T *hi;
26338 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026339 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026340 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026341 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026342 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026343 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026344
26345 if (find_viminfo_parameter('!') == NULL)
26346 return;
26347
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026348 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026349
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026350 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026351 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026352 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026353 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026354 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026355 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026356 this_var = HI2DI(hi);
26357 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026358 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026359 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026360 {
26361 case VAR_STRING: s = "STR"; break;
26362 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026363 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026364 case VAR_DICT: s = "DIC"; break;
26365 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026366 case VAR_SPECIAL: s = "XPL"; break;
26367
26368 case VAR_UNKNOWN:
26369 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026370 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026371 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026372 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026373 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026374 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026375 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026376 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026377 if (p != NULL)
26378 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026379 vim_free(tofree);
26380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026381 }
26382 }
26383}
26384#endif
26385
26386#if defined(FEAT_SESSION) || defined(PROTO)
26387 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026388store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026389{
Bram Moolenaar33570922005-01-25 22:26:29 +000026390 hashitem_T *hi;
26391 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026392 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026393 char_u *p, *t;
26394
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026395 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026396 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026397 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026398 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026399 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026400 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026401 this_var = HI2DI(hi);
26402 if ((this_var->di_tv.v_type == VAR_NUMBER
26403 || this_var->di_tv.v_type == VAR_STRING)
26404 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026405 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026406 /* Escape special characters with a backslash. Turn a LF and
26407 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026408 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026409 (char_u *)"\\\"\n\r");
26410 if (p == NULL) /* out of memory */
26411 break;
26412 for (t = p; *t != NUL; ++t)
26413 if (*t == '\n')
26414 *t = 'n';
26415 else if (*t == '\r')
26416 *t = 'r';
26417 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026418 this_var->di_key,
26419 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26420 : ' ',
26421 p,
26422 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26423 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026424 || put_eol(fd) == FAIL)
26425 {
26426 vim_free(p);
26427 return FAIL;
26428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026429 vim_free(p);
26430 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026431#ifdef FEAT_FLOAT
26432 else if (this_var->di_tv.v_type == VAR_FLOAT
26433 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26434 {
26435 float_T f = this_var->di_tv.vval.v_float;
26436 int sign = ' ';
26437
26438 if (f < 0)
26439 {
26440 f = -f;
26441 sign = '-';
26442 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026443 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026444 this_var->di_key, sign, f) < 0)
26445 || put_eol(fd) == FAIL)
26446 return FAIL;
26447 }
26448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026449 }
26450 }
26451 return OK;
26452}
26453#endif
26454
Bram Moolenaar661b1822005-07-28 22:36:45 +000026455/*
26456 * Display script name where an item was last set.
26457 * Should only be invoked when 'verbose' is non-zero.
26458 */
26459 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026460last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026461{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026462 char_u *p;
26463
Bram Moolenaar661b1822005-07-28 22:36:45 +000026464 if (scriptID != 0)
26465 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026466 p = home_replace_save(NULL, get_scriptname(scriptID));
26467 if (p != NULL)
26468 {
26469 verbose_enter();
26470 MSG_PUTS(_("\n\tLast set from "));
26471 MSG_PUTS(p);
26472 vim_free(p);
26473 verbose_leave();
26474 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026475 }
26476}
26477
Bram Moolenaard812df62008-11-09 12:46:09 +000026478/*
26479 * List v:oldfiles in a nice way.
26480 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026482ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026483{
26484 list_T *l = vimvars[VV_OLDFILES].vv_list;
26485 listitem_T *li;
26486 int nr = 0;
26487
26488 if (l == NULL)
26489 msg((char_u *)_("No old files"));
26490 else
26491 {
26492 msg_start();
26493 msg_scroll = TRUE;
26494 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26495 {
26496 msg_outnum((long)++nr);
26497 MSG_PUTS(": ");
26498 msg_outtrans(get_tv_string(&li->li_tv));
26499 msg_putchar('\n');
26500 out_flush(); /* output one line at a time */
26501 ui_breakcheck();
26502 }
26503 /* Assume "got_int" was set to truncate the listing. */
26504 got_int = FALSE;
26505
26506#ifdef FEAT_BROWSE_CMD
26507 if (cmdmod.browse)
26508 {
26509 quit_more = FALSE;
26510 nr = prompt_for_number(FALSE);
26511 msg_starthere();
26512 if (nr > 0)
26513 {
26514 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26515 (long)nr);
26516
26517 if (p != NULL)
26518 {
26519 p = expand_env_save(p);
26520 eap->arg = p;
26521 eap->cmdidx = CMD_edit;
26522 cmdmod.browse = FALSE;
26523 do_exedit(eap, NULL);
26524 vim_free(p);
26525 }
26526 }
26527 }
26528#endif
26529 }
26530}
26531
Bram Moolenaar53744302015-07-17 17:38:22 +020026532/* reset v:option_new, v:option_old and v:option_type */
26533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026534reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026535{
26536 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26537 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26538 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26539}
26540
26541
Bram Moolenaar071d4272004-06-13 20:20:40 +000026542#endif /* FEAT_EVAL */
26543
Bram Moolenaar071d4272004-06-13 20:20:40 +000026544
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026545#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026546
26547#ifdef WIN3264
26548/*
26549 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26550 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026551static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26552static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26553static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026554
26555/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026556 * Get the short path (8.3) for the filename in "fnamep".
26557 * Only works for a valid file name.
26558 * When the path gets longer "fnamep" is changed and the allocated buffer
26559 * is put in "bufp".
26560 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26561 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562 */
26563 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026564get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026565{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026566 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026567 char_u *newbuf;
26568
26569 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026570 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026571 if (l > len - 1)
26572 {
26573 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026574 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026575 newbuf = vim_strnsave(*fnamep, l);
26576 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026577 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026578
26579 vim_free(*bufp);
26580 *fnamep = *bufp = newbuf;
26581
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026582 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026583 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026584 }
26585
26586 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026587 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026588}
26589
26590/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026591 * Get the short path (8.3) for the filename in "fname". The converted
26592 * path is returned in "bufp".
26593 *
26594 * Some of the directories specified in "fname" may not exist. This function
26595 * will shorten the existing directories at the beginning of the path and then
26596 * append the remaining non-existing path.
26597 *
26598 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026599 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026600 * bufp - Pointer to an allocated buffer for the filename.
26601 * fnamelen - Length of the filename pointed to by fname
26602 *
26603 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026604 */
26605 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026606shortpath_for_invalid_fname(
26607 char_u **fname,
26608 char_u **bufp,
26609 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026610{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026611 char_u *short_fname, *save_fname, *pbuf_unused;
26612 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026613 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026614 int old_len, len;
26615 int new_len, sfx_len;
26616 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026617
26618 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026619 old_len = *fnamelen;
26620 save_fname = vim_strnsave(*fname, old_len);
26621 pbuf_unused = NULL;
26622 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026623
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026624 endp = save_fname + old_len - 1; /* Find the end of the copy */
26625 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026626
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026627 /*
26628 * Try shortening the supplied path till it succeeds by removing one
26629 * directory at a time from the tail of the path.
26630 */
26631 len = 0;
26632 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026633 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026634 /* go back one path-separator */
26635 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26636 --endp;
26637 if (endp <= save_fname)
26638 break; /* processed the complete path */
26639
26640 /*
26641 * Replace the path separator with a NUL and try to shorten the
26642 * resulting path.
26643 */
26644 ch = *endp;
26645 *endp = 0;
26646 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026647 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026648 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26649 {
26650 retval = FAIL;
26651 goto theend;
26652 }
26653 *endp = ch; /* preserve the string */
26654
26655 if (len > 0)
26656 break; /* successfully shortened the path */
26657
26658 /* failed to shorten the path. Skip the path separator */
26659 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026660 }
26661
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026662 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026663 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026664 /*
26665 * Succeeded in shortening the path. Now concatenate the shortened
26666 * path with the remaining path at the tail.
26667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026668
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026669 /* Compute the length of the new path. */
26670 sfx_len = (int)(save_endp - endp) + 1;
26671 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026672
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026673 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026674 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026675 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026676 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026677 /* There is not enough space in the currently allocated string,
26678 * copy it to a buffer big enough. */
26679 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026680 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026681 {
26682 retval = FAIL;
26683 goto theend;
26684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026685 }
26686 else
26687 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026688 /* Transfer short_fname to the main buffer (it's big enough),
26689 * unless get_short_pathname() did its work in-place. */
26690 *fname = *bufp = save_fname;
26691 if (short_fname != save_fname)
26692 vim_strncpy(save_fname, short_fname, len);
26693 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026694 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026695
26696 /* concat the not-shortened part of the path */
26697 vim_strncpy(*fname + len, endp, sfx_len);
26698 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026699 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026700
26701theend:
26702 vim_free(pbuf_unused);
26703 vim_free(save_fname);
26704
26705 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026706}
26707
26708/*
26709 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026710 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026711 */
26712 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026713shortpath_for_partial(
26714 char_u **fnamep,
26715 char_u **bufp,
26716 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026717{
26718 int sepcount, len, tflen;
26719 char_u *p;
26720 char_u *pbuf, *tfname;
26721 int hasTilde;
26722
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026723 /* Count up the path separators from the RHS.. so we know which part
26724 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026725 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026726 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026727 if (vim_ispathsep(*p))
26728 ++sepcount;
26729
26730 /* Need full path first (use expand_env() to remove a "~/") */
26731 hasTilde = (**fnamep == '~');
26732 if (hasTilde)
26733 pbuf = tfname = expand_env_save(*fnamep);
26734 else
26735 pbuf = tfname = FullName_save(*fnamep, FALSE);
26736
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026737 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026738
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026739 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26740 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026741
26742 if (len == 0)
26743 {
26744 /* Don't have a valid filename, so shorten the rest of the
26745 * path if we can. This CAN give us invalid 8.3 filenames, but
26746 * there's not a lot of point in guessing what it might be.
26747 */
26748 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026749 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26750 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026751 }
26752
26753 /* Count the paths backward to find the beginning of the desired string. */
26754 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026755 {
26756#ifdef FEAT_MBYTE
26757 if (has_mbyte)
26758 p -= mb_head_off(tfname, p);
26759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026760 if (vim_ispathsep(*p))
26761 {
26762 if (sepcount == 0 || (hasTilde && sepcount == 1))
26763 break;
26764 else
26765 sepcount --;
26766 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026768 if (hasTilde)
26769 {
26770 --p;
26771 if (p >= tfname)
26772 *p = '~';
26773 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026774 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026775 }
26776 else
26777 ++p;
26778
26779 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26780 vim_free(*bufp);
26781 *fnamelen = (int)STRLEN(p);
26782 *bufp = pbuf;
26783 *fnamep = p;
26784
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026785 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026786}
26787#endif /* WIN3264 */
26788
26789/*
26790 * Adjust a filename, according to a string of modifiers.
26791 * *fnamep must be NUL terminated when called. When returning, the length is
26792 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026793 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026794 * When there is an error, *fnamep is set to NULL.
26795 */
26796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026797modify_fname(
26798 char_u *src, /* string with modifiers */
26799 int *usedlen, /* characters after src that are used */
26800 char_u **fnamep, /* file name so far */
26801 char_u **bufp, /* buffer for allocated file name or NULL */
26802 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026803{
26804 int valid = 0;
26805 char_u *tail;
26806 char_u *s, *p, *pbuf;
26807 char_u dirname[MAXPATHL];
26808 int c;
26809 int has_fullname = 0;
26810#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026811 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026812 int has_shortname = 0;
26813#endif
26814
26815repeat:
26816 /* ":p" - full path/file_name */
26817 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26818 {
26819 has_fullname = 1;
26820
26821 valid |= VALID_PATH;
26822 *usedlen += 2;
26823
26824 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26825 if ((*fnamep)[0] == '~'
26826#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26827 && ((*fnamep)[1] == '/'
26828# ifdef BACKSLASH_IN_FILENAME
26829 || (*fnamep)[1] == '\\'
26830# endif
26831 || (*fnamep)[1] == NUL)
26832
26833#endif
26834 )
26835 {
26836 *fnamep = expand_env_save(*fnamep);
26837 vim_free(*bufp); /* free any allocated file name */
26838 *bufp = *fnamep;
26839 if (*fnamep == NULL)
26840 return -1;
26841 }
26842
26843 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026844 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026845 {
26846 if (vim_ispathsep(*p)
26847 && p[1] == '.'
26848 && (p[2] == NUL
26849 || vim_ispathsep(p[2])
26850 || (p[2] == '.'
26851 && (p[3] == NUL || vim_ispathsep(p[3])))))
26852 break;
26853 }
26854
26855 /* FullName_save() is slow, don't use it when not needed. */
26856 if (*p != NUL || !vim_isAbsName(*fnamep))
26857 {
26858 *fnamep = FullName_save(*fnamep, *p != NUL);
26859 vim_free(*bufp); /* free any allocated file name */
26860 *bufp = *fnamep;
26861 if (*fnamep == NULL)
26862 return -1;
26863 }
26864
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026865#ifdef WIN3264
26866# if _WIN32_WINNT >= 0x0500
26867 if (vim_strchr(*fnamep, '~') != NULL)
26868 {
26869 /* Expand 8.3 filename to full path. Needed to make sure the same
26870 * file does not have two different names.
26871 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26872 p = alloc(_MAX_PATH + 1);
26873 if (p != NULL)
26874 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026875 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026876 {
26877 vim_free(*bufp);
26878 *bufp = *fnamep = p;
26879 }
26880 else
26881 vim_free(p);
26882 }
26883 }
26884# endif
26885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026886 /* Append a path separator to a directory. */
26887 if (mch_isdir(*fnamep))
26888 {
26889 /* Make room for one or two extra characters. */
26890 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26891 vim_free(*bufp); /* free any allocated file name */
26892 *bufp = *fnamep;
26893 if (*fnamep == NULL)
26894 return -1;
26895 add_pathsep(*fnamep);
26896 }
26897 }
26898
26899 /* ":." - path relative to the current directory */
26900 /* ":~" - path relative to the home directory */
26901 /* ":8" - shortname path - postponed till after */
26902 while (src[*usedlen] == ':'
26903 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26904 {
26905 *usedlen += 2;
26906 if (c == '8')
26907 {
26908#ifdef WIN3264
26909 has_shortname = 1; /* Postpone this. */
26910#endif
26911 continue;
26912 }
26913 pbuf = NULL;
26914 /* Need full path first (use expand_env() to remove a "~/") */
26915 if (!has_fullname)
26916 {
26917 if (c == '.' && **fnamep == '~')
26918 p = pbuf = expand_env_save(*fnamep);
26919 else
26920 p = pbuf = FullName_save(*fnamep, FALSE);
26921 }
26922 else
26923 p = *fnamep;
26924
26925 has_fullname = 0;
26926
26927 if (p != NULL)
26928 {
26929 if (c == '.')
26930 {
26931 mch_dirname(dirname, MAXPATHL);
26932 s = shorten_fname(p, dirname);
26933 if (s != NULL)
26934 {
26935 *fnamep = s;
26936 if (pbuf != NULL)
26937 {
26938 vim_free(*bufp); /* free any allocated file name */
26939 *bufp = pbuf;
26940 pbuf = NULL;
26941 }
26942 }
26943 }
26944 else
26945 {
26946 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26947 /* Only replace it when it starts with '~' */
26948 if (*dirname == '~')
26949 {
26950 s = vim_strsave(dirname);
26951 if (s != NULL)
26952 {
26953 *fnamep = s;
26954 vim_free(*bufp);
26955 *bufp = s;
26956 }
26957 }
26958 }
26959 vim_free(pbuf);
26960 }
26961 }
26962
26963 tail = gettail(*fnamep);
26964 *fnamelen = (int)STRLEN(*fnamep);
26965
26966 /* ":h" - head, remove "/file_name", can be repeated */
26967 /* Don't remove the first "/" or "c:\" */
26968 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26969 {
26970 valid |= VALID_HEAD;
26971 *usedlen += 2;
26972 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026973 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026974 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026975 *fnamelen = (int)(tail - *fnamep);
26976#ifdef VMS
26977 if (*fnamelen > 0)
26978 *fnamelen += 1; /* the path separator is part of the path */
26979#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026980 if (*fnamelen == 0)
26981 {
26982 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26983 p = vim_strsave((char_u *)".");
26984 if (p == NULL)
26985 return -1;
26986 vim_free(*bufp);
26987 *bufp = *fnamep = tail = p;
26988 *fnamelen = 1;
26989 }
26990 else
26991 {
26992 while (tail > s && !after_pathsep(s, tail))
26993 mb_ptr_back(*fnamep, tail);
26994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026995 }
26996
26997 /* ":8" - shortname */
26998 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26999 {
27000 *usedlen += 2;
27001#ifdef WIN3264
27002 has_shortname = 1;
27003#endif
27004 }
27005
27006#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027007 /*
27008 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027009 */
27010 if (has_shortname)
27011 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027012 /* Copy the string if it is shortened by :h and when it wasn't copied
27013 * yet, because we are going to change it in place. Avoids changing
27014 * the buffer name for "%:8". */
27015 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027016 {
27017 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027018 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027019 return -1;
27020 vim_free(*bufp);
27021 *bufp = *fnamep = p;
27022 }
27023
27024 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027025 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027026 if (!has_fullname && !vim_isAbsName(*fnamep))
27027 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027028 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027029 return -1;
27030 }
27031 else
27032 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027033 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027034
Bram Moolenaardc935552011-08-17 15:23:23 +020027035 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027036 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027037 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027038 return -1;
27039
27040 if (l == 0)
27041 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027042 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027043 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027044 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027045 return -1;
27046 }
27047 *fnamelen = l;
27048 }
27049 }
27050#endif /* WIN3264 */
27051
27052 /* ":t" - tail, just the basename */
27053 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27054 {
27055 *usedlen += 2;
27056 *fnamelen -= (int)(tail - *fnamep);
27057 *fnamep = tail;
27058 }
27059
27060 /* ":e" - extension, can be repeated */
27061 /* ":r" - root, without extension, can be repeated */
27062 while (src[*usedlen] == ':'
27063 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27064 {
27065 /* find a '.' in the tail:
27066 * - for second :e: before the current fname
27067 * - otherwise: The last '.'
27068 */
27069 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27070 s = *fnamep - 2;
27071 else
27072 s = *fnamep + *fnamelen - 1;
27073 for ( ; s > tail; --s)
27074 if (s[0] == '.')
27075 break;
27076 if (src[*usedlen + 1] == 'e') /* :e */
27077 {
27078 if (s > tail)
27079 {
27080 *fnamelen += (int)(*fnamep - (s + 1));
27081 *fnamep = s + 1;
27082#ifdef VMS
27083 /* cut version from the extension */
27084 s = *fnamep + *fnamelen - 1;
27085 for ( ; s > *fnamep; --s)
27086 if (s[0] == ';')
27087 break;
27088 if (s > *fnamep)
27089 *fnamelen = s - *fnamep;
27090#endif
27091 }
27092 else if (*fnamep <= tail)
27093 *fnamelen = 0;
27094 }
27095 else /* :r */
27096 {
27097 if (s > tail) /* remove one extension */
27098 *fnamelen = (int)(s - *fnamep);
27099 }
27100 *usedlen += 2;
27101 }
27102
27103 /* ":s?pat?foo?" - substitute */
27104 /* ":gs?pat?foo?" - global substitute */
27105 if (src[*usedlen] == ':'
27106 && (src[*usedlen + 1] == 's'
27107 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27108 {
27109 char_u *str;
27110 char_u *pat;
27111 char_u *sub;
27112 int sep;
27113 char_u *flags;
27114 int didit = FALSE;
27115
27116 flags = (char_u *)"";
27117 s = src + *usedlen + 2;
27118 if (src[*usedlen + 1] == 'g')
27119 {
27120 flags = (char_u *)"g";
27121 ++s;
27122 }
27123
27124 sep = *s++;
27125 if (sep)
27126 {
27127 /* find end of pattern */
27128 p = vim_strchr(s, sep);
27129 if (p != NULL)
27130 {
27131 pat = vim_strnsave(s, (int)(p - s));
27132 if (pat != NULL)
27133 {
27134 s = p + 1;
27135 /* find end of substitution */
27136 p = vim_strchr(s, sep);
27137 if (p != NULL)
27138 {
27139 sub = vim_strnsave(s, (int)(p - s));
27140 str = vim_strnsave(*fnamep, *fnamelen);
27141 if (sub != NULL && str != NULL)
27142 {
27143 *usedlen = (int)(p + 1 - src);
27144 s = do_string_sub(str, pat, sub, flags);
27145 if (s != NULL)
27146 {
27147 *fnamep = s;
27148 *fnamelen = (int)STRLEN(s);
27149 vim_free(*bufp);
27150 *bufp = s;
27151 didit = TRUE;
27152 }
27153 }
27154 vim_free(sub);
27155 vim_free(str);
27156 }
27157 vim_free(pat);
27158 }
27159 }
27160 /* after using ":s", repeat all the modifiers */
27161 if (didit)
27162 goto repeat;
27163 }
27164 }
27165
Bram Moolenaar26df0922014-02-23 23:39:13 +010027166 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27167 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027168 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027169 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027170 if (c != NUL)
27171 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027172 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027173 if (c != NUL)
27174 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027175 if (p == NULL)
27176 return -1;
27177 vim_free(*bufp);
27178 *bufp = *fnamep = p;
27179 *fnamelen = (int)STRLEN(p);
27180 *usedlen += 2;
27181 }
27182
Bram Moolenaar071d4272004-06-13 20:20:40 +000027183 return valid;
27184}
27185
27186/*
27187 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27188 * "flags" can be "g" to do a global substitute.
27189 * Returns an allocated string, NULL for error.
27190 */
27191 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027192do_string_sub(
27193 char_u *str,
27194 char_u *pat,
27195 char_u *sub,
27196 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027197{
27198 int sublen;
27199 regmatch_T regmatch;
27200 int i;
27201 int do_all;
27202 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027203 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027204 garray_T ga;
27205 char_u *ret;
27206 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027207 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027208
27209 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27210 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027211 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027212
27213 ga_init2(&ga, 1, 200);
27214
27215 do_all = (flags[0] == 'g');
27216
27217 regmatch.rm_ic = p_ic;
27218 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27219 if (regmatch.regprog != NULL)
27220 {
27221 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027222 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027223 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27224 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027225 /* Skip empty match except for first match. */
27226 if (regmatch.startp[0] == regmatch.endp[0])
27227 {
27228 if (zero_width == regmatch.startp[0])
27229 {
27230 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027231 i = MB_PTR2LEN(tail);
27232 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27233 (size_t)i);
27234 ga.ga_len += i;
27235 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027236 continue;
27237 }
27238 zero_width = regmatch.startp[0];
27239 }
27240
Bram Moolenaar071d4272004-06-13 20:20:40 +000027241 /*
27242 * Get some space for a temporary buffer to do the substitution
27243 * into. It will contain:
27244 * - The text up to where the match is.
27245 * - The substituted text.
27246 * - The text after the match.
27247 */
27248 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027249 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027250 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27251 {
27252 ga_clear(&ga);
27253 break;
27254 }
27255
27256 /* copy the text up to where the match is */
27257 i = (int)(regmatch.startp[0] - tail);
27258 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27259 /* add the substituted text */
27260 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27261 + ga.ga_len + i, TRUE, TRUE, FALSE);
27262 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027263 tail = regmatch.endp[0];
27264 if (*tail == NUL)
27265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027266 if (!do_all)
27267 break;
27268 }
27269
27270 if (ga.ga_data != NULL)
27271 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27272
Bram Moolenaar473de612013-06-08 18:19:48 +020027273 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027274 }
27275
27276 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27277 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027278 if (p_cpo == empty_option)
27279 p_cpo = save_cpo;
27280 else
27281 /* Darn, evaluating {sub} expression changed the value. */
27282 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027283
27284 return ret;
27285}
27286
27287#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */