blob: 4825e8be8affe2462aaa793994a4b945beec4444 [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 Moolenaar888ccac2016-06-04 18:49:36 +020013569 if (nr >= LOWEST_WIN_ID)
13570 {
13571 if (wp->w_id == nr)
13572 return wp;
13573 }
13574 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013575 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013576 if (nr >= LOWEST_WIN_ID)
13577 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013578 return wp;
13579#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013580 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013581 return curwin;
13582 return NULL;
13583#endif
13584}
13585
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013587 * Find window specified by "wvp" in tabpage "tvp".
13588 */
13589 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013590find_tabwin(
13591 typval_T *wvp, /* VAR_UNKNOWN for current window */
13592 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013593{
13594 win_T *wp = NULL;
13595 tabpage_T *tp = NULL;
13596 long n;
13597
13598 if (wvp->v_type != VAR_UNKNOWN)
13599 {
13600 if (tvp->v_type != VAR_UNKNOWN)
13601 {
13602 n = get_tv_number(tvp);
13603 if (n >= 0)
13604 tp = find_tabpage(n);
13605 }
13606 else
13607 tp = curtab;
13608
13609 if (tp != NULL)
13610 wp = find_win_by_nr(wvp, tp);
13611 }
13612 else
13613 wp = curwin;
13614
13615 return wp;
13616}
13617
13618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 * "getwinvar()" function
13620 */
13621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013622f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013624 getwinvar(argvars, rettv, 0);
13625}
13626
13627/*
13628 * getwinvar() and gettabwinvar()
13629 */
13630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013631getwinvar(
13632 typval_T *argvars,
13633 typval_T *rettv,
13634 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013635{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013636 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013639 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013640 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013641#ifdef FEAT_WINDOWS
13642 win_T *oldcurwin;
13643 tabpage_T *oldtabpage;
13644 int need_switch_win;
13645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013647#ifdef FEAT_WINDOWS
13648 if (off == 1)
13649 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13650 else
13651 tp = curtab;
13652#endif
13653 win = find_win_by_nr(&argvars[off], tp);
13654 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013655 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013657 rettv->v_type = VAR_STRING;
13658 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659
13660 if (win != NULL && varname != NULL)
13661 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013662#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013663 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013664 * otherwise the window is not valid. Only do this when needed,
13665 * autocommands get blocked. */
13666 need_switch_win = !(tp == curtab && win == curwin);
13667 if (!need_switch_win
13668 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13669#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013670 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013671 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013672 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013673 if (get_option_tv(&varname, rettv, 1) == OK)
13674 done = TRUE;
13675 }
13676 else
13677 {
13678 /* Look up the variable. */
13679 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13680 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13681 varname, FALSE);
13682 if (v != NULL)
13683 {
13684 copy_tv(&v->di_tv, rettv);
13685 done = TRUE;
13686 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013689
Bram Moolenaarba117c22015-09-29 16:53:22 +020013690#ifdef FEAT_WINDOWS
13691 if (need_switch_win)
13692 /* restore previous notion of curwin */
13693 restore_win(oldcurwin, oldtabpage, TRUE);
13694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 }
13696
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013697 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13698 /* use the default return value */
13699 copy_tv(&argvars[off + 2], rettv);
13700
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 --emsg_off;
13702}
13703
13704/*
13705 * "glob()" function
13706 */
13707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013708f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013710 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013712 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013714 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013715 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013717 if (argvars[1].v_type != VAR_UNKNOWN)
13718 {
13719 if (get_tv_number_chk(&argvars[1], &error))
13720 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013721 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013722 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013723 if (get_tv_number_chk(&argvars[2], &error))
13724 {
13725 rettv->v_type = VAR_LIST;
13726 rettv->vval.v_list = NULL;
13727 }
13728 if (argvars[3].v_type != VAR_UNKNOWN
13729 && get_tv_number_chk(&argvars[3], &error))
13730 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013731 }
13732 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013733 if (!error)
13734 {
13735 ExpandInit(&xpc);
13736 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013737 if (p_wic)
13738 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013739 if (rettv->v_type == VAR_STRING)
13740 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013741 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013742 else if (rettv_list_alloc(rettv) != FAIL)
13743 {
13744 int i;
13745
13746 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13747 NULL, options, WILD_ALL_KEEP);
13748 for (i = 0; i < xpc.xp_numfiles; i++)
13749 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13750
13751 ExpandCleanup(&xpc);
13752 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013753 }
13754 else
13755 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756}
13757
13758/*
13759 * "globpath()" function
13760 */
13761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013762f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013764 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013765 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013766 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013767 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013768 garray_T ga;
13769 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013771 /* When the optional second argument is non-zero, don't remove matches
13772 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013773 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013774 if (argvars[2].v_type != VAR_UNKNOWN)
13775 {
13776 if (get_tv_number_chk(&argvars[2], &error))
13777 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013778 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013779 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013780 if (get_tv_number_chk(&argvars[3], &error))
13781 {
13782 rettv->v_type = VAR_LIST;
13783 rettv->vval.v_list = NULL;
13784 }
13785 if (argvars[4].v_type != VAR_UNKNOWN
13786 && get_tv_number_chk(&argvars[4], &error))
13787 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013788 }
13789 }
13790 if (file != NULL && !error)
13791 {
13792 ga_init2(&ga, (int)sizeof(char_u *), 10);
13793 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13794 if (rettv->v_type == VAR_STRING)
13795 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13796 else if (rettv_list_alloc(rettv) != FAIL)
13797 for (i = 0; i < ga.ga_len; ++i)
13798 list_append_string(rettv->vval.v_list,
13799 ((char_u **)(ga.ga_data))[i], -1);
13800 ga_clear_strings(&ga);
13801 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013802 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013803 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804}
13805
13806/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013807 * "glob2regpat()" function
13808 */
13809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013810f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013811{
13812 char_u *pat = get_tv_string_chk(&argvars[0]);
13813
13814 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013815 rettv->vval.v_string = (pat == NULL)
13816 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013817}
13818
13819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 * "has()" function
13821 */
13822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013823f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824{
13825 int i;
13826 char_u *name;
13827 int n = FALSE;
13828 static char *(has_list[]) =
13829 {
13830#ifdef AMIGA
13831 "amiga",
13832# ifdef FEAT_ARP
13833 "arp",
13834# endif
13835#endif
13836#ifdef __BEOS__
13837 "beos",
13838#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013839#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 "mac",
13841#endif
13842#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013843 "macunix", /* built with 'darwin' enabled */
13844#endif
13845#if defined(__APPLE__) && __APPLE__ == 1
13846 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848#ifdef __QNX__
13849 "qnx",
13850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851#ifdef UNIX
13852 "unix",
13853#endif
13854#ifdef VMS
13855 "vms",
13856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857#ifdef WIN32
13858 "win32",
13859#endif
13860#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13861 "win32unix",
13862#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013863#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 "win64",
13865#endif
13866#ifdef EBCDIC
13867 "ebcdic",
13868#endif
13869#ifndef CASE_INSENSITIVE_FILENAME
13870 "fname_case",
13871#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013872#ifdef HAVE_ACL
13873 "acl",
13874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875#ifdef FEAT_ARABIC
13876 "arabic",
13877#endif
13878#ifdef FEAT_AUTOCMD
13879 "autocmd",
13880#endif
13881#ifdef FEAT_BEVAL
13882 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013883# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13884 "balloon_multiline",
13885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886#endif
13887#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13888 "builtin_terms",
13889# ifdef ALL_BUILTIN_TCAPS
13890 "all_builtin_terms",
13891# endif
13892#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013893#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13894 || defined(FEAT_GUI_W32) \
13895 || defined(FEAT_GUI_MOTIF))
13896 "browsefilter",
13897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898#ifdef FEAT_BYTEOFF
13899 "byte_offset",
13900#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013901#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013902 "channel",
13903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904#ifdef FEAT_CINDENT
13905 "cindent",
13906#endif
13907#ifdef FEAT_CLIENTSERVER
13908 "clientserver",
13909#endif
13910#ifdef FEAT_CLIPBOARD
13911 "clipboard",
13912#endif
13913#ifdef FEAT_CMDL_COMPL
13914 "cmdline_compl",
13915#endif
13916#ifdef FEAT_CMDHIST
13917 "cmdline_hist",
13918#endif
13919#ifdef FEAT_COMMENTS
13920 "comments",
13921#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013922#ifdef FEAT_CONCEAL
13923 "conceal",
13924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925#ifdef FEAT_CRYPT
13926 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013927 "crypt-blowfish",
13928 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929#endif
13930#ifdef FEAT_CSCOPE
13931 "cscope",
13932#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013933#ifdef FEAT_CURSORBIND
13934 "cursorbind",
13935#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013936#ifdef CURSOR_SHAPE
13937 "cursorshape",
13938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939#ifdef DEBUG
13940 "debug",
13941#endif
13942#ifdef FEAT_CON_DIALOG
13943 "dialog_con",
13944#endif
13945#ifdef FEAT_GUI_DIALOG
13946 "dialog_gui",
13947#endif
13948#ifdef FEAT_DIFF
13949 "diff",
13950#endif
13951#ifdef FEAT_DIGRAPHS
13952 "digraphs",
13953#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013954#ifdef FEAT_DIRECTX
13955 "directx",
13956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013957#ifdef FEAT_DND
13958 "dnd",
13959#endif
13960#ifdef FEAT_EMACS_TAGS
13961 "emacs_tags",
13962#endif
13963 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013964 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965#ifdef FEAT_SEARCH_EXTRA
13966 "extra_search",
13967#endif
13968#ifdef FEAT_FKMAP
13969 "farsi",
13970#endif
13971#ifdef FEAT_SEARCHPATH
13972 "file_in_path",
13973#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013974#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013975 "filterpipe",
13976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977#ifdef FEAT_FIND_ID
13978 "find_in_path",
13979#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013980#ifdef FEAT_FLOAT
13981 "float",
13982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983#ifdef FEAT_FOLDING
13984 "folding",
13985#endif
13986#ifdef FEAT_FOOTER
13987 "footer",
13988#endif
13989#if !defined(USE_SYSTEM) && defined(UNIX)
13990 "fork",
13991#endif
13992#ifdef FEAT_GETTEXT
13993 "gettext",
13994#endif
13995#ifdef FEAT_GUI
13996 "gui",
13997#endif
13998#ifdef FEAT_GUI_ATHENA
13999# ifdef FEAT_GUI_NEXTAW
14000 "gui_neXtaw",
14001# else
14002 "gui_athena",
14003# endif
14004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005#ifdef FEAT_GUI_GTK
14006 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014007# ifdef USE_GTK3
14008 "gui_gtk3",
14009# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014013#ifdef FEAT_GUI_GNOME
14014 "gui_gnome",
14015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016#ifdef FEAT_GUI_MAC
14017 "gui_mac",
14018#endif
14019#ifdef FEAT_GUI_MOTIF
14020 "gui_motif",
14021#endif
14022#ifdef FEAT_GUI_PHOTON
14023 "gui_photon",
14024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025#ifdef FEAT_GUI_W32
14026 "gui_win32",
14027#endif
14028#ifdef FEAT_HANGULIN
14029 "hangul_input",
14030#endif
14031#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14032 "iconv",
14033#endif
14034#ifdef FEAT_INS_EXPAND
14035 "insert_expand",
14036#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014037#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014038 "job",
14039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040#ifdef FEAT_JUMPLIST
14041 "jumplist",
14042#endif
14043#ifdef FEAT_KEYMAP
14044 "keymap",
14045#endif
14046#ifdef FEAT_LANGMAP
14047 "langmap",
14048#endif
14049#ifdef FEAT_LIBCALL
14050 "libcall",
14051#endif
14052#ifdef FEAT_LINEBREAK
14053 "linebreak",
14054#endif
14055#ifdef FEAT_LISP
14056 "lispindent",
14057#endif
14058#ifdef FEAT_LISTCMDS
14059 "listcmds",
14060#endif
14061#ifdef FEAT_LOCALMAP
14062 "localmap",
14063#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014064#ifdef FEAT_LUA
14065# ifndef DYNAMIC_LUA
14066 "lua",
14067# endif
14068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069#ifdef FEAT_MENU
14070 "menu",
14071#endif
14072#ifdef FEAT_SESSION
14073 "mksession",
14074#endif
14075#ifdef FEAT_MODIFY_FNAME
14076 "modify_fname",
14077#endif
14078#ifdef FEAT_MOUSE
14079 "mouse",
14080#endif
14081#ifdef FEAT_MOUSESHAPE
14082 "mouseshape",
14083#endif
14084#if defined(UNIX) || defined(VMS)
14085# ifdef FEAT_MOUSE_DEC
14086 "mouse_dec",
14087# endif
14088# ifdef FEAT_MOUSE_GPM
14089 "mouse_gpm",
14090# endif
14091# ifdef FEAT_MOUSE_JSB
14092 "mouse_jsbterm",
14093# endif
14094# ifdef FEAT_MOUSE_NET
14095 "mouse_netterm",
14096# endif
14097# ifdef FEAT_MOUSE_PTERM
14098 "mouse_pterm",
14099# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014100# ifdef FEAT_MOUSE_SGR
14101 "mouse_sgr",
14102# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014103# ifdef FEAT_SYSMOUSE
14104 "mouse_sysmouse",
14105# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014106# ifdef FEAT_MOUSE_URXVT
14107 "mouse_urxvt",
14108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109# ifdef FEAT_MOUSE_XTERM
14110 "mouse_xterm",
14111# endif
14112#endif
14113#ifdef FEAT_MBYTE
14114 "multi_byte",
14115#endif
14116#ifdef FEAT_MBYTE_IME
14117 "multi_byte_ime",
14118#endif
14119#ifdef FEAT_MULTI_LANG
14120 "multi_lang",
14121#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014122#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014123#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014124 "mzscheme",
14125#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127#ifdef FEAT_OLE
14128 "ole",
14129#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014130 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131#ifdef FEAT_PATH_EXTRA
14132 "path_extra",
14133#endif
14134#ifdef FEAT_PERL
14135#ifndef DYNAMIC_PERL
14136 "perl",
14137#endif
14138#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014139#ifdef FEAT_PERSISTENT_UNDO
14140 "persistent_undo",
14141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142#ifdef FEAT_PYTHON
14143#ifndef DYNAMIC_PYTHON
14144 "python",
14145#endif
14146#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014147#ifdef FEAT_PYTHON3
14148#ifndef DYNAMIC_PYTHON3
14149 "python3",
14150#endif
14151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152#ifdef FEAT_POSTSCRIPT
14153 "postscript",
14154#endif
14155#ifdef FEAT_PRINTER
14156 "printer",
14157#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014158#ifdef FEAT_PROFILE
14159 "profile",
14160#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014161#ifdef FEAT_RELTIME
14162 "reltime",
14163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164#ifdef FEAT_QUICKFIX
14165 "quickfix",
14166#endif
14167#ifdef FEAT_RIGHTLEFT
14168 "rightleft",
14169#endif
14170#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14171 "ruby",
14172#endif
14173#ifdef FEAT_SCROLLBIND
14174 "scrollbind",
14175#endif
14176#ifdef FEAT_CMDL_INFO
14177 "showcmd",
14178 "cmdline_info",
14179#endif
14180#ifdef FEAT_SIGNS
14181 "signs",
14182#endif
14183#ifdef FEAT_SMARTINDENT
14184 "smartindent",
14185#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014186#ifdef STARTUPTIME
14187 "startuptime",
14188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189#ifdef FEAT_STL_OPT
14190 "statusline",
14191#endif
14192#ifdef FEAT_SUN_WORKSHOP
14193 "sun_workshop",
14194#endif
14195#ifdef FEAT_NETBEANS_INTG
14196 "netbeans_intg",
14197#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014198#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014199 "spell",
14200#endif
14201#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202 "syntax",
14203#endif
14204#if defined(USE_SYSTEM) || !defined(UNIX)
14205 "system",
14206#endif
14207#ifdef FEAT_TAG_BINS
14208 "tag_binary",
14209#endif
14210#ifdef FEAT_TAG_OLDSTATIC
14211 "tag_old_static",
14212#endif
14213#ifdef FEAT_TAG_ANYWHITE
14214 "tag_any_white",
14215#endif
14216#ifdef FEAT_TCL
14217# ifndef DYNAMIC_TCL
14218 "tcl",
14219# endif
14220#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014221#ifdef FEAT_TERMGUICOLORS
14222 "termguicolors",
14223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224#ifdef TERMINFO
14225 "terminfo",
14226#endif
14227#ifdef FEAT_TERMRESPONSE
14228 "termresponse",
14229#endif
14230#ifdef FEAT_TEXTOBJ
14231 "textobjects",
14232#endif
14233#ifdef HAVE_TGETENT
14234 "tgetent",
14235#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014236#ifdef FEAT_TIMERS
14237 "timers",
14238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239#ifdef FEAT_TITLE
14240 "title",
14241#endif
14242#ifdef FEAT_TOOLBAR
14243 "toolbar",
14244#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014245#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14246 "unnamedplus",
14247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248#ifdef FEAT_USR_CMDS
14249 "user-commands", /* was accidentally included in 5.4 */
14250 "user_commands",
14251#endif
14252#ifdef FEAT_VIMINFO
14253 "viminfo",
14254#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014255#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 "vertsplit",
14257#endif
14258#ifdef FEAT_VIRTUALEDIT
14259 "virtualedit",
14260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262#ifdef FEAT_VISUALEXTRA
14263 "visualextra",
14264#endif
14265#ifdef FEAT_VREPLACE
14266 "vreplace",
14267#endif
14268#ifdef FEAT_WILDIGN
14269 "wildignore",
14270#endif
14271#ifdef FEAT_WILDMENU
14272 "wildmenu",
14273#endif
14274#ifdef FEAT_WINDOWS
14275 "windows",
14276#endif
14277#ifdef FEAT_WAK
14278 "winaltkeys",
14279#endif
14280#ifdef FEAT_WRITEBACKUP
14281 "writebackup",
14282#endif
14283#ifdef FEAT_XIM
14284 "xim",
14285#endif
14286#ifdef FEAT_XFONTSET
14287 "xfontset",
14288#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014289#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014290 "xpm",
14291 "xpm_w32", /* for backward compatibility */
14292#else
14293# if defined(HAVE_XPM)
14294 "xpm",
14295# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297#ifdef USE_XSMP
14298 "xsmp",
14299#endif
14300#ifdef USE_XSMP_INTERACT
14301 "xsmp_interact",
14302#endif
14303#ifdef FEAT_XCLIPBOARD
14304 "xterm_clipboard",
14305#endif
14306#ifdef FEAT_XTERM_SAVE
14307 "xterm_save",
14308#endif
14309#if defined(UNIX) && defined(FEAT_X11)
14310 "X11",
14311#endif
14312 NULL
14313 };
14314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014315 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 for (i = 0; has_list[i] != NULL; ++i)
14317 if (STRICMP(name, has_list[i]) == 0)
14318 {
14319 n = TRUE;
14320 break;
14321 }
14322
14323 if (n == FALSE)
14324 {
14325 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014326 {
14327 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014328 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014329 && vim_isdigit(name[6])
14330 && vim_isdigit(name[8])
14331 && vim_isdigit(name[10]))
14332 {
14333 int major = atoi((char *)name + 6);
14334 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014335
14336 /* Expect "patch-9.9.01234". */
14337 n = (major < VIM_VERSION_MAJOR
14338 || (major == VIM_VERSION_MAJOR
14339 && (minor < VIM_VERSION_MINOR
14340 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014341 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014342 }
14343 else
14344 n = has_patch(atoi((char *)name + 5));
14345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 else if (STRICMP(name, "vim_starting") == 0)
14347 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014348#ifdef FEAT_MBYTE
14349 else if (STRICMP(name, "multi_byte_encoding") == 0)
14350 n = has_mbyte;
14351#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014352#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14353 else if (STRICMP(name, "balloon_multiline") == 0)
14354 n = multiline_balloon_available();
14355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356#ifdef DYNAMIC_TCL
14357 else if (STRICMP(name, "tcl") == 0)
14358 n = tcl_enabled(FALSE);
14359#endif
14360#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14361 else if (STRICMP(name, "iconv") == 0)
14362 n = iconv_enabled(FALSE);
14363#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014364#ifdef DYNAMIC_LUA
14365 else if (STRICMP(name, "lua") == 0)
14366 n = lua_enabled(FALSE);
14367#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014368#ifdef DYNAMIC_MZSCHEME
14369 else if (STRICMP(name, "mzscheme") == 0)
14370 n = mzscheme_enabled(FALSE);
14371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372#ifdef DYNAMIC_RUBY
14373 else if (STRICMP(name, "ruby") == 0)
14374 n = ruby_enabled(FALSE);
14375#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014376#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377#ifdef DYNAMIC_PYTHON
14378 else if (STRICMP(name, "python") == 0)
14379 n = python_enabled(FALSE);
14380#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014381#endif
14382#ifdef FEAT_PYTHON3
14383#ifdef DYNAMIC_PYTHON3
14384 else if (STRICMP(name, "python3") == 0)
14385 n = python3_enabled(FALSE);
14386#endif
14387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388#ifdef DYNAMIC_PERL
14389 else if (STRICMP(name, "perl") == 0)
14390 n = perl_enabled(FALSE);
14391#endif
14392#ifdef FEAT_GUI
14393 else if (STRICMP(name, "gui_running") == 0)
14394 n = (gui.in_use || gui.starting);
14395# ifdef FEAT_GUI_W32
14396 else if (STRICMP(name, "gui_win32s") == 0)
14397 n = gui_is_win32s();
14398# endif
14399# ifdef FEAT_BROWSE
14400 else if (STRICMP(name, "browse") == 0)
14401 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14402# endif
14403#endif
14404#ifdef FEAT_SYN_HL
14405 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014406 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407#endif
14408#if defined(WIN3264)
14409 else if (STRICMP(name, "win95") == 0)
14410 n = mch_windows95();
14411#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014412#ifdef FEAT_NETBEANS_INTG
14413 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014414 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 }
14417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419}
14420
14421/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014422 * "has_key()" function
14423 */
14424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014425f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014426{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014427 if (argvars[0].v_type != VAR_DICT)
14428 {
14429 EMSG(_(e_dictreq));
14430 return;
14431 }
14432 if (argvars[0].vval.v_dict == NULL)
14433 return;
14434
14435 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014436 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014437}
14438
14439/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014440 * "haslocaldir()" function
14441 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014443f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014444{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014445 win_T *wp = NULL;
14446
14447 wp = find_tabwin(&argvars[0], &argvars[1]);
14448 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014449}
14450
14451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 * "hasmapto()" function
14453 */
14454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014455f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456{
14457 char_u *name;
14458 char_u *mode;
14459 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014460 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014463 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464 mode = (char_u *)"nvo";
14465 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014468 if (argvars[2].v_type != VAR_UNKNOWN)
14469 abbr = get_tv_number(&argvars[2]);
14470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471
Bram Moolenaar2c932302006-03-18 21:42:09 +000014472 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476}
14477
14478/*
14479 * "histadd()" function
14480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014482f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483{
14484#ifdef FEAT_CMDHIST
14485 int histype;
14486 char_u *str;
14487 char_u buf[NUMBUFLEN];
14488#endif
14489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 if (check_restricted() || check_secure())
14492 return;
14493#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14495 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496 if (histype >= 0)
14497 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014498 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 if (*str != NUL)
14500 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014501 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014503 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 return;
14505 }
14506 }
14507#endif
14508}
14509
14510/*
14511 * "histdel()" function
14512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014514f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515{
14516#ifdef FEAT_CMDHIST
14517 int n;
14518 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014519 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14522 if (str == NULL)
14523 n = 0;
14524 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014525 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014527 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014529 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 else
14532 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014534 get_tv_string_buf(&argvars[1], buf));
14535 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536#endif
14537}
14538
14539/*
14540 * "histget()" function
14541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014543f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544{
14545#ifdef FEAT_CMDHIST
14546 int type;
14547 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014548 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14551 if (str == NULL)
14552 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014554 {
14555 type = get_histtype(str);
14556 if (argvars[1].v_type == VAR_UNKNOWN)
14557 idx = get_history_idx(type);
14558 else
14559 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14560 /* -1 on type error */
14561 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014564 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567}
14568
14569/*
14570 * "histnr()" function
14571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014573f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574{
14575 int i;
14576
14577#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 char_u *history = get_tv_string_chk(&argvars[0]);
14579
14580 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 if (i >= HIST_CMD && i < HIST_COUNT)
14582 i = get_history_idx(i);
14583 else
14584#endif
14585 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014586 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587}
14588
14589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 * "highlightID(name)" function
14591 */
14592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014593f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014595 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596}
14597
14598/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599 * "highlight_exists()" function
14600 */
14601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014602f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603{
14604 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14605}
14606
14607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 * "hostname()" function
14609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014611f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014612{
14613 char_u hostname[256];
14614
14615 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014616 rettv->v_type = VAR_STRING;
14617 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014618}
14619
14620/*
14621 * iconv() function
14622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014624f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625{
14626#ifdef FEAT_MBYTE
14627 char_u buf1[NUMBUFLEN];
14628 char_u buf2[NUMBUFLEN];
14629 char_u *from, *to, *str;
14630 vimconv_T vimconv;
14631#endif
14632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014633 rettv->v_type = VAR_STRING;
14634 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635
14636#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014637 str = get_tv_string(&argvars[0]);
14638 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14639 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 vimconv.vc_type = CONV_NONE;
14641 convert_setup(&vimconv, from, to);
14642
14643 /* If the encodings are equal, no conversion needed. */
14644 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014645 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648
14649 convert_setup(&vimconv, NULL, NULL);
14650 vim_free(from);
14651 vim_free(to);
14652#endif
14653}
14654
14655/*
14656 * "indent()" function
14657 */
14658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014659f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660{
14661 linenr_T lnum;
14662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014663 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014665 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014667 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014668}
14669
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014670/*
14671 * "index()" function
14672 */
14673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014674f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014675{
Bram Moolenaar33570922005-01-25 22:26:29 +000014676 list_T *l;
14677 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014678 long idx = 0;
14679 int ic = FALSE;
14680
14681 rettv->vval.v_number = -1;
14682 if (argvars[0].v_type != VAR_LIST)
14683 {
14684 EMSG(_(e_listreq));
14685 return;
14686 }
14687 l = argvars[0].vval.v_list;
14688 if (l != NULL)
14689 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014690 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014691 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014693 int error = FALSE;
14694
Bram Moolenaar758711c2005-02-02 23:11:38 +000014695 /* Start at specified item. Use the cached index that list_find()
14696 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014697 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014698 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014699 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014700 ic = get_tv_number_chk(&argvars[3], &error);
14701 if (error)
14702 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014703 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014704
Bram Moolenaar758711c2005-02-02 23:11:38 +000014705 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014706 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014707 {
14708 rettv->vval.v_number = idx;
14709 break;
14710 }
14711 }
14712}
14713
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714static int inputsecret_flag = 0;
14715
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014716static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014717
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014719 * This function is used by f_input() and f_inputdialog() functions. The third
14720 * argument to f_input() specifies the type of completion to use at the
14721 * prompt. The third argument to f_inputdialog() specifies the value to return
14722 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 */
14724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014725get_user_input(
14726 typval_T *argvars,
14727 typval_T *rettv,
14728 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014730 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731 char_u *p = NULL;
14732 int c;
14733 char_u buf[NUMBUFLEN];
14734 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014735 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014736 int xp_type = EXPAND_NOTHING;
14737 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014739 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741
14742#ifdef NO_CONSOLE_INPUT
14743 /* While starting up, there is no place to enter text. */
14744 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746#endif
14747
14748 cmd_silent = FALSE; /* Want to see the prompt. */
14749 if (prompt != NULL)
14750 {
14751 /* Only the part of the message after the last NL is considered as
14752 * prompt for the command line */
14753 p = vim_strrchr(prompt, '\n');
14754 if (p == NULL)
14755 p = prompt;
14756 else
14757 {
14758 ++p;
14759 c = *p;
14760 *p = NUL;
14761 msg_start();
14762 msg_clr_eos();
14763 msg_puts_attr(prompt, echo_attr);
14764 msg_didout = FALSE;
14765 msg_starthere();
14766 *p = c;
14767 }
14768 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014770 if (argvars[1].v_type != VAR_UNKNOWN)
14771 {
14772 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14773 if (defstr != NULL)
14774 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014776 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014777 {
14778 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014779 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014780 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014781
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014782 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014783 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014784
Bram Moolenaar4463f292005-09-25 22:20:24 +000014785 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14786 if (xp_name == NULL)
14787 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014788
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014789 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014790
Bram Moolenaar4463f292005-09-25 22:20:24 +000014791 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14792 &xp_arg) == FAIL)
14793 return;
14794 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014795 }
14796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014797 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014798 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014799 int save_ex_normal_busy = ex_normal_busy;
14800 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014801 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014802 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14803 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014804 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014805 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014806 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014807 && argvars[1].v_type != VAR_UNKNOWN
14808 && argvars[2].v_type != VAR_UNKNOWN)
14809 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14810 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014811
14812 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014814 /* since the user typed this, no need to wait for return */
14815 need_wait_return = FALSE;
14816 msg_didout = FALSE;
14817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818 cmd_silent = cmd_silent_save;
14819}
14820
14821/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014822 * "input()" function
14823 * Also handles inputsecret() when inputsecret is set.
14824 */
14825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014826f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014827{
14828 get_user_input(argvars, rettv, FALSE);
14829}
14830
14831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832 * "inputdialog()" function
14833 */
14834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014835f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836{
14837#if defined(FEAT_GUI_TEXTDIALOG)
14838 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14839 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14840 {
14841 char_u *message;
14842 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014843 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014845 message = get_tv_string_chk(&argvars[0]);
14846 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014847 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014848 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 else
14850 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014851 if (message != NULL && defstr != NULL
14852 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014853 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014854 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855 else
14856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014857 if (message != NULL && defstr != NULL
14858 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014859 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014860 rettv->vval.v_string = vim_strsave(
14861 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014863 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014865 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014866 }
14867 else
14868#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014869 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870}
14871
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014872/*
14873 * "inputlist()" function
14874 */
14875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014876f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014877{
14878 listitem_T *li;
14879 int selected;
14880 int mouse_used;
14881
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014882#ifdef NO_CONSOLE_INPUT
14883 /* While starting up, there is no place to enter text. */
14884 if (no_console_input())
14885 return;
14886#endif
14887 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14888 {
14889 EMSG2(_(e_listarg), "inputlist()");
14890 return;
14891 }
14892
14893 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014894 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014895 lines_left = Rows; /* avoid more prompt */
14896 msg_scroll = TRUE;
14897 msg_clr_eos();
14898
14899 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14900 {
14901 msg_puts(get_tv_string(&li->li_tv));
14902 msg_putchar('\n');
14903 }
14904
14905 /* Ask for choice. */
14906 selected = prompt_for_number(&mouse_used);
14907 if (mouse_used)
14908 selected -= lines_left;
14909
14910 rettv->vval.v_number = selected;
14911}
14912
14913
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14915
14916/*
14917 * "inputrestore()" function
14918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014920f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921{
14922 if (ga_userinput.ga_len > 0)
14923 {
14924 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14926 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014927 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 }
14929 else if (p_verbose > 1)
14930 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014931 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014932 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 }
14934}
14935
14936/*
14937 * "inputsave()" function
14938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014940f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014941{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014942 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 if (ga_grow(&ga_userinput, 1) == OK)
14944 {
14945 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14946 + ga_userinput.ga_len);
14947 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014948 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 }
14950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014951 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952}
14953
14954/*
14955 * "inputsecret()" function
14956 */
14957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014958f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959{
14960 ++cmdline_star;
14961 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014962 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 --cmdline_star;
14964 --inputsecret_flag;
14965}
14966
14967/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014968 * "insert()" function
14969 */
14970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014971f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014972{
14973 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014974 listitem_T *item;
14975 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014976 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014977
14978 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014979 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014980 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014981 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014982 {
14983 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014984 before = get_tv_number_chk(&argvars[2], &error);
14985 if (error)
14986 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014987
Bram Moolenaar758711c2005-02-02 23:11:38 +000014988 if (before == l->lv_len)
14989 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014990 else
14991 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014992 item = list_find(l, before);
14993 if (item == NULL)
14994 {
14995 EMSGN(_(e_listidx), before);
14996 l = NULL;
14997 }
14998 }
14999 if (l != NULL)
15000 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015001 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015002 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015003 }
15004 }
15005}
15006
15007/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015008 * "invert(expr)" function
15009 */
15010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015011f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015012{
15013 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15014}
15015
15016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 * "isdirectory()" function
15018 */
15019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015020f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015022 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023}
15024
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015025/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015026 * "islocked()" function
15027 */
15028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015029f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015030{
15031 lval_T lv;
15032 char_u *end;
15033 dictitem_T *di;
15034
15035 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015036 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15037 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015038 if (end != NULL && lv.ll_name != NULL)
15039 {
15040 if (*end != NUL)
15041 EMSG(_(e_trailing));
15042 else
15043 {
15044 if (lv.ll_tv == NULL)
15045 {
15046 if (check_changedtick(lv.ll_name))
15047 rettv->vval.v_number = 1; /* always locked */
15048 else
15049 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015050 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015051 if (di != NULL)
15052 {
15053 /* Consider a variable locked when:
15054 * 1. the variable itself is locked
15055 * 2. the value of the variable is locked.
15056 * 3. the List or Dict value is locked.
15057 */
15058 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15059 || tv_islocked(&di->di_tv));
15060 }
15061 }
15062 }
15063 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015064 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015065 else if (lv.ll_newkey != NULL)
15066 EMSG2(_(e_dictkey), lv.ll_newkey);
15067 else if (lv.ll_list != NULL)
15068 /* List item. */
15069 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15070 else
15071 /* Dictionary item. */
15072 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15073 }
15074 }
15075
15076 clear_lval(&lv);
15077}
15078
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015079#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15080/*
15081 * "isnan()" function
15082 */
15083 static void
15084f_isnan(typval_T *argvars, typval_T *rettv)
15085{
15086 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15087 && isnan(argvars[0].vval.v_float);
15088}
15089#endif
15090
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015091static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015092
15093/*
15094 * Turn a dict into a list:
15095 * "what" == 0: list of keys
15096 * "what" == 1: list of values
15097 * "what" == 2: list of items
15098 */
15099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015100dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015101{
Bram Moolenaar33570922005-01-25 22:26:29 +000015102 list_T *l2;
15103 dictitem_T *di;
15104 hashitem_T *hi;
15105 listitem_T *li;
15106 listitem_T *li2;
15107 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015108 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015109
Bram Moolenaar8c711452005-01-14 21:53:12 +000015110 if (argvars[0].v_type != VAR_DICT)
15111 {
15112 EMSG(_(e_dictreq));
15113 return;
15114 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015115 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015116 return;
15117
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015118 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015119 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015121 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015122 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015123 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015124 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015125 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015126 --todo;
15127 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015128
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015129 li = listitem_alloc();
15130 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015131 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015132 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015133
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015134 if (what == 0)
15135 {
15136 /* keys() */
15137 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015138 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015139 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15140 }
15141 else if (what == 1)
15142 {
15143 /* values() */
15144 copy_tv(&di->di_tv, &li->li_tv);
15145 }
15146 else
15147 {
15148 /* items() */
15149 l2 = list_alloc();
15150 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015151 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015152 li->li_tv.vval.v_list = l2;
15153 if (l2 == NULL)
15154 break;
15155 ++l2->lv_refcount;
15156
15157 li2 = listitem_alloc();
15158 if (li2 == NULL)
15159 break;
15160 list_append(l2, li2);
15161 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015162 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015163 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15164
15165 li2 = listitem_alloc();
15166 if (li2 == NULL)
15167 break;
15168 list_append(l2, li2);
15169 copy_tv(&di->di_tv, &li2->li_tv);
15170 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015171 }
15172 }
15173}
15174
15175/*
15176 * "items(dict)" function
15177 */
15178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015179f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015180{
15181 dict_list(argvars, rettv, 2);
15182}
15183
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015184#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015185/*
15186 * Get the job from the argument.
15187 * Returns NULL if the job is invalid.
15188 */
15189 static job_T *
15190get_job_arg(typval_T *tv)
15191{
15192 job_T *job;
15193
15194 if (tv->v_type != VAR_JOB)
15195 {
15196 EMSG2(_(e_invarg2), get_tv_string(tv));
15197 return NULL;
15198 }
15199 job = tv->vval.v_job;
15200
15201 if (job == NULL)
15202 EMSG(_("E916: not a valid job"));
15203 return job;
15204}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015205
Bram Moolenaar835dc632016-02-07 14:27:38 +010015206/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015207 * "job_getchannel()" function
15208 */
15209 static void
15210f_job_getchannel(typval_T *argvars, typval_T *rettv)
15211{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015212 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015213
Bram Moolenaar65edff82016-02-21 16:40:11 +010015214 if (job != NULL)
15215 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015216 rettv->v_type = VAR_CHANNEL;
15217 rettv->vval.v_channel = job->jv_channel;
15218 if (job->jv_channel != NULL)
15219 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015220 }
15221}
15222
15223/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015224 * "job_info()" function
15225 */
15226 static void
15227f_job_info(typval_T *argvars, typval_T *rettv)
15228{
15229 job_T *job = get_job_arg(&argvars[0]);
15230
15231 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15232 job_info(job, rettv->vval.v_dict);
15233}
15234
15235/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015236 * "job_setoptions()" function
15237 */
15238 static void
15239f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15240{
15241 job_T *job = get_job_arg(&argvars[0]);
15242 jobopt_T opt;
15243
15244 if (job == NULL)
15245 return;
15246 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015247 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15248 job_set_options(job, &opt);
15249 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015250}
15251
15252/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015253 * "job_start()" function
15254 */
15255 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015256f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015257{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015258 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015259 if (check_restricted() || check_secure())
15260 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015261 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015262}
15263
15264/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015265 * "job_status()" function
15266 */
15267 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015268f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015269{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015270 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015271
Bram Moolenaar65edff82016-02-21 16:40:11 +010015272 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015273 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015274 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015275 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015276 }
15277}
15278
15279/*
15280 * "job_stop()" function
15281 */
15282 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015283f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015284{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015285 job_T *job = get_job_arg(&argvars[0]);
15286
15287 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015288 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015289}
15290#endif
15291
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015293 * "join()" function
15294 */
15295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015296f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015297{
15298 garray_T ga;
15299 char_u *sep;
15300
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015301 if (argvars[0].v_type != VAR_LIST)
15302 {
15303 EMSG(_(e_listreq));
15304 return;
15305 }
15306 if (argvars[0].vval.v_list == NULL)
15307 return;
15308 if (argvars[1].v_type == VAR_UNKNOWN)
15309 sep = (char_u *)" ";
15310 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015311 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015312
15313 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015314
15315 if (sep != NULL)
15316 {
15317 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015318 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015319 ga_append(&ga, NUL);
15320 rettv->vval.v_string = (char_u *)ga.ga_data;
15321 }
15322 else
15323 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015324}
15325
15326/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015327 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015328 */
15329 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015330f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015331{
15332 js_read_T reader;
15333
15334 reader.js_buf = get_tv_string(&argvars[0]);
15335 reader.js_fill = NULL;
15336 reader.js_used = 0;
15337 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15338 EMSG(_(e_invarg));
15339}
15340
15341/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015342 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015343 */
15344 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015345f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015346{
15347 rettv->v_type = VAR_STRING;
15348 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15349}
15350
15351/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015352 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015353 */
15354 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015355f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015356{
15357 js_read_T reader;
15358
15359 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015360 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015361 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015362 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015363 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015364}
15365
15366/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015367 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015368 */
15369 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015370f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015371{
15372 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015373 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015374}
15375
15376/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015377 * "keys()" function
15378 */
15379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015380f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015381{
15382 dict_list(argvars, rettv, 0);
15383}
15384
15385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386 * "last_buffer_nr()" function.
15387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015389f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390{
15391 int n = 0;
15392 buf_T *buf;
15393
15394 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15395 if (n < buf->b_fnum)
15396 n = buf->b_fnum;
15397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015398 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015399}
15400
15401/*
15402 * "len()" function
15403 */
15404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015405f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015406{
15407 switch (argvars[0].v_type)
15408 {
15409 case VAR_STRING:
15410 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015411 rettv->vval.v_number = (varnumber_T)STRLEN(
15412 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015413 break;
15414 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015416 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015417 case VAR_DICT:
15418 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15419 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015420 case VAR_UNKNOWN:
15421 case VAR_SPECIAL:
15422 case VAR_FLOAT:
15423 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015424 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015425 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015426 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015427 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015428 break;
15429 }
15430}
15431
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015432static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015433
15434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015435libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015436{
15437#ifdef FEAT_LIBCALL
15438 char_u *string_in;
15439 char_u **string_result;
15440 int nr_result;
15441#endif
15442
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015444 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015445 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015446
15447 if (check_restricted() || check_secure())
15448 return;
15449
15450#ifdef FEAT_LIBCALL
15451 /* The first two args must be strings, otherwise its meaningless */
15452 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15453 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015454 string_in = NULL;
15455 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015456 string_in = argvars[2].vval.v_string;
15457 if (type == VAR_NUMBER)
15458 string_result = NULL;
15459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015460 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015461 if (mch_libcall(argvars[0].vval.v_string,
15462 argvars[1].vval.v_string,
15463 string_in,
15464 argvars[2].vval.v_number,
15465 string_result,
15466 &nr_result) == OK
15467 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015468 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015469 }
15470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471}
15472
15473/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015474 * "libcall()" function
15475 */
15476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015477f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015478{
15479 libcall_common(argvars, rettv, VAR_STRING);
15480}
15481
15482/*
15483 * "libcallnr()" function
15484 */
15485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015486f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015487{
15488 libcall_common(argvars, rettv, VAR_NUMBER);
15489}
15490
15491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492 * "line(string)" function
15493 */
15494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015495f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496{
15497 linenr_T lnum = 0;
15498 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015499 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015501 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 if (fp != NULL)
15503 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015504 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505}
15506
15507/*
15508 * "line2byte(lnum)" function
15509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015511f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512{
15513#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515#else
15516 linenr_T lnum;
15517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015518 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015522 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15523 if (rettv->vval.v_number >= 0)
15524 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525#endif
15526}
15527
15528/*
15529 * "lispindent(lnum)" function
15530 */
15531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015532f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533{
15534#ifdef FEAT_LISP
15535 pos_T pos;
15536 linenr_T lnum;
15537
15538 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015539 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15541 {
15542 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015543 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 curwin->w_cursor = pos;
15545 }
15546 else
15547#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549}
15550
15551/*
15552 * "localtime()" function
15553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015555f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015556{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015557 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558}
15559
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015560static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561
15562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015563get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015564{
15565 char_u *keys;
15566 char_u *which;
15567 char_u buf[NUMBUFLEN];
15568 char_u *keys_buf = NULL;
15569 char_u *rhs;
15570 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015571 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015572 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015573 mapblock_T *mp;
15574 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015575
15576 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015577 rettv->v_type = VAR_STRING;
15578 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015580 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 if (*keys == NUL)
15582 return;
15583
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015584 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015585 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015586 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015587 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015588 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015589 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015590 if (argvars[3].v_type != VAR_UNKNOWN)
15591 get_dict = get_tv_number(&argvars[3]);
15592 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594 else
15595 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015596 if (which == NULL)
15597 return;
15598
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 mode = get_map_mode(&which, 0);
15600
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015601 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015602 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015604
15605 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015607 /* Return a string. */
15608 if (rhs != NULL)
15609 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610
Bram Moolenaarbd743252010-10-20 21:23:33 +020015611 }
15612 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15613 {
15614 /* Return a dictionary. */
15615 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15616 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15617 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618
Bram Moolenaarbd743252010-10-20 21:23:33 +020015619 dict_add_nr_str(dict, "lhs", 0L, lhs);
15620 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15621 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15622 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15623 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15624 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15625 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015626 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015627 dict_add_nr_str(dict, "mode", 0L, mapmode);
15628
15629 vim_free(lhs);
15630 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 }
15632}
15633
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015634#ifdef FEAT_FLOAT
15635/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015636 * "log()" function
15637 */
15638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015639f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015640{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015641 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015642
15643 rettv->v_type = VAR_FLOAT;
15644 if (get_float_arg(argvars, &f) == OK)
15645 rettv->vval.v_float = log(f);
15646 else
15647 rettv->vval.v_float = 0.0;
15648}
15649
15650/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015651 * "log10()" function
15652 */
15653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015654f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015655{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015656 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015657
15658 rettv->v_type = VAR_FLOAT;
15659 if (get_float_arg(argvars, &f) == OK)
15660 rettv->vval.v_float = log10(f);
15661 else
15662 rettv->vval.v_float = 0.0;
15663}
15664#endif
15665
Bram Moolenaar1dced572012-04-05 16:54:08 +020015666#ifdef FEAT_LUA
15667/*
15668 * "luaeval()" function
15669 */
15670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015671f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015672{
15673 char_u *str;
15674 char_u buf[NUMBUFLEN];
15675
15676 str = get_tv_string_buf(&argvars[0], buf);
15677 do_luaeval(str, argvars + 1, rettv);
15678}
15679#endif
15680
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015682 * "map()" function
15683 */
15684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015685f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015686{
15687 filter_map(argvars, rettv, TRUE);
15688}
15689
15690/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015691 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692 */
15693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015694f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015696 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697}
15698
15699/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015700 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701 */
15702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015703f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015705 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706}
15707
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015708static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709
15710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015711find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015713 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015714 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015715 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 char_u *pat;
15717 regmatch_T regmatch;
15718 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015719 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 char_u *save_cpo;
15721 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015722 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015723 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015724 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015725 list_T *l = NULL;
15726 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015727 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015728 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729
15730 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15731 save_cpo = p_cpo;
15732 p_cpo = (char_u *)"";
15733
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015735 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015736 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015737 /* type 3: return empty list when there are no matches.
15738 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015739 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015740 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015741 if (type == 4
15742 && (list_append_string(rettv->vval.v_list,
15743 (char_u *)"", 0) == FAIL
15744 || list_append_number(rettv->vval.v_list,
15745 (varnumber_T)-1) == FAIL
15746 || list_append_number(rettv->vval.v_list,
15747 (varnumber_T)-1) == FAIL
15748 || list_append_number(rettv->vval.v_list,
15749 (varnumber_T)-1) == FAIL))
15750 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015751 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015752 rettv->vval.v_list = NULL;
15753 goto theend;
15754 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015755 }
15756 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758 rettv->v_type = VAR_STRING;
15759 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015762 if (argvars[0].v_type == VAR_LIST)
15763 {
15764 if ((l = argvars[0].vval.v_list) == NULL)
15765 goto theend;
15766 li = l->lv_first;
15767 }
15768 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015769 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015770 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015771 len = (long)STRLEN(str);
15772 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015774 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15775 if (pat == NULL)
15776 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015777
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015778 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015780 int error = FALSE;
15781
15782 start = get_tv_number_chk(&argvars[2], &error);
15783 if (error)
15784 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015785 if (l != NULL)
15786 {
15787 li = list_find(l, start);
15788 if (li == NULL)
15789 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015790 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015791 }
15792 else
15793 {
15794 if (start < 0)
15795 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015796 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015797 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015798 /* When "count" argument is there ignore matches before "start",
15799 * otherwise skip part of the string. Differs when pattern is "^"
15800 * or "\<". */
15801 if (argvars[3].v_type != VAR_UNKNOWN)
15802 startcol = start;
15803 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015804 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015805 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015806 len -= start;
15807 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015808 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015809
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015810 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015811 nth = get_tv_number_chk(&argvars[3], &error);
15812 if (error)
15813 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 }
15815
15816 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15817 if (regmatch.regprog != NULL)
15818 {
15819 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015820
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015821 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015822 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015823 if (l != NULL)
15824 {
15825 if (li == NULL)
15826 {
15827 match = FALSE;
15828 break;
15829 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015830 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015831 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015832 if (str == NULL)
15833 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015834 }
15835
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015836 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015837
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015838 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015839 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015840 if (l == NULL && !match)
15841 break;
15842
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015843 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015844 if (l != NULL)
15845 {
15846 li = li->li_next;
15847 ++idx;
15848 }
15849 else
15850 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015851#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015852 startcol = (colnr_T)(regmatch.startp[0]
15853 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015854#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015855 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015856#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015857 if (startcol > (colnr_T)len
15858 || str + startcol <= regmatch.startp[0])
15859 {
15860 match = FALSE;
15861 break;
15862 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015863 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015864 }
15865
15866 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015868 if (type == 4)
15869 {
15870 listitem_T *li1 = rettv->vval.v_list->lv_first;
15871 listitem_T *li2 = li1->li_next;
15872 listitem_T *li3 = li2->li_next;
15873 listitem_T *li4 = li3->li_next;
15874
Bram Moolenaar3c809342016-06-01 22:34:48 +020015875 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015876 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15877 (int)(regmatch.endp[0] - regmatch.startp[0]));
15878 li3->li_tv.vval.v_number =
15879 (varnumber_T)(regmatch.startp[0] - expr);
15880 li4->li_tv.vval.v_number =
15881 (varnumber_T)(regmatch.endp[0] - expr);
15882 if (l != NULL)
15883 li2->li_tv.vval.v_number = (varnumber_T)idx;
15884 }
15885 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015886 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015887 int i;
15888
15889 /* return list with matched string and submatches */
15890 for (i = 0; i < NSUBEXP; ++i)
15891 {
15892 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015893 {
15894 if (list_append_string(rettv->vval.v_list,
15895 (char_u *)"", 0) == FAIL)
15896 break;
15897 }
15898 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015899 regmatch.startp[i],
15900 (int)(regmatch.endp[i] - regmatch.startp[i]))
15901 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015902 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015903 }
15904 }
15905 else if (type == 2)
15906 {
15907 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015908 if (l != NULL)
15909 copy_tv(&li->li_tv, rettv);
15910 else
15911 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015913 }
15914 else if (l != NULL)
15915 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916 else
15917 {
15918 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015919 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015920 (varnumber_T)(regmatch.startp[0] - str);
15921 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015922 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015924 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925 }
15926 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015927 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 }
15929
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015930 if (type == 4 && l == NULL)
15931 /* matchstrpos() without a list: drop the second item. */
15932 listitem_remove(rettv->vval.v_list,
15933 rettv->vval.v_list->lv_first->li_next);
15934
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015936 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 p_cpo = save_cpo;
15938}
15939
15940/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015941 * "match()" function
15942 */
15943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015944f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015945{
15946 find_some_match(argvars, rettv, 1);
15947}
15948
15949/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015950 * "matchadd()" function
15951 */
15952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015953f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015954{
15955#ifdef FEAT_SEARCH_EXTRA
15956 char_u buf[NUMBUFLEN];
15957 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15958 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15959 int prio = 10; /* default priority */
15960 int id = -1;
15961 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015962 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015963
15964 rettv->vval.v_number = -1;
15965
15966 if (grp == NULL || pat == NULL)
15967 return;
15968 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015969 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015970 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015971 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015972 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015973 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015974 if (argvars[4].v_type != VAR_UNKNOWN)
15975 {
15976 if (argvars[4].v_type != VAR_DICT)
15977 {
15978 EMSG(_(e_dictreq));
15979 return;
15980 }
15981 if (dict_find(argvars[4].vval.v_dict,
15982 (char_u *)"conceal", -1) != NULL)
15983 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15984 (char_u *)"conceal", FALSE);
15985 }
15986 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015987 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015988 if (error == TRUE)
15989 return;
15990 if (id >= 1 && id <= 3)
15991 {
15992 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15993 return;
15994 }
15995
Bram Moolenaar6561d522015-07-21 15:48:27 +020015996 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15997 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015998#endif
15999}
16000
16001/*
16002 * "matchaddpos()" function
16003 */
16004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016005f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016006{
16007#ifdef FEAT_SEARCH_EXTRA
16008 char_u buf[NUMBUFLEN];
16009 char_u *group;
16010 int prio = 10;
16011 int id = -1;
16012 int error = FALSE;
16013 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016014 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016015
16016 rettv->vval.v_number = -1;
16017
16018 group = get_tv_string_buf_chk(&argvars[0], buf);
16019 if (group == NULL)
16020 return;
16021
16022 if (argvars[1].v_type != VAR_LIST)
16023 {
16024 EMSG2(_(e_listarg), "matchaddpos()");
16025 return;
16026 }
16027 l = argvars[1].vval.v_list;
16028 if (l == NULL)
16029 return;
16030
16031 if (argvars[2].v_type != VAR_UNKNOWN)
16032 {
16033 prio = get_tv_number_chk(&argvars[2], &error);
16034 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016035 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016036 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016037 if (argvars[4].v_type != VAR_UNKNOWN)
16038 {
16039 if (argvars[4].v_type != VAR_DICT)
16040 {
16041 EMSG(_(e_dictreq));
16042 return;
16043 }
16044 if (dict_find(argvars[4].vval.v_dict,
16045 (char_u *)"conceal", -1) != NULL)
16046 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16047 (char_u *)"conceal", FALSE);
16048 }
16049 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016050 }
16051 if (error == TRUE)
16052 return;
16053
16054 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16055 if (id == 1 || id == 2)
16056 {
16057 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16058 return;
16059 }
16060
Bram Moolenaar6561d522015-07-21 15:48:27 +020016061 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16062 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016063#endif
16064}
16065
16066/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016067 * "matcharg()" function
16068 */
16069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016070f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016071{
16072 if (rettv_list_alloc(rettv) == OK)
16073 {
16074#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016075 int id = get_tv_number(&argvars[0]);
16076 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016077
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016078 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016079 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016080 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16081 {
16082 list_append_string(rettv->vval.v_list,
16083 syn_id2name(m->hlg_id), -1);
16084 list_append_string(rettv->vval.v_list, m->pattern, -1);
16085 }
16086 else
16087 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016088 list_append_string(rettv->vval.v_list, NULL, -1);
16089 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016090 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016091 }
16092#endif
16093 }
16094}
16095
16096/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016097 * "matchdelete()" function
16098 */
16099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016100f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016101{
16102#ifdef FEAT_SEARCH_EXTRA
16103 rettv->vval.v_number = match_delete(curwin,
16104 (int)get_tv_number(&argvars[0]), TRUE);
16105#endif
16106}
16107
16108/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016109 * "matchend()" function
16110 */
16111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016112f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016113{
16114 find_some_match(argvars, rettv, 0);
16115}
16116
16117/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016118 * "matchlist()" function
16119 */
16120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016121f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016122{
16123 find_some_match(argvars, rettv, 3);
16124}
16125
16126/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016127 * "matchstr()" function
16128 */
16129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016130f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016131{
16132 find_some_match(argvars, rettv, 2);
16133}
16134
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016135/*
16136 * "matchstrpos()" function
16137 */
16138 static void
16139f_matchstrpos(typval_T *argvars, typval_T *rettv)
16140{
16141 find_some_match(argvars, rettv, 4);
16142}
16143
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016144static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016145
16146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016147max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016148{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016149 long n = 0;
16150 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016151 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016152
16153 if (argvars[0].v_type == VAR_LIST)
16154 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016155 list_T *l;
16156 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016157
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016158 l = argvars[0].vval.v_list;
16159 if (l != NULL)
16160 {
16161 li = l->lv_first;
16162 if (li != NULL)
16163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016164 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016165 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016166 {
16167 li = li->li_next;
16168 if (li == NULL)
16169 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016170 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016171 if (domax ? i > n : i < n)
16172 n = i;
16173 }
16174 }
16175 }
16176 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016177 else if (argvars[0].v_type == VAR_DICT)
16178 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016179 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016180 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016181 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016182 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016183
16184 d = argvars[0].vval.v_dict;
16185 if (d != NULL)
16186 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016187 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016188 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016189 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016190 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016191 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016192 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016193 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016194 if (first)
16195 {
16196 n = i;
16197 first = FALSE;
16198 }
16199 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016200 n = i;
16201 }
16202 }
16203 }
16204 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016205 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016206 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016207 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016208}
16209
16210/*
16211 * "max()" function
16212 */
16213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016214f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016215{
16216 max_min(argvars, rettv, TRUE);
16217}
16218
16219/*
16220 * "min()" function
16221 */
16222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016223f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016224{
16225 max_min(argvars, rettv, FALSE);
16226}
16227
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016228static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016229
16230/*
16231 * Create the directory in which "dir" is located, and higher levels when
16232 * needed.
16233 */
16234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016235mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016236{
16237 char_u *p;
16238 char_u *updir;
16239 int r = FAIL;
16240
16241 /* Get end of directory name in "dir".
16242 * We're done when it's "/" or "c:/". */
16243 p = gettail_sep(dir);
16244 if (p <= get_past_head(dir))
16245 return OK;
16246
16247 /* If the directory exists we're done. Otherwise: create it.*/
16248 updir = vim_strnsave(dir, (int)(p - dir));
16249 if (updir == NULL)
16250 return FAIL;
16251 if (mch_isdir(updir))
16252 r = OK;
16253 else if (mkdir_recurse(updir, prot) == OK)
16254 r = vim_mkdir_emsg(updir, prot);
16255 vim_free(updir);
16256 return r;
16257}
16258
16259#ifdef vim_mkdir
16260/*
16261 * "mkdir()" function
16262 */
16263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016264f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016265{
16266 char_u *dir;
16267 char_u buf[NUMBUFLEN];
16268 int prot = 0755;
16269
16270 rettv->vval.v_number = FAIL;
16271 if (check_restricted() || check_secure())
16272 return;
16273
16274 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016275 if (*dir == NUL)
16276 rettv->vval.v_number = FAIL;
16277 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016278 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016279 if (*gettail(dir) == NUL)
16280 /* remove trailing slashes */
16281 *gettail_sep(dir) = NUL;
16282
16283 if (argvars[1].v_type != VAR_UNKNOWN)
16284 {
16285 if (argvars[2].v_type != VAR_UNKNOWN)
16286 prot = get_tv_number_chk(&argvars[2], NULL);
16287 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16288 mkdir_recurse(dir, prot);
16289 }
16290 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016291 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016292}
16293#endif
16294
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 * "mode()" function
16297 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016299f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016301 char_u buf[3];
16302
16303 buf[1] = NUL;
16304 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 if (VIsual_active)
16307 {
16308 if (VIsual_select)
16309 buf[0] = VIsual_mode + 's' - 'v';
16310 else
16311 buf[0] = VIsual_mode;
16312 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016313 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016314 || State == CONFIRM)
16315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016317 if (State == ASKMORE)
16318 buf[1] = 'm';
16319 else if (State == CONFIRM)
16320 buf[1] = '?';
16321 }
16322 else if (State == EXTERNCMD)
16323 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016324 else if (State & INSERT)
16325 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016326#ifdef FEAT_VREPLACE
16327 if (State & VREPLACE_FLAG)
16328 {
16329 buf[0] = 'R';
16330 buf[1] = 'v';
16331 }
16332 else
16333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 if (State & REPLACE_FLAG)
16335 buf[0] = 'R';
16336 else
16337 buf[0] = 'i';
16338 }
16339 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016342 if (exmode_active)
16343 buf[1] = 'v';
16344 }
16345 else if (exmode_active)
16346 {
16347 buf[0] = 'c';
16348 buf[1] = 'e';
16349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016351 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016353 if (finish_op)
16354 buf[1] = 'o';
16355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016357 /* Clear out the minor mode when the argument is not a non-zero number or
16358 * non-empty string. */
16359 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016360 buf[1] = NUL;
16361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016362 rettv->vval.v_string = vim_strsave(buf);
16363 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364}
16365
Bram Moolenaar429fa852013-04-15 12:27:36 +020016366#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016367/*
16368 * "mzeval()" function
16369 */
16370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016371f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016372{
16373 char_u *str;
16374 char_u buf[NUMBUFLEN];
16375
16376 str = get_tv_string_buf(&argvars[0], buf);
16377 do_mzeval(str, rettv);
16378}
Bram Moolenaar75676462013-01-30 14:55:42 +010016379
16380 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016381mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016382{
16383 typval_T argvars[3];
16384
16385 argvars[0].v_type = VAR_STRING;
16386 argvars[0].vval.v_string = name;
16387 copy_tv(args, &argvars[1]);
16388 argvars[2].v_type = VAR_UNKNOWN;
16389 f_call(argvars, rettv);
16390 clear_tv(&argvars[1]);
16391}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016392#endif
16393
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395 * "nextnonblank()" function
16396 */
16397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016398f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399{
16400 linenr_T lnum;
16401
16402 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016404 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016405 {
16406 lnum = 0;
16407 break;
16408 }
16409 if (*skipwhite(ml_get(lnum)) != NUL)
16410 break;
16411 }
16412 rettv->vval.v_number = lnum;
16413}
16414
16415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 * "nr2char()" function
16417 */
16418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016419f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420{
16421 char_u buf[NUMBUFLEN];
16422
16423#ifdef FEAT_MBYTE
16424 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016425 {
16426 int utf8 = 0;
16427
16428 if (argvars[1].v_type != VAR_UNKNOWN)
16429 utf8 = get_tv_number_chk(&argvars[1], NULL);
16430 if (utf8)
16431 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16432 else
16433 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435 else
16436#endif
16437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016438 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 buf[1] = NUL;
16440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016441 rettv->v_type = VAR_STRING;
16442 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016443}
16444
16445/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016446 * "or(expr, expr)" function
16447 */
16448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016449f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016450{
16451 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16452 | get_tv_number_chk(&argvars[1], NULL);
16453}
16454
16455/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016456 * "pathshorten()" function
16457 */
16458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016459f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016460{
16461 char_u *p;
16462
16463 rettv->v_type = VAR_STRING;
16464 p = get_tv_string_chk(&argvars[0]);
16465 if (p == NULL)
16466 rettv->vval.v_string = NULL;
16467 else
16468 {
16469 p = vim_strsave(p);
16470 rettv->vval.v_string = p;
16471 if (p != NULL)
16472 shorten_dir(p);
16473 }
16474}
16475
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016476#ifdef FEAT_PERL
16477/*
16478 * "perleval()" function
16479 */
16480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016481f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016482{
16483 char_u *str;
16484 char_u buf[NUMBUFLEN];
16485
16486 str = get_tv_string_buf(&argvars[0], buf);
16487 do_perleval(str, rettv);
16488}
16489#endif
16490
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016491#ifdef FEAT_FLOAT
16492/*
16493 * "pow()" function
16494 */
16495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016496f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016497{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016498 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016499
16500 rettv->v_type = VAR_FLOAT;
16501 if (get_float_arg(argvars, &fx) == OK
16502 && get_float_arg(&argvars[1], &fy) == OK)
16503 rettv->vval.v_float = pow(fx, fy);
16504 else
16505 rettv->vval.v_float = 0.0;
16506}
16507#endif
16508
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016509/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510 * "prevnonblank()" function
16511 */
16512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016513f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016514{
16515 linenr_T lnum;
16516
16517 lnum = get_tv_lnum(argvars);
16518 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16519 lnum = 0;
16520 else
16521 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16522 --lnum;
16523 rettv->vval.v_number = lnum;
16524}
16525
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016526/* This dummy va_list is here because:
16527 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16528 * - locally in the function results in a "used before set" warning
16529 * - using va_start() to initialize it gives "function with fixed args" error */
16530static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016531
Bram Moolenaar8c711452005-01-14 21:53:12 +000016532/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016533 * "printf()" function
16534 */
16535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016536f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016537{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016538 char_u buf[NUMBUFLEN];
16539 int len;
16540 char_u *s;
16541 int saved_did_emsg = did_emsg;
16542 char *fmt;
16543
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016544 rettv->v_type = VAR_STRING;
16545 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016546
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016547 /* Get the required length, allocate the buffer and do it for real. */
16548 did_emsg = FALSE;
16549 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16550 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16551 if (!did_emsg)
16552 {
16553 s = alloc(len + 1);
16554 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016555 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016556 rettv->vval.v_string = s;
16557 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016558 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016559 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016560 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016561}
16562
16563/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016564 * "pumvisible()" function
16565 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016567f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016568{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016569#ifdef FEAT_INS_EXPAND
16570 if (pum_visible())
16571 rettv->vval.v_number = 1;
16572#endif
16573}
16574
Bram Moolenaardb913952012-06-29 12:54:53 +020016575#ifdef FEAT_PYTHON3
16576/*
16577 * "py3eval()" function
16578 */
16579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016580f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016581{
16582 char_u *str;
16583 char_u buf[NUMBUFLEN];
16584
16585 str = get_tv_string_buf(&argvars[0], buf);
16586 do_py3eval(str, rettv);
16587}
16588#endif
16589
16590#ifdef FEAT_PYTHON
16591/*
16592 * "pyeval()" function
16593 */
16594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016595f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016596{
16597 char_u *str;
16598 char_u buf[NUMBUFLEN];
16599
16600 str = get_tv_string_buf(&argvars[0], buf);
16601 do_pyeval(str, rettv);
16602}
16603#endif
16604
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016605/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016606 * "range()" function
16607 */
16608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016609f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016610{
16611 long start;
16612 long end;
16613 long stride = 1;
16614 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016615 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016617 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016618 if (argvars[1].v_type == VAR_UNKNOWN)
16619 {
16620 end = start - 1;
16621 start = 0;
16622 }
16623 else
16624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016625 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016626 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016627 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016628 }
16629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016630 if (error)
16631 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016632 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016633 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016634 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016635 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016636 else
16637 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016638 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016639 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016640 if (list_append_number(rettv->vval.v_list,
16641 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016642 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016643 }
16644}
16645
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016646/*
16647 * "readfile()" function
16648 */
16649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016650f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016651{
16652 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016653 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016654 char_u *fname;
16655 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016656 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16657 int io_size = sizeof(buf);
16658 int readlen; /* size of last fread() */
16659 char_u *prev = NULL; /* previously read bytes, if any */
16660 long prevlen = 0; /* length of data in prev */
16661 long prevsize = 0; /* size of prev buffer */
16662 long maxline = MAXLNUM;
16663 long cnt = 0;
16664 char_u *p; /* position in buf */
16665 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016666
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016667 if (argvars[1].v_type != VAR_UNKNOWN)
16668 {
16669 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16670 binary = TRUE;
16671 if (argvars[2].v_type != VAR_UNKNOWN)
16672 maxline = get_tv_number(&argvars[2]);
16673 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016674
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016675 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016676 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016677
16678 /* Always open the file in binary mode, library functions have a mind of
16679 * their own about CR-LF conversion. */
16680 fname = get_tv_string(&argvars[0]);
16681 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16682 {
16683 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16684 return;
16685 }
16686
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016687 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016688 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016689 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016690
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016691 /* This for loop processes what was read, but is also entered at end
16692 * of file so that either:
16693 * - an incomplete line gets written
16694 * - a "binary" file gets an empty line at the end if it ends in a
16695 * newline. */
16696 for (p = buf, start = buf;
16697 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16698 ++p)
16699 {
16700 if (*p == '\n' || readlen <= 0)
16701 {
16702 listitem_T *li;
16703 char_u *s = NULL;
16704 long_u len = p - start;
16705
16706 /* Finished a line. Remove CRs before NL. */
16707 if (readlen > 0 && !binary)
16708 {
16709 while (len > 0 && start[len - 1] == '\r')
16710 --len;
16711 /* removal may cross back to the "prev" string */
16712 if (len == 0)
16713 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16714 --prevlen;
16715 }
16716 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016717 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016718 else
16719 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016720 /* Change "prev" buffer to be the right size. This way
16721 * the bytes are only copied once, and very long lines are
16722 * allocated only once. */
16723 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016724 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016725 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016726 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016727 prev = NULL; /* the list will own the string */
16728 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016729 }
16730 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016731 if (s == NULL)
16732 {
16733 do_outofmem_msg((long_u) prevlen + len + 1);
16734 failed = TRUE;
16735 break;
16736 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016737
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016738 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016739 {
16740 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016741 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016742 break;
16743 }
16744 li->li_tv.v_type = VAR_STRING;
16745 li->li_tv.v_lock = 0;
16746 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016747 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016748
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016749 start = p + 1; /* step over newline */
16750 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016751 break;
16752 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016753 else if (*p == NUL)
16754 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016755#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016756 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16757 * when finding the BF and check the previous two bytes. */
16758 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016759 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016760 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16761 * + 1, these may be in the "prev" string. */
16762 char_u back1 = p >= buf + 1 ? p[-1]
16763 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16764 char_u back2 = p >= buf + 2 ? p[-2]
16765 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16766 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016767
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016768 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016769 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016770 char_u *dest = p - 2;
16771
16772 /* Usually a BOM is at the beginning of a file, and so at
16773 * the beginning of a line; then we can just step over it.
16774 */
16775 if (start == dest)
16776 start = p + 1;
16777 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016778 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016779 /* have to shuffle buf to close gap */
16780 int adjust_prevlen = 0;
16781
16782 if (dest < buf)
16783 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016784 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016785 dest = buf;
16786 }
16787 if (readlen > p - buf + 1)
16788 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16789 readlen -= 3 - adjust_prevlen;
16790 prevlen -= adjust_prevlen;
16791 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016792 }
16793 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016794 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016795#endif
16796 } /* for */
16797
16798 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16799 break;
16800 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016801 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016802 /* There's part of a line in buf, store it in "prev". */
16803 if (p - start + prevlen >= prevsize)
16804 {
16805 /* need bigger "prev" buffer */
16806 char_u *newprev;
16807
16808 /* A common use case is ordinary text files and "prev" gets a
16809 * fragment of a line, so the first allocation is made
16810 * small, to avoid repeatedly 'allocing' large and
16811 * 'reallocing' small. */
16812 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016813 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016814 else
16815 {
16816 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016817 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016818 prevsize = grow50pc > growmin ? grow50pc : growmin;
16819 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016820 newprev = prev == NULL ? alloc(prevsize)
16821 : vim_realloc(prev, prevsize);
16822 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016823 {
16824 do_outofmem_msg((long_u)prevsize);
16825 failed = TRUE;
16826 break;
16827 }
16828 prev = newprev;
16829 }
16830 /* Add the line part to end of "prev". */
16831 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016832 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016833 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016834 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016835
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016836 /*
16837 * For a negative line count use only the lines at the end of the file,
16838 * free the rest.
16839 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016840 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016841 while (cnt > -maxline)
16842 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016843 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016844 --cnt;
16845 }
16846
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016847 if (failed)
16848 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016849 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016850 /* readfile doc says an empty list is returned on error */
16851 rettv->vval.v_list = list_alloc();
16852 }
16853
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016854 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016855 fclose(fd);
16856}
16857
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016858#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016859static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016860
16861/*
16862 * Convert a List to proftime_T.
16863 * Return FAIL when there is something wrong.
16864 */
16865 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016866list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016867{
16868 long n1, n2;
16869 int error = FALSE;
16870
16871 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16872 || arg->vval.v_list->lv_len != 2)
16873 return FAIL;
16874 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16875 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16876# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016877 tm->HighPart = n1;
16878 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016879# else
16880 tm->tv_sec = n1;
16881 tm->tv_usec = n2;
16882# endif
16883 return error ? FAIL : OK;
16884}
16885#endif /* FEAT_RELTIME */
16886
16887/*
16888 * "reltime()" function
16889 */
16890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016891f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016892{
16893#ifdef FEAT_RELTIME
16894 proftime_T res;
16895 proftime_T start;
16896
16897 if (argvars[0].v_type == VAR_UNKNOWN)
16898 {
16899 /* No arguments: get current time. */
16900 profile_start(&res);
16901 }
16902 else if (argvars[1].v_type == VAR_UNKNOWN)
16903 {
16904 if (list2proftime(&argvars[0], &res) == FAIL)
16905 return;
16906 profile_end(&res);
16907 }
16908 else
16909 {
16910 /* Two arguments: compute the difference. */
16911 if (list2proftime(&argvars[0], &start) == FAIL
16912 || list2proftime(&argvars[1], &res) == FAIL)
16913 return;
16914 profile_sub(&res, &start);
16915 }
16916
16917 if (rettv_list_alloc(rettv) == OK)
16918 {
16919 long n1, n2;
16920
16921# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016922 n1 = res.HighPart;
16923 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016924# else
16925 n1 = res.tv_sec;
16926 n2 = res.tv_usec;
16927# endif
16928 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16929 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16930 }
16931#endif
16932}
16933
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016934#ifdef FEAT_FLOAT
16935/*
16936 * "reltimefloat()" function
16937 */
16938 static void
16939f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16940{
16941# ifdef FEAT_RELTIME
16942 proftime_T tm;
16943# endif
16944
16945 rettv->v_type = VAR_FLOAT;
16946 rettv->vval.v_float = 0;
16947# ifdef FEAT_RELTIME
16948 if (list2proftime(&argvars[0], &tm) == OK)
16949 rettv->vval.v_float = profile_float(&tm);
16950# endif
16951}
16952#endif
16953
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016954/*
16955 * "reltimestr()" function
16956 */
16957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016958f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016959{
16960#ifdef FEAT_RELTIME
16961 proftime_T tm;
16962#endif
16963
16964 rettv->v_type = VAR_STRING;
16965 rettv->vval.v_string = NULL;
16966#ifdef FEAT_RELTIME
16967 if (list2proftime(&argvars[0], &tm) == OK)
16968 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16969#endif
16970}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016971
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016973static void make_connection(void);
16974static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975
16976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016977make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978{
16979 if (X_DISPLAY == NULL
16980# ifdef FEAT_GUI
16981 && !gui.in_use
16982# endif
16983 )
16984 {
16985 x_force_connect = TRUE;
16986 setup_term_clip();
16987 x_force_connect = FALSE;
16988 }
16989}
16990
16991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016992check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993{
16994 make_connection();
16995 if (X_DISPLAY == NULL)
16996 {
16997 EMSG(_("E240: No connection to Vim server"));
16998 return FAIL;
16999 }
17000 return OK;
17001}
17002#endif
17003
17004#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017006remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007{
17008 char_u *server_name;
17009 char_u *keys;
17010 char_u *r = NULL;
17011 char_u buf[NUMBUFLEN];
17012# ifdef WIN32
17013 HWND w;
17014# else
17015 Window w;
17016# endif
17017
17018 if (check_restricted() || check_secure())
17019 return;
17020
17021# ifdef FEAT_X11
17022 if (check_connection() == FAIL)
17023 return;
17024# endif
17025
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017026 server_name = get_tv_string_chk(&argvars[0]);
17027 if (server_name == NULL)
17028 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017029 keys = get_tv_string_buf(&argvars[1], buf);
17030# ifdef WIN32
17031 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17032# else
17033 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17034 < 0)
17035# endif
17036 {
17037 if (r != NULL)
17038 EMSG(r); /* sending worked but evaluation failed */
17039 else
17040 EMSG2(_("E241: Unable to send to %s"), server_name);
17041 return;
17042 }
17043
17044 rettv->vval.v_string = r;
17045
17046 if (argvars[2].v_type != VAR_UNKNOWN)
17047 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017048 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017049 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017050 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017051
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017052 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017053 v.di_tv.v_type = VAR_STRING;
17054 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017055 idvar = get_tv_string_chk(&argvars[2]);
17056 if (idvar != NULL)
17057 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017058 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017059 }
17060}
17061#endif
17062
17063/*
17064 * "remote_expr()" function
17065 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017067f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068{
17069 rettv->v_type = VAR_STRING;
17070 rettv->vval.v_string = NULL;
17071#ifdef FEAT_CLIENTSERVER
17072 remote_common(argvars, rettv, TRUE);
17073#endif
17074}
17075
17076/*
17077 * "remote_foreground()" function
17078 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017080f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082#ifdef FEAT_CLIENTSERVER
17083# ifdef WIN32
17084 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017085 {
17086 char_u *server_name = get_tv_string_chk(&argvars[0]);
17087
17088 if (server_name != NULL)
17089 serverForeground(server_name);
17090 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091# else
17092 /* Send a foreground() expression to the server. */
17093 argvars[1].v_type = VAR_STRING;
17094 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17095 argvars[2].v_type = VAR_UNKNOWN;
17096 remote_common(argvars, rettv, TRUE);
17097 vim_free(argvars[1].vval.v_string);
17098# endif
17099#endif
17100}
17101
Bram Moolenaar0d660222005-01-07 21:51:51 +000017102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017103f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017104{
17105#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017106 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107 char_u *s = NULL;
17108# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017109 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017111 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017112
17113 if (check_restricted() || check_secure())
17114 {
17115 rettv->vval.v_number = -1;
17116 return;
17117 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017118 serverid = get_tv_string_chk(&argvars[0]);
17119 if (serverid == NULL)
17120 {
17121 rettv->vval.v_number = -1;
17122 return; /* type error; errmsg already given */
17123 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017125 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126 if (n == 0)
17127 rettv->vval.v_number = -1;
17128 else
17129 {
17130 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17131 rettv->vval.v_number = (s != NULL);
17132 }
17133# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017134 if (check_connection() == FAIL)
17135 return;
17136
17137 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017138 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017139# endif
17140
17141 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017143 char_u *retvar;
17144
Bram Moolenaar33570922005-01-25 22:26:29 +000017145 v.di_tv.v_type = VAR_STRING;
17146 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017147 retvar = get_tv_string_chk(&argvars[1]);
17148 if (retvar != NULL)
17149 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017150 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017151 }
17152#else
17153 rettv->vval.v_number = -1;
17154#endif
17155}
17156
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017158f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159{
17160 char_u *r = NULL;
17161
17162#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017163 char_u *serverid = get_tv_string_chk(&argvars[0]);
17164
17165 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166 {
17167# ifdef WIN32
17168 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017169 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017170
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017171 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017172 if (n != 0)
17173 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17174 if (r == NULL)
17175# else
17176 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017177 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017178# endif
17179 EMSG(_("E277: Unable to read a server reply"));
17180 }
17181#endif
17182 rettv->v_type = VAR_STRING;
17183 rettv->vval.v_string = r;
17184}
17185
17186/*
17187 * "remote_send()" function
17188 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017190f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191{
17192 rettv->v_type = VAR_STRING;
17193 rettv->vval.v_string = NULL;
17194#ifdef FEAT_CLIENTSERVER
17195 remote_common(argvars, rettv, FALSE);
17196#endif
17197}
17198
17199/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017200 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017201 */
17202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017203f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017204{
Bram Moolenaar33570922005-01-25 22:26:29 +000017205 list_T *l;
17206 listitem_T *item, *item2;
17207 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017208 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017209 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017210 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017211 dict_T *d;
17212 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017213 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017214
Bram Moolenaar8c711452005-01-14 21:53:12 +000017215 if (argvars[0].v_type == VAR_DICT)
17216 {
17217 if (argvars[2].v_type != VAR_UNKNOWN)
17218 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017219 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017220 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017222 key = get_tv_string_chk(&argvars[1]);
17223 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017225 di = dict_find(d, key, -1);
17226 if (di == NULL)
17227 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017228 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17229 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017230 {
17231 *rettv = di->di_tv;
17232 init_tv(&di->di_tv);
17233 dictitem_remove(d, di);
17234 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017235 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017236 }
17237 }
17238 else if (argvars[0].v_type != VAR_LIST)
17239 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017240 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017241 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017243 int error = FALSE;
17244
17245 idx = get_tv_number_chk(&argvars[1], &error);
17246 if (error)
17247 ; /* type error: do nothing, errmsg already given */
17248 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017249 EMSGN(_(e_listidx), idx);
17250 else
17251 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017252 if (argvars[2].v_type == VAR_UNKNOWN)
17253 {
17254 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017255 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017256 *rettv = item->li_tv;
17257 vim_free(item);
17258 }
17259 else
17260 {
17261 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017262 end = get_tv_number_chk(&argvars[2], &error);
17263 if (error)
17264 ; /* type error: do nothing */
17265 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017266 EMSGN(_(e_listidx), end);
17267 else
17268 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017269 int cnt = 0;
17270
17271 for (li = item; li != NULL; li = li->li_next)
17272 {
17273 ++cnt;
17274 if (li == item2)
17275 break;
17276 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017277 if (li == NULL) /* didn't find "item2" after "item" */
17278 EMSG(_(e_invrange));
17279 else
17280 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017281 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017282 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017283 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017284 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017285 l->lv_first = item;
17286 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017287 item->li_prev = NULL;
17288 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017289 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017290 }
17291 }
17292 }
17293 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017294 }
17295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296}
17297
17298/*
17299 * "rename({from}, {to})" function
17300 */
17301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017302f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303{
17304 char_u buf[NUMBUFLEN];
17305
17306 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017307 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017308 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017309 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17310 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017311}
17312
17313/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017314 * "repeat()" function
17315 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017317f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017318{
17319 char_u *p;
17320 int n;
17321 int slen;
17322 int len;
17323 char_u *r;
17324 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017325
17326 n = get_tv_number(&argvars[1]);
17327 if (argvars[0].v_type == VAR_LIST)
17328 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017329 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017330 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017331 if (list_extend(rettv->vval.v_list,
17332 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017333 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017334 }
17335 else
17336 {
17337 p = get_tv_string(&argvars[0]);
17338 rettv->v_type = VAR_STRING;
17339 rettv->vval.v_string = NULL;
17340
17341 slen = (int)STRLEN(p);
17342 len = slen * n;
17343 if (len <= 0)
17344 return;
17345
17346 r = alloc(len + 1);
17347 if (r != NULL)
17348 {
17349 for (i = 0; i < n; i++)
17350 mch_memmove(r + i * slen, p, (size_t)slen);
17351 r[len] = NUL;
17352 }
17353
17354 rettv->vval.v_string = r;
17355 }
17356}
17357
17358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 * "resolve()" function
17360 */
17361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017362f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363{
17364 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017365#ifdef HAVE_READLINK
17366 char_u *buf = NULL;
17367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017369 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370#ifdef FEAT_SHORTCUT
17371 {
17372 char_u *v = NULL;
17373
17374 v = mch_resolve_shortcut(p);
17375 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017376 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017378 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 }
17380#else
17381# ifdef HAVE_READLINK
17382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 char_u *cpy;
17384 int len;
17385 char_u *remain = NULL;
17386 char_u *q;
17387 int is_relative_to_current = FALSE;
17388 int has_trailing_pathsep = FALSE;
17389 int limit = 100;
17390
17391 p = vim_strsave(p);
17392
17393 if (p[0] == '.' && (vim_ispathsep(p[1])
17394 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17395 is_relative_to_current = TRUE;
17396
17397 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017398 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017401 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403
17404 q = getnextcomp(p);
17405 if (*q != NUL)
17406 {
17407 /* Separate the first path component in "p", and keep the
17408 * remainder (beginning with the path separator). */
17409 remain = vim_strsave(q - 1);
17410 q[-1] = NUL;
17411 }
17412
Bram Moolenaard9462e32011-04-11 21:35:11 +020017413 buf = alloc(MAXPATHL + 1);
17414 if (buf == NULL)
17415 goto fail;
17416
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 for (;;)
17418 {
17419 for (;;)
17420 {
17421 len = readlink((char *)p, (char *)buf, MAXPATHL);
17422 if (len <= 0)
17423 break;
17424 buf[len] = NUL;
17425
17426 if (limit-- == 0)
17427 {
17428 vim_free(p);
17429 vim_free(remain);
17430 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432 goto fail;
17433 }
17434
17435 /* Ensure that the result will have a trailing path separator
17436 * if the argument has one. */
17437 if (remain == NULL && has_trailing_pathsep)
17438 add_pathsep(buf);
17439
17440 /* Separate the first path component in the link value and
17441 * concatenate the remainders. */
17442 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17443 if (*q != NUL)
17444 {
17445 if (remain == NULL)
17446 remain = vim_strsave(q - 1);
17447 else
17448 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017449 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 if (cpy != NULL)
17451 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 vim_free(remain);
17453 remain = cpy;
17454 }
17455 }
17456 q[-1] = NUL;
17457 }
17458
17459 q = gettail(p);
17460 if (q > p && *q == NUL)
17461 {
17462 /* Ignore trailing path separator. */
17463 q[-1] = NUL;
17464 q = gettail(p);
17465 }
17466 if (q > p && !mch_isFullName(buf))
17467 {
17468 /* symlink is relative to directory of argument */
17469 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17470 if (cpy != NULL)
17471 {
17472 STRCPY(cpy, p);
17473 STRCPY(gettail(cpy), buf);
17474 vim_free(p);
17475 p = cpy;
17476 }
17477 }
17478 else
17479 {
17480 vim_free(p);
17481 p = vim_strsave(buf);
17482 }
17483 }
17484
17485 if (remain == NULL)
17486 break;
17487
17488 /* Append the first path component of "remain" to "p". */
17489 q = getnextcomp(remain + 1);
17490 len = q - remain - (*q != NUL);
17491 cpy = vim_strnsave(p, STRLEN(p) + len);
17492 if (cpy != NULL)
17493 {
17494 STRNCAT(cpy, remain, len);
17495 vim_free(p);
17496 p = cpy;
17497 }
17498 /* Shorten "remain". */
17499 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017500 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 else
17502 {
17503 vim_free(remain);
17504 remain = NULL;
17505 }
17506 }
17507
17508 /* If the result is a relative path name, make it explicitly relative to
17509 * the current directory if and only if the argument had this form. */
17510 if (!vim_ispathsep(*p))
17511 {
17512 if (is_relative_to_current
17513 && *p != NUL
17514 && !(p[0] == '.'
17515 && (p[1] == NUL
17516 || vim_ispathsep(p[1])
17517 || (p[1] == '.'
17518 && (p[2] == NUL
17519 || vim_ispathsep(p[2]))))))
17520 {
17521 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017522 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 if (cpy != NULL)
17524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017525 vim_free(p);
17526 p = cpy;
17527 }
17528 }
17529 else if (!is_relative_to_current)
17530 {
17531 /* Strip leading "./". */
17532 q = p;
17533 while (q[0] == '.' && vim_ispathsep(q[1]))
17534 q += 2;
17535 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017536 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 }
17538 }
17539
17540 /* Ensure that the result will have no trailing path separator
17541 * if the argument had none. But keep "/" or "//". */
17542 if (!has_trailing_pathsep)
17543 {
17544 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017545 if (after_pathsep(p, q))
17546 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547 }
17548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017549 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 }
17551# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017552 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553# endif
17554#endif
17555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017556 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557
17558#ifdef HAVE_READLINK
17559fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017560 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017562 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563}
17564
17565/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017566 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 */
17568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017569f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570{
Bram Moolenaar33570922005-01-25 22:26:29 +000017571 list_T *l;
17572 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573
Bram Moolenaar0d660222005-01-07 21:51:51 +000017574 if (argvars[0].v_type != VAR_LIST)
17575 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017576 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017577 && !tv_check_lock(l->lv_lock,
17578 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579 {
17580 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017581 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017582 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017583 while (li != NULL)
17584 {
17585 ni = li->li_prev;
17586 list_append(l, li);
17587 li = ni;
17588 }
17589 rettv->vval.v_list = l;
17590 rettv->v_type = VAR_LIST;
17591 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017592 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017594}
17595
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017596#define SP_NOMOVE 0x01 /* don't move cursor */
17597#define SP_REPEAT 0x02 /* repeat to find outer pair */
17598#define SP_RETCOUNT 0x04 /* return matchcount */
17599#define SP_SETPCMARK 0x08 /* set previous context mark */
17600#define SP_START 0x10 /* accept match at start position */
17601#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17602#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017603#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017604
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017605static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017606
17607/*
17608 * Get flags for a search function.
17609 * Possibly sets "p_ws".
17610 * Returns BACKWARD, FORWARD or zero (for an error).
17611 */
17612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017613get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017614{
17615 int dir = FORWARD;
17616 char_u *flags;
17617 char_u nbuf[NUMBUFLEN];
17618 int mask;
17619
17620 if (varp->v_type != VAR_UNKNOWN)
17621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017622 flags = get_tv_string_buf_chk(varp, nbuf);
17623 if (flags == NULL)
17624 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017625 while (*flags != NUL)
17626 {
17627 switch (*flags)
17628 {
17629 case 'b': dir = BACKWARD; break;
17630 case 'w': p_ws = TRUE; break;
17631 case 'W': p_ws = FALSE; break;
17632 default: mask = 0;
17633 if (flagsp != NULL)
17634 switch (*flags)
17635 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017636 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017637 case 'e': mask = SP_END; break;
17638 case 'm': mask = SP_RETCOUNT; break;
17639 case 'n': mask = SP_NOMOVE; break;
17640 case 'p': mask = SP_SUBPAT; break;
17641 case 'r': mask = SP_REPEAT; break;
17642 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017643 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017644 }
17645 if (mask == 0)
17646 {
17647 EMSG2(_(e_invarg2), flags);
17648 dir = 0;
17649 }
17650 else
17651 *flagsp |= mask;
17652 }
17653 if (dir == 0)
17654 break;
17655 ++flags;
17656 }
17657 }
17658 return dir;
17659}
17660
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017662 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017664 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017665search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017667 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 char_u *pat;
17669 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017670 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017671 int save_p_ws = p_ws;
17672 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017673 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017674 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017675 proftime_T tm;
17676#ifdef FEAT_RELTIME
17677 long time_limit = 0;
17678#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017679 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017680 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017682 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017683 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017684 if (dir == 0)
17685 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017686 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017687 if (flags & SP_START)
17688 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017689 if (flags & SP_END)
17690 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017691 if (flags & SP_COLUMN)
17692 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017693
Bram Moolenaar76929292008-01-06 19:07:36 +000017694 /* Optional arguments: line number to stop searching and timeout. */
17695 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017696 {
17697 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17698 if (lnum_stop < 0)
17699 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017700#ifdef FEAT_RELTIME
17701 if (argvars[3].v_type != VAR_UNKNOWN)
17702 {
17703 time_limit = get_tv_number_chk(&argvars[3], NULL);
17704 if (time_limit < 0)
17705 goto theend;
17706 }
17707#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017708 }
17709
Bram Moolenaar76929292008-01-06 19:07:36 +000017710#ifdef FEAT_RELTIME
17711 /* Set the time limit, if there is one. */
17712 profile_setlimit(time_limit, &tm);
17713#endif
17714
Bram Moolenaar231334e2005-07-25 20:46:57 +000017715 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017716 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017717 * Check to make sure only those flags are set.
17718 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17719 * flags cannot be set. Check for that condition also.
17720 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017721 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017722 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017724 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017725 goto theend;
17726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017728 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017729 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017730 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017731 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017733 if (flags & SP_SUBPAT)
17734 retval = subpatnum;
17735 else
17736 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017737 if (flags & SP_SETPCMARK)
17738 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017740 if (match_pos != NULL)
17741 {
17742 /* Store the match cursor position */
17743 match_pos->lnum = pos.lnum;
17744 match_pos->col = pos.col + 1;
17745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 /* "/$" will put the cursor after the end of the line, may need to
17747 * correct that here */
17748 check_cursor();
17749 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017750
17751 /* If 'n' flag is used: restore cursor position. */
17752 if (flags & SP_NOMOVE)
17753 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017754 else
17755 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017756theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017758
17759 return retval;
17760}
17761
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017762#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017763
17764/*
17765 * round() is not in C90, use ceil() or floor() instead.
17766 */
17767 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017768vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017769{
17770 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17771}
17772
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017773/*
17774 * "round({float})" function
17775 */
17776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017777f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017778{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017779 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017780
17781 rettv->v_type = VAR_FLOAT;
17782 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017783 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017784 else
17785 rettv->vval.v_float = 0.0;
17786}
17787#endif
17788
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017789/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017790 * "screenattr()" function
17791 */
17792 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017793f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017794{
17795 int row;
17796 int col;
17797 int c;
17798
17799 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17800 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17801 if (row < 0 || row >= screen_Rows
17802 || col < 0 || col >= screen_Columns)
17803 c = -1;
17804 else
17805 c = ScreenAttrs[LineOffset[row] + col];
17806 rettv->vval.v_number = c;
17807}
17808
17809/*
17810 * "screenchar()" function
17811 */
17812 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017813f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017814{
17815 int row;
17816 int col;
17817 int off;
17818 int c;
17819
17820 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17821 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17822 if (row < 0 || row >= screen_Rows
17823 || col < 0 || col >= screen_Columns)
17824 c = -1;
17825 else
17826 {
17827 off = LineOffset[row] + col;
17828#ifdef FEAT_MBYTE
17829 if (enc_utf8 && ScreenLinesUC[off] != 0)
17830 c = ScreenLinesUC[off];
17831 else
17832#endif
17833 c = ScreenLines[off];
17834 }
17835 rettv->vval.v_number = c;
17836}
17837
17838/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017839 * "screencol()" function
17840 *
17841 * First column is 1 to be consistent with virtcol().
17842 */
17843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017844f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017845{
17846 rettv->vval.v_number = screen_screencol() + 1;
17847}
17848
17849/*
17850 * "screenrow()" function
17851 */
17852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017853f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017854{
17855 rettv->vval.v_number = screen_screenrow() + 1;
17856}
17857
17858/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017859 * "search()" function
17860 */
17861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017862f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017863{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017864 int flags = 0;
17865
17866 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867}
17868
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017870 * "searchdecl()" function
17871 */
17872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017873f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017874{
17875 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017876 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017877 int error = FALSE;
17878 char_u *name;
17879
17880 rettv->vval.v_number = 1; /* default: FAIL */
17881
17882 name = get_tv_string_chk(&argvars[0]);
17883 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017884 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017885 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017886 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17887 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17888 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017889 if (!error && name != NULL)
17890 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017891 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017892}
17893
17894/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017895 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017897 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017898searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899{
17900 char_u *spat, *mpat, *epat;
17901 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 int dir;
17904 int flags = 0;
17905 char_u nbuf1[NUMBUFLEN];
17906 char_u nbuf2[NUMBUFLEN];
17907 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017908 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017909 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017910 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017913 spat = get_tv_string_chk(&argvars[0]);
17914 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17915 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17916 if (spat == NULL || mpat == NULL || epat == NULL)
17917 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919 /* Handle the optional fourth argument: flags */
17920 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017921 if (dir == 0)
17922 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017923
17924 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017925 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17926 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017927 if ((flags & (SP_END | SP_SUBPAT)) != 0
17928 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017929 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017930 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017931 goto theend;
17932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017934 /* Using 'r' implies 'W', otherwise it doesn't work. */
17935 if (flags & SP_REPEAT)
17936 p_ws = FALSE;
17937
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017938 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017939 if (argvars[3].v_type == VAR_UNKNOWN
17940 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017941 skip = (char_u *)"";
17942 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017943 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017944 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017945 if (argvars[5].v_type != VAR_UNKNOWN)
17946 {
17947 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17948 if (lnum_stop < 0)
17949 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017950#ifdef FEAT_RELTIME
17951 if (argvars[6].v_type != VAR_UNKNOWN)
17952 {
17953 time_limit = get_tv_number_chk(&argvars[6], NULL);
17954 if (time_limit < 0)
17955 goto theend;
17956 }
17957#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017958 }
17959 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017960 if (skip == NULL)
17961 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017963 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017964 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017965
17966theend:
17967 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017968
17969 return retval;
17970}
17971
17972/*
17973 * "searchpair()" function
17974 */
17975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017976f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017977{
17978 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17979}
17980
17981/*
17982 * "searchpairpos()" function
17983 */
17984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017985f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017986{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017987 pos_T match_pos;
17988 int lnum = 0;
17989 int col = 0;
17990
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017991 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017992 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017993
17994 if (searchpair_cmn(argvars, &match_pos) > 0)
17995 {
17996 lnum = match_pos.lnum;
17997 col = match_pos.col;
17998 }
17999
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018000 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18001 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018002}
18003
18004/*
18005 * Search for a start/middle/end thing.
18006 * Used by searchpair(), see its documentation for the details.
18007 * Returns 0 or -1 for no match,
18008 */
18009 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018010do_searchpair(
18011 char_u *spat, /* start pattern */
18012 char_u *mpat, /* middle pattern */
18013 char_u *epat, /* end pattern */
18014 int dir, /* BACKWARD or FORWARD */
18015 char_u *skip, /* skip expression */
18016 int flags, /* SP_SETPCMARK and other SP_ values */
18017 pos_T *match_pos,
18018 linenr_T lnum_stop, /* stop at this line if not zero */
18019 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018020{
18021 char_u *save_cpo;
18022 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18023 long retval = 0;
18024 pos_T pos;
18025 pos_T firstpos;
18026 pos_T foundpos;
18027 pos_T save_cursor;
18028 pos_T save_pos;
18029 int n;
18030 int r;
18031 int nest = 1;
18032 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018033 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018034 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018035
18036 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18037 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018038 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018039
Bram Moolenaar76929292008-01-06 19:07:36 +000018040#ifdef FEAT_RELTIME
18041 /* Set the time limit, if there is one. */
18042 profile_setlimit(time_limit, &tm);
18043#endif
18044
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018045 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18046 * start/middle/end (pat3, for the top pair). */
18047 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18048 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18049 if (pat2 == NULL || pat3 == NULL)
18050 goto theend;
18051 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18052 if (*mpat == NUL)
18053 STRCPY(pat3, pat2);
18054 else
18055 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18056 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018057 if (flags & SP_START)
18058 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018059
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 save_cursor = curwin->w_cursor;
18061 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018062 clearpos(&firstpos);
18063 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 pat = pat3;
18065 for (;;)
18066 {
18067 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018068 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18070 /* didn't find it or found the first match again: FAIL */
18071 break;
18072
18073 if (firstpos.lnum == 0)
18074 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018075 if (equalpos(pos, foundpos))
18076 {
18077 /* Found the same position again. Can happen with a pattern that
18078 * has "\zs" at the end and searching backwards. Advance one
18079 * character and try again. */
18080 if (dir == BACKWARD)
18081 decl(&pos);
18082 else
18083 incl(&pos);
18084 }
18085 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018087 /* clear the start flag to avoid getting stuck here */
18088 options &= ~SEARCH_START;
18089
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090 /* If the skip pattern matches, ignore this match. */
18091 if (*skip != NUL)
18092 {
18093 save_pos = curwin->w_cursor;
18094 curwin->w_cursor = pos;
18095 r = eval_to_bool(skip, &err, NULL, FALSE);
18096 curwin->w_cursor = save_pos;
18097 if (err)
18098 {
18099 /* Evaluating {skip} caused an error, break here. */
18100 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018101 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 break;
18103 }
18104 if (r)
18105 continue;
18106 }
18107
18108 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18109 {
18110 /* Found end when searching backwards or start when searching
18111 * forward: nested pair. */
18112 ++nest;
18113 pat = pat2; /* nested, don't search for middle */
18114 }
18115 else
18116 {
18117 /* Found end when searching forward or start when searching
18118 * backward: end of (nested) pair; or found middle in outer pair. */
18119 if (--nest == 1)
18120 pat = pat3; /* outer level, search for middle */
18121 }
18122
18123 if (nest == 0)
18124 {
18125 /* Found the match: return matchcount or line number. */
18126 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018127 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018129 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018130 if (flags & SP_SETPCMARK)
18131 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132 curwin->w_cursor = pos;
18133 if (!(flags & SP_REPEAT))
18134 break;
18135 nest = 1; /* search for next unmatched */
18136 }
18137 }
18138
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018139 if (match_pos != NULL)
18140 {
18141 /* Store the match cursor position */
18142 match_pos->lnum = curwin->w_cursor.lnum;
18143 match_pos->col = curwin->w_cursor.col + 1;
18144 }
18145
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018147 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 curwin->w_cursor = save_cursor;
18149
18150theend:
18151 vim_free(pat2);
18152 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018153 if (p_cpo == empty_option)
18154 p_cpo = save_cpo;
18155 else
18156 /* Darn, evaluating the {skip} expression changed the value. */
18157 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018158
18159 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018160}
18161
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018162/*
18163 * "searchpos()" function
18164 */
18165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018166f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018167{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018168 pos_T match_pos;
18169 int lnum = 0;
18170 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018171 int n;
18172 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018173
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018174 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018175 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018176
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018177 n = search_cmn(argvars, &match_pos, &flags);
18178 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018179 {
18180 lnum = match_pos.lnum;
18181 col = match_pos.col;
18182 }
18183
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018184 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18185 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018186 if (flags & SP_SUBPAT)
18187 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018188}
18189
Bram Moolenaar0d660222005-01-07 21:51:51 +000018190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018191f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018193#ifdef FEAT_CLIENTSERVER
18194 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018195 char_u *server = get_tv_string_chk(&argvars[0]);
18196 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197
Bram Moolenaar0d660222005-01-07 21:51:51 +000018198 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018199 if (server == NULL || reply == NULL)
18200 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018201 if (check_restricted() || check_secure())
18202 return;
18203# ifdef FEAT_X11
18204 if (check_connection() == FAIL)
18205 return;
18206# endif
18207
18208 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018210 EMSG(_("E258: Unable to send to client"));
18211 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018213 rettv->vval.v_number = 0;
18214#else
18215 rettv->vval.v_number = -1;
18216#endif
18217}
18218
Bram Moolenaar0d660222005-01-07 21:51:51 +000018219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018220f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018221{
18222 char_u *r = NULL;
18223
18224#ifdef FEAT_CLIENTSERVER
18225# ifdef WIN32
18226 r = serverGetVimNames();
18227# else
18228 make_connection();
18229 if (X_DISPLAY != NULL)
18230 r = serverGetVimNames(X_DISPLAY);
18231# endif
18232#endif
18233 rettv->v_type = VAR_STRING;
18234 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235}
18236
18237/*
18238 * "setbufvar()" function
18239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018241f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242{
18243 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018244 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018246 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 char_u nbuf[NUMBUFLEN];
18248
18249 if (check_restricted() || check_secure())
18250 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018251 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18252 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018253 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254 varp = &argvars[2];
18255
18256 if (buf != NULL && varname != NULL && varp != NULL)
18257 {
18258 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018260
18261 if (*varname == '&')
18262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018263 long numval;
18264 char_u *strval;
18265 int error = FALSE;
18266
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018268 numval = get_tv_number_chk(varp, &error);
18269 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018270 if (!error && strval != NULL)
18271 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 }
18273 else
18274 {
18275 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18276 if (bufvarname != NULL)
18277 {
18278 STRCPY(bufvarname, "b:");
18279 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018280 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281 vim_free(bufvarname);
18282 }
18283 }
18284
18285 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288}
18289
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018291f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018292{
18293 dict_T *d;
18294 dictitem_T *di;
18295 char_u *csearch;
18296
18297 if (argvars[0].v_type != VAR_DICT)
18298 {
18299 EMSG(_(e_dictreq));
18300 return;
18301 }
18302
18303 if ((d = argvars[0].vval.v_dict) != NULL)
18304 {
18305 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18306 if (csearch != NULL)
18307 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018308#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018309 if (enc_utf8)
18310 {
18311 int pcc[MAX_MCO];
18312 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018313
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018314 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18315 }
18316 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018317#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018318 set_last_csearch(PTR2CHAR(csearch),
18319 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018320 }
18321
18322 di = dict_find(d, (char_u *)"forward", -1);
18323 if (di != NULL)
18324 set_csearch_direction(get_tv_number(&di->di_tv)
18325 ? FORWARD : BACKWARD);
18326
18327 di = dict_find(d, (char_u *)"until", -1);
18328 if (di != NULL)
18329 set_csearch_until(!!get_tv_number(&di->di_tv));
18330 }
18331}
18332
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333/*
18334 * "setcmdpos()" function
18335 */
18336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018337f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018339 int pos = (int)get_tv_number(&argvars[0]) - 1;
18340
18341 if (pos >= 0)
18342 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343}
18344
18345/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018346 * "setfperm({fname}, {mode})" function
18347 */
18348 static void
18349f_setfperm(typval_T *argvars, typval_T *rettv)
18350{
18351 char_u *fname;
18352 char_u modebuf[NUMBUFLEN];
18353 char_u *mode_str;
18354 int i;
18355 int mask;
18356 int mode = 0;
18357
18358 rettv->vval.v_number = 0;
18359 fname = get_tv_string_chk(&argvars[0]);
18360 if (fname == NULL)
18361 return;
18362 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18363 if (mode_str == NULL)
18364 return;
18365 if (STRLEN(mode_str) != 9)
18366 {
18367 EMSG2(_(e_invarg2), mode_str);
18368 return;
18369 }
18370
18371 mask = 1;
18372 for (i = 8; i >= 0; --i)
18373 {
18374 if (mode_str[i] != '-')
18375 mode |= mask;
18376 mask = mask << 1;
18377 }
18378 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18379}
18380
18381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 * "setline()" function
18383 */
18384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018385f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386{
18387 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018388 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018389 list_T *l = NULL;
18390 listitem_T *li = NULL;
18391 long added = 0;
18392 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018394 lnum = get_tv_lnum(&argvars[0]);
18395 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018397 l = argvars[1].vval.v_list;
18398 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018400 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018401 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018402
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018403 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018404 for (;;)
18405 {
18406 if (l != NULL)
18407 {
18408 /* list argument, get next string */
18409 if (li == NULL)
18410 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018411 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018412 li = li->li_next;
18413 }
18414
18415 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018416 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018417 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018418
18419 /* When coming here from Insert mode, sync undo, so that this can be
18420 * undone separately from what was previously inserted. */
18421 if (u_sync_once == 2)
18422 {
18423 u_sync_once = 1; /* notify that u_sync() was called */
18424 u_sync(TRUE);
18425 }
18426
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018427 if (lnum <= curbuf->b_ml.ml_line_count)
18428 {
18429 /* existing line, replace it */
18430 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18431 {
18432 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018433 if (lnum == curwin->w_cursor.lnum)
18434 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018435 rettv->vval.v_number = 0; /* OK */
18436 }
18437 }
18438 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18439 {
18440 /* lnum is one past the last line, append the line */
18441 ++added;
18442 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18443 rettv->vval.v_number = 0; /* OK */
18444 }
18445
18446 if (l == NULL) /* only one string argument */
18447 break;
18448 ++lnum;
18449 }
18450
18451 if (added > 0)
18452 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018453}
18454
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018455static 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 +000018456
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018458 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018459 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018461set_qf_ll_list(
18462 win_T *wp UNUSED,
18463 typval_T *list_arg UNUSED,
18464 typval_T *action_arg UNUSED,
18465 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018466{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018467#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018468 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018469 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018470 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018471#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018472
Bram Moolenaar2641f772005-03-25 21:58:17 +000018473 rettv->vval.v_number = -1;
18474
18475#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018476 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018477 EMSG(_(e_listreq));
18478 else
18479 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018480 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018481
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018482 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018483 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018484 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018485 if (act == NULL)
18486 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018487 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018488 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018489 else
18490 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018491 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018492 else if (action_arg->v_type == VAR_UNKNOWN)
18493 action = ' ';
18494 else
18495 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018496
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018497 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018498 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018499 rettv->vval.v_number = 0;
18500 }
18501#endif
18502}
18503
18504/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018505 * "setloclist()" function
18506 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018508f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018509{
18510 win_T *win;
18511
18512 rettv->vval.v_number = -1;
18513
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018514 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018515 if (win != NULL)
18516 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18517}
18518
18519/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018520 * "setmatches()" function
18521 */
18522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018523f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018524{
18525#ifdef FEAT_SEARCH_EXTRA
18526 list_T *l;
18527 listitem_T *li;
18528 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018529 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018530
18531 rettv->vval.v_number = -1;
18532 if (argvars[0].v_type != VAR_LIST)
18533 {
18534 EMSG(_(e_listreq));
18535 return;
18536 }
18537 if ((l = argvars[0].vval.v_list) != NULL)
18538 {
18539
18540 /* To some extent make sure that we are dealing with a list from
18541 * "getmatches()". */
18542 li = l->lv_first;
18543 while (li != NULL)
18544 {
18545 if (li->li_tv.v_type != VAR_DICT
18546 || (d = li->li_tv.vval.v_dict) == NULL)
18547 {
18548 EMSG(_(e_invarg));
18549 return;
18550 }
18551 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018552 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18553 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018554 && dict_find(d, (char_u *)"priority", -1) != NULL
18555 && dict_find(d, (char_u *)"id", -1) != NULL))
18556 {
18557 EMSG(_(e_invarg));
18558 return;
18559 }
18560 li = li->li_next;
18561 }
18562
18563 clear_matches(curwin);
18564 li = l->lv_first;
18565 while (li != NULL)
18566 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018567 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018568 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018569 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018570 char_u *group;
18571 int priority;
18572 int id;
18573 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018574
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018575 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018576 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18577 {
18578 if (s == NULL)
18579 {
18580 s = list_alloc();
18581 if (s == NULL)
18582 return;
18583 }
18584
18585 /* match from matchaddpos() */
18586 for (i = 1; i < 9; i++)
18587 {
18588 sprintf((char *)buf, (char *)"pos%d", i);
18589 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18590 {
18591 if (di->di_tv.v_type != VAR_LIST)
18592 return;
18593
18594 list_append_tv(s, &di->di_tv);
18595 s->lv_refcount++;
18596 }
18597 else
18598 break;
18599 }
18600 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018601
18602 group = get_dict_string(d, (char_u *)"group", FALSE);
18603 priority = (int)get_dict_number(d, (char_u *)"priority");
18604 id = (int)get_dict_number(d, (char_u *)"id");
18605 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18606 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18607 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018608 if (i == 0)
18609 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018610 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018611 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018612 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018613 }
18614 else
18615 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018616 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018617 list_unref(s);
18618 s = NULL;
18619 }
18620
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018621 li = li->li_next;
18622 }
18623 rettv->vval.v_number = 0;
18624 }
18625#endif
18626}
18627
18628/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018629 * "setpos()" function
18630 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018632f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018633{
18634 pos_T pos;
18635 int fnum;
18636 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018637 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018638
Bram Moolenaar08250432008-02-13 11:42:46 +000018639 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018640 name = get_tv_string_chk(argvars);
18641 if (name != NULL)
18642 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018643 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018644 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018645 if (--pos.col < 0)
18646 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018647 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018648 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018649 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018650 if (fnum == curbuf->b_fnum)
18651 {
18652 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018653 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018654 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018655 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018656 curwin->w_set_curswant = FALSE;
18657 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018658 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018659 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018660 }
18661 else
18662 EMSG(_(e_invarg));
18663 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018664 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18665 {
18666 /* set mark */
18667 if (setmark_pos(name[1], &pos, fnum) == OK)
18668 rettv->vval.v_number = 0;
18669 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018670 else
18671 EMSG(_(e_invarg));
18672 }
18673 }
18674}
18675
18676/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018677 * "setqflist()" function
18678 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018680f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018681{
18682 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18683}
18684
18685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 * "setreg()" function
18687 */
18688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018689f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690{
18691 int regname;
18692 char_u *strregname;
18693 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018694 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 int append;
18696 char_u yank_type;
18697 long block_len;
18698
18699 block_len = -1;
18700 yank_type = MAUTO;
18701 append = FALSE;
18702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018703 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018706 if (strregname == NULL)
18707 return; /* type error; errmsg already given */
18708 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 if (regname == 0 || regname == '@')
18710 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018712 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018714 stropt = get_tv_string_chk(&argvars[2]);
18715 if (stropt == NULL)
18716 return; /* type error */
18717 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 switch (*stropt)
18719 {
18720 case 'a': case 'A': /* append */
18721 append = TRUE;
18722 break;
18723 case 'v': case 'c': /* character-wise selection */
18724 yank_type = MCHAR;
18725 break;
18726 case 'V': case 'l': /* line-wise selection */
18727 yank_type = MLINE;
18728 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729 case 'b': case Ctrl_V: /* block-wise selection */
18730 yank_type = MBLOCK;
18731 if (VIM_ISDIGIT(stropt[1]))
18732 {
18733 ++stropt;
18734 block_len = getdigits(&stropt) - 1;
18735 --stropt;
18736 }
18737 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 }
18739 }
18740
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018741 if (argvars[1].v_type == VAR_LIST)
18742 {
18743 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018744 char_u **allocval;
18745 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018746 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018747 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018748 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018749 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018750 int len;
18751
18752 /* If the list is NULL handle like an empty list. */
18753 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018754
Bram Moolenaar7d647822014-04-05 21:28:56 +020018755 /* First half: use for pointers to result lines; second half: use for
18756 * pointers to allocated copies. */
18757 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018758 if (lstval == NULL)
18759 return;
18760 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018761 allocval = lstval + len + 2;
18762 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018763
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018764 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018765 li = li->li_next)
18766 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018767 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018768 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018769 goto free_lstval;
18770 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018771 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018772 /* Need to make a copy, next get_tv_string_buf_chk() will
18773 * overwrite the string. */
18774 strval = vim_strsave(buf);
18775 if (strval == NULL)
18776 goto free_lstval;
18777 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018778 }
18779 *curval++ = strval;
18780 }
18781 *curval++ = NULL;
18782
18783 write_reg_contents_lst(regname, lstval, -1,
18784 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018785free_lstval:
18786 while (curallocval > allocval)
18787 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018788 vim_free(lstval);
18789 }
18790 else
18791 {
18792 strval = get_tv_string_chk(&argvars[1]);
18793 if (strval == NULL)
18794 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018795 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018797 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018798 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799}
18800
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018801/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018802 * "settabvar()" function
18803 */
18804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018805f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018806{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018807#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018808 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018809 tabpage_T *tp;
18810#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018811 char_u *varname, *tabvarname;
18812 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018813
18814 rettv->vval.v_number = 0;
18815
18816 if (check_restricted() || check_secure())
18817 return;
18818
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018819#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018820 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018821#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018822 varname = get_tv_string_chk(&argvars[1]);
18823 varp = &argvars[2];
18824
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018825 if (varname != NULL && varp != NULL
18826#ifdef FEAT_WINDOWS
18827 && tp != NULL
18828#endif
18829 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018830 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018831#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018832 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018833 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018834#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018835
18836 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18837 if (tabvarname != NULL)
18838 {
18839 STRCPY(tabvarname, "t:");
18840 STRCPY(tabvarname + 2, varname);
18841 set_var(tabvarname, varp, TRUE);
18842 vim_free(tabvarname);
18843 }
18844
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018845#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018846 /* Restore current tabpage */
18847 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018848 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018849#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018850 }
18851}
18852
18853/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018854 * "settabwinvar()" function
18855 */
18856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018857f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018858{
18859 setwinvar(argvars, rettv, 1);
18860}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861
18862/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018863 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018866f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018868 setwinvar(argvars, rettv, 0);
18869}
18870
18871/*
18872 * "setwinvar()" and "settabwinvar()" functions
18873 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018874
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018876setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018877{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878 win_T *win;
18879#ifdef FEAT_WINDOWS
18880 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018881 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018882 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018883#endif
18884 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018885 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018887 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888
18889 if (check_restricted() || check_secure())
18890 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018891
18892#ifdef FEAT_WINDOWS
18893 if (off == 1)
18894 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18895 else
18896 tp = curtab;
18897#endif
18898 win = find_win_by_nr(&argvars[off], tp);
18899 varname = get_tv_string_chk(&argvars[off + 1]);
18900 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901
18902 if (win != NULL && varname != NULL && varp != NULL)
18903 {
18904#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018905 need_switch_win = !(tp == curtab && win == curwin);
18906 if (!need_switch_win
18907 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018910 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018912 long numval;
18913 char_u *strval;
18914 int error = FALSE;
18915
18916 ++varname;
18917 numval = get_tv_number_chk(varp, &error);
18918 strval = get_tv_string_buf_chk(varp, nbuf);
18919 if (!error && strval != NULL)
18920 set_option_value(varname, numval, strval, OPT_LOCAL);
18921 }
18922 else
18923 {
18924 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18925 if (winvarname != NULL)
18926 {
18927 STRCPY(winvarname, "w:");
18928 STRCPY(winvarname + 2, varname);
18929 set_var(winvarname, varp, TRUE);
18930 vim_free(winvarname);
18931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 }
18933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018935 if (need_switch_win)
18936 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937#endif
18938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939}
18940
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018941#ifdef FEAT_CRYPT
18942/*
18943 * "sha256({string})" function
18944 */
18945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018946f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018947{
18948 char_u *p;
18949
18950 p = get_tv_string(&argvars[0]);
18951 rettv->vval.v_string = vim_strsave(
18952 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18953 rettv->v_type = VAR_STRING;
18954}
18955#endif /* FEAT_CRYPT */
18956
Bram Moolenaar071d4272004-06-13 20:20:40 +000018957/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018958 * "shellescape({string})" function
18959 */
18960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018961f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018962{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018963 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018964 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018965 rettv->v_type = VAR_STRING;
18966}
18967
18968/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018969 * shiftwidth() function
18970 */
18971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018972f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018973{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018974 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018975}
18976
18977/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018978 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979 */
18980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018981f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018983 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984
Bram Moolenaar0d660222005-01-07 21:51:51 +000018985 p = get_tv_string(&argvars[0]);
18986 rettv->vval.v_string = vim_strsave(p);
18987 simplify_filename(rettv->vval.v_string); /* simplify in place */
18988 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989}
18990
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018991#ifdef FEAT_FLOAT
18992/*
18993 * "sin()" function
18994 */
18995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018996f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018997{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018998 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018999
19000 rettv->v_type = VAR_FLOAT;
19001 if (get_float_arg(argvars, &f) == OK)
19002 rettv->vval.v_float = sin(f);
19003 else
19004 rettv->vval.v_float = 0.0;
19005}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019006
19007/*
19008 * "sinh()" function
19009 */
19010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019011f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019012{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019013 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019014
19015 rettv->v_type = VAR_FLOAT;
19016 if (get_float_arg(argvars, &f) == OK)
19017 rettv->vval.v_float = sinh(f);
19018 else
19019 rettv->vval.v_float = 0.0;
19020}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019021#endif
19022
Bram Moolenaar0d660222005-01-07 21:51:51 +000019023static int
19024#ifdef __BORLANDC__
19025 _RTLENTRYF
19026#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019027 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019028static int
19029#ifdef __BORLANDC__
19030 _RTLENTRYF
19031#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019032 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019033
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019034/* struct used in the array that's given to qsort() */
19035typedef struct
19036{
19037 listitem_T *item;
19038 int idx;
19039} sortItem_T;
19040
Bram Moolenaar0b962472016-02-22 22:51:33 +010019041/* struct storing information about current sort */
19042typedef struct
19043{
19044 int item_compare_ic;
19045 int item_compare_numeric;
19046 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019047#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019048 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019049#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019050 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019051 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019052 dict_T *item_compare_selfdict;
19053 int item_compare_func_err;
19054 int item_compare_keep_zero;
19055} sortinfo_T;
19056static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019057static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019058#define ITEM_COMPARE_FAIL 999
19059
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019061 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019063 static int
19064#ifdef __BORLANDC__
19065_RTLENTRYF
19066#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019067item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019069 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019070 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019071 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019072 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073 int res;
19074 char_u numbuf1[NUMBUFLEN];
19075 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019077 si1 = (sortItem_T *)s1;
19078 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019079 tv1 = &si1->item->li_tv;
19080 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019081
Bram Moolenaar0b962472016-02-22 22:51:33 +010019082 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019083 {
19084 long v1 = get_tv_number(tv1);
19085 long v2 = get_tv_number(tv2);
19086
19087 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19088 }
19089
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019090#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019091 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019092 {
19093 float_T v1 = get_tv_float(tv1);
19094 float_T v2 = get_tv_float(tv2);
19095
19096 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19097 }
19098#endif
19099
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019100 /* tv2string() puts quotes around a string and allocates memory. Don't do
19101 * that for string variables. Use a single quote when comparing with a
19102 * non-string to do what the docs promise. */
19103 if (tv1->v_type == VAR_STRING)
19104 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019105 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019106 p1 = (char_u *)"'";
19107 else
19108 p1 = tv1->vval.v_string;
19109 }
19110 else
19111 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19112 if (tv2->v_type == VAR_STRING)
19113 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019115 p2 = (char_u *)"'";
19116 else
19117 p2 = tv2->vval.v_string;
19118 }
19119 else
19120 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019121 if (p1 == NULL)
19122 p1 = (char_u *)"";
19123 if (p2 == NULL)
19124 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019125 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019126 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019127 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019128 res = STRICMP(p1, p2);
19129 else
19130 res = STRCMP(p1, p2);
19131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019133 {
19134 double n1, n2;
19135 n1 = strtod((char *)p1, (char **)&p1);
19136 n2 = strtod((char *)p2, (char **)&p2);
19137 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19138 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019139
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019140 /* When the result would be zero, compare the item indexes. Makes the
19141 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019142 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019143 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019144
Bram Moolenaar0d660222005-01-07 21:51:51 +000019145 vim_free(tofree1);
19146 vim_free(tofree2);
19147 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148}
19149
19150 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019151#ifdef __BORLANDC__
19152_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019154item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019155{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019156 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019157 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019158 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019159 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019160 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019161 char_u *func_name;
19162 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019164 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019165 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019166 return 0;
19167
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019168 si1 = (sortItem_T *)s1;
19169 si2 = (sortItem_T *)s2;
19170
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019171 if (partial == NULL)
19172 func_name = sortinfo->item_compare_func;
19173 else
19174 func_name = partial->pt_name;
19175
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019176 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019177 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019178 copy_tv(&si1->item->li_tv, &argv[0]);
19179 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019180
19181 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019182 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019183 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019184 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019185 clear_tv(&argv[0]);
19186 clear_tv(&argv[1]);
19187
19188 if (res == FAIL)
19189 res = ITEM_COMPARE_FAIL;
19190 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019191 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19192 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019193 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019194 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019195
19196 /* When the result would be zero, compare the pointers themselves. Makes
19197 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019198 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019199 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019200
Bram Moolenaar0d660222005-01-07 21:51:51 +000019201 return res;
19202}
19203
19204/*
19205 * "sort({list})" function
19206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019208do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209{
Bram Moolenaar33570922005-01-25 22:26:29 +000019210 list_T *l;
19211 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019212 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019213 sortinfo_T *old_sortinfo;
19214 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019215 long len;
19216 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217
Bram Moolenaar0b962472016-02-22 22:51:33 +010019218 /* Pointer to current info struct used in compare function. Save and
19219 * restore the current one for nested calls. */
19220 old_sortinfo = sortinfo;
19221 sortinfo = &info;
19222
Bram Moolenaar0d660222005-01-07 21:51:51 +000019223 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019224 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 else
19226 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019227 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019228 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019229 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19230 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019231 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019232 rettv->vval.v_list = l;
19233 rettv->v_type = VAR_LIST;
19234 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235
Bram Moolenaar0d660222005-01-07 21:51:51 +000019236 len = list_len(l);
19237 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019238 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239
Bram Moolenaar0b962472016-02-22 22:51:33 +010019240 info.item_compare_ic = FALSE;
19241 info.item_compare_numeric = FALSE;
19242 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019243#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019244 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019245#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019246 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019247 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019248 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019249 if (argvars[1].v_type != VAR_UNKNOWN)
19250 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019251 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019252 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019253 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019254 else if (argvars[1].v_type == VAR_PARTIAL)
19255 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019256 else
19257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019258 int error = FALSE;
19259
19260 i = get_tv_number_chk(&argvars[1], &error);
19261 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019262 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019263 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019264 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019265 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019266 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019267 else if (i != 0)
19268 {
19269 EMSG(_(e_invarg));
19270 goto theend;
19271 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019272 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019273 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019274 if (*info.item_compare_func == NUL)
19275 {
19276 /* empty string means default sort */
19277 info.item_compare_func = NULL;
19278 }
19279 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019280 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019281 info.item_compare_func = NULL;
19282 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019283 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019284 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019285 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019286 info.item_compare_func = NULL;
19287 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019288 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019289#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019290 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019291 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019292 info.item_compare_func = NULL;
19293 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019294 }
19295#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019296 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019297 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019298 info.item_compare_func = NULL;
19299 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019300 }
19301 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019302 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019303
19304 if (argvars[2].v_type != VAR_UNKNOWN)
19305 {
19306 /* optional third argument: {dict} */
19307 if (argvars[2].v_type != VAR_DICT)
19308 {
19309 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019310 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019311 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019312 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019313 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315
Bram Moolenaar0d660222005-01-07 21:51:51 +000019316 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019317 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019318 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019319 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320
Bram Moolenaar327aa022014-03-25 18:24:23 +010019321 i = 0;
19322 if (sort)
19323 {
19324 /* sort(): ptrs will be the list to sort */
19325 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019326 {
19327 ptrs[i].item = li;
19328 ptrs[i].idx = i;
19329 ++i;
19330 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019331
Bram Moolenaar0b962472016-02-22 22:51:33 +010019332 info.item_compare_func_err = FALSE;
19333 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019334 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019335 if ((info.item_compare_func != NULL
19336 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019337 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019338 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019339 EMSG(_("E702: Sort compare function failed"));
19340 else
19341 {
19342 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019343 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019344 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019345 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019346 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019347
Bram Moolenaar0b962472016-02-22 22:51:33 +010019348 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019349 {
19350 /* Clear the List and append the items in sorted order. */
19351 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19352 l->lv_len = 0;
19353 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019354 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019355 }
19356 }
19357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019359 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019360 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019361
19362 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019363 info.item_compare_func_err = FALSE;
19364 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019365 item_compare_func_ptr = info.item_compare_func != NULL
19366 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019367 ? item_compare2 : item_compare;
19368
19369 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19370 li = li->li_next)
19371 {
19372 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19373 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019374 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019375 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019376 {
19377 EMSG(_("E882: Uniq compare function failed"));
19378 break;
19379 }
19380 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019381
Bram Moolenaar0b962472016-02-22 22:51:33 +010019382 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019383 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019384 while (--i >= 0)
19385 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019386 li = ptrs[i].item->li_next;
19387 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019388 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019389 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019390 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019391 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019392 list_fix_watch(l, li);
19393 listitem_free(li);
19394 l->lv_len--;
19395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019396 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019397 }
19398
19399 vim_free(ptrs);
19400 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019401theend:
19402 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019403}
19404
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019405/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019406 * "sort({list})" function
19407 */
19408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019409f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019410{
19411 do_sort_uniq(argvars, rettv, TRUE);
19412}
19413
19414/*
19415 * "uniq({list})" function
19416 */
19417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019418f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019419{
19420 do_sort_uniq(argvars, rettv, FALSE);
19421}
19422
19423/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019424 * "soundfold({word})" function
19425 */
19426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019427f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019428{
19429 char_u *s;
19430
19431 rettv->v_type = VAR_STRING;
19432 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019433#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019434 rettv->vval.v_string = eval_soundfold(s);
19435#else
19436 rettv->vval.v_string = vim_strsave(s);
19437#endif
19438}
19439
19440/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019441 * "spellbadword()" function
19442 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019444f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019445{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019446 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019447 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019448 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019449
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019450 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019451 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019452
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019453#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019454 if (argvars[0].v_type == VAR_UNKNOWN)
19455 {
19456 /* Find the start and length of the badly spelled word. */
19457 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19458 if (len != 0)
19459 word = ml_get_cursor();
19460 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019461 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019462 {
19463 char_u *str = get_tv_string_chk(&argvars[0]);
19464 int capcol = -1;
19465
19466 if (str != NULL)
19467 {
19468 /* Check the argument for spelling. */
19469 while (*str != NUL)
19470 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019471 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019472 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019473 {
19474 word = str;
19475 break;
19476 }
19477 str += len;
19478 }
19479 }
19480 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019481#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019482
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019483 list_append_string(rettv->vval.v_list, word, len);
19484 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019485 attr == HLF_SPB ? "bad" :
19486 attr == HLF_SPR ? "rare" :
19487 attr == HLF_SPL ? "local" :
19488 attr == HLF_SPC ? "caps" :
19489 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019490}
19491
19492/*
19493 * "spellsuggest()" function
19494 */
19495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019496f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019497{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019498#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019499 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019500 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019501 int maxcount;
19502 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019503 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019504 listitem_T *li;
19505 int need_capital = FALSE;
19506#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019507
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019508 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019509 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019510
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019511#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019512 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019513 {
19514 str = get_tv_string(&argvars[0]);
19515 if (argvars[1].v_type != VAR_UNKNOWN)
19516 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019517 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019518 if (maxcount <= 0)
19519 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019520 if (argvars[2].v_type != VAR_UNKNOWN)
19521 {
19522 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19523 if (typeerr)
19524 return;
19525 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019526 }
19527 else
19528 maxcount = 25;
19529
Bram Moolenaar4770d092006-01-12 23:22:24 +000019530 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019531
19532 for (i = 0; i < ga.ga_len; ++i)
19533 {
19534 str = ((char_u **)ga.ga_data)[i];
19535
19536 li = listitem_alloc();
19537 if (li == NULL)
19538 vim_free(str);
19539 else
19540 {
19541 li->li_tv.v_type = VAR_STRING;
19542 li->li_tv.v_lock = 0;
19543 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019544 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019545 }
19546 }
19547 ga_clear(&ga);
19548 }
19549#endif
19550}
19551
Bram Moolenaar0d660222005-01-07 21:51:51 +000019552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019553f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019554{
19555 char_u *str;
19556 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019557 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019558 regmatch_T regmatch;
19559 char_u patbuf[NUMBUFLEN];
19560 char_u *save_cpo;
19561 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019562 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019563 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019564 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019565
19566 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19567 save_cpo = p_cpo;
19568 p_cpo = (char_u *)"";
19569
19570 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019571 if (argvars[1].v_type != VAR_UNKNOWN)
19572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019573 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19574 if (pat == NULL)
19575 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019576 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019577 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019578 }
19579 if (pat == NULL || *pat == NUL)
19580 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019581
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019582 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019584 if (typeerr)
19585 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586
Bram Moolenaar0d660222005-01-07 21:51:51 +000019587 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19588 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019590 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019591 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019592 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019593 if (*str == NUL)
19594 match = FALSE; /* empty item at the end */
19595 else
19596 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019597 if (match)
19598 end = regmatch.startp[0];
19599 else
19600 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019601 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19602 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019603 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019604 if (list_append_string(rettv->vval.v_list, str,
19605 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019606 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019607 }
19608 if (!match)
19609 break;
19610 /* Advance to just after the match. */
19611 if (regmatch.endp[0] > str)
19612 col = 0;
19613 else
19614 {
19615 /* Don't get stuck at the same match. */
19616#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019617 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019618#else
19619 col = 1;
19620#endif
19621 }
19622 str = regmatch.endp[0];
19623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624
Bram Moolenaar473de612013-06-08 18:19:48 +020019625 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627
Bram Moolenaar0d660222005-01-07 21:51:51 +000019628 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629}
19630
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019631#ifdef FEAT_FLOAT
19632/*
19633 * "sqrt()" function
19634 */
19635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019636f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019637{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019638 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019639
19640 rettv->v_type = VAR_FLOAT;
19641 if (get_float_arg(argvars, &f) == OK)
19642 rettv->vval.v_float = sqrt(f);
19643 else
19644 rettv->vval.v_float = 0.0;
19645}
19646
19647/*
19648 * "str2float()" function
19649 */
19650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019651f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019652{
19653 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19654
19655 if (*p == '+')
19656 p = skipwhite(p + 1);
19657 (void)string2float(p, &rettv->vval.v_float);
19658 rettv->v_type = VAR_FLOAT;
19659}
19660#endif
19661
Bram Moolenaar2c932302006-03-18 21:42:09 +000019662/*
19663 * "str2nr()" function
19664 */
19665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019666f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019667{
19668 int base = 10;
19669 char_u *p;
19670 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019671 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019672
19673 if (argvars[1].v_type != VAR_UNKNOWN)
19674 {
19675 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019676 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019677 {
19678 EMSG(_(e_invarg));
19679 return;
19680 }
19681 }
19682
19683 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019684 if (*p == '+')
19685 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019686 switch (base)
19687 {
19688 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19689 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19690 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19691 default: what = 0;
19692 }
19693 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019694 rettv->vval.v_number = n;
19695}
19696
Bram Moolenaar071d4272004-06-13 20:20:40 +000019697#ifdef HAVE_STRFTIME
19698/*
19699 * "strftime({format}[, {time}])" function
19700 */
19701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019702f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703{
19704 char_u result_buf[256];
19705 struct tm *curtime;
19706 time_t seconds;
19707 char_u *p;
19708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019709 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019711 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019712 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 seconds = time(NULL);
19714 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019715 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 curtime = localtime(&seconds);
19717 /* MSVC returns NULL for an invalid value of seconds. */
19718 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019719 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 else
19721 {
19722# ifdef FEAT_MBYTE
19723 vimconv_T conv;
19724 char_u *enc;
19725
19726 conv.vc_type = CONV_NONE;
19727 enc = enc_locale();
19728 convert_setup(&conv, p_enc, enc);
19729 if (conv.vc_type != CONV_NONE)
19730 p = string_convert(&conv, p, NULL);
19731# endif
19732 if (p != NULL)
19733 (void)strftime((char *)result_buf, sizeof(result_buf),
19734 (char *)p, curtime);
19735 else
19736 result_buf[0] = NUL;
19737
19738# ifdef FEAT_MBYTE
19739 if (conv.vc_type != CONV_NONE)
19740 vim_free(p);
19741 convert_setup(&conv, enc, p_enc);
19742 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019743 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 else
19745# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747
19748# ifdef FEAT_MBYTE
19749 /* Release conversion descriptors */
19750 convert_setup(&conv, NULL, NULL);
19751 vim_free(enc);
19752# endif
19753 }
19754}
19755#endif
19756
19757/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019758 * "strgetchar()" function
19759 */
19760 static void
19761f_strgetchar(typval_T *argvars, typval_T *rettv)
19762{
19763 char_u *str;
19764 int len;
19765 int error = FALSE;
19766 int charidx;
19767
19768 rettv->vval.v_number = -1;
19769 str = get_tv_string_chk(&argvars[0]);
19770 if (str == NULL)
19771 return;
19772 len = (int)STRLEN(str);
19773 charidx = get_tv_number_chk(&argvars[1], &error);
19774 if (error)
19775 return;
19776#ifdef FEAT_MBYTE
19777 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019778 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019779
19780 while (charidx >= 0 && byteidx < len)
19781 {
19782 if (charidx == 0)
19783 {
19784 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19785 break;
19786 }
19787 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019788 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019789 }
19790 }
19791#else
19792 if (charidx < len)
19793 rettv->vval.v_number = str[charidx];
19794#endif
19795}
19796
19797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 * "stridx()" function
19799 */
19800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019801f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802{
19803 char_u buf[NUMBUFLEN];
19804 char_u *needle;
19805 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019808 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019810 needle = get_tv_string_chk(&argvars[1]);
19811 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019812 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019813 if (needle == NULL || haystack == NULL)
19814 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815
Bram Moolenaar33570922005-01-25 22:26:29 +000019816 if (argvars[2].v_type != VAR_UNKNOWN)
19817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019818 int error = FALSE;
19819
19820 start_idx = get_tv_number_chk(&argvars[2], &error);
19821 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019822 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019823 if (start_idx >= 0)
19824 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019825 }
19826
19827 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19828 if (pos != NULL)
19829 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830}
19831
19832/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019833 * "string()" function
19834 */
19835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019836f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019837{
19838 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019839 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019841 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019842 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19843 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019844 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019845 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019846 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847}
19848
19849/*
19850 * "strlen()" function
19851 */
19852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019853f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019855 rettv->vval.v_number = (varnumber_T)(STRLEN(
19856 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857}
19858
19859/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019860 * "strchars()" function
19861 */
19862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019863f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019864{
19865 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019866 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019867#ifdef FEAT_MBYTE
19868 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019869 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019870#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019871
19872 if (argvars[1].v_type != VAR_UNKNOWN)
19873 skipcc = get_tv_number_chk(&argvars[1], NULL);
19874 if (skipcc < 0 || skipcc > 1)
19875 EMSG(_(e_invarg));
19876 else
19877 {
19878#ifdef FEAT_MBYTE
19879 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19880 while (*s != NUL)
19881 {
19882 func_mb_ptr2char_adv(&s);
19883 ++len;
19884 }
19885 rettv->vval.v_number = len;
19886#else
19887 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19888#endif
19889 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019890}
19891
19892/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019893 * "strdisplaywidth()" function
19894 */
19895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019896f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019897{
19898 char_u *s = get_tv_string(&argvars[0]);
19899 int col = 0;
19900
19901 if (argvars[1].v_type != VAR_UNKNOWN)
19902 col = get_tv_number(&argvars[1]);
19903
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019904 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019905}
19906
19907/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019908 * "strwidth()" function
19909 */
19910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019911f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019912{
19913 char_u *s = get_tv_string(&argvars[0]);
19914
19915 rettv->vval.v_number = (varnumber_T)(
19916#ifdef FEAT_MBYTE
19917 mb_string2cells(s, -1)
19918#else
19919 STRLEN(s)
19920#endif
19921 );
19922}
19923
19924/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019925 * "strcharpart()" function
19926 */
19927 static void
19928f_strcharpart(typval_T *argvars, typval_T *rettv)
19929{
19930#ifdef FEAT_MBYTE
19931 char_u *p;
19932 int nchar;
19933 int nbyte = 0;
19934 int charlen;
19935 int len = 0;
19936 int slen;
19937 int error = FALSE;
19938
19939 p = get_tv_string(&argvars[0]);
19940 slen = (int)STRLEN(p);
19941
19942 nchar = get_tv_number_chk(&argvars[1], &error);
19943 if (!error)
19944 {
19945 if (nchar > 0)
19946 while (nchar > 0 && nbyte < slen)
19947 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019948 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019949 --nchar;
19950 }
19951 else
19952 nbyte = nchar;
19953 if (argvars[2].v_type != VAR_UNKNOWN)
19954 {
19955 charlen = get_tv_number(&argvars[2]);
19956 while (charlen > 0 && nbyte + len < slen)
19957 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019958 int off = nbyte + len;
19959
19960 if (off < 0)
19961 len += 1;
19962 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019963 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019964 --charlen;
19965 }
19966 }
19967 else
19968 len = slen - nbyte; /* default: all bytes that are available. */
19969 }
19970
19971 /*
19972 * Only return the overlap between the specified part and the actual
19973 * string.
19974 */
19975 if (nbyte < 0)
19976 {
19977 len += nbyte;
19978 nbyte = 0;
19979 }
19980 else if (nbyte > slen)
19981 nbyte = slen;
19982 if (len < 0)
19983 len = 0;
19984 else if (nbyte + len > slen)
19985 len = slen - nbyte;
19986
19987 rettv->v_type = VAR_STRING;
19988 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19989#else
19990 f_strpart(argvars, rettv);
19991#endif
19992}
19993
19994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 * "strpart()" function
19996 */
19997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019998f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999{
20000 char_u *p;
20001 int n;
20002 int len;
20003 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020004 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020006 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 slen = (int)STRLEN(p);
20008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020009 n = get_tv_number_chk(&argvars[1], &error);
20010 if (error)
20011 len = 0;
20012 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020013 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020014 else
20015 len = slen - n; /* default len: all bytes that are available. */
20016
20017 /*
20018 * Only return the overlap between the specified part and the actual
20019 * string.
20020 */
20021 if (n < 0)
20022 {
20023 len += n;
20024 n = 0;
20025 }
20026 else if (n > slen)
20027 n = slen;
20028 if (len < 0)
20029 len = 0;
20030 else if (n + len > slen)
20031 len = slen - n;
20032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020033 rettv->v_type = VAR_STRING;
20034 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035}
20036
20037/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020038 * "strridx()" function
20039 */
20040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020041f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020042{
20043 char_u buf[NUMBUFLEN];
20044 char_u *needle;
20045 char_u *haystack;
20046 char_u *rest;
20047 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020048 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020050 needle = get_tv_string_chk(&argvars[1]);
20051 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020052
20053 rettv->vval.v_number = -1;
20054 if (needle == NULL || haystack == NULL)
20055 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020056
20057 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020058 if (argvars[2].v_type != VAR_UNKNOWN)
20059 {
20060 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020061 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020062 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020063 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020064 }
20065 else
20066 end_idx = haystack_len;
20067
Bram Moolenaar0d660222005-01-07 21:51:51 +000020068 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020069 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020070 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020071 lastmatch = haystack + end_idx;
20072 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020073 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020074 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020075 for (rest = haystack; *rest != '\0'; ++rest)
20076 {
20077 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020078 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020079 break;
20080 lastmatch = rest;
20081 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020082 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020083
20084 if (lastmatch == NULL)
20085 rettv->vval.v_number = -1;
20086 else
20087 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20088}
20089
20090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 * "strtrans()" function
20092 */
20093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020094f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 rettv->v_type = VAR_STRING;
20097 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098}
20099
20100/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020101 * "submatch()" function
20102 */
20103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020104f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020105{
Bram Moolenaar41571762014-04-02 19:00:58 +020020106 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020107 int no;
20108 int retList = 0;
20109
20110 no = (int)get_tv_number_chk(&argvars[0], &error);
20111 if (error)
20112 return;
20113 error = FALSE;
20114 if (argvars[1].v_type != VAR_UNKNOWN)
20115 retList = get_tv_number_chk(&argvars[1], &error);
20116 if (error)
20117 return;
20118
20119 if (retList == 0)
20120 {
20121 rettv->v_type = VAR_STRING;
20122 rettv->vval.v_string = reg_submatch(no);
20123 }
20124 else
20125 {
20126 rettv->v_type = VAR_LIST;
20127 rettv->vval.v_list = reg_submatch_list(no);
20128 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020129}
20130
20131/*
20132 * "substitute()" function
20133 */
20134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020135f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020136{
20137 char_u patbuf[NUMBUFLEN];
20138 char_u subbuf[NUMBUFLEN];
20139 char_u flagsbuf[NUMBUFLEN];
20140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020141 char_u *str = get_tv_string_chk(&argvars[0]);
20142 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20143 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20144 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20145
Bram Moolenaar0d660222005-01-07 21:51:51 +000020146 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020147 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20148 rettv->vval.v_string = NULL;
20149 else
20150 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020151}
20152
20153/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020154 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020157f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158{
20159 int id = 0;
20160#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020161 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162 long col;
20163 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020164 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020166 lnum = get_tv_lnum(argvars); /* -1 on type error */
20167 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20168 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020170 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020171 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020172 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173#endif
20174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020175 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176}
20177
20178/*
20179 * "synIDattr(id, what [, mode])" function
20180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020182f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020183{
20184 char_u *p = NULL;
20185#ifdef FEAT_SYN_HL
20186 int id;
20187 char_u *what;
20188 char_u *mode;
20189 char_u modebuf[NUMBUFLEN];
20190 int modec;
20191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020192 id = get_tv_number(&argvars[0]);
20193 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020194 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020196 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020198 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 modec = 0; /* replace invalid with current */
20200 }
20201 else
20202 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020203#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020204 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 modec = 'g';
20206 else
20207#endif
20208 if (t_colors > 1)
20209 modec = 'c';
20210 else
20211 modec = 't';
20212 }
20213
20214
20215 switch (TOLOWER_ASC(what[0]))
20216 {
20217 case 'b':
20218 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20219 p = highlight_color(id, what, modec);
20220 else /* bold */
20221 p = highlight_has_attr(id, HL_BOLD, modec);
20222 break;
20223
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020224 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 p = highlight_color(id, what, modec);
20226 break;
20227
20228 case 'i':
20229 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20230 p = highlight_has_attr(id, HL_INVERSE, modec);
20231 else /* italic */
20232 p = highlight_has_attr(id, HL_ITALIC, modec);
20233 break;
20234
20235 case 'n': /* name */
20236 p = get_highlight_name(NULL, id - 1);
20237 break;
20238
20239 case 'r': /* reverse */
20240 p = highlight_has_attr(id, HL_INVERSE, modec);
20241 break;
20242
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020243 case 's':
20244 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20245 p = highlight_color(id, what, modec);
20246 else /* standout */
20247 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 break;
20249
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020250 case 'u':
20251 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20252 /* underline */
20253 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20254 else
20255 /* undercurl */
20256 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 break;
20258 }
20259
20260 if (p != NULL)
20261 p = vim_strsave(p);
20262#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020263 rettv->v_type = VAR_STRING;
20264 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265}
20266
20267/*
20268 * "synIDtrans(id)" function
20269 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020271f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272{
20273 int id;
20274
20275#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020276 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277
20278 if (id > 0)
20279 id = syn_get_final_id(id);
20280 else
20281#endif
20282 id = 0;
20283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020284 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285}
20286
20287/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020288 * "synconcealed(lnum, col)" function
20289 */
20290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020291f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020292{
20293#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20294 long lnum;
20295 long col;
20296 int syntax_flags = 0;
20297 int cchar;
20298 int matchid = 0;
20299 char_u str[NUMBUFLEN];
20300#endif
20301
20302 rettv->v_type = VAR_LIST;
20303 rettv->vval.v_list = NULL;
20304
20305#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20306 lnum = get_tv_lnum(argvars); /* -1 on type error */
20307 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20308
20309 vim_memset(str, NUL, sizeof(str));
20310
20311 if (rettv_list_alloc(rettv) != FAIL)
20312 {
20313 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20314 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20315 && curwin->w_p_cole > 0)
20316 {
20317 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20318 syntax_flags = get_syntax_info(&matchid);
20319
20320 /* get the conceal character */
20321 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20322 {
20323 cchar = syn_get_sub_char();
20324 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20325 cchar = lcs_conceal;
20326 if (cchar != NUL)
20327 {
20328# ifdef FEAT_MBYTE
20329 if (has_mbyte)
20330 (*mb_char2bytes)(cchar, str);
20331 else
20332# endif
20333 str[0] = cchar;
20334 }
20335 }
20336 }
20337
20338 list_append_number(rettv->vval.v_list,
20339 (syntax_flags & HL_CONCEAL) != 0);
20340 /* -1 to auto-determine strlen */
20341 list_append_string(rettv->vval.v_list, str, -1);
20342 list_append_number(rettv->vval.v_list, matchid);
20343 }
20344#endif
20345}
20346
20347/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020348 * "synstack(lnum, col)" function
20349 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020351f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020352{
20353#ifdef FEAT_SYN_HL
20354 long lnum;
20355 long col;
20356 int i;
20357 int id;
20358#endif
20359
20360 rettv->v_type = VAR_LIST;
20361 rettv->vval.v_list = NULL;
20362
20363#ifdef FEAT_SYN_HL
20364 lnum = get_tv_lnum(argvars); /* -1 on type error */
20365 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20366
20367 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020368 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020369 && rettv_list_alloc(rettv) != FAIL)
20370 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020371 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020372 for (i = 0; ; ++i)
20373 {
20374 id = syn_get_stack_item(i);
20375 if (id < 0)
20376 break;
20377 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20378 break;
20379 }
20380 }
20381#endif
20382}
20383
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020385get_cmd_output_as_rettv(
20386 typval_T *argvars,
20387 typval_T *rettv,
20388 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020390 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020391 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020392 char_u *infile = NULL;
20393 char_u buf[NUMBUFLEN];
20394 int err = FALSE;
20395 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020396 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020397 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020399 rettv->v_type = VAR_STRING;
20400 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020401 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020402 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020404 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020405 {
20406 /*
20407 * Write the string to a temp file, to be used for input of the shell
20408 * command.
20409 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020410 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020411 {
20412 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020413 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020414 }
20415
20416 fd = mch_fopen((char *)infile, WRITEBIN);
20417 if (fd == NULL)
20418 {
20419 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020420 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020421 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020422 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020423 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020424 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20425 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020426 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020427 else
20428 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020429 size_t len;
20430
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020431 p = get_tv_string_buf_chk(&argvars[1], buf);
20432 if (p == NULL)
20433 {
20434 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020435 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020436 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020437 len = STRLEN(p);
20438 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020439 err = TRUE;
20440 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020441 if (fclose(fd) != 0)
20442 err = TRUE;
20443 if (err)
20444 {
20445 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020446 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020447 }
20448 }
20449
Bram Moolenaar52a72462014-08-29 15:53:52 +020020450 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20451 * echoes typeahead, that messes up the display. */
20452 if (!msg_silent)
20453 flags += SHELL_COOKED;
20454
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020455 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020457 int len;
20458 listitem_T *li;
20459 char_u *s = NULL;
20460 char_u *start;
20461 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020462 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463
Bram Moolenaar52a72462014-08-29 15:53:52 +020020464 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020465 if (res == NULL)
20466 goto errret;
20467
20468 list = list_alloc();
20469 if (list == NULL)
20470 goto errret;
20471
20472 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020474 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020475 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020476 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020477 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020478
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020479 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020480 if (s == NULL)
20481 goto errret;
20482
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020483 for (p = s; start < end; ++p, ++start)
20484 *p = *start == NUL ? NL : *start;
20485 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020486
20487 li = listitem_alloc();
20488 if (li == NULL)
20489 {
20490 vim_free(s);
20491 goto errret;
20492 }
20493 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020494 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020495 li->li_tv.vval.v_string = s;
20496 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020498
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020499 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020500 rettv->v_type = VAR_LIST;
20501 rettv->vval.v_list = list;
20502 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020504 else
20505 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020506 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020507#ifdef USE_CR
20508 /* translate <CR> into <NL> */
20509 if (res != NULL)
20510 {
20511 char_u *s;
20512
20513 for (s = res; *s; ++s)
20514 {
20515 if (*s == CAR)
20516 *s = NL;
20517 }
20518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519#else
20520# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020521 /* translate <CR><NL> into <NL> */
20522 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020523 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020524 char_u *s, *d;
20525
20526 d = res;
20527 for (s = res; *s; ++s)
20528 {
20529 if (s[0] == CAR && s[1] == NL)
20530 ++s;
20531 *d++ = *s;
20532 }
20533 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535# endif
20536#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020537 rettv->vval.v_string = res;
20538 res = NULL;
20539 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020540
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020541errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020542 if (infile != NULL)
20543 {
20544 mch_remove(infile);
20545 vim_free(infile);
20546 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020547 if (res != NULL)
20548 vim_free(res);
20549 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020550 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020551}
20552
20553/*
20554 * "system()" function
20555 */
20556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020557f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020558{
20559 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20560}
20561
20562/*
20563 * "systemlist()" function
20564 */
20565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020566f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020567{
20568 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569}
20570
20571/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020572 * "tabpagebuflist()" function
20573 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020575f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020576{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020577#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020578 tabpage_T *tp;
20579 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020580
20581 if (argvars[0].v_type == VAR_UNKNOWN)
20582 wp = firstwin;
20583 else
20584 {
20585 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20586 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020587 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020588 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020589 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020590 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020591 for (; wp != NULL; wp = wp->w_next)
20592 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020593 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020594 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020595 }
20596#endif
20597}
20598
20599
20600/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020601 * "tabpagenr()" function
20602 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020604f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020605{
20606 int nr = 1;
20607#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020608 char_u *arg;
20609
20610 if (argvars[0].v_type != VAR_UNKNOWN)
20611 {
20612 arg = get_tv_string_chk(&argvars[0]);
20613 nr = 0;
20614 if (arg != NULL)
20615 {
20616 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020617 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020618 else
20619 EMSG2(_(e_invexpr2), arg);
20620 }
20621 }
20622 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020623 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020624#endif
20625 rettv->vval.v_number = nr;
20626}
20627
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020628
20629#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020630static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020631
20632/*
20633 * Common code for tabpagewinnr() and winnr().
20634 */
20635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020636get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020637{
20638 win_T *twin;
20639 int nr = 1;
20640 win_T *wp;
20641 char_u *arg;
20642
20643 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20644 if (argvar->v_type != VAR_UNKNOWN)
20645 {
20646 arg = get_tv_string_chk(argvar);
20647 if (arg == NULL)
20648 nr = 0; /* type error; errmsg already given */
20649 else if (STRCMP(arg, "$") == 0)
20650 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20651 else if (STRCMP(arg, "#") == 0)
20652 {
20653 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20654 if (twin == NULL)
20655 nr = 0;
20656 }
20657 else
20658 {
20659 EMSG2(_(e_invexpr2), arg);
20660 nr = 0;
20661 }
20662 }
20663
20664 if (nr > 0)
20665 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20666 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020667 {
20668 if (wp == NULL)
20669 {
20670 /* didn't find it in this tabpage */
20671 nr = 0;
20672 break;
20673 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020674 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020675 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020676 return nr;
20677}
20678#endif
20679
20680/*
20681 * "tabpagewinnr()" function
20682 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020684f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020685{
20686 int nr = 1;
20687#ifdef FEAT_WINDOWS
20688 tabpage_T *tp;
20689
20690 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20691 if (tp == NULL)
20692 nr = 0;
20693 else
20694 nr = get_winnr(tp, &argvars[1]);
20695#endif
20696 rettv->vval.v_number = nr;
20697}
20698
20699
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020700/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020701 * "tagfiles()" function
20702 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020704f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020705{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020706 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020707 tagname_T tn;
20708 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020709
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020710 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020711 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020712 fname = alloc(MAXPATHL);
20713 if (fname == NULL)
20714 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020715
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020716 for (first = TRUE; ; first = FALSE)
20717 if (get_tagfname(&tn, first, fname) == FAIL
20718 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020719 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020720 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020721 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020722}
20723
20724/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020725 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020726 */
20727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020728f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020729{
20730 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020731
20732 tag_pattern = get_tv_string(&argvars[0]);
20733
20734 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020735 if (*tag_pattern == NUL)
20736 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020737
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020738 if (rettv_list_alloc(rettv) == OK)
20739 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020740}
20741
20742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 * "tempname()" function
20744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020746f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747{
20748 static int x = 'A';
20749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020750 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020751 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752
20753 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20754 * names. Skip 'I' and 'O', they are used for shell redirection. */
20755 do
20756 {
20757 if (x == 'Z')
20758 x = '0';
20759 else if (x == '9')
20760 x = 'A';
20761 else
20762 {
20763#ifdef EBCDIC
20764 if (x == 'I')
20765 x = 'J';
20766 else if (x == 'R')
20767 x = 'S';
20768 else
20769#endif
20770 ++x;
20771 }
20772 } while (x == 'I' || x == 'O');
20773}
20774
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020775#ifdef FEAT_FLOAT
20776/*
20777 * "tan()" function
20778 */
20779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020780f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020781{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020782 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020783
20784 rettv->v_type = VAR_FLOAT;
20785 if (get_float_arg(argvars, &f) == OK)
20786 rettv->vval.v_float = tan(f);
20787 else
20788 rettv->vval.v_float = 0.0;
20789}
20790
20791/*
20792 * "tanh()" function
20793 */
20794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020795f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020796{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020797 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020798
20799 rettv->v_type = VAR_FLOAT;
20800 if (get_float_arg(argvars, &f) == OK)
20801 rettv->vval.v_float = tanh(f);
20802 else
20803 rettv->vval.v_float = 0.0;
20804}
20805#endif
20806
Bram Moolenaar574860b2016-05-24 17:33:34 +020020807/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020808 * "test_alloc_fail(id, countdown, repeat)" function
20809 */
20810 static void
20811f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20812{
20813 if (argvars[0].v_type != VAR_NUMBER
20814 || argvars[0].vval.v_number <= 0
20815 || argvars[1].v_type != VAR_NUMBER
20816 || argvars[1].vval.v_number < 0
20817 || argvars[2].v_type != VAR_NUMBER)
20818 EMSG(_(e_invarg));
20819 else
20820 {
20821 alloc_fail_id = argvars[0].vval.v_number;
20822 if (alloc_fail_id >= aid_last)
20823 EMSG(_(e_invarg));
20824 alloc_fail_countdown = argvars[1].vval.v_number;
20825 alloc_fail_repeat = argvars[2].vval.v_number;
20826 did_outofmem_msg = FALSE;
20827 }
20828}
20829
20830/*
20831 * "test_disable_char_avail({expr})" function
20832 */
20833 static void
20834f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20835{
20836 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20837}
20838
20839/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020840 * "test_garbagecollect_now()" function
20841 */
20842 static void
20843f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20844{
20845 /* This is dangerous, any Lists and Dicts used internally may be freed
20846 * while still in use. */
20847 garbage_collect(TRUE);
20848}
20849
20850#ifdef FEAT_JOB_CHANNEL
20851 static void
20852f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20853{
20854 rettv->v_type = VAR_CHANNEL;
20855 rettv->vval.v_channel = NULL;
20856}
20857#endif
20858
20859 static void
20860f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20861{
20862 rettv->v_type = VAR_DICT;
20863 rettv->vval.v_dict = NULL;
20864}
20865
20866#ifdef FEAT_JOB_CHANNEL
20867 static void
20868f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20869{
20870 rettv->v_type = VAR_JOB;
20871 rettv->vval.v_job = NULL;
20872}
20873#endif
20874
20875 static void
20876f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20877{
20878 rettv->v_type = VAR_LIST;
20879 rettv->vval.v_list = NULL;
20880}
20881
20882 static void
20883f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20884{
20885 rettv->v_type = VAR_PARTIAL;
20886 rettv->vval.v_partial = NULL;
20887}
20888
20889 static void
20890f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20891{
20892 rettv->v_type = VAR_STRING;
20893 rettv->vval.v_string = NULL;
20894}
20895
Bram Moolenaar975b5272016-03-15 23:10:59 +010020896#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20897/*
20898 * Get a callback from "arg". It can be a Funcref or a function name.
20899 * When "arg" is zero return an empty string.
20900 * Return NULL for an invalid argument.
20901 */
20902 char_u *
20903get_callback(typval_T *arg, partial_T **pp)
20904{
20905 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20906 {
20907 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020908 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020909 return (*pp)->pt_name;
20910 }
20911 *pp = NULL;
20912 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20913 return arg->vval.v_string;
20914 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20915 return (char_u *)"";
20916 EMSG(_("E921: Invalid callback argument"));
20917 return NULL;
20918}
20919#endif
20920
20921#ifdef FEAT_TIMERS
20922/*
20923 * "timer_start(time, callback [, options])" function
20924 */
20925 static void
20926f_timer_start(typval_T *argvars, typval_T *rettv)
20927{
20928 long msec = get_tv_number(&argvars[0]);
20929 timer_T *timer;
20930 int repeat = 0;
20931 char_u *callback;
20932 dict_T *dict;
20933
Bram Moolenaar38499922016-04-22 20:46:52 +020020934 if (check_secure())
20935 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020936 if (argvars[2].v_type != VAR_UNKNOWN)
20937 {
20938 if (argvars[2].v_type != VAR_DICT
20939 || (dict = argvars[2].vval.v_dict) == NULL)
20940 {
20941 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20942 return;
20943 }
20944 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20945 repeat = get_dict_number(dict, (char_u *)"repeat");
20946 }
20947
20948 timer = create_timer(msec, repeat);
20949 callback = get_callback(&argvars[1], &timer->tr_partial);
20950 if (callback == NULL)
20951 {
20952 stop_timer(timer);
20953 rettv->vval.v_number = -1;
20954 }
20955 else
20956 {
20957 timer->tr_callback = vim_strsave(callback);
20958 rettv->vval.v_number = timer->tr_id;
20959 }
20960}
20961
20962/*
20963 * "timer_stop(timer)" function
20964 */
20965 static void
20966f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20967{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020968 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020969
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020970 if (argvars[0].v_type != VAR_NUMBER)
20971 {
20972 EMSG(_(e_number_exp));
20973 return;
20974 }
20975 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020976 if (timer != NULL)
20977 stop_timer(timer);
20978}
20979#endif
20980
Bram Moolenaard52d9742005-08-21 22:20:28 +000020981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 * "tolower(string)" function
20983 */
20984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020985f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986{
20987 char_u *p;
20988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020989 p = vim_strsave(get_tv_string(&argvars[0]));
20990 rettv->v_type = VAR_STRING;
20991 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992
20993 if (p != NULL)
20994 while (*p != NUL)
20995 {
20996#ifdef FEAT_MBYTE
20997 int l;
20998
20999 if (enc_utf8)
21000 {
21001 int c, lc;
21002
21003 c = utf_ptr2char(p);
21004 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021005 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 /* TODO: reallocate string when byte count changes. */
21007 if (utf_char2len(lc) == l)
21008 utf_char2bytes(lc, p);
21009 p += l;
21010 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021011 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 p += l; /* skip multi-byte character */
21013 else
21014#endif
21015 {
21016 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21017 ++p;
21018 }
21019 }
21020}
21021
21022/*
21023 * "toupper(string)" function
21024 */
21025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021026f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021028 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021029 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030}
21031
21032/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021033 * "tr(string, fromstr, tostr)" function
21034 */
21035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021036f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021037{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021038 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021039 char_u *fromstr;
21040 char_u *tostr;
21041 char_u *p;
21042#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021043 int inlen;
21044 int fromlen;
21045 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021046 int idx;
21047 char_u *cpstr;
21048 int cplen;
21049 int first = TRUE;
21050#endif
21051 char_u buf[NUMBUFLEN];
21052 char_u buf2[NUMBUFLEN];
21053 garray_T ga;
21054
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021055 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021056 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21057 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021058
21059 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021060 rettv->v_type = VAR_STRING;
21061 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021062 if (fromstr == NULL || tostr == NULL)
21063 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021064 ga_init2(&ga, (int)sizeof(char), 80);
21065
21066#ifdef FEAT_MBYTE
21067 if (!has_mbyte)
21068#endif
21069 /* not multi-byte: fromstr and tostr must be the same length */
21070 if (STRLEN(fromstr) != STRLEN(tostr))
21071 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021072#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021073error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021074#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021075 EMSG2(_(e_invarg2), fromstr);
21076 ga_clear(&ga);
21077 return;
21078 }
21079
21080 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021081 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021082 {
21083#ifdef FEAT_MBYTE
21084 if (has_mbyte)
21085 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021086 inlen = (*mb_ptr2len)(in_str);
21087 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021088 cplen = inlen;
21089 idx = 0;
21090 for (p = fromstr; *p != NUL; p += fromlen)
21091 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021092 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021093 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021094 {
21095 for (p = tostr; *p != NUL; p += tolen)
21096 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021097 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021098 if (idx-- == 0)
21099 {
21100 cplen = tolen;
21101 cpstr = p;
21102 break;
21103 }
21104 }
21105 if (*p == NUL) /* tostr is shorter than fromstr */
21106 goto error;
21107 break;
21108 }
21109 ++idx;
21110 }
21111
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021112 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021113 {
21114 /* Check that fromstr and tostr have the same number of
21115 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021116 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021117 first = FALSE;
21118 for (p = tostr; *p != NUL; p += tolen)
21119 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021120 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021121 --idx;
21122 }
21123 if (idx != 0)
21124 goto error;
21125 }
21126
Bram Moolenaarcde88542015-08-11 19:14:00 +020021127 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021128 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021129 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021130
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021131 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021132 }
21133 else
21134#endif
21135 {
21136 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021137 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021138 if (p != NULL)
21139 ga_append(&ga, tostr[p - fromstr]);
21140 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021141 ga_append(&ga, *in_str);
21142 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021143 }
21144 }
21145
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021146 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021147 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021148 ga_append(&ga, NUL);
21149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021150 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021151}
21152
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021153#ifdef FEAT_FLOAT
21154/*
21155 * "trunc({float})" function
21156 */
21157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021158f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021159{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021160 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021161
21162 rettv->v_type = VAR_FLOAT;
21163 if (get_float_arg(argvars, &f) == OK)
21164 /* trunc() is not in C90, use floor() or ceil() instead. */
21165 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21166 else
21167 rettv->vval.v_float = 0.0;
21168}
21169#endif
21170
Bram Moolenaar8299df92004-07-10 09:47:34 +000021171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021172 * "type(expr)" function
21173 */
21174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021175f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021177 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021178
21179 switch (argvars[0].v_type)
21180 {
21181 case VAR_NUMBER: n = 0; break;
21182 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021183 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021184 case VAR_FUNC: n = 2; break;
21185 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021186 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021187 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021188 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021189 if (argvars[0].vval.v_number == VVAL_FALSE
21190 || argvars[0].vval.v_number == VVAL_TRUE)
21191 n = 6;
21192 else
21193 n = 7;
21194 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021195 case VAR_JOB: n = 8; break;
21196 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021197 case VAR_UNKNOWN:
21198 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21199 n = -1;
21200 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021201 }
21202 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203}
21204
21205/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021206 * "undofile(name)" function
21207 */
21208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021209f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021210{
21211 rettv->v_type = VAR_STRING;
21212#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021213 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021214 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021215
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021216 if (*fname == NUL)
21217 {
21218 /* If there is no file name there will be no undo file. */
21219 rettv->vval.v_string = NULL;
21220 }
21221 else
21222 {
21223 char_u *ffname = FullName_save(fname, FALSE);
21224
21225 if (ffname != NULL)
21226 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21227 vim_free(ffname);
21228 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021229 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021230#else
21231 rettv->vval.v_string = NULL;
21232#endif
21233}
21234
21235/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021236 * "undotree()" function
21237 */
21238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021239f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021240{
21241 if (rettv_dict_alloc(rettv) == OK)
21242 {
21243 dict_T *dict = rettv->vval.v_dict;
21244 list_T *list;
21245
Bram Moolenaar730cde92010-06-27 05:18:54 +020021246 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021247 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021248 dict_add_nr_str(dict, "save_last",
21249 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021250 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21251 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021252 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021253
21254 list = list_alloc();
21255 if (list != NULL)
21256 {
21257 u_eval_tree(curbuf->b_u_oldhead, list);
21258 dict_add_list(dict, "entries", list);
21259 }
21260 }
21261}
21262
21263/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021264 * "values(dict)" function
21265 */
21266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021267f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021268{
21269 dict_list(argvars, rettv, 1);
21270}
21271
21272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 * "virtcol(string)" function
21274 */
21275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021276f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277{
21278 colnr_T vcol = 0;
21279 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021280 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021282 fp = var2fpos(&argvars[0], FALSE, &fnum);
21283 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21284 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285 {
21286 getvvcol(curwin, fp, NULL, NULL, &vcol);
21287 ++vcol;
21288 }
21289
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021290 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021291}
21292
21293/*
21294 * "visualmode()" function
21295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021297f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 char_u str[2];
21300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021301 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 str[0] = curbuf->b_visual_mode_eval;
21303 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021304 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305
21306 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021307 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309}
21310
21311/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021312 * "wildmenumode()" function
21313 */
21314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021315f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021316{
21317#ifdef FEAT_WILDMENU
21318 if (wild_menu_showing)
21319 rettv->vval.v_number = 1;
21320#endif
21321}
21322
21323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324 * "winbufnr(nr)" function
21325 */
21326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021327f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328{
21329 win_T *wp;
21330
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021331 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021333 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021335 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336}
21337
21338/*
21339 * "wincol()" function
21340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021342f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021343{
21344 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021345 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346}
21347
21348/*
21349 * "winheight(nr)" function
21350 */
21351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021352f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021353{
21354 win_T *wp;
21355
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021356 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021358 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021360 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361}
21362
21363/*
21364 * "winline()" function
21365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021367f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368{
21369 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021370 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371}
21372
21373/*
21374 * "winnr()" function
21375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021377f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378{
21379 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021380
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021382 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021384 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385}
21386
21387/*
21388 * "winrestcmd()" function
21389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021391f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021392{
21393#ifdef FEAT_WINDOWS
21394 win_T *wp;
21395 int winnr = 1;
21396 garray_T ga;
21397 char_u buf[50];
21398
21399 ga_init2(&ga, (int)sizeof(char), 70);
21400 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21401 {
21402 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21403 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21405 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406 ++winnr;
21407 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021408 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021410 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021412 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021413#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021414 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415}
21416
21417/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021418 * "winrestview()" function
21419 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021421f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021422{
21423 dict_T *dict;
21424
21425 if (argvars[0].v_type != VAR_DICT
21426 || (dict = argvars[0].vval.v_dict) == NULL)
21427 EMSG(_(e_invarg));
21428 else
21429 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021430 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21431 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21432 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21433 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021434#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021435 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21436 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021437#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021438 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21439 {
21440 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21441 curwin->w_set_curswant = FALSE;
21442 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021443
Bram Moolenaar82c25852014-05-28 16:47:16 +020021444 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21445 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021446#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021447 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21448 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021449#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021450 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21451 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21452 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21453 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021454
21455 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021456 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021457# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021458 win_new_width(curwin, W_WIDTH(curwin));
21459# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021460 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021461
Bram Moolenaarb851a962014-10-31 15:45:52 +010021462 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021463 curwin->w_topline = 1;
21464 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21465 curwin->w_topline = curbuf->b_ml.ml_line_count;
21466#ifdef FEAT_DIFF
21467 check_topfill(curwin, TRUE);
21468#endif
21469 }
21470}
21471
21472/*
21473 * "winsaveview()" function
21474 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021476f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021477{
21478 dict_T *dict;
21479
Bram Moolenaara800b422010-06-27 01:15:55 +020021480 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021481 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021482 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021483
21484 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21485 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21486#ifdef FEAT_VIRTUALEDIT
21487 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21488#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021489 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021490 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21491
21492 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21493#ifdef FEAT_DIFF
21494 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21495#endif
21496 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21497 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21498}
21499
21500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 * "winwidth(nr)" function
21502 */
21503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021504f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505{
21506 win_T *wp;
21507
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021508 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021510 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021512#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021513 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021515 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516#endif
21517}
21518
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021520 * "wordcount()" function
21521 */
21522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021523f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021524{
21525 if (rettv_dict_alloc(rettv) == FAIL)
21526 return;
21527 cursor_pos_info(rettv->vval.v_dict);
21528}
21529
21530/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021531 * Write list of strings to file
21532 */
21533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021534write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021535{
21536 listitem_T *li;
21537 int c;
21538 int ret = OK;
21539 char_u *s;
21540
21541 for (li = list->lv_first; li != NULL; li = li->li_next)
21542 {
21543 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21544 {
21545 if (*s == '\n')
21546 c = putc(NUL, fd);
21547 else
21548 c = putc(*s, fd);
21549 if (c == EOF)
21550 {
21551 ret = FAIL;
21552 break;
21553 }
21554 }
21555 if (!binary || li->li_next != NULL)
21556 if (putc('\n', fd) == EOF)
21557 {
21558 ret = FAIL;
21559 break;
21560 }
21561 if (ret == FAIL)
21562 {
21563 EMSG(_(e_write));
21564 break;
21565 }
21566 }
21567 return ret;
21568}
21569
21570/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021571 * "writefile()" function
21572 */
21573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021574f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021575{
21576 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021577 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021578 char_u *fname;
21579 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021580 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021581
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021582 if (check_restricted() || check_secure())
21583 return;
21584
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021585 if (argvars[0].v_type != VAR_LIST)
21586 {
21587 EMSG2(_(e_listarg), "writefile()");
21588 return;
21589 }
21590 if (argvars[0].vval.v_list == NULL)
21591 return;
21592
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021593 if (argvars[2].v_type != VAR_UNKNOWN)
21594 {
21595 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21596 binary = TRUE;
21597 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21598 append = TRUE;
21599 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021600
21601 /* Always open the file in binary mode, library functions have a mind of
21602 * their own about CR-LF conversion. */
21603 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021604 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21605 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021606 {
21607 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21608 ret = -1;
21609 }
21610 else
21611 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021612 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21613 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021614 fclose(fd);
21615 }
21616
21617 rettv->vval.v_number = ret;
21618}
21619
21620/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021621 * "xor(expr, expr)" function
21622 */
21623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021624f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021625{
21626 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21627 ^ get_tv_number_chk(&argvars[1], NULL);
21628}
21629
21630
21631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021633 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 */
21635 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021636var2fpos(
21637 typval_T *varp,
21638 int dollar_lnum, /* TRUE when $ is last line */
21639 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021641 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021643 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644
Bram Moolenaara5525202006-03-02 22:52:09 +000021645 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021646 if (varp->v_type == VAR_LIST)
21647 {
21648 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021649 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021650 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021651 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021652
21653 l = varp->vval.v_list;
21654 if (l == NULL)
21655 return NULL;
21656
21657 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021658 pos.lnum = list_find_nr(l, 0L, &error);
21659 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021660 return NULL; /* invalid line number */
21661
21662 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021663 pos.col = list_find_nr(l, 1L, &error);
21664 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021665 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021666 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021667
21668 /* We accept "$" for the column number: last column. */
21669 li = list_find(l, 1L);
21670 if (li != NULL && li->li_tv.v_type == VAR_STRING
21671 && li->li_tv.vval.v_string != NULL
21672 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21673 pos.col = len + 1;
21674
Bram Moolenaara5525202006-03-02 22:52:09 +000021675 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021676 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021677 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021678 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021679
Bram Moolenaara5525202006-03-02 22:52:09 +000021680#ifdef FEAT_VIRTUALEDIT
21681 /* Get the virtual offset. Defaults to zero. */
21682 pos.coladd = list_find_nr(l, 2L, &error);
21683 if (error)
21684 pos.coladd = 0;
21685#endif
21686
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021687 return &pos;
21688 }
21689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021690 name = get_tv_string_chk(varp);
21691 if (name == NULL)
21692 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021693 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021695 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21696 {
21697 if (VIsual_active)
21698 return &VIsual;
21699 return &curwin->w_cursor;
21700 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021701 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021703 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21705 return NULL;
21706 return pp;
21707 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021708
21709#ifdef FEAT_VIRTUALEDIT
21710 pos.coladd = 0;
21711#endif
21712
Bram Moolenaar477933c2007-07-17 14:32:23 +000021713 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021714 {
21715 pos.col = 0;
21716 if (name[1] == '0') /* "w0": first visible line */
21717 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021718 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021719 pos.lnum = curwin->w_topline;
21720 return &pos;
21721 }
21722 else if (name[1] == '$') /* "w$": last visible line */
21723 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021724 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021725 pos.lnum = curwin->w_botline - 1;
21726 return &pos;
21727 }
21728 }
21729 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021731 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 {
21733 pos.lnum = curbuf->b_ml.ml_line_count;
21734 pos.col = 0;
21735 }
21736 else
21737 {
21738 pos.lnum = curwin->w_cursor.lnum;
21739 pos.col = (colnr_T)STRLEN(ml_get_curline());
21740 }
21741 return &pos;
21742 }
21743 return NULL;
21744}
21745
21746/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021747 * Convert list in "arg" into a position and optional file number.
21748 * When "fnump" is NULL there is no file number, only 3 items.
21749 * Note that the column is passed on as-is, the caller may want to decrement
21750 * it to use 1 for the first column.
21751 * Return FAIL when conversion is not possible, doesn't check the position for
21752 * validity.
21753 */
21754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021755list2fpos(
21756 typval_T *arg,
21757 pos_T *posp,
21758 int *fnump,
21759 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021760{
21761 list_T *l = arg->vval.v_list;
21762 long i = 0;
21763 long n;
21764
Bram Moolenaar493c1782014-05-28 14:34:46 +020021765 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21766 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021767 if (arg->v_type != VAR_LIST
21768 || l == NULL
21769 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021770 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021771 return FAIL;
21772
21773 if (fnump != NULL)
21774 {
21775 n = list_find_nr(l, i++, NULL); /* fnum */
21776 if (n < 0)
21777 return FAIL;
21778 if (n == 0)
21779 n = curbuf->b_fnum; /* current buffer */
21780 *fnump = n;
21781 }
21782
21783 n = list_find_nr(l, i++, NULL); /* lnum */
21784 if (n < 0)
21785 return FAIL;
21786 posp->lnum = n;
21787
21788 n = list_find_nr(l, i++, NULL); /* col */
21789 if (n < 0)
21790 return FAIL;
21791 posp->col = n;
21792
21793#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021794 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021795 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021796 posp->coladd = 0;
21797 else
21798 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021799#endif
21800
Bram Moolenaar493c1782014-05-28 14:34:46 +020021801 if (curswantp != NULL)
21802 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21803
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021804 return OK;
21805}
21806
21807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 * Get the length of an environment variable name.
21809 * Advance "arg" to the first character after the name.
21810 * Return 0 for error.
21811 */
21812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021813get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814{
21815 char_u *p;
21816 int len;
21817
21818 for (p = *arg; vim_isIDc(*p); ++p)
21819 ;
21820 if (p == *arg) /* no name found */
21821 return 0;
21822
21823 len = (int)(p - *arg);
21824 *arg = p;
21825 return len;
21826}
21827
21828/*
21829 * Get the length of the name of a function or internal variable.
21830 * "arg" is advanced to the first non-white character after the name.
21831 * Return 0 if something is wrong.
21832 */
21833 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021834get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835{
21836 char_u *p;
21837 int len;
21838
21839 /* Find the end of the name. */
21840 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021841 {
21842 if (*p == ':')
21843 {
21844 /* "s:" is start of "s:var", but "n:" is not and can be used in
21845 * slice "[n:]". Also "xx:" is not a namespace. */
21846 len = (int)(p - *arg);
21847 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21848 || len > 1)
21849 break;
21850 }
21851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 if (p == *arg) /* no name found */
21853 return 0;
21854
21855 len = (int)(p - *arg);
21856 *arg = skipwhite(p);
21857
21858 return len;
21859}
21860
21861/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021862 * Get the length of the name of a variable or function.
21863 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021865 * Return -1 if curly braces expansion failed.
21866 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867 * If the name contains 'magic' {}'s, expand them and return the
21868 * expanded name in an allocated string via 'alias' - caller must free.
21869 */
21870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021871get_name_len(
21872 char_u **arg,
21873 char_u **alias,
21874 int evaluate,
21875 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876{
21877 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878 char_u *p;
21879 char_u *expr_start;
21880 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881
21882 *alias = NULL; /* default to no alias */
21883
21884 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21885 && (*arg)[2] == (int)KE_SNR)
21886 {
21887 /* hard coded <SNR>, already translated */
21888 *arg += 3;
21889 return get_id_len(arg) + 3;
21890 }
21891 len = eval_fname_script(*arg);
21892 if (len > 0)
21893 {
21894 /* literal "<SID>", "s:" or "<SNR>" */
21895 *arg += len;
21896 }
21897
Bram Moolenaar071d4272004-06-13 20:20:40 +000021898 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021899 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021901 p = find_name_end(*arg, &expr_start, &expr_end,
21902 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021903 if (expr_start != NULL)
21904 {
21905 char_u *temp_string;
21906
21907 if (!evaluate)
21908 {
21909 len += (int)(p - *arg);
21910 *arg = skipwhite(p);
21911 return len;
21912 }
21913
21914 /*
21915 * Include any <SID> etc in the expanded string:
21916 * Thus the -len here.
21917 */
21918 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21919 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021920 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 *alias = temp_string;
21922 *arg = skipwhite(p);
21923 return (int)STRLEN(temp_string);
21924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925
21926 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021927 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 EMSG2(_(e_invexpr2), *arg);
21929
21930 return len;
21931}
21932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021933/*
21934 * Find the end of a variable or function name, taking care of magic braces.
21935 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21936 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021937 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021938 * Return a pointer to just after the name. Equal to "arg" if there is no
21939 * valid name.
21940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021942find_name_end(
21943 char_u *arg,
21944 char_u **expr_start,
21945 char_u **expr_end,
21946 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021948 int mb_nest = 0;
21949 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021951 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021953 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021955 *expr_start = NULL;
21956 *expr_end = NULL;
21957 }
21958
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021959 /* Quick check for valid starting character. */
21960 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21961 return arg;
21962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021963 for (p = arg; *p != NUL
21964 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021965 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021966 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021967 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021968 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021969 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021970 if (*p == '\'')
21971 {
21972 /* skip over 'string' to avoid counting [ and ] inside it. */
21973 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21974 ;
21975 if (*p == NUL)
21976 break;
21977 }
21978 else if (*p == '"')
21979 {
21980 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21981 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21982 if (*p == '\\' && p[1] != NUL)
21983 ++p;
21984 if (*p == NUL)
21985 break;
21986 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021987 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21988 {
21989 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021990 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021991 len = (int)(p - arg);
21992 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021993 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021994 break;
21995 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021997 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021999 if (*p == '[')
22000 ++br_nest;
22001 else if (*p == ']')
22002 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022005 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022007 if (*p == '{')
22008 {
22009 mb_nest++;
22010 if (expr_start != NULL && *expr_start == NULL)
22011 *expr_start = p;
22012 }
22013 else if (*p == '}')
22014 {
22015 mb_nest--;
22016 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22017 *expr_end = p;
22018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 }
22021
22022 return p;
22023}
22024
22025/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022026 * Expands out the 'magic' {}'s in a variable/function name.
22027 * Note that this can call itself recursively, to deal with
22028 * constructs like foo{bar}{baz}{bam}
22029 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22030 * "in_start" ^
22031 * "expr_start" ^
22032 * "expr_end" ^
22033 * "in_end" ^
22034 *
22035 * Returns a new allocated string, which the caller must free.
22036 * Returns NULL for failure.
22037 */
22038 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022039make_expanded_name(
22040 char_u *in_start,
22041 char_u *expr_start,
22042 char_u *expr_end,
22043 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022044{
22045 char_u c1;
22046 char_u *retval = NULL;
22047 char_u *temp_result;
22048 char_u *nextcmd = NULL;
22049
22050 if (expr_end == NULL || in_end == NULL)
22051 return NULL;
22052 *expr_start = NUL;
22053 *expr_end = NUL;
22054 c1 = *in_end;
22055 *in_end = NUL;
22056
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022057 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022058 if (temp_result != NULL && nextcmd == NULL)
22059 {
22060 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22061 + (in_end - expr_end) + 1));
22062 if (retval != NULL)
22063 {
22064 STRCPY(retval, in_start);
22065 STRCAT(retval, temp_result);
22066 STRCAT(retval, expr_end + 1);
22067 }
22068 }
22069 vim_free(temp_result);
22070
22071 *in_end = c1; /* put char back for error messages */
22072 *expr_start = '{';
22073 *expr_end = '}';
22074
22075 if (retval != NULL)
22076 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022077 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022078 if (expr_start != NULL)
22079 {
22080 /* Further expansion! */
22081 temp_result = make_expanded_name(retval, expr_start,
22082 expr_end, temp_result);
22083 vim_free(retval);
22084 retval = temp_result;
22085 }
22086 }
22087
22088 return retval;
22089}
22090
22091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022093 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 */
22095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022096eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022098 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22099}
22100
22101/*
22102 * Return TRUE if character "c" can be used as the first character in a
22103 * variable or function name (excluding '{' and '}').
22104 */
22105 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022106eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022107{
22108 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109}
22110
22111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 * Set number v: variable to "val".
22113 */
22114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022115set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022117 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118}
22119
22120/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022121 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122 */
22123 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022124get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022126 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127}
22128
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022129/*
22130 * Get string v: variable value. Uses a static buffer, can only be used once.
22131 */
22132 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022133get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022134{
22135 return get_tv_string(&vimvars[idx].vv_tv);
22136}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022137
Bram Moolenaar071d4272004-06-13 20:20:40 +000022138/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022139 * Get List v: variable value. Caller must take care of reference count when
22140 * needed.
22141 */
22142 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022143get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022144{
22145 return vimvars[idx].vv_list;
22146}
22147
22148/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022149 * Set v:char to character "c".
22150 */
22151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022152set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022153{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022154 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022155
22156#ifdef FEAT_MBYTE
22157 if (has_mbyte)
22158 buf[(*mb_char2bytes)(c, buf)] = NUL;
22159 else
22160#endif
22161 {
22162 buf[0] = c;
22163 buf[1] = NUL;
22164 }
22165 set_vim_var_string(VV_CHAR, buf, -1);
22166}
22167
22168/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022169 * Set v:count to "count" and v:count1 to "count1".
22170 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 */
22172 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022173set_vcount(
22174 long count,
22175 long count1,
22176 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022178 if (set_prevcount)
22179 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022180 vimvars[VV_COUNT].vv_nr = count;
22181 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182}
22183
22184/*
22185 * Set string v: variable to a copy of "val".
22186 */
22187 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022188set_vim_var_string(
22189 int idx,
22190 char_u *val,
22191 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192{
Bram Moolenaara542c682016-01-31 16:28:04 +010022193 clear_tv(&vimvars[idx].vv_di.di_tv);
22194 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022195 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022196 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022198 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022200 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201}
22202
22203/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022204 * Set List v: variable to "val".
22205 */
22206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022207set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022208{
Bram Moolenaara542c682016-01-31 16:28:04 +010022209 clear_tv(&vimvars[idx].vv_di.di_tv);
22210 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022211 vimvars[idx].vv_list = val;
22212 if (val != NULL)
22213 ++val->lv_refcount;
22214}
22215
22216/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022217 * Set Dictionary v: variable to "val".
22218 */
22219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022220set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022221{
22222 int todo;
22223 hashitem_T *hi;
22224
Bram Moolenaara542c682016-01-31 16:28:04 +010022225 clear_tv(&vimvars[idx].vv_di.di_tv);
22226 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022227 vimvars[idx].vv_dict = val;
22228 if (val != NULL)
22229 {
22230 ++val->dv_refcount;
22231
22232 /* Set readonly */
22233 todo = (int)val->dv_hashtab.ht_used;
22234 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22235 {
22236 if (HASHITEM_EMPTY(hi))
22237 continue;
22238 --todo;
22239 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22240 }
22241 }
22242}
22243
22244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 * Set v:register if needed.
22246 */
22247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022248set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249{
22250 char_u regname;
22251
22252 if (c == 0 || c == ' ')
22253 regname = '"';
22254 else
22255 regname = c;
22256 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022257 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022258 set_vim_var_string(VV_REG, &regname, 1);
22259}
22260
22261/*
22262 * Get or set v:exception. If "oldval" == NULL, return the current value.
22263 * Otherwise, restore the value to "oldval" and return NULL.
22264 * Must always be called in pairs to save and restore v:exception! Does not
22265 * take care of memory allocations.
22266 */
22267 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022268v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269{
22270 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022271 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272
Bram Moolenaare9a41262005-01-15 22:18:47 +000022273 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 return NULL;
22275}
22276
22277/*
22278 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22279 * Otherwise, restore the value to "oldval" and return NULL.
22280 * Must always be called in pairs to save and restore v:throwpoint! Does not
22281 * take care of memory allocations.
22282 */
22283 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022284v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285{
22286 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022287 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288
Bram Moolenaare9a41262005-01-15 22:18:47 +000022289 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 return NULL;
22291}
22292
22293#if defined(FEAT_AUTOCMD) || defined(PROTO)
22294/*
22295 * Set v:cmdarg.
22296 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22297 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22298 * Must always be called in pairs!
22299 */
22300 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022301set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302{
22303 char_u *oldval;
22304 char_u *newval;
22305 unsigned len;
22306
Bram Moolenaare9a41262005-01-15 22:18:47 +000022307 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022308 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022309 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022310 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022311 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022312 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022313 }
22314
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022315 if (eap->force_bin == FORCE_BIN)
22316 len = 6;
22317 else if (eap->force_bin == FORCE_NOBIN)
22318 len = 8;
22319 else
22320 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022321
22322 if (eap->read_edit)
22323 len += 7;
22324
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022325 if (eap->force_ff != 0)
22326 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22327# ifdef FEAT_MBYTE
22328 if (eap->force_enc != 0)
22329 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022330 if (eap->bad_char != 0)
22331 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022332# endif
22333
22334 newval = alloc(len + 1);
22335 if (newval == NULL)
22336 return NULL;
22337
22338 if (eap->force_bin == FORCE_BIN)
22339 sprintf((char *)newval, " ++bin");
22340 else if (eap->force_bin == FORCE_NOBIN)
22341 sprintf((char *)newval, " ++nobin");
22342 else
22343 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022344
22345 if (eap->read_edit)
22346 STRCAT(newval, " ++edit");
22347
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022348 if (eap->force_ff != 0)
22349 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22350 eap->cmd + eap->force_ff);
22351# ifdef FEAT_MBYTE
22352 if (eap->force_enc != 0)
22353 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22354 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022355 if (eap->bad_char == BAD_KEEP)
22356 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22357 else if (eap->bad_char == BAD_DROP)
22358 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22359 else if (eap->bad_char != 0)
22360 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022361# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022362 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022363 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364}
22365#endif
22366
22367/*
22368 * Get the value of internal variable "name".
22369 * Return OK or FAIL.
22370 */
22371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022372get_var_tv(
22373 char_u *name,
22374 int len, /* length of "name" */
22375 typval_T *rettv, /* NULL when only checking existence */
22376 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22377 int verbose, /* may give error message */
22378 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379{
22380 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022381 typval_T *tv = NULL;
22382 typval_T atv;
22383 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385
22386 /* truncate the name, so that we can use strcmp() */
22387 cc = name[len];
22388 name[len] = NUL;
22389
22390 /*
22391 * Check for "b:changedtick".
22392 */
22393 if (STRCMP(name, "b:changedtick") == 0)
22394 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022395 atv.v_type = VAR_NUMBER;
22396 atv.vval.v_number = curbuf->b_changedtick;
22397 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 }
22399
22400 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 * Check for user-defined variables.
22402 */
22403 else
22404 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022405 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022407 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022408 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022409 if (dip != NULL)
22410 *dip = v;
22411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 }
22413
Bram Moolenaare9a41262005-01-15 22:18:47 +000022414 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022416 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022417 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 ret = FAIL;
22419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022420 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022421 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422
22423 name[len] = cc;
22424
22425 return ret;
22426}
22427
22428/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022429 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22430 * Also handle function call with Funcref variable: func(expr)
22431 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22432 */
22433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022434handle_subscript(
22435 char_u **arg,
22436 typval_T *rettv,
22437 int evaluate, /* do more than finding the end */
22438 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022439{
22440 int ret = OK;
22441 dict_T *selfdict = NULL;
22442 char_u *s;
22443 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022444 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022445
22446 while (ret == OK
22447 && (**arg == '['
22448 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022449 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22450 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022451 && !vim_iswhite(*(*arg - 1)))
22452 {
22453 if (**arg == '(')
22454 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022455 partial_T *pt = NULL;
22456
Bram Moolenaard9fba312005-06-26 22:34:35 +000022457 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022458 if (evaluate)
22459 {
22460 functv = *rettv;
22461 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022462
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022463 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022464 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022465 {
22466 pt = functv.vval.v_partial;
22467 s = pt->pt_name;
22468 }
22469 else
22470 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022471 }
22472 else
22473 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022474 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022475 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022476 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022477
22478 /* Clear the funcref afterwards, so that deleting it while
22479 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022480 if (evaluate)
22481 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022482
22483 /* Stop the expression evaluation when immediately aborting on
22484 * error, or when an interrupt occurred or an exception was thrown
22485 * but not caught. */
22486 if (aborting())
22487 {
22488 if (ret == OK)
22489 clear_tv(rettv);
22490 ret = FAIL;
22491 }
22492 dict_unref(selfdict);
22493 selfdict = NULL;
22494 }
22495 else /* **arg == '[' || **arg == '.' */
22496 {
22497 dict_unref(selfdict);
22498 if (rettv->v_type == VAR_DICT)
22499 {
22500 selfdict = rettv->vval.v_dict;
22501 if (selfdict != NULL)
22502 ++selfdict->dv_refcount;
22503 }
22504 else
22505 selfdict = NULL;
22506 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22507 {
22508 clear_tv(rettv);
22509 ret = FAIL;
22510 }
22511 }
22512 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022513
Bram Moolenaar1d429612016-05-24 15:44:17 +020022514 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22515 * Don't do this when "Func" is already a partial that was bound
22516 * explicitly (pt_auto is FALSE). */
22517 if (selfdict != NULL
22518 && (rettv->v_type == VAR_FUNC
22519 || (rettv->v_type == VAR_PARTIAL
22520 && (rettv->vval.v_partial->pt_auto
22521 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022522 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022523 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22524 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022525 char_u *tofree = NULL;
22526 ufunc_T *fp;
22527 char_u fname_buf[FLEN_FIXED + 1];
22528 int error;
22529
22530 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022531 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022532 fp = find_func(fname);
22533 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022534
Bram Moolenaar65639032016-03-16 21:40:30 +010022535 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022536 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022537 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22538
22539 if (pt != NULL)
22540 {
22541 pt->pt_refcount = 1;
22542 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022543 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022544 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022545 if (rettv->v_type == VAR_FUNC)
22546 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022547 /* Just a function: Take over the function name and use
22548 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022549 pt->pt_name = rettv->vval.v_string;
22550 }
22551 else
22552 {
22553 partial_T *ret_pt = rettv->vval.v_partial;
22554 int i;
22555
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022556 /* Partial: copy the function name, use selfdict and copy
22557 * args. Can't take over name or args, the partial might
22558 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022559 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022560 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022561 if (ret_pt->pt_argc > 0)
22562 {
22563 pt->pt_argv = (typval_T *)alloc(
22564 sizeof(typval_T) * ret_pt->pt_argc);
22565 if (pt->pt_argv == NULL)
22566 /* out of memory: drop the arguments */
22567 pt->pt_argc = 0;
22568 else
22569 {
22570 pt->pt_argc = ret_pt->pt_argc;
22571 for (i = 0; i < pt->pt_argc; i++)
22572 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22573 }
22574 }
22575 partial_unref(ret_pt);
22576 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022577 rettv->v_type = VAR_PARTIAL;
22578 rettv->vval.v_partial = pt;
22579 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022580 }
22581 }
22582
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022583 dict_unref(selfdict);
22584 return ret;
22585}
22586
22587/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022588 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022589 * value).
22590 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022591 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022592alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022593{
Bram Moolenaar33570922005-01-25 22:26:29 +000022594 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022595}
22596
22597/*
22598 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 * The string "s" must have been allocated, it is consumed.
22600 * Return NULL for out of memory, the variable otherwise.
22601 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022602 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022603alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604{
Bram Moolenaar33570922005-01-25 22:26:29 +000022605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022607 rettv = alloc_tv();
22608 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022610 rettv->v_type = VAR_STRING;
22611 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 }
22613 else
22614 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022615 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616}
22617
22618/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022619 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022622free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623{
22624 if (varp != NULL)
22625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022626 switch (varp->v_type)
22627 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022628 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022629 func_unref(varp->vval.v_string);
22630 /*FALLTHROUGH*/
22631 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022632 vim_free(varp->vval.v_string);
22633 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022634 case VAR_PARTIAL:
22635 partial_unref(varp->vval.v_partial);
22636 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022637 case VAR_LIST:
22638 list_unref(varp->vval.v_list);
22639 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022640 case VAR_DICT:
22641 dict_unref(varp->vval.v_dict);
22642 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022643 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022644#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022645 job_unref(varp->vval.v_job);
22646 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022647#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022648 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022649#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022650 channel_unref(varp->vval.v_channel);
22651 break;
22652#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022653 case VAR_NUMBER:
22654 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022655 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022656 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022657 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 vim_free(varp);
22660 }
22661}
22662
22663/*
22664 * Free the memory for a variable value and set the value to NULL or 0.
22665 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022667clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668{
22669 if (varp != NULL)
22670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022671 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022673 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022674 func_unref(varp->vval.v_string);
22675 /*FALLTHROUGH*/
22676 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022677 vim_free(varp->vval.v_string);
22678 varp->vval.v_string = NULL;
22679 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022680 case VAR_PARTIAL:
22681 partial_unref(varp->vval.v_partial);
22682 varp->vval.v_partial = NULL;
22683 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022684 case VAR_LIST:
22685 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022686 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022687 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022688 case VAR_DICT:
22689 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022690 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022691 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022692 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022693 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022694 varp->vval.v_number = 0;
22695 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022696 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022697#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022698 varp->vval.v_float = 0.0;
22699 break;
22700#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022701 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022702#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022703 job_unref(varp->vval.v_job);
22704 varp->vval.v_job = NULL;
22705#endif
22706 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022707 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022708#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022709 channel_unref(varp->vval.v_channel);
22710 varp->vval.v_channel = NULL;
22711#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022712 case VAR_UNKNOWN:
22713 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022715 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 }
22717}
22718
22719/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022720 * Set the value of a variable to NULL without freeing items.
22721 */
22722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022723init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022724{
22725 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022726 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022727}
22728
22729/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 * Get the number value of a variable.
22731 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022732 * For incompatible types, return 0.
22733 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22734 * caller of incompatible types: it sets *denote to TRUE if "denote"
22735 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022737 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022738get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022740 int error = FALSE;
22741
22742 return get_tv_number_chk(varp, &error); /* return 0L on error */
22743}
22744
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022745 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022746get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022747{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022748 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022750 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022752 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022753 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022754 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022755#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022756 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022757 break;
22758#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022759 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022760 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022761 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022762 break;
22763 case VAR_STRING:
22764 if (varp->vval.v_string != NULL)
22765 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022766 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022767 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022768 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022769 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022770 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022771 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022772 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022773 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022774 case VAR_SPECIAL:
22775 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22776 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022777 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022778#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022779 EMSG(_("E910: Using a Job as a Number"));
22780 break;
22781#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022782 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022783#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022784 EMSG(_("E913: Using a Channel as a Number"));
22785 break;
22786#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022787 case VAR_UNKNOWN:
22788 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022789 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022791 if (denote == NULL) /* useful for values that must be unsigned */
22792 n = -1;
22793 else
22794 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022795 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796}
22797
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022798#ifdef FEAT_FLOAT
22799 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022800get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022801{
22802 switch (varp->v_type)
22803 {
22804 case VAR_NUMBER:
22805 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022806 case VAR_FLOAT:
22807 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022808 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022809 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022810 EMSG(_("E891: Using a Funcref as a Float"));
22811 break;
22812 case VAR_STRING:
22813 EMSG(_("E892: Using a String as a Float"));
22814 break;
22815 case VAR_LIST:
22816 EMSG(_("E893: Using a List as a Float"));
22817 break;
22818 case VAR_DICT:
22819 EMSG(_("E894: Using a Dictionary as a Float"));
22820 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022821 case VAR_SPECIAL:
22822 EMSG(_("E907: Using a special value as a Float"));
22823 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022824 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022825# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022826 EMSG(_("E911: Using a Job as a Float"));
22827 break;
22828# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022829 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022830# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022831 EMSG(_("E914: Using a Channel as a Float"));
22832 break;
22833# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022834 case VAR_UNKNOWN:
22835 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022836 break;
22837 }
22838 return 0;
22839}
22840#endif
22841
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022843 * Get the lnum from the first argument.
22844 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022845 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 */
22847 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022848get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849{
Bram Moolenaar33570922005-01-25 22:26:29 +000022850 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 linenr_T lnum;
22852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022853 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 if (lnum == 0) /* no valid number, try using line() */
22855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022856 rettv.v_type = VAR_NUMBER;
22857 f_line(argvars, &rettv);
22858 lnum = rettv.vval.v_number;
22859 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 }
22861 return lnum;
22862}
22863
22864/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022865 * Get the lnum from the first argument.
22866 * Also accepts "$", then "buf" is used.
22867 * Returns 0 on error.
22868 */
22869 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022870get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022871{
22872 if (argvars[0].v_type == VAR_STRING
22873 && argvars[0].vval.v_string != NULL
22874 && argvars[0].vval.v_string[0] == '$'
22875 && buf != NULL)
22876 return buf->b_ml.ml_line_count;
22877 return get_tv_number_chk(&argvars[0], NULL);
22878}
22879
22880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 * Get the string value of a variable.
22882 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022883 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22884 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 * If the String variable has never been set, return an empty string.
22886 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022887 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22888 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022890 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022891get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892{
22893 static char_u mybuf[NUMBUFLEN];
22894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022895 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896}
22897
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022898 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022899get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022901 char_u *res = get_tv_string_buf_chk(varp, buf);
22902
22903 return res != NULL ? res : (char_u *)"";
22904}
22905
Bram Moolenaar7d647822014-04-05 21:28:56 +020022906/*
22907 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22908 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022909 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022910get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022911{
22912 static char_u mybuf[NUMBUFLEN];
22913
22914 return get_tv_string_buf_chk(varp, mybuf);
22915}
22916
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022917 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022918get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022919{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022920 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022922 case VAR_NUMBER:
22923 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22924 return buf;
22925 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022926 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022927 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022928 break;
22929 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022930 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022931 break;
22932 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022933 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022934 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022935 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022936#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022937 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022938 break;
22939#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022940 case VAR_STRING:
22941 if (varp->vval.v_string != NULL)
22942 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022943 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022944 case VAR_SPECIAL:
22945 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22946 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022947 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022948#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022949 {
22950 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022951 char *status;
22952
22953 if (job == NULL)
22954 return (char_u *)"no process";
22955 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022956 : job->jv_status == JOB_ENDED ? "dead"
22957 : "run";
22958# ifdef UNIX
22959 vim_snprintf((char *)buf, NUMBUFLEN,
22960 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022961# elif defined(WIN32)
22962 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022963 "process %ld %s",
22964 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022965 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022966# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022967 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022968 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22969# endif
22970 return buf;
22971 }
22972#endif
22973 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022974 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022975#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022976 {
22977 channel_T *channel = varp->vval.v_channel;
22978 char *status = channel_status(channel);
22979
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022980 if (channel == NULL)
22981 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22982 else
22983 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022984 "channel %d %s", channel->ch_id, status);
22985 return buf;
22986 }
22987#endif
22988 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022989 case VAR_UNKNOWN:
22990 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022991 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022993 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994}
22995
22996/*
22997 * Find variable "name" in the list of variables.
22998 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022999 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023000 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023001 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023003 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023004find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023007 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008
Bram Moolenaara7043832005-01-21 11:56:39 +000023009 ht = find_var_ht(name, &varname);
23010 if (htp != NULL)
23011 *htp = ht;
23012 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023014 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015}
23016
23017/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023018 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023019 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023022find_var_in_ht(
23023 hashtab_T *ht,
23024 int htname,
23025 char_u *varname,
23026 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023027{
Bram Moolenaar33570922005-01-25 22:26:29 +000023028 hashitem_T *hi;
23029
23030 if (*varname == NUL)
23031 {
23032 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023033 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023034 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023035 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023036 case 'g': return &globvars_var;
23037 case 'v': return &vimvars_var;
23038 case 'b': return &curbuf->b_bufvar;
23039 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023040#ifdef FEAT_WINDOWS
23041 case 't': return &curtab->tp_winvar;
23042#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023043 case 'l': return current_funccal == NULL
23044 ? NULL : &current_funccal->l_vars_var;
23045 case 'a': return current_funccal == NULL
23046 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023047 }
23048 return NULL;
23049 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023050
23051 hi = hash_find(ht, varname);
23052 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023053 {
23054 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023055 * worked find the variable again. Don't auto-load a script if it was
23056 * loaded already, otherwise it would be loaded every time when
23057 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023058 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023059 {
23060 /* Note: script_autoload() may make "hi" invalid. It must either
23061 * be obtained again or not used. */
23062 if (!script_autoload(varname, FALSE) || aborting())
23063 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023064 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023065 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023066 if (HASHITEM_EMPTY(hi))
23067 return NULL;
23068 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023069 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023070}
23071
23072/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023073 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023074 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023075 * Set "varname" to the start of name without ':'.
23076 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023077 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023078find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023080 hashitem_T *hi;
23081
Bram Moolenaar73627d02015-08-11 15:46:09 +020023082 if (name[0] == NUL)
23083 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023084 if (name[1] != ':')
23085 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023086 /* The name must not start with a colon or #. */
23087 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088 return NULL;
23089 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023090
23091 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023092 hi = hash_find(&compat_hashtab, name);
23093 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023094 return &compat_hashtab;
23095
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023097 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023098 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 }
23100 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023101 if (*name == 'g') /* global variable */
23102 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023103 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23104 */
23105 if (vim_strchr(name + 2, ':') != NULL
23106 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023107 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023109 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023111 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023112#ifdef FEAT_WINDOWS
23113 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023114 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023115#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023116 if (*name == 'v') /* v: variable */
23117 return &vimvarht;
23118 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023119 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023120 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023121 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 if (*name == 's' /* script variable */
23123 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23124 return &SCRIPT_VARS(current_SID);
23125 return NULL;
23126}
23127
23128/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023129 * Get function call environment based on bactrace debug level
23130 */
23131 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023132get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023133{
23134 int i;
23135 funccall_T *funccal;
23136 funccall_T *temp_funccal;
23137
23138 funccal = current_funccal;
23139 if (debug_backtrace_level > 0)
23140 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023141 for (i = 0; i < debug_backtrace_level; i++)
23142 {
23143 temp_funccal = funccal->caller;
23144 if (temp_funccal)
23145 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023146 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023147 /* backtrace level overflow. reset to max */
23148 debug_backtrace_level = i;
23149 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023150 }
23151 return funccal;
23152}
23153
23154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023156 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 * Returns NULL when it doesn't exist.
23158 */
23159 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023160get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161{
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023164 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 if (v == NULL)
23166 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023167 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168}
23169
23170/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023171 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 * sourcing this script and when executing functions defined in the script.
23173 */
23174 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023175new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176{
Bram Moolenaara7043832005-01-21 11:56:39 +000023177 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023178 hashtab_T *ht;
23179 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023180
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23182 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023183 /* Re-allocating ga_data means that an ht_array pointing to
23184 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023185 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023186 for (i = 1; i <= ga_scripts.ga_len; ++i)
23187 {
23188 ht = &SCRIPT_VARS(i);
23189 if (ht->ht_mask == HT_INIT_SIZE - 1)
23190 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023191 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023192 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023193 }
23194
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 while (ga_scripts.ga_len < id)
23196 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023197 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023198 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023199 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023200 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 }
23202 }
23203}
23204
23205/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023206 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23207 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 */
23209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023210init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211{
Bram Moolenaar33570922005-01-25 22:26:29 +000023212 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023213 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023214 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023215 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023216 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023217 dict_var->di_tv.vval.v_dict = dict;
23218 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023219 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023220 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23221 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222}
23223
23224/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023225 * Unreference a dictionary initialized by init_var_dict().
23226 */
23227 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023228unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023229{
23230 /* Now the dict needs to be freed if no one else is using it, go back to
23231 * normal reference counting. */
23232 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23233 dict_unref(dict);
23234}
23235
23236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023238 * Frees all allocated variables and the value they contain.
23239 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 */
23241 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023242vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023243{
23244 vars_clear_ext(ht, TRUE);
23245}
23246
23247/*
23248 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23249 */
23250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023251vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252{
Bram Moolenaara7043832005-01-21 11:56:39 +000023253 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023254 hashitem_T *hi;
23255 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256
Bram Moolenaar33570922005-01-25 22:26:29 +000023257 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023258 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023259 for (hi = ht->ht_array; todo > 0; ++hi)
23260 {
23261 if (!HASHITEM_EMPTY(hi))
23262 {
23263 --todo;
23264
Bram Moolenaar33570922005-01-25 22:26:29 +000023265 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023266 * ht_array might change then. hash_clear() takes care of it
23267 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023268 v = HI2DI(hi);
23269 if (free_val)
23270 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023271 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023272 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023273 }
23274 }
23275 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023276 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277}
23278
Bram Moolenaara7043832005-01-21 11:56:39 +000023279/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023280 * Delete a variable from hashtab "ht" at item "hi".
23281 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023284delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285{
Bram Moolenaar33570922005-01-25 22:26:29 +000023286 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023287
23288 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023289 clear_tv(&di->di_tv);
23290 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291}
23292
23293/*
23294 * List the value of one internal variable.
23295 */
23296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023297list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023299 char_u *tofree;
23300 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023301 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023302
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023303 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023304 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023305 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023306 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307}
23308
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023310list_one_var_a(
23311 char_u *prefix,
23312 char_u *name,
23313 int type,
23314 char_u *string,
23315 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316{
Bram Moolenaar31859182007-08-14 20:41:13 +000023317 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23318 msg_start();
23319 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023320 if (name != NULL) /* "a:" vars don't have a name stored */
23321 msg_puts(name);
23322 msg_putchar(' ');
23323 msg_advance(22);
23324 if (type == VAR_NUMBER)
23325 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023326 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023327 msg_putchar('*');
23328 else if (type == VAR_LIST)
23329 {
23330 msg_putchar('[');
23331 if (*string == '[')
23332 ++string;
23333 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023334 else if (type == VAR_DICT)
23335 {
23336 msg_putchar('{');
23337 if (*string == '{')
23338 ++string;
23339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 else
23341 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023342
Bram Moolenaar071d4272004-06-13 20:20:40 +000023343 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023344
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023345 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023346 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023347 if (*first)
23348 {
23349 msg_clr_eos();
23350 *first = FALSE;
23351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352}
23353
23354/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023355 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 * If the variable already exists, the value is updated.
23357 * Otherwise the variable is created.
23358 */
23359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023360set_var(
23361 char_u *name,
23362 typval_T *tv,
23363 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364{
Bram Moolenaar33570922005-01-25 22:26:29 +000023365 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023367 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023369 ht = find_var_ht(name, &varname);
23370 if (ht == NULL || *varname == NUL)
23371 {
23372 EMSG2(_(e_illvar), name);
23373 return;
23374 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023375 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023376
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023377 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23378 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023379 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023380
Bram Moolenaar33570922005-01-25 22:26:29 +000023381 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023383 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023384 if (var_check_ro(v->di_flags, name, FALSE)
23385 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023386 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023387
23388 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023389 * Handle setting internal v: variables separately where needed to
23390 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023391 */
23392 if (ht == &vimvarht)
23393 {
23394 if (v->di_tv.v_type == VAR_STRING)
23395 {
23396 vim_free(v->di_tv.vval.v_string);
23397 if (copy || tv->v_type != VAR_STRING)
23398 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23399 else
23400 {
23401 /* Take over the string to avoid an extra alloc/free. */
23402 v->di_tv.vval.v_string = tv->vval.v_string;
23403 tv->vval.v_string = NULL;
23404 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023405 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023406 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023407 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023408 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023409 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023410 if (STRCMP(varname, "searchforward") == 0)
23411 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023412#ifdef FEAT_SEARCH_EXTRA
23413 else if (STRCMP(varname, "hlsearch") == 0)
23414 {
23415 no_hlsearch = !v->di_tv.vval.v_number;
23416 redraw_all_later(SOME_VALID);
23417 }
23418#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023419 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023420 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023421 else if (v->di_tv.v_type != tv->v_type)
23422 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023423 }
23424
23425 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 }
23427 else /* add a new variable */
23428 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023429 /* Can't add "v:" variable. */
23430 if (ht == &vimvarht)
23431 {
23432 EMSG2(_(e_illvar), name);
23433 return;
23434 }
23435
Bram Moolenaar92124a32005-06-17 22:03:40 +000023436 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023437 if (!valid_varname(varname))
23438 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023439
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023440 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23441 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023442 if (v == NULL)
23443 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023444 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023445 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023447 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448 return;
23449 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023450 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023452
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023453 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023454 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023455 else
23456 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023457 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023458 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023459 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461}
23462
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023463/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023464 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023465 * Also give an error message.
23466 */
23467 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023468var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023469{
23470 if (flags & DI_FLAGS_RO)
23471 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023472 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023473 return TRUE;
23474 }
23475 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23476 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023477 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023478 return TRUE;
23479 }
23480 return FALSE;
23481}
23482
23483/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023484 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23485 * Also give an error message.
23486 */
23487 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023488var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023489{
23490 if (flags & DI_FLAGS_FIX)
23491 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023492 EMSG2(_("E795: Cannot delete variable %s"),
23493 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023494 return TRUE;
23495 }
23496 return FALSE;
23497}
23498
23499/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023500 * Check if a funcref is assigned to a valid variable name.
23501 * Return TRUE and give an error if not.
23502 */
23503 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023504var_check_func_name(
23505 char_u *name, /* points to start of variable name */
23506 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023507{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023508 /* Allow for w: b: s: and t:. */
23509 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023510 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23511 ? name[2] : name[0]))
23512 {
23513 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23514 name);
23515 return TRUE;
23516 }
23517 /* Don't allow hiding a function. When "v" is not NULL we might be
23518 * assigning another function to the same var, the type is checked
23519 * below. */
23520 if (new_var && function_exists(name))
23521 {
23522 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23523 name);
23524 return TRUE;
23525 }
23526 return FALSE;
23527}
23528
23529/*
23530 * Check if a variable name is valid.
23531 * Return FALSE and give an error if not.
23532 */
23533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023534valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023535{
23536 char_u *p;
23537
23538 for (p = varname; *p != NUL; ++p)
23539 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23540 && *p != AUTOLOAD_CHAR)
23541 {
23542 EMSG2(_(e_illvar), varname);
23543 return FALSE;
23544 }
23545 return TRUE;
23546}
23547
23548/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023549 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023550 * Also give an error message, using "name" or _("name") when use_gettext is
23551 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023552 */
23553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023554tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023555{
23556 if (lock & VAR_LOCKED)
23557 {
23558 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023559 name == NULL ? (char_u *)_("Unknown")
23560 : use_gettext ? (char_u *)_(name)
23561 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023562 return TRUE;
23563 }
23564 if (lock & VAR_FIXED)
23565 {
23566 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023567 name == NULL ? (char_u *)_("Unknown")
23568 : use_gettext ? (char_u *)_(name)
23569 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023570 return TRUE;
23571 }
23572 return FALSE;
23573}
23574
23575/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023576 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023577 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023578 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023579 * It is OK for "from" and "to" to point to the same item. This is used to
23580 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023581 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023583copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023585 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023586 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023587 switch (from->v_type)
23588 {
23589 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023590 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023591 to->vval.v_number = from->vval.v_number;
23592 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023593 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023594#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023595 to->vval.v_float = from->vval.v_float;
23596 break;
23597#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023598 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023599#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023600 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023601 if (to->vval.v_job != NULL)
23602 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023603 break;
23604#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023605 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023606#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023607 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023608 if (to->vval.v_channel != NULL)
23609 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023610 break;
23611#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023612 case VAR_STRING:
23613 case VAR_FUNC:
23614 if (from->vval.v_string == NULL)
23615 to->vval.v_string = NULL;
23616 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023618 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023619 if (from->v_type == VAR_FUNC)
23620 func_ref(to->vval.v_string);
23621 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023622 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023623 case VAR_PARTIAL:
23624 if (from->vval.v_partial == NULL)
23625 to->vval.v_partial = NULL;
23626 else
23627 {
23628 to->vval.v_partial = from->vval.v_partial;
23629 ++to->vval.v_partial->pt_refcount;
23630 }
23631 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023632 case VAR_LIST:
23633 if (from->vval.v_list == NULL)
23634 to->vval.v_list = NULL;
23635 else
23636 {
23637 to->vval.v_list = from->vval.v_list;
23638 ++to->vval.v_list->lv_refcount;
23639 }
23640 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023641 case VAR_DICT:
23642 if (from->vval.v_dict == NULL)
23643 to->vval.v_dict = NULL;
23644 else
23645 {
23646 to->vval.v_dict = from->vval.v_dict;
23647 ++to->vval.v_dict->dv_refcount;
23648 }
23649 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023650 case VAR_UNKNOWN:
23651 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023652 break;
23653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654}
23655
23656/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023657 * Make a copy of an item.
23658 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023659 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23660 * reference to an already copied list/dict can be used.
23661 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023662 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023663 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023664item_copy(
23665 typval_T *from,
23666 typval_T *to,
23667 int deep,
23668 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023669{
23670 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023671 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023672
Bram Moolenaar33570922005-01-25 22:26:29 +000023673 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023674 {
23675 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023676 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023677 }
23678 ++recurse;
23679
23680 switch (from->v_type)
23681 {
23682 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023683 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023684 case VAR_STRING:
23685 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023686 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023687 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023688 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023689 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023690 copy_tv(from, to);
23691 break;
23692 case VAR_LIST:
23693 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023694 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023695 if (from->vval.v_list == NULL)
23696 to->vval.v_list = NULL;
23697 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23698 {
23699 /* use the copy made earlier */
23700 to->vval.v_list = from->vval.v_list->lv_copylist;
23701 ++to->vval.v_list->lv_refcount;
23702 }
23703 else
23704 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23705 if (to->vval.v_list == NULL)
23706 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023707 break;
23708 case VAR_DICT:
23709 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023710 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023711 if (from->vval.v_dict == NULL)
23712 to->vval.v_dict = NULL;
23713 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23714 {
23715 /* use the copy made earlier */
23716 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23717 ++to->vval.v_dict->dv_refcount;
23718 }
23719 else
23720 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23721 if (to->vval.v_dict == NULL)
23722 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023723 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023724 case VAR_UNKNOWN:
23725 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023726 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023727 }
23728 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023729 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023730}
23731
23732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023733 * ":echo expr1 ..." print each argument separated with a space, add a
23734 * newline at the end.
23735 * ":echon expr1 ..." print each argument plain.
23736 */
23737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023738ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739{
23740 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023741 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023742 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743 char_u *p;
23744 int needclr = TRUE;
23745 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023746 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747
23748 if (eap->skip)
23749 ++emsg_skip;
23750 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23751 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023752 /* If eval1() causes an error message the text from the command may
23753 * still need to be cleared. E.g., "echo 22,44". */
23754 need_clr_eos = needclr;
23755
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023757 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023758 {
23759 /*
23760 * Report the invalid expression unless the expression evaluation
23761 * has been cancelled due to an aborting error, an interrupt, or an
23762 * exception.
23763 */
23764 if (!aborting())
23765 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023766 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 break;
23768 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023769 need_clr_eos = FALSE;
23770
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 if (!eap->skip)
23772 {
23773 if (atstart)
23774 {
23775 atstart = FALSE;
23776 /* Call msg_start() after eval1(), evaluating the expression
23777 * may cause a message to appear. */
23778 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023779 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023780 /* Mark the saved text as finishing the line, so that what
23781 * follows is displayed on a new line when scrolling back
23782 * at the more prompt. */
23783 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023786 }
23787 else if (eap->cmdidx == CMD_echo)
23788 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023789 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023790 if (p != NULL)
23791 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023793 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023795 if (*p != TAB && needclr)
23796 {
23797 /* remove any text still there from the command */
23798 msg_clr_eos();
23799 needclr = FALSE;
23800 }
23801 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802 }
23803 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023804 {
23805#ifdef FEAT_MBYTE
23806 if (has_mbyte)
23807 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023808 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023809
23810 (void)msg_outtrans_len_attr(p, i, echo_attr);
23811 p += i - 1;
23812 }
23813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023815 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023818 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023820 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821 arg = skipwhite(arg);
23822 }
23823 eap->nextcmd = check_nextcmd(arg);
23824
23825 if (eap->skip)
23826 --emsg_skip;
23827 else
23828 {
23829 /* remove text that may still be there from the command */
23830 if (needclr)
23831 msg_clr_eos();
23832 if (eap->cmdidx == CMD_echo)
23833 msg_end();
23834 }
23835}
23836
23837/*
23838 * ":echohl {name}".
23839 */
23840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023841ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842{
23843 int id;
23844
23845 id = syn_name2id(eap->arg);
23846 if (id == 0)
23847 echo_attr = 0;
23848 else
23849 echo_attr = syn_id2attr(id);
23850}
23851
23852/*
23853 * ":execute expr1 ..." execute the result of an expression.
23854 * ":echomsg expr1 ..." Print a message
23855 * ":echoerr expr1 ..." Print an error
23856 * Each gets spaces around each argument and a newline at the end for
23857 * echo commands
23858 */
23859 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023860ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861{
23862 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023863 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 int ret = OK;
23865 char_u *p;
23866 garray_T ga;
23867 int len;
23868 int save_did_emsg;
23869
23870 ga_init2(&ga, 1, 80);
23871
23872 if (eap->skip)
23873 ++emsg_skip;
23874 while (*arg != NUL && *arg != '|' && *arg != '\n')
23875 {
23876 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023877 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878 {
23879 /*
23880 * Report the invalid expression unless the expression evaluation
23881 * has been cancelled due to an aborting error, an interrupt, or an
23882 * exception.
23883 */
23884 if (!aborting())
23885 EMSG2(_(e_invexpr2), p);
23886 ret = FAIL;
23887 break;
23888 }
23889
23890 if (!eap->skip)
23891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023892 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893 len = (int)STRLEN(p);
23894 if (ga_grow(&ga, len + 2) == FAIL)
23895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023896 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 ret = FAIL;
23898 break;
23899 }
23900 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 ga.ga_len += len;
23904 }
23905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023906 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 arg = skipwhite(arg);
23908 }
23909
23910 if (ret != FAIL && ga.ga_data != NULL)
23911 {
23912 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023914 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023915 out_flush();
23916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 else if (eap->cmdidx == CMD_echoerr)
23918 {
23919 /* We don't want to abort following commands, restore did_emsg. */
23920 save_did_emsg = did_emsg;
23921 EMSG((char_u *)ga.ga_data);
23922 if (!force_abort)
23923 did_emsg = save_did_emsg;
23924 }
23925 else if (eap->cmdidx == CMD_execute)
23926 do_cmdline((char_u *)ga.ga_data,
23927 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23928 }
23929
23930 ga_clear(&ga);
23931
23932 if (eap->skip)
23933 --emsg_skip;
23934
23935 eap->nextcmd = check_nextcmd(arg);
23936}
23937
23938/*
23939 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23940 * "arg" points to the "&" or '+' when called, to "option" when returning.
23941 * Returns NULL when no option name found. Otherwise pointer to the char
23942 * after the option name.
23943 */
23944 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023945find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946{
23947 char_u *p = *arg;
23948
23949 ++p;
23950 if (*p == 'g' && p[1] == ':')
23951 {
23952 *opt_flags = OPT_GLOBAL;
23953 p += 2;
23954 }
23955 else if (*p == 'l' && p[1] == ':')
23956 {
23957 *opt_flags = OPT_LOCAL;
23958 p += 2;
23959 }
23960 else
23961 *opt_flags = 0;
23962
23963 if (!ASCII_ISALPHA(*p))
23964 return NULL;
23965 *arg = p;
23966
23967 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23968 p += 4; /* termcap option */
23969 else
23970 while (ASCII_ISALPHA(*p))
23971 ++p;
23972 return p;
23973}
23974
23975/*
23976 * ":function"
23977 */
23978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023979ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023980{
23981 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023982 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983 int j;
23984 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023985 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023986 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 char_u *name = NULL;
23988 char_u *p;
23989 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023990 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991 garray_T newargs;
23992 garray_T newlines;
23993 int varargs = FALSE;
23994 int mustend = FALSE;
23995 int flags = 0;
23996 ufunc_T *fp;
23997 int indent;
23998 int nesting;
23999 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024000 dictitem_T *v;
24001 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024002 static int func_nr = 0; /* number for nameless function */
24003 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024004 hashtab_T *ht;
24005 int todo;
24006 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024007 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008
24009 /*
24010 * ":function" without argument: list functions.
24011 */
24012 if (ends_excmd(*eap->arg))
24013 {
24014 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024015 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024016 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024017 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024018 {
24019 if (!HASHITEM_EMPTY(hi))
24020 {
24021 --todo;
24022 fp = HI2UF(hi);
24023 if (!isdigit(*fp->uf_name))
24024 list_func_head(fp, FALSE);
24025 }
24026 }
24027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024028 eap->nextcmd = check_nextcmd(eap->arg);
24029 return;
24030 }
24031
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024032 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024033 * ":function /pat": list functions matching pattern.
24034 */
24035 if (*eap->arg == '/')
24036 {
24037 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24038 if (!eap->skip)
24039 {
24040 regmatch_T regmatch;
24041
24042 c = *p;
24043 *p = NUL;
24044 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24045 *p = c;
24046 if (regmatch.regprog != NULL)
24047 {
24048 regmatch.rm_ic = p_ic;
24049
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024050 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024051 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24052 {
24053 if (!HASHITEM_EMPTY(hi))
24054 {
24055 --todo;
24056 fp = HI2UF(hi);
24057 if (!isdigit(*fp->uf_name)
24058 && vim_regexec(&regmatch, fp->uf_name, 0))
24059 list_func_head(fp, FALSE);
24060 }
24061 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024062 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024063 }
24064 }
24065 if (*p == '/')
24066 ++p;
24067 eap->nextcmd = check_nextcmd(p);
24068 return;
24069 }
24070
24071 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024072 * Get the function name. There are these situations:
24073 * func normal function name
24074 * "name" == func, "fudi.fd_dict" == NULL
24075 * dict.func new dictionary entry
24076 * "name" == NULL, "fudi.fd_dict" set,
24077 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24078 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024079 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024080 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24081 * dict.func existing dict entry that's not a Funcref
24082 * "name" == NULL, "fudi.fd_dict" set,
24083 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024084 * s:func script-local function name
24085 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024088 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024089 paren = (vim_strchr(p, '(') != NULL);
24090 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024091 {
24092 /*
24093 * Return on an invalid expression in braces, unless the expression
24094 * evaluation has been cancelled due to an aborting error, an
24095 * interrupt, or an exception.
24096 */
24097 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024099 if (!eap->skip && fudi.fd_newkey != NULL)
24100 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024101 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024102 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104 else
24105 eap->skip = TRUE;
24106 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024107
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108 /* An error in a function call during evaluation of an expression in magic
24109 * braces should not cause the function not to be defined. */
24110 saved_did_emsg = did_emsg;
24111 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112
24113 /*
24114 * ":function func" with only function name: list function.
24115 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024116 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117 {
24118 if (!ends_excmd(*skipwhite(p)))
24119 {
24120 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024121 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 }
24123 eap->nextcmd = check_nextcmd(p);
24124 if (eap->nextcmd != NULL)
24125 *p = NUL;
24126 if (!eap->skip && !got_int)
24127 {
24128 fp = find_func(name);
24129 if (fp != NULL)
24130 {
24131 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024132 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024134 if (FUNCLINE(fp, j) == NULL)
24135 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136 msg_putchar('\n');
24137 msg_outnum((long)(j + 1));
24138 if (j < 9)
24139 msg_putchar(' ');
24140 if (j < 99)
24141 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024142 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 out_flush(); /* show a line at a time */
24144 ui_breakcheck();
24145 }
24146 if (!got_int)
24147 {
24148 msg_putchar('\n');
24149 msg_puts((char_u *)" endfunction");
24150 }
24151 }
24152 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024153 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024155 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024156 }
24157
24158 /*
24159 * ":function name(arg1, arg2)" Define function.
24160 */
24161 p = skipwhite(p);
24162 if (*p != '(')
24163 {
24164 if (!eap->skip)
24165 {
24166 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024167 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168 }
24169 /* attempt to continue by skipping some text */
24170 if (vim_strchr(p, '(') != NULL)
24171 p = vim_strchr(p, '(');
24172 }
24173 p = skipwhite(p + 1);
24174
24175 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24176 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24177
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024178 if (!eap->skip)
24179 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024180 /* Check the name of the function. Unless it's a dictionary function
24181 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024182 if (name != NULL)
24183 arg = name;
24184 else
24185 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024186 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024187 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24188 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024189 {
24190 if (*arg == K_SPECIAL)
24191 j = 3;
24192 else
24193 j = 0;
24194 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24195 : eval_isnamec(arg[j])))
24196 ++j;
24197 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024198 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024199 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024200 /* Disallow using the g: dict. */
24201 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24202 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024203 }
24204
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 /*
24206 * Isolate the arguments: "arg1, arg2, ...)"
24207 */
24208 while (*p != ')')
24209 {
24210 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24211 {
24212 varargs = TRUE;
24213 p += 3;
24214 mustend = TRUE;
24215 }
24216 else
24217 {
24218 arg = p;
24219 while (ASCII_ISALNUM(*p) || *p == '_')
24220 ++p;
24221 if (arg == p || isdigit(*arg)
24222 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24223 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24224 {
24225 if (!eap->skip)
24226 EMSG2(_("E125: Illegal argument: %s"), arg);
24227 break;
24228 }
24229 if (ga_grow(&newargs, 1) == FAIL)
24230 goto erret;
24231 c = *p;
24232 *p = NUL;
24233 arg = vim_strsave(arg);
24234 if (arg == NULL)
24235 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024236
24237 /* Check for duplicate argument name. */
24238 for (i = 0; i < newargs.ga_len; ++i)
24239 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24240 {
24241 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024242 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024243 goto erret;
24244 }
24245
Bram Moolenaar071d4272004-06-13 20:20:40 +000024246 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24247 *p = c;
24248 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024249 if (*p == ',')
24250 ++p;
24251 else
24252 mustend = TRUE;
24253 }
24254 p = skipwhite(p);
24255 if (mustend && *p != ')')
24256 {
24257 if (!eap->skip)
24258 EMSG2(_(e_invarg2), eap->arg);
24259 break;
24260 }
24261 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024262 if (*p != ')')
24263 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024264 ++p; /* skip the ')' */
24265
Bram Moolenaare9a41262005-01-15 22:18:47 +000024266 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267 for (;;)
24268 {
24269 p = skipwhite(p);
24270 if (STRNCMP(p, "range", 5) == 0)
24271 {
24272 flags |= FC_RANGE;
24273 p += 5;
24274 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024275 else if (STRNCMP(p, "dict", 4) == 0)
24276 {
24277 flags |= FC_DICT;
24278 p += 4;
24279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024280 else if (STRNCMP(p, "abort", 5) == 0)
24281 {
24282 flags |= FC_ABORT;
24283 p += 5;
24284 }
24285 else
24286 break;
24287 }
24288
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024289 /* When there is a line break use what follows for the function body.
24290 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24291 if (*p == '\n')
24292 line_arg = p + 1;
24293 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024294 EMSG(_(e_trailing));
24295
24296 /*
24297 * Read the body of the function, until ":endfunction" is found.
24298 */
24299 if (KeyTyped)
24300 {
24301 /* Check if the function already exists, don't let the user type the
24302 * whole function before telling him it doesn't work! For a script we
24303 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024304 if (!eap->skip && !eap->forceit)
24305 {
24306 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24307 EMSG(_(e_funcdict));
24308 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024309 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024311
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024312 if (!eap->skip && did_emsg)
24313 goto erret;
24314
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315 msg_putchar('\n'); /* don't overwrite the function name */
24316 cmdline_row = msg_row;
24317 }
24318
24319 indent = 2;
24320 nesting = 0;
24321 for (;;)
24322 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024323 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024324 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024325 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024326 saved_wait_return = FALSE;
24327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024329 sourcing_lnum_off = sourcing_lnum;
24330
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024331 if (line_arg != NULL)
24332 {
24333 /* Use eap->arg, split up in parts by line breaks. */
24334 theline = line_arg;
24335 p = vim_strchr(theline, '\n');
24336 if (p == NULL)
24337 line_arg += STRLEN(line_arg);
24338 else
24339 {
24340 *p = NUL;
24341 line_arg = p + 1;
24342 }
24343 }
24344 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024345 theline = getcmdline(':', 0L, indent);
24346 else
24347 theline = eap->getline(':', eap->cookie, indent);
24348 if (KeyTyped)
24349 lines_left = Rows - 1;
24350 if (theline == NULL)
24351 {
24352 EMSG(_("E126: Missing :endfunction"));
24353 goto erret;
24354 }
24355
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024356 /* Detect line continuation: sourcing_lnum increased more than one. */
24357 if (sourcing_lnum > sourcing_lnum_off + 1)
24358 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24359 else
24360 sourcing_lnum_off = 0;
24361
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362 if (skip_until != NULL)
24363 {
24364 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24365 * don't check for ":endfunc". */
24366 if (STRCMP(theline, skip_until) == 0)
24367 {
24368 vim_free(skip_until);
24369 skip_until = NULL;
24370 }
24371 }
24372 else
24373 {
24374 /* skip ':' and blanks*/
24375 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24376 ;
24377
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024378 /* Check for "endfunction". */
24379 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024380 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024381 if (line_arg == NULL)
24382 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 break;
24384 }
24385
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024386 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387 * at "end". */
24388 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24389 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024390 else if (STRNCMP(p, "if", 2) == 0
24391 || STRNCMP(p, "wh", 2) == 0
24392 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024393 || STRNCMP(p, "try", 3) == 0)
24394 indent += 2;
24395
24396 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024397 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024399 if (*p == '!')
24400 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024402 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024403 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024405 ++nesting;
24406 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024407 }
24408 }
24409
24410 /* Check for ":append" or ":insert". */
24411 p = skip_range(p, NULL);
24412 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24413 || (p[0] == 'i'
24414 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24415 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24416 skip_until = vim_strsave((char_u *)".");
24417
24418 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24419 arg = skipwhite(skiptowhite(p));
24420 if (arg[0] == '<' && arg[1] =='<'
24421 && ((p[0] == 'p' && p[1] == 'y'
24422 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24423 || (p[0] == 'p' && p[1] == 'e'
24424 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24425 || (p[0] == 't' && p[1] == 'c'
24426 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024427 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24428 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024429 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24430 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024431 || (p[0] == 'm' && p[1] == 'z'
24432 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024433 ))
24434 {
24435 /* ":python <<" continues until a dot, like ":append" */
24436 p = skipwhite(arg + 2);
24437 if (*p == NUL)
24438 skip_until = vim_strsave((char_u *)".");
24439 else
24440 skip_until = vim_strsave(p);
24441 }
24442 }
24443
24444 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024445 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024446 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024447 if (line_arg == NULL)
24448 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024449 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024450 }
24451
24452 /* Copy the line to newly allocated memory. get_one_sourceline()
24453 * allocates 250 bytes per line, this saves 80% on average. The cost
24454 * is an extra alloc/free. */
24455 p = vim_strsave(theline);
24456 if (p != NULL)
24457 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024458 if (line_arg == NULL)
24459 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024460 theline = p;
24461 }
24462
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024463 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24464
24465 /* Add NULL lines for continuation lines, so that the line count is
24466 * equal to the index in the growarray. */
24467 while (sourcing_lnum_off-- > 0)
24468 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024469
24470 /* Check for end of eap->arg. */
24471 if (line_arg != NULL && *line_arg == NUL)
24472 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473 }
24474
24475 /* Don't define the function when skipping commands or when an error was
24476 * detected. */
24477 if (eap->skip || did_emsg)
24478 goto erret;
24479
24480 /*
24481 * If there are no errors, add the function
24482 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024483 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024484 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024485 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024486 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024487 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024488 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024489 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024490 goto erret;
24491 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024492
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024493 fp = find_func(name);
24494 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024495 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024496 if (!eap->forceit)
24497 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024498 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024499 goto erret;
24500 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024501 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024502 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024503 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024504 name);
24505 goto erret;
24506 }
24507 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024508 ga_clear_strings(&(fp->uf_args));
24509 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024510 vim_free(name);
24511 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513 }
24514 else
24515 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024516 char numbuf[20];
24517
24518 fp = NULL;
24519 if (fudi.fd_newkey == NULL && !eap->forceit)
24520 {
24521 EMSG(_(e_funcdict));
24522 goto erret;
24523 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024524 if (fudi.fd_di == NULL)
24525 {
24526 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024527 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024528 goto erret;
24529 }
24530 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024531 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024532 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024533
24534 /* Give the function a sequential number. Can only be used with a
24535 * Funcref! */
24536 vim_free(name);
24537 sprintf(numbuf, "%d", ++func_nr);
24538 name = vim_strsave((char_u *)numbuf);
24539 if (name == NULL)
24540 goto erret;
24541 }
24542
24543 if (fp == NULL)
24544 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024545 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024546 {
24547 int slen, plen;
24548 char_u *scriptname;
24549
24550 /* Check that the autoload name matches the script name. */
24551 j = FAIL;
24552 if (sourcing_name != NULL)
24553 {
24554 scriptname = autoload_name(name);
24555 if (scriptname != NULL)
24556 {
24557 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024558 plen = (int)STRLEN(p);
24559 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024560 if (slen > plen && fnamecmp(p,
24561 sourcing_name + slen - plen) == 0)
24562 j = OK;
24563 vim_free(scriptname);
24564 }
24565 }
24566 if (j == FAIL)
24567 {
24568 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24569 goto erret;
24570 }
24571 }
24572
24573 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574 if (fp == NULL)
24575 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024576
24577 if (fudi.fd_dict != NULL)
24578 {
24579 if (fudi.fd_di == NULL)
24580 {
24581 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024582 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024583 if (fudi.fd_di == NULL)
24584 {
24585 vim_free(fp);
24586 goto erret;
24587 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024588 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24589 {
24590 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024591 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024592 goto erret;
24593 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024594 }
24595 else
24596 /* overwrite existing dict entry */
24597 clear_tv(&fudi.fd_di->di_tv);
24598 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024599 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024600 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024601 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024602
24603 /* behave like "dict" was used */
24604 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024605 }
24606
Bram Moolenaar071d4272004-06-13 20:20:40 +000024607 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024608 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024609 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24610 {
24611 vim_free(fp);
24612 goto erret;
24613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024615 fp->uf_args = newargs;
24616 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024617#ifdef FEAT_PROFILE
24618 fp->uf_tml_count = NULL;
24619 fp->uf_tml_total = NULL;
24620 fp->uf_tml_self = NULL;
24621 fp->uf_profiling = FALSE;
24622 if (prof_def_func())
24623 func_do_profile(fp);
24624#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024625 fp->uf_varargs = varargs;
24626 fp->uf_flags = flags;
24627 fp->uf_calls = 0;
24628 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024629 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630
24631erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 ga_clear_strings(&newargs);
24633 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024634ret_free:
24635 vim_free(skip_until);
24636 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024639 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024640}
24641
24642/*
24643 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024644 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024646 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024647 * TFN_INT: internal function name OK
24648 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024649 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 * Advances "pp" to just after the function name (if no error).
24651 */
24652 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024653trans_function_name(
24654 char_u **pp,
24655 int skip, /* only find the end, don't evaluate */
24656 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024657 funcdict_T *fdp, /* return: info about dictionary used */
24658 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024660 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661 char_u *start;
24662 char_u *end;
24663 int lead;
24664 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024666 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024667
24668 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024669 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024671
24672 /* Check for hard coded <SNR>: already translated function ID (from a user
24673 * command). */
24674 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24675 && (*pp)[2] == (int)KE_SNR)
24676 {
24677 *pp += 3;
24678 len = get_id_len(pp) + 3;
24679 return vim_strnsave(start, len);
24680 }
24681
24682 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24683 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024684 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024685 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024687
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024688 /* Note that TFN_ flags use the same values as GLV_ flags. */
24689 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024690 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024691 if (end == start)
24692 {
24693 if (!skip)
24694 EMSG(_("E129: Function name required"));
24695 goto theend;
24696 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024697 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024698 {
24699 /*
24700 * Report an invalid expression in braces, unless the expression
24701 * evaluation has been cancelled due to an aborting error, an
24702 * interrupt, or an exception.
24703 */
24704 if (!aborting())
24705 {
24706 if (end != NULL)
24707 EMSG2(_(e_invarg2), start);
24708 }
24709 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024710 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024711 goto theend;
24712 }
24713
24714 if (lv.ll_tv != NULL)
24715 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024716 if (fdp != NULL)
24717 {
24718 fdp->fd_dict = lv.ll_dict;
24719 fdp->fd_newkey = lv.ll_newkey;
24720 lv.ll_newkey = NULL;
24721 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024722 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024723 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24724 {
24725 name = vim_strsave(lv.ll_tv->vval.v_string);
24726 *pp = end;
24727 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024728 else if (lv.ll_tv->v_type == VAR_PARTIAL
24729 && lv.ll_tv->vval.v_partial != NULL)
24730 {
24731 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24732 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024733 if (partial != NULL)
24734 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024735 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024736 else
24737 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024738 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24739 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024740 EMSG(_(e_funcref));
24741 else
24742 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024743 name = NULL;
24744 }
24745 goto theend;
24746 }
24747
24748 if (lv.ll_name == NULL)
24749 {
24750 /* Error found, but continue after the function name. */
24751 *pp = end;
24752 goto theend;
24753 }
24754
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024755 /* Check if the name is a Funcref. If so, use the value. */
24756 if (lv.ll_exp_name != NULL)
24757 {
24758 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024759 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024760 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024761 if (name == lv.ll_exp_name)
24762 name = NULL;
24763 }
24764 else
24765 {
24766 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024767 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024768 if (name == *pp)
24769 name = NULL;
24770 }
24771 if (name != NULL)
24772 {
24773 name = vim_strsave(name);
24774 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024775 if (STRNCMP(name, "<SNR>", 5) == 0)
24776 {
24777 /* Change "<SNR>" to the byte sequence. */
24778 name[0] = K_SPECIAL;
24779 name[1] = KS_EXTRA;
24780 name[2] = (int)KE_SNR;
24781 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24782 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024783 goto theend;
24784 }
24785
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024786 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024787 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024788 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024789 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24790 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24791 {
24792 /* When there was "s:" already or the name expanded to get a
24793 * leading "s:" then remove it. */
24794 lv.ll_name += 2;
24795 len -= 2;
24796 lead = 2;
24797 }
24798 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024799 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024800 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024801 /* skip over "s:" and "g:" */
24802 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024803 lv.ll_name += 2;
24804 len = (int)(end - lv.ll_name);
24805 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024806
24807 /*
24808 * Copy the function name to allocated memory.
24809 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24810 * Accept <SNR>123_name() outside a script.
24811 */
24812 if (skip)
24813 lead = 0; /* do nothing */
24814 else if (lead > 0)
24815 {
24816 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024817 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24818 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024819 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024820 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024821 if (current_SID <= 0)
24822 {
24823 EMSG(_(e_usingsid));
24824 goto theend;
24825 }
24826 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24827 lead += (int)STRLEN(sid_buf);
24828 }
24829 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024830 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024831 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024832 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024833 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024834 goto theend;
24835 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024836 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024837 {
24838 char_u *cp = vim_strchr(lv.ll_name, ':');
24839
24840 if (cp != NULL && cp < end)
24841 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024842 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024843 goto theend;
24844 }
24845 }
24846
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024847 name = alloc((unsigned)(len + lead + 1));
24848 if (name != NULL)
24849 {
24850 if (lead > 0)
24851 {
24852 name[0] = K_SPECIAL;
24853 name[1] = KS_EXTRA;
24854 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024855 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024856 STRCPY(name + 3, sid_buf);
24857 }
24858 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024859 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024860 }
24861 *pp = end;
24862
24863theend:
24864 clear_lval(&lv);
24865 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866}
24867
24868/*
24869 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24870 * Return 2 if "p" starts with "s:".
24871 * Return 0 otherwise.
24872 */
24873 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024874eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024876 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24877 * the standard library function. */
24878 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24879 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880 return 5;
24881 if (p[0] == 's' && p[1] == ':')
24882 return 2;
24883 return 0;
24884}
24885
24886/*
24887 * Return TRUE if "p" starts with "<SID>" or "s:".
24888 * Only works if eval_fname_script() returned non-zero for "p"!
24889 */
24890 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024891eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892{
24893 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24894}
24895
24896/*
24897 * List the head of the function: "name(arg1, arg2)".
24898 */
24899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024900list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024901{
24902 int j;
24903
24904 msg_start();
24905 if (indent)
24906 MSG_PUTS(" ");
24907 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024908 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 {
24910 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024911 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912 }
24913 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024914 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024916 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917 {
24918 if (j)
24919 MSG_PUTS(", ");
24920 msg_puts(FUNCARG(fp, j));
24921 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024922 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923 {
24924 if (j)
24925 MSG_PUTS(", ");
24926 MSG_PUTS("...");
24927 }
24928 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024929 if (fp->uf_flags & FC_ABORT)
24930 MSG_PUTS(" abort");
24931 if (fp->uf_flags & FC_RANGE)
24932 MSG_PUTS(" range");
24933 if (fp->uf_flags & FC_DICT)
24934 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024935 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024936 if (p_verbose > 0)
24937 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938}
24939
24940/*
24941 * Find a function by name, return pointer to it in ufuncs.
24942 * Return NULL for unknown function.
24943 */
24944 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024945find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024947 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024948
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024949 hi = hash_find(&func_hashtab, name);
24950 if (!HASHITEM_EMPTY(hi))
24951 return HI2UF(hi);
24952 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024953}
24954
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024955#if defined(EXITFREE) || defined(PROTO)
24956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024957free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024958{
24959 hashitem_T *hi;
24960
24961 /* Need to start all over every time, because func_free() may change the
24962 * hash table. */
24963 while (func_hashtab.ht_used > 0)
24964 for (hi = func_hashtab.ht_array; ; ++hi)
24965 if (!HASHITEM_EMPTY(hi))
24966 {
24967 func_free(HI2UF(hi));
24968 break;
24969 }
24970}
24971#endif
24972
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024973 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024974translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024975{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024976 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024977 return find_internal_func(name) >= 0;
24978 return find_func(name) != NULL;
24979}
24980
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024981/*
24982 * Return TRUE if a function "name" exists.
24983 */
24984 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024985function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024986{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024987 char_u *nm = name;
24988 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024989 int n = FALSE;
24990
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024991 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024992 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024993 nm = skipwhite(nm);
24994
24995 /* Only accept "funcname", "funcname ", "funcname (..." and
24996 * "funcname(...", not "funcname!...". */
24997 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024998 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024999 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025000 return n;
25001}
25002
Bram Moolenaara1544c02013-05-30 12:35:52 +020025003 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025004get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025005{
25006 char_u *nm = name;
25007 char_u *p;
25008
Bram Moolenaar65639032016-03-16 21:40:30 +010025009 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025010
25011 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025012 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025013 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025014
Bram Moolenaara1544c02013-05-30 12:35:52 +020025015 vim_free(p);
25016 return NULL;
25017}
25018
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025019/*
25020 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025021 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25022 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025023 */
25024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025025builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025026{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025027 char_u *p;
25028
25029 if (!ASCII_ISLOWER(name[0]))
25030 return FALSE;
25031 p = vim_strchr(name, AUTOLOAD_CHAR);
25032 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025033}
25034
Bram Moolenaar05159a02005-02-26 23:04:13 +000025035#if defined(FEAT_PROFILE) || defined(PROTO)
25036/*
25037 * Start profiling function "fp".
25038 */
25039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025040func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025041{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025042 int len = fp->uf_lines.ga_len;
25043
25044 if (len == 0)
25045 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025046 fp->uf_tm_count = 0;
25047 profile_zero(&fp->uf_tm_self);
25048 profile_zero(&fp->uf_tm_total);
25049 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025050 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025051 if (fp->uf_tml_total == NULL)
25052 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025053 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025054 if (fp->uf_tml_self == NULL)
25055 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025056 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025057 fp->uf_tml_idx = -1;
25058 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25059 || fp->uf_tml_self == NULL)
25060 return; /* out of memory */
25061
25062 fp->uf_profiling = TRUE;
25063}
25064
25065/*
25066 * Dump the profiling results for all functions in file "fd".
25067 */
25068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025069func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025070{
25071 hashitem_T *hi;
25072 int todo;
25073 ufunc_T *fp;
25074 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025075 ufunc_T **sorttab;
25076 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025077
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025078 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025079 if (todo == 0)
25080 return; /* nothing to dump */
25081
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025082 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025083
Bram Moolenaar05159a02005-02-26 23:04:13 +000025084 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25085 {
25086 if (!HASHITEM_EMPTY(hi))
25087 {
25088 --todo;
25089 fp = HI2UF(hi);
25090 if (fp->uf_profiling)
25091 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025092 if (sorttab != NULL)
25093 sorttab[st_len++] = fp;
25094
Bram Moolenaar05159a02005-02-26 23:04:13 +000025095 if (fp->uf_name[0] == K_SPECIAL)
25096 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25097 else
25098 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25099 if (fp->uf_tm_count == 1)
25100 fprintf(fd, "Called 1 time\n");
25101 else
25102 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25103 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25104 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25105 fprintf(fd, "\n");
25106 fprintf(fd, "count total (s) self (s)\n");
25107
25108 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25109 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025110 if (FUNCLINE(fp, i) == NULL)
25111 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025112 prof_func_line(fd, fp->uf_tml_count[i],
25113 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025114 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25115 }
25116 fprintf(fd, "\n");
25117 }
25118 }
25119 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025120
25121 if (sorttab != NULL && st_len > 0)
25122 {
25123 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25124 prof_total_cmp);
25125 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25126 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25127 prof_self_cmp);
25128 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25129 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025130
25131 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025132}
Bram Moolenaar73830342005-02-28 22:48:19 +000025133
25134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025135prof_sort_list(
25136 FILE *fd,
25137 ufunc_T **sorttab,
25138 int st_len,
25139 char *title,
25140 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025141{
25142 int i;
25143 ufunc_T *fp;
25144
25145 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25146 fprintf(fd, "count total (s) self (s) function\n");
25147 for (i = 0; i < 20 && i < st_len; ++i)
25148 {
25149 fp = sorttab[i];
25150 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25151 prefer_self);
25152 if (fp->uf_name[0] == K_SPECIAL)
25153 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25154 else
25155 fprintf(fd, " %s()\n", fp->uf_name);
25156 }
25157 fprintf(fd, "\n");
25158}
25159
25160/*
25161 * Print the count and times for one function or function line.
25162 */
25163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025164prof_func_line(
25165 FILE *fd,
25166 int count,
25167 proftime_T *total,
25168 proftime_T *self,
25169 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025170{
25171 if (count > 0)
25172 {
25173 fprintf(fd, "%5d ", count);
25174 if (prefer_self && profile_equal(total, self))
25175 fprintf(fd, " ");
25176 else
25177 fprintf(fd, "%s ", profile_msg(total));
25178 if (!prefer_self && profile_equal(total, self))
25179 fprintf(fd, " ");
25180 else
25181 fprintf(fd, "%s ", profile_msg(self));
25182 }
25183 else
25184 fprintf(fd, " ");
25185}
25186
25187/*
25188 * Compare function for total time sorting.
25189 */
25190 static int
25191#ifdef __BORLANDC__
25192_RTLENTRYF
25193#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025194prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025195{
25196 ufunc_T *p1, *p2;
25197
25198 p1 = *(ufunc_T **)s1;
25199 p2 = *(ufunc_T **)s2;
25200 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25201}
25202
25203/*
25204 * Compare function for self time sorting.
25205 */
25206 static int
25207#ifdef __BORLANDC__
25208_RTLENTRYF
25209#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025210prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025211{
25212 ufunc_T *p1, *p2;
25213
25214 p1 = *(ufunc_T **)s1;
25215 p2 = *(ufunc_T **)s2;
25216 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25217}
25218
Bram Moolenaar05159a02005-02-26 23:04:13 +000025219#endif
25220
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025221/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025222 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025223 * Return TRUE if a package was loaded.
25224 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025226script_autoload(
25227 char_u *name,
25228 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025229{
25230 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025231 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025232 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025233 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025234
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025235 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025236 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025237 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025238 return FALSE;
25239
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025240 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025241
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025242 /* Find the name in the list of previously loaded package names. Skip
25243 * "autoload/", it's always the same. */
25244 for (i = 0; i < ga_loaded.ga_len; ++i)
25245 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25246 break;
25247 if (!reload && i < ga_loaded.ga_len)
25248 ret = FALSE; /* was loaded already */
25249 else
25250 {
25251 /* Remember the name if it wasn't loaded already. */
25252 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25253 {
25254 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25255 tofree = NULL;
25256 }
25257
25258 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025259 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025260 ret = TRUE;
25261 }
25262
25263 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025264 return ret;
25265}
25266
25267/*
25268 * Return the autoload script name for a function or variable name.
25269 * Returns NULL when out of memory.
25270 */
25271 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025272autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025273{
25274 char_u *p;
25275 char_u *scriptname;
25276
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025277 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025278 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25279 if (scriptname == NULL)
25280 return FALSE;
25281 STRCPY(scriptname, "autoload/");
25282 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025283 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025284 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025285 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025286 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025287 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025288}
25289
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25291
25292/*
25293 * Function given to ExpandGeneric() to obtain the list of user defined
25294 * function names.
25295 */
25296 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025297get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025298{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025299 static long_u done;
25300 static hashitem_T *hi;
25301 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302
25303 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025304 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025305 done = 0;
25306 hi = func_hashtab.ht_array;
25307 }
25308 if (done < func_hashtab.ht_used)
25309 {
25310 if (done++ > 0)
25311 ++hi;
25312 while (HASHITEM_EMPTY(hi))
25313 ++hi;
25314 fp = HI2UF(hi);
25315
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025316 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025317 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025318
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025319 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25320 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025321
25322 cat_func_name(IObuff, fp);
25323 if (xp->xp_context != EXPAND_USER_FUNC)
25324 {
25325 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025326 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025327 STRCAT(IObuff, ")");
25328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329 return IObuff;
25330 }
25331 return NULL;
25332}
25333
25334#endif /* FEAT_CMDL_COMPL */
25335
25336/*
25337 * Copy the function name of "fp" to buffer "buf".
25338 * "buf" must be able to hold the function name plus three bytes.
25339 * Takes care of script-local function names.
25340 */
25341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025342cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025344 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345 {
25346 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025347 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025348 }
25349 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025350 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351}
25352
25353/*
25354 * ":delfunction {name}"
25355 */
25356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025357ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025358{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025359 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360 char_u *p;
25361 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025362 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363
25364 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025365 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025366 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025368 {
25369 if (fudi.fd_dict != NULL && !eap->skip)
25370 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373 if (!ends_excmd(*skipwhite(p)))
25374 {
25375 vim_free(name);
25376 EMSG(_(e_trailing));
25377 return;
25378 }
25379 eap->nextcmd = check_nextcmd(p);
25380 if (eap->nextcmd != NULL)
25381 *p = NUL;
25382
25383 if (!eap->skip)
25384 fp = find_func(name);
25385 vim_free(name);
25386
25387 if (!eap->skip)
25388 {
25389 if (fp == NULL)
25390 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025391 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025392 return;
25393 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025394 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395 {
25396 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25397 return;
25398 }
25399
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025400 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025402 /* Delete the dict item that refers to the function, it will
25403 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025404 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025405 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025406 else
25407 func_free(fp);
25408 }
25409}
25410
25411/*
25412 * Free a function and remove it from the list of functions.
25413 */
25414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025415func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025416{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025417 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025418
25419 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025420 ga_clear_strings(&(fp->uf_args));
25421 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025422#ifdef FEAT_PROFILE
25423 vim_free(fp->uf_tml_count);
25424 vim_free(fp->uf_tml_total);
25425 vim_free(fp->uf_tml_self);
25426#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025427
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025428 /* remove the function from the function hashtable */
25429 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25430 if (HASHITEM_EMPTY(hi))
25431 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025432 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025433 hash_remove(&func_hashtab, hi);
25434
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025435 vim_free(fp);
25436}
25437
25438/*
25439 * Unreference a Function: decrement the reference count and free it when it
25440 * becomes zero. Only for numbered functions.
25441 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025443func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025444{
25445 ufunc_T *fp;
25446
25447 if (name != NULL && isdigit(*name))
25448 {
25449 fp = find_func(name);
25450 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025451 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025452#ifdef EXITFREE
25453 if (!entered_free_all_mem)
25454#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025455 EMSG2(_(e_intern2), "func_unref()");
25456 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025457 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025458 {
25459 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025460 * when "uf_calls" becomes zero. */
25461 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025462 func_free(fp);
25463 }
25464 }
25465}
25466
25467/*
25468 * Count a reference to a Function.
25469 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025470 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025471func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025472{
25473 ufunc_T *fp;
25474
25475 if (name != NULL && isdigit(*name))
25476 {
25477 fp = find_func(name);
25478 if (fp == NULL)
25479 EMSG2(_(e_intern2), "func_ref()");
25480 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025481 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482 }
25483}
25484
25485/*
25486 * Call a user function.
25487 */
25488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025489call_user_func(
25490 ufunc_T *fp, /* pointer to function */
25491 int argcount, /* nr of args */
25492 typval_T *argvars, /* arguments */
25493 typval_T *rettv, /* return value */
25494 linenr_T firstline, /* first line of range */
25495 linenr_T lastline, /* last line of range */
25496 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497{
Bram Moolenaar33570922005-01-25 22:26:29 +000025498 char_u *save_sourcing_name;
25499 linenr_T save_sourcing_lnum;
25500 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025501 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025502 int save_did_emsg;
25503 static int depth = 0;
25504 dictitem_T *v;
25505 int fixvar_idx = 0; /* index in fixvar[] */
25506 int i;
25507 int ai;
25508 char_u numbuf[NUMBUFLEN];
25509 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025510 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025511#ifdef FEAT_PROFILE
25512 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025513 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515
25516 /* If depth of calling is getting too high, don't execute the function */
25517 if (depth >= p_mfd)
25518 {
25519 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025520 rettv->v_type = VAR_NUMBER;
25521 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522 return;
25523 }
25524 ++depth;
25525
25526 line_breakcheck(); /* check for CTRL-C hit */
25527
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025528 fc = (funccall_T *)alloc(sizeof(funccall_T));
25529 fc->caller = current_funccal;
25530 current_funccal = fc;
25531 fc->func = fp;
25532 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025533 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025534 fc->linenr = 0;
25535 fc->returned = FALSE;
25536 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025537 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025538 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25539 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540
Bram Moolenaar33570922005-01-25 22:26:29 +000025541 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025542 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025543 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25544 * each argument variable and saves a lot of time.
25545 */
25546 /*
25547 * Init l: variables.
25548 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025549 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025550 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025551 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025552 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25553 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025554 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025555 name = v->di_key;
25556 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025557 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025558 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025559 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025560 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025561 v->di_tv.vval.v_dict = selfdict;
25562 ++selfdict->dv_refcount;
25563 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025564
Bram Moolenaar33570922005-01-25 22:26:29 +000025565 /*
25566 * Init a: variables.
25567 * Set a:0 to "argcount".
25568 * Set a:000 to a list with room for the "..." arguments.
25569 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025570 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025571 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025572 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025573 /* Use "name" to avoid a warning from some compiler that checks the
25574 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025575 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025576 name = v->di_key;
25577 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025578 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025579 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025580 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025581 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025582 v->di_tv.vval.v_list = &fc->l_varlist;
25583 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25584 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25585 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025586
25587 /*
25588 * Set a:firstline to "firstline" and a:lastline to "lastline".
25589 * Set a:name to named arguments.
25590 * Set a:N to the "..." arguments.
25591 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025592 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025593 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025594 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025595 (varnumber_T)lastline);
25596 for (i = 0; i < argcount; ++i)
25597 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025598 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025599 if (ai < 0)
25600 /* named argument a:name */
25601 name = FUNCARG(fp, i);
25602 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025603 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025604 /* "..." argument a:1, a:2, etc. */
25605 sprintf((char *)numbuf, "%d", ai + 1);
25606 name = numbuf;
25607 }
25608 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25609 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025610 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025611 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25612 }
25613 else
25614 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025615 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25616 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025617 if (v == NULL)
25618 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025619 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025620 }
25621 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025622 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025623
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025624 /* Note: the values are copied directly to avoid alloc/free.
25625 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025626 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025627 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025628
25629 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25630 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025631 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25632 fc->l_listitems[ai].li_tv = argvars[i];
25633 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025634 }
25635 }
25636
Bram Moolenaar071d4272004-06-13 20:20:40 +000025637 /* Don't redraw while executing the function. */
25638 ++RedrawingDisabled;
25639 save_sourcing_name = sourcing_name;
25640 save_sourcing_lnum = sourcing_lnum;
25641 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025642 /* need space for function name + ("function " + 3) or "[number]" */
25643 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25644 + STRLEN(fp->uf_name) + 20;
25645 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025646 if (sourcing_name != NULL)
25647 {
25648 if (save_sourcing_name != NULL
25649 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025650 sprintf((char *)sourcing_name, "%s[%d]..",
25651 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 else
25653 STRCPY(sourcing_name, "function ");
25654 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25655
25656 if (p_verbose >= 12)
25657 {
25658 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025659 verbose_enter_scroll();
25660
Bram Moolenaar555b2802005-05-19 21:08:39 +000025661 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025662 if (p_verbose >= 14)
25663 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025665 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025666 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025667 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025668
25669 msg_puts((char_u *)"(");
25670 for (i = 0; i < argcount; ++i)
25671 {
25672 if (i > 0)
25673 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025674 if (argvars[i].v_type == VAR_NUMBER)
25675 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676 else
25677 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025678 /* Do not want errors such as E724 here. */
25679 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025680 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025681 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025682 if (s != NULL)
25683 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025684 if (vim_strsize(s) > MSG_BUF_CLEN)
25685 {
25686 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25687 s = buf;
25688 }
25689 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025690 vim_free(tofree);
25691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692 }
25693 }
25694 msg_puts((char_u *)")");
25695 }
25696 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025697
25698 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025699 --no_wait_return;
25700 }
25701 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025702#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025703 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025704 {
25705 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25706 func_do_profile(fp);
25707 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025708 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025709 {
25710 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025711 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025712 profile_zero(&fp->uf_tm_children);
25713 }
25714 script_prof_save(&wait_start);
25715 }
25716#endif
25717
Bram Moolenaar071d4272004-06-13 20:20:40 +000025718 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025719 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720 save_did_emsg = did_emsg;
25721 did_emsg = FALSE;
25722
25723 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025724 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025725 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25726
25727 --RedrawingDisabled;
25728
25729 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025730 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025732 clear_tv(rettv);
25733 rettv->v_type = VAR_NUMBER;
25734 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025735 }
25736
Bram Moolenaar05159a02005-02-26 23:04:13 +000025737#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025738 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025739 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025740 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025741 profile_end(&call_start);
25742 profile_sub_wait(&wait_start, &call_start);
25743 profile_add(&fp->uf_tm_total, &call_start);
25744 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025745 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025746 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025747 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25748 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025749 }
25750 }
25751#endif
25752
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753 /* when being verbose, mention the return value */
25754 if (p_verbose >= 12)
25755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025757 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758
Bram Moolenaar071d4272004-06-13 20:20:40 +000025759 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025760 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025761 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025762 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025763 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025764 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025765 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025766 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025767 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025768 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025769 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025770
Bram Moolenaar555b2802005-05-19 21:08:39 +000025771 /* The value may be very long. Skip the middle part, so that we
25772 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025773 * truncate it at the end. Don't want errors such as E724 here. */
25774 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025775 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025776 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025777 if (s != NULL)
25778 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025779 if (vim_strsize(s) > MSG_BUF_CLEN)
25780 {
25781 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25782 s = buf;
25783 }
25784 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025785 vim_free(tofree);
25786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787 }
25788 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025789
25790 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791 --no_wait_return;
25792 }
25793
25794 vim_free(sourcing_name);
25795 sourcing_name = save_sourcing_name;
25796 sourcing_lnum = save_sourcing_lnum;
25797 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025798#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025799 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025800 script_prof_restore(&wait_start);
25801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802
25803 if (p_verbose >= 12 && sourcing_name != NULL)
25804 {
25805 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025806 verbose_enter_scroll();
25807
Bram Moolenaar555b2802005-05-19 21:08:39 +000025808 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025810
25811 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812 --no_wait_return;
25813 }
25814
25815 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025816 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025817 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025818
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025819 /* If the a:000 list and the l: and a: dicts are not referenced we can
25820 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025821 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25822 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25823 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25824 {
25825 free_funccal(fc, FALSE);
25826 }
25827 else
25828 {
25829 hashitem_T *hi;
25830 listitem_T *li;
25831 int todo;
25832
25833 /* "fc" is still in use. This can happen when returning "a:000" or
25834 * assigning "l:" to a global variable.
25835 * Link "fc" in the list for garbage collection later. */
25836 fc->caller = previous_funccal;
25837 previous_funccal = fc;
25838
25839 /* Make a copy of the a: variables, since we didn't do that above. */
25840 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25841 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25842 {
25843 if (!HASHITEM_EMPTY(hi))
25844 {
25845 --todo;
25846 v = HI2DI(hi);
25847 copy_tv(&v->di_tv, &v->di_tv);
25848 }
25849 }
25850
25851 /* Make a copy of the a:000 items, since we didn't do that above. */
25852 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25853 copy_tv(&li->li_tv, &li->li_tv);
25854 }
25855}
25856
25857/*
25858 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025859 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025860 */
25861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025862can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025863{
25864 return (fc->l_varlist.lv_copyID != copyID
25865 && fc->l_vars.dv_copyID != copyID
25866 && fc->l_avars.dv_copyID != copyID);
25867}
25868
25869/*
25870 * Free "fc" and what it contains.
25871 */
25872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025873free_funccal(
25874 funccall_T *fc,
25875 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025876{
25877 listitem_T *li;
25878
25879 /* The a: variables typevals may not have been allocated, only free the
25880 * allocated variables. */
25881 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25882
25883 /* free all l: variables */
25884 vars_clear(&fc->l_vars.dv_hashtab);
25885
25886 /* Free the a:000 variables if they were allocated. */
25887 if (free_val)
25888 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25889 clear_tv(&li->li_tv);
25890
25891 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892}
25893
25894/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025895 * Add a number variable "name" to dict "dp" with value "nr".
25896 */
25897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025898add_nr_var(
25899 dict_T *dp,
25900 dictitem_T *v,
25901 char *name,
25902 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025903{
25904 STRCPY(v->di_key, name);
25905 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25906 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25907 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025908 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025909 v->di_tv.vval.v_number = nr;
25910}
25911
25912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 * ":return [expr]"
25914 */
25915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025916ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025917{
25918 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025919 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025920 int returning = FALSE;
25921
25922 if (current_funccal == NULL)
25923 {
25924 EMSG(_("E133: :return not inside a function"));
25925 return;
25926 }
25927
25928 if (eap->skip)
25929 ++emsg_skip;
25930
25931 eap->nextcmd = NULL;
25932 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025933 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934 {
25935 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025936 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025938 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939 }
25940 /* It's safer to return also on error. */
25941 else if (!eap->skip)
25942 {
25943 /*
25944 * Return unless the expression evaluation has been cancelled due to an
25945 * aborting error, an interrupt, or an exception.
25946 */
25947 if (!aborting())
25948 returning = do_return(eap, FALSE, TRUE, NULL);
25949 }
25950
25951 /* When skipping or the return gets pending, advance to the next command
25952 * in this line (!returning). Otherwise, ignore the rest of the line.
25953 * Following lines will be ignored by get_func_line(). */
25954 if (returning)
25955 eap->nextcmd = NULL;
25956 else if (eap->nextcmd == NULL) /* no argument */
25957 eap->nextcmd = check_nextcmd(arg);
25958
25959 if (eap->skip)
25960 --emsg_skip;
25961}
25962
25963/*
25964 * Return from a function. Possibly makes the return pending. Also called
25965 * for a pending return at the ":endtry" or after returning from an extra
25966 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025967 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025968 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969 * FALSE when the return gets pending.
25970 */
25971 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025972do_return(
25973 exarg_T *eap,
25974 int reanimate,
25975 int is_cmd,
25976 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977{
25978 int idx;
25979 struct condstack *cstack = eap->cstack;
25980
25981 if (reanimate)
25982 /* Undo the return. */
25983 current_funccal->returned = FALSE;
25984
25985 /*
25986 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25987 * not in its finally clause (which then is to be executed next) is found.
25988 * In this case, make the ":return" pending for execution at the ":endtry".
25989 * Otherwise, return normally.
25990 */
25991 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25992 if (idx >= 0)
25993 {
25994 cstack->cs_pending[idx] = CSTP_RETURN;
25995
25996 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025997 /* A pending return again gets pending. "rettv" points to an
25998 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026000 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026001 else
26002 {
26003 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026004 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026005 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026006 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026008 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026009 {
26010 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026011 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026012 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013 else
26014 EMSG(_(e_outofmem));
26015 }
26016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026017 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018
26019 if (reanimate)
26020 {
26021 /* The pending return value could be overwritten by a ":return"
26022 * without argument in a finally clause; reset the default
26023 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026024 current_funccal->rettv->v_type = VAR_NUMBER;
26025 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026 }
26027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026028 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026029 }
26030 else
26031 {
26032 current_funccal->returned = TRUE;
26033
26034 /* If the return is carried out now, store the return value. For
26035 * a return immediately after reanimation, the value is already
26036 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026037 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026039 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026040 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026041 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026042 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043 }
26044 }
26045
26046 return idx < 0;
26047}
26048
26049/*
26050 * Free the variable with a pending return value.
26051 */
26052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026053discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026054{
Bram Moolenaar33570922005-01-25 22:26:29 +000026055 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026056}
26057
26058/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026059 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060 * is an allocated string. Used by report_pending() for verbose messages.
26061 */
26062 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026063get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026064{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026065 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026066 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026067 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026069 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026070 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026071 if (s == NULL)
26072 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026073
26074 STRCPY(IObuff, ":return ");
26075 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26076 if (STRLEN(s) + 8 >= IOSIZE)
26077 STRCPY(IObuff + IOSIZE - 4, "...");
26078 vim_free(tofree);
26079 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026080}
26081
26082/*
26083 * Get next function line.
26084 * Called by do_cmdline() to get the next line.
26085 * Returns allocated string, or NULL for end of function.
26086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026087 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026088get_func_line(
26089 int c UNUSED,
26090 void *cookie,
26091 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026092{
Bram Moolenaar33570922005-01-25 22:26:29 +000026093 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026094 ufunc_T *fp = fcp->func;
26095 char_u *retval;
26096 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026097
26098 /* If breakpoints have been added/deleted need to check for it. */
26099 if (fcp->dbg_tick != debug_tick)
26100 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026101 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102 sourcing_lnum);
26103 fcp->dbg_tick = debug_tick;
26104 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026105#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026106 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026107 func_line_end(cookie);
26108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026109
Bram Moolenaar05159a02005-02-26 23:04:13 +000026110 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026111 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26112 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026113 retval = NULL;
26114 else
26115 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026116 /* Skip NULL lines (continuation lines). */
26117 while (fcp->linenr < gap->ga_len
26118 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26119 ++fcp->linenr;
26120 if (fcp->linenr >= gap->ga_len)
26121 retval = NULL;
26122 else
26123 {
26124 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26125 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026126#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026127 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026128 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026129#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131 }
26132
26133 /* Did we encounter a breakpoint? */
26134 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26135 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026136 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026138 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026139 sourcing_lnum);
26140 fcp->dbg_tick = debug_tick;
26141 }
26142
26143 return retval;
26144}
26145
Bram Moolenaar05159a02005-02-26 23:04:13 +000026146#if defined(FEAT_PROFILE) || defined(PROTO)
26147/*
26148 * Called when starting to read a function line.
26149 * "sourcing_lnum" must be correct!
26150 * When skipping lines it may not actually be executed, but we won't find out
26151 * until later and we need to store the time now.
26152 */
26153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026154func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026155{
26156 funccall_T *fcp = (funccall_T *)cookie;
26157 ufunc_T *fp = fcp->func;
26158
26159 if (fp->uf_profiling && sourcing_lnum >= 1
26160 && sourcing_lnum <= fp->uf_lines.ga_len)
26161 {
26162 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026163 /* Skip continuation lines. */
26164 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26165 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026166 fp->uf_tml_execed = FALSE;
26167 profile_start(&fp->uf_tml_start);
26168 profile_zero(&fp->uf_tml_children);
26169 profile_get_wait(&fp->uf_tml_wait);
26170 }
26171}
26172
26173/*
26174 * Called when actually executing a function line.
26175 */
26176 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026177func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026178{
26179 funccall_T *fcp = (funccall_T *)cookie;
26180 ufunc_T *fp = fcp->func;
26181
26182 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26183 fp->uf_tml_execed = TRUE;
26184}
26185
26186/*
26187 * Called when done with a function line.
26188 */
26189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026190func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026191{
26192 funccall_T *fcp = (funccall_T *)cookie;
26193 ufunc_T *fp = fcp->func;
26194
26195 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26196 {
26197 if (fp->uf_tml_execed)
26198 {
26199 ++fp->uf_tml_count[fp->uf_tml_idx];
26200 profile_end(&fp->uf_tml_start);
26201 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026202 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026203 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26204 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026205 }
26206 fp->uf_tml_idx = -1;
26207 }
26208}
26209#endif
26210
Bram Moolenaar071d4272004-06-13 20:20:40 +000026211/*
26212 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026213 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026214 */
26215 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026216func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026217{
Bram Moolenaar33570922005-01-25 22:26:29 +000026218 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026219
26220 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26221 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026222 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026223 || fcp->returned);
26224}
26225
26226/*
26227 * return TRUE if cookie indicates a function which "abort"s on errors.
26228 */
26229 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026230func_has_abort(
26231 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026232{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026233 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026234}
26235
26236#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26237typedef enum
26238{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026239 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26240 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26241 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026242} var_flavour_T;
26243
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026244static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026245
26246 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026247var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026248{
26249 char_u *p = varname;
26250
26251 if (ASCII_ISUPPER(*p))
26252 {
26253 while (*(++p))
26254 if (ASCII_ISLOWER(*p))
26255 return VAR_FLAVOUR_SESSION;
26256 return VAR_FLAVOUR_VIMINFO;
26257 }
26258 else
26259 return VAR_FLAVOUR_DEFAULT;
26260}
26261#endif
26262
26263#if defined(FEAT_VIMINFO) || defined(PROTO)
26264/*
26265 * Restore global vars that start with a capital from the viminfo file
26266 */
26267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026268read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269{
26270 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026271 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026272 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026273 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026274
26275 if (!writing && (find_viminfo_parameter('!') != NULL))
26276 {
26277 tab = vim_strchr(virp->vir_line + 1, '\t');
26278 if (tab != NULL)
26279 {
26280 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026281 switch (*tab)
26282 {
26283 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026284#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026285 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026286#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026287 case 'D': type = VAR_DICT; break;
26288 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026289 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026291
26292 tab = vim_strchr(tab, '\t');
26293 if (tab != NULL)
26294 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026295 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026296 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026297 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026298 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026299#ifdef FEAT_FLOAT
26300 else if (type == VAR_FLOAT)
26301 (void)string2float(tab + 1, &tv.vval.v_float);
26302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026303 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026304 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026305 if (type == VAR_DICT || type == VAR_LIST)
26306 {
26307 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26308
26309 if (etv == NULL)
26310 /* Failed to parse back the dict or list, use it as a
26311 * string. */
26312 tv.v_type = VAR_STRING;
26313 else
26314 {
26315 vim_free(tv.vval.v_string);
26316 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026317 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026318 }
26319 }
26320
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026321 /* when in a function use global variables */
26322 save_funccal = current_funccal;
26323 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026324 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026325 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026326
26327 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026328 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026329 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26330 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026331 }
26332 }
26333 }
26334
26335 return viminfo_readline(virp);
26336}
26337
26338/*
26339 * Write global vars that start with a capital to the viminfo file
26340 */
26341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026342write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026343{
Bram Moolenaar33570922005-01-25 22:26:29 +000026344 hashitem_T *hi;
26345 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026346 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026347 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026348 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026349 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026350 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026351
26352 if (find_viminfo_parameter('!') == NULL)
26353 return;
26354
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026355 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026356
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026357 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026358 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026359 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026360 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026361 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026362 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026363 this_var = HI2DI(hi);
26364 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026365 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026366 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026367 {
26368 case VAR_STRING: s = "STR"; break;
26369 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026370 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026371 case VAR_DICT: s = "DIC"; break;
26372 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026373 case VAR_SPECIAL: s = "XPL"; break;
26374
26375 case VAR_UNKNOWN:
26376 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026377 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026378 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026379 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026380 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026381 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026382 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026383 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026384 if (p != NULL)
26385 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026386 vim_free(tofree);
26387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026388 }
26389 }
26390}
26391#endif
26392
26393#if defined(FEAT_SESSION) || defined(PROTO)
26394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026395store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026396{
Bram Moolenaar33570922005-01-25 22:26:29 +000026397 hashitem_T *hi;
26398 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026399 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400 char_u *p, *t;
26401
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026402 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026403 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026404 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026405 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026406 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026407 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026408 this_var = HI2DI(hi);
26409 if ((this_var->di_tv.v_type == VAR_NUMBER
26410 || this_var->di_tv.v_type == VAR_STRING)
26411 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026412 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026413 /* Escape special characters with a backslash. Turn a LF and
26414 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026415 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026416 (char_u *)"\\\"\n\r");
26417 if (p == NULL) /* out of memory */
26418 break;
26419 for (t = p; *t != NUL; ++t)
26420 if (*t == '\n')
26421 *t = 'n';
26422 else if (*t == '\r')
26423 *t = 'r';
26424 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026425 this_var->di_key,
26426 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26427 : ' ',
26428 p,
26429 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26430 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026431 || put_eol(fd) == FAIL)
26432 {
26433 vim_free(p);
26434 return FAIL;
26435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026436 vim_free(p);
26437 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026438#ifdef FEAT_FLOAT
26439 else if (this_var->di_tv.v_type == VAR_FLOAT
26440 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26441 {
26442 float_T f = this_var->di_tv.vval.v_float;
26443 int sign = ' ';
26444
26445 if (f < 0)
26446 {
26447 f = -f;
26448 sign = '-';
26449 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026450 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026451 this_var->di_key, sign, f) < 0)
26452 || put_eol(fd) == FAIL)
26453 return FAIL;
26454 }
26455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026456 }
26457 }
26458 return OK;
26459}
26460#endif
26461
Bram Moolenaar661b1822005-07-28 22:36:45 +000026462/*
26463 * Display script name where an item was last set.
26464 * Should only be invoked when 'verbose' is non-zero.
26465 */
26466 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026467last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026468{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026469 char_u *p;
26470
Bram Moolenaar661b1822005-07-28 22:36:45 +000026471 if (scriptID != 0)
26472 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026473 p = home_replace_save(NULL, get_scriptname(scriptID));
26474 if (p != NULL)
26475 {
26476 verbose_enter();
26477 MSG_PUTS(_("\n\tLast set from "));
26478 MSG_PUTS(p);
26479 vim_free(p);
26480 verbose_leave();
26481 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026482 }
26483}
26484
Bram Moolenaard812df62008-11-09 12:46:09 +000026485/*
26486 * List v:oldfiles in a nice way.
26487 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026489ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026490{
26491 list_T *l = vimvars[VV_OLDFILES].vv_list;
26492 listitem_T *li;
26493 int nr = 0;
26494
26495 if (l == NULL)
26496 msg((char_u *)_("No old files"));
26497 else
26498 {
26499 msg_start();
26500 msg_scroll = TRUE;
26501 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26502 {
26503 msg_outnum((long)++nr);
26504 MSG_PUTS(": ");
26505 msg_outtrans(get_tv_string(&li->li_tv));
26506 msg_putchar('\n');
26507 out_flush(); /* output one line at a time */
26508 ui_breakcheck();
26509 }
26510 /* Assume "got_int" was set to truncate the listing. */
26511 got_int = FALSE;
26512
26513#ifdef FEAT_BROWSE_CMD
26514 if (cmdmod.browse)
26515 {
26516 quit_more = FALSE;
26517 nr = prompt_for_number(FALSE);
26518 msg_starthere();
26519 if (nr > 0)
26520 {
26521 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26522 (long)nr);
26523
26524 if (p != NULL)
26525 {
26526 p = expand_env_save(p);
26527 eap->arg = p;
26528 eap->cmdidx = CMD_edit;
26529 cmdmod.browse = FALSE;
26530 do_exedit(eap, NULL);
26531 vim_free(p);
26532 }
26533 }
26534 }
26535#endif
26536 }
26537}
26538
Bram Moolenaar53744302015-07-17 17:38:22 +020026539/* reset v:option_new, v:option_old and v:option_type */
26540 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026541reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026542{
26543 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26544 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26545 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26546}
26547
26548
Bram Moolenaar071d4272004-06-13 20:20:40 +000026549#endif /* FEAT_EVAL */
26550
Bram Moolenaar071d4272004-06-13 20:20:40 +000026551
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026552#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026553
26554#ifdef WIN3264
26555/*
26556 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26557 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026558static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26559static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26560static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026561
26562/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026563 * Get the short path (8.3) for the filename in "fnamep".
26564 * Only works for a valid file name.
26565 * When the path gets longer "fnamep" is changed and the allocated buffer
26566 * is put in "bufp".
26567 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26568 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026569 */
26570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026571get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026572{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026573 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026574 char_u *newbuf;
26575
26576 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026577 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026578 if (l > len - 1)
26579 {
26580 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026581 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026582 newbuf = vim_strnsave(*fnamep, l);
26583 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026585
26586 vim_free(*bufp);
26587 *fnamep = *bufp = newbuf;
26588
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026589 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026590 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026591 }
26592
26593 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026594 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026595}
26596
26597/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026598 * Get the short path (8.3) for the filename in "fname". The converted
26599 * path is returned in "bufp".
26600 *
26601 * Some of the directories specified in "fname" may not exist. This function
26602 * will shorten the existing directories at the beginning of the path and then
26603 * append the remaining non-existing path.
26604 *
26605 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026606 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026607 * bufp - Pointer to an allocated buffer for the filename.
26608 * fnamelen - Length of the filename pointed to by fname
26609 *
26610 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026611 */
26612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026613shortpath_for_invalid_fname(
26614 char_u **fname,
26615 char_u **bufp,
26616 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026617{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026618 char_u *short_fname, *save_fname, *pbuf_unused;
26619 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026620 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026621 int old_len, len;
26622 int new_len, sfx_len;
26623 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026624
26625 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026626 old_len = *fnamelen;
26627 save_fname = vim_strnsave(*fname, old_len);
26628 pbuf_unused = NULL;
26629 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026630
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026631 endp = save_fname + old_len - 1; /* Find the end of the copy */
26632 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026633
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026634 /*
26635 * Try shortening the supplied path till it succeeds by removing one
26636 * directory at a time from the tail of the path.
26637 */
26638 len = 0;
26639 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026640 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026641 /* go back one path-separator */
26642 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26643 --endp;
26644 if (endp <= save_fname)
26645 break; /* processed the complete path */
26646
26647 /*
26648 * Replace the path separator with a NUL and try to shorten the
26649 * resulting path.
26650 */
26651 ch = *endp;
26652 *endp = 0;
26653 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026654 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026655 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26656 {
26657 retval = FAIL;
26658 goto theend;
26659 }
26660 *endp = ch; /* preserve the string */
26661
26662 if (len > 0)
26663 break; /* successfully shortened the path */
26664
26665 /* failed to shorten the path. Skip the path separator */
26666 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026667 }
26668
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026669 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026670 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026671 /*
26672 * Succeeded in shortening the path. Now concatenate the shortened
26673 * path with the remaining path at the tail.
26674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026675
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026676 /* Compute the length of the new path. */
26677 sfx_len = (int)(save_endp - endp) + 1;
26678 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026679
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026680 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026681 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026682 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026683 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026684 /* There is not enough space in the currently allocated string,
26685 * copy it to a buffer big enough. */
26686 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026687 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026688 {
26689 retval = FAIL;
26690 goto theend;
26691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026692 }
26693 else
26694 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026695 /* Transfer short_fname to the main buffer (it's big enough),
26696 * unless get_short_pathname() did its work in-place. */
26697 *fname = *bufp = save_fname;
26698 if (short_fname != save_fname)
26699 vim_strncpy(save_fname, short_fname, len);
26700 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026701 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026702
26703 /* concat the not-shortened part of the path */
26704 vim_strncpy(*fname + len, endp, sfx_len);
26705 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026706 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026707
26708theend:
26709 vim_free(pbuf_unused);
26710 vim_free(save_fname);
26711
26712 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026713}
26714
26715/*
26716 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026717 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026718 */
26719 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026720shortpath_for_partial(
26721 char_u **fnamep,
26722 char_u **bufp,
26723 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026724{
26725 int sepcount, len, tflen;
26726 char_u *p;
26727 char_u *pbuf, *tfname;
26728 int hasTilde;
26729
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026730 /* Count up the path separators from the RHS.. so we know which part
26731 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026732 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026733 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026734 if (vim_ispathsep(*p))
26735 ++sepcount;
26736
26737 /* Need full path first (use expand_env() to remove a "~/") */
26738 hasTilde = (**fnamep == '~');
26739 if (hasTilde)
26740 pbuf = tfname = expand_env_save(*fnamep);
26741 else
26742 pbuf = tfname = FullName_save(*fnamep, FALSE);
26743
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026744 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026745
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026746 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26747 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026748
26749 if (len == 0)
26750 {
26751 /* Don't have a valid filename, so shorten the rest of the
26752 * path if we can. This CAN give us invalid 8.3 filenames, but
26753 * there's not a lot of point in guessing what it might be.
26754 */
26755 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026756 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26757 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026758 }
26759
26760 /* Count the paths backward to find the beginning of the desired string. */
26761 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026762 {
26763#ifdef FEAT_MBYTE
26764 if (has_mbyte)
26765 p -= mb_head_off(tfname, p);
26766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026767 if (vim_ispathsep(*p))
26768 {
26769 if (sepcount == 0 || (hasTilde && sepcount == 1))
26770 break;
26771 else
26772 sepcount --;
26773 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026775 if (hasTilde)
26776 {
26777 --p;
26778 if (p >= tfname)
26779 *p = '~';
26780 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026781 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026782 }
26783 else
26784 ++p;
26785
26786 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26787 vim_free(*bufp);
26788 *fnamelen = (int)STRLEN(p);
26789 *bufp = pbuf;
26790 *fnamep = p;
26791
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026792 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026793}
26794#endif /* WIN3264 */
26795
26796/*
26797 * Adjust a filename, according to a string of modifiers.
26798 * *fnamep must be NUL terminated when called. When returning, the length is
26799 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026800 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026801 * When there is an error, *fnamep is set to NULL.
26802 */
26803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026804modify_fname(
26805 char_u *src, /* string with modifiers */
26806 int *usedlen, /* characters after src that are used */
26807 char_u **fnamep, /* file name so far */
26808 char_u **bufp, /* buffer for allocated file name or NULL */
26809 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026810{
26811 int valid = 0;
26812 char_u *tail;
26813 char_u *s, *p, *pbuf;
26814 char_u dirname[MAXPATHL];
26815 int c;
26816 int has_fullname = 0;
26817#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026818 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026819 int has_shortname = 0;
26820#endif
26821
26822repeat:
26823 /* ":p" - full path/file_name */
26824 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26825 {
26826 has_fullname = 1;
26827
26828 valid |= VALID_PATH;
26829 *usedlen += 2;
26830
26831 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26832 if ((*fnamep)[0] == '~'
26833#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26834 && ((*fnamep)[1] == '/'
26835# ifdef BACKSLASH_IN_FILENAME
26836 || (*fnamep)[1] == '\\'
26837# endif
26838 || (*fnamep)[1] == NUL)
26839
26840#endif
26841 )
26842 {
26843 *fnamep = expand_env_save(*fnamep);
26844 vim_free(*bufp); /* free any allocated file name */
26845 *bufp = *fnamep;
26846 if (*fnamep == NULL)
26847 return -1;
26848 }
26849
26850 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026851 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026852 {
26853 if (vim_ispathsep(*p)
26854 && p[1] == '.'
26855 && (p[2] == NUL
26856 || vim_ispathsep(p[2])
26857 || (p[2] == '.'
26858 && (p[3] == NUL || vim_ispathsep(p[3])))))
26859 break;
26860 }
26861
26862 /* FullName_save() is slow, don't use it when not needed. */
26863 if (*p != NUL || !vim_isAbsName(*fnamep))
26864 {
26865 *fnamep = FullName_save(*fnamep, *p != NUL);
26866 vim_free(*bufp); /* free any allocated file name */
26867 *bufp = *fnamep;
26868 if (*fnamep == NULL)
26869 return -1;
26870 }
26871
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026872#ifdef WIN3264
26873# if _WIN32_WINNT >= 0x0500
26874 if (vim_strchr(*fnamep, '~') != NULL)
26875 {
26876 /* Expand 8.3 filename to full path. Needed to make sure the same
26877 * file does not have two different names.
26878 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26879 p = alloc(_MAX_PATH + 1);
26880 if (p != NULL)
26881 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026882 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026883 {
26884 vim_free(*bufp);
26885 *bufp = *fnamep = p;
26886 }
26887 else
26888 vim_free(p);
26889 }
26890 }
26891# endif
26892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026893 /* Append a path separator to a directory. */
26894 if (mch_isdir(*fnamep))
26895 {
26896 /* Make room for one or two extra characters. */
26897 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26898 vim_free(*bufp); /* free any allocated file name */
26899 *bufp = *fnamep;
26900 if (*fnamep == NULL)
26901 return -1;
26902 add_pathsep(*fnamep);
26903 }
26904 }
26905
26906 /* ":." - path relative to the current directory */
26907 /* ":~" - path relative to the home directory */
26908 /* ":8" - shortname path - postponed till after */
26909 while (src[*usedlen] == ':'
26910 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26911 {
26912 *usedlen += 2;
26913 if (c == '8')
26914 {
26915#ifdef WIN3264
26916 has_shortname = 1; /* Postpone this. */
26917#endif
26918 continue;
26919 }
26920 pbuf = NULL;
26921 /* Need full path first (use expand_env() to remove a "~/") */
26922 if (!has_fullname)
26923 {
26924 if (c == '.' && **fnamep == '~')
26925 p = pbuf = expand_env_save(*fnamep);
26926 else
26927 p = pbuf = FullName_save(*fnamep, FALSE);
26928 }
26929 else
26930 p = *fnamep;
26931
26932 has_fullname = 0;
26933
26934 if (p != NULL)
26935 {
26936 if (c == '.')
26937 {
26938 mch_dirname(dirname, MAXPATHL);
26939 s = shorten_fname(p, dirname);
26940 if (s != NULL)
26941 {
26942 *fnamep = s;
26943 if (pbuf != NULL)
26944 {
26945 vim_free(*bufp); /* free any allocated file name */
26946 *bufp = pbuf;
26947 pbuf = NULL;
26948 }
26949 }
26950 }
26951 else
26952 {
26953 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26954 /* Only replace it when it starts with '~' */
26955 if (*dirname == '~')
26956 {
26957 s = vim_strsave(dirname);
26958 if (s != NULL)
26959 {
26960 *fnamep = s;
26961 vim_free(*bufp);
26962 *bufp = s;
26963 }
26964 }
26965 }
26966 vim_free(pbuf);
26967 }
26968 }
26969
26970 tail = gettail(*fnamep);
26971 *fnamelen = (int)STRLEN(*fnamep);
26972
26973 /* ":h" - head, remove "/file_name", can be repeated */
26974 /* Don't remove the first "/" or "c:\" */
26975 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26976 {
26977 valid |= VALID_HEAD;
26978 *usedlen += 2;
26979 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026980 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026981 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026982 *fnamelen = (int)(tail - *fnamep);
26983#ifdef VMS
26984 if (*fnamelen > 0)
26985 *fnamelen += 1; /* the path separator is part of the path */
26986#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026987 if (*fnamelen == 0)
26988 {
26989 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26990 p = vim_strsave((char_u *)".");
26991 if (p == NULL)
26992 return -1;
26993 vim_free(*bufp);
26994 *bufp = *fnamep = tail = p;
26995 *fnamelen = 1;
26996 }
26997 else
26998 {
26999 while (tail > s && !after_pathsep(s, tail))
27000 mb_ptr_back(*fnamep, tail);
27001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027002 }
27003
27004 /* ":8" - shortname */
27005 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27006 {
27007 *usedlen += 2;
27008#ifdef WIN3264
27009 has_shortname = 1;
27010#endif
27011 }
27012
27013#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027014 /*
27015 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027016 */
27017 if (has_shortname)
27018 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027019 /* Copy the string if it is shortened by :h and when it wasn't copied
27020 * yet, because we are going to change it in place. Avoids changing
27021 * the buffer name for "%:8". */
27022 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027023 {
27024 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027025 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027026 return -1;
27027 vim_free(*bufp);
27028 *bufp = *fnamep = p;
27029 }
27030
27031 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027032 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027033 if (!has_fullname && !vim_isAbsName(*fnamep))
27034 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027035 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027036 return -1;
27037 }
27038 else
27039 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027040 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027041
Bram Moolenaardc935552011-08-17 15:23:23 +020027042 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027043 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027044 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027045 return -1;
27046
27047 if (l == 0)
27048 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027049 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027050 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027051 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027052 return -1;
27053 }
27054 *fnamelen = l;
27055 }
27056 }
27057#endif /* WIN3264 */
27058
27059 /* ":t" - tail, just the basename */
27060 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27061 {
27062 *usedlen += 2;
27063 *fnamelen -= (int)(tail - *fnamep);
27064 *fnamep = tail;
27065 }
27066
27067 /* ":e" - extension, can be repeated */
27068 /* ":r" - root, without extension, can be repeated */
27069 while (src[*usedlen] == ':'
27070 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27071 {
27072 /* find a '.' in the tail:
27073 * - for second :e: before the current fname
27074 * - otherwise: The last '.'
27075 */
27076 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27077 s = *fnamep - 2;
27078 else
27079 s = *fnamep + *fnamelen - 1;
27080 for ( ; s > tail; --s)
27081 if (s[0] == '.')
27082 break;
27083 if (src[*usedlen + 1] == 'e') /* :e */
27084 {
27085 if (s > tail)
27086 {
27087 *fnamelen += (int)(*fnamep - (s + 1));
27088 *fnamep = s + 1;
27089#ifdef VMS
27090 /* cut version from the extension */
27091 s = *fnamep + *fnamelen - 1;
27092 for ( ; s > *fnamep; --s)
27093 if (s[0] == ';')
27094 break;
27095 if (s > *fnamep)
27096 *fnamelen = s - *fnamep;
27097#endif
27098 }
27099 else if (*fnamep <= tail)
27100 *fnamelen = 0;
27101 }
27102 else /* :r */
27103 {
27104 if (s > tail) /* remove one extension */
27105 *fnamelen = (int)(s - *fnamep);
27106 }
27107 *usedlen += 2;
27108 }
27109
27110 /* ":s?pat?foo?" - substitute */
27111 /* ":gs?pat?foo?" - global substitute */
27112 if (src[*usedlen] == ':'
27113 && (src[*usedlen + 1] == 's'
27114 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27115 {
27116 char_u *str;
27117 char_u *pat;
27118 char_u *sub;
27119 int sep;
27120 char_u *flags;
27121 int didit = FALSE;
27122
27123 flags = (char_u *)"";
27124 s = src + *usedlen + 2;
27125 if (src[*usedlen + 1] == 'g')
27126 {
27127 flags = (char_u *)"g";
27128 ++s;
27129 }
27130
27131 sep = *s++;
27132 if (sep)
27133 {
27134 /* find end of pattern */
27135 p = vim_strchr(s, sep);
27136 if (p != NULL)
27137 {
27138 pat = vim_strnsave(s, (int)(p - s));
27139 if (pat != NULL)
27140 {
27141 s = p + 1;
27142 /* find end of substitution */
27143 p = vim_strchr(s, sep);
27144 if (p != NULL)
27145 {
27146 sub = vim_strnsave(s, (int)(p - s));
27147 str = vim_strnsave(*fnamep, *fnamelen);
27148 if (sub != NULL && str != NULL)
27149 {
27150 *usedlen = (int)(p + 1 - src);
27151 s = do_string_sub(str, pat, sub, flags);
27152 if (s != NULL)
27153 {
27154 *fnamep = s;
27155 *fnamelen = (int)STRLEN(s);
27156 vim_free(*bufp);
27157 *bufp = s;
27158 didit = TRUE;
27159 }
27160 }
27161 vim_free(sub);
27162 vim_free(str);
27163 }
27164 vim_free(pat);
27165 }
27166 }
27167 /* after using ":s", repeat all the modifiers */
27168 if (didit)
27169 goto repeat;
27170 }
27171 }
27172
Bram Moolenaar26df0922014-02-23 23:39:13 +010027173 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27174 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027175 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027176 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027177 if (c != NUL)
27178 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027179 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027180 if (c != NUL)
27181 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027182 if (p == NULL)
27183 return -1;
27184 vim_free(*bufp);
27185 *bufp = *fnamep = p;
27186 *fnamelen = (int)STRLEN(p);
27187 *usedlen += 2;
27188 }
27189
Bram Moolenaar071d4272004-06-13 20:20:40 +000027190 return valid;
27191}
27192
27193/*
27194 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27195 * "flags" can be "g" to do a global substitute.
27196 * Returns an allocated string, NULL for error.
27197 */
27198 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027199do_string_sub(
27200 char_u *str,
27201 char_u *pat,
27202 char_u *sub,
27203 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027204{
27205 int sublen;
27206 regmatch_T regmatch;
27207 int i;
27208 int do_all;
27209 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027210 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027211 garray_T ga;
27212 char_u *ret;
27213 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027214 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027215
27216 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27217 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027218 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027219
27220 ga_init2(&ga, 1, 200);
27221
27222 do_all = (flags[0] == 'g');
27223
27224 regmatch.rm_ic = p_ic;
27225 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27226 if (regmatch.regprog != NULL)
27227 {
27228 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027229 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027230 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27231 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027232 /* Skip empty match except for first match. */
27233 if (regmatch.startp[0] == regmatch.endp[0])
27234 {
27235 if (zero_width == regmatch.startp[0])
27236 {
27237 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027238 i = MB_PTR2LEN(tail);
27239 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27240 (size_t)i);
27241 ga.ga_len += i;
27242 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027243 continue;
27244 }
27245 zero_width = regmatch.startp[0];
27246 }
27247
Bram Moolenaar071d4272004-06-13 20:20:40 +000027248 /*
27249 * Get some space for a temporary buffer to do the substitution
27250 * into. It will contain:
27251 * - The text up to where the match is.
27252 * - The substituted text.
27253 * - The text after the match.
27254 */
27255 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027256 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027257 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27258 {
27259 ga_clear(&ga);
27260 break;
27261 }
27262
27263 /* copy the text up to where the match is */
27264 i = (int)(regmatch.startp[0] - tail);
27265 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27266 /* add the substituted text */
27267 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27268 + ga.ga_len + i, TRUE, TRUE, FALSE);
27269 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027270 tail = regmatch.endp[0];
27271 if (*tail == NUL)
27272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027273 if (!do_all)
27274 break;
27275 }
27276
27277 if (ga.ga_data != NULL)
27278 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27279
Bram Moolenaar473de612013-06-08 18:19:48 +020027280 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027281 }
27282
27283 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27284 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027285 if (p_cpo == empty_option)
27286 p_cpo = save_cpo;
27287 else
27288 /* Darn, evaluating {sub} expression changed the value. */
27289 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027290
27291 return ret;
27292}
27293
27294#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */